ltt-types fix
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
CommitLineData
449cb9d7 1/* This file is part of the Linux Trace Toolkit viewer
57df94dd 2 * Copyright (C) 2003-2004 Xiangxiu Yang, Mathieu Desnoyers
449cb9d7 3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
6cd62ccf 19#include <stdio.h>
20#include <fcntl.h>
8d1e6362 21#include <string.h>
22#include <dirent.h>
6cd62ccf 23#include <sys/stat.h>
24#include <sys/types.h>
8d1e6362 25#include <errno.h>
26#include <unistd.h>
42db9bf1 27#include <math.h>
6cd62ccf 28
ef35d837 29// For realpath
30#include <limits.h>
31#include <stdlib.h>
32
33
6cd62ccf 34#include "parser.h"
a5dcde2f 35#include <ltt/ltt.h>
36#include "ltt-private.h"
963b5f2d 37#include <ltt/trace.h>
a5dcde2f 38#include <ltt/facility.h>
c02ea99f 39#include <ltt/event.h>
8d1e6362 40#include <ltt/type.h>
6cd62ccf 41
963b5f2d 42#define DIR_NAME_SIZE 256
51b5991e 43#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 44
a1062ddd 45#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
46#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
47
8959a0c8 48
49/* obtain the time of an event */
50
51static inline LttTime getEventTime(LttTracefile * tf);
52
53
6cd62ccf 54/* set the offset of the fields belonging to the event,
55 need the information of the archecture */
40331ba8 56void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
6cd62ccf 57
58/* get the size of the field type according to the archtecture's
59 size and endian type(info of the archecture) */
21182d4a 60static inline gint getFieldtypeSize(LttTracefile * tf,
61 LttEventType * evT, gint offsetRoot,
62 gint offsetParent, LttField *fld, void *evD, LttTrace* t);
6cd62ccf 63
64/* read a fixed size or a block information from the file (fd) */
65int readFile(int fd, void * buf, size_t size, char * mesg);
963b5f2d 66int readBlock(LttTracefile * tf, int whichBlock);
6cd62ccf 67
68/* calculate cycles per nsec for current block */
963b5f2d 69void getCyclePerNsec(LttTracefile * t);
6cd62ccf 70
71/* reinitialize the info of the block which is already in the buffer */
963b5f2d 72void updateTracefile(LttTracefile * tf);
6cd62ccf 73
74/* go to the next event */
963b5f2d 75int skipEvent(LttTracefile * t);
6cd62ccf 76
6cd62ccf 77
43da6a59 78/* Functions to parse system.xml file (using glib xml parser) */
51b5991e 79static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
43da6a59 80 const gchar *element_name,
81 const gchar **attribute_names,
82 const gchar **attribute_values,
83 gpointer user_data,
84 GError **error)
85{
86 int i=0;
87 LttSystemDescription* des = (LttSystemDescription* )user_data;
88 if(strcmp("system", element_name)){
2a74fbf4 89 *error = g_error_new(G_MARKUP_ERROR,
90 G_LOG_LEVEL_WARNING,
91 "This is not system.xml file");
92 return;
43da6a59 93 }
94
95 while(attribute_names[i]){
96 if(strcmp("node_name", attribute_names[i])==0){
97 des->node_name = g_strdup(attribute_values[i]);
98 }else if(strcmp("domainname", attribute_names[i])==0){
99 des->domain_name = g_strdup(attribute_values[i]);
100 }else if(strcmp("cpu", attribute_names[i])==0){
101 des->nb_cpu = atoi(attribute_values[i]);
102 }else if(strcmp("arch_size", attribute_names[i])==0){
103 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
104 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
105 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
106 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
107 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
108 }else if(strcmp("endian", attribute_names[i])==0){
109 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
110 des->endian = LTT_LITTLE_ENDIAN;
111 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
112 des->endian = LTT_BIG_ENDIAN;
113 }else if(strcmp("kernel_name", attribute_names[i])==0){
114 des->kernel_name = g_strdup(attribute_values[i]);
115 }else if(strcmp("kernel_release", attribute_names[i])==0){
116 des->kernel_release = g_strdup(attribute_values[i]);
117 }else if(strcmp("kernel_version", attribute_names[i])==0){
118 des->kernel_version = g_strdup(attribute_values[i]);
119 }else if(strcmp("machine", attribute_names[i])==0){
120 des->machine = g_strdup(attribute_values[i]);
121 }else if(strcmp("processor", attribute_names[i])==0){
122 des->processor = g_strdup(attribute_values[i]);
123 }else if(strcmp("hardware_platform", attribute_names[i])==0){
124 des->hardware_platform = g_strdup(attribute_values[i]);
125 }else if(strcmp("operating_system", attribute_names[i])==0){
126 des->operating_system = g_strdup(attribute_values[i]);
127 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
128 des->ltt_major_version = atoi(attribute_values[i]);
129 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
130 des->ltt_minor_version = atoi(attribute_values[i]);
131 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
132 des->ltt_block_size = atoi(attribute_values[i]);
133 }else{
2a74fbf4 134 *error = g_error_new(G_MARKUP_ERROR,
135 G_LOG_LEVEL_WARNING,
136 "Not a valid attribute");
137 return;
43da6a59 138 }
139 i++;
140 }
141}
142
51b5991e 143static void parser_characters (GMarkupParseContext __UNUSED__ *context,
43da6a59 144 const gchar *text,
51b5991e 145 gsize __UNUSED__ text_len,
43da6a59 146 gpointer user_data,
51b5991e 147 GError __UNUSED__ **error)
43da6a59 148{
149 LttSystemDescription* des = (LttSystemDescription* )user_data;
150 des->description = g_strdup(text);
151}
152
153
6cd62ccf 154/*****************************************************************************
155 *Function name
963b5f2d 156 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 157 *Input params
963b5f2d 158 * t : the trace containing the tracefile
159 * fileName : path name of the trace file
6cd62ccf 160 *Return value
963b5f2d 161 * : a pointer to a tracefile
6cd62ccf 162 ****************************************************************************/
163
963b5f2d 164LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName)
6cd62ccf 165{
963b5f2d 166 LttTracefile * tf;
167 struct stat lTDFStat; /* Trace data file status */
6cd62ccf 168
963b5f2d 169 tf = g_new(LttTracefile, 1);
6cd62ccf 170
171 //open the file
963b5f2d 172 tf->name = g_strdup(fileName);
173 tf->trace = t;
6cd62ccf 174 tf->fd = open(fileName, O_RDONLY, 0);
175 if(tf->fd < 0){
2a74fbf4 176 g_warning("Unable to open input data file %s\n", fileName);
177 g_free(tf->name);
178 g_free(tf);
179 return NULL;
6cd62ccf 180 }
181
182 // Get the file's status
183 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 184 g_warning("Unable to get the status of the input data file %s\n", fileName);
185 g_free(tf->name);
186 close(tf->fd);
187 g_free(tf);
188 return NULL;
6cd62ccf 189 }
190
191 // Is the file large enough to contain a trace
8d1e6362 192 if(lTDFStat.st_size < (off_t)(sizeof(BlockStart) + EVENT_HEADER_SIZE)){
8710c6c7 193 g_print("The input data file %s does not contain a trace\n", fileName);
663bd9ff 194 g_free(tf->name);
195 close(tf->fd);
196 g_free(tf);
8710c6c7 197 return NULL;
6cd62ccf 198 }
199
200 //store the size of the file
201 tf->file_size = lTDFStat.st_size;
963b5f2d 202 tf->block_size = t->system_description->ltt_block_size;
203 tf->block_number = tf->file_size / tf->block_size;
204 tf->which_block = 0;
6cd62ccf 205
206 //allocate memory to contain the info of a block
963b5f2d 207 tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size);
6cd62ccf 208
963b5f2d 209 //read the first block
210 if(readBlock(tf,1)) exit(1);
6cd62ccf 211
212 return tf;
213}
214
6cd62ccf 215
216/*****************************************************************************
963b5f2d 217 *Open control and per cpu tracefiles
6cd62ccf 218 ****************************************************************************/
219
963b5f2d 220void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name)
6cd62ccf 221{
963b5f2d 222 LttTracefile * tf;
223 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 224 if(!tf) return;
963b5f2d 225 t->per_cpu_tracefile_number++;
226 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 227}
228
2a74fbf4 229gint ltt_tracefile_open_control(LttTrace *t, char * control_name)
6cd62ccf 230{
963b5f2d 231 LttTracefile * tf;
c02ea99f 232 LttEvent ev;
963b5f2d 233 LttFacility * f;
963b5f2d 234 void * pos;
235 FacilityLoad fLoad;
8d1e6362 236 unsigned int i;
963b5f2d 237
238 tf = ltt_tracefile_open(t,control_name);
2a74fbf4 239 if(!tf) {
240 g_warning("ltt_tracefile_open_control : bad file descriptor");
241 return -1;
242 }
963b5f2d 243 t->control_tracefile_number++;
244 g_ptr_array_add(t->control_tracefiles,tf);
245
246 //parse facilities tracefile to get base_id
542ceddd 247 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 248 while(1){
c02ea99f 249 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
40331ba8 250
c02ea99f 251 if(ev.event_id == TRACE_FACILITY_LOAD){
252 pos = ev.data;
963b5f2d 253 fLoad.name = (char*)pos;
47a166fc 254 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
cbd41522 255 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 256
257 for(i=0;i<t->facility_number;i++){
258 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
259 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
260 f->base_id = fLoad.base_code;
261 break;
262 }
263 }
2a74fbf4 264 if(i==t->facility_number) {
8d1e6362 265 g_warning("Facility: %s, checksum: %u is not found",
266 fLoad.name,(unsigned int)fLoad.checksum);
2a74fbf4 267 return -1;
268 }
c02ea99f 269 }else if(ev.event_id == TRACE_BLOCK_START){
40331ba8 270 continue;
c02ea99f 271 }else if(ev.event_id == TRACE_BLOCK_END){
40331ba8 272 break;
2a74fbf4 273 }else {
8d1e6362 274 g_warning("Not valid facilities trace file");
2a74fbf4 275 return -1;
276 }
963b5f2d 277 }
278 }
2a74fbf4 279 return 0;
6cd62ccf 280}
281
282/*****************************************************************************
283 *Function name
963b5f2d 284 * ltt_tracefile_close: close a trace file,
6cd62ccf 285 *Input params
963b5f2d 286 * t : tracefile which will be closed
6cd62ccf 287 ****************************************************************************/
288
963b5f2d 289void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 290{
963b5f2d 291 g_free(t->name);
292 g_free(t->buffer);
663bd9ff 293 close(t->fd);
963b5f2d 294 g_free(t);
295}
6cd62ccf 296
6cd62ccf 297
963b5f2d 298/*****************************************************************************
299 *Get system information
300 ****************************************************************************/
2a74fbf4 301gint getSystemInfo(LttSystemDescription* des, char * pathname)
963b5f2d 302{
303 FILE * fp;
963b5f2d 304 char buf[DIR_NAME_SIZE];
43da6a59 305
306 GMarkupParseContext * context;
2a74fbf4 307 GError * error = NULL;
43da6a59 308 GMarkupParser markup_parser =
309 {
310 parser_start_element,
8d1e6362 311 NULL,
43da6a59 312 parser_characters,
313 NULL, /* passthrough */
314 NULL /* error */
315 };
963b5f2d 316
317 fp = fopen(pathname,"r");
318 if(!fp){
2a74fbf4 319 g_warning("Can not open file : %s\n", pathname);
320 return -1;
6cd62ccf 321 }
963b5f2d 322
43da6a59 323 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
6cd62ccf 324
43da6a59 325 while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
326 if(!g_markup_parse_context_parse(context, buf, DIR_NAME_SIZE, &error)){
2a74fbf4 327 if(error != NULL) {
328 g_warning("Can not parse xml file: \n%s\n", error->message);
329 g_error_free(error);
330 }
331 g_markup_parse_context_free(context);
332 fclose(fp);
333 return -1;
43da6a59 334 }
963b5f2d 335 }
2a74fbf4 336 g_markup_parse_context_free(context);
963b5f2d 337 fclose(fp);
2a74fbf4 338 return 0;
6cd62ccf 339}
340
341/*****************************************************************************
963b5f2d 342 *The following functions get facility/tracefile information
6cd62ccf 343 ****************************************************************************/
344
2a74fbf4 345gint getFacilityInfo(LttTrace *t, char* eventdefs)
6cd62ccf 346{
963b5f2d 347 DIR * dir;
348 struct dirent *entry;
349 char * ptr;
8d1e6362 350 unsigned int i,j;
963b5f2d 351 LttFacility * f;
352 LttEventType * et;
e8bb1a73 353 char name[DIR_NAME_SIZE];
963b5f2d 354
355 dir = opendir(eventdefs);
2a74fbf4 356 if(!dir) {
357 g_warning("Can not open directory: %s\n", eventdefs);
358 return -1;
359 }
963b5f2d 360
361 while((entry = readdir(dir)) != NULL){
362 ptr = &entry->d_name[strlen(entry->d_name)-4];
363 if(strcmp(ptr,".xml") != 0) continue;
e8bb1a73 364 strcpy(name,eventdefs);
365 strcat(name,entry->d_name);
366 ltt_facility_open(t,name);
963b5f2d 367 }
368 closedir(dir);
369
963b5f2d 370 for(j=0;j<t->facility_number;j++){
8710c6c7 371 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 372 for(i=0; i<f->event_number; i++){
373 et = f->events[i];
40331ba8 374 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 375 }
376 }
2a74fbf4 377 return 0;
963b5f2d 378}
6cd62ccf 379
2a74fbf4 380gint getControlFileInfo(LttTrace *t, char* control)
6cd62ccf 381{
963b5f2d 382 DIR * dir;
383 struct dirent *entry;
e8bb1a73 384 char name[DIR_NAME_SIZE];
963b5f2d 385
386 dir = opendir(control);
2a74fbf4 387 if(!dir) {
388 g_warning("Can not open directory: %s\n", control);
389 return -1;
390 }
963b5f2d 391
392 while((entry = readdir(dir)) != NULL){
8710c6c7 393 if(strcmp(entry->d_name,"facilities") != 0 &&
394 strcmp(entry->d_name,"interrupts") != 0 &&
963b5f2d 395 strcmp(entry->d_name,"processes") != 0) continue;
396
e8bb1a73 397 strcpy(name,control);
398 strcat(name,entry->d_name);
2a74fbf4 399 if(ltt_tracefile_open_control(t,name))
400 return -1;
963b5f2d 401 }
402 closedir(dir);
2a74fbf4 403 return 0;
6cd62ccf 404}
405
2a74fbf4 406gint getCpuFileInfo(LttTrace *t, char* cpu)
6cd62ccf 407{
963b5f2d 408 DIR * dir;
409 struct dirent *entry;
e8bb1a73 410 char name[DIR_NAME_SIZE];
963b5f2d 411
412 dir = opendir(cpu);
2a74fbf4 413 if(!dir) {
414 g_warning("Can not open directory: %s\n", cpu);
415 return -1;
416 }
963b5f2d 417
418 while((entry = readdir(dir)) != NULL){
8710c6c7 419 if(strcmp(entry->d_name,".") != 0 &&
a614481a 420 strcmp(entry->d_name,"..") != 0 &&
421 strcmp(entry->d_name,".svn") != 0){
e8bb1a73 422 strcpy(name,cpu);
423 strcat(name,entry->d_name);
424 ltt_tracefile_open_cpu(t,name);
963b5f2d 425 }else continue;
426 }
427 closedir(dir);
2a74fbf4 428 return 0;
6cd62ccf 429}
430
963b5f2d 431/*****************************************************************************
432 *A trace is specified as a pathname to the directory containing all the
433 *associated data (control tracefiles, per cpu tracefiles, event
434 *descriptions...).
435 *
436 *When a trace is closed, all the associated facilities, types and fields
437 *are released as well.
8d1e6362 438 */
439
440
441/****************************************************************************
442 * get_absolute_pathname
803229fa 443 *
8d1e6362 444 * return the unique pathname in the system
445 *
ef35d837 446 * MD : Fixed this function so it uses realpath, dealing well with
447 * forgotten cases (.. were not used correctly before).
803229fa 448 *
963b5f2d 449 ****************************************************************************/
9f797243 450void get_absolute_pathname(const char *pathname, char * abs_pathname)
451{
9f797243 452 abs_pathname[0] = '\0';
803229fa 453
ef35d837 454 if ( realpath (pathname, abs_pathname) != NULL)
455 return;
456 else
457 {
8d1e6362 458 /* error, return the original path unmodified */
ef35d837 459 strcpy(abs_pathname, pathname);
9f797243 460 return;
461 }
ef35d837 462 return;
9f797243 463}
464
f7afe191 465LttTrace *ltt_trace_open(const char *pathname)
6cd62ccf 466{
963b5f2d 467 LttTrace * t;
468 LttSystemDescription * sys_description;
469 char eventdefs[DIR_NAME_SIZE];
470 char info[DIR_NAME_SIZE];
471 char control[DIR_NAME_SIZE];
472 char cpu[DIR_NAME_SIZE];
473 char tmp[DIR_NAME_SIZE];
9f797243 474 char abs_path[DIR_NAME_SIZE];
963b5f2d 475 gboolean has_slash = FALSE;
476
9f797243 477 get_absolute_pathname(pathname, abs_path);
963b5f2d 478 //establish the pathname to different directories
9f797243 479 if(abs_path[strlen(abs_path)-1] == '/')has_slash = TRUE;
480 strcpy(eventdefs,abs_path);
963b5f2d 481 if(!has_slash)strcat(eventdefs,"/");
482 strcat(eventdefs,"eventdefs/");
483
9f797243 484 strcpy(info,abs_path);
963b5f2d 485 if(!has_slash)strcat(info,"/");
486 strcat(info,"info/");
487
9f797243 488 strcpy(control,abs_path);
963b5f2d 489 if(!has_slash)strcat(control,"/");
490 strcat(control,"control/");
491
9f797243 492 strcpy(cpu,abs_path);
963b5f2d 493 if(!has_slash)strcat(cpu,"/");
494 strcat(cpu,"cpu/");
495
496 //new trace
963b5f2d 497 sys_description = g_new(LttSystemDescription, 1);
2a74fbf4 498 t = g_new(LttTrace, 1);
9f797243 499 t->pathname = g_strdup(abs_path);
963b5f2d 500 t->facility_number = 0;
501 t->control_tracefile_number = 0;
502 t->per_cpu_tracefile_number = 0;
503 t->system_description = sys_description;
504 t->control_tracefiles = g_ptr_array_new();
505 t->per_cpu_tracefiles = g_ptr_array_new();
506 t->facilities = g_ptr_array_new();
507 getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
508
509 //get system description
510 strcpy(tmp,info);
511 strcat(tmp,"system.xml");
2a74fbf4 512 if(getSystemInfo(sys_description, tmp)) {
513 g_ptr_array_free(t->facilities, TRUE);
514 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
515 g_ptr_array_free(t->control_tracefiles, TRUE);
516 g_free(sys_description);
517 g_free(t->pathname);
518 g_free(t);
519 return NULL;
520 }
521
522
963b5f2d 523
b333a76b 524 //get facilities info
2a74fbf4 525 if(getFacilityInfo(t,eventdefs)) {
526 g_ptr_array_free(t->facilities, TRUE);
527 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
528 g_ptr_array_free(t->control_tracefiles, TRUE);
529 g_free(sys_description);
530 g_free(t->pathname);
531 g_free(t);
532 return NULL;
533 }
b333a76b 534
963b5f2d 535 //get control tracefile info
a00149f6 536 getControlFileInfo(t,control);
537 /*
2a74fbf4 538 if(getControlFileInfo(t,control)) {
539 g_ptr_array_free(t->facilities, TRUE);
540 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
541 g_ptr_array_free(t->control_tracefiles, TRUE);
542 g_free(sys_description);
543 g_free(t->pathname);
544 g_free(t);
545 return NULL;
a00149f6 546 }*/ // With fatal error
963b5f2d 547
548 //get cpu tracefile info
2a74fbf4 549 if(getCpuFileInfo(t,cpu)) {
550 g_ptr_array_free(t->facilities, TRUE);
551 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
552 g_ptr_array_free(t->control_tracefiles, TRUE);
553 g_free(sys_description);
554 g_free(t->pathname);
555 g_free(t);
556 return NULL;
557 }
963b5f2d 558
963b5f2d 559 return t;
6cd62ccf 560}
561
49bf71b5 562char * ltt_trace_name(LttTrace *t)
563{
564 return t->pathname;
565}
566
567
f7afe191 568/******************************************************************************
569 * When we copy a trace, we want all the opening actions to happen again :
570 * the trace will be reopened and totally independant from the original.
571 * That's why we call ltt_trace_open.
572 *****************************************************************************/
573LttTrace *ltt_trace_copy(LttTrace *self)
574{
575 return ltt_trace_open(self->pathname);
576}
577
963b5f2d 578void ltt_trace_close(LttTrace *t)
6cd62ccf 579{
8d1e6362 580 unsigned int i;
963b5f2d 581 LttTracefile * tf;
582 LttFacility * f;
583
584 g_free(t->pathname);
585
586 //free system_description
587 g_free(t->system_description->description);
588 g_free(t->system_description->node_name);
589 g_free(t->system_description->domain_name);
590 g_free(t->system_description->kernel_name);
591 g_free(t->system_description->kernel_release);
592 g_free(t->system_description->kernel_version);
593 g_free(t->system_description->machine);
594 g_free(t->system_description->processor);
595 g_free(t->system_description->hardware_platform);
596 g_free(t->system_description->operating_system);
597 g_free(t->system_description);
598
599 //free control_tracefiles
600 for(i=0;i<t->control_tracefile_number;i++){
601 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
602 ltt_tracefile_close(tf);
603 }
09ad4797 604 g_ptr_array_free(t->control_tracefiles, TRUE);
963b5f2d 605
606 //free per_cpu_tracefiles
607 for(i=0;i<t->per_cpu_tracefile_number;i++){
608 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
609 ltt_tracefile_close(tf);
610 }
09ad4797 611 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
963b5f2d 612
613 //free facilities
614 for(i=0;i<t->facility_number;i++){
615 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
616 ltt_facility_close(f);
617 }
09ad4797 618 g_ptr_array_free(t->facilities, TRUE);
963b5f2d 619
620 g_free(t);
09ad4797 621
622 g_blow_chunks();
6cd62ccf 623}
624
963b5f2d 625
6cd62ccf 626/*****************************************************************************
963b5f2d 627 *Get the system description of the trace
6cd62ccf 628 ****************************************************************************/
629
963b5f2d 630LttSystemDescription *ltt_trace_system_description(LttTrace *t)
6cd62ccf 631{
963b5f2d 632 return t->system_description;
6cd62ccf 633}
634
635/*****************************************************************************
963b5f2d 636 * The following functions discover the facilities of the trace
6cd62ccf 637 ****************************************************************************/
638
963b5f2d 639unsigned ltt_trace_facility_number(LttTrace *t)
640{
641 return (unsigned)(t->facility_number);
642}
643
644LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
6cd62ccf 645{
963b5f2d 646 return (LttFacility*)g_ptr_array_index(t->facilities, i);
6cd62ccf 647}
648
649/*****************************************************************************
650 *Function name
963b5f2d 651 * ltt_trace_facility_find : find facilities in the trace
6cd62ccf 652 *Input params
963b5f2d 653 * t : the trace
654 * name : facility name
655 *Output params
656 * position : position of the facility in the trace
6cd62ccf 657 *Return value
963b5f2d 658 * : the number of facilities
6cd62ccf 659 ****************************************************************************/
660
963b5f2d 661unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
6cd62ccf 662{
8d1e6362 663 unsigned int i, count=0;
963b5f2d 664 LttFacility * f;
8a3005f3 665 for(i=0;i<t->facility_number;i++){
963b5f2d 666 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
667 if(strcmp(f->name,name)==0){
668 count++;
669 if(count==1) *position = i;
670 }else{
671 if(count) break;
672 }
673 }
674 return count;
6cd62ccf 675}
676
677/*****************************************************************************
963b5f2d 678 * Functions to discover all the event types in the trace
6cd62ccf 679 ****************************************************************************/
680
963b5f2d 681unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 682{
8d1e6362 683 unsigned int i;
963b5f2d 684 unsigned count = 0;
dc1cad90 685 unsigned int num = t->facility_number;
963b5f2d 686 LttFacility * f;
dc1cad90 687
688 for(i=0;i<num;i++){
963b5f2d 689 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
690 count += f->event_number;
691 }
692 return count;
6cd62ccf 693}
694
155a7b0a 695/* FIXME : performances could be improved with a better design for this
6e8c19d4 696 * function : sequential search through a container has never been the
697 * best on the critical path. */
963b5f2d 698LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
6cd62ccf 699{
cf74a6f1 700 LttFacility * facility = NULL;
8d1e6362 701 unsigned int i;
dc1cad90 702 unsigned int num = trace->facility_number;
703 GPtrArray *facilities = trace->facilities;
cf74a6f1 704
dc1cad90 705 for(i=0;unlikely(i<num);){
cf74a6f1 706 LttFacility *iter_facility =
dc1cad90 707 (LttFacility*) g_ptr_array_index(facilities,i);
708 unsigned base_id = iter_facility->base_id;
709
710 if(likely(id >= base_id &&
711 id < base_id + iter_facility->event_number)) {
cf74a6f1 712 facility = iter_facility;
963b5f2d 713 break;
dc1cad90 714 } else {
715 i++;
cf74a6f1 716 }
963b5f2d 717 }
dc1cad90 718
cf74a6f1 719 return facility;
6cd62ccf 720}
721
963b5f2d 722LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 723{
6e8c19d4 724 LttEventType *event_type;
725
963b5f2d 726 LttFacility * f;
727 f = ltt_trace_facility_by_id(t,evId);
6e8c19d4 728
729 if(unlikely(!f)) event_type = NULL;
730 else event_type = f->events[evId - f->base_id];
731
732 return event_type;
6cd62ccf 733}
734
735/*****************************************************************************
963b5f2d 736 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
737 *the maximum number of CPU in the system. When the number of CPU installed
738 *is less than the maximum, some positions are unused. There are also a
739 *number of "control" tracefiles (facilities, interrupts...).
6cd62ccf 740 ****************************************************************************/
963b5f2d 741unsigned ltt_trace_control_tracefile_number(LttTrace *t)
6cd62ccf 742{
963b5f2d 743 return t->control_tracefile_number;
6cd62ccf 744}
745
963b5f2d 746unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
6cd62ccf 747{
963b5f2d 748 return t->per_cpu_tracefile_number;
6cd62ccf 749}
750
751/*****************************************************************************
963b5f2d 752 *It is possible to search for the tracefiles by name or by CPU position.
753 *The index within the tracefiles of the same type is returned if found
754 *and a negative value otherwise.
6cd62ccf 755 ****************************************************************************/
756
8d1e6362 757int ltt_trace_control_tracefile_find(LttTrace *t, const gchar *name)
6cd62ccf 758{
963b5f2d 759 LttTracefile * tracefile;
8d1e6362 760 unsigned int i;
963b5f2d 761 for(i=0;i<t->control_tracefile_number;i++){
762 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
763 if(strcmp(tracefile->name, name)==0)break;
764 }
765 if(i == t->control_tracefile_number) return -1;
766 return i;
6cd62ccf 767}
768
8d1e6362 769/* not really useful. We just have to know that cpu tracefiles
770 * comes before control tracefiles.
771 */
772int ltt_trace_per_cpu_tracefile_find(LttTrace *t, const gchar *name)
6cd62ccf 773{
963b5f2d 774 LttTracefile * tracefile;
8d1e6362 775 unsigned int i;
776 for(i=0;i<t->per_cpu_tracefile_number;i++){
777 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
778 if(strcmp(tracefile->name, name)==0)break;
963b5f2d 779 }
8d1e6362 780 if(i == t->per_cpu_tracefile_number) return -1;
781 return i;
6cd62ccf 782}
783
784/*****************************************************************************
963b5f2d 785 *Get a specific tracefile
6cd62ccf 786 ****************************************************************************/
787
963b5f2d 788LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
6cd62ccf 789{
5598cfe3 790 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
963b5f2d 791}
792
793LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
794{
795 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
6cd62ccf 796}
797
487ad181 798/*****************************************************************************
799 * Get the start time and end time of the trace
800 ****************************************************************************/
801
802void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
803{
804 LttTime startSmall, startTmp, endBig, endTmp;
8d1e6362 805 unsigned int i, j=0;
487ad181 806 LttTracefile * tf;
807
808 for(i=0;i<t->control_tracefile_number;i++){
809 tf = g_ptr_array_index(t->control_tracefiles, i);
810 readBlock(tf,1);
811 startTmp = tf->a_block_start->time;
812 readBlock(tf,tf->block_number);
813 endTmp = tf->a_block_end->time;
814 if(i==0){
815 startSmall = startTmp;
816 endBig = endTmp;
817 j = 1;
818 continue;
819 }
308711e5 820 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
821 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
487ad181 822 }
823
824 for(i=0;i<t->per_cpu_tracefile_number;i++){
825 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
826 readBlock(tf,1);
827 startTmp = tf->a_block_start->time;
828 readBlock(tf,tf->block_number);
829 endTmp = tf->a_block_end->time;
830 if(j == 0 && i==0){
831 startSmall = startTmp;
832 endBig = endTmp;
833 continue;
834 }
308711e5 835 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
836 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
487ad181 837 }
838
c02ea99f 839 if(start != NULL) *start = startSmall;
840 if(end != NULL) *end = endBig;
487ad181 841}
842
843
6cd62ccf 844/*****************************************************************************
963b5f2d 845 *Get the name of a tracefile
6cd62ccf 846 ****************************************************************************/
847
963b5f2d 848char *ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 849{
963b5f2d 850 return tf->name;
6cd62ccf 851}
852
80da81ad 853/*****************************************************************************
854 * Get the number of blocks in the tracefile
855 ****************************************************************************/
856
857unsigned ltt_tracefile_block_number(LttTracefile *tf)
858{
859 return tf->block_number;
860}
861
6cd62ccf 862/*****************************************************************************
863 *Function name
864 * ltt_tracefile_seek_time: seek to the first event of the trace with time
865 * larger or equal to time
866 *Input params
867 * t : tracefile
868 * time : criteria of the time
6cd62ccf 869 ****************************************************************************/
caf7a67a 870void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time,
871 int start_block, int end_block)
872{
873 int err, tmp_block, s, e;
874 int headTime;
875 int tailTime;
876
877 err=readBlock(t,start_block);
878 if(err) g_error("Can not read tracefile: %s\n", t->name);
879 if(start_block == end_block)return;
880
881 tailTime = ltt_time_compare(t->a_block_end->time, time);
882 if(tailTime >= 0) return;
883
884 err=readBlock(t,end_block);
885 if(err) g_error("Can not read tracefile: %s\n", t->name);
886 if(start_block+1 == end_block)return;
887
888 headTime = ltt_time_compare(t->a_block_start->time, time);
889 if(headTime <= 0 ) return;
890
891 tmp_block = (end_block + start_block)/2;
892 err=readBlock(t,tmp_block);
893 if(err) g_error("Can not read tracefile: %s\n", t->name);
894
895 headTime = ltt_time_compare(t->a_block_start->time, time);
896 tailTime = ltt_time_compare(t->a_block_end->time, time);
897 if(headTime <= 0 && tailTime >= 0) return;
898
899 if(headTime > 0){
900 s = start_block + 1;
901 e = tmp_block - 1;
902 if(s <= e)
903 ltt_tracefile_find_time_block(t, time, s, e);
904 else return;
905 }
906
907 if(tailTime < 0){
908 s = tmp_block + 1;
909 e = end_block - 1;
910 if(s <= e)
911 ltt_tracefile_find_time_block(t, time, s, e);
912 else return;
913 }
914}
915
916void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
917{
918 int t_time, h_time, err;
919 err=readBlock(t,t->which_block-1);
920 if(err) g_error("Can not read tracefile: %s\n", t->name);
921 h_time = ltt_time_compare(t->a_block_start->time, time);
922 t_time = ltt_time_compare(t->a_block_end->time, time);
923 if(h_time == 0){
924 int tmp;
925 if(t->which_block == 1) return;
926 err=readBlock(t,t->which_block-1);
927 if(err) g_error("Can not read tracefile: %s\n", t->name);
928 tmp = ltt_time_compare(t->a_block_end->time, time);
929 if(tmp == 0) return ltt_tracefile_seek_time(t, time);
930 err=readBlock(t,t->which_block+1);
931 if(err) g_error("Can not read tracefile: %s\n", t->name);
932 }else if(h_time > 0){
933 ltt_tracefile_find_time_block(t, time, 1, t->which_block);
934 return ltt_tracefile_seek_time(t, time) ;
935 }else{
936 if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
937 err=readBlock(t,t->which_block+1);
938 if(err) g_error("Can not read tracefile: %s\n", t->name);
939 }
940}
6cd62ccf 941
963b5f2d 942void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
6cd62ccf 943{
944 int err;
963b5f2d 945 LttTime lttTime;
308711e5 946 int headTime = ltt_time_compare(t->a_block_start->time, time);
947 int tailTime = ltt_time_compare(t->a_block_end->time, time);
c02ea99f 948 LttEvent ev;
62e55dd6 949
6cd62ccf 950 if(headTime < 0 && tailTime > 0){
308711e5 951 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
db55eaae 952 lttTime = getEventTime(t);
308711e5 953 err = ltt_time_compare(lttTime, time);
db55eaae 954 if(err > 0){
8d1e6362 955 if(t->which_event==2 || ltt_time_compare(t->prev_event_time,time)<0){
1a3b8cbd 956 return;
db55eaae 957 }else{
958 updateTracefile(t);
959 return ltt_tracefile_seek_time(t, time);
1a3b8cbd 960 }
db55eaae 961 }else if(err < 0){
962 while(1){
c02ea99f 963 if(ltt_tracefile_read(t,&ev) == NULL) {
db55eaae 964 g_print("End of file\n");
965 return;
966 }
967 lttTime = getEventTime(t);
308711e5 968 err = ltt_time_compare(lttTime, time);
db55eaae 969 if(err >= 0)return;
970 }
971 }else return;
972 }else{//we are at the end of the block
973 updateTracefile(t);
974 return ltt_tracefile_seek_time(t, time);
975 }
e37c1372 976 }else if(headTime >= 0){
6cd62ccf 977 if(t->which_block == 1){
978 updateTracefile(t);
979 }else{
1ee6a9af 980 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
981 (t->prev_block_end_time.tv_sec == 0 &&
982 t->prev_block_end_time.tv_nsec == 0 )){
caf7a67a 983 ltt_tracefile_backward_find_time_block(t, time);
6cd62ccf 984 }else{
985 updateTracefile(t);
986 }
987 }
40331ba8 988 }else if(tailTime < 0){
6cd62ccf 989 if(t->which_block != t->block_number){
caf7a67a 990 ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
991 return ltt_tracefile_seek_time(t, time);
963b5f2d 992 }else {
a8c0f09d 993 t->cur_event_pos = t->buffer + t->block_size;
994 g_print("End of file\n");
963b5f2d 995 return;
996 }
40331ba8 997 }else if(tailTime == 0){
e37c1372 998 t->cur_event_pos = t->last_event_pos;
62e55dd6 999 t->current_event_time = time;
1000 t->cur_heart_beat_number = 0;
1001 t->prev_event_time.tv_sec = 0;
1002 t->prev_event_time.tv_nsec = 0;
40331ba8 1003 return;
6cd62ccf 1004 }
6cd62ccf 1005}
1006
80da81ad 1007/*****************************************************************************
1008 * Seek to the first event with position equal or larger to ep
18206708 1009 *
1010 * Modified by Mathieu Desnoyers to used faster offset position instead of
1011 * re-reading the whole buffer.
80da81ad 1012 ****************************************************************************/
1013
04b44e05 1014void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
80da81ad 1015{
1016 //if we are at the right place, just return
155a7b0a 1017 if(likely(t->which_block == ep->block_num && t->which_event == ep->event_num))
80da81ad 1018 return;
1019
155a7b0a 1020 if(likely(t->which_block == ep->block_num)) updateTracefile(t);
80da81ad 1021 else readBlock(t,ep->block_num);
18206708 1022 //event offset is available
155a7b0a 1023 if(likely(ep->old_position)){
18206708 1024 int err;
1025
1026 t->which_event = ep->event_num;
80da81ad 1027 t->cur_event_pos = t->buffer + ep->event_offset;
18206708 1028 t->prev_event_time = ep->event_time;
1029 t->current_event_time = ep->event_time;
1030 t->cur_heart_beat_number = ep->heart_beat_number;
1031 t->cur_cycle_count = ep->event_cycle_count;
1032
1033 /* This is a workaround for fast position seek */
1034 t->last_event_pos = ep->last_event_pos;
1035 t->prev_block_end_time = ep->prev_block_end_time;
1036 t->prev_event_time = ep->prev_event_time;
1037 t->pre_cycle_count = ep->pre_cycle_count;
1038 t->count = ep->count;
2dee981d 1039 t->overflow_nsec = ep->overflow_nsec;
dfb73233 1040 t->last_heartbeat = ep->last_heartbeat;
18206708 1041 /* end of workaround */
1042
1043 //update the fields of the current event and go to the next event
1044 err = skipEvent(t);
155a7b0a 1045 if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
18206708 1046
80da81ad 1047 return;
1048 }
1049
18206708 1050 //only block number and event index are available
1051 //MD: warning : this is slow!
1052 g_warning("using slow O(n) tracefile seek position");
1053
c02ea99f 1054 LttEvent event;
155a7b0a 1055 while(likely(t->which_event < ep->event_num)) ltt_tracefile_read(t, &event);
80da81ad 1056
1057 return;
1058}
1059
6cd62ccf 1060/*****************************************************************************
1061 *Function name
40331ba8 1062 * ltt_tracefile_read : read the current event, set the pointer to the next
6cd62ccf 1063 *Input params
1064 * t : tracefile
1065 *Return value
963b5f2d 1066 * LttEvent * : an event to be processed
6cd62ccf 1067 ****************************************************************************/
1068
c02ea99f 1069LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
6cd62ccf 1070{
963b5f2d 1071 int err;
6cd62ccf 1072
155a7b0a 1073 if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
1074 if(unlikely(t->which_block == t->block_number)){
bdc36259 1075 return NULL;
1076 }
1077 err = readBlock(t, t->which_block + 1);
155a7b0a 1078 if(unlikely(err))g_error("Can not read tracefile");
bdc36259 1079 }
1080
c02ea99f 1081 event->event_id = (int)(*(guint16 *)(t->cur_event_pos));
155a7b0a 1082 if(unlikely(event->event_id == TRACE_TIME_HEARTBEAT))
963b5f2d 1083 t->cur_heart_beat_number++;
6cd62ccf 1084
40331ba8 1085 t->prev_event_time = t->current_event_time;
62e55dd6 1086 // t->current_event_time = getEventTime(t);
6cd62ccf 1087
c02ea99f 1088 event->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1089 event->event_time = t->current_event_time;
1090 event->event_cycle_count = t->cur_cycle_count;
963b5f2d 1091
c02ea99f 1092 event->tracefile = t;
1093 event->data = t->cur_event_pos + EVENT_HEADER_SIZE;
1094 event->which_block = t->which_block;
1095 event->which_event = t->which_event;
6cd62ccf 1096
18206708 1097 /* This is a workaround for fast position seek */
c02ea99f 1098 event->last_event_pos = t->last_event_pos;
1099 event->prev_block_end_time = t->prev_block_end_time;
1100 event->prev_event_time = t->prev_event_time;
1101 event->pre_cycle_count = t->pre_cycle_count;
1102 event->count = t->count;
2dee981d 1103 event->overflow_nsec = t->overflow_nsec;
dfb73233 1104 event->last_heartbeat = t->last_heartbeat;
2dee981d 1105
18206708 1106 /* end of workaround */
1107
1108
1109
40331ba8 1110 //update the fields of the current event and go to the next event
1111 err = skipEvent(t);
155a7b0a 1112 if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
40331ba8 1113
c02ea99f 1114 return event;
6cd62ccf 1115}
1116
1117/****************************************************************************
1118 *Function name
1119 * readFile : wrap function to read from a file
1120 *Input Params
1121 * fd : file descriptor
1122 * buf : buf to contain the content
1123 * size : number of bytes to be read
1124 * mesg : message to be printed if some thing goes wrong
1125 *return value
1126 * 0 : success
1127 * EIO : can not read from the file
1128 ****************************************************************************/
1129
1130int readFile(int fd, void * buf, size_t size, char * mesg)
1131{
8d1e6362 1132 ssize_t nbBytes = read(fd, buf, size);
1133
1134 if((size_t)nbBytes != size) {
1135 if(nbBytes < 0) {
1136 perror("Error in readFile : ");
1137 } else {
1138 g_warning("%s",mesg);
1139 }
6cd62ccf 1140 return EIO;
1141 }
1142 return 0;
1143}
1144
507915ee 1145
6cd62ccf 1146/****************************************************************************
1147 *Function name
1148 * readBlock : read a block from the file
1149 *Input Params
1150 * lttdes : ltt trace file
1151 * whichBlock : the block which will be read
1152 *return value
1153 * 0 : success
1154 * EINVAL : lseek fail
1155 * EIO : can not read from the file
1156 ****************************************************************************/
1157
963b5f2d 1158int readBlock(LttTracefile * tf, int whichBlock)
6cd62ccf 1159{
1160 off_t nbBytes;
cbd41522 1161 guint32 lostSize;
6cd62ccf 1162
ac849774 1163 /* same block already opened requested */
1164 if((guint)whichBlock == tf->which_block) return 0;
1165
155a7b0a 1166 if(likely(whichBlock - tf->which_block == 1 && tf->which_block != 0)){
963b5f2d 1167 tf->prev_block_end_time = tf->a_block_end->time;
40331ba8 1168 tf->prev_event_time = tf->a_block_end->time;
6cd62ccf 1169 }else{
1170 tf->prev_block_end_time.tv_sec = 0;
1171 tf->prev_block_end_time.tv_nsec = 0;
40331ba8 1172 tf->prev_event_time.tv_sec = 0;
1173 tf->prev_event_time.tv_nsec = 0;
6cd62ccf 1174 }
6cd62ccf 1175
963b5f2d 1176 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
155a7b0a 1177 if(unlikely(nbBytes == -1)) return EINVAL;
6cd62ccf 1178
155a7b0a 1179 if(unlikely(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")))
963b5f2d 1180 return EIO;
6cd62ccf 1181
963b5f2d 1182 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
cbd41522 1183 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
63c35f6c 1184 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size
1185 - sizeof(guint32) - lostSize - sizeof(BlockEnd));
1186 tf->last_event_pos = tf->buffer + tf->block_size -
1187 sizeof(guint32) - lostSize
1188 - sizeof(BlockEnd) - EVENT_HEADER_SIZE;
6cd62ccf 1189
6cd62ccf 1190 tf->which_block = whichBlock;
963b5f2d 1191 tf->which_event = 1;
40331ba8 1192 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
6cd62ccf 1193 tf->cur_heart_beat_number = 0;
dfb73233 1194 tf->last_heartbeat = NULL;
507915ee 1195
1196 /* read the whole block to precalculate total of cycles in it */
1197 tf->count = 0;
1198 tf->pre_cycle_count = 0;
ac849774 1199 tf->cur_cycle_count = 0;
39ae3eed 1200
6cd62ccf 1201 getCyclePerNsec(tf);
2dee981d 1202
21ff84a0 1203 tf->overflow_nsec =
42db9bf1 1204 (-((double)(tf->a_block_start->cycle_count&0xFFFFFFFF))
21ff84a0 1205 * tf->nsec_per_cycle);
6cd62ccf 1206
62e55dd6 1207 tf->current_event_time = getEventTime(tf);
40331ba8 1208
6cd62ccf 1209 return 0;
1210}
1211
1212/*****************************************************************************
1213 *Function name
1214 * updateTracefile : reinitialize the info of the block which is already
1215 * in the buffer
1216 *Input params
1217 * tf : tracefile
1218 ****************************************************************************/
1219
963b5f2d 1220void updateTracefile(LttTracefile * tf)
6cd62ccf 1221{
963b5f2d 1222 tf->which_event = 1;
40331ba8 1223 tf->cur_event_pos = tf->buffer;
62e55dd6 1224 tf->current_event_time = getEventTime(tf);
6cd62ccf 1225 tf->cur_heart_beat_number = 0;
1226
1227 tf->prev_event_time.tv_sec = 0;
1228 tf->prev_event_time.tv_nsec = 0;
2dee981d 1229 tf->count = 0;
1230
21ff84a0 1231 tf->overflow_nsec = (-((double)tf->a_block_start->cycle_count)
1232 * tf->nsec_per_cycle);
2dee981d 1233
6cd62ccf 1234}
1235
1236/*****************************************************************************
1237 *Function name
1238 * skipEvent : go to the next event, update the fields of the current event
1239 *Input params
1240 * t : tracefile
1241 *return value
1242 * 0 : success
6cd62ccf 1243 * ERANGE : event id is out of range
1244 ****************************************************************************/
1245
963b5f2d 1246int skipEvent(LttTracefile * t)
6cd62ccf 1247{
8d1e6362 1248 int evId;
6cd62ccf 1249 void * evData;
963b5f2d 1250 LttEventType * evT;
1251 LttField * rootFld;
6cd62ccf 1252
cbd41522 1253 evId = (int)(*(guint16 *)(t->cur_event_pos));
6cd62ccf 1254 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
6cd62ccf 1255
908f42fa 1256 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
47a166fc 1257
1d1df11d 1258 if(likely(evT)) rootFld = evT->root_field;
908f42fa 1259 else return ERANGE;
6cd62ccf 1260
1d1df11d 1261 if(likely(rootFld)){
908f42fa 1262 //event has string/sequence or the last event is not the same event
1d1df11d 1263 if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1264 && rootFld->field_fixed == 0)){
908f42fa 1265 setFieldsOffset(t, evT, evData, t->trace);
47a166fc 1266 }
908f42fa 1267 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1268 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1269
1270 evT->latest_block = t->which_block;
1271 evT->latest_event = t->which_event;
1272
6cd62ccf 1273 //the next event is in the next block
1d1df11d 1274 if(unlikely(evId == TRACE_BLOCK_END)){
bdc36259 1275 t->cur_event_pos = t->buffer + t->block_size;
6cd62ccf 1276 }else{
1277 t->which_event++;
62e55dd6 1278 t->current_event_time = getEventTime(t);
6cd62ccf 1279 }
1280
1281 return 0;
1282}
1283
18206708 1284
6cd62ccf 1285/*****************************************************************************
1286 *Function name
1287 * getCyclePerNsec : calculate cycles per nsec for current block
507915ee 1288 * MD: should have tracefile_read the whole block, so we know the
1289 * total of cycles in it before being called.
6cd62ccf 1290 *Input Params
1291 * t : tracefile
1292 ****************************************************************************/
1293
963b5f2d 1294void getCyclePerNsec(LttTracefile * t)
6cd62ccf 1295{
963b5f2d 1296 LttTime lBufTotalTime; /* Total time for this buffer */
887208b7 1297 double lBufTotalNSec; /* Total time for this buffer in nsecs */
8ee1c3d5 1298 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1299
1300 /* Calculate the total time for this buffer */
308711e5 1301 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
6cd62ccf 1302
1303 /* Calculate the total cycles for this bufffer */
e4eced0f 1304 lBufTotalCycle = t->a_block_end->cycle_count;
1305 lBufTotalCycle -= t->a_block_start->cycle_count;
6cd62ccf 1306
8ee1c3d5 1307 /* Convert the total time to double */
887208b7 1308 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
6cd62ccf 1309
887208b7 1310 t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle;
8ee1c3d5 1311
2dee981d 1312 /* Pre-multiply one overflow (2^32 cycles) by nsec_per_cycle */
21ff84a0 1313 t->one_overflow_nsec = t->nsec_per_cycle * (double)0x100000000ULL;
2dee981d 1314
6cd62ccf 1315}
1316
1317/****************************************************************************
1318 *Function name
1319 * getEventTime : obtain the time of an event
887208b7 1320 * NOTE : this function _really_ is on critical path.
6cd62ccf 1321 *Input params
1322 * tf : tracefile
1323 *Return value
963b5f2d 1324 * LttTime : the time of the event
6cd62ccf 1325 ****************************************************************************/
1326
8959a0c8 1327static inline LttTime getEventTime(LttTracefile * tf)
6cd62ccf 1328{
963b5f2d 1329 LttTime time;
1330 LttCycleCount cycle_count; // cycle count for the current event
2dee981d 1331 //LttCycleCount lEventTotalCycle; // Total cycles from start for event
1332 gint64 lEventNSec; // Total nsecs from start for event
963b5f2d 1333 LttTime lTimeOffset; // Time offset in struct LttTime
cbd41522 1334 guint16 evId;
e4eced0f 1335
cbd41522 1336 evId = *(guint16 *)tf->cur_event_pos;
ac849774 1337
cbd41522 1338 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
e4eced0f 1339
dfb73233 1340 gboolean comp_count = cycle_count < tf->pre_cycle_count;
bbf28e50 1341
dfb73233 1342 tf->pre_cycle_count = cycle_count;
1343
1344 if(unlikely(comp_count)) {
bbf28e50 1345 /* Overflow */
dfb73233 1346 tf->overflow_nsec += tf->one_overflow_nsec;
1347 tf->count++; //increment overflow count
1348 }
1349
1350 if(unlikely(evId == TRACE_BLOCK_START)) {
1351 lEventNSec = 0;
1352 } else if(unlikely(evId == TRACE_BLOCK_END)) {
21ff84a0 1353 lEventNSec = ((double)
dfb73233 1354 (tf->a_block_end->cycle_count - tf->a_block_start->cycle_count)
21ff84a0 1355 * tf->nsec_per_cycle);
dfb73233 1356 }
dfb73233 1357#if 0
bbf28e50 1358 /* If you want to make heart beat a special case and use their own 64 bits
1359 * TSC, activate this.
1360 */
dfb73233 1361 else if(unlikely(evId == TRACE_TIME_HEARTBEAT)) {
1362
1363 tf->last_heartbeat = (TimeHeartbeat*)(tf->cur_event_pos+EVENT_HEADER_SIZE);
1364 lEventNSec = ((double)(tf->last_heartbeat->cycle_count
1365 - tf->a_block_start->cycle_count)
1366 * tf->nsec_per_cycle);
1367 }
1368#endif //0
1369 else {
21ff84a0 1370 lEventNSec = (gint64)((double)cycle_count * tf->nsec_per_cycle)
dfb73233 1371 +tf->overflow_nsec;
1372 }
1373
2dee981d 1374 lTimeOffset = ltt_time_from_uint64(lEventNSec);
1375
308711e5 1376 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
e4eced0f 1377
6cd62ccf 1378 return time;
1379}
1380
1381/*****************************************************************************
1382 *Function name
1383 * setFieldsOffset : set offset of the fields
1384 *Input params
1385 * tracefile : opened trace file
1386 * evT : the event type
1387 * evD : event data, it may be NULL
1388 ****************************************************************************/
1389
40331ba8 1390void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
6cd62ccf 1391{
963b5f2d 1392 LttField * rootFld = evT->root_field;
6cd62ccf 1393 // rootFld->base_address = evD;
1394
1d1df11d 1395 if(likely(rootFld))
8710c6c7 1396 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
6cd62ccf 1397}
1398
1399/*****************************************************************************
1400 *Function name
1401 * getFieldtypeSize: get the size of the field type (primitive type)
1402 *Input params
1403 * tracefile : opened trace file
1404 * evT : event type
1405 * offsetRoot : offset from the root
1406 * offsetParent : offset from the parrent
1407 * fld : field
1408 * evD : event data, it may be NULL
1409 *Return value
1410 * int : size of the field
1411 ****************************************************************************/
1412
21182d4a 1413static inline gint getFieldtypeSize(LttTracefile * t,
1414 LttEventType * evT, gint offsetRoot,
1415 gint offsetParent, LttField * fld, void *evD, LttTrace *trace)
6cd62ccf 1416{
21182d4a 1417 gint size, size1, element_number, i, offset1, offset2;
963b5f2d 1418 LttType * type = fld->field_type;
6cd62ccf 1419
21182d4a 1420 if(unlikely(t && evT->latest_block==t->which_block &&
1421 evT->latest_event==t->which_event)){
1422 size = fld->field_size;
1423 goto end_getFieldtypeSize;
a0d63196 1424 } else {
1425 /* This likely has been tested with gcov : half of them.. */
1426 if(unlikely(fld->field_fixed == 1)){
1427 /* tested : none */
1428 if(unlikely(fld == evT->root_field)) {
1429 size = fld->field_size;
1430 goto end_getFieldtypeSize;
1431 }
21182d4a 1432 }
6cd62ccf 1433
a0d63196 1434 /* From gcov profiling : half string, half struct, can we gain something
1435 * from that ? (Mathieu) */
1436 switch(type->type_class) {
1437 case LTT_ARRAY:
1438 element_number = (int) type->element_number;
1439 if(fld->field_fixed == -1){
1440 size = getFieldtypeSize(t, evT, offsetRoot,
1441 0,fld->child[0], NULL, trace);
1442 if(size == 0){ //has string or sequence
1443 fld->field_fixed = 0;
1444 }else{
1445 fld->field_fixed = 1;
1446 size *= element_number;
1447 }
1448 }else if(fld->field_fixed == 0){// has string or sequence
1449 size = 0;
1450 for(i=0;i<element_number;i++){
1451 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1452 fld->child[0], evD+size, trace);
1453 }
1454 }else size = fld->field_size;
1455 if(unlikely(!evD)){
1456 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1457 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1458 }
1459
1460 break;
1461
1462 case LTT_SEQUENCE:
1463 size1 = (int) ltt_type_size(trace, type);
1464 if(fld->field_fixed == -1){
1465 fld->sequ_number_size = size1;
cf74a6f1 1466 fld->field_fixed = 0;
a0d63196 1467 size = getFieldtypeSize(t, evT, offsetRoot,
1468 0,fld->child[0], NULL, trace);
1469 fld->element_size = size;
1470 }else{//0: sequence
1471 element_number = getIntNumber(size1,evD);
1472 type->element_number = element_number;
1473 if(fld->element_size > 0){
1474 size = element_number * fld->element_size;
1475 }else{//sequence has string or sequence
1476 size = 0;
1477 for(i=0;i<element_number;i++){
1478 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1479 fld->child[0], evD+size+size1, trace);
1480 }
1481 }
1482 size += size1;
cf74a6f1 1483 }
a0d63196 1484 if(unlikely(!evD)){
1485 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1486 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1487 }
1488
1489 break;
1490
1491 case LTT_STRING:
cf74a6f1 1492 size = 0;
a0d63196 1493 if(fld->field_fixed == -1){
1494 fld->field_fixed = 0;
1495 }else{//0: string
1496 /* Hope my implementation is faster than strlen (Mathieu) */
1497 char *ptr=(char*)evD;
1498 size = 1;
1499 /* from gcov : many many strings are empty, make it the common case.*/
1500 while(unlikely(*ptr != '\0')) { size++; ptr++; }
1501 //size = ptr - (char*)evD + 1; //include end : '\0'
cf74a6f1 1502 }
a0d63196 1503 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1504 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
cf74a6f1 1505
a0d63196 1506 break;
1507
1508 case LTT_STRUCT:
1509 element_number = (int) type->element_number;
1510 size = 0;
1511 /* tested with gcov */
1512 if(unlikely(fld->field_fixed == -1)){
1513 offset1 = offsetRoot;
1514 offset2 = 0;
cf74a6f1 1515 for(i=0;i<element_number;i++){
a0d63196 1516 size1=getFieldtypeSize(t, evT,offset1,offset2,
1517 fld->child[i], NULL, trace);
1518 if(likely(size1 > 0 && size >= 0)){
1519 size += size1;
1520 if(likely(offset1 >= 0)) offset1 += size1;
1521 offset2 += size1;
1522 }else{
1523 size = -1;
1524 offset1 = -1;
1525 offset2 = -1;
1526 }
cf74a6f1 1527 }
a0d63196 1528 if(unlikely(size == -1)){
1529 fld->field_fixed = 0;
1530 size = 0;
1531 }else fld->field_fixed = 1;
1532 }else if(likely(fld->field_fixed == 0)){
1533 offset1 = offsetRoot;
1534 offset2 = 0;
1535 for(i=0;unlikely(i<element_number);i++){
1536 size=getFieldtypeSize(t,evT,offset1,offset2,
1537 fld->child[i],evD+offset2, trace);
1538 offset1 += size;
1539 offset2 += size;
1540 }
1541 size = offset2;
1542 }else size = fld->field_size;
d37b2aaa 1543 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1544 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
a0d63196 1545 break;
cf74a6f1 1546
a0d63196 1547 default:
1548 if(unlikely(fld->field_fixed == -1)){
1549 size = (int) ltt_type_size(trace, type);
1550 fld->field_fixed = 1;
1551 }else size = fld->field_size;
1552 if(unlikely(!evD)){
1553 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1554 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1555 }
1556 break;
1557 }
cf74a6f1 1558 }
1559
6cd62ccf 1560 fld->offset_root = offsetRoot;
1561 fld->offset_parent = offsetParent;
6cd62ccf 1562 fld->field_size = size;
1563
21182d4a 1564end_getFieldtypeSize:
1565
6cd62ccf 1566 return size;
1567}
1568
6cd62ccf 1569
1570/*****************************************************************************
1571 *Function name
1572 * getIntNumber : get an integer number
1573 *Input params
1574 * size : the size of the integer
1575 * evD : the event data
1576 *Return value
cf74a6f1 1577 * gint64 : a 64 bits integer
6cd62ccf 1578 ****************************************************************************/
1579
cf74a6f1 1580gint64 getIntNumber(int size, void *evD)
6cd62ccf 1581{
cbd41522 1582 gint64 i;
cf74a6f1 1583
1584 switch(size) {
1585 case 1: i = *(gint8 *)evD; break;
1586 case 2: i = *(gint16 *)evD; break;
1587 case 4: i = *(gint32 *)evD; break;
1588 case 8: i = *(gint64 *)evD; break;
1589 default: i = *(gint64 *)evD;
1590 g_critical("getIntNumber : integer size %d unknown", size);
1591 break;
1592 }
1593
cf74a6f1 1594 return (gint64)i;
6cd62ccf 1595}
1596
1597/*****************************************************************************
1598 *Function name
1599 * getDataEndianType : get the data type size and endian type of the local
1600 * machine
1601 *Input params
1602 * size : size of data type
1603 * endian : endian type, little or big
1604 ****************************************************************************/
1605
963b5f2d 1606void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
6cd62ccf 1607{
1608 int i = 1;
1609 char c = (char) i;
1610 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1611
1612 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1613 else *endian = LTT_BIG_ENDIAN;
1614
1615 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1616 *size = LTT_LP32;
1617 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1618 *size = LTT_ILP32;
1619 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1620 *size = LTT_LP64;
1621 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1622 *size = LTT_ILP64;
1623 else *size = LTT_UNKNOWN;
1624}
1625
a5dcde2f 1626/* get the node name of the system */
1627
1628char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1629{
1630 return s->node_name;
1631}
1632
1633
1634/* get the domain name of the system */
1635
1636char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1637{
1638 return s->domain_name;
1639}
1640
1641
1642/* get the description of the system */
1643
1644char * ltt_trace_system_description_description (LttSystemDescription * s)
1645{
1646 return s->description;
1647}
1648
1649
1650/* get the start time of the trace */
1651
1652LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
1653{
1654 return s->trace_start;
1655}
1656
18206708 1657
1658LttTracefile *ltt_tracefile_new()
1659{
1660 return g_new(LttTracefile, 1);
1661}
1662
1663void ltt_tracefile_destroy(LttTracefile *tf)
1664{
1665 g_free(tf);
1666}
1667
1668void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1669{
1670 *dest = *src;
1671}
1672
This page took 0.11488 seconds and 4 git commands to generate.