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