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