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