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