git-svn-id: http://ltt.polymtl.ca/svn@147 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, TRUE);
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, TRUE);
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, TRUE);
539
540 g_free(t);
541
542 g_blow_chunks();
543 }
544
545
546 /*****************************************************************************
547 *Get the system description of the trace
548 ****************************************************************************/
549
550 LttSystemDescription *ltt_trace_system_description(LttTrace *t)
551 {
552 return t->system_description;
553 }
554
555 /*****************************************************************************
556 * The following functions discover the facilities of the trace
557 ****************************************************************************/
558
559 unsigned ltt_trace_facility_number(LttTrace *t)
560 {
561 return (unsigned)(t->facility_number);
562 }
563
564 LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
565 {
566 return (LttFacility*)g_ptr_array_index(t->facilities, i);
567 }
568
569 /*****************************************************************************
570 *Function name
571 * ltt_trace_facility_find : find facilities in the trace
572 *Input params
573 * t : the trace
574 * name : facility name
575 *Output params
576 * position : position of the facility in the trace
577 *Return value
578 * : the number of facilities
579 ****************************************************************************/
580
581 unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
582 {
583 int i, count=0;
584 LttFacility * f;
585 for(i=0;i=t->facility_number;i++){
586 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
587 if(strcmp(f->name,name)==0){
588 count++;
589 if(count==1) *position = i;
590 }else{
591 if(count) break;
592 }
593 }
594 return count;
595 }
596
597 /*****************************************************************************
598 * Functions to discover all the event types in the trace
599 ****************************************************************************/
600
601 unsigned ltt_trace_eventtype_number(LttTrace *t)
602 {
603 int i;
604 unsigned count = 0;
605 LttFacility * f;
606 for(i=0;i=t->facility_number;i++){
607 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
608 count += f->event_number;
609 }
610 return count;
611 }
612
613 LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
614 {
615 LttFacility * facility;
616 int i;
617 for(i=0;i<trace->facility_number;i++){
618 facility = (LttFacility*) g_ptr_array_index(trace->facilities,i);
619 if(id >= facility->base_id &&
620 id < facility->base_id + facility->event_number)
621 break;
622 }
623 if(i==trace->facility_number) return NULL;
624 else return facility;
625 }
626
627 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
628 {
629 LttFacility * f;
630 f = ltt_trace_facility_by_id(t,evId);
631 if(!f) return NULL;
632 return f->events[evId - f->base_id];
633 }
634
635 /*****************************************************************************
636 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
637 *the maximum number of CPU in the system. When the number of CPU installed
638 *is less than the maximum, some positions are unused. There are also a
639 *number of "control" tracefiles (facilities, interrupts...).
640 ****************************************************************************/
641 unsigned ltt_trace_control_tracefile_number(LttTrace *t)
642 {
643 return t->control_tracefile_number;
644 }
645
646 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
647 {
648 return t->per_cpu_tracefile_number;
649 }
650
651 /*****************************************************************************
652 *It is possible to search for the tracefiles by name or by CPU position.
653 *The index within the tracefiles of the same type is returned if found
654 *and a negative value otherwise.
655 ****************************************************************************/
656
657 int ltt_trace_control_tracefile_find(LttTrace *t, char *name)
658 {
659 LttTracefile * tracefile;
660 int i;
661 for(i=0;i<t->control_tracefile_number;i++){
662 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
663 if(strcmp(tracefile->name, name)==0)break;
664 }
665 if(i == t->control_tracefile_number) return -1;
666 return i;
667 }
668
669 int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i)
670 {
671 LttTracefile * tracefile;
672 int j, name;
673 for(j=0;j<t->per_cpu_tracefile_number;j++){
674 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j);
675 name = atoi(tracefile->name);
676 if(name == (int)i)break;
677 }
678 if(j == t->per_cpu_tracefile_number) return -1;
679 return j;
680 }
681
682 /*****************************************************************************
683 *Get a specific tracefile
684 ****************************************************************************/
685
686 LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
687 {
688 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
689 }
690
691 LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
692 {
693 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
694 }
695
696 /*****************************************************************************
697 *Get the name of a tracefile
698 ****************************************************************************/
699
700 char *ltt_tracefile_name(LttTracefile *tf)
701 {
702 return tf->name;
703 }
704
705 /*****************************************************************************
706 *Function name
707 * ltt_tracefile_seek_time: seek to the first event of the trace with time
708 * larger or equal to time
709 *Input params
710 * t : tracefile
711 * time : criteria of the time
712 ****************************************************************************/
713
714 void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
715 {
716 int err;
717 LttTime lttTime;
718 int headTime = timecmp(&(t->a_block_start->time), &time);
719 int tailTime = timecmp(&(t->a_block_end->time), &time);
720 LttEvent * ev;
721
722 if(headTime < 0 && tailTime > 0){
723 lttTime = getEventTime(t);
724 err = timecmp(&lttTime, &time);
725 if(err > 0){
726 if(t->which_event==2 || timecmp(&t->prev_event_time,&time)<0){
727 return;
728 }else{
729 updateTracefile(t);
730 return ltt_tracefile_seek_time(t, time);
731 }
732 }else if(err < 0){
733 while(1){
734 ev = ltt_tracefile_read(t);
735 if(ev == NULL){
736 g_print("End of file\n");
737 return;
738 }
739 g_free(ev);
740 lttTime = getEventTime(t);
741 err = timecmp(&lttTime, &time);
742 if(err >= 0)return;
743 }
744 }else return;
745 }else if(headTime > 0){
746 if(t->which_block == 1){
747 updateTracefile(t);
748 }else{
749 if(timecmp(&(t->prev_block_end_time),&time) >= 0 ){
750 err=readBlock(t,t->which_block-1);
751 if(err) g_error("Can not read tracefile: %s\n", t->name);
752 return ltt_tracefile_seek_time(t, time) ;
753 }else{
754 updateTracefile(t);
755 }
756 }
757 }else if(tailTime < 0){
758 if(t->which_block != t->block_number){
759 err=readBlock(t,t->which_block+1);
760 if(err) g_error("Can not read tracefile: %s\n", t->name);
761 }else {
762 g_print("End of file\n");
763 return;
764 }
765 if(tailTime < 0) return ltt_tracefile_seek_time(t, time);
766 }else if(headTime == 0){
767 updateTracefile(t);
768 }else if(tailTime == 0){
769 t->cur_event_pos = t->a_block_end - EVENT_HEADER_SIZE;
770 t->current_event_time = time;
771 t->cur_heart_beat_number = 0;
772 t->prev_event_time.tv_sec = 0;
773 t->prev_event_time.tv_nsec = 0;
774 return;
775 }
776 }
777
778 /*****************************************************************************
779 *Function name
780 * ltt_tracefile_read : read the current event, set the pointer to the next
781 *Input params
782 * t : tracefile
783 *Return value
784 * LttEvent * : an event to be processed
785 ****************************************************************************/
786
787 LttEvent *ltt_tracefile_read(LttTracefile *t)
788 {
789 LttEvent * lttEvent = (LttEvent *)g_new(LttEvent, 1);
790 int err;
791
792 lttEvent->event_id = (int)(*(uint16_t *)(t->cur_event_pos));
793 if(lttEvent->event_id == TRACE_TIME_HEARTBEAT)
794 t->cur_heart_beat_number++;
795
796 t->prev_event_time = t->current_event_time;
797 // t->current_event_time = getEventTime(t);
798
799 lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE);
800 lttEvent->event_time = t->current_event_time;
801
802 lttEvent->event_cycle_count = ((uint64_t)1)<<32 * t->cur_heart_beat_number
803 + lttEvent->time_delta;
804
805 lttEvent->tracefile = t;
806 lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE;
807
808 //update the fields of the current event and go to the next event
809 err = skipEvent(t);
810 if(err == ENOMEM){
811 g_free(lttEvent);
812 return NULL;
813 }
814 if(err == ENOENT) return lttEvent;
815 if(err == ERANGE) g_error("event id is out of range\n");
816 if(err)g_error("Can not read tracefile\n");
817
818 return lttEvent;
819 }
820
821 /****************************************************************************
822 *Function name
823 * readFile : wrap function to read from a file
824 *Input Params
825 * fd : file descriptor
826 * buf : buf to contain the content
827 * size : number of bytes to be read
828 * mesg : message to be printed if some thing goes wrong
829 *return value
830 * 0 : success
831 * EIO : can not read from the file
832 ****************************************************************************/
833
834 int readFile(int fd, void * buf, size_t size, char * mesg)
835 {
836 ssize_t nbBytes;
837 nbBytes = read(fd, buf, size);
838 if(nbBytes != size){
839 printf("%s\n",mesg);
840 return EIO;
841 }
842 return 0;
843 }
844
845 /****************************************************************************
846 *Function name
847 * readBlock : read a block from the file
848 *Input Params
849 * lttdes : ltt trace file
850 * whichBlock : the block which will be read
851 *return value
852 * 0 : success
853 * EINVAL : lseek fail
854 * EIO : can not read from the file
855 ****************************************************************************/
856
857 int readBlock(LttTracefile * tf, int whichBlock)
858 {
859 off_t nbBytes;
860 uint32_t lostSize;
861
862 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
863 tf->prev_block_end_time = tf->a_block_end->time;
864 tf->prev_event_time = tf->a_block_end->time;
865 }else{
866 tf->prev_block_end_time.tv_sec = 0;
867 tf->prev_block_end_time.tv_nsec = 0;
868 tf->prev_event_time.tv_sec = 0;
869 tf->prev_event_time.tv_nsec = 0;
870 }
871
872 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
873 if(nbBytes == -1) return EINVAL;
874
875 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
876 return EIO;
877
878 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
879 lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t));
880 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
881 lostSize + EVENT_HEADER_SIZE);
882
883 tf->which_block = whichBlock;
884 tf->which_event = 1;
885 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
886 tf->cur_heart_beat_number = 0;
887
888 getCyclePerNsec(tf);
889
890 tf->current_event_time = getEventTime(tf);
891
892 return 0;
893 }
894
895 /*****************************************************************************
896 *Function name
897 * updateTracefile : reinitialize the info of the block which is already
898 * in the buffer
899 *Input params
900 * tf : tracefile
901 ****************************************************************************/
902
903 void updateTracefile(LttTracefile * tf)
904 {
905 tf->which_event = 1;
906 tf->cur_event_pos = tf->buffer;
907 tf->current_event_time = getEventTime(tf);
908 tf->cur_heart_beat_number = 0;
909
910 tf->prev_event_time.tv_sec = 0;
911 tf->prev_event_time.tv_nsec = 0;
912 }
913
914 /*****************************************************************************
915 *Function name
916 * skipEvent : go to the next event, update the fields of the current event
917 *Input params
918 * t : tracefile
919 *return value
920 * 0 : success
921 * EINVAL : lseek fail
922 * EIO : can not read from the file
923 * ENOMEM : end of file
924 * ENOENT : last event
925 * ERANGE : event id is out of range
926 ****************************************************************************/
927
928 int skipEvent(LttTracefile * t)
929 {
930 int evId, err;
931 void * evData;
932 LttEventType * evT;
933 LttField * rootFld;
934 static int evCount = 0;
935
936 if(evCount){
937 if(t->which_block == t->block_number &&
938 evCount == t->which_event){
939 return ENOMEM;
940 }else evCount = 0;
941 }
942
943 evId = (int)(*(uint16_t *)(t->cur_event_pos));
944 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
945
946 //regard BLOCK_START, END and HEARTBEAT as special case, there are buildin events
947 if(evId != TRACE_BLOCK_START && evId != TRACE_BLOCK_END && evId != TRACE_TIME_HEARTBEAT){
948 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
949
950 if(evT) rootFld = evT->root_field;
951 else return ERANGE;
952
953 if(rootFld){
954 //event has string/sequence or the last event is not the same event
955 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
956 && rootFld->field_fixed == 0){
957 setFieldsOffset(t, evT, evData, t->trace);
958 }
959 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
960 }else t->cur_event_pos += EVENT_HEADER_SIZE;
961
962 evT->latest_block = t->which_block;
963 evT->latest_event = t->which_event;
964 }else{
965 if(evId == TRACE_BLOCK_START || evId == TRACE_BLOCK_END){
966 t->cur_event_pos += sizeof(BlockStart) + EVENT_HEADER_SIZE;
967 }else{
968 t->cur_event_pos += sizeof(TimeHeartbeat) + EVENT_HEADER_SIZE;
969 }
970 }
971
972 //the next event is in the next block
973 if(evId == TRACE_BLOCK_END){
974 if(t->which_block == t->block_number){
975 t->which_event++;
976 evCount = t->which_event;
977 return ENOENT;
978 }
979 err = readBlock(t, t->which_block + 1);
980 if(err) return err;
981 }else{
982 t->which_event++;
983 t->current_event_time = getEventTime(t);
984 }
985
986 return 0;
987 }
988
989 /*****************************************************************************
990 *Function name
991 * getCyclePerNsec : calculate cycles per nsec for current block
992 *Input Params
993 * t : tracefile
994 ****************************************************************************/
995
996 void getCyclePerNsec(LttTracefile * t)
997 {
998 LttTime lBufTotalTime; /* Total time for this buffer */
999 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
1000 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
1001
1002 /* Calculate the total time for this buffer */
1003 TimeSub(lBufTotalTime,t->a_block_end->time, t->a_block_start->time);
1004
1005 /* Calculate the total cycles for this bufffer */
1006 lBufTotalCycle = t->a_block_end->cycle_count
1007 - t->a_block_start->cycle_count;
1008
1009 /* Convert the total time to nsecs */
1010 lBufTotalNSec = lBufTotalTime.tv_sec * 1000000000 + lBufTotalTime.tv_nsec;
1011
1012 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
1013 }
1014
1015 /****************************************************************************
1016 *Function name
1017 * getEventTime : obtain the time of an event
1018 *Input params
1019 * tf : tracefile
1020 *Return value
1021 * LttTime : the time of the event
1022 ****************************************************************************/
1023
1024 LttTime getEventTime(LttTracefile * tf)
1025 {
1026 LttTime time;
1027 LttCycleCount cycle_count; // cycle count for the current event
1028 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1029 double lEventNSec; // Total usecs from start for event
1030 LttTime lTimeOffset; // Time offset in struct LttTime
1031 uint16_t evId;
1032
1033 evId = *(uint16_t*)tf->cur_event_pos;
1034 if(evId == TRACE_BLOCK_START)
1035 return tf->a_block_start->time;
1036 else if(evId == TRACE_BLOCK_END)
1037 return tf->a_block_end->time;
1038
1039
1040 // Calculate total time in cycles from start of buffer for this event
1041 cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE);
1042 if(tf->cur_heart_beat_number)
1043 cycle_count += ((uint64_t)1)<<32 * tf->cur_heart_beat_number;
1044 lEventTotalCycle = cycle_count - tf->a_block_start->cycle_count;
1045
1046 // Convert it to nsecs
1047 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
1048
1049 // Determine offset in struct LttTime
1050 lTimeOffset.tv_nsec = (long)lEventNSec % 1000000000;
1051 lTimeOffset.tv_sec = (long)lEventNSec / 1000000000;
1052
1053 TimeAdd(time, tf->a_block_start->time, lTimeOffset);
1054
1055 return time;
1056 }
1057
1058 /*****************************************************************************
1059 *Function name
1060 * setFieldsOffset : set offset of the fields
1061 *Input params
1062 * tracefile : opened trace file
1063 * evT : the event type
1064 * evD : event data, it may be NULL
1065 ****************************************************************************/
1066
1067 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1068 {
1069 LttField * rootFld = evT->root_field;
1070 // rootFld->base_address = evD;
1071
1072 if(rootFld)
1073 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1074 }
1075
1076 /*****************************************************************************
1077 *Function name
1078 * getFieldtypeSize: get the size of the field type (primitive type)
1079 *Input params
1080 * tracefile : opened trace file
1081 * evT : event type
1082 * offsetRoot : offset from the root
1083 * offsetParent : offset from the parrent
1084 * fld : field
1085 * evD : event data, it may be NULL
1086 *Return value
1087 * int : size of the field
1088 ****************************************************************************/
1089
1090 int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
1091 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
1092 {
1093 int size, size1, element_number, i, offset1, offset2;
1094 LttType * type = fld->field_type;
1095
1096 if(t){
1097 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1098 return fld->field_size;
1099 }
1100 }
1101
1102 if(fld->field_fixed == 1){
1103 if(fld == evT->root_field) return fld->field_size;
1104 }
1105
1106 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1107 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1108 if(fld->field_fixed == -1){
1109 size = (int) ltt_type_size(trace, type);
1110 fld->field_fixed = 1;
1111 }else size = fld->field_size;
1112
1113 }else if(type->type_class == LTT_ARRAY){
1114 element_number = (int) type->element_number;
1115 if(fld->field_fixed == -1){
1116 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1117 if(size == 0){ //has string or sequence
1118 fld->field_fixed = 0;
1119 }else{
1120 fld->field_fixed = 1;
1121 size *= element_number;
1122 }
1123 }else if(fld->field_fixed == 0){// has string or sequence
1124 size = 0;
1125 for(i=0;i<element_number;i++){
1126 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1127 fld->child[0], evD+size, trace);
1128 }
1129 }else size = fld->field_size;
1130
1131 }else if(type->type_class == LTT_SEQUENCE){
1132 size1 = (int) ltt_type_size(trace, type);
1133 if(fld->field_fixed == -1){
1134 fld->field_fixed = 0;
1135 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1136 fld->element_size = size;
1137 }else{//0: sequence
1138 element_number = getIntNumber(size1,evD);
1139 type->element_number = element_number;
1140 if(fld->element_size > 0){
1141 size = element_number * fld->element_size;
1142 }else{//sequence has string or sequence
1143 size = 0;
1144 for(i=0;i<element_number;i++){
1145 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1146 fld->child[0], evD+size+size1, trace);
1147 }
1148 }
1149 size += size1;
1150 }
1151
1152 }else if(type->type_class == LTT_STRING){
1153 size = 0;
1154 if(fld->field_fixed == -1){
1155 fld->field_fixed = 0;
1156 }else{//0: string
1157 size = strlen((char*)evD) + 1; //include end : '\0'
1158 }
1159
1160 }else if(type->type_class == LTT_STRUCT){
1161 element_number = (int) type->element_number;
1162 size = 0;
1163 if(fld->field_fixed == -1){
1164 offset1 = offsetRoot;
1165 offset2 = 0;
1166 for(i=0;i<element_number;i++){
1167 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
1168 if(size1 > 0 && size >= 0){
1169 size += size1;
1170 if(offset1 >= 0) offset1 += size1;
1171 offset2 += size1;
1172 }else{
1173 size = -1;
1174 offset1 = -1;
1175 offset2 = -1;
1176 }
1177 }
1178 if(size == -1){
1179 fld->field_fixed = 0;
1180 size = 0;
1181 }else fld->field_fixed = 1;
1182 }else if(fld->field_fixed == 0){
1183 offset1 = offsetRoot;
1184 offset2 = 0;
1185 for(i=0;i<element_number;i++){
1186 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
1187 offset1 += size;
1188 offset2 += size;
1189 }
1190 size = offset2;
1191 }else size = fld->field_size;
1192 }
1193
1194 fld->offset_root = offsetRoot;
1195 fld->offset_parent = offsetParent;
1196 if(!evD){
1197 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1198 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1199 }
1200 fld->field_size = size;
1201
1202 return size;
1203 }
1204
1205 /*****************************************************************************
1206 *Function name
1207 * timecmp : compare two time
1208 *Input params
1209 * t1 : first time
1210 * t2 : second time
1211 *Return value
1212 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1213 ****************************************************************************/
1214
1215 int timecmp(LttTime * t1, LttTime * t2)
1216 {
1217 LttTime T;
1218 TimeSub(T, *t1, *t2);
1219 if(T.tv_sec == 0 && T.tv_nsec == 0) return 0;
1220 else if(T.tv_sec > 0 || (T.tv_sec==0 && T.tv_nsec > 0)) return 1;
1221 else return -1;
1222 }
1223
1224 /*****************************************************************************
1225 *Function name
1226 * getIntNumber : get an integer number
1227 *Input params
1228 * size : the size of the integer
1229 * evD : the event data
1230 *Return value
1231 * int : an integer
1232 ****************************************************************************/
1233
1234 int getIntNumber(int size, void *evD)
1235 {
1236 int64_t i;
1237 if(size == 1) i = *(int8_t *)evD;
1238 else if(size == 2) i = *(int16_t *)evD;
1239 else if(size == 4) i = *(int32_t *)evD;
1240 else if(size == 8) i = *(int64_t *)evD;
1241
1242 return (int) i;
1243 }
1244
1245 /*****************************************************************************
1246 *Function name
1247 * getDataEndianType : get the data type size and endian type of the local
1248 * machine
1249 *Input params
1250 * size : size of data type
1251 * endian : endian type, little or big
1252 ****************************************************************************/
1253
1254 void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
1255 {
1256 int i = 1;
1257 char c = (char) i;
1258 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1259
1260 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1261 else *endian = LTT_BIG_ENDIAN;
1262
1263 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1264 *size = LTT_LP32;
1265 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1266 *size = LTT_ILP32;
1267 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1268 *size = LTT_LP64;
1269 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1270 *size = LTT_ILP64;
1271 else *size = LTT_UNKNOWN;
1272 }
1273
This page took 0.0623 seconds and 5 git commands to generate.