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