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