genevent for 0.1.99.1
[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
74/* obtain the time of an event */
75
76static inline LttTime getEventTime(LttTracefile * tf);
77
78
6cd62ccf 79/* set the offset of the fields belonging to the event,
80 need the information of the archecture */
3aee1200 81void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
82size_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. */
86static inline void preset_field_type_size(LttTracefile *tf,
87 LttEventType *event_type,
88 off_t offset_root, off_t offset_parent,
89 enum field_status *fixed_root, enum field_status *fixed_parent,
90 LttField *field);
6cd62ccf 91
6cd62ccf 92
3aee1200 93static inline size_t get_field_type_size(LttTracefile *tf,
94 LttEventType *event_type,
95 off_t offset_root, off_t offset_parent,
96 LttField *field, void *data);
6cd62ccf 97
6cd62ccf 98
3aee1200 99/* map a fixed size or a block information from the file (fd) */
100static gint map_block(LttTracefile * tf, guint block_num);
101
102/* calculate nsec per cycles for current block */
103static double calc_nsecs_per_cycle(LttTracefile * t);
6cd62ccf 104
3aee1200 105/* go to the next event */
106static int ltt_seek_next_event(LttTracefile *tf);
6cd62ccf 107
3aee1200 108#if 0
43da6a59 109/* Functions to parse system.xml file (using glib xml parser) */
51b5991e 110static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
43da6a59 111 const gchar *element_name,
112 const gchar **attribute_names,
113 const gchar **attribute_values,
114 gpointer user_data,
115 GError **error)
116{
117 int i=0;
118 LttSystemDescription* des = (LttSystemDescription* )user_data;
119 if(strcmp("system", element_name)){
2a74fbf4 120 *error = g_error_new(G_MARKUP_ERROR,
121 G_LOG_LEVEL_WARNING,
122 "This is not system.xml file");
123 return;
43da6a59 124 }
125
126 while(attribute_names[i]){
127 if(strcmp("node_name", attribute_names[i])==0){
128 des->node_name = g_strdup(attribute_values[i]);
129 }else if(strcmp("domainname", attribute_names[i])==0){
130 des->domain_name = g_strdup(attribute_values[i]);
131 }else if(strcmp("cpu", attribute_names[i])==0){
132 des->nb_cpu = atoi(attribute_values[i]);
133 }else if(strcmp("arch_size", attribute_names[i])==0){
134 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
135 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
136 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
137 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
138 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
139 }else if(strcmp("endian", attribute_names[i])==0){
140 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
141 des->endian = LTT_LITTLE_ENDIAN;
142 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
143 des->endian = LTT_BIG_ENDIAN;
144 }else if(strcmp("kernel_name", attribute_names[i])==0){
145 des->kernel_name = g_strdup(attribute_values[i]);
146 }else if(strcmp("kernel_release", attribute_names[i])==0){
147 des->kernel_release = g_strdup(attribute_values[i]);
148 }else if(strcmp("kernel_version", attribute_names[i])==0){
149 des->kernel_version = g_strdup(attribute_values[i]);
150 }else if(strcmp("machine", attribute_names[i])==0){
151 des->machine = g_strdup(attribute_values[i]);
152 }else if(strcmp("processor", attribute_names[i])==0){
153 des->processor = g_strdup(attribute_values[i]);
154 }else if(strcmp("hardware_platform", attribute_names[i])==0){
155 des->hardware_platform = g_strdup(attribute_values[i]);
156 }else if(strcmp("operating_system", attribute_names[i])==0){
157 des->operating_system = g_strdup(attribute_values[i]);
158 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
159 des->ltt_major_version = atoi(attribute_values[i]);
160 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
161 des->ltt_minor_version = atoi(attribute_values[i]);
162 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
163 des->ltt_block_size = atoi(attribute_values[i]);
164 }else{
2a74fbf4 165 *error = g_error_new(G_MARKUP_ERROR,
166 G_LOG_LEVEL_WARNING,
167 "Not a valid attribute");
168 return;
43da6a59 169 }
170 i++;
171 }
172}
173
51b5991e 174static void parser_characters (GMarkupParseContext __UNUSED__ *context,
43da6a59 175 const gchar *text,
51b5991e 176 gsize __UNUSED__ text_len,
43da6a59 177 gpointer user_data,
51b5991e 178 GError __UNUSED__ **error)
43da6a59 179{
180 LttSystemDescription* des = (LttSystemDescription* )user_data;
181 des->description = g_strdup(text);
182}
3aee1200 183#endif //0
77175651 184LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
3aee1200 185 guint num)
186{
187 g_assert(num < t->facilities_by_num->len);
188
189 return &g_array_index(t->facilities_by_num, LttFacility, num);
190
191}
43da6a59 192
193
6cd62ccf 194/*****************************************************************************
195 *Function name
963b5f2d 196 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 197 *Input params
963b5f2d 198 * t : the trace containing the tracefile
199 * fileName : path name of the trace file
3aee1200 200 * tf : the tracefile structure
6cd62ccf 201 *Return value
3aee1200 202 * : 0 for success, -1 otherwise.
6cd62ccf 203 ****************************************************************************/
204
3aee1200 205gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
6cd62ccf 206{
963b5f2d 207 struct stat lTDFStat; /* Trace data file status */
3aee1200 208 struct ltt_block_start_header *header;
6cd62ccf 209
210 //open the file
3aee1200 211 tf->name = g_quark_from_string(fileName);
963b5f2d 212 tf->trace = t;
45e14832 213 tf->fd = g_open(fileName, O_RDONLY, 0);
6cd62ccf 214 if(tf->fd < 0){
2a74fbf4 215 g_warning("Unable to open input data file %s\n", fileName);
3aee1200 216 goto end;
6cd62ccf 217 }
218
219 // Get the file's status
220 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 221 g_warning("Unable to get the status of the input data file %s\n", fileName);
3aee1200 222 goto close_file;
6cd62ccf 223 }
224
225 // Is the file large enough to contain a trace
3aee1200 226 if(lTDFStat.st_size < (off_t)(sizeof(struct ltt_block_start_header))){
8710c6c7 227 g_print("The input data file %s does not contain a trace\n", fileName);
3aee1200 228 goto close_file;
229 }
230
231 /* Temporarily map the buffer start header to get trace information */
232 /* Multiple of pages aligned head */
233 tf->buffer.head = mmap(0, sizeof(struct ltt_block_start_header), PROT_READ,
234 MAP_PRIVATE, tf->fd, 0);
235 if(tf->buffer.head == NULL) {
236 perror("Error in allocating memory for buffer of tracefile");
237 goto close_file;
6cd62ccf 238 }
3aee1200 239 g_assert(((guint)tf->buffer.head & (8-1)) == 0); // make sure it's aligned.
240
241 header = (struct ltt_block_start_header*)tf->buffer.head;
6cd62ccf 242
3aee1200 243 if(header->trace.magic_number == LTT_MAGIC_NUMBER)
244 tf->reverse_bo = 0;
245 else if(header->trace.magic_number == LTT_REV_MAGIC_NUMBER)
246 tf->reverse_bo = 1;
247 else /* invalid magic number, bad tracefile ! */
248 goto unmap_file;
249
6cd62ccf 250 //store the size of the file
251 tf->file_size = lTDFStat.st_size;
3aee1200 252 tf->block_size = header->buf_size;
253 tf->num_blocks = tf->file_size / tf->block_size;
6cd62ccf 254
3aee1200 255 vfree(tf->buffer.head);
256 tf->buffer.head = NULL;
6cd62ccf 257
963b5f2d 258 //read the first block
3aee1200 259 if(map_block(tf,0)) {
260 perror("Cannot map block for tracefile");
261 goto close_file;
262 }
263
264 return 0;
6cd62ccf 265
3aee1200 266 /* Error */
267unmap_file:
268 munmap(tf->buffer.head, sizeof(struct ltt_block_start_header));
269close_file:
270 g_close(tf->fd);
271end:
272 return -1;
6cd62ccf 273}
274
3aee1200 275#if 0
6cd62ccf 276/*****************************************************************************
963b5f2d 277 *Open control and per cpu tracefiles
6cd62ccf 278 ****************************************************************************/
279
45e14832 280void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
6cd62ccf 281{
963b5f2d 282 LttTracefile * tf;
283 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 284 if(!tf) return;
963b5f2d 285 t->per_cpu_tracefile_number++;
286 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 287}
288
45e14832 289gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
6cd62ccf 290{
963b5f2d 291 LttTracefile * tf;
c02ea99f 292 LttEvent ev;
963b5f2d 293 LttFacility * f;
963b5f2d 294 void * pos;
295 FacilityLoad fLoad;
8d1e6362 296 unsigned int i;
963b5f2d 297
298 tf = ltt_tracefile_open(t,control_name);
2a74fbf4 299 if(!tf) {
300 g_warning("ltt_tracefile_open_control : bad file descriptor");
301 return -1;
302 }
963b5f2d 303 t->control_tracefile_number++;
304 g_ptr_array_add(t->control_tracefiles,tf);
305
306 //parse facilities tracefile to get base_id
542ceddd 307 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 308 while(1){
c02ea99f 309 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
40331ba8 310
c02ea99f 311 if(ev.event_id == TRACE_FACILITY_LOAD){
312 pos = ev.data;
45e14832 313 fLoad.name = (gchar*)pos;
47a166fc 314 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
cbd41522 315 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 316
317 for(i=0;i<t->facility_number;i++){
318 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
319 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
320 f->base_id = fLoad.base_code;
321 break;
322 }
323 }
2a74fbf4 324 if(i==t->facility_number) {
8d1e6362 325 g_warning("Facility: %s, checksum: %u is not found",
326 fLoad.name,(unsigned int)fLoad.checksum);
2a74fbf4 327 return -1;
328 }
c02ea99f 329 }else if(ev.event_id == TRACE_BLOCK_START){
40331ba8 330 continue;
c02ea99f 331 }else if(ev.event_id == TRACE_BLOCK_END){
40331ba8 332 break;
2a74fbf4 333 }else {
8d1e6362 334 g_warning("Not valid facilities trace file");
2a74fbf4 335 return -1;
336 }
963b5f2d 337 }
338 }
2a74fbf4 339 return 0;
6cd62ccf 340}
3aee1200 341#endif //0
6cd62ccf 342
343/*****************************************************************************
344 *Function name
963b5f2d 345 * ltt_tracefile_close: close a trace file,
6cd62ccf 346 *Input params
963b5f2d 347 * t : tracefile which will be closed
6cd62ccf 348 ****************************************************************************/
349
963b5f2d 350void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 351{
3aee1200 352 if(t->buffer.head != NULL)
353 munmap(t->buffer.head, t->buf_size);
354 g_close(t->fd);
963b5f2d 355}
6cd62ccf 356
6cd62ccf 357
963b5f2d 358/*****************************************************************************
359 *Get system information
360 ****************************************************************************/
3aee1200 361#if 0
45e14832 362gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
963b5f2d 363{
45e14832 364 int fd;
365 GIOChannel *iochan;
366 gchar *buf = NULL;
367 gsize length;
43da6a59 368
369 GMarkupParseContext * context;
2a74fbf4 370 GError * error = NULL;
43da6a59 371 GMarkupParser markup_parser =
372 {
373 parser_start_element,
8d1e6362 374 NULL,
43da6a59 375 parser_characters,
376 NULL, /* passthrough */
377 NULL /* error */
378 };
963b5f2d 379
45e14832 380 fd = g_open(pathname, O_RDONLY, 0);
381 if(fd == -1){
2a74fbf4 382 g_warning("Can not open file : %s\n", pathname);
383 return -1;
6cd62ccf 384 }
963b5f2d 385
45e14832 386 iochan = g_io_channel_unix_new(fd);
387
43da6a59 388 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
6cd62ccf 389
45e14832 390 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
391 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
392 != G_IO_STATUS_EOF) {
393
394 if(error != NULL) {
395 g_warning("Can not read xml file: \n%s\n", error->message);
396 g_error_free(error);
397 }
398 if(!g_markup_parse_context_parse(context, buf, length, &error)){
2a74fbf4 399 if(error != NULL) {
400 g_warning("Can not parse xml file: \n%s\n", error->message);
401 g_error_free(error);
402 }
403 g_markup_parse_context_free(context);
45e14832 404
405 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
406 if(error != NULL) {
407 g_warning("Can not close file: \n%s\n", error->message);
408 g_error_free(error);
409 }
410
411 close(fd);
2a74fbf4 412 return -1;
43da6a59 413 }
963b5f2d 414 }
2a74fbf4 415 g_markup_parse_context_free(context);
45e14832 416
417 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
418 if(error != NULL) {
419 g_warning("Can not close file: \n%s\n", error->message);
420 g_error_free(error);
421 }
422
423 g_close(fd);
424
425 g_free(buf);
2a74fbf4 426 return 0;
6cd62ccf 427}
3aee1200 428#endif //0
6cd62ccf 429
430/*****************************************************************************
963b5f2d 431 *The following functions get facility/tracefile information
6cd62ccf 432 ****************************************************************************/
3aee1200 433#if 0
45e14832 434gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
6cd62ccf 435{
45e14832 436 GDir * dir;
437 const gchar * name;
8d1e6362 438 unsigned int i,j;
963b5f2d 439 LttFacility * f;
440 LttEventType * et;
45e14832 441 gchar fullname[DIR_NAME_SIZE];
442 GError * error = NULL;
443
444 dir = g_dir_open(eventdefs, 0, &error);
963b5f2d 445
45e14832 446 if(error != NULL) {
447 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
448 g_error_free(error);
2a74fbf4 449 return -1;
450 }
963b5f2d 451
45e14832 452 while((name = g_dir_read_name(dir)) != NULL){
453 if(!g_pattern_match_simple("*.xml", name)) continue;
454 strcpy(fullname,eventdefs);
455 strcat(fullname,name);
456 ltt_facility_open(t,fullname);
457 }
458 g_dir_close(dir);
963b5f2d 459
963b5f2d 460 for(j=0;j<t->facility_number;j++){
8710c6c7 461 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 462 for(i=0; i<f->event_number; i++){
463 et = f->events[i];
40331ba8 464 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 465 }
466 }
2a74fbf4 467 return 0;
963b5f2d 468}
3aee1200 469#endif //0
6cd62ccf 470
963b5f2d 471/*****************************************************************************
472 *A trace is specified as a pathname to the directory containing all the
473 *associated data (control tracefiles, per cpu tracefiles, event
474 *descriptions...).
475 *
476 *When a trace is closed, all the associated facilities, types and fields
477 *are released as well.
8d1e6362 478 */
479
480
481/****************************************************************************
482 * get_absolute_pathname
803229fa 483 *
8d1e6362 484 * return the unique pathname in the system
485 *
ef35d837 486 * MD : Fixed this function so it uses realpath, dealing well with
487 * forgotten cases (.. were not used correctly before).
803229fa 488 *
963b5f2d 489 ****************************************************************************/
45e14832 490void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
9f797243 491{
9f797243 492 abs_pathname[0] = '\0';
803229fa 493
ef35d837 494 if ( realpath (pathname, abs_pathname) != NULL)
495 return;
496 else
497 {
8d1e6362 498 /* error, return the original path unmodified */
ef35d837 499 strcpy(abs_pathname, pathname);
9f797243 500 return;
501 }
ef35d837 502 return;
9f797243 503}
504
3aee1200 505/* Search for something like : .*_.*
506 *
507 * The left side is the name, the right side is the number.
508 */
509
510int get_tracefile_name_number(const gchar *raw_name,
511 GQuark *name,
512 guint *num)
6cd62ccf 513{
3aee1200 514 guint raw_name_len = strlen(raw_name);
515 gchar char_name[PATH_MAX];
516 gchar *digit_begin;
517 int i;
518 int underscore_pos;
519 long int cpu_num;
520 gchar *endptr;
521
522 for(i=raw_name_len-1;i>=0;i--) {
523 if(raw_name[i] == '_') break;
524 }
525 if(i==0) /* Either not found or name length is 0 */
526 return -1;
527 underscore_pos = i;
963b5f2d 528
3aee1200 529 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
530
531 if(endptr == raw_name+underscore_pos+1)
532 return -1; /* No digit */
533 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
534 return -1; /* underflow / overflow */
1a2ceb63 535
3aee1200 536 strncpy(char_name, raw_name, underscore_pos);
1a2ceb63 537
3aee1200 538 *name = g_quark_from_string(char_name);
539 *num = cpu_num;
b333a76b 540
3aee1200 541 return 0;
542}
963b5f2d 543
3aee1200 544
77175651 545GData *ltt_trace_get_tracefiles_groups(LttTrace *trace)
546{
547 return trace->tracefiles;
548}
549
550
551void compute_tracefile_group(GArray *group,
552 struct compute_tracefile_group_args args)
553{
554 int i;
555 LttTracefile *tf;
556
557 for(i=0; i<group->len; i++) {
558 tf = &g_array_index (group, LttTracefile, i);
559 if(tf->cpu_online)
560 args.func(tf, args.func_args);
561 }
562}
563
564
3aee1200 565void ltt_tracefile_group_destroy(gpointer data)
566{
567 GArray *group = (GArray *)data;
568 int i;
569 LttTracefile *tf;
570
571 for(i=0; i<group->len; i++) {
572 tf = &g_array_index (group, LttTracefile, i);
573 if(tf->cpu_online)
574 ltt_tracefile_close(tf);
575 }
576 g_array_free(group, TRUE);
6cd62ccf 577}
578
3aee1200 579gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 580{
3aee1200 581 GArray *group = (GArray *)data;
582 int i;
583 LttTracefile *tf;
584
585 for(i=0; i<group->len; i++) {
586 tf = &g_array_index (group, LttTracefile, i);
587 if(tf->cpu_online) return 1;
588 }
589 return 0;
49bf71b5 590}
591
592
3aee1200 593/* Open each tracefile under a specific directory. Put them in a
594 * GData : permits to access them using their tracefile group pathname.
595 * i.e. access control/modules tracefile group by index :
596 * "control/module".
597 *
598 * A tracefile group is simply an array where all the per cpu tracefiles sits.
599 */
600
601static int open_tracefiles(LttTrace *trace, char *root_path, GData *tracefiles)
f7afe191 602{
3aee1200 603 DIR *dir = opendir(root_path);
604 struct dirent *entry;
605 struct stat stat_buf;
606 int ret;
607 char path[PATH_MAX];
608 int path_len;
609 char *path_ptr;
610
611 if(dir == NULL) {
612 perror(root_path);
613 return ENOENT;
614 }
615
616 strncpy(path, root_path, PATH_MAX-1);
617 path_len = strlen(path);
618 path[path_len] = '/';
619 path_len++;
620 path_ptr = path + path_len;
621
622 while((entry = readdir(dir)) != NULL) {
623
624 if(entry->d_name[0] == '.') continue;
625
626 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
627
628 ret = stat(path, &stat_buf);
629 if(ret == -1) {
630 perror(path);
631 continue;
632 }
633
634 g_debug("Tracefile file or directory : %s\n", path);
635
636 if(S_ISDIR(stat_buf.st_mode)) {
637
638 g_debug("Entering subdirectory...\n");
639 ret = open_tracefiles(trace, path, tracefiles);
640 if(ret < 0) continue;
641 } else if(S_ISREG(stat_buf.st_mode)) {
642 g_debug("Opening file.\n");
643
644 GQuark name;
645 guint num;
646 GArray *group;
647 LttTracefile *tf;
648 guint len;
649
650 if(get_tracefile_name_number(path, &name, &num))
651 continue; /* invalid name */
652
653 group = g_datalist_id_get_data(&tracefiles, name);
654 if(group == NULL) {
655 /* Elements are automatically cleared when the array is allocated.
656 * It makes the cpu_online variable set to 0 : cpu offline, by default.
657 */
658 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
659 g_datalist_id_set_data_full(&tracefiles, name,
660 group, ltt_tracefile_group_destroy);
661 }
662 /* Add the per cpu tracefile to the named group */
663 unsigned int old_len = group->len;
664 if(num+1 > old_len)
665 group = g_array_set_size(group, num+1);
666 tf = &g_array_index (group, LttTracefile, num);
667
668 if(ltt_tracefile_open(trace, path, tf)) {
669 g_info("Error opening tracefile %s", path);
670 g_array_set_size(group, old_len);
671
672 if(!ltt_tracefile_group_has_cpu_online(group))
673 g_datalist_id_remove_data(&tracefiles, name);
674
675 continue; /* error opening the tracefile : bad magic number ? */
676 }
677 tf->cpu_online = 1;
678 tf->cpu_num = num;
679 }
680 }
681
682 closedir(dir);
683
684 return 0;
f7afe191 685}
686
3aee1200 687/* ltt_get_facility_description
688 *
689 * Opens the trace corresponding to the requested facility (identified by fac_id
690 * and checksum).
691 *
692 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
693 *
694 * Returns 0 on success, or 1 on failure.
695 */
696
697static int ltt_get_facility_description(LttFacility *f,
698 LttTrace *t,
699 LttTracefile *fac_tf)
6cd62ccf 700{
3aee1200 701 char desc_file_name[PATH_MAX];
702 const gchar *text;
703 guint textlen;
704 gint err;
705 int i, j;
706 LttEventType *et;
707
708 text = g_quark_to_string(t->pathname);
709 textlen = strlen(text);
710
711 if(textlen >= PATH_MAX) goto name_error;
712 strcpy(desc_file_name, text);
713
714 text = "/eventdefs/";
715 textlen+=strlen(text);
716 if(textlen >= PATH_MAX) goto name_error;
717 strcat(desc_file_name, text);
718
719 text = g_quark_to_string(f->name);
720 textlen+=strlen(text);
721 if(textlen >= PATH_MAX) goto name_error;
722 strcat(desc_file_name, text);
723
724 text = "_";
725 textlen+=strlen(text);
726 if(textlen >= PATH_MAX) goto name_error;
727 strcat(desc_file_name, text);
963b5f2d 728
3aee1200 729 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
730 "%u", f->checksum);
731 if(err) goto name_error;
732
733 textlen=strlen(desc_file_name);
734
735 text = ".xml";
736 textlen+=strlen(text);
737 if(textlen >= PATH_MAX) goto name_error;
738 strcat(desc_file_name, text);
963b5f2d 739
3aee1200 740 err = ltt_facility_open(f, t, desc_file_name);
741 if(err) goto facility_error;
742
743 for(i=0;i<t->facilities_by_num->len;i++){
744 f = &g_array_index(t->facilities_by_num, LttFacility, i);
745 if(f->exists) {
746 for(j=0; j<f->events->len; j++){
747 et = &g_array_index(f->events, LttEventType, j);
748 set_fields_offsets(fac_tf, et);
749 }
750 }
963b5f2d 751 }
963b5f2d 752
3aee1200 753
754 return 0;
755
756facility_error:
757name_error:
758 return 1;
759}
760
761static void ltt_fac_ids_destroy(gpointer data)
762{
763 GArray *fac_ids = (GArray *)data;
764 int i;
765 LttFacility *fac;
766
767 for(i=0; i<fac_ids->len; i++) {
768 fac = &g_array_index (fac_ids, LttFacility, i);
769 ltt_facility_close(fac);
963b5f2d 770 }
963b5f2d 771
3aee1200 772 g_array_free(fac_ids, TRUE);
773}
774
775
776/* Presumes the tracefile is already seeked at the beginning. It makes sense,
777 * because it must be done just after the opening */
778static int ltt_process_facility_tracefile(LttTracefile *tf)
779{
780 int err;
781 LttFacility *fac;
782 GArray *fac_ids;
783
784 while(1) {
785 err = ltt_tracefile_read_seek(tf);
786 if(err == EPERM) goto seek_error;
787 else if(err == ERANGE) break; /* End of tracefile */
788
789 err = ltt_tracefile_read_update_event(tf);
790 if(err) goto update_error;
791
792 /* We are on a facility load/or facility unload/ or heartbeat event */
793 /* The rules are :
794 * * facility 0 is hardcoded : this is the core facility. It will be shown
795 * in the facility array though, and is shown as "loaded builtin" in the
796 * trace.
797 * It contains event :
798 * 0 : facility load
799 * 1 : facility unload
800 * 2 : state dump facility load
801 * Facility 1 : (heartbeat)
802 * 0 : heartbeat
803 */
804 if(tf->event.facility_id > 1) { /* Should only contain core and heartbeat
805 facilities */
806 g_warning("Error in processing facility file %s, "
807 "should not contain facility id %u.", g_quark_to_string(tf->name),
808 tf->event.facility_id);
809 err = EPERM;
810 goto fac_id_error;
811 } else if(tf->event.facility_id == LTT_FACILITY_CORE) {
812
813 struct LttFacilityLoad *fac_load_data;
814 char *fac_name;
815
816 // FIXME align
817 switch((enum ltt_core_events)tf->event.event_id) {
818 case LTT_EVENT_FACILITY_LOAD:
819 fac_load_data =
820 (struct LttFacilityLoad *)tf->event.data;
821 fac_name =
822 (char*)(tf->event.data + sizeof(struct LttFacilityLoad));
823 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
824 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
825 g_assert(fac->exists == 0);
826 fac->name = g_quark_from_string(fac_name);
827 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
828 &fac_load_data->checksum);
829 fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
830 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
831 &fac_load_data->pointer_size);
832 fac->size_t_size = ltt_get_uin32(LTT_GET_BO(tf),
833 &fac_load_data->size_t_size);
834 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
835 &fac_load_data->alignment);
836
837 if(ltt_get_facility_description(fac, tf->trace, tf))
838 goto facility_error;
839
840 fac->trace = tf->trace;
841 fac->exists = 1;
842
77175651 843 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
844 fac->name);
3aee1200 845 if(fac_ids == NULL) {
846 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
847 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
848 fac->name,
849 fac_ids, ltt_fac_ids_destroy);
850 }
851 g_array_append_val(fac_ids, fac->id);
852
853 break;
854 case LTT_EVENT_FACILITY_UNLOAD:
855 /* We don't care about unload : facilities ID are valid for the whole
856 * trace. They simply won't be used after the unload. */
857 break;
858 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
859 fac_load_data =
860 (struct LttFacilityLoad *)tf->event.data;
861 fac_name =
862 (char*)(tf->event.data + sizeof(struct LttFacilityLoad));
863 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
864 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
865 g_assert(fac->exists == 0);
866 fac->name = g_quark_from_string(fac_name);
867 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
868 &fac_load_data->checksum);
869 fac->id = ltt_get_uint8(LTT_GET_BO(tf), &fac_load_data->id);
870 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
871 &fac_load_data->pointer_size);
872 fac->size_t_size = ltt_get_uin32(LTT_GET_BO(tf),
873 &fac_load_data->size_t_size);
874 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
875 &fac_load_data->alignment);
876 if(ltt_get_facility_description(fac, tf->trace, tf))
877 goto facility_error;
878
879 fac->trace = tf->trace;
880
881 fac->exists = 1;
882
883 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
884 fac->name);
885 if(fac_ids == NULL) {
886 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
887 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
888 fac->name,
889 fac_ids, ltt_fac_ids_destroy);
890 }
891 g_array_append_val(fac_ids, fac->id);
892
893 break;
894 case LTT_EVENT_HEARTBEAT:
895 break;
896 default:
897 g_warning("Error in processing facility file %s, "
898 "unknown event id %hhu in core facility.",
899 g_quark_to_string(tf->name),
900 tf->event.event_id);
901 err = EPERM;
902 goto event_id_error;
903 }
904 }
963b5f2d 905 }
3aee1200 906 return 0;
963b5f2d 907
3aee1200 908 /* Error handling */
909facility_error:
910event_id_error:
911fac_id_error:
912update_error:
913seek_error:
914 return err;
6cd62ccf 915}
916
963b5f2d 917
3aee1200 918LttTrace *ltt_trace_open(const gchar *pathname)
919{
920 gchar abs_path[PATH_MAX];
921 LttTrace * t;
922 LttTracefile *tf;
923 GArray *group;
924 int i;
925
926 t = g_new(LttTrace, 1);
927 if(!t) goto alloc_error;
928
929 get_absolute_pathname(pathname, abs_path);
930 t->pathname = g_quark_from_string(abs_path);
931
932 /* Open all the tracefiles */
933 g_datalist_init(&t->tracefiles);
934 if(open_tracefiles(t, abs_path, t->tracefiles))
935 goto open_error;
936
937 /* Prepare the facilities containers : array and mapping */
938 /* Array is zeroed : the "exists" field is set to false by default */
939 t->facilities_by_num = g_array_sized_new (FALSE,
940 TRUE, sizeof(LttFacility),
941 NUM_FACILITIES);
942 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
943
944 g_datalist_init(&t->facilities_by_name);
945
946 /* Parse each trace control/facilitiesN files : get runtime fac. info */
947 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
948 if(group == NULL) {
949 g_error("Trace %s has no facility tracefile", abs_path);
950 goto facilities_error;
951 }
952
953 for(i=0; i<group->len; i++) {
954 tf = &g_array_index (group, LttTracefile, i);
955 if(ltt_process_facility_tracefile(tf))
956 goto facilities_error;
957 }
958
959
960
961 return t;
962
963 /* Error handling */
964facilities_error:
965 g_datalist_clear(&t->facilities_by_name);
966 g_array_free(t->facilities_by_num, TRUE);
967open_error:
968 g_datalist_clear(&t->tracefiles);
969 g_free(t);
970alloc_error:
971 return NULL;
972
973}
6cd62ccf 974
3aee1200 975GQuark ltt_trace_name(LttTrace *t)
6cd62ccf 976{
3aee1200 977 return t->pathname;
6cd62ccf 978}
979
6cd62ccf 980
3aee1200 981/******************************************************************************
982 * When we copy a trace, we want all the opening actions to happen again :
983 * the trace will be reopened and totally independant from the original.
984 * That's why we call ltt_trace_open.
985 *****************************************************************************/
986LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 987{
3aee1200 988 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 989}
990
3aee1200 991void ltt_trace_close(LttTrace *t)
6cd62ccf 992{
3aee1200 993 g_datalist_clear(&t->facilities_by_name);
994 g_array_free(t->facilities_by_num, TRUE);
995 g_datalist_clear(&t->tracefiles);
996 g_free(t);
6cd62ccf 997}
998
3aee1200 999
6cd62ccf 1000/*****************************************************************************
3aee1200 1001 *Get the system description of the trace
6cd62ccf 1002 ****************************************************************************/
1003
3aee1200 1004LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
6cd62ccf 1005{
3aee1200 1006 g_assert(id < t->facilities_by_num->len);
1007 return &g_array_index(t->facilities_by_num, LttFacility, id);
1008}
1009
1010/* ltt_trace_facility_get_by_name
1011 *
1012 * Returns the GArray of facility indexes. All the fac_ids that matches the
1013 * requested facility name.
1014 *
1015 * If name is not found, returns NULL.
1016 */
1017GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1018{
1019 return g_datalist_id_get_data(&t->facilities_by_name, name);
6cd62ccf 1020}
1021
1022/*****************************************************************************
963b5f2d 1023 * Functions to discover all the event types in the trace
6cd62ccf 1024 ****************************************************************************/
1025
3aee1200 1026#if 0
963b5f2d 1027unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 1028{
8d1e6362 1029 unsigned int i;
963b5f2d 1030 unsigned count = 0;
dc1cad90 1031 unsigned int num = t->facility_number;
963b5f2d 1032 LttFacility * f;
dc1cad90 1033
1034 for(i=0;i<num;i++){
963b5f2d 1035 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1036 count += f->event_number;
1037 }
1038 return count;
6cd62ccf 1039}
3aee1200 1040#endif //0
6cd62ccf 1041
3aee1200 1042#if 0
1043//use an iteration on all the trace facilities, and inside iteration on all the
1044//event types in each facilities instead.
963b5f2d 1045LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 1046{
6e8c19d4 1047 LttEventType *event_type;
1048
963b5f2d 1049 LttFacility * f;
1050 f = ltt_trace_facility_by_id(t,evId);
6e8c19d4 1051
1052 if(unlikely(!f)) event_type = NULL;
1053 else event_type = f->events[evId - f->base_id];
1054
1055 return event_type;
6cd62ccf 1056}
3aee1200 1057#endif //0
6cd62ccf 1058
3aee1200 1059#if 0
6cd62ccf 1060/*****************************************************************************
3aee1200 1061 * ltt_trace_find_tracefile
1062 *
1063 * Find a tracefile by name and index in the group.
1064 *
1065 * Returns a pointer to the tracefiles, else NULL.
6cd62ccf 1066 ****************************************************************************/
6cd62ccf 1067
3aee1200 1068LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
6cd62ccf 1069{
6cd62ccf 1070}
3aee1200 1071#endif //0
6cd62ccf 1072
1073/*****************************************************************************
3aee1200 1074 * Get the start time and end time of the trace
6cd62ccf 1075 ****************************************************************************/
1076
3aee1200 1077static void ltt_tracefile_time_span_get(LttTracefile *tf,
1078 LttTime *start, LttTime *end)
6cd62ccf 1079{
3aee1200 1080 struct ltt_block_start_header * header;
1081 int err;
6cd62ccf 1082
3aee1200 1083 err = map_block(tf, 0);
1084 if(unlikely(err)) {
1085 g_error("Can not map block");
1086 *start = ltt_time_infinite;
1087 } else
1088 *start = tf->buffer.begin.timestamp;
1089
1090 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1091 if(unlikely(err)) {
1092 g_error("Can not map block");
1093 *end = ltt_time_zero;
1094 } else
1095 *end = tf->buffer.end.timestamp;
6cd62ccf 1096}
1097
3aee1200 1098struct tracefile_time_span_get_args {
1099 LttTrace *t;
1100 LttTime *start;
1101 LttTime *end;
1102};
963b5f2d 1103
3aee1200 1104static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 1105{
3aee1200 1106 struct tracefile_time_span_get_args *args =
1107 (struct tracefile_time_span_get_args*)user_data;
1108
1109 GArray *group = (GArray *)data;
1110 int i;
1111 LttTracefile *tf;
1112 LttTime tmp_start;
1113 LttTime tmp_end;
1114
1115 for(i=0; i<group->len; i++) {
1116 tf = &g_array_index (group, LttTracefile, i);
1117 if(tf->cpu_online) {
1118 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1119 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1120 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1121 }
1122 }
6cd62ccf 1123}
1124
487ad181 1125void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1126{
3aee1200 1127 LttTime min_start = ltt_time_infinite;
1128 LttTime max_end = ltt_time_zero;
1129 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 1130
3aee1200 1131 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1132
1133 if(start != NULL) *start = min_start;
1134 if(end != NULL) *end = max_end;
1135
487ad181 1136}
1137
1138
6cd62ccf 1139/*****************************************************************************
963b5f2d 1140 *Get the name of a tracefile
6cd62ccf 1141 ****************************************************************************/
1142
3aee1200 1143GQuark ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 1144{
963b5f2d 1145 return tf->name;
6cd62ccf 1146}
1147
80da81ad 1148/*****************************************************************************
1149 * Get the number of blocks in the tracefile
1150 ****************************************************************************/
1151
3aee1200 1152guint ltt_tracefile_block_number(LttTracefile *tf)
80da81ad 1153{
3aee1200 1154 return tf->num_blocks;
80da81ad 1155}
1156
caf7a67a 1157
3aee1200 1158/* Seek to the first event in a tracefile that has a time equal or greater than
1159 * the time passed in parameter.
1160 *
1161 * If the time parameter is outside the tracefile time span, seek to the first
1162 * or the last event of the tracefile.
1163 *
1164 * If the time parameter is before the first event, we have to seek specially to
1165 * there.
1166 *
1167 * If the time is after the end of the trace, get the last event.
1168 *
1169 * Do a binary search to find the right block, then a sequential search in the
1170 * block to find the event.
1171 *
1172 * In the special case where the time requested fits inside a block that has no
1173 * event corresponding to the requested time, the first event of the next block
1174 * will be seeked.
1175 *
1176 * IMPORTANT NOTE : // FIXME everywhere...
1177 *
1178 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1179 * you will jump over an event if you do.
1180 *
1181 * Return value : 0 : no error, the tf->event can be used
1182 * otherwise : this is an error.
1183 *
1184 * */
1185
1186int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1187{
1188 int ret = 0;
1189 int err;
1190 unsigned int block_num, high, low;
1191
1192 /* seek at the beginning of trace */
1193 err = map_block(tf, 0); /* First block */
1194 if(unlikely(err)) {
1195 g_error("Can not map block");
1196 goto fail;
caf7a67a 1197 }
1198
3aee1200 1199 /* If the time is lower or equal the beginning of the trace,
1200 * go to the first event. */
1201 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1202 ret = ltt_tracefile_read(tf);
1203 goto found; /* There is either no event in the trace or the event points
1204 to the first event in the trace */
1205 }
caf7a67a 1206
3aee1200 1207 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1208 if(unlikely(err)) {
1209 g_error("Can not map block");
1210 goto fail;
caf7a67a 1211 }
6cd62ccf 1212
3aee1200 1213 /* If the time is after the end of the trace, get the last event. */
1214 if(ltt_time_compare(time, tf->buffer.end.timestamp) >= 0) {
1215 /* While the ltt_tracefile_read doesn't return ERANGE or EPERM,
1216 * continue reading.
1217 */
1218 while(1) {
1219 ret = ltt_tracefile_read(tf);
1220 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1221 else if(ret) goto fail;
1222 }
1223 }
62e55dd6 1224
3aee1200 1225 /* Binary search the block */
1226 high = tf->num_blocks - 1;
1227 low = 0;
1228
1229 while(1) {
1230 block_num = ((high-low) / 2) + low;
1231
1232 err = map_block(tf, block_num);
1233 if(unlikely(err)) {
1234 g_error("Can not map block");
1235 goto fail;
db55eaae 1236 }
3aee1200 1237 if(high == low) {
1238 /* We cannot divide anymore : this is what would happen if the time
1239 * requested was exactly between two consecutive buffers'end and start
1240 * timestamps. This is also what would happend if we didn't deal with out
1241 * of span cases prior in this function. */
1242 /* The event is right in the buffer!
1243 * (or in the next buffer first event) */
1244 while(1) {
1245 ret = ltt_tracefile_read(tf);
1246 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1247 else if(ret) goto fail;
1248
1249 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1250 break;
1251 }
1252
1253 } if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
1254 /* go to lower part */
1255 high = block_num;
1256 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1257 /* go to higher part */
1258 low = block_num;
1259 } else {/* The event is right in the buffer!
1260 (or in the next buffer first event) */
1261 while(1) {
1262 ltt_tracefile_read(tf);
1263 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1264 else if(ret) goto fail;
1265
1266 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1267 break;
6cd62ccf 1268 }
3aee1200 1269 goto found;
6cd62ccf 1270 }
6cd62ccf 1271 }
3aee1200 1272
1273found:
1274 return 0;
1275
1276 /* Error handling */
1277fail:
1278 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1279 g_quark_to_string(tf->name));
1280 return EPERM;
6cd62ccf 1281}
1282
80da81ad 1283
3aee1200 1284int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
80da81ad 1285
3aee1200 1286 int err;
1287
1288 if(ep->tracefile != tf) {
1289 goto fail;
80da81ad 1290 }
1291
3aee1200 1292 err = map_block(tf, ep->block);
1293 if(unlikely(err)) {
1294 g_error("Can not map block");
1295 goto fail;
1296 }
1297
1298 tf->event.offset = ep->offset;
18206708 1299
3aee1200 1300 err = ltt_tracefile_read_update_event(tf);
1301 if(err) goto fail;
1302 err = ltt_tracefile_read_op(tf);
1303 if(err) goto fail;
80da81ad 1304
1305 return;
3aee1200 1306
1307fail:
1308 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1309 g_quark_to_string(tf->name));
1310}
1311
1312/* Calculate the real event time based on the buffer boundaries */
1313LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1314{
1315 LttTime time;
1316
1317 g_assert(tf->trace->has_tsc);
1318
1319 time = ltt_time_from_uint64(
1320 (guint64)tf->buffer.tsc*tf->buffer.nsecs_per_cycle);
1321 time = ltt_time_add(tf->buffer.begin.timestamp, time);
1322
1323 return time;
80da81ad 1324}
1325
6cd62ccf 1326/*****************************************************************************
1327 *Function name
3aee1200 1328 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1329 *Input params
1330 * t : tracefile
1331 *Return value
3aee1200 1332 *
1333 * Returns 0 if an event can be used in tf->event.
1334 * Returns ERANGE on end of trace. The event in tf->event still can be used.
1335 * Returns EPERM on error.
1336 *
1337 * This function does make the tracefile event structure point to the event
1338 * currently pointed to by the tf->event.
1339 *
1340 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1341 * reinitialize it after an error if you want results to be coherent.
1342 * It would be the case if a end of trace last buffer has no event : the end
1343 * of trace wouldn't be returned, but an error.
1344 * We make the assumption there is at least one event per buffer.
6cd62ccf 1345 ****************************************************************************/
1346
3aee1200 1347int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1348{
963b5f2d 1349 int err;
6cd62ccf 1350
3aee1200 1351 err = ltt_tracefile_read_seek(tf);
1352 if(err) return err;
1353 err = ltt_tracefile_read_update_event(tf);
1354 if(err) return err;
1355 err = ltt_tracefile_read_op(tf);
1356 if(err) return err;
1357
1358 return 0;
1359}
1360
1361int ltt_tracefile_read_seek(LttTracefile *tf)
1362{
1363 int err;
1364
1365 /* Get next buffer until we finally have an event, or end of trace */
1366 while(1) {
1367 err = ltt_seek_next_event(tf);
1368 if(unlikely(err == ENOPROTOOPT)) {
1369 return EPERM;
bdc36259 1370 }
bdc36259 1371
3aee1200 1372 /* Are we at the end of the buffer ? */
1373 if(err == ERANGE) {
1374 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1375 return ERANGE;
1376 } else {
1377 /* get next block */
1378 err = map_block(tf, tf->buffer.index + 1);
1379 if(unlikely(err)) {
1380 g_error("Can not map block");
1381 return EPERM;
1382 }
1383 }
1384 } else break; /* We found an event ! */
1385 }
2dee981d 1386
3aee1200 1387 return 0;
1388}
18206708 1389
1390
3aee1200 1391/* do specific operation on events */
1392int ltt_tracefile_read_op(LttTracefile *tf)
1393{
1394 int err;
1395 LttFacility *f;
1396 void * pos;
1397 LttEvent *event;
1398
1399 event = &tf->event;
18206708 1400
3aee1200 1401 /* do event specific operation */
40331ba8 1402
3aee1200 1403 /* do something if its an heartbeat event : increment the heartbeat count */
1404 //if(event->facility_id == LTT_FACILITY_CORE)
1405 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1406 // tf->cur_heart_beat_number++;
1407
1408 return 0;
6cd62ccf 1409}
1410
6cd62ccf 1411
3aee1200 1412/* same as ltt_tracefile_read, but does not seek to the next event nor call
1413 * event specific operation. */
1414int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1415{
3aee1200 1416 int err;
1417 LttFacility *f;
1418 void * pos;
1419 LttEvent *event;
1420
1421 event = &tf->event;
1422 pos = event->offset;
1423
1424 /* Read event header */
1425
1426 //TODO align
1427
1428 if(tf->trace->has_tsc) {
1429 event->time.timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1430 pos);
1431 /* 32 bits -> 64 bits tsc */
1432 /* note : still works for seek and non seek cases. */
1433 if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1434 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1435 + 0x100000000ULL)
1436 | (guint64)event->time.timestamp;
1437 event->tsc = tf->buffer.tsc;
1438 } else {
1439 /* no overflow */
1440 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1441 | (guint64)event->time.timestamp;
1442 event->tsc = tf->buffer.tsc;
1443 }
1444
1445 event->event_time = ltt_interpolate_time(tf, event);
1446
1447 pos += sizeof(guint32);
1448 } else {
1449 event->time.delta.tv_sec = 0;
1450 event->time.delta.tv_nsec = ltt_get_uint32(LTT_GET_BO(tf),
1451 pos) * NSEC_PER_USEC;
1452 tf->buffer.tsc = 0;
1453 event->tsc = tf->buffer.tsc;
1454
1455 event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
1456 event->time.delta);
1457 pos += sizeof(guint32);
1458 }
1459
1460 event->facility_id = ltt_get_uint8(LTT_GET_BO(tf),
1461 pos);
1462 pos += sizeof(guint8);
1463
1464 event->event_id = ltt_get_uint8(LTT_GET_BO(tf),
1465 pos);
1466 pos += sizeof(guint8);
1467
1468 event->data = pos;
1469
77175651 1470 /* get the data size and update the event fields with the current
1471 * information */
1472 event->data_size = ltt_update_event_size(tf);
1473
3aee1200 1474 return 0;
6cd62ccf 1475}
1476
507915ee 1477
6cd62ccf 1478/****************************************************************************
1479 *Function name
3aee1200 1480 * map_block : map a block from the file
6cd62ccf 1481 *Input Params
1482 * lttdes : ltt trace file
1483 * whichBlock : the block which will be read
1484 *return value
1485 * 0 : success
1486 * EINVAL : lseek fail
1487 * EIO : can not read from the file
1488 ****************************************************************************/
1489
3aee1200 1490static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1491{
3aee1200 1492 struct ltt_block_start_header *header;
1493
1494 g_assert(block_num < tf->num_blocks);
6cd62ccf 1495
3aee1200 1496 if(tf->buffer.head != NULL)
1497 munmap(tf->buffer.head, tf->buf_size);
ac849774 1498
3aee1200 1499 /* Multiple of pages aligned head */
1500 tf->buffer.head = mmap(0, tf->block_size, PROT_READ, tf->fd, MAP_PRIVATE,
1501 (off_t)tf->block_size * (off_t)block_num);
1502
1503 if(tf->buffer.head == NULL) {
1504 perror("Error in allocating memory for buffer of tracefile");
1505 goto map_error;
6cd62ccf 1506 }
3aee1200 1507 g_assert(((guint)tf->buffer.head) & (8-1) == 0); // make sure it's aligned.
1508
6cd62ccf 1509
3aee1200 1510 tf->buffer.index = block_num;
1511
1512 header = (struct ltt_block_start_header*)tf->buffer.head;
1513
1514 tf->buffer.begin.timestamp = ltt_get_time(LTT_GET_BO(tf),
1515 &header->begin.timestamp);
1516 tf->buffer.begin.timestamp.tv_nsec *= NSEC_PER_USEC;
1517 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1518 &header->begin.cycle_count);
1519 tf->buffer.end.timestamp = ltt_get_time(LTT_GET_BO(tf),
1520 &header->end.timestamp);
1521 tf->buffer.end.timestamp.tv_nsec *= NSEC_PER_USEC;
1522 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1523 &header->end.cycle_count);
1524 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
1525 &header->lost_size);
6cd62ccf 1526
3aee1200 1527 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1528 tf->event.tsc = tf->buffer.tsc;
1529
1530 /* FIXME
1531 * eventually support variable buffer size : will need a partial pre-read of
1532 * the headers to create an index when we open the trace... eventually. */
1533 g_assert(tf->block_size == ltt_get_uint32(LTT_GET_BO(tf),
1534 &header->buf_size));
507915ee 1535
3aee1200 1536 /* Now that the buffer is mapped, calculate the time interpolation for the
1537 * block. */
1538
1539 tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1540
1541 /* Make the current event point to the beginning of the buffer :
1542 * it means that the event read must get the first event. */
1543 tf->event.tracefile = tf;
1544 tf->event.block = block_num;
1545 tf->event.offset = tf->buffer.head;
1546
1547 return 0;
6cd62ccf 1548
3aee1200 1549map_error:
1550 return -errno;
40331ba8 1551
6cd62ccf 1552}
1553
77175651 1554/* It will update the fields offsets too */
1555void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1556{
3aee1200 1557 ssize_t size = 0;
1558
1559 /* Specific handling of core events : necessary to read the facility control
1560 * tracefile. */
77175651 1561 LttFacility *f = ltt_trace_get_facility_by_num(tf->trace,
1562 tf->event.facility_id);
6cd62ccf 1563
77175651 1564 if(unlikely(f == NULL)) {
1565 if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) {
1566 switch((enum ltt_core_events)tf->event.event_id) {
1567 case LTT_EVENT_FACILITY_LOAD:
1568 size = sizeof(struct LttFacilityLoad);
1569 break;
1570 case LTT_EVENT_FACILITY_UNLOAD:
1571 size = sizeof(struct LttFacilityUnload);
1572 break;
1573 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
1574 size = sizeof(struct LttStateDumpFacilityLoad);
1575 break;
1576 case LTT_EVENT_HEARTBEAT:
1577 size = sizeof(TimeHeartbeat);
1578 break;
1579 default:
1580 g_warning("Error in getting event size : tracefile %s, "
1581 "unknown event id %hhu in core facility.",
1582 g_quark_to_string(tf->name),
1583 tf->event.event_id);
1584 goto event_id_error;
2dee981d 1585
77175651 1586 }
1587 }
3aee1200 1588 } else {
3aee1200 1589 LttEventType *event_type =
1590 ltt_facility_eventtype_get(f, tf->event.event_id);
1591 size = get_fields_offsets(tf, event_type, tf->event.data);
1592 }
3aee1200 1593
77175651 1594 tf->event.data_size = size;
3aee1200 1595event_id_error:
1596 return -1;
6cd62ccf 1597}
1598
6cd62ccf 1599
3aee1200 1600/* Take the tf current event offset and use the event facility id and event id
1601 * to figure out where is the next event offset.
1602 *
1603 * This is an internal function not aiming at being used elsewhere : it will
1604 * not jump over the current block limits. Please consider using
1605 * ltt_tracefile_read to do this.
1606 *
1607 * Returns 0 on success
1608 * ERANGE if we are at the end of the buffer.
1609 * ENOPROTOOPT if an error occured when getting the current event size.
1610 */
1611static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1612{
3aee1200 1613 int ret = 0;
1614 void *pos;
1615 ssize_t event_size;
1616
1617 /* seek over the buffer header if we are at the buffer start */
1618 if(tf->event.offset == tf->buffer.head) {
1619 tf->event.offset += sizeof(struct ltt_block_start_header);
1620 goto found;
1621 }
6cd62ccf 1622
6cd62ccf 1623
3aee1200 1624 if(tf->event.offset == tf->buffer.head + tf->buffer.lost_size) {
1625 ret = ERANGE;
1626 goto found;
6cd62ccf 1627 }
1628
3aee1200 1629 pos = tf->event.data;
1630
77175651 1631 if(tf->event.data_size < 0) goto error;
3aee1200 1632
77175651 1633 pos += (size_t)tf->event.data_size;
3aee1200 1634
1635 tf->event.offset = pos;
1636
1637found:
1638 return ret;
1639
1640error:
1641 g_error("Error in ltt_seek_next_event for tracefile %s",
1642 g_quark_to_string(tf->name));
1643 return ENOPROTOOPT;
6cd62ccf 1644}
1645
18206708 1646
6cd62ccf 1647/*****************************************************************************
1648 *Function name
3aee1200 1649 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
6cd62ccf 1650 *Input Params
1651 * t : tracefile
1652 ****************************************************************************/
1653
3aee1200 1654static double calc_nsecs_per_cycle(LttTracefile * tf)
6cd62ccf 1655{
963b5f2d 1656 LttTime lBufTotalTime; /* Total time for this buffer */
887208b7 1657 double lBufTotalNSec; /* Total time for this buffer in nsecs */
8ee1c3d5 1658 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1659
1660 /* Calculate the total time for this buffer */
01bb5763 1661 lBufTotalTime = ltt_time_sub(
3aee1200 1662 ltt_get_time(LTT_GET_BO(tf), &tf->buffer.end.timestamp),
1663 ltt_get_time(LTT_GET_BO(tf), &tf->buffer.begin.timestamp));
6cd62ccf 1664
1665 /* Calculate the total cycles for this bufffer */
3aee1200 1666 lBufTotalCycle = ltt_get_uint64(LTT_GET_BO(tf), &tf->buffer.end.cycle_count);
1667 lBufTotalCycle -= ltt_get_uint64(LTT_GET_BO(tf),
1668 &tf->buffer.begin.cycle_count);
6cd62ccf 1669
8ee1c3d5 1670 /* Convert the total time to double */
887208b7 1671 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
6cd62ccf 1672
3aee1200 1673 return lBufTotalNSec / (double)lBufTotalCycle;
8ee1c3d5 1674
3aee1200 1675}
1676#if 0
1677void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
1678{
1679 LttField * rootFld = evT->root_field;
1680 // rootFld->base_address = evD;
2dee981d 1681
3aee1200 1682 if(likely(rootFld))
1683 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
1684 evT, 0,0,rootFld, evD);
6cd62ccf 1685}
3aee1200 1686#endif //0
6cd62ccf 1687
3aee1200 1688/*****************************************************************************
6cd62ccf 1689 *Function name
3aee1200 1690 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 1691 *Input params
3aee1200 1692 * tracefile : opened trace file
1693 * event_type : the event type
6cd62ccf 1694 ****************************************************************************/
1695
3aee1200 1696void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 1697{
3aee1200 1698 LttField *field = event_type->root_field;
1699 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
1700
1701 if(likely(field))
1702 preset_field_type_size(tf, event_type, 0, 0,
1703 &fixed_root, &fixed_parent,
1704 field);
1705
1706}
e4eced0f 1707
bbf28e50 1708
3aee1200 1709/*****************************************************************************
1710 *Function name
1711 * preset_field_type_size : set the fixed sizes of the field type
1712 *Input params
1713 * tf : tracefile
1714 * event_type : event type
1715 * offset_root : offset from the root
1716 * offset_parent : offset from the parent
1717 * fixed_root : Do we know a fixed offset to the root ?
1718 * fixed_parent : Do we know a fixed offset to the parent ?
1719 * field : field
1720 ****************************************************************************/
1721void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
1722 off_t offset_root, off_t offset_parent,
1723 enum field_status *fixed_root, enum field_status *fixed_parent,
1724 LttField *field)
1725{
1726 enum field_status local_fixed_root, local_fixed_parent;
1727 guint i;
1728 LttType *type;
dfb73233 1729
3aee1200 1730 g_assert(field->fixed_root == FIELD_UNKNOWN);
1731 g_assert(field->fixed_parent == FIELD_UNKNOWN);
1732 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 1733
3aee1200 1734 type = field->field_type;
1735
1736 field->fixed_root = *fixed_root;
1737 if(field->fixed_root == FIELD_FIXED)
1738 field->offset_root = offset_root;
1739 else
1740 field->offset_root = 0;
1741
1742 field->fixed_parent = *fixed_parent;
1743 if(field->fixed_parent == FIELD_FIXED)
1744 field->offset_parent = offset_parent;
1745 else
1746 field->offset_parent = 0;
1747
1748 size_t current_root_offset;
1749 size_t current_offset;
1750 enum field_status current_child_status, final_child_status;
1751 size_t max_size;
1752
1753 switch(type->type_class) {
1754 case LTT_INT:
1755 case LTT_UINT:
1756 case LTT_FLOAT:
1757 case LTT_ENUM:
1758 field->field_size = ltt_type_size(tf->trace, type);
1759 field->fixed_size = FIELD_FIXED;
1760 break;
1761 case LTT_POINTER:
1762 field->field_size = (off_t)event_type->facility->pointer_size;
1763 field->fixed_size = FIELD_FIXED;
1764 break;
1765 case LTT_LONG:
1766 case LTT_ULONG:
1767 field->field_size = (off_t)event_type->facility->pointer_size;
1768 field->fixed_size = FIELD_FIXED;
1769 break;
1770 case LTT_SIZE_T:
1771 case LTT_SSIZE_T:
1772 case LTT_OFF_T:
1773 field->field_size = (off_t)event_type->facility->size_t_size;
1774 field->fixed_size = FIELD_FIXED;
1775 break;
1776 case LTT_SEQUENCE:
1777 local_fixed_root = FIELD_VARIABLE;
1778 local_fixed_parent = FIELD_VARIABLE;
1779 preset_field_type_size(tf, event_type,
1780 0, 0,
1781 &local_fixed_root, &local_fixed_parent,
1782 field->child[0]);
1783 field->fixed_size = FIELD_VARIABLE;
1784 field->field_size = 0;
1785 break;
1786 case LTT_STRING:
1787 field->fixed_size = FIELD_VARIABLE;
1788 field->field_size = 0;
1789 break;
1790 case LTT_ARRAY:
1791 local_fixed_root = FIELD_VARIABLE;
1792 local_fixed_parent = FIELD_VARIABLE;
1793 preset_field_type_size(tf, event_type,
1794 0, 0,
1795 &local_fixed_root, &local_fixed_parent,
1796 field->child[0]);
1797 field->fixed_size = field->child[0]->fixed_size;
1798 if(field->fixed_size == FIELD_FIXED)
1799 field->field_size = type->element_number * field->child[0]->field_size;
1800 else
1801 field->field_size = 0;
1802 break;
1803 case LTT_STRUCT:
1804 current_root_offset = field->offset_root;
1805 current_offset = 0;
1806 current_child_status = FIELD_FIXED;
1807 for(i=0;i<type->element_number;i++) {
1808 preset_field_type_size(tf, event_type,
1809 current_root_offset, current_offset,
1810 fixed_root, &current_child_status,
1811 field->child[i]);
1812 if(current_child_status == FIELD_FIXED) {
1813 current_root_offset += field->child[i]->field_size;
1814 current_offset += field->child[i]->field_size;
1815 } else {
1816 current_root_offset = 0;
1817 current_offset = 0;
1818 }
1819 }
1820 if(current_child_status != FIELD_FIXED) {
1821 *fixed_parent = current_child_status;
1822 field->field_size = 0;
1823 field->fixed_size = current_child_status;
1824 } else {
1825 field->field_size = current_offset;
1826 field->fixed_size = FIELD_FIXED;
1827 }
1828 break;
1829 case LTT_UNION:
1830 current_root_offset = field->offset_root;
1831 current_offset = 0;
1832 max_size = 0;
1833 final_child_status = FIELD_FIXED;
1834 for(i=0;i<type->element_number;i++) {
1835 enum field_status current_root_child_status = FIELD_FIXED;
1836 enum field_status current_child_status = FIELD_FIXED;
1837 preset_field_type_size(tf, event_type,
1838 current_root_offset, current_offset,
1839 &current_root_child_status, &current_child_status,
1840 field->child[i]);
1841 if(current_child_status != FIELD_FIXED)
1842 final_child_status = current_child_status;
1843 else
1844 max_size = max(max_size, field->child[i]->field_size);
1845 }
1846 if(final_child_status != FIELD_FIXED) {
1847 *fixed_root = final_child_status;
1848 *fixed_parent = final_child_status;
1849 field->field_size = 0;
1850 field->fixed_size = current_child_status;
1851 } else {
1852 field->field_size = max_size;
1853 field->fixed_size = FIELD_FIXED;
1854 }
1855 break;
dfb73233 1856 }
1857
6cd62ccf 1858}
1859
3aee1200 1860
6cd62ccf 1861/*****************************************************************************
1862 *Function name
3aee1200 1863 * get_field_type_size : set the fixed and dynamic sizes of the field type
1864 * from the data read.
6cd62ccf 1865 *Input params
3aee1200 1866 * tf : tracefile
1867 * event_type : event type
1868 * offset_root : offset from the root
1869 * offset_parent : offset from the parent
1870 * field : field
1871 * data : a pointer to the event data.
1872 *Returns the field type size.
6cd62ccf 1873 ****************************************************************************/
3aee1200 1874size_t get_field_type_size(LttTracefile *tf, LttEventType *event_type,
1875 off_t offset_root, off_t offset_parent,
1876 LttField *field, void *data)
6cd62ccf 1877{
3aee1200 1878 size_t size = 0;
1879 guint i;
1880 LttType *type;
1881
1882 g_assert(field->fixed_root != FIELD_UNKNOWN);
1883 g_assert(field->fixed_parent != FIELD_UNKNOWN);
1884 g_assert(field->fixed_size != FIELD_UNKNOWN);
6cd62ccf 1885
3aee1200 1886 field->offset_root = offset_root;
1887 field->offset_parent = offset_parent;
1888
1889 type = field->field_type;
1890
1891 switch(type->type_class) {
1892 case LTT_INT:
1893 case LTT_UINT:
1894 case LTT_FLOAT:
1895 case LTT_ENUM:
1896 case LTT_POINTER:
1897 case LTT_LONG:
1898 case LTT_ULONG:
1899 case LTT_SIZE_T:
1900 case LTT_SSIZE_T:
1901 case LTT_OFF_T:
1902 g_assert(field->fixed_size == FIELD_FIXED);
1903 size = field->field_size;
1904 break;
1905 case LTT_SEQUENCE:
1906 {
1907 gint seqnum = ltt_get_uint(LTT_GET_BO(tf),
1908 field->sequ_number_size,
1909 data + offset_root);
1910
1911 if(field->child[0]->fixed_size == FIELD_FIXED) {
1912 size = field->sequ_number_size +
1913 (seqnum * get_field_type_size(tf, event_type,
1914 offset_root, offset_parent,
1915 field->child[0], data));
1916 } else {
1917 size += field->sequ_number_size;
1918 for(i=0;i<seqnum;i++) {
1919 size_t child_size;
1920 child_size = get_field_type_size(tf, event_type,
1921 offset_root, offset_parent,
1922 field->child[0], data);
1923 offset_root += child_size;
1924 offset_parent += child_size;
1925 size += child_size;
1926 }
1927 }
1928 field->field_size = size;
1929 }
1930 break;
1931 case LTT_STRING:
1932 size = strlen((char*)(data+offset_root)) + 1;// length + \0
1933 field->field_size = size;
1934 break;
1935 case LTT_ARRAY:
1936 if(field->fixed_size == FIELD_FIXED)
1937 size = field->field_size;
1938 else {
1939 for(i=0;i<field->field_type->element_number;i++) {
1940 size_t child_size;
1941 child_size = get_field_type_size(tf, event_type,
1942 offset_root, offset_parent,
1943 field->child[0], data);
1944 offset_root += child_size;
1945 offset_parent += child_size;
1946 size += child_size;
1947 }
1948 field->field_size = size;
1949 }
1950 break;
1951 case LTT_STRUCT:
1952 if(field->fixed_size == FIELD_FIXED)
1953 size = field->field_size;
1954 else {
1955 size_t current_root_offset = offset_root;
1956 size_t current_offset = 0;
1957 size_t child_size = 0;
1958 for(i=0;i<type->element_number;i++) {
1959 child_size = get_field_type_size(tf,
1960 event_type, current_root_offset, current_offset,
1961 field->child[i], data);
1962 current_offset += child_size;
1963 current_root_offset += child_size;
1964
1965 }
1966 size = current_offset;
1967 field->field_size = size;
1968 }
1969 break;
1970 case LTT_UNION:
1971 if(field->fixed_size == FIELD_FIXED)
1972 size = field->field_size;
1973 else {
1974 size_t current_root_offset = field->offset_root;
1975 size_t current_offset = 0;
1976 for(i=0;i<type->element_number;i++) {
1977 size = get_field_type_size(tf, event_type,
1978 current_root_offset, current_offset,
1979 field->child[i], data);
1980 size = max(size, field->child[i]->field_size);
1981 }
1982 field->field_size = size;
1983 }
1984 break;
1985 }
1986
1987 return size;
6cd62ccf 1988}
1989
3aee1200 1990
77175651 1991/*****************************************************************************
1992 *Function name
1993 * check_fields_compatibility : Check for compatibility between two fields :
1994 * do they use the same inner structure ?
1995 *Input params
1996 * event_type1 : event type
1997 * event_type2 : event type
1998 * field1 : field
1999 * field2 : field
2000 *Returns : 0 if identical
2001 * 1 if not.
2002 ****************************************************************************/
2003gint check_fields_compatibility(LttEventType *event_type1,
2004 LttEventType *event_type2,
2005 LttField *field1, LttField *field2)
2006{
2007 guint different = 0;
2008 enum field_status local_fixed_root, local_fixed_parent;
2009 guint i;
2010 LttType *type1;
2011 LttType *type2;
2012
2013 if(field1 == NULL) {
2014 if(field2 == NULL) goto end;
2015 else {
2016 different = 1;
2017 goto end;
2018 }
2019 } else if(field2 == NULL) {
2020 different = 1;
2021 goto end;
2022 }
2023
2024 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2025 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2026 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2027 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2028 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2029 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2030
2031 type1 = field1->field_type;
2032 type2 = field2->field_type;
2033
2034 size_t current_root_offset;
2035 size_t current_offset;
2036 enum field_status current_child_status, final_child_status;
2037 size_t max_size;
2038
2039 if(type1->type_class != type2->type_class) {
2040 different = 1;
2041 goto end;
2042 }
2043 if(type1->element_name != type2->element_name) {
2044 different = 1;
2045 goto end;
2046 }
2047
2048 switch(type1->type_class) {
2049 case LTT_INT:
2050 case LTT_UINT:
2051 case LTT_FLOAT:
2052 case LTT_POINTER:
2053 case LTT_LONG:
2054 case LTT_ULONG:
2055 case LTT_SIZE_T:
2056 case LTT_SSIZE_T:
2057 case LTT_OFF_T:
2058 if(field1->field_size != field2->field_size) {
2059 different = 1;
2060 goto end;
2061 }
2062 break;
2063 case LTT_ENUM:
2064 if(type1->element_number != type2->element_number) {
2065 different = 1;
2066 goto end;
2067 }
2068 for(i=0;i<type1->element_number;i++) {
2069 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2070 different = 1;
2071 goto end;
2072 }
2073 }
2074 break;
2075 case LTT_SEQUENCE:
2076 /* Two elements : size and child */
2077 g_assert(type1->element_number != type2->element_number);
2078 for(i=0;i<type1->element_number;i++) {
2079 if(check_fields_compatibility(event_type1, event_type2,
2080 field1->child[0], field2->child[0])) {
2081 different = 1;
2082 goto end;
2083 }
2084 }
2085 break;
2086 case LTT_STRING:
2087 break;
2088 case LTT_ARRAY:
2089 if(field1->field_size != field2->field_size) {
2090 different = 1;
2091 goto end;
2092 }
2093 /* Two elements : size and child */
2094 g_assert(type1->element_number != type2->element_number);
2095 for(i=0;i<type1->element_number;i++) {
2096 if(check_fields_compatibility(event_type1, event_type2,
2097 field1->child[0], field2->child[0])) {
2098 different = 1;
2099 goto end;
2100 }
2101 }
2102 break;
2103 case LTT_STRUCT:
2104 case LTT_UNION:
2105 if(type1->element_number != type2->element_number) {
2106 different = 1;
2107 break;
2108 }
2109 for(i=0;i<type1->element_number;i++) {
2110 if(check_fields_compatibility(event_type1, event_type2,
2111 field1->child[0], field2->child[0])) {
2112 different = 1;
2113 goto end;
2114 }
2115 }
2116 break;
2117 }
2118end:
2119 return different;
2120}
2121
2122
2123
3aee1200 2124
2125#if 0
6cd62ccf 2126/*****************************************************************************
2127 *Function name
2128 * getFieldtypeSize: get the size of the field type (primitive type)
2129 *Input params
6cd62ccf 2130 * evT : event type
2131 * offsetRoot : offset from the root
2132 * offsetParent : offset from the parrent
2133 * fld : field
2134 * evD : event data, it may be NULL
2135 *Return value
2136 * int : size of the field
2137 ****************************************************************************/
2138
3aee1200 2139static inline gint getFieldtypeSize(LttTracefile *tf,
21182d4a 2140 LttEventType * evT, gint offsetRoot,
3aee1200 2141 gint offsetParent, LttField * fld, void *evD)
6cd62ccf 2142{
21182d4a 2143 gint size, size1, element_number, i, offset1, offset2;
963b5f2d 2144 LttType * type = fld->field_type;
6cd62ccf 2145
3aee1200 2146 /* This likely has been tested with gcov : half of them.. */
2147 if(unlikely(fld->field_fixed == 1)){
2148 /* tested : none */
2149 if(unlikely(fld == evT->root_field)) {
2150 size = fld->field_size;
2151 goto end_getFieldtypeSize;
21182d4a 2152 }
3aee1200 2153 }
6cd62ccf 2154
3aee1200 2155 /* From gcov profiling : half string, half struct, can we gain something
2156 * from that ? (Mathieu) */
2157 switch(type->type_class) {
2158 case LTT_ARRAY:
2159 element_number = (int) type->element_number;
2160 if(fld->field_fixed == -1){
2161 size = getFieldtypeSize(tf, evT, offsetRoot,
2162 0,fld->child[0], NULL);
2163 if(size == 0){ //has string or sequence
cf74a6f1 2164 fld->field_fixed = 0;
3aee1200 2165 }else{
2166 fld->field_fixed = 1;
2167 size *= element_number;
a0d63196 2168 }
3aee1200 2169 }else if(fld->field_fixed == 0){// has string or sequence
cf74a6f1 2170 size = 0;
3aee1200 2171 for(i=0;i<element_number;i++){
2172 size += getFieldtypeSize(tf, evT, offsetRoot+size,size,
2173 fld->child[0], evD+size);
cf74a6f1 2174 }
3aee1200 2175 }else size = fld->field_size;
2176 if(unlikely(!evD)){
a0d63196 2177 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2178 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
3aee1200 2179 }
cf74a6f1 2180
3aee1200 2181 break;
2182
2183 case LTT_SEQUENCE:
2184 size1 = (int) ltt_type_size(fac, type);
2185 if(fld->field_fixed == -1){
2186 fld->sequ_number_size = size1;
2187 fld->field_fixed = 0;
2188 size = getFieldtypeSize(evT, offsetRoot,
2189 0,fld->child[0], NULL);
2190 fld->element_size = size;
2191 }else{//0: sequence
2192 element_number = getIntNumber(tf,size1,evD);
2193 type->element_number = element_number;
2194 if(fld->element_size > 0){
2195 size = element_number * fld->element_size;
2196 }else{//sequence has string or sequence
2197 size = 0;
cf74a6f1 2198 for(i=0;i<element_number;i++){
3aee1200 2199 size += getFieldtypeSize(tf, evT,
2200 offsetRoot+size+size1,size+size1,
2201 fld->child[0], evD+size+size1);
cf74a6f1 2202 }
3aee1200 2203 }
2204 size += size1;
2205 }
2206 if(unlikely(!evD)){
d37b2aaa 2207 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2208 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
3aee1200 2209 }
cf74a6f1 2210
3aee1200 2211 break;
2212
2213 case LTT_STRING:
2214 size = 0;
2215 if(fld->field_fixed == -1){
2216 fld->field_fixed = 0;
2217 }else{//0: string
2218 /* Hope my implementation is faster than strlen (Mathieu) */
2219 char *ptr=(char*)evD;
2220 size = 1;
2221 /* from gcov : many many strings are empty, make it the common case.*/
2222 while(unlikely(*ptr != '\0')) { size++; ptr++; }
2223 //size = ptr - (char*)evD + 1; //include end : '\0'
2224 }
2225 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2226 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2227
2228 break;
2229
2230 case LTT_STRUCT:
2231 element_number = (int) type->element_number;
2232 size = 0;
2233 /* tested with gcov */
2234 if(unlikely(fld->field_fixed == -1)){
2235 offset1 = offsetRoot;
2236 offset2 = 0;
2237 for(i=0;i<element_number;i++){
2238 size1=getFieldtypeSize(tf, evT,offset1,offset2,
2239 fld->child[i], NULL);
2240 if(likely(size1 > 0 && size >= 0)){
2241 size += size1;
2242 if(likely(offset1 >= 0)) offset1 += size1;
2243 offset2 += size1;
2244 }else{
2245 size = -1;
2246 offset1 = -1;
2247 offset2 = -1;
2248 }
a0d63196 2249 }
3aee1200 2250 if(unlikely(size == -1)){
2251 fld->field_fixed = 0;
2252 size = 0;
2253 }else fld->field_fixed = 1;
2254 }else if(likely(fld->field_fixed == 0)){
2255 offset1 = offsetRoot;
2256 offset2 = 0;
2257 for(i=0;unlikely(i<element_number);i++){
2258 size=getFieldtypeSize(tf, evT, offset1, offset2,
2259 fld->child[i], evD+offset2);
2260 offset1 += size;
2261 offset2 += size;
2262 }
2263 size = offset2;
2264 }else size = fld->field_size;
2265 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2266 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2267 break;
2268
2269 default:
2270 if(unlikely(fld->field_fixed == -1)){
2271 size = (int) ltt_type_size(LTT_GET_BO(tf), type);
2272 fld->field_fixed = 1;
2273 }else size = fld->field_size;
2274 if(unlikely(!evD)){
2275 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2276 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2277 }
2278 break;
cf74a6f1 2279 }
2280
6cd62ccf 2281 fld->offset_root = offsetRoot;
2282 fld->offset_parent = offsetParent;
6cd62ccf 2283 fld->field_size = size;
2284
21182d4a 2285end_getFieldtypeSize:
2286
6cd62ccf 2287 return size;
2288}
3aee1200 2289#endif //0
6cd62ccf 2290
2291/*****************************************************************************
2292 *Function name
3aee1200 2293 * get_int : get an integer number
6cd62ccf 2294 *Input params
3aee1200 2295 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2296 * size : the size of the integer
3aee1200 2297 * ptr : the data pointer
6cd62ccf 2298 *Return value
cf74a6f1 2299 * gint64 : a 64 bits integer
6cd62ccf 2300 ****************************************************************************/
2301
3aee1200 2302gint64 get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2303{
3aee1200 2304 gint64 val;
cf74a6f1 2305
2306 switch(size) {
3aee1200 2307 case 1: val = *((gint8*)data); break;
2308 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2309 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2310 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2311 default: val = ltt_get_int64(reverse_byte_order, data);
2312 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2313 break;
2314 }
2315
3aee1200 2316 return val;
6cd62ccf 2317}
3aee1200 2318
6cd62ccf 2319/*****************************************************************************
2320 *Function name
3aee1200 2321 * get_uint : get an unsigned integer number
6cd62ccf 2322 *Input params
3aee1200 2323 * reverse_byte_order: must we reverse the byte order ?
2324 * size : the size of the integer
2325 * ptr : the data pointer
2326 *Return value
2327 * guint64 : a 64 bits unsigned integer
6cd62ccf 2328 ****************************************************************************/
2329
3aee1200 2330guint64 get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2331{
3aee1200 2332 guint64 val;
2333
2334 switch(size) {
2335 case 1: val = *((gint8*)data); break;
2336 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2337 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2338 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2339 default: val = ltt_get_uint64(reverse_byte_order, data);
2340 g_critical("get_uint : unsigned integer size %d unknown",
2341 size);
2342 break;
2343 }
2344
2345 return val;
6cd62ccf 2346}
3aee1200 2347
2348
a5dcde2f 2349/* get the node name of the system */
2350
2351char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2352{
2353 return s->node_name;
2354}
2355
2356
2357/* get the domain name of the system */
2358
2359char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2360{
2361 return s->domain_name;
2362}
2363
2364
2365/* get the description of the system */
2366
2367char * ltt_trace_system_description_description (LttSystemDescription * s)
2368{
2369 return s->description;
2370}
2371
2372
2373/* get the start time of the trace */
2374
2375LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
2376{
2377 return s->trace_start;
2378}
2379
18206708 2380
2381LttTracefile *ltt_tracefile_new()
2382{
2383 return g_new(LttTracefile, 1);
2384}
2385
2386void ltt_tracefile_destroy(LttTracefile *tf)
2387{
2388 g_free(tf);
2389}
2390
2391void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
2392{
2393 *dest = *src;
2394}
2395
3aee1200 2396/* Before library loading... */
2397
2398static void __attribute__((constructor)) init(void)
2399{
2400 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2401 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2402
2403 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("control/facilities");
2404}
2405
This page took 0.148066 seconds and 4 git commands to generate.