update parser to fit LTTV
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
CommitLineData
449cb9d7 1/* This file is part of the Linux Trace Toolkit viewer
3aee1200 2 * Copyright (C) 2005 Mathieu Desnoyers
449cb9d7 3 *
3aee1200 4 * Complete rewrite from the original version made by XangXiu Yang.
5 *
449cb9d7 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation;
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
19 */
20
4e4d11b3 21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
6cd62ccf 25#include <stdio.h>
26#include <fcntl.h>
8d1e6362 27#include <string.h>
28#include <dirent.h>
6cd62ccf 29#include <sys/stat.h>
30#include <sys/types.h>
8d1e6362 31#include <errno.h>
32#include <unistd.h>
42db9bf1 33#include <math.h>
cdf90f40 34#include <glib.h>
3aee1200 35#include <malloc.h>
36#include <sys/mman.h>
6cd62ccf 37
ef35d837 38// For realpath
39#include <limits.h>
40#include <stdlib.h>
41
42
6cd62ccf 43#include "parser.h"
a5dcde2f 44#include <ltt/ltt.h>
45#include "ltt-private.h"
963b5f2d 46#include <ltt/trace.h>
a5dcde2f 47#include <ltt/facility.h>
c02ea99f 48#include <ltt/event.h>
8d1e6362 49#include <ltt/type.h>
1a2ceb63 50#include <ltt/ltt-types.h>
6cd62ccf 51
3aee1200 52
53/* Facility names used in this file */
54
55GQuark LTT_FACILITY_NAME_HEARTBEAT,
56 LTT_EVENT_NAME_HEARTBEAT;
57GQuark LTT_TRACEFILE_NAME_FACILITIES;
58
86005ded 59#ifndef g_open
60#define g_open open
61#endif
62
63
51b5991e 64#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 65
a1062ddd 66#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
3aee1200 67
68#ifndef g_debug
a1062ddd 69#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
3aee1200 70#endif
a1062ddd 71
45e14832 72#define g_close close
8959a0c8 73
b77d1b57 74/* Those macros must be called from within a function where page_size is a known
75 * variable */
76#define PAGE_MASK (~(page_size-1))
77#define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
78
6cd62ccf 79/* set the offset of the fields belonging to the event,
80 need the information of the archecture */
f104d082 81//void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
eed2ef37 82//size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
6cd62ccf 83
3aee1200 84/* get the size of the field type according to
85 * The facility size information. */
f104d082 86#if 0
3aee1200 87static inline void preset_field_type_size(LttTracefile *tf,
88 LttEventType *event_type,
89 off_t offset_root, off_t offset_parent,
90 enum field_status *fixed_root, enum field_status *fixed_parent,
91 LttField *field);
f104d082 92#endif //0
6cd62ccf 93
3aee1200 94/* map a fixed size or a block information from the file (fd) */
95static gint map_block(LttTracefile * tf, guint block_num);
96
97/* calculate nsec per cycles for current block */
791dffa6 98#if 0
99static guint32 calc_nsecs_per_cycle(LttTracefile * t);
100static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
101#endif //0
6cd62ccf 102
3aee1200 103/* go to the next event */
104static int ltt_seek_next_event(LttTracefile *tf);
6cd62ccf 105
eed2ef37 106void ltt_update_event_size(LttTracefile *tf);
107
2312de30 108
109void precompute_offsets(LttTracefile *tf, LttEventType *event);
110
3aee1200 111#if 0
43da6a59 112/* Functions to parse system.xml file (using glib xml parser) */
51b5991e 113static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
43da6a59 114 const gchar *element_name,
115 const gchar **attribute_names,
116 const gchar **attribute_values,
117 gpointer user_data,
118 GError **error)
119{
120 int i=0;
121 LttSystemDescription* des = (LttSystemDescription* )user_data;
122 if(strcmp("system", element_name)){
2a74fbf4 123 *error = g_error_new(G_MARKUP_ERROR,
124 G_LOG_LEVEL_WARNING,
125 "This is not system.xml file");
126 return;
43da6a59 127 }
128
129 while(attribute_names[i]){
130 if(strcmp("node_name", attribute_names[i])==0){
131 des->node_name = g_strdup(attribute_values[i]);
132 }else if(strcmp("domainname", attribute_names[i])==0){
133 des->domain_name = g_strdup(attribute_values[i]);
134 }else if(strcmp("cpu", attribute_names[i])==0){
135 des->nb_cpu = atoi(attribute_values[i]);
136 }else if(strcmp("arch_size", attribute_names[i])==0){
137 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
138 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
139 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
140 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
141 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
142 }else if(strcmp("endian", attribute_names[i])==0){
143 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
144 des->endian = LTT_LITTLE_ENDIAN;
145 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
146 des->endian = LTT_BIG_ENDIAN;
147 }else if(strcmp("kernel_name", attribute_names[i])==0){
148 des->kernel_name = g_strdup(attribute_values[i]);
149 }else if(strcmp("kernel_release", attribute_names[i])==0){
150 des->kernel_release = g_strdup(attribute_values[i]);
151 }else if(strcmp("kernel_version", attribute_names[i])==0){
152 des->kernel_version = g_strdup(attribute_values[i]);
153 }else if(strcmp("machine", attribute_names[i])==0){
154 des->machine = g_strdup(attribute_values[i]);
155 }else if(strcmp("processor", attribute_names[i])==0){
156 des->processor = g_strdup(attribute_values[i]);
157 }else if(strcmp("hardware_platform", attribute_names[i])==0){
158 des->hardware_platform = g_strdup(attribute_values[i]);
159 }else if(strcmp("operating_system", attribute_names[i])==0){
160 des->operating_system = g_strdup(attribute_values[i]);
161 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
162 des->ltt_major_version = atoi(attribute_values[i]);
163 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
164 des->ltt_minor_version = atoi(attribute_values[i]);
165 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
166 des->ltt_block_size = atoi(attribute_values[i]);
167 }else{
2a74fbf4 168 *error = g_error_new(G_MARKUP_ERROR,
169 G_LOG_LEVEL_WARNING,
170 "Not a valid attribute");
171 return;
43da6a59 172 }
173 i++;
174 }
175}
176
51b5991e 177static void parser_characters (GMarkupParseContext __UNUSED__ *context,
43da6a59 178 const gchar *text,
51b5991e 179 gsize __UNUSED__ text_len,
43da6a59 180 gpointer user_data,
51b5991e 181 GError __UNUSED__ **error)
43da6a59 182{
183 LttSystemDescription* des = (LttSystemDescription* )user_data;
184 des->description = g_strdup(text);
185}
3aee1200 186#endif //0
f104d082 187
188
77175651 189LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
3aee1200 190 guint num)
191{
192 g_assert(num < t->facilities_by_num->len);
193
194 return &g_array_index(t->facilities_by_num, LttFacility, num);
195
196}
43da6a59 197
b56dcdf2 198guint ltt_trace_get_num_cpu(LttTrace *t)
199{
200 return t->num_cpu;
201}
202
43da6a59 203
b7576a11 204/* trace can be NULL
205 *
206 * Return value : 0 success, 1 bad tracefile
207 */
208int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
209{
210 guint32 *magic_number = (guint32*)header;
211 struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header;
212
213 if(*magic_number == LTT_MAGIC_NUMBER)
214 tf->reverse_bo = 0;
215 else if(*magic_number == LTT_REV_MAGIC_NUMBER)
216 tf->reverse_bo = 1;
217 else /* invalid magic number, bad tracefile ! */
218 return 1;
219
220 /* Get float byte order : might be different from int byte order
221 * (or is set to 0 if the trace has no float (kernel trace)) */
222 tf->float_word_order = any->float_word_order;
f104d082 223 tf->has_alignment = any->has_alignment;
b7576a11 224
225 if(t) {
226 t->arch_type = ltt_get_uint32(LTT_GET_BO(tf),
227 &any->arch_type);
228 t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf),
229 &any->arch_variant);
230 t->arch_size = any->arch_size;
231 t->ltt_major_version = any->major_version;
232 t->ltt_minor_version = any->minor_version;
233 t->flight_recorder = any->flight_recorder;
234 t->has_heartbeat = any->has_heartbeat;
b7576a11 235 t->has_tsc = any->has_tsc;
236 }
237
238
239 switch(any->major_version) {
240
241 case 0:
242 switch(any->minor_version) {
243 case 3:
244 {
823820eb 245 tf->buffer_header_size =
246 sizeof(struct ltt_block_start_header)
247 + sizeof(struct ltt_trace_header_0_3);
986e2a7c 248 g_warning("Unsupported trace version : %hhu.%hhu",
249 any->major_version, any->minor_version);
250 return 1;
251 }
252 break;
69183aa9 253 case 6:
e939e5b2 254 {
5a697565 255 struct ltt_trace_header_0_6 *vheader =
256 (struct ltt_trace_header_0_6 *)header;
e939e5b2 257 tf->buffer_header_size =
258 sizeof(struct ltt_block_start_header)
5a697565 259 + sizeof(struct ltt_trace_header_0_6);
e939e5b2 260 if(t) {
261 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
262 &vheader->start_freq);
263 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
264 &vheader->start_tsc);
265 t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
266 &vheader->start_monotonic);
267 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
268 &vheader->start_time_sec);
269 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
270 &vheader->start_time_usec);
271 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
272
273 t->start_time_from_tsc = ltt_time_from_uint64(
274 (double)t->start_tsc * 1000000.0 / (double)t->start_freq);
275 }
276 }
277 break;
b7576a11 278 default:
986e2a7c 279 g_warning("Unsupported trace version : %hhu.%hhu",
b7576a11 280 any->major_version, any->minor_version);
281 return 1;
282 }
283 break;
284
285 default:
286 g_warning("Unsupported trace version : %hhu.%hhu",
287 any->major_version, any->minor_version);
288 return 1;
289 }
290
291
292 return 0;
293}
294
295
296
6cd62ccf 297/*****************************************************************************
298 *Function name
963b5f2d 299 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 300 *Input params
963b5f2d 301 * t : the trace containing the tracefile
302 * fileName : path name of the trace file
3aee1200 303 * tf : the tracefile structure
6cd62ccf 304 *Return value
3aee1200 305 * : 0 for success, -1 otherwise.
6cd62ccf 306 ****************************************************************************/
307
3aee1200 308gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
6cd62ccf 309{
963b5f2d 310 struct stat lTDFStat; /* Trace data file status */
3aee1200 311 struct ltt_block_start_header *header;
b77d1b57 312 int page_size = getpagesize();
6cd62ccf 313
314 //open the file
d3d34f49 315 tf->long_name = g_quark_from_string(fileName);
963b5f2d 316 tf->trace = t;
3865ea09 317 tf->fd = open(fileName, O_RDONLY);
6cd62ccf 318 if(tf->fd < 0){
2a74fbf4 319 g_warning("Unable to open input data file %s\n", fileName);
3aee1200 320 goto end;
6cd62ccf 321 }
322
323 // Get the file's status
324 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 325 g_warning("Unable to get the status of the input data file %s\n", fileName);
3aee1200 326 goto close_file;
6cd62ccf 327 }
328
329 // Is the file large enough to contain a trace
823820eb 330 if(lTDFStat.st_size <
9ea7b94b 331 (off_t)(sizeof(struct ltt_block_start_header)
332 + sizeof(struct ltt_trace_header_any))){
8710c6c7 333 g_print("The input data file %s does not contain a trace\n", fileName);
3aee1200 334 goto close_file;
335 }
336
337 /* Temporarily map the buffer start header to get trace information */
338 /* Multiple of pages aligned head */
b77d1b57 339 tf->buffer.head = mmap(0,
9ea7b94b 340 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
341 + sizeof(struct ltt_trace_header_any)), PROT_READ,
3aee1200 342 MAP_PRIVATE, tf->fd, 0);
3865ea09 343 if(tf->buffer.head == MAP_FAILED) {
3aee1200 344 perror("Error in allocating memory for buffer of tracefile");
345 goto close_file;
6cd62ccf 346 }
3865ea09 347 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 348
349 header = (struct ltt_block_start_header*)tf->buffer.head;
6cd62ccf 350
51551c6f 351 if(parse_trace_header(header->trace, tf, NULL)) {
352 g_warning("parse_trace_header error");
353 goto unmap_file;
354 }
3aee1200 355
6cd62ccf 356 //store the size of the file
357 tf->file_size = lTDFStat.st_size;
f628823c 358 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
359 tf->num_blocks = tf->file_size / tf->buf_size;
360
361 if(munmap(tf->buffer.head,
9ea7b94b 362 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
363 + sizeof(struct ltt_trace_header_any)))) {
f628823c 364 g_warning("unmap size : %u\n",
9ea7b94b 365 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
366 + sizeof(struct ltt_trace_header_any)));
f628823c 367 perror("munmap error");
368 g_assert(0);
369 }
3aee1200 370 tf->buffer.head = NULL;
6cd62ccf 371
963b5f2d 372 //read the first block
3aee1200 373 if(map_block(tf,0)) {
374 perror("Cannot map block for tracefile");
375 goto close_file;
376 }
377
378 return 0;
6cd62ccf 379
3aee1200 380 /* Error */
381unmap_file:
f628823c 382 if(munmap(tf->buffer.head,
9ea7b94b 383 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
384 + sizeof(struct ltt_trace_header_any)))) {
f628823c 385 g_warning("unmap size : %u\n",
9ea7b94b 386 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
387 + sizeof(struct ltt_trace_header_any)));
f628823c 388 perror("munmap error");
389 g_assert(0);
390 }
3aee1200 391close_file:
3865ea09 392 close(tf->fd);
3aee1200 393end:
394 return -1;
6cd62ccf 395}
396
d3d34f49 397LttTrace *ltt_tracefile_get_trace(LttTracefile *tf)
398{
399 return tf->trace;
400}
401
3aee1200 402#if 0
6cd62ccf 403/*****************************************************************************
963b5f2d 404 *Open control and per cpu tracefiles
6cd62ccf 405 ****************************************************************************/
406
45e14832 407void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
6cd62ccf 408{
963b5f2d 409 LttTracefile * tf;
410 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 411 if(!tf) return;
963b5f2d 412 t->per_cpu_tracefile_number++;
413 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 414}
415
45e14832 416gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
6cd62ccf 417{
963b5f2d 418 LttTracefile * tf;
c02ea99f 419 LttEvent ev;
963b5f2d 420 LttFacility * f;
963b5f2d 421 void * pos;
422 FacilityLoad fLoad;
8d1e6362 423 unsigned int i;
963b5f2d 424
425 tf = ltt_tracefile_open(t,control_name);
2a74fbf4 426 if(!tf) {
427 g_warning("ltt_tracefile_open_control : bad file descriptor");
428 return -1;
429 }
963b5f2d 430 t->control_tracefile_number++;
431 g_ptr_array_add(t->control_tracefiles,tf);
432
433 //parse facilities tracefile to get base_id
542ceddd 434 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 435 while(1){
c02ea99f 436 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
40331ba8 437
c02ea99f 438 if(ev.event_id == TRACE_FACILITY_LOAD){
439 pos = ev.data;
45e14832 440 fLoad.name = (gchar*)pos;
47a166fc 441 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
cbd41522 442 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 443
444 for(i=0;i<t->facility_number;i++){
445 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
446 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
447 f->base_id = fLoad.base_code;
448 break;
449 }
450 }
2a74fbf4 451 if(i==t->facility_number) {
8d1e6362 452 g_warning("Facility: %s, checksum: %u is not found",
453 fLoad.name,(unsigned int)fLoad.checksum);
2a74fbf4 454 return -1;
455 }
c02ea99f 456 }else if(ev.event_id == TRACE_BLOCK_START){
40331ba8 457 continue;
c02ea99f 458 }else if(ev.event_id == TRACE_BLOCK_END){
40331ba8 459 break;
2a74fbf4 460 }else {
8d1e6362 461 g_warning("Not valid facilities trace file");
2a74fbf4 462 return -1;
463 }
963b5f2d 464 }
465 }
2a74fbf4 466 return 0;
6cd62ccf 467}
3aee1200 468#endif //0
6cd62ccf 469
470/*****************************************************************************
471 *Function name
963b5f2d 472 * ltt_tracefile_close: close a trace file,
6cd62ccf 473 *Input params
963b5f2d 474 * t : tracefile which will be closed
6cd62ccf 475 ****************************************************************************/
476
963b5f2d 477void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 478{
f628823c 479 int page_size = getpagesize();
480
3aee1200 481 if(t->buffer.head != NULL)
f628823c 482 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
483 g_warning("unmap size : %u\n",
484 PAGE_ALIGN(t->buf_size));
485 perror("munmap error");
486 g_assert(0);
487 }
488
3865ea09 489 close(t->fd);
963b5f2d 490}
6cd62ccf 491
6cd62ccf 492
963b5f2d 493/*****************************************************************************
494 *Get system information
495 ****************************************************************************/
3aee1200 496#if 0
45e14832 497gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
963b5f2d 498{
45e14832 499 int fd;
500 GIOChannel *iochan;
501 gchar *buf = NULL;
502 gsize length;
43da6a59 503
504 GMarkupParseContext * context;
2a74fbf4 505 GError * error = NULL;
43da6a59 506 GMarkupParser markup_parser =
507 {
508 parser_start_element,
8d1e6362 509 NULL,
43da6a59 510 parser_characters,
511 NULL, /* passthrough */
512 NULL /* error */
513 };
963b5f2d 514
45e14832 515 fd = g_open(pathname, O_RDONLY, 0);
516 if(fd == -1){
2a74fbf4 517 g_warning("Can not open file : %s\n", pathname);
518 return -1;
6cd62ccf 519 }
963b5f2d 520
45e14832 521 iochan = g_io_channel_unix_new(fd);
522
43da6a59 523 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
6cd62ccf 524
45e14832 525 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
526 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
527 != G_IO_STATUS_EOF) {
528
529 if(error != NULL) {
530 g_warning("Can not read xml file: \n%s\n", error->message);
531 g_error_free(error);
532 }
533 if(!g_markup_parse_context_parse(context, buf, length, &error)){
2a74fbf4 534 if(error != NULL) {
535 g_warning("Can not parse xml file: \n%s\n", error->message);
536 g_error_free(error);
537 }
538 g_markup_parse_context_free(context);
45e14832 539
540 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
541 if(error != NULL) {
542 g_warning("Can not close file: \n%s\n", error->message);
543 g_error_free(error);
544 }
545
546 close(fd);
2a74fbf4 547 return -1;
43da6a59 548 }
963b5f2d 549 }
2a74fbf4 550 g_markup_parse_context_free(context);
45e14832 551
552 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
553 if(error != NULL) {
554 g_warning("Can not close file: \n%s\n", error->message);
555 g_error_free(error);
556 }
557
558 g_close(fd);
559
560 g_free(buf);
2a74fbf4 561 return 0;
6cd62ccf 562}
3aee1200 563#endif //0
6cd62ccf 564
565/*****************************************************************************
963b5f2d 566 *The following functions get facility/tracefile information
6cd62ccf 567 ****************************************************************************/
3aee1200 568#if 0
45e14832 569gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
6cd62ccf 570{
45e14832 571 GDir * dir;
572 const gchar * name;
8d1e6362 573 unsigned int i,j;
963b5f2d 574 LttFacility * f;
575 LttEventType * et;
45e14832 576 gchar fullname[DIR_NAME_SIZE];
577 GError * error = NULL;
578
579 dir = g_dir_open(eventdefs, 0, &error);
963b5f2d 580
45e14832 581 if(error != NULL) {
582 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
583 g_error_free(error);
2a74fbf4 584 return -1;
585 }
963b5f2d 586
45e14832 587 while((name = g_dir_read_name(dir)) != NULL){
588 if(!g_pattern_match_simple("*.xml", name)) continue;
589 strcpy(fullname,eventdefs);
590 strcat(fullname,name);
591 ltt_facility_open(t,fullname);
592 }
593 g_dir_close(dir);
963b5f2d 594
963b5f2d 595 for(j=0;j<t->facility_number;j++){
8710c6c7 596 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 597 for(i=0; i<f->event_number; i++){
598 et = f->events[i];
40331ba8 599 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 600 }
601 }
2a74fbf4 602 return 0;
963b5f2d 603}
3aee1200 604#endif //0
6cd62ccf 605
963b5f2d 606/*****************************************************************************
607 *A trace is specified as a pathname to the directory containing all the
608 *associated data (control tracefiles, per cpu tracefiles, event
609 *descriptions...).
610 *
611 *When a trace is closed, all the associated facilities, types and fields
612 *are released as well.
8d1e6362 613 */
614
615
616/****************************************************************************
617 * get_absolute_pathname
803229fa 618 *
8d1e6362 619 * return the unique pathname in the system
620 *
ef35d837 621 * MD : Fixed this function so it uses realpath, dealing well with
622 * forgotten cases (.. were not used correctly before).
803229fa 623 *
963b5f2d 624 ****************************************************************************/
45e14832 625void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
9f797243 626{
9f797243 627 abs_pathname[0] = '\0';
803229fa 628
ef35d837 629 if ( realpath (pathname, abs_pathname) != NULL)
630 return;
631 else
632 {
8d1e6362 633 /* error, return the original path unmodified */
ef35d837 634 strcpy(abs_pathname, pathname);
9f797243 635 return;
636 }
ef35d837 637 return;
9f797243 638}
639
3aee1200 640/* Search for something like : .*_.*
641 *
642 * The left side is the name, the right side is the number.
643 */
644
645int get_tracefile_name_number(const gchar *raw_name,
646 GQuark *name,
647 guint *num)
6cd62ccf 648{
3aee1200 649 guint raw_name_len = strlen(raw_name);
650 gchar char_name[PATH_MAX];
3aee1200 651 int i;
652 int underscore_pos;
653 long int cpu_num;
654 gchar *endptr;
655
656 for(i=raw_name_len-1;i>=0;i--) {
657 if(raw_name[i] == '_') break;
658 }
659 if(i==0) /* Either not found or name length is 0 */
660 return -1;
661 underscore_pos = i;
963b5f2d 662
3aee1200 663 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
664
665 if(endptr == raw_name+underscore_pos+1)
666 return -1; /* No digit */
667 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
668 return -1; /* underflow / overflow */
1a2ceb63 669
3aee1200 670 strncpy(char_name, raw_name, underscore_pos);
1a2ceb63 671
69bd59ed 672 char_name[underscore_pos] = '\0';
673
3aee1200 674 *name = g_quark_from_string(char_name);
675 *num = cpu_num;
b333a76b 676
3aee1200 677 return 0;
678}
963b5f2d 679
3aee1200 680
3865ea09 681GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
77175651 682{
3865ea09 683 return &trace->tracefiles;
77175651 684}
685
686
3865ea09 687void compute_tracefile_group(GQuark key_id,
688 GArray *group,
689 struct compute_tracefile_group_args *args)
77175651 690{
691 int i;
692 LttTracefile *tf;
693
694 for(i=0; i<group->len; i++) {
695 tf = &g_array_index (group, LttTracefile, i);
696 if(tf->cpu_online)
3865ea09 697 args->func(tf, args->func_args);
77175651 698 }
699}
700
701
3aee1200 702void ltt_tracefile_group_destroy(gpointer data)
703{
704 GArray *group = (GArray *)data;
705 int i;
706 LttTracefile *tf;
707
708 for(i=0; i<group->len; i++) {
709 tf = &g_array_index (group, LttTracefile, i);
710 if(tf->cpu_online)
711 ltt_tracefile_close(tf);
712 }
713 g_array_free(group, TRUE);
6cd62ccf 714}
715
3aee1200 716gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 717{
3aee1200 718 GArray *group = (GArray *)data;
719 int i;
720 LttTracefile *tf;
721
722 for(i=0; i<group->len; i++) {
723 tf = &g_array_index (group, LttTracefile, i);
724 if(tf->cpu_online) return 1;
725 }
726 return 0;
49bf71b5 727}
728
729
3aee1200 730/* Open each tracefile under a specific directory. Put them in a
731 * GData : permits to access them using their tracefile group pathname.
732 * i.e. access control/modules tracefile group by index :
733 * "control/module".
3865ea09 734 *
735 * relative path is the path relative to the trace root
736 * root path is the full path
3aee1200 737 *
738 * A tracefile group is simply an array where all the per cpu tracefiles sits.
739 */
740
69bd59ed 741static int open_tracefiles(LttTrace *trace, gchar *root_path,
742 gchar *relative_path)
f7afe191 743{
3aee1200 744 DIR *dir = opendir(root_path);
745 struct dirent *entry;
746 struct stat stat_buf;
747 int ret;
3865ea09 748
69bd59ed 749 gchar path[PATH_MAX];
3aee1200 750 int path_len;
69bd59ed 751 gchar *path_ptr;
3aee1200 752
3865ea09 753 int rel_path_len;
69bd59ed 754 gchar rel_path[PATH_MAX];
755 gchar *rel_path_ptr;
cb03932a 756 LttTracefile tmp_tf;
3865ea09 757
3aee1200 758 if(dir == NULL) {
759 perror(root_path);
760 return ENOENT;
761 }
762
763 strncpy(path, root_path, PATH_MAX-1);
764 path_len = strlen(path);
765 path[path_len] = '/';
766 path_len++;
767 path_ptr = path + path_len;
768
3865ea09 769 strncpy(rel_path, relative_path, PATH_MAX-1);
770 rel_path_len = strlen(rel_path);
771 rel_path[rel_path_len] = '/';
772 rel_path_len++;
773 rel_path_ptr = rel_path + rel_path_len;
774
3aee1200 775 while((entry = readdir(dir)) != NULL) {
776
777 if(entry->d_name[0] == '.') continue;
778
779 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
3865ea09 780 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
3aee1200 781
782 ret = stat(path, &stat_buf);
783 if(ret == -1) {
784 perror(path);
785 continue;
786 }
787
788 g_debug("Tracefile file or directory : %s\n", path);
789
74a588bb 790 if(strcmp(rel_path, "/eventdefs") == 0) continue;
791
3aee1200 792 if(S_ISDIR(stat_buf.st_mode)) {
793
794 g_debug("Entering subdirectory...\n");
3865ea09 795 ret = open_tracefiles(trace, path, rel_path);
3aee1200 796 if(ret < 0) continue;
797 } else if(S_ISREG(stat_buf.st_mode)) {
3aee1200 798 GQuark name;
799 guint num;
800 GArray *group;
3aee1200 801
3865ea09 802 if(get_tracefile_name_number(rel_path, &name, &num))
3aee1200 803 continue; /* invalid name */
3865ea09 804
cb03932a 805 g_debug("Opening file.\n");
806 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
807 g_info("Error opening tracefile %s", path);
808
809 continue; /* error opening the tracefile : bad magic number ? */
810 }
811
3865ea09 812 g_debug("Tracefile name is %s and number is %u",
813 g_quark_to_string(name), num);
814
cb03932a 815 tmp_tf.cpu_online = 1;
816 tmp_tf.cpu_num = num;
d3d34f49 817 tmp_tf.name = name;
cb03932a 818
3865ea09 819 group = g_datalist_id_get_data(&trace->tracefiles, name);
3aee1200 820 if(group == NULL) {
821 /* Elements are automatically cleared when the array is allocated.
822 * It makes the cpu_online variable set to 0 : cpu offline, by default.
823 */
824 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
3865ea09 825 g_datalist_id_set_data_full(&trace->tracefiles, name,
3aee1200 826 group, ltt_tracefile_group_destroy);
827 }
cb03932a 828
3aee1200 829 /* Add the per cpu tracefile to the named group */
830 unsigned int old_len = group->len;
831 if(num+1 > old_len)
832 group = g_array_set_size(group, num+1);
cb03932a 833 g_array_index (group, LttTracefile, num) = tmp_tf;
3aee1200 834
3aee1200 835 }
836 }
837
838 closedir(dir);
839
840 return 0;
f7afe191 841}
842
3aee1200 843/* ltt_get_facility_description
844 *
29e7c5b3 845 * Opens the file corresponding to the requested facility (identified by fac_id
3aee1200 846 * and checksum).
847 *
848 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
849 *
850 * Returns 0 on success, or 1 on failure.
851 */
852
853static int ltt_get_facility_description(LttFacility *f,
854 LttTrace *t,
855 LttTracefile *fac_tf)
6cd62ccf 856{
3aee1200 857 char desc_file_name[PATH_MAX];
858 const gchar *text;
859 guint textlen;
860 gint err;
3aee1200 861
862 text = g_quark_to_string(t->pathname);
863 textlen = strlen(text);
864
865 if(textlen >= PATH_MAX) goto name_error;
866 strcpy(desc_file_name, text);
867
868 text = "/eventdefs/";
869 textlen+=strlen(text);
870 if(textlen >= PATH_MAX) goto name_error;
871 strcat(desc_file_name, text);
872
873 text = g_quark_to_string(f->name);
874 textlen+=strlen(text);
875 if(textlen >= PATH_MAX) goto name_error;
876 strcat(desc_file_name, text);
29e7c5b3 877#if 0
3aee1200 878 text = "_";
879 textlen+=strlen(text);
880 if(textlen >= PATH_MAX) goto name_error;
881 strcat(desc_file_name, text);
963b5f2d 882
3aee1200 883 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
884 "%u", f->checksum);
d2083cab 885 if(err < 0) goto name_error;
3aee1200 886
887 textlen=strlen(desc_file_name);
888
29e7c5b3 889#endif //0
3aee1200 890 text = ".xml";
891 textlen+=strlen(text);
892 if(textlen >= PATH_MAX) goto name_error;
893 strcat(desc_file_name, text);
29e7c5b3 894
3aee1200 895 err = ltt_facility_open(f, t, desc_file_name);
896 if(err) goto facility_error;
3aee1200 897
898 return 0;
899
900facility_error:
901name_error:
902 return 1;
903}
904
905static void ltt_fac_ids_destroy(gpointer data)
906{
907 GArray *fac_ids = (GArray *)data;
963b5f2d 908
3aee1200 909 g_array_free(fac_ids, TRUE);
910}
911
912
913/* Presumes the tracefile is already seeked at the beginning. It makes sense,
914 * because it must be done just after the opening */
915static int ltt_process_facility_tracefile(LttTracefile *tf)
916{
917 int err;
918 LttFacility *fac;
919 GArray *fac_ids;
cb03932a 920 guint i;
921 LttEventType *et;
3aee1200 922
923 while(1) {
924 err = ltt_tracefile_read_seek(tf);
925 if(err == EPERM) goto seek_error;
926 else if(err == ERANGE) break; /* End of tracefile */
927
928 err = ltt_tracefile_read_update_event(tf);
929 if(err) goto update_error;
930
931 /* We are on a facility load/or facility unload/ or heartbeat event */
932 /* The rules are :
933 * * facility 0 is hardcoded : this is the core facility. It will be shown
934 * in the facility array though, and is shown as "loaded builtin" in the
935 * trace.
936 * It contains event :
937 * 0 : facility load
938 * 1 : facility unload
939 * 2 : state dump facility load
d2083cab 940 * 3 : heartbeat
3aee1200 941 */
d2083cab 942 if(tf->event.facility_id != LTT_FACILITY_CORE) {
943 /* Should only contain core facility */
3aee1200 944 g_warning("Error in processing facility file %s, "
945 "should not contain facility id %u.", g_quark_to_string(tf->name),
946 tf->event.facility_id);
947 err = EPERM;
948 goto fac_id_error;
d2083cab 949 } else {
3aee1200 950
951 struct LttFacilityLoad *fac_load_data;
b77d1b57 952 struct LttStateDumpFacilityLoad *fac_state_dump_load_data;
3aee1200 953 char *fac_name;
954
955 // FIXME align
956 switch((enum ltt_core_events)tf->event.event_id) {
957 case LTT_EVENT_FACILITY_LOAD:
b77d1b57 958 fac_name = (char*)(tf->event.data);
d2083cab 959 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
960 fac_name);
3aee1200 961 fac_load_data =
b77d1b57 962 (struct LttFacilityLoad *)
963 (tf->event.data + strlen(fac_name) + 1);
3aee1200 964 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
965 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
950683f4 966 /* facility may already exist if trace is paused/unpaused */
967 if(fac->exists) continue;
3aee1200 968 fac->name = g_quark_from_string(fac_name);
969 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
970 &fac_load_data->checksum);
971 fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
972 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
973 &fac_load_data->pointer_size);
f104d082 974 fac->int_size = ltt_get_uint32(LTT_GET_BO(tf),
975 &fac_load_data->int_size);
cb03932a 976 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
977 &fac_load_data->long_size);
eed2ef37 978 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 979 &fac_load_data->size_t_size);
980 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
2312de30 981 &fac_load_data->has_alignment);
3aee1200 982
983 if(ltt_get_facility_description(fac, tf->trace, tf))
cb03932a 984 continue; /* error opening description */
3aee1200 985
986 fac->trace = tf->trace;
cb03932a 987
988 /* Preset the field offsets */
989 for(i=0; i<fac->events->len; i++){
990 et = &g_array_index(fac->events, LttEventType, i);
f104d082 991 precompute_offsets(tf, et);
cb03932a 992 }
993
3aee1200 994 fac->exists = 1;
995
77175651 996 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
997 fac->name);
3aee1200 998 if(fac_ids == NULL) {
999 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
1000 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
1001 fac->name,
1002 fac_ids, ltt_fac_ids_destroy);
1003 }
1004 g_array_append_val(fac_ids, fac->id);
1005
1006 break;
1007 case LTT_EVENT_FACILITY_UNLOAD:
d2083cab 1008 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
3aee1200 1009 /* We don't care about unload : facilities ID are valid for the whole
1010 * trace. They simply won't be used after the unload. */
1011 break;
1012 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
b77d1b57 1013 fac_name = (char*)(tf->event.data);
d2083cab 1014 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1015 fac_name);
b77d1b57 1016 fac_state_dump_load_data =
d2083cab 1017 (struct LttStateDumpFacilityLoad *)
b77d1b57 1018 (tf->event.data + strlen(fac_name) + 1);
3aee1200 1019 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
b77d1b57 1020 ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id));
950683f4 1021 /* facility may already exist if trace is paused/unpaused */
1022 if(fac->exists) continue;
3aee1200 1023 fac->name = g_quark_from_string(fac_name);
1024 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 1025 &fac_state_dump_load_data->checksum);
ad34d116 1026 fac->id = ltt_get_uint32(LTT_GET_BO(tf),
1027 &fac_state_dump_load_data->id);
3aee1200 1028 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 1029 &fac_state_dump_load_data->pointer_size);
f104d082 1030 fac->int_size = ltt_get_uint32(LTT_GET_BO(tf),
1031 &fac_state_dump_load_data->int_size);
cb03932a 1032 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
1033 &fac_state_dump_load_data->long_size);
eed2ef37 1034 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 1035 &fac_state_dump_load_data->size_t_size);
3aee1200 1036 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
2312de30 1037 &fac_state_dump_load_data->has_alignment);
3aee1200 1038 if(ltt_get_facility_description(fac, tf->trace, tf))
cb03932a 1039 continue; /* error opening description */
3aee1200 1040
1041 fac->trace = tf->trace;
1042
cb03932a 1043 /* Preset the field offsets */
1044 for(i=0; i<fac->events->len; i++){
1045 et = &g_array_index(fac->events, LttEventType, i);
f104d082 1046 precompute_offsets(tf, et);
cb03932a 1047 }
1048
3aee1200 1049 fac->exists = 1;
1050
1051 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
1052 fac->name);
1053 if(fac_ids == NULL) {
1054 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
1055 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
1056 fac->name,
1057 fac_ids, ltt_fac_ids_destroy);
1058 }
1059 g_array_append_val(fac_ids, fac->id);
1060
1061 break;
1062 case LTT_EVENT_HEARTBEAT:
1063 break;
1064 default:
1065 g_warning("Error in processing facility file %s, "
1066 "unknown event id %hhu in core facility.",
1067 g_quark_to_string(tf->name),
1068 tf->event.event_id);
1069 err = EPERM;
1070 goto event_id_error;
1071 }
1072 }
963b5f2d 1073 }
3aee1200 1074 return 0;
963b5f2d 1075
3aee1200 1076 /* Error handling */
3aee1200 1077event_id_error:
1078fac_id_error:
1079update_error:
1080seek_error:
d2083cab 1081 g_warning("An error occured in facility tracefile parsing");
3aee1200 1082 return err;
6cd62ccf 1083}
1084
963b5f2d 1085
3aee1200 1086LttTrace *ltt_trace_open(const gchar *pathname)
1087{
1088 gchar abs_path[PATH_MAX];
1089 LttTrace * t;
1090 LttTracefile *tf;
1091 GArray *group;
b56dcdf2 1092 int i, ret;
16fcbb80 1093 struct ltt_block_start_header *header;
b56dcdf2 1094 DIR *dir;
1095 struct dirent *entry;
1096 guint control_found = 0;
1097 guint eventdefs_found = 0;
1098 struct stat stat_buf;
1099 gchar path[PATH_MAX];
3aee1200 1100
1101 t = g_new(LttTrace, 1);
1102 if(!t) goto alloc_error;
1103
1104 get_absolute_pathname(pathname, abs_path);
1105 t->pathname = g_quark_from_string(abs_path);
1106
3aee1200 1107 g_datalist_init(&t->tracefiles);
b56dcdf2 1108
1109 /* Test to see if it looks like a trace */
1110 dir = opendir(abs_path);
1111 if(dir == NULL) {
1112 perror(abs_path);
1113 goto open_error;
1114 }
1115 while((entry = readdir(dir)) != NULL) {
1116 strcpy(path, abs_path);
1117 strcat(path, "/");
1118 strcat(path, entry->d_name);
1119 ret = stat(path, &stat_buf);
1120 if(ret == -1) {
1121 perror(path);
1122 continue;
1123 }
1124 if(S_ISDIR(stat_buf.st_mode)) {
1125 if(strcmp(entry->d_name, "control") == 0) {
1126 control_found = 1;
1127 }
1128 if(strcmp(entry->d_name, "eventdefs") == 0) {
1129 eventdefs_found = 1;
1130 }
1131 }
1132 }
1133 closedir(dir);
1134
1135 if(!control_found || !eventdefs_found) goto find_error;
1136
1137 /* Open all the tracefiles */
e45551ac 1138 if(open_tracefiles(t, abs_path, "")) {
1139 g_warning("Error opening tracefile %s", abs_path);
b56dcdf2 1140 goto find_error;
e45551ac 1141 }
3aee1200 1142
1143 /* Prepare the facilities containers : array and mapping */
1144 /* Array is zeroed : the "exists" field is set to false by default */
1145 t->facilities_by_num = g_array_sized_new (FALSE,
1146 TRUE, sizeof(LttFacility),
1147 NUM_FACILITIES);
1148 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
1149
1150 g_datalist_init(&t->facilities_by_name);
1151
1152 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1153 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1154 if(group == NULL) {
1155 g_error("Trace %s has no facility tracefile", abs_path);
3865ea09 1156 g_assert(0);
3aee1200 1157 goto facilities_error;
1158 }
1159
16fcbb80 1160 /* Get the trace information for the control/facility 0 tracefile */
1161 g_assert(group->len > 0);
1162 tf = &g_array_index (group, LttTracefile, 0);
1163 header = (struct ltt_block_start_header*)tf->buffer.head;
51551c6f 1164 g_assert(parse_trace_header(header->trace,
1165 tf, t) == 0);
b56dcdf2 1166
1167 t->num_cpu = group->len;
16fcbb80 1168
3aee1200 1169 for(i=0; i<group->len; i++) {
1170 tf = &g_array_index (group, LttTracefile, i);
1171 if(ltt_process_facility_tracefile(tf))
1172 goto facilities_error;
1173 }
3aee1200 1174
1175 return t;
1176
1177 /* Error handling */
1178facilities_error:
1179 g_datalist_clear(&t->facilities_by_name);
1180 g_array_free(t->facilities_by_num, TRUE);
b56dcdf2 1181find_error:
3aee1200 1182 g_datalist_clear(&t->tracefiles);
b56dcdf2 1183open_error:
3aee1200 1184 g_free(t);
1185alloc_error:
1186 return NULL;
1187
1188}
6cd62ccf 1189
826f1ab2 1190GQuark ltt_trace_name(const LttTrace *t)
6cd62ccf 1191{
3aee1200 1192 return t->pathname;
6cd62ccf 1193}
1194
6cd62ccf 1195
3aee1200 1196/******************************************************************************
1197 * When we copy a trace, we want all the opening actions to happen again :
1198 * the trace will be reopened and totally independant from the original.
1199 * That's why we call ltt_trace_open.
1200 *****************************************************************************/
1201LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 1202{
3aee1200 1203 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 1204}
1205
3aee1200 1206void ltt_trace_close(LttTrace *t)
6cd62ccf 1207{
cb03932a 1208 guint i;
1209 LttFacility *fac;
1210
1211 for(i=0; i<t->facilities_by_num->len; i++) {
1212 fac = &g_array_index (t->facilities_by_num, LttFacility, i);
1213 if(fac->exists)
1214 ltt_facility_close(fac);
1215 }
1216
3aee1200 1217 g_datalist_clear(&t->facilities_by_name);
1218 g_array_free(t->facilities_by_num, TRUE);
1219 g_datalist_clear(&t->tracefiles);
1220 g_free(t);
6cd62ccf 1221}
1222
3aee1200 1223
6cd62ccf 1224/*****************************************************************************
3aee1200 1225 *Get the system description of the trace
6cd62ccf 1226 ****************************************************************************/
1227
3aee1200 1228LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
6cd62ccf 1229{
3aee1200 1230 g_assert(id < t->facilities_by_num->len);
1231 return &g_array_index(t->facilities_by_num, LttFacility, id);
1232}
1233
1234/* ltt_trace_facility_get_by_name
1235 *
1236 * Returns the GArray of facility indexes. All the fac_ids that matches the
1237 * requested facility name.
1238 *
1239 * If name is not found, returns NULL.
1240 */
1241GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1242{
1243 return g_datalist_id_get_data(&t->facilities_by_name, name);
6cd62ccf 1244}
1245
1246/*****************************************************************************
963b5f2d 1247 * Functions to discover all the event types in the trace
6cd62ccf 1248 ****************************************************************************/
1249
3aee1200 1250#if 0
963b5f2d 1251unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 1252{
8d1e6362 1253 unsigned int i;
963b5f2d 1254 unsigned count = 0;
dc1cad90 1255 unsigned int num = t->facility_number;
963b5f2d 1256 LttFacility * f;
dc1cad90 1257
1258 for(i=0;i<num;i++){
963b5f2d 1259 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1260 count += f->event_number;
1261 }
1262 return count;
6cd62ccf 1263}
3aee1200 1264#endif //0
6cd62ccf 1265
3aee1200 1266#if 0
1267//use an iteration on all the trace facilities, and inside iteration on all the
1268//event types in each facilities instead.
963b5f2d 1269LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 1270{
6e8c19d4 1271 LttEventType *event_type;
1272
963b5f2d 1273 LttFacility * f;
1274 f = ltt_trace_facility_by_id(t,evId);
6e8c19d4 1275
1276 if(unlikely(!f)) event_type = NULL;
1277 else event_type = f->events[evId - f->base_id];
1278
1279 return event_type;
6cd62ccf 1280}
3aee1200 1281#endif //0
6cd62ccf 1282
3aee1200 1283#if 0
6cd62ccf 1284/*****************************************************************************
3aee1200 1285 * ltt_trace_find_tracefile
1286 *
1287 * Find a tracefile by name and index in the group.
1288 *
1289 * Returns a pointer to the tracefiles, else NULL.
6cd62ccf 1290 ****************************************************************************/
6cd62ccf 1291
3aee1200 1292LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
6cd62ccf 1293{
6cd62ccf 1294}
3aee1200 1295#endif //0
6cd62ccf 1296
1297/*****************************************************************************
3aee1200 1298 * Get the start time and end time of the trace
6cd62ccf 1299 ****************************************************************************/
1300
3aee1200 1301static void ltt_tracefile_time_span_get(LttTracefile *tf,
1302 LttTime *start, LttTime *end)
6cd62ccf 1303{
3aee1200 1304 int err;
6cd62ccf 1305
3aee1200 1306 err = map_block(tf, 0);
1307 if(unlikely(err)) {
1308 g_error("Can not map block");
1309 *start = ltt_time_infinite;
1310 } else
1311 *start = tf->buffer.begin.timestamp;
1312
1313 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1314 if(unlikely(err)) {
1315 g_error("Can not map block");
1316 *end = ltt_time_zero;
1317 } else
1318 *end = tf->buffer.end.timestamp;
6cd62ccf 1319}
1320
3aee1200 1321struct tracefile_time_span_get_args {
1322 LttTrace *t;
1323 LttTime *start;
1324 LttTime *end;
1325};
963b5f2d 1326
3aee1200 1327static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 1328{
3aee1200 1329 struct tracefile_time_span_get_args *args =
1330 (struct tracefile_time_span_get_args*)user_data;
1331
1332 GArray *group = (GArray *)data;
1333 int i;
1334 LttTracefile *tf;
1335 LttTime tmp_start;
1336 LttTime tmp_end;
1337
1338 for(i=0; i<group->len; i++) {
1339 tf = &g_array_index (group, LttTracefile, i);
1340 if(tf->cpu_online) {
1341 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1342 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1343 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1344 }
1345 }
6cd62ccf 1346}
1347
487ad181 1348void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1349{
3aee1200 1350 LttTime min_start = ltt_time_infinite;
1351 LttTime max_end = ltt_time_zero;
1352 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 1353
3aee1200 1354 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1355
1356 if(start != NULL) *start = min_start;
1357 if(end != NULL) *end = max_end;
1358
487ad181 1359}
1360
1361
6cd62ccf 1362/*****************************************************************************
963b5f2d 1363 *Get the name of a tracefile
6cd62ccf 1364 ****************************************************************************/
1365
348c6ba8 1366GQuark ltt_tracefile_name(const LttTracefile *tf)
6cd62ccf 1367{
963b5f2d 1368 return tf->name;
6cd62ccf 1369}
1370
348c6ba8 1371GQuark ltt_tracefile_long_name(const LttTracefile *tf)
1372{
1373 return tf->long_name;
1374}
1375
1376
d3d34f49 1377
1378guint ltt_tracefile_num(LttTracefile *tf)
1379{
1380 return tf->cpu_num;
1381}
1382
80da81ad 1383/*****************************************************************************
1384 * Get the number of blocks in the tracefile
1385 ****************************************************************************/
1386
3aee1200 1387guint ltt_tracefile_block_number(LttTracefile *tf)
80da81ad 1388{
3aee1200 1389 return tf->num_blocks;
80da81ad 1390}
1391
caf7a67a 1392
3aee1200 1393/* Seek to the first event in a tracefile that has a time equal or greater than
1394 * the time passed in parameter.
1395 *
1396 * If the time parameter is outside the tracefile time span, seek to the first
27304273 1397 * event or if after, return ERANGE.
3aee1200 1398 *
1399 * If the time parameter is before the first event, we have to seek specially to
1400 * there.
1401 *
27304273 1402 * If the time is after the end of the trace, return ERANGE.
3aee1200 1403 *
1404 * Do a binary search to find the right block, then a sequential search in the
1405 * block to find the event.
1406 *
1407 * In the special case where the time requested fits inside a block that has no
1408 * event corresponding to the requested time, the first event of the next block
1409 * will be seeked.
1410 *
1411 * IMPORTANT NOTE : // FIXME everywhere...
1412 *
1413 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1414 * you will jump over an event if you do.
1415 *
1416 * Return value : 0 : no error, the tf->event can be used
27304273 1417 * ERANGE : time if after the last event of the trace
3aee1200 1418 * otherwise : this is an error.
1419 *
1420 * */
1421
1422int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1423{
1424 int ret = 0;
1425 int err;
1426 unsigned int block_num, high, low;
1427
1428 /* seek at the beginning of trace */
1429 err = map_block(tf, 0); /* First block */
1430 if(unlikely(err)) {
1431 g_error("Can not map block");
1432 goto fail;
caf7a67a 1433 }
1434
3aee1200 1435 /* If the time is lower or equal the beginning of the trace,
1436 * go to the first event. */
1437 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1438 ret = ltt_tracefile_read(tf);
27304273 1439 if(ret == ERANGE) goto range;
1440 else if (ret) goto fail;
3aee1200 1441 goto found; /* There is either no event in the trace or the event points
1442 to the first event in the trace */
1443 }
caf7a67a 1444
3aee1200 1445 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1446 if(unlikely(err)) {
1447 g_error("Can not map block");
1448 goto fail;
caf7a67a 1449 }
6cd62ccf 1450
27304273 1451 /* If the time is after the end of the trace, return ERANGE. */
1452 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1453 goto range;
3aee1200 1454 }
62e55dd6 1455
3aee1200 1456 /* Binary search the block */
1457 high = tf->num_blocks - 1;
1458 low = 0;
1459
1460 while(1) {
1461 block_num = ((high-low) / 2) + low;
1462
1463 err = map_block(tf, block_num);
1464 if(unlikely(err)) {
1465 g_error("Can not map block");
1466 goto fail;
db55eaae 1467 }
3aee1200 1468 if(high == low) {
1469 /* We cannot divide anymore : this is what would happen if the time
1470 * requested was exactly between two consecutive buffers'end and start
1471 * timestamps. This is also what would happend if we didn't deal with out
1472 * of span cases prior in this function. */
1473 /* The event is right in the buffer!
1474 * (or in the next buffer first event) */
1475 while(1) {
1476 ret = ltt_tracefile_read(tf);
27304273 1477 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1478 else if(ret) goto fail;
1479
d9e13a0f 1480 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 1481 goto found;
3aee1200 1482 }
1483
27304273 1484 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 1485 /* go to lower part */
b1369bef 1486 high = block_num - 1;
3aee1200 1487 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1488 /* go to higher part */
b1369bef 1489 low = block_num + 1;
3aee1200 1490 } else {/* The event is right in the buffer!
1491 (or in the next buffer first event) */
1492 while(1) {
27304273 1493 ret = ltt_tracefile_read(tf);
1494 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1495 else if(ret) goto fail;
1496
d9e13a0f 1497 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1498 break;
6cd62ccf 1499 }
3aee1200 1500 goto found;
6cd62ccf 1501 }
6cd62ccf 1502 }
3aee1200 1503
1504found:
1505 return 0;
27304273 1506range:
1507 return ERANGE;
3aee1200 1508
1509 /* Error handling */
1510fail:
1511 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1512 g_quark_to_string(tf->name));
1513 return EPERM;
6cd62ccf 1514}
1515
80da81ad 1516
3aee1200 1517int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
80da81ad 1518
3aee1200 1519 int err;
1520
1521 if(ep->tracefile != tf) {
1522 goto fail;
80da81ad 1523 }
1524
3aee1200 1525 err = map_block(tf, ep->block);
1526 if(unlikely(err)) {
1527 g_error("Can not map block");
1528 goto fail;
1529 }
1530
1531 tf->event.offset = ep->offset;
18206708 1532
3aee1200 1533 err = ltt_tracefile_read_update_event(tf);
1534 if(err) goto fail;
1535 err = ltt_tracefile_read_op(tf);
1536 if(err) goto fail;
80da81ad 1537
a0c1f622 1538 return 0;
3aee1200 1539
1540fail:
1541 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1542 g_quark_to_string(tf->name));
a0c1f622 1543 return 1;
3aee1200 1544}
1545
1546/* Calculate the real event time based on the buffer boundaries */
1547LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1548{
1549 LttTime time;
1550
1551 g_assert(tf->trace->has_tsc);
1552
791dffa6 1553// time = ltt_time_from_uint64(
1554// cycles_2_ns(tf, (guint64)(tf->buffer.tsc - tf->buffer.begin.cycle_count)));
88612d17 1555 time = ltt_time_from_uint64(
1556 (double)(tf->buffer.tsc - tf->trace->start_tsc) * 1000000.0
791dffa6 1557 / (double)tf->trace->start_freq);
1558 //time = ltt_time_add(tf->buffer.begin.timestamp, time);
88612d17 1559 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
3aee1200 1560
1561 return time;
80da81ad 1562}
1563
eed2ef37 1564
1565/* Get the current event of the tracefile : valid until the next read */
1566LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1567{
1568 return &tf->event;
1569}
1570
1571
1572
6cd62ccf 1573/*****************************************************************************
1574 *Function name
3aee1200 1575 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1576 *Input params
1577 * t : tracefile
1578 *Return value
3aee1200 1579 *
1580 * Returns 0 if an event can be used in tf->event.
d822520b 1581 * Returns ERANGE on end of trace. The event in tf->event still can be used
1582 * (if the last block was not empty).
3aee1200 1583 * Returns EPERM on error.
1584 *
1585 * This function does make the tracefile event structure point to the event
1586 * currently pointed to by the tf->event.
1587 *
1588 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1589 * reinitialize it after an error if you want results to be coherent.
1590 * It would be the case if a end of trace last buffer has no event : the end
1591 * of trace wouldn't be returned, but an error.
1592 * We make the assumption there is at least one event per buffer.
6cd62ccf 1593 ****************************************************************************/
1594
3aee1200 1595int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1596{
963b5f2d 1597 int err;
6cd62ccf 1598
3aee1200 1599 err = ltt_tracefile_read_seek(tf);
1600 if(err) return err;
1601 err = ltt_tracefile_read_update_event(tf);
1602 if(err) return err;
1603 err = ltt_tracefile_read_op(tf);
1604 if(err) return err;
1605
1606 return 0;
1607}
1608
1609int ltt_tracefile_read_seek(LttTracefile *tf)
1610{
1611 int err;
1612
1613 /* Get next buffer until we finally have an event, or end of trace */
1614 while(1) {
1615 err = ltt_seek_next_event(tf);
1616 if(unlikely(err == ENOPROTOOPT)) {
1617 return EPERM;
bdc36259 1618 }
bdc36259 1619
3aee1200 1620 /* Are we at the end of the buffer ? */
1621 if(err == ERANGE) {
1622 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1623 return ERANGE;
1624 } else {
1625 /* get next block */
1626 err = map_block(tf, tf->buffer.index + 1);
1627 if(unlikely(err)) {
1628 g_error("Can not map block");
1629 return EPERM;
1630 }
1631 }
1632 } else break; /* We found an event ! */
1633 }
2dee981d 1634
3aee1200 1635 return 0;
1636}
18206708 1637
1638
3aee1200 1639/* do specific operation on events */
1640int ltt_tracefile_read_op(LttTracefile *tf)
1641{
3aee1200 1642 LttEvent *event;
1643
1644 event = &tf->event;
18206708 1645
3aee1200 1646 /* do event specific operation */
40331ba8 1647
3aee1200 1648 /* do something if its an heartbeat event : increment the heartbeat count */
1649 //if(event->facility_id == LTT_FACILITY_CORE)
1650 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1651 // tf->cur_heart_beat_number++;
1652
1653 return 0;
6cd62ccf 1654}
1655
6cd62ccf 1656
3aee1200 1657/* same as ltt_tracefile_read, but does not seek to the next event nor call
1658 * event specific operation. */
1659int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1660{
3aee1200 1661 void * pos;
1662 LttEvent *event;
1663
1664 event = &tf->event;
eed2ef37 1665 pos = tf->buffer.head + event->offset;
3aee1200 1666
1667 /* Read event header */
1668
f104d082 1669 /* Align the head */
2312de30 1670 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment);
3aee1200 1671
1672 if(tf->trace->has_tsc) {
16fcbb80 1673 if(tf->trace->has_heartbeat) {
1674 event->time.timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1675 pos);
1676 /* 32 bits -> 64 bits tsc */
1677 /* note : still works for seek and non seek cases. */
1678 if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1679 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1680 + 0x100000000ULL)
1681 | (guint64)event->time.timestamp;
1682 event->tsc = tf->buffer.tsc;
1683 } else {
1684 /* no overflow */
1685 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1686 | (guint64)event->time.timestamp;
1687 event->tsc = tf->buffer.tsc;
1688 }
1689 pos += sizeof(guint32);
3aee1200 1690 } else {
16fcbb80 1691 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1692 tf->buffer.tsc = event->tsc;
1693 pos += sizeof(guint64);
3aee1200 1694 }
16fcbb80 1695
3aee1200 1696 event->event_time = ltt_interpolate_time(tf, event);
3aee1200 1697 } else {
1698 event->time.delta.tv_sec = 0;
1699 event->time.delta.tv_nsec = ltt_get_uint32(LTT_GET_BO(tf),
1700 pos) * NSEC_PER_USEC;
1701 tf->buffer.tsc = 0;
1702 event->tsc = tf->buffer.tsc;
1703
1704 event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
1705 event->time.delta);
1706 pos += sizeof(guint32);
1707 }
1708
eed2ef37 1709 event->facility_id = *(guint8*)pos;
3aee1200 1710 pos += sizeof(guint8);
1711
eed2ef37 1712 event->event_id = *(guint8*)pos;
3aee1200 1713 pos += sizeof(guint8);
1714
b77d1b57 1715 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1716 pos += sizeof(guint16);
1717
f104d082 1718 /* Align the head */
2312de30 1719 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment);
f104d082 1720
3aee1200 1721 event->data = pos;
1722
77175651 1723 /* get the data size and update the event fields with the current
1724 * information */
eed2ef37 1725 ltt_update_event_size(tf);
77175651 1726
3aee1200 1727 return 0;
6cd62ccf 1728}
1729
507915ee 1730
6cd62ccf 1731/****************************************************************************
1732 *Function name
3aee1200 1733 * map_block : map a block from the file
6cd62ccf 1734 *Input Params
1735 * lttdes : ltt trace file
1736 * whichBlock : the block which will be read
1737 *return value
1738 * 0 : success
1739 * EINVAL : lseek fail
1740 * EIO : can not read from the file
1741 ****************************************************************************/
1742
3aee1200 1743static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1744{
b77d1b57 1745 int page_size = getpagesize();
3aee1200 1746 struct ltt_block_start_header *header;
1747
1748 g_assert(block_num < tf->num_blocks);
6cd62ccf 1749
f628823c 1750 if(tf->buffer.head != NULL) {
1751 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1752 g_warning("unmap size : %u\n",
1753 PAGE_ALIGN(tf->buf_size));
1754 perror("munmap error");
1755 g_assert(0);
1756 }
1757 }
1758
ac849774 1759
3aee1200 1760 /* Multiple of pages aligned head */
b77d1b57 1761 tf->buffer.head = mmap(0,
f628823c 1762 PAGE_ALIGN(tf->buf_size),
b77d1b57 1763 PROT_READ, MAP_PRIVATE, tf->fd,
f628823c 1764 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
3aee1200 1765
3865ea09 1766 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1767 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1768 g_assert(0);
3aee1200 1769 goto map_error;
6cd62ccf 1770 }
3865ea09 1771 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 1772
6cd62ccf 1773
3aee1200 1774 tf->buffer.index = block_num;
1775
1776 header = (struct ltt_block_start_header*)tf->buffer.head;
1777
791dffa6 1778#if 0
986e2a7c 1779 tf->buffer.begin.timestamp = ltt_time_add(
1780 ltt_time_from_uint64(
1781 ltt_get_uint64(LTT_GET_BO(tf),
1782 &header->begin.timestamp)
1783 - tf->trace->start_monotonic),
1784 tf->trace->start_time);
791dffa6 1785#endif //0
348c6ba8 1786 //g_debug("block %u begin : %lu.%lu", block_num,
1787 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
3aee1200 1788 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1789 &header->begin.cycle_count);
986e2a7c 1790 tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
1791 &header->begin.freq);
791dffa6 1792 tf->buffer.begin.timestamp = ltt_time_add(
1793 ltt_time_from_uint64(
88612d17 1794 (double)(tf->buffer.begin.cycle_count
1795 - tf->trace->start_tsc) * 1000000.0
791dffa6 1796 / (double)tf->trace->start_freq),
88612d17 1797 tf->trace->start_time_from_tsc);
791dffa6 1798#if 0
1799
986e2a7c 1800 tf->buffer.end.timestamp = ltt_time_add(
1801 ltt_time_from_uint64(
1802 ltt_get_uint64(LTT_GET_BO(tf),
1803 &header->end.timestamp)
1804 - tf->trace->start_monotonic),
1805 tf->trace->start_time);
791dffa6 1806#endif //0
348c6ba8 1807 //g_debug("block %u end : %lu.%lu", block_num,
1808 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
3aee1200 1809 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1810 &header->end.cycle_count);
986e2a7c 1811 tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
1812 &header->end.freq);
3aee1200 1813 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
986e2a7c 1814 &header->lost_size);
791dffa6 1815 tf->buffer.end.timestamp = ltt_time_add(
1816 ltt_time_from_uint64(
88612d17 1817 (double)(tf->buffer.end.cycle_count
1818 - tf->trace->start_tsc) * 1000000.0
791dffa6 1819 / (double)tf->trace->start_freq),
88612d17 1820 tf->trace->start_time_from_tsc);
791dffa6 1821
3aee1200 1822 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1823 tf->event.tsc = tf->buffer.tsc;
986e2a7c 1824 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 1825
1826 /* FIXME
1827 * eventually support variable buffer size : will need a partial pre-read of
1828 * the headers to create an index when we open the trace... eventually. */
f628823c 1829 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 1830 &header->buf_size));
507915ee 1831
3aee1200 1832 /* Now that the buffer is mapped, calculate the time interpolation for the
1833 * block. */
1834
791dffa6 1835// tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1836 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
3aee1200 1837
1838 /* Make the current event point to the beginning of the buffer :
1839 * it means that the event read must get the first event. */
1840 tf->event.tracefile = tf;
1841 tf->event.block = block_num;
eed2ef37 1842 tf->event.offset = 0;
3aee1200 1843
1844 return 0;
6cd62ccf 1845
3aee1200 1846map_error:
1847 return -errno;
40331ba8 1848
6cd62ccf 1849}
1850
77175651 1851/* It will update the fields offsets too */
1852void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1853{
2312de30 1854 off_t size = 0;
3aee1200 1855
1856 /* Specific handling of core events : necessary to read the facility control
1857 * tracefile. */
77175651 1858 LttFacility *f = ltt_trace_get_facility_by_num(tf->trace,
1859 tf->event.facility_id);
6cd62ccf 1860
3865ea09 1861 if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) {
1862 switch((enum ltt_core_events)tf->event.event_id) {
1863 case LTT_EVENT_FACILITY_LOAD:
b77d1b57 1864 size = strlen((char*)tf->event.data) + 1;
348c6ba8 1865 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
c4afd5d8 1866 size += sizeof(struct LttFacilityLoad);
3865ea09 1867 break;
1868 case LTT_EVENT_FACILITY_UNLOAD:
348c6ba8 1869 //g_debug("Update Event facility unload");
3865ea09 1870 size = sizeof(struct LttFacilityUnload);
1871 break;
1872 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
b77d1b57 1873 size = strlen((char*)tf->event.data) + 1;
348c6ba8 1874 //g_debug("Update Event facility load state dump of facility %s",
1875 // (char*)tf->event.data);
c4afd5d8 1876 size += sizeof(struct LttStateDumpFacilityLoad);
3865ea09 1877 break;
1878 case LTT_EVENT_HEARTBEAT:
348c6ba8 1879 //g_debug("Update Event heartbeat");
3865ea09 1880 size = sizeof(TimeHeartbeat);
1881 break;
1882 default:
1883 g_warning("Error in getting event size : tracefile %s, "
1884 "unknown event id %hhu in core facility.",
1885 g_quark_to_string(tf->name),
1886 tf->event.event_id);
1887 goto event_id_error;
2dee981d 1888
77175651 1889 }
3aee1200 1890 } else {
c4afd5d8 1891 if(!f->exists) {
1892 g_error("Unknown facility %hhu (0x%hhx) in tracefile %s",
1893 tf->event.facility_id,
1894 tf->event.facility_id,
1895 g_quark_to_string(tf->name));
1896 goto facility_error;
1897 }
1898
3aee1200 1899 LttEventType *event_type =
1900 ltt_facility_eventtype_get(f, tf->event.event_id);
c4afd5d8 1901
1902 if(!event_type) {
1903 g_error("Unknown event id %hhu in facility %s in tracefile %s",
1904 tf->event.event_id,
1905 g_quark_to_string(f->name),
1906 g_quark_to_string(tf->name));
1907 goto event_type_error;
1908 }
f104d082 1909
1910 /* Compute the dynamic offsets */
1911 compute_offsets(tf, event_type, &size, tf->event.data);
c4afd5d8 1912
348c6ba8 1913 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
1914 // tf->event.facility_id,
1915 // tf->event.event_id, size);
3aee1200 1916 }
3aee1200 1917
77175651 1918 tf->event.data_size = size;
eed2ef37 1919
b77d1b57 1920 /* Check consistency between kernel and LTTV structure sizes */
1921 g_assert(tf->event.data_size == tf->event.event_size);
9eb6aa7a 1922
eed2ef37 1923 return;
1924
c4afd5d8 1925facility_error:
1926event_type_error:
3aee1200 1927event_id_error:
eed2ef37 1928 tf->event.data_size = 0;
6cd62ccf 1929}
1930
6cd62ccf 1931
3aee1200 1932/* Take the tf current event offset and use the event facility id and event id
1933 * to figure out where is the next event offset.
1934 *
1935 * This is an internal function not aiming at being used elsewhere : it will
1936 * not jump over the current block limits. Please consider using
1937 * ltt_tracefile_read to do this.
1938 *
1939 * Returns 0 on success
1940 * ERANGE if we are at the end of the buffer.
1941 * ENOPROTOOPT if an error occured when getting the current event size.
1942 */
1943static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1944{
3aee1200 1945 int ret = 0;
1946 void *pos;
3aee1200 1947
1948 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1949 if(tf->event.offset == 0) {
51551c6f 1950 tf->event.offset += tf->buffer_header_size;
b77d1b57 1951
f628823c 1952 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
b77d1b57 1953 ret = ERANGE;
1954 }
3aee1200 1955 goto found;
1956 }
6cd62ccf 1957
6cd62ccf 1958
3aee1200 1959 pos = tf->event.data;
1960
77175651 1961 if(tf->event.data_size < 0) goto error;
3aee1200 1962
77175651 1963 pos += (size_t)tf->event.data_size;
3aee1200 1964
eed2ef37 1965 tf->event.offset = pos - tf->buffer.head;
cb03932a 1966
f628823c 1967 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
cb03932a 1968 ret = ERANGE;
1969 goto found;
1970 }
36d36c9f 1971 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
3aee1200 1972
1973found:
1974 return ret;
1975
1976error:
1977 g_error("Error in ltt_seek_next_event for tracefile %s",
1978 g_quark_to_string(tf->name));
1979 return ENOPROTOOPT;
6cd62ccf 1980}
1981
791dffa6 1982#if 0
6cd62ccf 1983/*****************************************************************************
1984 *Function name
3aee1200 1985 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
986e2a7c 1986 *
1987 * 1.0 / (freq(khz) *1000) * 1000000000
6cd62ccf 1988 *Input Params
1989 * t : tracefile
1990 ****************************************************************************/
986e2a7c 1991/* from timer_tsc.c */
1992#define CYC2NS_SCALE_FACTOR 10
791dffa6 1993static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
6cd62ccf 1994{
986e2a7c 1995 //return 1e6 / (double)tf->buffer.freq;
791dffa6 1996 guint32 cpu_mhz = tf->buffer.freq / 1000;
1997 guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
6cd62ccf 1998
791dffa6 1999 return cyc2ns_scale;
986e2a7c 2000 // return 1e6 / (double)tf->buffer.freq;
3aee1200 2001}
791dffa6 2002
2003static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
2004{
2005 return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
2006}
2007#endif //0
2008
3aee1200 2009#if 0
2010void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
2011{
2012 LttField * rootFld = evT->root_field;
2013 // rootFld->base_address = evD;
2dee981d 2014
3aee1200 2015 if(likely(rootFld))
2016 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
2017 evT, 0,0,rootFld, evD);
6cd62ccf 2018}
3aee1200 2019#endif //0
f104d082 2020#if 0
3aee1200 2021/*****************************************************************************
6cd62ccf 2022 *Function name
3aee1200 2023 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 2024 *Input params
3aee1200 2025 * tracefile : opened trace file
2026 * event_type : the event type
6cd62ccf 2027 ****************************************************************************/
2028
3aee1200 2029void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 2030{
3aee1200 2031 LttField *field = event_type->root_field;
2032 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
2033
2034 if(likely(field))
2035 preset_field_type_size(tf, event_type, 0, 0,
2036 &fixed_root, &fixed_parent,
2037 field);
2038
2039}
f104d082 2040#endif //0
2041
2042
2043/*****************************************************************************
2044 *Function name
2045 * get_alignment : Get the alignment needed for a field.
2046 *Input params
2047 * tf : tracefile
2048 * field : field
2049 *
2050 * returns : The size on which it must be aligned.
2051 *
2052 ****************************************************************************/
2053off_t get_alignment(LttTracefile *tf, LttField *field)
2054{
2312de30 2055 LttType *type = &field->field_type;
f104d082 2056
2057 switch(type->type_class) {
2058 case LTT_INT_FIXED:
2059 case LTT_UINT_FIXED:
2060 case LTT_POINTER:
2061 case LTT_CHAR:
2062 case LTT_UCHAR:
2063 case LTT_SHORT:
2064 case LTT_USHORT:
2065 case LTT_INT:
2066 case LTT_UINT:
2067 case LTT_LONG:
2068 case LTT_ULONG:
2069 case LTT_SIZE_T:
2070 case LTT_SSIZE_T:
2071 case LTT_OFF_T:
2072 case LTT_FLOAT:
2073 case LTT_ENUM:
2074 /* Align offset on type size */
2075 return field->field_size;
2076 break;
2077 case LTT_STRING:
2078 return 0;
2079 break;
2080 case LTT_ARRAY:
2081 g_assert(type->fields->len == 1);
2082 {
2083 LttField *child = &g_array_index(type->fields, LttField, 0);
2084 return get_alignment(tf, child);
2085 }
2086 break;
2087 case LTT_SEQUENCE:
2088 g_assert(type->fields->len == 2);
2089 {
2090 off_t localign = 0;
2091 LttField *child = &g_array_index(type->fields, LttField, 0);
2092
2093 localign = max(localign, get_alignment(tf, child));
2094
2095 child = &g_array_index(type->fields, LttField, 1);
2096 localign = max(localign, get_alignment(tf, child));
2097
2098 return localign;
2099 }
2100 break;
2101 case LTT_STRUCT:
2102 case LTT_UNION:
2103 {
2104 guint i;
2105 off_t localign = 0;
2106
2107 for(i=0; i<type->fields->len; i++) {
2108 LttField *child = &g_array_index(type->fields, LttField, i);
2109 localign = max(localign, get_alignment(tf, child));
2110 }
2111 return localign;
2112 }
2113 break;
2114 case LTT_NONE:
2115 default:
2116 g_error("get_alignment : unknown type");
2117 }
2118
2119}
2120
2121/*****************************************************************************
2122 *Function name
2123 * field_compute_static_size : Determine the size of fields known by their
2124 * sole definition. Unions, arrays and struct sizes might be known, but
2125 * the parser does not give that information.
2126 *Input params
2127 * tf : tracefile
2128 * field : field
2129 *
2130 ****************************************************************************/
2131
2132void field_compute_static_size(LttTracefile *tf, LttField *field)
2133{
2312de30 2134 LttType *type = &field->field_type;
f104d082 2135
2136 switch(type->type_class) {
2137 case LTT_INT_FIXED:
2138 case LTT_UINT_FIXED:
2139 case LTT_POINTER:
2140 case LTT_CHAR:
2141 case LTT_UCHAR:
2142 case LTT_SHORT:
2143 case LTT_USHORT:
2144 case LTT_INT:
2145 case LTT_UINT:
2146 case LTT_LONG:
2147 case LTT_ULONG:
2148 case LTT_SIZE_T:
2149 case LTT_SSIZE_T:
2150 case LTT_OFF_T:
2151 case LTT_FLOAT:
2152 case LTT_ENUM:
2153 case LTT_STRING:
2154 /* nothing to do */
2155 break;
2156 case LTT_ARRAY:
2157 /* note this : array type size is the number of elements in the array,
2158 * while array field size of the length of the array in bytes */
2159 g_assert(type->fields->len == 1);
2160 {
2161 LttField *child = &g_array_index(type->fields, LttField, 0);
2162 field_compute_static_size(tf, child);
2163
2164 if(child->field_size != 0) {
2165 field->field_size = type->size * child->field_size;
2166 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2167 sizeof(off_t), type->size);
2168 } else {
2169 field->field_size = 0;
2170 }
2171 }
2172 break;
2173 case LTT_SEQUENCE:
2174 g_assert(type->fields->len == 2);
2175 {
2312de30 2176 off_t local_offset = 0;
f104d082 2177 LttField *child = &g_array_index(type->fields, LttField, 1);
2178 field_compute_static_size(tf, child);
2179 field->field_size = 0;
2180 type->size = 0;
2181 if(child->field_size != 0) {
2182 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2183 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
2184 }
2185 }
2186 break;
2187 case LTT_STRUCT:
2188 case LTT_UNION:
2189 {
2190 guint i;
2191 for(i=0;i<type->fields->len;i++) {
2192 LttField *child = &g_array_index(type->fields, LttField, i);
2193 field_compute_static_size(tf, child);
2194 if(child->field_size != 0) {
2195 type->size += ltt_align(type->size, get_alignment(tf, child),
2196 tf->has_alignment);
2197 type->size += child->field_size;
2198 } else {
2199 /* As soon as we find a child with variable size, we have
2200 * a variable size */
2201 type->size = 0;
2202 break;
2203 }
2204 }
2205 field->field_size = type->size;
2206 }
2207 break;
2208 default:
2209 g_error("field_static_size : unknown type");
2312de30 2210 }
f104d082 2211
2212}
2213
2214
2215
2216/*****************************************************************************
2217 *Function name
2218 * precompute_fields_offsets : set the precomputable offset of the fields
2219 *Input params
2220 * tf : tracefile
2221 * field : the field
2222 * offset : pointer to the current offset, must be incremented
2223 *
2224 * return : 1 : found a variable length field, stop the processing.
2225 * 0 otherwise.
2226 ****************************************************************************/
2227
2228
2229gint precompute_fields_offsets(LttTracefile *tf, LttField *field, off_t *offset)
2230{
2312de30 2231 LttType *type = &field->field_type;
f104d082 2232
2233 switch(type->type_class) {
2234 case LTT_INT_FIXED:
2235 case LTT_UINT_FIXED:
2236 case LTT_POINTER:
2237 case LTT_CHAR:
2238 case LTT_UCHAR:
2239 case LTT_SHORT:
2240 case LTT_USHORT:
2241 case LTT_INT:
2242 case LTT_UINT:
2243 case LTT_LONG:
2244 case LTT_ULONG:
2245 case LTT_SIZE_T:
2246 case LTT_SSIZE_T:
2247 case LTT_OFF_T:
2248 case LTT_FLOAT:
2249 case LTT_ENUM:
2250 /* Align offset on type size */
2251 *offset += ltt_align(*offset, get_alignment(tf, field),
2252 tf->has_alignment);
2253 /* remember offset */
2254 field->offset_root = *offset;
2255 field->fixed_root = FIELD_FIXED;
2256 /* Increment offset */
2257 *offset += field->field_size;
2258 return 0;
2259 break;
2260 case LTT_STRING:
2261 field->offset_root = *offset;
2262 field->fixed_root = FIELD_FIXED;
2263 return 1;
2264 break;
2265 case LTT_ARRAY:
2266 g_assert(type->fields->len == 1);
2267 {
2268 LttField *child = &g_array_index(type->fields, LttField, 0);
2269
2270 *offset += ltt_align(*offset, get_alignment(tf, field),
2271 tf->has_alignment);
2272
2273 /* remember offset */
2274 field->offset_root = *offset;
2275 field->array_offset = *offset;
2276 field->fixed_root = FIELD_FIXED;
2277
2278 /* Let the child be variable */
2279 //precompute_fields_offsets(tf, child, offset);
2280
2281 if(field->field_size != 0) {
2282 /* Increment offset */
2283 /* field_size is the array size in bytes */
2284 *offset += field->field_size;
2285 return 0;
2286 } else {
2287 return 1;
2288 }
2289 }
2290 break;
2291 case LTT_SEQUENCE:
2292 g_assert(type->fields->len == 2);
2293 {
2294 LttField *child;
2295 guint ret;
2296
2297 *offset += ltt_align(*offset, get_alignment(tf, field),
2298 tf->has_alignment);
2299
2300 /* remember offset */
2301 field->offset_root = *offset;
2302 field->fixed_root = FIELD_FIXED;
2303
2304 child = &g_array_index(type->fields, LttField, 0);
2305 ret = precompute_fields_offsets(tf, child, offset);
2306 g_assert(ret == 0); /* Seq len cannot have variable len */
2307
2308 child = &g_array_index(type->fields, LttField, 1);
2309 *offset += ltt_align(*offset, get_alignment(tf, child),
2310 tf->has_alignment);
2311 field->array_offset = *offset;
2312 /* Set the offset position at position 0 */
2313 ret = precompute_fields_offsets(tf, child, offset);
2314
2315 /* Cannot precompute fields offsets of sequence members, and has
2316 * variable length. */
2317 return 1;
2318 }
2319 break;
2320 case LTT_STRUCT:
2321 {
2322 LttField *child;
2323 guint i;
2324 gint ret=0;
2325
2326 *offset += ltt_align(*offset, get_alignment(tf, field),
2327 tf->has_alignment);
2328 /* remember offset */
2329 field->offset_root = *offset;
2330 field->fixed_root = FIELD_FIXED;
2331
2332 for(i=0; i< type->fields->len; i++) {
2333 child = &g_array_index(type->fields, LttField, i);
2334 ret = precompute_fields_offsets(tf, child, offset);
2335
2336 if(ret) break;
2337 }
2338 return ret;
2339 }
2340 break;
2341 case LTT_UNION:
2342 {
2343 LttField *child;
2344 guint i;
2345 gint ret=0;
2346
2347 *offset += ltt_align(*offset, get_alignment(tf, field),
2348 tf->has_alignment);
2349 /* remember offset */
2350 field->offset_root = *offset;
2351 field->fixed_root = FIELD_FIXED;
2352
2353 for(i=0; i< type->fields->len; i++) {
2354 *offset = field->offset_root;
2355 child = &g_array_index(type->fields, LttField, i);
2356 ret = precompute_fields_offsets(tf, child, offset);
2357
2358 if(ret) break;
2359 }
2360 *offset = field->offset_root + field->field_size;
2361 return ret;
2362 }
2363
2364 break;
2365 case LTT_NONE:
2366 default:
2367 g_error("precompute_fields_offsets : unknown type");
2368 return 1;
2369 }
2370
2371}
2372
2373
2374/*****************************************************************************
2375 *Function name
2376 * precompute_offsets : set the precomputable offset of an event type
2377 *Input params
2378 * tf : tracefile
2379 * event : event type
2380 *
2381 ****************************************************************************/
2382void precompute_offsets(LttTracefile *tf, LttEventType *event)
2383{
2384 guint i;
2385 off_t offset = 0;
2386 gint ret;
2387
2388 /* First, compute the size of fixed size fields. Will determine size for
2389 * arrays, struct and unions, which is not done by the parser */
2390 for(i=0; i<event->fields->len; i++) {
2391 LttField *field = &g_array_index(event->fields, LttField, i);
2392 field_compute_static_size(tf, field);
2393 }
2394
2395 /* Precompute all known offsets */
2396 for(i=0; i<event->fields->len; i++) {
2397 LttField *field = &g_array_index(event->fields, LttField, i);
2398 ret = precompute_fields_offsets(tf, field, &offset);
2399 if(ret) break;
2400 }
2401}
2402
2403
e4eced0f 2404
bbf28e50 2405
3aee1200 2406/*****************************************************************************
2407 *Function name
2408 * preset_field_type_size : set the fixed sizes of the field type
2409 *Input params
2410 * tf : tracefile
2411 * event_type : event type
2412 * offset_root : offset from the root
2413 * offset_parent : offset from the parent
2414 * fixed_root : Do we know a fixed offset to the root ?
2415 * fixed_parent : Do we know a fixed offset to the parent ?
2416 * field : field
2417 ****************************************************************************/
f104d082 2418
2419
2420
2421// preset the fixed size offsets. Calculate them just like genevent-new : an
2422// increment of a *to value that represents the offset from the start of the
2423// event data.
2424// The preset information is : offsets up to (and including) the first element
2425// of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2426#if 0
3aee1200 2427void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
2428 off_t offset_root, off_t offset_parent,
2429 enum field_status *fixed_root, enum field_status *fixed_parent,
2430 LttField *field)
2431{
2432 enum field_status local_fixed_root, local_fixed_parent;
2433 guint i;
2434 LttType *type;
dfb73233 2435
3aee1200 2436 g_assert(field->fixed_root == FIELD_UNKNOWN);
2437 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2438 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 2439
3aee1200 2440 type = field->field_type;
2441
2442 field->fixed_root = *fixed_root;
2443 if(field->fixed_root == FIELD_FIXED)
2444 field->offset_root = offset_root;
2445 else
2446 field->offset_root = 0;
2447
2448 field->fixed_parent = *fixed_parent;
2449 if(field->fixed_parent == FIELD_FIXED)
2450 field->offset_parent = offset_parent;
2451 else
2452 field->offset_parent = 0;
2453
2454 size_t current_root_offset;
2455 size_t current_offset;
2456 enum field_status current_child_status, final_child_status;
2457 size_t max_size;
2458
2459 switch(type->type_class) {
2460 case LTT_INT:
2461 case LTT_UINT:
2462 case LTT_FLOAT:
2463 case LTT_ENUM:
2464 field->field_size = ltt_type_size(tf->trace, type);
2465 field->fixed_size = FIELD_FIXED;
2466 break;
2467 case LTT_POINTER:
2468 field->field_size = (off_t)event_type->facility->pointer_size;
2469 field->fixed_size = FIELD_FIXED;
2470 break;
2471 case LTT_LONG:
2472 case LTT_ULONG:
cb03932a 2473 field->field_size = (off_t)event_type->facility->long_size;
3aee1200 2474 field->fixed_size = FIELD_FIXED;
2475 break;
2476 case LTT_SIZE_T:
2477 case LTT_SSIZE_T:
2478 case LTT_OFF_T:
2479 field->field_size = (off_t)event_type->facility->size_t_size;
2480 field->fixed_size = FIELD_FIXED;
2481 break;
2482 case LTT_SEQUENCE:
2483 local_fixed_root = FIELD_VARIABLE;
2484 local_fixed_parent = FIELD_VARIABLE;
2485 preset_field_type_size(tf, event_type,
2486 0, 0,
2487 &local_fixed_root, &local_fixed_parent,
2488 field->child[0]);
2489 field->fixed_size = FIELD_VARIABLE;
2490 field->field_size = 0;
27304273 2491 *fixed_root = FIELD_VARIABLE;
2492 *fixed_parent = FIELD_VARIABLE;
3aee1200 2493 break;
2494 case LTT_STRING:
2495 field->fixed_size = FIELD_VARIABLE;
2496 field->field_size = 0;
27304273 2497 *fixed_root = FIELD_VARIABLE;
2498 *fixed_parent = FIELD_VARIABLE;
3aee1200 2499 break;
2500 case LTT_ARRAY:
2501 local_fixed_root = FIELD_VARIABLE;
2502 local_fixed_parent = FIELD_VARIABLE;
2503 preset_field_type_size(tf, event_type,
2504 0, 0,
2505 &local_fixed_root, &local_fixed_parent,
2506 field->child[0]);
2507 field->fixed_size = field->child[0]->fixed_size;
27304273 2508 if(field->fixed_size == FIELD_FIXED) {
3aee1200 2509 field->field_size = type->element_number * field->child[0]->field_size;
27304273 2510 } else {
3aee1200 2511 field->field_size = 0;
27304273 2512 *fixed_root = FIELD_VARIABLE;
2513 *fixed_parent = FIELD_VARIABLE;
2514 }
3aee1200 2515 break;
2516 case LTT_STRUCT:
2517 current_root_offset = field->offset_root;
2518 current_offset = 0;
2519 current_child_status = FIELD_FIXED;
2520 for(i=0;i<type->element_number;i++) {
2521 preset_field_type_size(tf, event_type,
2522 current_root_offset, current_offset,
2523 fixed_root, &current_child_status,
2524 field->child[i]);
2525 if(current_child_status == FIELD_FIXED) {
2526 current_root_offset += field->child[i]->field_size;
2527 current_offset += field->child[i]->field_size;
2528 } else {
2529 current_root_offset = 0;
2530 current_offset = 0;
2531 }
2532 }
2533 if(current_child_status != FIELD_FIXED) {
2534 *fixed_parent = current_child_status;
2535 field->field_size = 0;
2536 field->fixed_size = current_child_status;
2537 } else {
2538 field->field_size = current_offset;
2539 field->fixed_size = FIELD_FIXED;
2540 }
2541 break;
2542 case LTT_UNION:
2543 current_root_offset = field->offset_root;
2544 current_offset = 0;
2545 max_size = 0;
2546 final_child_status = FIELD_FIXED;
2547 for(i=0;i<type->element_number;i++) {
2548 enum field_status current_root_child_status = FIELD_FIXED;
2549 enum field_status current_child_status = FIELD_FIXED;
2550 preset_field_type_size(tf, event_type,
2551 current_root_offset, current_offset,
2552 &current_root_child_status, &current_child_status,
2553 field->child[i]);
2554 if(current_child_status != FIELD_FIXED)
2555 final_child_status = current_child_status;
2556 else
2557 max_size = max(max_size, field->child[i]->field_size);
2558 }
2559 if(final_child_status != FIELD_FIXED) {
e2542235 2560 g_error("LTTV does not support variable size fields in unions.");
2561 /* This will stop the application. */
3aee1200 2562 *fixed_root = final_child_status;
2563 *fixed_parent = final_child_status;
2564 field->field_size = 0;
2565 field->fixed_size = current_child_status;
2566 } else {
2567 field->field_size = max_size;
2568 field->fixed_size = FIELD_FIXED;
2569 }
2570 break;
dfb73233 2571 }
2572
6cd62ccf 2573}
f104d082 2574#endif //0
3aee1200 2575
77175651 2576/*****************************************************************************
2577 *Function name
2578 * check_fields_compatibility : Check for compatibility between two fields :
2579 * do they use the same inner structure ?
2580 *Input params
2581 * event_type1 : event type
2582 * event_type2 : event type
2583 * field1 : field
2584 * field2 : field
2585 *Returns : 0 if identical
2586 * 1 if not.
2587 ****************************************************************************/
f104d082 2588// this function checks for equality of field types. Therefore, it does not use
2589// per se offsets. For instance, an aligned version of a structure is
2590// compatible with an unaligned version of the same structure.
2591gint check_fields_compatibility(LttEventType *event_type1,
2592 LttEventType *event_type2,
2593 LttField *field1, LttField *field2)
2594{
2595 guint different = 0;
2312de30 2596 LttType *type1;
2597 LttType *type2;
f104d082 2598
2599 if(field1 == NULL) {
2600 if(field2 == NULL) goto end;
2601 else {
2602 different = 1;
2603 goto end;
2604 }
2605 } else if(field2 == NULL) {
2606 different = 1;
2607 goto end;
2608 }
2609
2312de30 2610 type1 = &field1->field_type;
2611 type2 = &field2->field_type;
f104d082 2612
2613 if(type1->type_class != type2->type_class) {
2614 different = 1;
2615 goto end;
2616 }
f104d082 2617
2618 switch(type1->type_class) {
2619 case LTT_INT_FIXED:
2620 case LTT_UINT_FIXED:
2621 case LTT_POINTER:
2622 case LTT_CHAR:
2623 case LTT_UCHAR:
2624 case LTT_SHORT:
2625 case LTT_USHORT:
2626 case LTT_INT:
2627 case LTT_UINT:
2628 case LTT_LONG:
2629 case LTT_ULONG:
2630 case LTT_SIZE_T:
2631 case LTT_SSIZE_T:
2632 case LTT_OFF_T:
2633 case LTT_FLOAT:
2634 case LTT_ENUM:
2635 if(field1->field_size != field2->field_size)
2636 different = 1;
2637 break;
2638 case LTT_STRING:
2639 break;
2640 case LTT_ARRAY:
2641 {
2642 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2643 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2644
2645 if(type1->size != type2->size)
2646 different = 1;
2647 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2648 different = 1;
2649 }
2650 break;
2651 case LTT_SEQUENCE:
2652 {
2653 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2654 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2655
2656 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2657 different = 1;
2658 }
2659 break;
2660 case LTT_STRUCT:
2661 case LTT_UNION:
2662 {
2663 LttField *child;
2664 guint i;
2665
2666 if(type1->fields->len != type2->fields->len) {
2667 different = 1;
2668 goto end;
2669 }
2670
2671 for(i=0; i< type1->fields->len; i++) {
2312de30 2672 LttField *child1;
2673 LttField *child2;
f104d082 2674 child1 = &g_array_index(type1->fields, LttField, i);
2675 child2 = &g_array_index(type2->fields, LttField, i);
2676 different = check_fields_compatibility(event_type1,
2677 event_type2, child1, child2);
2678
2679 if(different) break;
2680 }
2681 }
2682 break;
2683 case LTT_NONE:
2684 default:
2685 g_error("precompute_fields_offsets : unknown type");
2686 }
2687
2688end:
2689 return different;
2690}
2691
2692
2693#if 0
77175651 2694gint check_fields_compatibility(LttEventType *event_type1,
2695 LttEventType *event_type2,
2696 LttField *field1, LttField *field2)
2697{
2698 guint different = 0;
77175651 2699 guint i;
2700 LttType *type1;
2701 LttType *type2;
2702
2703 if(field1 == NULL) {
2704 if(field2 == NULL) goto end;
2705 else {
2706 different = 1;
2707 goto end;
2708 }
2709 } else if(field2 == NULL) {
2710 different = 1;
2711 goto end;
2712 }
2713
2714 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2715 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2716 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2717 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2718 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2719 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2720
2721 type1 = field1->field_type;
2722 type2 = field2->field_type;
2723
77175651 2724 if(type1->type_class != type2->type_class) {
2725 different = 1;
2726 goto end;
2727 }
2728 if(type1->element_name != type2->element_name) {
2729 different = 1;
2730 goto end;
2731 }
2732
2733 switch(type1->type_class) {
2734 case LTT_INT:
2735 case LTT_UINT:
2736 case LTT_FLOAT:
2737 case LTT_POINTER:
2738 case LTT_LONG:
2739 case LTT_ULONG:
2740 case LTT_SIZE_T:
2741 case LTT_SSIZE_T:
2742 case LTT_OFF_T:
2743 if(field1->field_size != field2->field_size) {
2744 different = 1;
2745 goto end;
2746 }
2747 break;
2748 case LTT_ENUM:
2749 if(type1->element_number != type2->element_number) {
2750 different = 1;
2751 goto end;
2752 }
2753 for(i=0;i<type1->element_number;i++) {
2754 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2755 different = 1;
2756 goto end;
2757 }
2758 }
2759 break;
2760 case LTT_SEQUENCE:
2761 /* Two elements : size and child */
2762 g_assert(type1->element_number != type2->element_number);
2763 for(i=0;i<type1->element_number;i++) {
2764 if(check_fields_compatibility(event_type1, event_type2,
2765 field1->child[0], field2->child[0])) {
2766 different = 1;
2767 goto end;
2768 }
2769 }
2770 break;
2771 case LTT_STRING:
2772 break;
2773 case LTT_ARRAY:
2774 if(field1->field_size != field2->field_size) {
2775 different = 1;
2776 goto end;
2777 }
2778 /* Two elements : size and child */
2779 g_assert(type1->element_number != type2->element_number);
2780 for(i=0;i<type1->element_number;i++) {
2781 if(check_fields_compatibility(event_type1, event_type2,
2782 field1->child[0], field2->child[0])) {
2783 different = 1;
2784 goto end;
2785 }
2786 }
2787 break;
2788 case LTT_STRUCT:
2789 case LTT_UNION:
2790 if(type1->element_number != type2->element_number) {
2791 different = 1;
2792 break;
2793 }
2794 for(i=0;i<type1->element_number;i++) {
2795 if(check_fields_compatibility(event_type1, event_type2,
2796 field1->child[0], field2->child[0])) {
2797 different = 1;
2798 goto end;
2799 }
2800 }
2801 break;
2802 }
2803end:
2804 return different;
2805}
3aee1200 2806#endif //0
6cd62ccf 2807
f104d082 2808
6cd62ccf 2809/*****************************************************************************
2810 *Function name
eed2ef37 2811 * ltt_get_int : get an integer number
6cd62ccf 2812 *Input params
3aee1200 2813 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2814 * size : the size of the integer
3aee1200 2815 * ptr : the data pointer
6cd62ccf 2816 *Return value
cf74a6f1 2817 * gint64 : a 64 bits integer
6cd62ccf 2818 ****************************************************************************/
2819
eed2ef37 2820gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2821{
3aee1200 2822 gint64 val;
cf74a6f1 2823
2824 switch(size) {
3aee1200 2825 case 1: val = *((gint8*)data); break;
2826 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2827 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2828 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2829 default: val = ltt_get_int64(reverse_byte_order, data);
2830 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2831 break;
2832 }
2833
3aee1200 2834 return val;
6cd62ccf 2835}
3aee1200 2836
6cd62ccf 2837/*****************************************************************************
2838 *Function name
eed2ef37 2839 * ltt_get_uint : get an unsigned integer number
6cd62ccf 2840 *Input params
3aee1200 2841 * reverse_byte_order: must we reverse the byte order ?
2842 * size : the size of the integer
2843 * ptr : the data pointer
2844 *Return value
2845 * guint64 : a 64 bits unsigned integer
6cd62ccf 2846 ****************************************************************************/
2847
eed2ef37 2848guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2849{
3aee1200 2850 guint64 val;
2851
2852 switch(size) {
2853 case 1: val = *((gint8*)data); break;
2854 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2855 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2856 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2857 default: val = ltt_get_uint64(reverse_byte_order, data);
2858 g_critical("get_uint : unsigned integer size %d unknown",
2859 size);
2860 break;
2861 }
2862
2863 return val;
6cd62ccf 2864}
3aee1200 2865
2866
a5dcde2f 2867/* get the node name of the system */
2868
2869char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2870{
2871 return s->node_name;
2872}
2873
2874
2875/* get the domain name of the system */
2876
2877char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2878{
2879 return s->domain_name;
2880}
2881
2882
2883/* get the description of the system */
2884
2885char * ltt_trace_system_description_description (LttSystemDescription * s)
2886{
2887 return s->description;
2888}
2889
2890
bf33dd50 2891/* get the NTP corrected start time of the trace */
7bd563ec 2892LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 2893{
7bd563ec 2894 return t->start_time;
a5dcde2f 2895}
2896
bf33dd50 2897/* get the monotonic start time of the trace */
2898LttTime ltt_trace_start_time_monotonic(LttTrace *t)
2899{
2900 return t->start_time_from_tsc;
2901}
2902
18206708 2903LttTracefile *ltt_tracefile_new()
2904{
2905 return g_new(LttTracefile, 1);
2906}
2907
2908void ltt_tracefile_destroy(LttTracefile *tf)
2909{
2910 g_free(tf);
2911}
2912
2913void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
2914{
2915 *dest = *src;
2916}
2917
3aee1200 2918/* Before library loading... */
2919
2920static void __attribute__((constructor)) init(void)
2921{
2922 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2923 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2924
3865ea09 2925 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
3aee1200 2926}
2927
This page took 0.202179 seconds and 4 git commands to generate.