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