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