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