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