git-svn-id: http://ltt.polymtl.ca/svn@240 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <dirent.h>
6 #include <linux/errno.h>
7
8 #include <ltt/LTTTypes.h>
9 #include "parser.h"
10 #include <ltt/trace.h>
11
12 #define DIR_NAME_SIZE 256
13
14 /* set the offset of the fields belonging to the event,
15 need the information of the archecture */
16 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
17
18 /* get the size of the field type according to the archtecture's
19 size and endian type(info of the archecture) */
20 int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot,
21 int offsetParent, LttField *fld, void *evD, LttTrace* t);
22
23 /* read a fixed size or a block information from the file (fd) */
24 int readFile(int fd, void * buf, size_t size, char * mesg);
25 int readBlock(LttTracefile * tf, int whichBlock);
26
27 /* calculate cycles per nsec for current block */
28 void getCyclePerNsec(LttTracefile * t);
29
30 /* reinitialize the info of the block which is already in the buffer */
31 void updateTracefile(LttTracefile * tf);
32
33 /* go to the next event */
34 int skipEvent(LttTracefile * t);
35
36 /* compare two time (LttTime), 0:t1=t2, -1:t1<t2, 1:t1>t2 */
37 int timecmp(LttTime * t1, LttTime * t2);
38
39
40
41 /*****************************************************************************
42 *Function name
43 * ltt_tracefile_open : open a trace file, construct a LttTracefile
44 *Input params
45 * t : the trace containing the tracefile
46 * fileName : path name of the trace file
47 *Return value
48 * : a pointer to a tracefile
49 ****************************************************************************/
50
51 LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName)
52 {
53 LttTracefile * tf;
54 struct stat lTDFStat; /* Trace data file status */
55 BlockStart a_block_start;
56
57 tf = g_new(LttTracefile, 1);
58
59 //open the file
60 tf->name = g_strdup(fileName);
61 tf->trace = t;
62 tf->fd = open(fileName, O_RDONLY, 0);
63 if(tf->fd < 0){
64 g_error("Unable to open input data file %s\n", fileName);
65 }
66
67 // Get the file's status
68 if(fstat(tf->fd, &lTDFStat) < 0){
69 g_error("Unable to get the status of the input data file %s\n", fileName);
70 }
71
72 // Is the file large enough to contain a trace
73 if(lTDFStat.st_size < sizeof(BlockStart) + EVENT_HEADER_SIZE){
74 g_print("The input data file %s does not contain a trace\n", fileName);
75 g_free(tf->name);
76 close(tf->fd);
77 g_free(tf);
78 return NULL;
79 }
80
81 //store the size of the file
82 tf->file_size = lTDFStat.st_size;
83 tf->block_size = t->system_description->ltt_block_size;
84 tf->block_number = tf->file_size / tf->block_size;
85 tf->which_block = 0;
86
87 //allocate memory to contain the info of a block
88 tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size);
89
90 //read the first block
91 if(readBlock(tf,1)) exit(1);
92
93 return tf;
94 }
95
96
97 /*****************************************************************************
98 *Open control and per cpu tracefiles
99 ****************************************************************************/
100
101 void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name)
102 {
103 LttTracefile * tf;
104 tf = ltt_tracefile_open(t,tracefile_name);
105 if(!tf) return;
106 t->per_cpu_tracefile_number++;
107 g_ptr_array_add(t->per_cpu_tracefiles, tf);
108 }
109
110 void ltt_tracefile_open_control(LttTrace *t, char * control_name)
111 {
112 LttTracefile * tf;
113 LttEvent * ev;
114 LttFacility * f;
115 uint16_t evId;
116 void * pos;
117 FacilityLoad fLoad;
118 int i;
119
120 tf = ltt_tracefile_open(t,control_name);
121 if(!tf) return;
122 t->control_tracefile_number++;
123 g_ptr_array_add(t->control_tracefiles,tf);
124
125 //parse facilities tracefile to get base_id
126 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
127 while(1){
128 ev = ltt_tracefile_read(tf);
129 if(!ev)return; // end of file
130
131 if(ev->event_id == TRACE_FACILITY_LOAD){
132 pos = ev->data;
133 fLoad.name = (char*)pos;
134 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
135 fLoad.base_code = *(uint32_t*)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
136
137 for(i=0;i<t->facility_number;i++){
138 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
139 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
140 f->base_id = fLoad.base_code;
141 break;
142 }
143 }
144 if(i==t->facility_number)
145 g_error("Facility: %s, checksum: %d is not founded\n",
146 fLoad.name,fLoad.checksum);
147 }else if(ev->event_id == TRACE_BLOCK_START){
148 continue;
149 }else if(ev->event_id == TRACE_BLOCK_END){
150 break;
151 }else g_error("Not valid facilities trace file\n");
152 }
153 }
154 }
155
156 /*****************************************************************************
157 *Function name
158 * ltt_tracefile_close: close a trace file,
159 *Input params
160 * t : tracefile which will be closed
161 ****************************************************************************/
162
163 void ltt_tracefile_close(LttTracefile *t)
164 {
165 g_free(t->name);
166 g_free(t->buffer);
167 close(t->fd);
168 g_free(t);
169 }
170
171
172 /*****************************************************************************
173 *Get system information
174 ****************************************************************************/
175 void getSystemInfo(LttSystemDescription* des, char * pathname)
176 {
177 FILE * fp;
178 int i;
179 int entry_number = 15;
180 char buf[DIR_NAME_SIZE];
181 char description[4*DIR_NAME_SIZE];
182 char * ptr;
183
184 fp = fopen(pathname,"r");
185 if(!fp){
186 g_error("Can not open file : %s\n", pathname);
187 }
188
189 while(fgets(buf,DIR_NAME_SIZE, fp)!= NULL){
190 ptr = buf;
191 while(isspace(*ptr)) ptr++;
192 if(strlen(ptr) == 0) continue;
193 break;
194 }
195
196 if(strlen(ptr) == 0) g_error("Not a valid file: %s\n", pathname);
197 if(strncmp("<system",ptr,7) !=0)g_error("Not a valid file: %s\n", pathname);
198
199 for(i=0;i<entry_number;i++){
200 if(fgets(buf,DIR_NAME_SIZE, fp)== NULL)
201 g_error("Not a valid file: %s\n", pathname);
202 buf[strlen(buf)-1] = '\0';
203 ptr = buf;
204 while(isspace(*ptr)) ptr++;
205 switch(i){
206 case 0:
207 if(strncmp("node_name=",ptr,10)!=0)
208 g_error("Not a valid file: %s\n", pathname);
209 des->node_name = g_strdup(ptr+10);
210 break;
211 case 1:
212 if(strncmp("domainname=",ptr,11)!=0)
213 g_error("Not a valid file: %s\n", pathname);
214 des->domain_name = g_strdup(ptr+11);
215 break;
216 case 2:
217 if(strncmp("cpu=",ptr,4)!=0)
218 g_error("Not a valid file: %s\n", pathname);
219 des->nb_cpu = (unsigned)atoi(ptr+4);
220 break;
221 case 3:
222 if(strncmp("arch_size=",ptr,10)!=0)
223 g_error("Not a valid file: %s\n", pathname);
224 if(strcmp(ptr+10,"\"LP32\"") == 0) des->size = LTT_LP32;
225 else if(strcmp(ptr+10,"\"ILP32\"") == 0) des->size = LTT_ILP32;
226 else if(strcmp(ptr+10,"\"LP64\"") == 0) des->size = LTT_LP64;
227 else if(strcmp(ptr+10,"\"ILP64\"") == 0) des->size = LTT_ILP64;
228 else if(strcmp(ptr+10,"\"UNKNOWN\"") == 0) des->size = LTT_UNKNOWN;
229 break;
230 case 4:
231 if(strncmp("endian=",ptr,7)!=0)
232 g_error("Not a valid file: %s\n", pathname);
233 if(strcmp(ptr+7,"\"LITTLE_ENDIAN\"") == 0)
234 des->endian = LTT_LITTLE_ENDIAN;
235 else if(strcmp(ptr+7,"\"BIG_ENDIAN\"") == 0)
236 des->endian = LTT_BIG_ENDIAN;
237 break;
238 case 5:
239 if(strncmp("kernel_name=",ptr,12)!=0)
240 g_error("Not a valid file: %s\n", pathname);
241 des->kernel_name = g_strdup(ptr+12);
242 break;
243 case 6:
244 if(strncmp("kernel_release=",ptr,15)!=0)
245 g_error("Not a valid file: %s\n", pathname);
246 des->kernel_release = g_strdup(ptr+15);
247 break;
248 case 7:
249 if(strncmp("kernel_version=",ptr,15)!=0)
250 g_error("Not a valid file: %s\n", pathname);
251 des->kernel_version = g_strdup(ptr+15);
252 break;
253 case 8:
254 if(strncmp("machine=",ptr,8)!=0)
255 g_error("Not a valid file: %s\n", pathname);
256 des->machine = g_strdup(ptr+8);
257 break;
258 case 9:
259 if(strncmp("processor=",ptr,10)!=0)
260 g_error("Not a valid file: %s\n", pathname);
261 des->processor = g_strdup(ptr+10);
262 break;
263 case 10:
264 if(strncmp("hardware_platform=",ptr,18)!=0)
265 g_error("Not a valid file: %s\n", pathname);
266 des->hardware_platform = g_strdup(ptr+18);
267 break;
268 case 11:
269 if(strncmp("operating_system=",ptr,17)!=0)
270 g_error("Not a valid file: %s\n", pathname);
271 des->operating_system = g_strdup(ptr+17);
272 break;
273 case 12:
274 if(strncmp("ltt_major_version=",ptr,18)!=0)
275 g_error("Not a valid file: %s\n", pathname);
276 ptr += 18;
277 // ptr++;//skip begining "
278 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
279 des->ltt_major_version = (unsigned)atoi(ptr);
280 break;
281 case 13:
282 if(strncmp("ltt_minor_version=",ptr,18)!=0)
283 g_error("Not a valid file: %s\n", pathname);
284 ptr += 18;
285 // ptr++;//skip begining "
286 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
287 des->ltt_minor_version = (unsigned)atoi(ptr);
288 break;
289 case 14:
290 if(strncmp("ltt_block_size=",ptr,15)!=0)
291 g_error("Not a valid file: %s\n", pathname);
292 ptr += 15;
293 // ptr++;//skip begining "
294 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
295 des->ltt_block_size = (unsigned)atoi(ptr);
296 break;
297 default:
298 g_error("Not a valid file: %s\n", pathname);
299 }
300 }
301
302 //get description
303 description[0] = '\0';
304 if(fgets(buf,DIR_NAME_SIZE, fp)== NULL)
305 g_error("Not a valid file: %s\n", pathname);
306 ptr = buf;
307 while(isspace(*ptr)) ptr++;
308 if(*ptr != '>') g_error("Not a valid file: %s\n", pathname);
309 while((ptr=fgets(buf,DIR_NAME_SIZE, fp))!= NULL){
310 ptr = buf;
311 while(isspace(*ptr)) ptr++;
312 if(strncmp("</system>",ptr,9) == 0 )break;
313 strcat(description, buf);
314 }
315 if(!ptr)g_error("Not a valid file: %s\n", pathname);
316 if(description[0] = '\0')des->description = NULL;
317 des->description = g_strdup(description);
318
319 fclose(fp);
320 }
321
322 /*****************************************************************************
323 *The following functions get facility/tracefile information
324 ****************************************************************************/
325
326 void getFacilityInfo(LttTrace *t, char* eventdefs)
327 {
328 DIR * dir;
329 struct dirent *entry;
330 char * ptr;
331 int i,j;
332 LttFacility * f;
333 LttEventType * et;
334 char name[DIR_NAME_SIZE];
335
336 dir = opendir(eventdefs);
337 if(!dir) g_error("Can not open directory: %s\n", eventdefs);
338
339 while((entry = readdir(dir)) != NULL){
340 ptr = &entry->d_name[strlen(entry->d_name)-4];
341 if(strcmp(ptr,".xml") != 0) continue;
342 strcpy(name,eventdefs);
343 strcat(name,entry->d_name);
344 ltt_facility_open(t,name);
345 }
346 closedir(dir);
347
348 for(j=0;j<t->facility_number;j++){
349 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
350 for(i=0; i<f->event_number; i++){
351 et = f->events[i];
352 setFieldsOffset(NULL, et, NULL, t);
353 }
354 }
355 }
356
357 void getControlFileInfo(LttTrace *t, char* control)
358 {
359 DIR * dir;
360 struct dirent *entry;
361 char name[DIR_NAME_SIZE];
362
363 dir = opendir(control);
364 if(!dir) g_error("Can not open directory: %s\n", control);
365
366 while((entry = readdir(dir)) != NULL){
367 if(strcmp(entry->d_name,"facilities") != 0 &&
368 strcmp(entry->d_name,"interrupts") != 0 &&
369 strcmp(entry->d_name,"processes") != 0) continue;
370
371 strcpy(name,control);
372 strcat(name,entry->d_name);
373 ltt_tracefile_open_control(t,name);
374 }
375 closedir(dir);
376 }
377
378 void getCpuFileInfo(LttTrace *t, char* cpu)
379 {
380 DIR * dir;
381 struct dirent *entry;
382 char name[DIR_NAME_SIZE];
383
384 dir = opendir(cpu);
385 if(!dir) g_error("Can not open directory: %s\n", cpu);
386
387 while((entry = readdir(dir)) != NULL){
388 if(strcmp(entry->d_name,".") != 0 &&
389 strcmp(entry->d_name,"..") != 0 ){
390 strcpy(name,cpu);
391 strcat(name,entry->d_name);
392 ltt_tracefile_open_cpu(t,name);
393 }else continue;
394 }
395 closedir(dir);
396 }
397
398 /*****************************************************************************
399 *A trace is specified as a pathname to the directory containing all the
400 *associated data (control tracefiles, per cpu tracefiles, event
401 *descriptions...).
402 *
403 *When a trace is closed, all the associated facilities, types and fields
404 *are released as well.
405 ****************************************************************************/
406
407 LttTrace *ltt_trace_open(char *pathname)
408 {
409 LttTrace * t;
410 LttSystemDescription * sys_description;
411 char eventdefs[DIR_NAME_SIZE];
412 char info[DIR_NAME_SIZE];
413 char control[DIR_NAME_SIZE];
414 char cpu[DIR_NAME_SIZE];
415 char tmp[DIR_NAME_SIZE];
416 gboolean has_slash = FALSE;
417
418 //establish the pathname to different directories
419 if(pathname[strlen(pathname)-1] == '/')has_slash = TRUE;
420 strcpy(eventdefs,pathname);
421 if(!has_slash)strcat(eventdefs,"/");
422 strcat(eventdefs,"eventdefs/");
423
424 strcpy(info,pathname);
425 if(!has_slash)strcat(info,"/");
426 strcat(info,"info/");
427
428 strcpy(control,pathname);
429 if(!has_slash)strcat(control,"/");
430 strcat(control,"control/");
431
432 strcpy(cpu,pathname);
433 if(!has_slash)strcat(cpu,"/");
434 strcat(cpu,"cpu/");
435
436 //new trace
437 t = g_new(LttTrace, 1);
438 sys_description = g_new(LttSystemDescription, 1);
439 t->pathname = g_strdup(pathname);
440 t->facility_number = 0;
441 t->control_tracefile_number = 0;
442 t->per_cpu_tracefile_number = 0;
443 t->system_description = sys_description;
444 t->control_tracefiles = g_ptr_array_new();
445 t->per_cpu_tracefiles = g_ptr_array_new();
446 t->facilities = g_ptr_array_new();
447 getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
448
449 //get system description
450 strcpy(tmp,info);
451 strcat(tmp,"system.xml");
452 getSystemInfo(sys_description, tmp);
453
454 //get facilities info
455 getFacilityInfo(t,eventdefs);
456
457 //get control tracefile info
458 getControlFileInfo(t,control);
459
460 //get cpu tracefile info
461 getCpuFileInfo(t,cpu);
462
463 return t;
464 }
465
466 void ltt_trace_close(LttTrace *t)
467 {
468 int i;
469 LttTracefile * tf;
470 LttFacility * f;
471
472 g_free(t->pathname);
473
474 //free system_description
475 g_free(t->system_description->description);
476 g_free(t->system_description->node_name);
477 g_free(t->system_description->domain_name);
478 g_free(t->system_description->kernel_name);
479 g_free(t->system_description->kernel_release);
480 g_free(t->system_description->kernel_version);
481 g_free(t->system_description->machine);
482 g_free(t->system_description->processor);
483 g_free(t->system_description->hardware_platform);
484 g_free(t->system_description->operating_system);
485 g_free(t->system_description);
486
487 //free control_tracefiles
488 for(i=0;i<t->control_tracefile_number;i++){
489 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
490 ltt_tracefile_close(tf);
491 }
492 g_ptr_array_free(t->control_tracefiles, TRUE);
493
494 //free per_cpu_tracefiles
495 for(i=0;i<t->per_cpu_tracefile_number;i++){
496 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
497 ltt_tracefile_close(tf);
498 }
499 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
500
501 //free facilities
502 for(i=0;i<t->facility_number;i++){
503 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
504 ltt_facility_close(f);
505 }
506 g_ptr_array_free(t->facilities, TRUE);
507
508 g_free(t);
509
510 g_blow_chunks();
511 }
512
513
514 /*****************************************************************************
515 *Get the system description of the trace
516 ****************************************************************************/
517
518 LttSystemDescription *ltt_trace_system_description(LttTrace *t)
519 {
520 return t->system_description;
521 }
522
523 /*****************************************************************************
524 * The following functions discover the facilities of the trace
525 ****************************************************************************/
526
527 unsigned ltt_trace_facility_number(LttTrace *t)
528 {
529 return (unsigned)(t->facility_number);
530 }
531
532 LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
533 {
534 return (LttFacility*)g_ptr_array_index(t->facilities, i);
535 }
536
537 /*****************************************************************************
538 *Function name
539 * ltt_trace_facility_find : find facilities in the trace
540 *Input params
541 * t : the trace
542 * name : facility name
543 *Output params
544 * position : position of the facility in the trace
545 *Return value
546 * : the number of facilities
547 ****************************************************************************/
548
549 unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
550 {
551 int i, count=0;
552 LttFacility * f;
553 for(i=0;i<t->facility_number;i++){
554 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
555 if(strcmp(f->name,name)==0){
556 count++;
557 if(count==1) *position = i;
558 }else{
559 if(count) break;
560 }
561 }
562 return count;
563 }
564
565 /*****************************************************************************
566 * Functions to discover all the event types in the trace
567 ****************************************************************************/
568
569 unsigned ltt_trace_eventtype_number(LttTrace *t)
570 {
571 int i;
572 unsigned count = 0;
573 LttFacility * f;
574 for(i=0;i=t->facility_number;i++){
575 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
576 count += f->event_number;
577 }
578 return count;
579 }
580
581 LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
582 {
583 LttFacility * facility;
584 int i;
585 for(i=0;i<trace->facility_number;i++){
586 facility = (LttFacility*) g_ptr_array_index(trace->facilities,i);
587 if(id >= facility->base_id &&
588 id < facility->base_id + facility->event_number)
589 break;
590 }
591 if(i==trace->facility_number) return NULL;
592 else return facility;
593 }
594
595 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
596 {
597 LttFacility * f;
598 f = ltt_trace_facility_by_id(t,evId);
599 if(!f) return NULL;
600 return f->events[evId - f->base_id];
601 }
602
603 /*****************************************************************************
604 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
605 *the maximum number of CPU in the system. When the number of CPU installed
606 *is less than the maximum, some positions are unused. There are also a
607 *number of "control" tracefiles (facilities, interrupts...).
608 ****************************************************************************/
609 unsigned ltt_trace_control_tracefile_number(LttTrace *t)
610 {
611 return t->control_tracefile_number;
612 }
613
614 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
615 {
616 return t->per_cpu_tracefile_number;
617 }
618
619 /*****************************************************************************
620 *It is possible to search for the tracefiles by name or by CPU position.
621 *The index within the tracefiles of the same type is returned if found
622 *and a negative value otherwise.
623 ****************************************************************************/
624
625 int ltt_trace_control_tracefile_find(LttTrace *t, char *name)
626 {
627 LttTracefile * tracefile;
628 int i;
629 for(i=0;i<t->control_tracefile_number;i++){
630 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
631 if(strcmp(tracefile->name, name)==0)break;
632 }
633 if(i == t->control_tracefile_number) return -1;
634 return i;
635 }
636
637 int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i)
638 {
639 LttTracefile * tracefile;
640 int j, name;
641 for(j=0;j<t->per_cpu_tracefile_number;j++){
642 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j);
643 name = atoi(tracefile->name);
644 if(name == (int)i)break;
645 }
646 if(j == t->per_cpu_tracefile_number) return -1;
647 return j;
648 }
649
650 /*****************************************************************************
651 *Get a specific tracefile
652 ****************************************************************************/
653
654 LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
655 {
656 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
657 }
658
659 LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
660 {
661 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
662 }
663
664 /*****************************************************************************
665 * Get the start time and end time of the trace
666 ****************************************************************************/
667
668 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
669 {
670 LttTime startSmall, startTmp, endBig, endTmp;
671 int i, j=0;
672 LttTracefile * tf;
673
674 for(i=0;i<t->control_tracefile_number;i++){
675 tf = g_ptr_array_index(t->control_tracefiles, i);
676 readBlock(tf,1);
677 startTmp = tf->a_block_start->time;
678 readBlock(tf,tf->block_number);
679 endTmp = tf->a_block_end->time;
680 if(i==0){
681 startSmall = startTmp;
682 endBig = endTmp;
683 j = 1;
684 continue;
685 }
686 if(timecmp(&startSmall,&startTmp) > 0) startSmall = startTmp;
687 if(timecmp(&endBig,&endTmp) < 0) endBig = endTmp;
688 }
689
690 for(i=0;i<t->per_cpu_tracefile_number;i++){
691 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
692 readBlock(tf,1);
693 startTmp = tf->a_block_start->time;
694 readBlock(tf,tf->block_number);
695 endTmp = tf->a_block_end->time;
696 if(j == 0 && i==0){
697 startSmall = startTmp;
698 endBig = endTmp;
699 continue;
700 }
701 if(timecmp(&startSmall,&startTmp) > 0) startSmall = startTmp;
702 if(timecmp(&endBig,&endTmp) < 0) endBig = endTmp;
703 }
704
705 *start = startSmall;
706 *end = endBig;
707 }
708
709
710 /*****************************************************************************
711 *Get the name of a tracefile
712 ****************************************************************************/
713
714 char *ltt_tracefile_name(LttTracefile *tf)
715 {
716 return tf->name;
717 }
718
719 /*****************************************************************************
720 * Get the number of blocks in the tracefile
721 ****************************************************************************/
722
723 unsigned ltt_tracefile_block_number(LttTracefile *tf)
724 {
725 return tf->block_number;
726 }
727
728 /*****************************************************************************
729 *Function name
730 * ltt_tracefile_seek_time: seek to the first event of the trace with time
731 * larger or equal to time
732 *Input params
733 * t : tracefile
734 * time : criteria of the time
735 ****************************************************************************/
736
737 void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
738 {
739 int err;
740 LttTime lttTime;
741 int headTime = timecmp(&(t->a_block_start->time), &time);
742 int tailTime = timecmp(&(t->a_block_end->time), &time);
743 LttEvent * ev;
744
745 if(headTime < 0 && tailTime > 0){
746 lttTime = getEventTime(t);
747 err = timecmp(&lttTime, &time);
748 if(err > 0){
749 if(t->which_event==2 || timecmp(&t->prev_event_time,&time)<0){
750 return;
751 }else{
752 updateTracefile(t);
753 return ltt_tracefile_seek_time(t, time);
754 }
755 }else if(err < 0){
756 while(1){
757 ev = ltt_tracefile_read(t);
758 if(ev == NULL){
759 g_print("End of file\n");
760 return;
761 }
762 lttTime = getEventTime(t);
763 err = timecmp(&lttTime, &time);
764 if(err >= 0)return;
765 }
766 }else return;
767 }else if(headTime > 0){
768 if(t->which_block == 1){
769 updateTracefile(t);
770 }else{
771 if(timecmp(&(t->prev_block_end_time),&time) >= 0 ){
772 err=readBlock(t,t->which_block-1);
773 if(err) g_error("Can not read tracefile: %s\n", t->name);
774 return ltt_tracefile_seek_time(t, time) ;
775 }else{
776 updateTracefile(t);
777 }
778 }
779 }else if(tailTime < 0){
780 if(t->which_block != t->block_number){
781 err=readBlock(t,t->which_block+1);
782 if(err) g_error("Can not read tracefile: %s\n", t->name);
783 }else {
784 g_print("End of file\n");
785 return;
786 }
787 if(tailTime < 0) return ltt_tracefile_seek_time(t, time);
788 }else if(headTime == 0){
789 updateTracefile(t);
790 }else if(tailTime == 0){
791 t->cur_event_pos = t->a_block_end - EVENT_HEADER_SIZE;
792 t->current_event_time = time;
793 t->cur_heart_beat_number = 0;
794 t->prev_event_time.tv_sec = 0;
795 t->prev_event_time.tv_nsec = 0;
796 return;
797 }
798 }
799
800 /*****************************************************************************
801 * Seek to the first event with position equal or larger to ep
802 ****************************************************************************/
803
804 void ltt_tracefile_seek_position(LttTracefile *t, LttEventPosition *ep)
805 {
806 //if we are at the right place, just return
807 if(t->which_block == ep->block_num && t->which_event == ep->event_num)
808 return;
809
810 if(t->which_block == ep->block_num) updateTracefile(t);
811 else readBlock(t,ep->block_num);
812
813 //event offset is availiable
814 if(ep->old_position){
815 t->cur_heart_beat_number = ep->heart_beat_number;
816 t->cur_event_pos = t->buffer + ep->event_offset;
817 return;
818 }
819
820 //only block number and event index are availiable
821 while(t->which_event < ep->event_num) ltt_tracefile_read(t);
822
823 return;
824 }
825
826 /*****************************************************************************
827 *Function name
828 * ltt_tracefile_read : read the current event, set the pointer to the next
829 *Input params
830 * t : tracefile
831 *Return value
832 * LttEvent * : an event to be processed
833 ****************************************************************************/
834
835 LttEvent *ltt_tracefile_read(LttTracefile *t)
836 {
837 LttEvent * lttEvent = &t->an_event;
838 int err;
839
840 if(t->cur_event_pos == t->buffer + t->block_size){
841 if(t->which_block == t->block_number){
842 return NULL;
843 }
844 err = readBlock(t, t->which_block + 1);
845 if(err)g_error("Can not read tracefile");
846 }
847
848 lttEvent->event_id = (int)(*(uint16_t *)(t->cur_event_pos));
849 if(lttEvent->event_id == TRACE_TIME_HEARTBEAT)
850 t->cur_heart_beat_number++;
851
852 t->prev_event_time = t->current_event_time;
853 // t->current_event_time = getEventTime(t);
854
855 lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE);
856 lttEvent->event_time = t->current_event_time;
857
858 lttEvent->tracefile = t;
859 lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE;
860 lttEvent->which_block = t->which_block;
861 lttEvent->which_event = t->which_event;
862
863 //update the fields of the current event and go to the next event
864 err = skipEvent(t);
865 if(err == ERANGE) g_error("event id is out of range\n");
866
867 lttEvent->event_cycle_count = t->cur_cycle_count;
868
869 return lttEvent;
870 }
871
872 /****************************************************************************
873 *Function name
874 * readFile : wrap function to read from a file
875 *Input Params
876 * fd : file descriptor
877 * buf : buf to contain the content
878 * size : number of bytes to be read
879 * mesg : message to be printed if some thing goes wrong
880 *return value
881 * 0 : success
882 * EIO : can not read from the file
883 ****************************************************************************/
884
885 int readFile(int fd, void * buf, size_t size, char * mesg)
886 {
887 ssize_t nbBytes;
888 nbBytes = read(fd, buf, size);
889 if(nbBytes != size){
890 printf("%s\n",mesg);
891 return EIO;
892 }
893 return 0;
894 }
895
896 /****************************************************************************
897 *Function name
898 * readBlock : read a block from the file
899 *Input Params
900 * lttdes : ltt trace file
901 * whichBlock : the block which will be read
902 *return value
903 * 0 : success
904 * EINVAL : lseek fail
905 * EIO : can not read from the file
906 ****************************************************************************/
907
908 int readBlock(LttTracefile * tf, int whichBlock)
909 {
910 off_t nbBytes;
911 uint32_t lostSize;
912
913 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
914 tf->prev_block_end_time = tf->a_block_end->time;
915 tf->prev_event_time = tf->a_block_end->time;
916 }else{
917 tf->prev_block_end_time.tv_sec = 0;
918 tf->prev_block_end_time.tv_nsec = 0;
919 tf->prev_event_time.tv_sec = 0;
920 tf->prev_event_time.tv_nsec = 0;
921 }
922
923 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
924 if(nbBytes == -1) return EINVAL;
925
926 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
927 return EIO;
928
929 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
930 lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t));
931 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
932 lostSize + EVENT_HEADER_SIZE);
933
934 tf->which_block = whichBlock;
935 tf->which_event = 1;
936 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
937 tf->cur_heart_beat_number = 0;
938
939 getCyclePerNsec(tf);
940
941 tf->current_event_time = getEventTime(tf);
942
943 return 0;
944 }
945
946 /*****************************************************************************
947 *Function name
948 * updateTracefile : reinitialize the info of the block which is already
949 * in the buffer
950 *Input params
951 * tf : tracefile
952 ****************************************************************************/
953
954 void updateTracefile(LttTracefile * tf)
955 {
956 tf->which_event = 1;
957 tf->cur_event_pos = tf->buffer;
958 tf->current_event_time = getEventTime(tf);
959 tf->cur_heart_beat_number = 0;
960
961 tf->prev_event_time.tv_sec = 0;
962 tf->prev_event_time.tv_nsec = 0;
963 }
964
965 /*****************************************************************************
966 *Function name
967 * skipEvent : go to the next event, update the fields of the current event
968 *Input params
969 * t : tracefile
970 *return value
971 * 0 : success
972 * ERANGE : event id is out of range
973 ****************************************************************************/
974
975 int skipEvent(LttTracefile * t)
976 {
977 int evId, err;
978 void * evData;
979 LttEventType * evT;
980 LttField * rootFld;
981
982 evId = (int)(*(uint16_t *)(t->cur_event_pos));
983 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
984
985 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
986
987 if(evT) rootFld = evT->root_field;
988 else return ERANGE;
989
990 if(rootFld){
991 //event has string/sequence or the last event is not the same event
992 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
993 && rootFld->field_fixed == 0){
994 setFieldsOffset(t, evT, evData, t->trace);
995 }
996 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
997 }else t->cur_event_pos += EVENT_HEADER_SIZE;
998
999 evT->latest_block = t->which_block;
1000 evT->latest_event = t->which_event;
1001
1002 //the next event is in the next block
1003 if(evId == TRACE_BLOCK_END){
1004 t->cur_event_pos = t->buffer + t->block_size;
1005 }else{
1006 t->which_event++;
1007 t->current_event_time = getEventTime(t);
1008 }
1009
1010 return 0;
1011 }
1012
1013 /*****************************************************************************
1014 *Function name
1015 * getCyclePerNsec : calculate cycles per nsec for current block
1016 *Input Params
1017 * t : tracefile
1018 ****************************************************************************/
1019
1020 void getCyclePerNsec(LttTracefile * t)
1021 {
1022 LttTime lBufTotalTime; /* Total time for this buffer */
1023 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
1024 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
1025
1026 /* Calculate the total time for this buffer */
1027 TimeSub(lBufTotalTime,t->a_block_end->time, t->a_block_start->time);
1028
1029 /* Calculate the total cycles for this bufffer */
1030 lBufTotalCycle = t->a_block_end->cycle_count;
1031 lBufTotalCycle -= t->a_block_start->cycle_count;
1032
1033 /* Convert the total time to nsecs */
1034 lBufTotalNSec = lBufTotalTime.tv_sec;
1035 lBufTotalNSec *= NANSECOND_CONST;
1036 lBufTotalNSec += lBufTotalTime.tv_nsec;
1037
1038 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
1039 }
1040
1041 /****************************************************************************
1042 *Function name
1043 * getEventTime : obtain the time of an event
1044 *Input params
1045 * tf : tracefile
1046 *Return value
1047 * LttTime : the time of the event
1048 ****************************************************************************/
1049
1050 LttTime getEventTime(LttTracefile * tf)
1051 {
1052 LttTime time;
1053 LttCycleCount cycle_count; // cycle count for the current event
1054 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1055 double lEventNSec; // Total usecs from start for event
1056 LttTime lTimeOffset; // Time offset in struct LttTime
1057 uint16_t evId;
1058 int64_t nanoSec, tmpCycleCount = (((uint64_t)1)<<32);
1059 static LttCycleCount preCycleCount = 0;
1060 static int count = 0;
1061
1062 evId = *(uint16_t*)tf->cur_event_pos;
1063 if(evId == TRACE_BLOCK_START){
1064 count = 0;
1065 preCycleCount = 0;
1066 tf->cur_cycle_count = tf->a_block_start->cycle_count;
1067 return tf->a_block_start->time;
1068 }else if(evId == TRACE_BLOCK_END){
1069 count = 0;
1070 preCycleCount = 0;
1071 tf->cur_cycle_count = tf->a_block_end->cycle_count;
1072 return tf->a_block_end->time;
1073 }
1074
1075 // Calculate total time in cycles from start of buffer for this event
1076 cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE);
1077
1078 if(cycle_count < preCycleCount)count++;
1079 preCycleCount = cycle_count;
1080 cycle_count += tmpCycleCount * count;
1081
1082 if(tf->cur_heart_beat_number > count)
1083 cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - count);
1084
1085 tf->cur_cycle_count = cycle_count;
1086
1087 lEventTotalCycle = cycle_count;
1088 lEventTotalCycle -= tf->a_block_start->cycle_count;
1089
1090 // Convert it to nsecs
1091 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
1092 nanoSec = lEventNSec;
1093
1094 // Determine offset in struct LttTime
1095 lTimeOffset.tv_nsec = nanoSec % NANSECOND_CONST;
1096 lTimeOffset.tv_sec = nanoSec / NANSECOND_CONST;
1097
1098 TimeAdd(time, tf->a_block_start->time, lTimeOffset);
1099
1100 return time;
1101 }
1102
1103 /*****************************************************************************
1104 *Function name
1105 * setFieldsOffset : set offset of the fields
1106 *Input params
1107 * tracefile : opened trace file
1108 * evT : the event type
1109 * evD : event data, it may be NULL
1110 ****************************************************************************/
1111
1112 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1113 {
1114 LttField * rootFld = evT->root_field;
1115 // rootFld->base_address = evD;
1116
1117 if(rootFld)
1118 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1119 }
1120
1121 /*****************************************************************************
1122 *Function name
1123 * getFieldtypeSize: get the size of the field type (primitive type)
1124 *Input params
1125 * tracefile : opened trace file
1126 * evT : event type
1127 * offsetRoot : offset from the root
1128 * offsetParent : offset from the parrent
1129 * fld : field
1130 * evD : event data, it may be NULL
1131 *Return value
1132 * int : size of the field
1133 ****************************************************************************/
1134
1135 int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
1136 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
1137 {
1138 int size, size1, element_number, i, offset1, offset2;
1139 LttType * type = fld->field_type;
1140
1141 if(t){
1142 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1143 return fld->field_size;
1144 }
1145 }
1146
1147 if(fld->field_fixed == 1){
1148 if(fld == evT->root_field) return fld->field_size;
1149 }
1150
1151 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1152 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1153 if(fld->field_fixed == -1){
1154 size = (int) ltt_type_size(trace, type);
1155 fld->field_fixed = 1;
1156 }else size = fld->field_size;
1157
1158 }else if(type->type_class == LTT_ARRAY){
1159 element_number = (int) type->element_number;
1160 if(fld->field_fixed == -1){
1161 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1162 if(size == 0){ //has string or sequence
1163 fld->field_fixed = 0;
1164 }else{
1165 fld->field_fixed = 1;
1166 size *= element_number;
1167 }
1168 }else if(fld->field_fixed == 0){// has string or sequence
1169 size = 0;
1170 for(i=0;i<element_number;i++){
1171 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1172 fld->child[0], evD+size, trace);
1173 }
1174 }else size = fld->field_size;
1175
1176 }else if(type->type_class == LTT_SEQUENCE){
1177 size1 = (int) ltt_type_size(trace, type);
1178 if(fld->field_fixed == -1){
1179 fld->sequ_number_size = size1;
1180 fld->field_fixed = 0;
1181 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1182 fld->element_size = size;
1183 }else{//0: sequence
1184 element_number = getIntNumber(size1,evD);
1185 type->element_number = element_number;
1186 if(fld->element_size > 0){
1187 size = element_number * fld->element_size;
1188 }else{//sequence has string or sequence
1189 size = 0;
1190 for(i=0;i<element_number;i++){
1191 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1192 fld->child[0], evD+size+size1, trace);
1193 }
1194 }
1195 size += size1;
1196 }
1197
1198 }else if(type->type_class == LTT_STRING){
1199 size = 0;
1200 if(fld->field_fixed == -1){
1201 fld->field_fixed = 0;
1202 }else{//0: string
1203 size = strlen((char*)evD) + 1; //include end : '\0'
1204 }
1205
1206 }else if(type->type_class == LTT_STRUCT){
1207 element_number = (int) type->element_number;
1208 size = 0;
1209 if(fld->field_fixed == -1){
1210 offset1 = offsetRoot;
1211 offset2 = 0;
1212 for(i=0;i<element_number;i++){
1213 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
1214 if(size1 > 0 && size >= 0){
1215 size += size1;
1216 if(offset1 >= 0) offset1 += size1;
1217 offset2 += size1;
1218 }else{
1219 size = -1;
1220 offset1 = -1;
1221 offset2 = -1;
1222 }
1223 }
1224 if(size == -1){
1225 fld->field_fixed = 0;
1226 size = 0;
1227 }else fld->field_fixed = 1;
1228 }else if(fld->field_fixed == 0){
1229 offset1 = offsetRoot;
1230 offset2 = 0;
1231 for(i=0;i<element_number;i++){
1232 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
1233 offset1 += size;
1234 offset2 += size;
1235 }
1236 size = offset2;
1237 }else size = fld->field_size;
1238 }
1239
1240 fld->offset_root = offsetRoot;
1241 fld->offset_parent = offsetParent;
1242 if(!evD){
1243 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1244 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1245 }
1246 fld->field_size = size;
1247
1248 return size;
1249 }
1250
1251 /*****************************************************************************
1252 *Function name
1253 * timecmp : compare two time
1254 *Input params
1255 * t1 : first time
1256 * t2 : second time
1257 *Return value
1258 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1259 ****************************************************************************/
1260
1261 int timecmp(LttTime * t1, LttTime * t2)
1262 {
1263 if(t1->tv_sec < t2->tv_sec) return -1;
1264 if(t1->tv_sec > t2->tv_sec) return 1;
1265 if(t1->tv_nsec < t2->tv_nsec) return -1;
1266 if(t1->tv_nsec > t2->tv_nsec) return 1;
1267 return 0;
1268 }
1269
1270 /*****************************************************************************
1271 *Function name
1272 * getIntNumber : get an integer number
1273 *Input params
1274 * size : the size of the integer
1275 * evD : the event data
1276 *Return value
1277 * int : an integer
1278 ****************************************************************************/
1279
1280 int getIntNumber(int size, void *evD)
1281 {
1282 int64_t i;
1283 if(size == 1) i = *(int8_t *)evD;
1284 else if(size == 2) i = *(int16_t *)evD;
1285 else if(size == 4) i = *(int32_t *)evD;
1286 else if(size == 8) i = *(int64_t *)evD;
1287
1288 return (int) i;
1289 }
1290
1291 /*****************************************************************************
1292 *Function name
1293 * getDataEndianType : get the data type size and endian type of the local
1294 * machine
1295 *Input params
1296 * size : size of data type
1297 * endian : endian type, little or big
1298 ****************************************************************************/
1299
1300 void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
1301 {
1302 int i = 1;
1303 char c = (char) i;
1304 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1305
1306 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1307 else *endian = LTT_BIG_ENDIAN;
1308
1309 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1310 *size = LTT_LP32;
1311 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1312 *size = LTT_ILP32;
1313 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1314 *size = LTT_LP64;
1315 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1316 *size = LTT_ILP64;
1317 else *size = LTT_UNKNOWN;
1318 }
1319
This page took 0.061729 seconds and 5 git commands to generate.