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