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