ltt newlib commit it, it compiles, not tested
[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
184static inline LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
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
545void ltt_tracefile_group_destroy(gpointer data)
546{
547 GArray *group = (GArray *)data;
548 int i;
549 LttTracefile *tf;
550
551 for(i=0; i<group->len; i++) {
552 tf = &g_array_index (group, LttTracefile, i);
553 if(tf->cpu_online)
554 ltt_tracefile_close(tf);
555 }
556 g_array_free(group, TRUE);
6cd62ccf 557}
558
3aee1200 559gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 560{
3aee1200 561 GArray *group = (GArray *)data;
562 int i;
563 LttTracefile *tf;
564
565 for(i=0; i<group->len; i++) {
566 tf = &g_array_index (group, LttTracefile, i);
567 if(tf->cpu_online) return 1;
568 }
569 return 0;
49bf71b5 570}
571
572
3aee1200 573/* Open each tracefile under a specific directory. Put them in a
574 * GData : permits to access them using their tracefile group pathname.
575 * i.e. access control/modules tracefile group by index :
576 * "control/module".
577 *
578 * A tracefile group is simply an array where all the per cpu tracefiles sits.
579 */
580
581static int open_tracefiles(LttTrace *trace, char *root_path, GData *tracefiles)
f7afe191 582{
3aee1200 583 DIR *dir = opendir(root_path);
584 struct dirent *entry;
585 struct stat stat_buf;
586 int ret;
587 char path[PATH_MAX];
588 int path_len;
589 char *path_ptr;
590
591 if(dir == NULL) {
592 perror(root_path);
593 return ENOENT;
594 }
595
596 strncpy(path, root_path, PATH_MAX-1);
597 path_len = strlen(path);
598 path[path_len] = '/';
599 path_len++;
600 path_ptr = path + path_len;
601
602 while((entry = readdir(dir)) != NULL) {
603
604 if(entry->d_name[0] == '.') continue;
605
606 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
607
608 ret = stat(path, &stat_buf);
609 if(ret == -1) {
610 perror(path);
611 continue;
612 }
613
614 g_debug("Tracefile file or directory : %s\n", path);
615
616 if(S_ISDIR(stat_buf.st_mode)) {
617
618 g_debug("Entering subdirectory...\n");
619 ret = open_tracefiles(trace, path, tracefiles);
620 if(ret < 0) continue;
621 } else if(S_ISREG(stat_buf.st_mode)) {
622 g_debug("Opening file.\n");
623
624 GQuark name;
625 guint num;
626 GArray *group;
627 LttTracefile *tf;
628 guint len;
629
630 if(get_tracefile_name_number(path, &name, &num))
631 continue; /* invalid name */
632
633 group = g_datalist_id_get_data(&tracefiles, name);
634 if(group == NULL) {
635 /* Elements are automatically cleared when the array is allocated.
636 * It makes the cpu_online variable set to 0 : cpu offline, by default.
637 */
638 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
639 g_datalist_id_set_data_full(&tracefiles, name,
640 group, ltt_tracefile_group_destroy);
641 }
642 /* Add the per cpu tracefile to the named group */
643 unsigned int old_len = group->len;
644 if(num+1 > old_len)
645 group = g_array_set_size(group, num+1);
646 tf = &g_array_index (group, LttTracefile, num);
647
648 if(ltt_tracefile_open(trace, path, tf)) {
649 g_info("Error opening tracefile %s", path);
650 g_array_set_size(group, old_len);
651
652 if(!ltt_tracefile_group_has_cpu_online(group))
653 g_datalist_id_remove_data(&tracefiles, name);
654
655 continue; /* error opening the tracefile : bad magic number ? */
656 }
657 tf->cpu_online = 1;
658 tf->cpu_num = num;
659 }
660 }
661
662 closedir(dir);
663
664 return 0;
f7afe191 665}
666
3aee1200 667/* ltt_get_facility_description
668 *
669 * Opens the trace corresponding to the requested facility (identified by fac_id
670 * and checksum).
671 *
672 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
673 *
674 * Returns 0 on success, or 1 on failure.
675 */
676
677static int ltt_get_facility_description(LttFacility *f,
678 LttTrace *t,
679 LttTracefile *fac_tf)
6cd62ccf 680{
3aee1200 681 char desc_file_name[PATH_MAX];
682 const gchar *text;
683 guint textlen;
684 gint err;
685 int i, j;
686 LttEventType *et;
687
688 text = g_quark_to_string(t->pathname);
689 textlen = strlen(text);
690
691 if(textlen >= PATH_MAX) goto name_error;
692 strcpy(desc_file_name, text);
693
694 text = "/eventdefs/";
695 textlen+=strlen(text);
696 if(textlen >= PATH_MAX) goto name_error;
697 strcat(desc_file_name, text);
698
699 text = g_quark_to_string(f->name);
700 textlen+=strlen(text);
701 if(textlen >= PATH_MAX) goto name_error;
702 strcat(desc_file_name, text);
703
704 text = "_";
705 textlen+=strlen(text);
706 if(textlen >= PATH_MAX) goto name_error;
707 strcat(desc_file_name, text);
963b5f2d 708
3aee1200 709 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
710 "%u", f->checksum);
711 if(err) goto name_error;
712
713 textlen=strlen(desc_file_name);
714
715 text = ".xml";
716 textlen+=strlen(text);
717 if(textlen >= PATH_MAX) goto name_error;
718 strcat(desc_file_name, text);
963b5f2d 719
3aee1200 720 err = ltt_facility_open(f, t, desc_file_name);
721 if(err) goto facility_error;
722
723 for(i=0;i<t->facilities_by_num->len;i++){
724 f = &g_array_index(t->facilities_by_num, LttFacility, i);
725 if(f->exists) {
726 for(j=0; j<f->events->len; j++){
727 et = &g_array_index(f->events, LttEventType, j);
728 set_fields_offsets(fac_tf, et);
729 }
730 }
963b5f2d 731 }
963b5f2d 732
3aee1200 733
734 return 0;
735
736facility_error:
737name_error:
738 return 1;
739}
740
741static void ltt_fac_ids_destroy(gpointer data)
742{
743 GArray *fac_ids = (GArray *)data;
744 int i;
745 LttFacility *fac;
746
747 for(i=0; i<fac_ids->len; i++) {
748 fac = &g_array_index (fac_ids, LttFacility, i);
749 ltt_facility_close(fac);
963b5f2d 750 }
963b5f2d 751
3aee1200 752 g_array_free(fac_ids, TRUE);
753}
754
755
756/* Presumes the tracefile is already seeked at the beginning. It makes sense,
757 * because it must be done just after the opening */
758static int ltt_process_facility_tracefile(LttTracefile *tf)
759{
760 int err;
761 LttFacility *fac;
762 GArray *fac_ids;
763
764 while(1) {
765 err = ltt_tracefile_read_seek(tf);
766 if(err == EPERM) goto seek_error;
767 else if(err == ERANGE) break; /* End of tracefile */
768
769 err = ltt_tracefile_read_update_event(tf);
770 if(err) goto update_error;
771
772 /* We are on a facility load/or facility unload/ or heartbeat event */
773 /* The rules are :
774 * * facility 0 is hardcoded : this is the core facility. It will be shown
775 * in the facility array though, and is shown as "loaded builtin" in the
776 * trace.
777 * It contains event :
778 * 0 : facility load
779 * 1 : facility unload
780 * 2 : state dump facility load
781 * Facility 1 : (heartbeat)
782 * 0 : heartbeat
783 */
784 if(tf->event.facility_id > 1) { /* Should only contain core and heartbeat
785 facilities */
786 g_warning("Error in processing facility file %s, "
787 "should not contain facility id %u.", g_quark_to_string(tf->name),
788 tf->event.facility_id);
789 err = EPERM;
790 goto fac_id_error;
791 } else if(tf->event.facility_id == LTT_FACILITY_CORE) {
792
793 struct LttFacilityLoad *fac_load_data;
794 char *fac_name;
795
796 // FIXME align
797 switch((enum ltt_core_events)tf->event.event_id) {
798 case LTT_EVENT_FACILITY_LOAD:
799 fac_load_data =
800 (struct LttFacilityLoad *)tf->event.data;
801 fac_name =
802 (char*)(tf->event.data + sizeof(struct LttFacilityLoad));
803 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
804 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
805 g_assert(fac->exists == 0);
806 fac->name = g_quark_from_string(fac_name);
807 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
808 &fac_load_data->checksum);
809 fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
810 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
811 &fac_load_data->pointer_size);
812 fac->size_t_size = ltt_get_uin32(LTT_GET_BO(tf),
813 &fac_load_data->size_t_size);
814 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
815 &fac_load_data->alignment);
816
817 if(ltt_get_facility_description(fac, tf->trace, tf))
818 goto facility_error;
819
820 fac->trace = tf->trace;
821 fac->exists = 1;
822
823 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name, fac->name);
824 if(fac_ids == NULL) {
825 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
826 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
827 fac->name,
828 fac_ids, ltt_fac_ids_destroy);
829 }
830 g_array_append_val(fac_ids, fac->id);
831
832 break;
833 case LTT_EVENT_FACILITY_UNLOAD:
834 /* We don't care about unload : facilities ID are valid for the whole
835 * trace. They simply won't be used after the unload. */
836 break;
837 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
838 fac_load_data =
839 (struct LttFacilityLoad *)tf->event.data;
840 fac_name =
841 (char*)(tf->event.data + sizeof(struct LttFacilityLoad));
842 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
843 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
844 g_assert(fac->exists == 0);
845 fac->name = g_quark_from_string(fac_name);
846 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
847 &fac_load_data->checksum);
848 fac->id = ltt_get_uint8(LTT_GET_BO(tf), &fac_load_data->id);
849 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
850 &fac_load_data->pointer_size);
851 fac->size_t_size = ltt_get_uin32(LTT_GET_BO(tf),
852 &fac_load_data->size_t_size);
853 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
854 &fac_load_data->alignment);
855 if(ltt_get_facility_description(fac, tf->trace, tf))
856 goto facility_error;
857
858 fac->trace = tf->trace;
859
860 fac->exists = 1;
861
862 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
863 fac->name);
864 if(fac_ids == NULL) {
865 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
866 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
867 fac->name,
868 fac_ids, ltt_fac_ids_destroy);
869 }
870 g_array_append_val(fac_ids, fac->id);
871
872 break;
873 case LTT_EVENT_HEARTBEAT:
874 break;
875 default:
876 g_warning("Error in processing facility file %s, "
877 "unknown event id %hhu in core facility.",
878 g_quark_to_string(tf->name),
879 tf->event.event_id);
880 err = EPERM;
881 goto event_id_error;
882 }
883 }
963b5f2d 884 }
3aee1200 885 return 0;
963b5f2d 886
3aee1200 887 /* Error handling */
888facility_error:
889event_id_error:
890fac_id_error:
891update_error:
892seek_error:
893 return err;
6cd62ccf 894}
895
963b5f2d 896
3aee1200 897LttTrace *ltt_trace_open(const gchar *pathname)
898{
899 gchar abs_path[PATH_MAX];
900 LttTrace * t;
901 LttTracefile *tf;
902 GArray *group;
903 int i;
904
905 t = g_new(LttTrace, 1);
906 if(!t) goto alloc_error;
907
908 get_absolute_pathname(pathname, abs_path);
909 t->pathname = g_quark_from_string(abs_path);
910
911 /* Open all the tracefiles */
912 g_datalist_init(&t->tracefiles);
913 if(open_tracefiles(t, abs_path, t->tracefiles))
914 goto open_error;
915
916 /* Prepare the facilities containers : array and mapping */
917 /* Array is zeroed : the "exists" field is set to false by default */
918 t->facilities_by_num = g_array_sized_new (FALSE,
919 TRUE, sizeof(LttFacility),
920 NUM_FACILITIES);
921 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
922
923 g_datalist_init(&t->facilities_by_name);
924
925 /* Parse each trace control/facilitiesN files : get runtime fac. info */
926 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
927 if(group == NULL) {
928 g_error("Trace %s has no facility tracefile", abs_path);
929 goto facilities_error;
930 }
931
932 for(i=0; i<group->len; i++) {
933 tf = &g_array_index (group, LttTracefile, i);
934 if(ltt_process_facility_tracefile(tf))
935 goto facilities_error;
936 }
937
938
939
940 return t;
941
942 /* Error handling */
943facilities_error:
944 g_datalist_clear(&t->facilities_by_name);
945 g_array_free(t->facilities_by_num, TRUE);
946open_error:
947 g_datalist_clear(&t->tracefiles);
948 g_free(t);
949alloc_error:
950 return NULL;
951
952}
6cd62ccf 953
3aee1200 954GQuark ltt_trace_name(LttTrace *t)
6cd62ccf 955{
3aee1200 956 return t->pathname;
6cd62ccf 957}
958
6cd62ccf 959
3aee1200 960/******************************************************************************
961 * When we copy a trace, we want all the opening actions to happen again :
962 * the trace will be reopened and totally independant from the original.
963 * That's why we call ltt_trace_open.
964 *****************************************************************************/
965LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 966{
3aee1200 967 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 968}
969
3aee1200 970void ltt_trace_close(LttTrace *t)
6cd62ccf 971{
3aee1200 972 g_datalist_clear(&t->facilities_by_name);
973 g_array_free(t->facilities_by_num, TRUE);
974 g_datalist_clear(&t->tracefiles);
975 g_free(t);
6cd62ccf 976}
977
3aee1200 978
6cd62ccf 979/*****************************************************************************
3aee1200 980 *Get the system description of the trace
6cd62ccf 981 ****************************************************************************/
982
3aee1200 983LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
6cd62ccf 984{
3aee1200 985 g_assert(id < t->facilities_by_num->len);
986 return &g_array_index(t->facilities_by_num, LttFacility, id);
987}
988
989/* ltt_trace_facility_get_by_name
990 *
991 * Returns the GArray of facility indexes. All the fac_ids that matches the
992 * requested facility name.
993 *
994 * If name is not found, returns NULL.
995 */
996GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
997{
998 return g_datalist_id_get_data(&t->facilities_by_name, name);
6cd62ccf 999}
1000
1001/*****************************************************************************
963b5f2d 1002 * Functions to discover all the event types in the trace
6cd62ccf 1003 ****************************************************************************/
1004
3aee1200 1005#if 0
963b5f2d 1006unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 1007{
8d1e6362 1008 unsigned int i;
963b5f2d 1009 unsigned count = 0;
dc1cad90 1010 unsigned int num = t->facility_number;
963b5f2d 1011 LttFacility * f;
dc1cad90 1012
1013 for(i=0;i<num;i++){
963b5f2d 1014 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1015 count += f->event_number;
1016 }
1017 return count;
6cd62ccf 1018}
3aee1200 1019#endif //0
6cd62ccf 1020
3aee1200 1021#if 0
1022//use an iteration on all the trace facilities, and inside iteration on all the
1023//event types in each facilities instead.
963b5f2d 1024LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 1025{
6e8c19d4 1026 LttEventType *event_type;
1027
963b5f2d 1028 LttFacility * f;
1029 f = ltt_trace_facility_by_id(t,evId);
6e8c19d4 1030
1031 if(unlikely(!f)) event_type = NULL;
1032 else event_type = f->events[evId - f->base_id];
1033
1034 return event_type;
6cd62ccf 1035}
3aee1200 1036#endif //0
6cd62ccf 1037
3aee1200 1038#if 0
6cd62ccf 1039/*****************************************************************************
3aee1200 1040 * ltt_trace_find_tracefile
1041 *
1042 * Find a tracefile by name and index in the group.
1043 *
1044 * Returns a pointer to the tracefiles, else NULL.
6cd62ccf 1045 ****************************************************************************/
6cd62ccf 1046
3aee1200 1047LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
6cd62ccf 1048{
6cd62ccf 1049}
3aee1200 1050#endif //0
6cd62ccf 1051
1052/*****************************************************************************
3aee1200 1053 * Get the start time and end time of the trace
6cd62ccf 1054 ****************************************************************************/
1055
3aee1200 1056static void ltt_tracefile_time_span_get(LttTracefile *tf,
1057 LttTime *start, LttTime *end)
6cd62ccf 1058{
3aee1200 1059 struct ltt_block_start_header * header;
1060 int err;
6cd62ccf 1061
3aee1200 1062 err = map_block(tf, 0);
1063 if(unlikely(err)) {
1064 g_error("Can not map block");
1065 *start = ltt_time_infinite;
1066 } else
1067 *start = tf->buffer.begin.timestamp;
1068
1069 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1070 if(unlikely(err)) {
1071 g_error("Can not map block");
1072 *end = ltt_time_zero;
1073 } else
1074 *end = tf->buffer.end.timestamp;
6cd62ccf 1075}
1076
3aee1200 1077struct tracefile_time_span_get_args {
1078 LttTrace *t;
1079 LttTime *start;
1080 LttTime *end;
1081};
963b5f2d 1082
3aee1200 1083static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 1084{
3aee1200 1085 struct tracefile_time_span_get_args *args =
1086 (struct tracefile_time_span_get_args*)user_data;
1087
1088 GArray *group = (GArray *)data;
1089 int i;
1090 LttTracefile *tf;
1091 LttTime tmp_start;
1092 LttTime tmp_end;
1093
1094 for(i=0; i<group->len; i++) {
1095 tf = &g_array_index (group, LttTracefile, i);
1096 if(tf->cpu_online) {
1097 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1098 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1099 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1100 }
1101 }
6cd62ccf 1102}
1103
487ad181 1104void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1105{
3aee1200 1106 LttTime min_start = ltt_time_infinite;
1107 LttTime max_end = ltt_time_zero;
1108 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 1109
3aee1200 1110 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1111
1112 if(start != NULL) *start = min_start;
1113 if(end != NULL) *end = max_end;
1114
487ad181 1115}
1116
1117
6cd62ccf 1118/*****************************************************************************
963b5f2d 1119 *Get the name of a tracefile
6cd62ccf 1120 ****************************************************************************/
1121
3aee1200 1122GQuark ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 1123{
963b5f2d 1124 return tf->name;
6cd62ccf 1125}
1126
80da81ad 1127/*****************************************************************************
1128 * Get the number of blocks in the tracefile
1129 ****************************************************************************/
1130
3aee1200 1131guint ltt_tracefile_block_number(LttTracefile *tf)
80da81ad 1132{
3aee1200 1133 return tf->num_blocks;
80da81ad 1134}
1135
caf7a67a 1136
3aee1200 1137/* Seek to the first event in a tracefile that has a time equal or greater than
1138 * the time passed in parameter.
1139 *
1140 * If the time parameter is outside the tracefile time span, seek to the first
1141 * or the last event of the tracefile.
1142 *
1143 * If the time parameter is before the first event, we have to seek specially to
1144 * there.
1145 *
1146 * If the time is after the end of the trace, get the last event.
1147 *
1148 * Do a binary search to find the right block, then a sequential search in the
1149 * block to find the event.
1150 *
1151 * In the special case where the time requested fits inside a block that has no
1152 * event corresponding to the requested time, the first event of the next block
1153 * will be seeked.
1154 *
1155 * IMPORTANT NOTE : // FIXME everywhere...
1156 *
1157 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1158 * you will jump over an event if you do.
1159 *
1160 * Return value : 0 : no error, the tf->event can be used
1161 * otherwise : this is an error.
1162 *
1163 * */
1164
1165int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1166{
1167 int ret = 0;
1168 int err;
1169 unsigned int block_num, high, low;
1170
1171 /* seek at the beginning of trace */
1172 err = map_block(tf, 0); /* First block */
1173 if(unlikely(err)) {
1174 g_error("Can not map block");
1175 goto fail;
caf7a67a 1176 }
1177
3aee1200 1178 /* If the time is lower or equal the beginning of the trace,
1179 * go to the first event. */
1180 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1181 ret = ltt_tracefile_read(tf);
1182 goto found; /* There is either no event in the trace or the event points
1183 to the first event in the trace */
1184 }
caf7a67a 1185
3aee1200 1186 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1187 if(unlikely(err)) {
1188 g_error("Can not map block");
1189 goto fail;
caf7a67a 1190 }
6cd62ccf 1191
3aee1200 1192 /* If the time is after the end of the trace, get the last event. */
1193 if(ltt_time_compare(time, tf->buffer.end.timestamp) >= 0) {
1194 /* While the ltt_tracefile_read doesn't return ERANGE or EPERM,
1195 * continue reading.
1196 */
1197 while(1) {
1198 ret = ltt_tracefile_read(tf);
1199 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1200 else if(ret) goto fail;
1201 }
1202 }
62e55dd6 1203
3aee1200 1204 /* Binary search the block */
1205 high = tf->num_blocks - 1;
1206 low = 0;
1207
1208 while(1) {
1209 block_num = ((high-low) / 2) + low;
1210
1211 err = map_block(tf, block_num);
1212 if(unlikely(err)) {
1213 g_error("Can not map block");
1214 goto fail;
db55eaae 1215 }
3aee1200 1216 if(high == low) {
1217 /* We cannot divide anymore : this is what would happen if the time
1218 * requested was exactly between two consecutive buffers'end and start
1219 * timestamps. This is also what would happend if we didn't deal with out
1220 * of span cases prior in this function. */
1221 /* The event is right in the buffer!
1222 * (or in the next buffer first event) */
1223 while(1) {
1224 ret = ltt_tracefile_read(tf);
1225 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1226 else if(ret) goto fail;
1227
1228 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1229 break;
1230 }
1231
1232 } if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
1233 /* go to lower part */
1234 high = block_num;
1235 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1236 /* go to higher part */
1237 low = block_num;
1238 } else {/* The event is right in the buffer!
1239 (or in the next buffer first event) */
1240 while(1) {
1241 ltt_tracefile_read(tf);
1242 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1243 else if(ret) goto fail;
1244
1245 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1246 break;
6cd62ccf 1247 }
3aee1200 1248 goto found;
6cd62ccf 1249 }
6cd62ccf 1250 }
3aee1200 1251
1252found:
1253 return 0;
1254
1255 /* Error handling */
1256fail:
1257 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1258 g_quark_to_string(tf->name));
1259 return EPERM;
6cd62ccf 1260}
1261
80da81ad 1262
3aee1200 1263int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
80da81ad 1264
3aee1200 1265 int err;
1266
1267 if(ep->tracefile != tf) {
1268 goto fail;
80da81ad 1269 }
1270
3aee1200 1271 err = map_block(tf, ep->block);
1272 if(unlikely(err)) {
1273 g_error("Can not map block");
1274 goto fail;
1275 }
1276
1277 tf->event.offset = ep->offset;
18206708 1278
3aee1200 1279 err = ltt_tracefile_read_update_event(tf);
1280 if(err) goto fail;
1281 err = ltt_tracefile_read_op(tf);
1282 if(err) goto fail;
80da81ad 1283
1284 return;
3aee1200 1285
1286fail:
1287 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1288 g_quark_to_string(tf->name));
1289}
1290
1291/* Calculate the real event time based on the buffer boundaries */
1292LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1293{
1294 LttTime time;
1295
1296 g_assert(tf->trace->has_tsc);
1297
1298 time = ltt_time_from_uint64(
1299 (guint64)tf->buffer.tsc*tf->buffer.nsecs_per_cycle);
1300 time = ltt_time_add(tf->buffer.begin.timestamp, time);
1301
1302 return time;
80da81ad 1303}
1304
6cd62ccf 1305/*****************************************************************************
1306 *Function name
3aee1200 1307 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1308 *Input params
1309 * t : tracefile
1310 *Return value
3aee1200 1311 *
1312 * Returns 0 if an event can be used in tf->event.
1313 * Returns ERANGE on end of trace. The event in tf->event still can be used.
1314 * Returns EPERM on error.
1315 *
1316 * This function does make the tracefile event structure point to the event
1317 * currently pointed to by the tf->event.
1318 *
1319 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1320 * reinitialize it after an error if you want results to be coherent.
1321 * It would be the case if a end of trace last buffer has no event : the end
1322 * of trace wouldn't be returned, but an error.
1323 * We make the assumption there is at least one event per buffer.
6cd62ccf 1324 ****************************************************************************/
1325
3aee1200 1326int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1327{
963b5f2d 1328 int err;
6cd62ccf 1329
3aee1200 1330 err = ltt_tracefile_read_seek(tf);
1331 if(err) return err;
1332 err = ltt_tracefile_read_update_event(tf);
1333 if(err) return err;
1334 err = ltt_tracefile_read_op(tf);
1335 if(err) return err;
1336
1337 return 0;
1338}
1339
1340int ltt_tracefile_read_seek(LttTracefile *tf)
1341{
1342 int err;
1343
1344 /* Get next buffer until we finally have an event, or end of trace */
1345 while(1) {
1346 err = ltt_seek_next_event(tf);
1347 if(unlikely(err == ENOPROTOOPT)) {
1348 return EPERM;
bdc36259 1349 }
bdc36259 1350
3aee1200 1351 /* Are we at the end of the buffer ? */
1352 if(err == ERANGE) {
1353 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1354 return ERANGE;
1355 } else {
1356 /* get next block */
1357 err = map_block(tf, tf->buffer.index + 1);
1358 if(unlikely(err)) {
1359 g_error("Can not map block");
1360 return EPERM;
1361 }
1362 }
1363 } else break; /* We found an event ! */
1364 }
2dee981d 1365
3aee1200 1366 return 0;
1367}
18206708 1368
1369
3aee1200 1370/* do specific operation on events */
1371int ltt_tracefile_read_op(LttTracefile *tf)
1372{
1373 int err;
1374 LttFacility *f;
1375 void * pos;
1376 LttEvent *event;
1377
1378 event = &tf->event;
18206708 1379
3aee1200 1380 /* do event specific operation */
40331ba8 1381
3aee1200 1382 /* do something if its an heartbeat event : increment the heartbeat count */
1383 //if(event->facility_id == LTT_FACILITY_CORE)
1384 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1385 // tf->cur_heart_beat_number++;
1386
1387 return 0;
6cd62ccf 1388}
1389
6cd62ccf 1390
3aee1200 1391/* same as ltt_tracefile_read, but does not seek to the next event nor call
1392 * event specific operation. */
1393int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1394{
3aee1200 1395 int err;
1396 LttFacility *f;
1397 void * pos;
1398 LttEvent *event;
1399
1400 event = &tf->event;
1401 pos = event->offset;
1402
1403 /* Read event header */
1404
1405 //TODO align
1406
1407 if(tf->trace->has_tsc) {
1408 event->time.timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1409 pos);
1410 /* 32 bits -> 64 bits tsc */
1411 /* note : still works for seek and non seek cases. */
1412 if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1413 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1414 + 0x100000000ULL)
1415 | (guint64)event->time.timestamp;
1416 event->tsc = tf->buffer.tsc;
1417 } else {
1418 /* no overflow */
1419 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1420 | (guint64)event->time.timestamp;
1421 event->tsc = tf->buffer.tsc;
1422 }
1423
1424 event->event_time = ltt_interpolate_time(tf, event);
1425
1426 pos += sizeof(guint32);
1427 } else {
1428 event->time.delta.tv_sec = 0;
1429 event->time.delta.tv_nsec = ltt_get_uint32(LTT_GET_BO(tf),
1430 pos) * NSEC_PER_USEC;
1431 tf->buffer.tsc = 0;
1432 event->tsc = tf->buffer.tsc;
1433
1434 event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
1435 event->time.delta);
1436 pos += sizeof(guint32);
1437 }
1438
1439 event->facility_id = ltt_get_uint8(LTT_GET_BO(tf),
1440 pos);
1441 pos += sizeof(guint8);
1442
1443 event->event_id = ltt_get_uint8(LTT_GET_BO(tf),
1444 pos);
1445 pos += sizeof(guint8);
1446
1447 event->data = pos;
1448
1449 return 0;
6cd62ccf 1450}
1451
507915ee 1452
6cd62ccf 1453/****************************************************************************
1454 *Function name
3aee1200 1455 * map_block : map a block from the file
6cd62ccf 1456 *Input Params
1457 * lttdes : ltt trace file
1458 * whichBlock : the block which will be read
1459 *return value
1460 * 0 : success
1461 * EINVAL : lseek fail
1462 * EIO : can not read from the file
1463 ****************************************************************************/
1464
3aee1200 1465static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1466{
3aee1200 1467 struct ltt_block_start_header *header;
1468
1469 g_assert(block_num < tf->num_blocks);
6cd62ccf 1470
3aee1200 1471 if(tf->buffer.head != NULL)
1472 munmap(tf->buffer.head, tf->buf_size);
ac849774 1473
3aee1200 1474 /* Multiple of pages aligned head */
1475 tf->buffer.head = mmap(0, tf->block_size, PROT_READ, tf->fd, MAP_PRIVATE,
1476 (off_t)tf->block_size * (off_t)block_num);
1477
1478 if(tf->buffer.head == NULL) {
1479 perror("Error in allocating memory for buffer of tracefile");
1480 goto map_error;
6cd62ccf 1481 }
3aee1200 1482 g_assert(((guint)tf->buffer.head) & (8-1) == 0); // make sure it's aligned.
1483
6cd62ccf 1484
3aee1200 1485 tf->buffer.index = block_num;
1486
1487 header = (struct ltt_block_start_header*)tf->buffer.head;
1488
1489 tf->buffer.begin.timestamp = ltt_get_time(LTT_GET_BO(tf),
1490 &header->begin.timestamp);
1491 tf->buffer.begin.timestamp.tv_nsec *= NSEC_PER_USEC;
1492 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1493 &header->begin.cycle_count);
1494 tf->buffer.end.timestamp = ltt_get_time(LTT_GET_BO(tf),
1495 &header->end.timestamp);
1496 tf->buffer.end.timestamp.tv_nsec *= NSEC_PER_USEC;
1497 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1498 &header->end.cycle_count);
1499 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
1500 &header->lost_size);
6cd62ccf 1501
3aee1200 1502 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1503 tf->event.tsc = tf->buffer.tsc;
1504
1505 /* FIXME
1506 * eventually support variable buffer size : will need a partial pre-read of
1507 * the headers to create an index when we open the trace... eventually. */
1508 g_assert(tf->block_size == ltt_get_uint32(LTT_GET_BO(tf),
1509 &header->buf_size));
507915ee 1510
3aee1200 1511 /* Now that the buffer is mapped, calculate the time interpolation for the
1512 * block. */
1513
1514 tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1515
1516 /* Make the current event point to the beginning of the buffer :
1517 * it means that the event read must get the first event. */
1518 tf->event.tracefile = tf;
1519 tf->event.block = block_num;
1520 tf->event.offset = tf->buffer.head;
1521
1522 return 0;
6cd62ccf 1523
3aee1200 1524map_error:
1525 return -errno;
40331ba8 1526
6cd62ccf 1527}
1528
3aee1200 1529ssize_t ltt_get_event_size(LttTracefile *tf)
6cd62ccf 1530{
3aee1200 1531 ssize_t size = 0;
1532
1533 /* Specific handling of core events : necessary to read the facility control
1534 * tracefile. */
1535 if(unlikely(tf->event.facility_id == LTT_FACILITY_CORE)) {
1536 switch((enum ltt_core_events)tf->event.event_id) {
1537 case LTT_EVENT_FACILITY_LOAD:
1538 size = sizeof(struct LttFacilityLoad);
1539 break;
1540 case LTT_EVENT_FACILITY_UNLOAD:
1541 size = sizeof(struct LttFacilityUnload);
1542 break;
1543 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
1544 size = sizeof(struct LttStateDumpFacilityLoad);
1545 break;
1546 case LTT_EVENT_HEARTBEAT:
1547 size = sizeof(TimeHeartbeat);
1548 break;
1549 default:
1550 g_warning("Error in getting event size : tracefile %s, "
1551 "unknown event id %hhu in core facility.",
1552 g_quark_to_string(tf->name),
1553 tf->event.event_id);
1554 goto event_id_error;
6cd62ccf 1555
3aee1200 1556 }
2dee981d 1557
3aee1200 1558 } else {
1559 LttFacility *f = ltt_trace_get_facility_by_num(tf->trace,
1560 tf->event.facility_id);
1561 LttEventType *event_type =
1562 ltt_facility_eventtype_get(f, tf->event.event_id);
1563 size = get_fields_offsets(tf, event_type, tf->event.data);
1564 }
2dee981d 1565
3aee1200 1566 return size;
1567
1568event_id_error:
1569 return -1;
6cd62ccf 1570}
1571
6cd62ccf 1572
3aee1200 1573/* Take the tf current event offset and use the event facility id and event id
1574 * to figure out where is the next event offset.
1575 *
1576 * This is an internal function not aiming at being used elsewhere : it will
1577 * not jump over the current block limits. Please consider using
1578 * ltt_tracefile_read to do this.
1579 *
1580 * Returns 0 on success
1581 * ERANGE if we are at the end of the buffer.
1582 * ENOPROTOOPT if an error occured when getting the current event size.
1583 */
1584static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1585{
3aee1200 1586 int ret = 0;
1587 void *pos;
1588 ssize_t event_size;
1589
1590 /* seek over the buffer header if we are at the buffer start */
1591 if(tf->event.offset == tf->buffer.head) {
1592 tf->event.offset += sizeof(struct ltt_block_start_header);
1593 goto found;
1594 }
6cd62ccf 1595
6cd62ccf 1596
3aee1200 1597 if(tf->event.offset == tf->buffer.head + tf->buffer.lost_size) {
1598 ret = ERANGE;
1599 goto found;
6cd62ccf 1600 }
1601
3aee1200 1602 pos = tf->event.data;
1603
1604 event_size = ltt_get_event_size(tf);
1605 if(event_size < 0) goto error;
1606
1607 pos += (size_t)event_size;
1608
1609 tf->event.offset = pos;
1610
1611found:
1612 return ret;
1613
1614error:
1615 g_error("Error in ltt_seek_next_event for tracefile %s",
1616 g_quark_to_string(tf->name));
1617 return ENOPROTOOPT;
6cd62ccf 1618}
1619
18206708 1620
6cd62ccf 1621/*****************************************************************************
1622 *Function name
3aee1200 1623 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
6cd62ccf 1624 *Input Params
1625 * t : tracefile
1626 ****************************************************************************/
1627
3aee1200 1628static double calc_nsecs_per_cycle(LttTracefile * tf)
6cd62ccf 1629{
963b5f2d 1630 LttTime lBufTotalTime; /* Total time for this buffer */
887208b7 1631 double lBufTotalNSec; /* Total time for this buffer in nsecs */
8ee1c3d5 1632 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1633
1634 /* Calculate the total time for this buffer */
01bb5763 1635 lBufTotalTime = ltt_time_sub(
3aee1200 1636 ltt_get_time(LTT_GET_BO(tf), &tf->buffer.end.timestamp),
1637 ltt_get_time(LTT_GET_BO(tf), &tf->buffer.begin.timestamp));
6cd62ccf 1638
1639 /* Calculate the total cycles for this bufffer */
3aee1200 1640 lBufTotalCycle = ltt_get_uint64(LTT_GET_BO(tf), &tf->buffer.end.cycle_count);
1641 lBufTotalCycle -= ltt_get_uint64(LTT_GET_BO(tf),
1642 &tf->buffer.begin.cycle_count);
6cd62ccf 1643
8ee1c3d5 1644 /* Convert the total time to double */
887208b7 1645 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
6cd62ccf 1646
3aee1200 1647 return lBufTotalNSec / (double)lBufTotalCycle;
8ee1c3d5 1648
3aee1200 1649}
1650#if 0
1651void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
1652{
1653 LttField * rootFld = evT->root_field;
1654 // rootFld->base_address = evD;
2dee981d 1655
3aee1200 1656 if(likely(rootFld))
1657 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
1658 evT, 0,0,rootFld, evD);
6cd62ccf 1659}
3aee1200 1660#endif //0
6cd62ccf 1661
3aee1200 1662/*****************************************************************************
6cd62ccf 1663 *Function name
3aee1200 1664 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 1665 *Input params
3aee1200 1666 * tracefile : opened trace file
1667 * event_type : the event type
6cd62ccf 1668 ****************************************************************************/
1669
3aee1200 1670void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 1671{
3aee1200 1672 LttField *field = event_type->root_field;
1673 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
1674
1675 if(likely(field))
1676 preset_field_type_size(tf, event_type, 0, 0,
1677 &fixed_root, &fixed_parent,
1678 field);
1679
1680}
e4eced0f 1681
bbf28e50 1682
3aee1200 1683/*****************************************************************************
1684 *Function name
1685 * preset_field_type_size : set the fixed sizes of the field type
1686 *Input params
1687 * tf : tracefile
1688 * event_type : event type
1689 * offset_root : offset from the root
1690 * offset_parent : offset from the parent
1691 * fixed_root : Do we know a fixed offset to the root ?
1692 * fixed_parent : Do we know a fixed offset to the parent ?
1693 * field : field
1694 ****************************************************************************/
1695void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
1696 off_t offset_root, off_t offset_parent,
1697 enum field_status *fixed_root, enum field_status *fixed_parent,
1698 LttField *field)
1699{
1700 enum field_status local_fixed_root, local_fixed_parent;
1701 guint i;
1702 LttType *type;
dfb73233 1703
3aee1200 1704 g_assert(field->fixed_root == FIELD_UNKNOWN);
1705 g_assert(field->fixed_parent == FIELD_UNKNOWN);
1706 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 1707
3aee1200 1708 type = field->field_type;
1709
1710 field->fixed_root = *fixed_root;
1711 if(field->fixed_root == FIELD_FIXED)
1712 field->offset_root = offset_root;
1713 else
1714 field->offset_root = 0;
1715
1716 field->fixed_parent = *fixed_parent;
1717 if(field->fixed_parent == FIELD_FIXED)
1718 field->offset_parent = offset_parent;
1719 else
1720 field->offset_parent = 0;
1721
1722 size_t current_root_offset;
1723 size_t current_offset;
1724 enum field_status current_child_status, final_child_status;
1725 size_t max_size;
1726
1727 switch(type->type_class) {
1728 case LTT_INT:
1729 case LTT_UINT:
1730 case LTT_FLOAT:
1731 case LTT_ENUM:
1732 field->field_size = ltt_type_size(tf->trace, type);
1733 field->fixed_size = FIELD_FIXED;
1734 break;
1735 case LTT_POINTER:
1736 field->field_size = (off_t)event_type->facility->pointer_size;
1737 field->fixed_size = FIELD_FIXED;
1738 break;
1739 case LTT_LONG:
1740 case LTT_ULONG:
1741 field->field_size = (off_t)event_type->facility->pointer_size;
1742 field->fixed_size = FIELD_FIXED;
1743 break;
1744 case LTT_SIZE_T:
1745 case LTT_SSIZE_T:
1746 case LTT_OFF_T:
1747 field->field_size = (off_t)event_type->facility->size_t_size;
1748 field->fixed_size = FIELD_FIXED;
1749 break;
1750 case LTT_SEQUENCE:
1751 local_fixed_root = FIELD_VARIABLE;
1752 local_fixed_parent = FIELD_VARIABLE;
1753 preset_field_type_size(tf, event_type,
1754 0, 0,
1755 &local_fixed_root, &local_fixed_parent,
1756 field->child[0]);
1757 field->fixed_size = FIELD_VARIABLE;
1758 field->field_size = 0;
1759 break;
1760 case LTT_STRING:
1761 field->fixed_size = FIELD_VARIABLE;
1762 field->field_size = 0;
1763 break;
1764 case LTT_ARRAY:
1765 local_fixed_root = FIELD_VARIABLE;
1766 local_fixed_parent = FIELD_VARIABLE;
1767 preset_field_type_size(tf, event_type,
1768 0, 0,
1769 &local_fixed_root, &local_fixed_parent,
1770 field->child[0]);
1771 field->fixed_size = field->child[0]->fixed_size;
1772 if(field->fixed_size == FIELD_FIXED)
1773 field->field_size = type->element_number * field->child[0]->field_size;
1774 else
1775 field->field_size = 0;
1776 break;
1777 case LTT_STRUCT:
1778 current_root_offset = field->offset_root;
1779 current_offset = 0;
1780 current_child_status = FIELD_FIXED;
1781 for(i=0;i<type->element_number;i++) {
1782 preset_field_type_size(tf, event_type,
1783 current_root_offset, current_offset,
1784 fixed_root, &current_child_status,
1785 field->child[i]);
1786 if(current_child_status == FIELD_FIXED) {
1787 current_root_offset += field->child[i]->field_size;
1788 current_offset += field->child[i]->field_size;
1789 } else {
1790 current_root_offset = 0;
1791 current_offset = 0;
1792 }
1793 }
1794 if(current_child_status != FIELD_FIXED) {
1795 *fixed_parent = current_child_status;
1796 field->field_size = 0;
1797 field->fixed_size = current_child_status;
1798 } else {
1799 field->field_size = current_offset;
1800 field->fixed_size = FIELD_FIXED;
1801 }
1802 break;
1803 case LTT_UNION:
1804 current_root_offset = field->offset_root;
1805 current_offset = 0;
1806 max_size = 0;
1807 final_child_status = FIELD_FIXED;
1808 for(i=0;i<type->element_number;i++) {
1809 enum field_status current_root_child_status = FIELD_FIXED;
1810 enum field_status current_child_status = FIELD_FIXED;
1811 preset_field_type_size(tf, event_type,
1812 current_root_offset, current_offset,
1813 &current_root_child_status, &current_child_status,
1814 field->child[i]);
1815 if(current_child_status != FIELD_FIXED)
1816 final_child_status = current_child_status;
1817 else
1818 max_size = max(max_size, field->child[i]->field_size);
1819 }
1820 if(final_child_status != FIELD_FIXED) {
1821 *fixed_root = final_child_status;
1822 *fixed_parent = final_child_status;
1823 field->field_size = 0;
1824 field->fixed_size = current_child_status;
1825 } else {
1826 field->field_size = max_size;
1827 field->fixed_size = FIELD_FIXED;
1828 }
1829 break;
dfb73233 1830 }
1831
6cd62ccf 1832}
1833
3aee1200 1834
6cd62ccf 1835/*****************************************************************************
1836 *Function name
3aee1200 1837 * get_field_type_size : set the fixed and dynamic sizes of the field type
1838 * from the data read.
6cd62ccf 1839 *Input params
3aee1200 1840 * tf : tracefile
1841 * event_type : event type
1842 * offset_root : offset from the root
1843 * offset_parent : offset from the parent
1844 * field : field
1845 * data : a pointer to the event data.
1846 *Returns the field type size.
6cd62ccf 1847 ****************************************************************************/
3aee1200 1848size_t get_field_type_size(LttTracefile *tf, LttEventType *event_type,
1849 off_t offset_root, off_t offset_parent,
1850 LttField *field, void *data)
6cd62ccf 1851{
3aee1200 1852 size_t size = 0;
1853 guint i;
1854 LttType *type;
1855
1856 g_assert(field->fixed_root != FIELD_UNKNOWN);
1857 g_assert(field->fixed_parent != FIELD_UNKNOWN);
1858 g_assert(field->fixed_size != FIELD_UNKNOWN);
6cd62ccf 1859
3aee1200 1860 field->offset_root = offset_root;
1861 field->offset_parent = offset_parent;
1862
1863 type = field->field_type;
1864
1865 switch(type->type_class) {
1866 case LTT_INT:
1867 case LTT_UINT:
1868 case LTT_FLOAT:
1869 case LTT_ENUM:
1870 case LTT_POINTER:
1871 case LTT_LONG:
1872 case LTT_ULONG:
1873 case LTT_SIZE_T:
1874 case LTT_SSIZE_T:
1875 case LTT_OFF_T:
1876 g_assert(field->fixed_size == FIELD_FIXED);
1877 size = field->field_size;
1878 break;
1879 case LTT_SEQUENCE:
1880 {
1881 gint seqnum = ltt_get_uint(LTT_GET_BO(tf),
1882 field->sequ_number_size,
1883 data + offset_root);
1884
1885 if(field->child[0]->fixed_size == FIELD_FIXED) {
1886 size = field->sequ_number_size +
1887 (seqnum * get_field_type_size(tf, event_type,
1888 offset_root, offset_parent,
1889 field->child[0], data));
1890 } else {
1891 size += field->sequ_number_size;
1892 for(i=0;i<seqnum;i++) {
1893 size_t child_size;
1894 child_size = get_field_type_size(tf, event_type,
1895 offset_root, offset_parent,
1896 field->child[0], data);
1897 offset_root += child_size;
1898 offset_parent += child_size;
1899 size += child_size;
1900 }
1901 }
1902 field->field_size = size;
1903 }
1904 break;
1905 case LTT_STRING:
1906 size = strlen((char*)(data+offset_root)) + 1;// length + \0
1907 field->field_size = size;
1908 break;
1909 case LTT_ARRAY:
1910 if(field->fixed_size == FIELD_FIXED)
1911 size = field->field_size;
1912 else {
1913 for(i=0;i<field->field_type->element_number;i++) {
1914 size_t child_size;
1915 child_size = get_field_type_size(tf, event_type,
1916 offset_root, offset_parent,
1917 field->child[0], data);
1918 offset_root += child_size;
1919 offset_parent += child_size;
1920 size += child_size;
1921 }
1922 field->field_size = size;
1923 }
1924 break;
1925 case LTT_STRUCT:
1926 if(field->fixed_size == FIELD_FIXED)
1927 size = field->field_size;
1928 else {
1929 size_t current_root_offset = offset_root;
1930 size_t current_offset = 0;
1931 size_t child_size = 0;
1932 for(i=0;i<type->element_number;i++) {
1933 child_size = get_field_type_size(tf,
1934 event_type, current_root_offset, current_offset,
1935 field->child[i], data);
1936 current_offset += child_size;
1937 current_root_offset += child_size;
1938
1939 }
1940 size = current_offset;
1941 field->field_size = size;
1942 }
1943 break;
1944 case LTT_UNION:
1945 if(field->fixed_size == FIELD_FIXED)
1946 size = field->field_size;
1947 else {
1948 size_t current_root_offset = field->offset_root;
1949 size_t current_offset = 0;
1950 for(i=0;i<type->element_number;i++) {
1951 size = get_field_type_size(tf, event_type,
1952 current_root_offset, current_offset,
1953 field->child[i], data);
1954 size = max(size, field->child[i]->field_size);
1955 }
1956 field->field_size = size;
1957 }
1958 break;
1959 }
1960
1961 return size;
6cd62ccf 1962}
1963
3aee1200 1964
1965
1966#if 0
6cd62ccf 1967/*****************************************************************************
1968 *Function name
1969 * getFieldtypeSize: get the size of the field type (primitive type)
1970 *Input params
6cd62ccf 1971 * evT : event type
1972 * offsetRoot : offset from the root
1973 * offsetParent : offset from the parrent
1974 * fld : field
1975 * evD : event data, it may be NULL
1976 *Return value
1977 * int : size of the field
1978 ****************************************************************************/
1979
3aee1200 1980static inline gint getFieldtypeSize(LttTracefile *tf,
21182d4a 1981 LttEventType * evT, gint offsetRoot,
3aee1200 1982 gint offsetParent, LttField * fld, void *evD)
6cd62ccf 1983{
21182d4a 1984 gint size, size1, element_number, i, offset1, offset2;
963b5f2d 1985 LttType * type = fld->field_type;
6cd62ccf 1986
3aee1200 1987 /* This likely has been tested with gcov : half of them.. */
1988 if(unlikely(fld->field_fixed == 1)){
1989 /* tested : none */
1990 if(unlikely(fld == evT->root_field)) {
1991 size = fld->field_size;
1992 goto end_getFieldtypeSize;
21182d4a 1993 }
3aee1200 1994 }
6cd62ccf 1995
3aee1200 1996 /* From gcov profiling : half string, half struct, can we gain something
1997 * from that ? (Mathieu) */
1998 switch(type->type_class) {
1999 case LTT_ARRAY:
2000 element_number = (int) type->element_number;
2001 if(fld->field_fixed == -1){
2002 size = getFieldtypeSize(tf, evT, offsetRoot,
2003 0,fld->child[0], NULL);
2004 if(size == 0){ //has string or sequence
cf74a6f1 2005 fld->field_fixed = 0;
3aee1200 2006 }else{
2007 fld->field_fixed = 1;
2008 size *= element_number;
a0d63196 2009 }
3aee1200 2010 }else if(fld->field_fixed == 0){// has string or sequence
cf74a6f1 2011 size = 0;
3aee1200 2012 for(i=0;i<element_number;i++){
2013 size += getFieldtypeSize(tf, evT, offsetRoot+size,size,
2014 fld->child[0], evD+size);
cf74a6f1 2015 }
3aee1200 2016 }else size = fld->field_size;
2017 if(unlikely(!evD)){
a0d63196 2018 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2019 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
3aee1200 2020 }
cf74a6f1 2021
3aee1200 2022 break;
2023
2024 case LTT_SEQUENCE:
2025 size1 = (int) ltt_type_size(fac, type);
2026 if(fld->field_fixed == -1){
2027 fld->sequ_number_size = size1;
2028 fld->field_fixed = 0;
2029 size = getFieldtypeSize(evT, offsetRoot,
2030 0,fld->child[0], NULL);
2031 fld->element_size = size;
2032 }else{//0: sequence
2033 element_number = getIntNumber(tf,size1,evD);
2034 type->element_number = element_number;
2035 if(fld->element_size > 0){
2036 size = element_number * fld->element_size;
2037 }else{//sequence has string or sequence
2038 size = 0;
cf74a6f1 2039 for(i=0;i<element_number;i++){
3aee1200 2040 size += getFieldtypeSize(tf, evT,
2041 offsetRoot+size+size1,size+size1,
2042 fld->child[0], evD+size+size1);
cf74a6f1 2043 }
3aee1200 2044 }
2045 size += size1;
2046 }
2047 if(unlikely(!evD)){
d37b2aaa 2048 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2049 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
3aee1200 2050 }
cf74a6f1 2051
3aee1200 2052 break;
2053
2054 case LTT_STRING:
2055 size = 0;
2056 if(fld->field_fixed == -1){
2057 fld->field_fixed = 0;
2058 }else{//0: string
2059 /* Hope my implementation is faster than strlen (Mathieu) */
2060 char *ptr=(char*)evD;
2061 size = 1;
2062 /* from gcov : many many strings are empty, make it the common case.*/
2063 while(unlikely(*ptr != '\0')) { size++; ptr++; }
2064 //size = ptr - (char*)evD + 1; //include end : '\0'
2065 }
2066 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2067 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2068
2069 break;
2070
2071 case LTT_STRUCT:
2072 element_number = (int) type->element_number;
2073 size = 0;
2074 /* tested with gcov */
2075 if(unlikely(fld->field_fixed == -1)){
2076 offset1 = offsetRoot;
2077 offset2 = 0;
2078 for(i=0;i<element_number;i++){
2079 size1=getFieldtypeSize(tf, evT,offset1,offset2,
2080 fld->child[i], NULL);
2081 if(likely(size1 > 0 && size >= 0)){
2082 size += size1;
2083 if(likely(offset1 >= 0)) offset1 += size1;
2084 offset2 += size1;
2085 }else{
2086 size = -1;
2087 offset1 = -1;
2088 offset2 = -1;
2089 }
a0d63196 2090 }
3aee1200 2091 if(unlikely(size == -1)){
2092 fld->field_fixed = 0;
2093 size = 0;
2094 }else fld->field_fixed = 1;
2095 }else if(likely(fld->field_fixed == 0)){
2096 offset1 = offsetRoot;
2097 offset2 = 0;
2098 for(i=0;unlikely(i<element_number);i++){
2099 size=getFieldtypeSize(tf, evT, offset1, offset2,
2100 fld->child[i], evD+offset2);
2101 offset1 += size;
2102 offset2 += size;
2103 }
2104 size = offset2;
2105 }else size = fld->field_size;
2106 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2107 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2108 break;
2109
2110 default:
2111 if(unlikely(fld->field_fixed == -1)){
2112 size = (int) ltt_type_size(LTT_GET_BO(tf), type);
2113 fld->field_fixed = 1;
2114 }else size = fld->field_size;
2115 if(unlikely(!evD)){
2116 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2117 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2118 }
2119 break;
cf74a6f1 2120 }
2121
6cd62ccf 2122 fld->offset_root = offsetRoot;
2123 fld->offset_parent = offsetParent;
6cd62ccf 2124 fld->field_size = size;
2125
21182d4a 2126end_getFieldtypeSize:
2127
6cd62ccf 2128 return size;
2129}
3aee1200 2130#endif //0
6cd62ccf 2131
2132/*****************************************************************************
2133 *Function name
3aee1200 2134 * get_int : get an integer number
6cd62ccf 2135 *Input params
3aee1200 2136 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2137 * size : the size of the integer
3aee1200 2138 * ptr : the data pointer
6cd62ccf 2139 *Return value
cf74a6f1 2140 * gint64 : a 64 bits integer
6cd62ccf 2141 ****************************************************************************/
2142
3aee1200 2143gint64 get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2144{
3aee1200 2145 gint64 val;
cf74a6f1 2146
2147 switch(size) {
3aee1200 2148 case 1: val = *((gint8*)data); break;
2149 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2150 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2151 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2152 default: val = ltt_get_int64(reverse_byte_order, data);
2153 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2154 break;
2155 }
2156
3aee1200 2157 return val;
6cd62ccf 2158}
3aee1200 2159
6cd62ccf 2160/*****************************************************************************
2161 *Function name
3aee1200 2162 * get_uint : get an unsigned integer number
6cd62ccf 2163 *Input params
3aee1200 2164 * reverse_byte_order: must we reverse the byte order ?
2165 * size : the size of the integer
2166 * ptr : the data pointer
2167 *Return value
2168 * guint64 : a 64 bits unsigned integer
6cd62ccf 2169 ****************************************************************************/
2170
3aee1200 2171guint64 get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2172{
3aee1200 2173 guint64 val;
2174
2175 switch(size) {
2176 case 1: val = *((gint8*)data); break;
2177 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2178 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2179 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2180 default: val = ltt_get_uint64(reverse_byte_order, data);
2181 g_critical("get_uint : unsigned integer size %d unknown",
2182 size);
2183 break;
2184 }
2185
2186 return val;
6cd62ccf 2187}
3aee1200 2188
2189
a5dcde2f 2190/* get the node name of the system */
2191
2192char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2193{
2194 return s->node_name;
2195}
2196
2197
2198/* get the domain name of the system */
2199
2200char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2201{
2202 return s->domain_name;
2203}
2204
2205
2206/* get the description of the system */
2207
2208char * ltt_trace_system_description_description (LttSystemDescription * s)
2209{
2210 return s->description;
2211}
2212
2213
2214/* get the start time of the trace */
2215
2216LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
2217{
2218 return s->trace_start;
2219}
2220
18206708 2221
2222LttTracefile *ltt_tracefile_new()
2223{
2224 return g_new(LttTracefile, 1);
2225}
2226
2227void ltt_tracefile_destroy(LttTracefile *tf)
2228{
2229 g_free(tf);
2230}
2231
2232void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
2233{
2234 *dest = *src;
2235}
2236
3aee1200 2237/* Before library loading... */
2238
2239static void __attribute__((constructor)) init(void)
2240{
2241 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2242 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2243
2244 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("control/facilities");
2245}
2246
This page took 0.152819 seconds and 4 git commands to generate.