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