git-svn-id: http://ltt.polymtl.ca/svn@130 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[strlen(control_name)-10],"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 facilities info
481 getFacilityInfo(t,eventdefs);
482
483 //get control tracefile info
484 getControlFileInfo(t,control);
485
486 //get cpu tracefile info
487 getCpuFileInfo(t,cpu);
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 == ENOMEM) return NULL;
794 if(err == ENOENT) return lttEvent;
795 if(err == ERANGE) g_error("event id is out of range\n");
796 if(err)g_error("Can not read tracefile\n");
797
798 return lttEvent;
799 }
800
801 /****************************************************************************
802 *Function name
803 * readFile : wrap function to read from a file
804 *Input Params
805 * fd : file descriptor
806 * buf : buf to contain the content
807 * size : number of bytes to be read
808 * mesg : message to be printed if some thing goes wrong
809 *return value
810 * 0 : success
811 * EIO : can not read from the file
812 ****************************************************************************/
813
814 int readFile(int fd, void * buf, size_t size, char * mesg)
815 {
816 ssize_t nbBytes;
817 nbBytes = read(fd, buf, size);
818 if(nbBytes != size){
819 printf("%s\n",mesg);
820 return EIO;
821 }
822 return 0;
823 }
824
825 /****************************************************************************
826 *Function name
827 * readBlock : read a block from the file
828 *Input Params
829 * lttdes : ltt trace file
830 * whichBlock : the block which will be read
831 *return value
832 * 0 : success
833 * EINVAL : lseek fail
834 * EIO : can not read from the file
835 ****************************************************************************/
836
837 int readBlock(LttTracefile * tf, int whichBlock)
838 {
839 off_t nbBytes;
840 uint32_t lostSize;
841
842 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
843 tf->prev_block_end_time = tf->a_block_end->time;
844 tf->prev_event_time = tf->a_block_end->time;
845 tf->current_event_time = tf->a_block_end->time;
846 }else{
847 tf->prev_block_end_time.tv_sec = 0;
848 tf->prev_block_end_time.tv_nsec = 0;
849 tf->prev_event_time.tv_sec = 0;
850 tf->prev_event_time.tv_nsec = 0;
851 tf->current_event_time.tv_sec = 0;
852 tf->current_event_time.tv_nsec = 0;
853 }
854
855 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
856 if(nbBytes == -1) return EINVAL;
857
858 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
859 return EIO;
860
861 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
862 lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t));
863 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
864 lostSize + EVENT_HEADER_SIZE);
865
866 tf->which_block = whichBlock;
867 tf->which_event = 1;
868 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
869 tf->cur_heart_beat_number = 0;
870
871 getCyclePerNsec(tf);
872
873 // tf->current_event_time = getEventTime(tf);
874
875 return 0;
876 }
877
878 /*****************************************************************************
879 *Function name
880 * updateTracefile : reinitialize the info of the block which is already
881 * in the buffer
882 *Input params
883 * tf : tracefile
884 ****************************************************************************/
885
886 void updateTracefile(LttTracefile * tf)
887 {
888 tf->which_event = 1;
889 tf->cur_event_pos = tf->buffer;
890 // tf->current_event_time = getEventTime(tf);
891 tf->current_event_time.tv_sec = 0;
892 tf->current_event_time.tv_nsec = 0;
893 tf->cur_heart_beat_number = 0;
894
895 tf->prev_event_time.tv_sec = 0;
896 tf->prev_event_time.tv_nsec = 0;
897 }
898
899 /*****************************************************************************
900 *Function name
901 * skipEvent : go to the next event, update the fields of the current event
902 *Input params
903 * t : tracefile
904 *return value
905 * 0 : success
906 * EINVAL : lseek fail
907 * EIO : can not read from the file
908 * ENOMEM : end of file
909 * ENOENT : last event
910 * ERANGE : event id is out of range
911 ****************************************************************************/
912
913 int skipEvent(LttTracefile * t)
914 {
915 int evId, err;
916 void * evData;
917 LttEventType * evT;
918 LttField * rootFld;
919 static int evCount = 0;
920
921 if(evCount){
922 if(t->which_block == t->block_number &&
923 evCount == t->which_event){
924 return ENOMEM;
925 }else evCount = 0;
926 }
927
928 evId = (int)(*(uint16_t *)(t->cur_event_pos));
929 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
930
931 //regard BLOCK_START, END and HEARTBEAT as special case, there are buildin events
932 if(evId != TRACE_BLOCK_START && evId != TRACE_BLOCK_END && evId != TRACE_TIME_HEARTBEAT){
933 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
934
935 if(evT) rootFld = evT->root_field;
936 else return ERANGE;
937
938 if(rootFld){
939 //event has string/sequence or the last event is not the same event
940 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
941 && rootFld->field_fixed == 0){
942 setFieldsOffset(t, evT, evData, t->trace);
943 }
944 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
945 }else t->cur_event_pos += EVENT_HEADER_SIZE;
946
947 evT->latest_block = t->which_block;
948 evT->latest_event = t->which_event;
949 }else{
950 if(evId == TRACE_BLOCK_START || evId == TRACE_BLOCK_END){
951 t->cur_event_pos += sizeof(BlockStart) + EVENT_HEADER_SIZE;
952 }else{
953 t->cur_event_pos += sizeof(TimeHeartbeat) + EVENT_HEADER_SIZE;
954 }
955 }
956
957 //the next event is in the next block
958 if(evId == TRACE_BLOCK_END){
959 if(t->which_block == t->block_number){
960 t->which_event++;
961 evCount = t->which_event;
962 return ENOENT;
963 }
964 err = readBlock(t, t->which_block + 1);
965 if(err) return err;
966 }else{
967 t->which_event++;
968 }
969
970 return 0;
971 }
972
973 /*****************************************************************************
974 *Function name
975 * getCyclePerNsec : calculate cycles per nsec for current block
976 *Input Params
977 * t : tracefile
978 ****************************************************************************/
979
980 void getCyclePerNsec(LttTracefile * t)
981 {
982 LttTime lBufTotalTime; /* Total time for this buffer */
983 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
984 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
985
986 /* Calculate the total time for this buffer */
987 TimeSub(lBufTotalTime,t->a_block_end->time, t->a_block_start->time);
988
989 /* Calculate the total cycles for this bufffer */
990 lBufTotalCycle = t->a_block_end->cycle_count
991 - t->a_block_start->cycle_count;
992
993 /* Convert the total time to nsecs */
994 lBufTotalNSec = lBufTotalTime.tv_sec * 1000000000 + lBufTotalTime.tv_nsec;
995
996 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
997 }
998
999 /****************************************************************************
1000 *Function name
1001 * getEventTime : obtain the time of an event
1002 *Input params
1003 * tf : tracefile
1004 *Return value
1005 * LttTime : the time of the event
1006 ****************************************************************************/
1007
1008 LttTime getEventTime(LttTracefile * tf)
1009 {
1010 LttTime time;
1011 LttCycleCount cycle_count; // cycle count for the current event
1012 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1013 double lEventNSec; // Total usecs from start for event
1014 LttTime lTimeOffset; // Time offset in struct LttTime
1015 uint16_t evId;
1016
1017 evId = *(uint16_t*)tf->cur_event_pos;
1018 if(evId == TRACE_BLOCK_START)
1019 return tf->a_block_start->time;
1020 else if(evId == TRACE_BLOCK_END)
1021 return tf->a_block_end->time;
1022
1023
1024 // Calculate total time in cycles from start of buffer for this event
1025 cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE);
1026 if(tf->cur_heart_beat_number)
1027 cycle_count += ((uint64_t)1)<<32 * tf->cur_heart_beat_number;
1028 lEventTotalCycle = cycle_count - tf->a_block_start->cycle_count;
1029
1030 // Convert it to nsecs
1031 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
1032
1033 // Determine offset in struct LttTime
1034 lTimeOffset.tv_nsec = (long)lEventNSec % 1000000000;
1035 lTimeOffset.tv_sec = (long)lEventNSec / 1000000000;
1036
1037 TimeAdd(time, tf->a_block_start->time, lTimeOffset);
1038
1039 return time;
1040 }
1041
1042 /*****************************************************************************
1043 *Function name
1044 * setFieldsOffset : set offset of the fields
1045 *Input params
1046 * tracefile : opened trace file
1047 * evT : the event type
1048 * evD : event data, it may be NULL
1049 ****************************************************************************/
1050
1051 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1052 {
1053 LttField * rootFld = evT->root_field;
1054 // rootFld->base_address = evD;
1055
1056 if(rootFld)
1057 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1058 }
1059
1060 /*****************************************************************************
1061 *Function name
1062 * getFieldtypeSize: get the size of the field type (primitive type)
1063 *Input params
1064 * tracefile : opened trace file
1065 * evT : event type
1066 * offsetRoot : offset from the root
1067 * offsetParent : offset from the parrent
1068 * fld : field
1069 * evD : event data, it may be NULL
1070 *Return value
1071 * int : size of the field
1072 ****************************************************************************/
1073
1074 int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
1075 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
1076 {
1077 int size, size1, element_number, i, offset1, offset2;
1078 LttType * type = fld->field_type;
1079
1080 if(t){
1081 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1082 return fld->field_size;
1083 }
1084 }
1085
1086 if(fld->field_fixed == 1){
1087 if(fld == evT->root_field) return fld->field_size;
1088 }
1089
1090 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1091 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1092 if(fld->field_fixed == -1){
1093 size = (int) ltt_type_size(trace, type);
1094 fld->field_fixed = 1;
1095 }else size = fld->field_size;
1096
1097 }else if(type->type_class == LTT_ARRAY){
1098 element_number = (int) type->element_number;
1099 if(fld->field_fixed == -1){
1100 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1101 if(size == 0){ //has string or sequence
1102 fld->field_fixed = 0;
1103 }else{
1104 fld->field_fixed = 1;
1105 size *= element_number;
1106 }
1107 }else if(fld->field_fixed == 0){// has string or sequence
1108 size = 0;
1109 for(i=0;i<element_number;i++){
1110 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1111 fld->child[0], evD+size, trace);
1112 }
1113 }else size = fld->field_size;
1114
1115 }else if(type->type_class == LTT_SEQUENCE){
1116 size1 = (int) ltt_type_size(trace, type);
1117 if(fld->field_fixed == -1){
1118 fld->field_fixed = 0;
1119 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1120 fld->element_size = size;
1121 }else{//0: sequence
1122 element_number = getIntNumber(size1,evD);
1123 type->element_number = element_number;
1124 if(fld->element_size > 0){
1125 size = element_number * fld->element_size;
1126 }else{//sequence has string or sequence
1127 size = 0;
1128 for(i=0;i<element_number;i++){
1129 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1130 fld->child[0], evD+size+size1, trace);
1131 }
1132 }
1133 size += size1;
1134 }
1135
1136 }else if(type->type_class == LTT_STRING){
1137 size = 0;
1138 if(fld->field_fixed == -1){
1139 fld->field_fixed = 0;
1140 }else{//0: string
1141 size = strlen((char*)evD) + 1; //include end : '\0'
1142 }
1143
1144 }else if(type->type_class == LTT_STRUCT){
1145 element_number = (int) type->element_number;
1146 size = 0;
1147 if(fld->field_fixed == -1){
1148 offset1 = offsetRoot;
1149 offset2 = 0;
1150 for(i=0;i<element_number;i++){
1151 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
1152 if(size1 > 0 && size >= 0){
1153 size += size1;
1154 if(offset1 >= 0) offset1 += size1;
1155 offset2 += size1;
1156 }else{
1157 size = -1;
1158 offset1 = -1;
1159 offset2 = -1;
1160 }
1161 }
1162 if(size == -1){
1163 fld->field_fixed = 0;
1164 size = 0;
1165 }else fld->field_fixed = 1;
1166 }else if(fld->field_fixed == 0){
1167 offset1 = offsetRoot;
1168 offset2 = 0;
1169 for(i=0;i<element_number;i++){
1170 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
1171 offset1 += size;
1172 offset2 += size;
1173 }
1174 size = offset2;
1175 }else size = fld->field_size;
1176 }
1177
1178 fld->offset_root = offsetRoot;
1179 fld->offset_parent = offsetParent;
1180 if(!evD){
1181 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1182 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1183 }
1184 fld->field_size = size;
1185
1186 return size;
1187 }
1188
1189 /*****************************************************************************
1190 *Function name
1191 * timecmp : compare two time
1192 *Input params
1193 * t1 : first time
1194 * t2 : second time
1195 *Return value
1196 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1197 ****************************************************************************/
1198
1199 int timecmp(LttTime * t1, LttTime * t2)
1200 {
1201 LttTime T;
1202 TimeSub(T, *t1, *t2);
1203 if(T.tv_sec == 0 && T.tv_nsec == 0) return 0;
1204 else if(T.tv_sec > 0 || (T.tv_sec==0 && T.tv_nsec > 0)) return 1;
1205 else return -1;
1206 }
1207
1208 /*****************************************************************************
1209 *Function name
1210 * getIntNumber : get an integer number
1211 *Input params
1212 * size : the size of the integer
1213 * evD : the event data
1214 *Return value
1215 * int : an integer
1216 ****************************************************************************/
1217
1218 int getIntNumber(int size, void *evD)
1219 {
1220 int64_t i;
1221 if(size == 1) i = *(int8_t *)evD;
1222 else if(size == 2) i = *(int16_t *)evD;
1223 else if(size == 4) i = *(int32_t *)evD;
1224 else if(size == 8) i = *(int64_t *)evD;
1225
1226 return (int) i;
1227 }
1228
1229 /*****************************************************************************
1230 *Function name
1231 * getDataEndianType : get the data type size and endian type of the local
1232 * machine
1233 *Input params
1234 * size : size of data type
1235 * endian : endian type, little or big
1236 ****************************************************************************/
1237
1238 void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
1239 {
1240 int i = 1;
1241 char c = (char) i;
1242 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1243
1244 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1245 else *endian = LTT_BIG_ENDIAN;
1246
1247 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1248 *size = LTT_LP32;
1249 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1250 *size = LTT_ILP32;
1251 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1252 *size = LTT_LP64;
1253 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1254 *size = LTT_ILP64;
1255 else *size = LTT_UNKNOWN;
1256 }
1257
This page took 0.059477 seconds and 5 git commands to generate.