needed private information not accessible otherwise
[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 */
963b5f2d 16void setFieldsOffset(LttTracefile * t, LttEventType *evT, void *evD);
6cd62ccf 17
18/* get the size of the field type according to the archtecture's
19 size and endian type(info of the archecture) */
963b5f2d 20int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
21 int offsetParent, LttField * fld, void * evD );
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){
153 evId = *(uint16_t*)tf->cur_event_pos;
154 if(evId == TRACE_FACILITY_LOAD){
155 pos = tf->cur_event_pos + EVENT_HEADER_SIZE;
156 fLoad.name = (char*)pos;
157 fLoad.checksum = *(LttChecksum*)(pos + sizeof(pos));
158 fLoad.base_code = *(uint32_t*)(pos + sizeof(pos) + sizeof(LttChecksum));
159
160 for(i=0;i<t->facility_number;i++){
161 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
162 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
163 f->base_id = fLoad.base_code;
164 break;
165 }
166 }
167 if(i==t->facility_number)
168 g_error("Facility: %s, checksum: %d is not founded\n",
169 fLoad.name,fLoad.checksum);
170
171 ev = ltt_tracefile_read(tf); //get next event
172 if(!ev) break; //end of tracefile
173 }else if(evId == TRACE_BLOCK_END){
174 //can only reach here if it is the first event
175 g_error("Facilities does not contain any facility_load event\n");
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;
357 LttTracefile * tracefile;
358
359 dir = opendir(eventdefs);
360 if(!dir) g_error("Can not open directory: %s\n", eventdefs);
361
362 while((entry = readdir(dir)) != NULL){
363 ptr = &entry->d_name[strlen(entry->d_name)-4];
364 if(strcmp(ptr,".xml") != 0) continue;
365 ltt_facility_open(t,entry->d_name);
366 }
367 closedir(dir);
368
369 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,0);
370 for(j=0;j<t->facility_number;j++){
371 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
372 for(i=0; i<f->event_number; i++){
373 et = f->events[i];
374 setFieldsOffset(tracefile, et, NULL);
375 }
376 }
377}
6cd62ccf 378
963b5f2d 379void getControlFileInfo(LttTrace *t, char* control)
6cd62ccf 380{
963b5f2d 381 DIR * dir;
382 struct dirent *entry;
383
384 dir = opendir(control);
385 if(!dir) g_error("Can not open directory: %s\n", control);
386
387 while((entry = readdir(dir)) != NULL){
388 if(strcmp(entry->d_name,"facilities") != 0 ||
389 strcmp(entry->d_name,"interrupts") != 0 ||
390 strcmp(entry->d_name,"processes") != 0) continue;
391
392 ltt_tracefile_open_control(t,entry->d_name);
393 }
394 closedir(dir);
6cd62ccf 395}
396
963b5f2d 397void getCpuFileInfo(LttTrace *t, char* cpu)
6cd62ccf 398{
963b5f2d 399 DIR * dir;
400 struct dirent *entry;
401
402 dir = opendir(cpu);
403 if(!dir) g_error("Can not open directory: %s\n", cpu);
404
405 while((entry = readdir(dir)) != NULL){
406 if(strcmp(entry->d_name,".") != 0 ||
407 strcmp(entry->d_name,"..") != 0 ){
408 ltt_tracefile_open_cpu(t,entry->d_name);
409 }else continue;
410 }
411 closedir(dir);
6cd62ccf 412}
413
963b5f2d 414/*****************************************************************************
415 *A trace is specified as a pathname to the directory containing all the
416 *associated data (control tracefiles, per cpu tracefiles, event
417 *descriptions...).
418 *
419 *When a trace is closed, all the associated facilities, types and fields
420 *are released as well.
421 ****************************************************************************/
6cd62ccf 422
963b5f2d 423LttTrace *ltt_trace_open(char *pathname)
6cd62ccf 424{
963b5f2d 425 LttTrace * t;
426 LttSystemDescription * sys_description;
427 char eventdefs[DIR_NAME_SIZE];
428 char info[DIR_NAME_SIZE];
429 char control[DIR_NAME_SIZE];
430 char cpu[DIR_NAME_SIZE];
431 char tmp[DIR_NAME_SIZE];
432 gboolean has_slash = FALSE;
433
434 //establish the pathname to different directories
435 if(pathname[strlen(pathname)-1] == '/')has_slash = TRUE;
436 strcpy(eventdefs,pathname);
437 if(!has_slash)strcat(eventdefs,"/");
438 strcat(eventdefs,"eventdefs/");
439
440 strcpy(info,pathname);
441 if(!has_slash)strcat(info,"/");
442 strcat(info,"info/");
443
444 strcpy(control,pathname);
445 if(!has_slash)strcat(control,"/");
446 strcat(control,"control/");
447
448 strcpy(cpu,pathname);
449 if(!has_slash)strcat(cpu,"/");
450 strcat(cpu,"cpu/");
451
452 //new trace
453 t = g_new(LttTrace, 1);
454 sys_description = g_new(LttSystemDescription, 1);
455 t->pathname = g_strdup(pathname);
456 t->facility_number = 0;
457 t->control_tracefile_number = 0;
458 t->per_cpu_tracefile_number = 0;
459 t->system_description = sys_description;
460 t->control_tracefiles = g_ptr_array_new();
461 t->per_cpu_tracefiles = g_ptr_array_new();
462 t->facilities = g_ptr_array_new();
463 getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
464
465 //get system description
466 strcpy(tmp,info);
467 strcat(tmp,"system.xml");
468 getSystemInfo(sys_description, tmp);
469
470 //get control tracefile info
471 getControlFileInfo(t,control);
472
473 //get cpu tracefile info
474 getCpuFileInfo(t,cpu);
475
476 //get facilities info
477 getFacilityInfo(t,eventdefs);
478
479 return t;
6cd62ccf 480}
481
963b5f2d 482void ltt_trace_close(LttTrace *t)
6cd62ccf 483{
963b5f2d 484 int i;
485 LttTracefile * tf;
486 LttFacility * f;
487
488 g_free(t->pathname);
489
490 //free system_description
491 g_free(t->system_description->description);
492 g_free(t->system_description->node_name);
493 g_free(t->system_description->domain_name);
494 g_free(t->system_description->kernel_name);
495 g_free(t->system_description->kernel_release);
496 g_free(t->system_description->kernel_version);
497 g_free(t->system_description->machine);
498 g_free(t->system_description->processor);
499 g_free(t->system_description->hardware_platform);
500 g_free(t->system_description->operating_system);
501 g_free(t->system_description);
502
503 //free control_tracefiles
504 for(i=0;i<t->control_tracefile_number;i++){
505 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
506 ltt_tracefile_close(tf);
507 }
508 g_ptr_array_free(t->control_tracefiles, FALSE);
509
510 //free per_cpu_tracefiles
511 for(i=0;i<t->per_cpu_tracefile_number;i++){
512 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
513 ltt_tracefile_close(tf);
514 }
515 g_ptr_array_free(t->per_cpu_tracefiles, FALSE);
516
517 //free facilities
518 for(i=0;i<t->facility_number;i++){
519 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
520 ltt_facility_close(f);
521 }
522 g_ptr_array_free(t->facilities, FALSE);
523
524 g_free(t);
6cd62ccf 525}
526
963b5f2d 527
6cd62ccf 528/*****************************************************************************
963b5f2d 529 *Get the system description of the trace
6cd62ccf 530 ****************************************************************************/
531
963b5f2d 532LttSystemDescription *ltt_trace_system_description(LttTrace *t)
6cd62ccf 533{
963b5f2d 534 return t->system_description;
6cd62ccf 535}
536
537/*****************************************************************************
963b5f2d 538 * The following functions discover the facilities of the trace
6cd62ccf 539 ****************************************************************************/
540
963b5f2d 541unsigned ltt_trace_facility_number(LttTrace *t)
542{
543 return (unsigned)(t->facility_number);
544}
545
546LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
6cd62ccf 547{
963b5f2d 548 return (LttFacility*)g_ptr_array_index(t->facilities, i);
6cd62ccf 549}
550
551/*****************************************************************************
552 *Function name
963b5f2d 553 * ltt_trace_facility_find : find facilities in the trace
6cd62ccf 554 *Input params
963b5f2d 555 * t : the trace
556 * name : facility name
557 *Output params
558 * position : position of the facility in the trace
6cd62ccf 559 *Return value
963b5f2d 560 * : the number of facilities
6cd62ccf 561 ****************************************************************************/
562
963b5f2d 563unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
6cd62ccf 564{
963b5f2d 565 int i, count=0;
566 LttFacility * f;
567 for(i=0;i=t->facility_number;i++){
568 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
569 if(strcmp(f->name,name)==0){
570 count++;
571 if(count==1) *position = i;
572 }else{
573 if(count) break;
574 }
575 }
576 return count;
6cd62ccf 577}
578
579/*****************************************************************************
963b5f2d 580 * Functions to discover all the event types in the trace
6cd62ccf 581 ****************************************************************************/
582
963b5f2d 583unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 584{
963b5f2d 585 int i;
586 unsigned count = 0;
587 LttFacility * f;
588 for(i=0;i=t->facility_number;i++){
589 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
590 count += f->event_number;
591 }
592 return count;
6cd62ccf 593}
594
963b5f2d 595LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
6cd62ccf 596{
963b5f2d 597 LttFacility * facility;
598 int i;
599 for(i=0;i<trace->facility_number;i++){
600 facility = (LttFacility*) g_ptr_array_index(trace->facilities,i);
601 if(id >= facility->base_id &&
602 id < facility->base_id + facility->event_number)
603 break;
604 }
605 if(i==trace->facility_number) return NULL;
606 else return facility;
6cd62ccf 607}
608
963b5f2d 609LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 610{
963b5f2d 611 LttFacility * f;
612 f = ltt_trace_facility_by_id(t,evId);
613 if(!f) return NULL;
614 return f->events[evId - f->base_id];
6cd62ccf 615}
616
617/*****************************************************************************
963b5f2d 618 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
619 *the maximum number of CPU in the system. When the number of CPU installed
620 *is less than the maximum, some positions are unused. There are also a
621 *number of "control" tracefiles (facilities, interrupts...).
6cd62ccf 622 ****************************************************************************/
963b5f2d 623unsigned ltt_trace_control_tracefile_number(LttTrace *t)
6cd62ccf 624{
963b5f2d 625 return t->control_tracefile_number;
6cd62ccf 626}
627
963b5f2d 628unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
6cd62ccf 629{
963b5f2d 630 return t->per_cpu_tracefile_number;
6cd62ccf 631}
632
633/*****************************************************************************
963b5f2d 634 *It is possible to search for the tracefiles by name or by CPU position.
635 *The index within the tracefiles of the same type is returned if found
636 *and a negative value otherwise.
6cd62ccf 637 ****************************************************************************/
638
963b5f2d 639int ltt_trace_control_tracefile_find(LttTrace *t, char *name)
6cd62ccf 640{
963b5f2d 641 LttTracefile * tracefile;
642 int i;
643 for(i=0;i<t->control_tracefile_number;i++){
644 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
645 if(strcmp(tracefile->name, name)==0)break;
646 }
647 if(i == t->control_tracefile_number) return -1;
648 return i;
6cd62ccf 649}
650
963b5f2d 651int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i)
6cd62ccf 652{
963b5f2d 653 LttTracefile * tracefile;
654 int j, name;
655 for(j=0;j<t->per_cpu_tracefile_number;j++){
656 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j);
657 name = atoi(tracefile->name);
658 if(name == (int)i)break;
659 }
660 if(j == t->per_cpu_tracefile_number) return -1;
661 return j;
6cd62ccf 662}
663
664/*****************************************************************************
963b5f2d 665 *Get a specific tracefile
6cd62ccf 666 ****************************************************************************/
667
963b5f2d 668LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
6cd62ccf 669{
963b5f2d 670 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
671}
672
673LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
674{
675 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
6cd62ccf 676}
677
678/*****************************************************************************
963b5f2d 679 *Get the name of a tracefile
6cd62ccf 680 ****************************************************************************/
681
963b5f2d 682char *ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 683{
963b5f2d 684 return tf->name;
6cd62ccf 685}
686
687/*****************************************************************************
688 *Function name
689 * ltt_tracefile_seek_time: seek to the first event of the trace with time
690 * larger or equal to time
691 *Input params
692 * t : tracefile
693 * time : criteria of the time
6cd62ccf 694 ****************************************************************************/
695
963b5f2d 696void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
6cd62ccf 697{
698 int err;
963b5f2d 699 LttTime lttTime;
700 int headTime = timecmp(&(t->a_block_start->time), &time);
701 int tailTime = timecmp(&(t->a_block_end->time), &time);
6cd62ccf 702
703 if(headTime < 0 && tailTime > 0){
704 lttTime = getEventTime(t);
705 err = timecmp(&lttTime, &time);
963b5f2d 706 if(err > 0){
707 if(t->which_event==1 || timecmp(&t->prev_event_time,&time)<0){
708 return;
709 }else{
6cd62ccf 710 updateTracefile(t);
711 return ltt_tracefile_seek_time(t, time);
712 }
713 }else if(err < 0){
714 err = t->which_block;
963b5f2d 715 if(ltt_tracefile_read(t) == NULL){
716 g_printf("End of file\n");
717 return;
718 }
6cd62ccf 719 if(t->which_block == err)
720 return ltt_tracefile_seek_time(t,time);
963b5f2d 721 }else return;
6cd62ccf 722 }else if(headTime > 0){
723 if(t->which_block == 1){
724 updateTracefile(t);
725 }else{
726 if( (t->prev_block_end_time.tv_sec == 0 &&
727 t->prev_block_end_time.tv_nsec == 0 ) ||
728 timecmp(&(t->prev_block_end_time),&time) > 0 ){
729 err=readBlock(t,t->which_block-1);
963b5f2d 730 if(err) g_error("Can not read tracefile: %s\n", t->name);
6cd62ccf 731 return ltt_tracefile_seek_time(t, time) ;
732 }else{
733 updateTracefile(t);
734 }
735 }
736 }else if(tailTime <= 0){
737 if(t->which_block != t->block_number){
738 err=readBlock(t,t->which_block+1);
963b5f2d 739 if(err) g_error("Can not read tracefile: %s\n", t->name);
740 }else {
741 g_printf("End of file\n");
742 return;
743 }
6cd62ccf 744 if(tailTime < 0) return ltt_tracefile_seek_time(t, time);
745 }else if(headTime == 0){
746 updateTracefile(t);
747 }
6cd62ccf 748}
749
750/*****************************************************************************
751 *Function name
752 * ltt_tracefile_read : read the next event
753 *Input params
754 * t : tracefile
755 *Return value
963b5f2d 756 * LttEvent * : an event to be processed
6cd62ccf 757 ****************************************************************************/
758
963b5f2d 759LttEvent *ltt_tracefile_read(LttTracefile *t)
6cd62ccf 760{
963b5f2d 761 LttEvent * lttEvent = (LttEvent *)g_new(LttEvent, 1);
762 int err;
6cd62ccf 763
764 //update the fields of the current event and go to the next event
963b5f2d 765 err = skipEvent(t);
766 if(err == ENOENT) return NULL;
767 if(err == ERANGE) g_error("event id is out of range\n");
768 if(err)g_error("Can not read tracefile\n");
769
770 lttEvent->event_id = (int)(*(uint16_t *)(t->cur_event_pos));
771 if(lttEvent->event_id == TRACE_TIME_HEARTBEAT)
772 t->cur_heart_beat_number++;
6cd62ccf 773
774 t->current_event_time = getEventTime(t);
775
963b5f2d 776 lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE);
777 lttEvent->event_time = t->current_event_time;
778
779 lttEvent->event_cycle_count = ((uint64_t)1)<<32 * t->cur_heart_beat_number
780 + lttEvent->time_delta;
781
6cd62ccf 782 lttEvent->tracefile = t;
783 lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE;
784
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;
6cd62ccf 831 }else{
832 tf->prev_block_end_time.tv_sec = 0;
833 tf->prev_block_end_time.tv_nsec = 0;
834 }
835 tf->prev_event_time.tv_sec = 0;
836 tf->prev_event_time.tv_nsec = 0;
837
963b5f2d 838 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
6cd62ccf 839 if(nbBytes == -1) return EINVAL;
840
963b5f2d 841 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
842 return EIO;
6cd62ccf 843
963b5f2d 844 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
845 lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t));
846 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
847 lostSize + EVENT_HEADER_SIZE);
6cd62ccf 848
6cd62ccf 849 tf->which_block = whichBlock;
963b5f2d 850 tf->which_event = 1;
851 tf->cur_event_pos = tf->a_block_start + sizeof(BlockStart); //first event
6cd62ccf 852 tf->cur_heart_beat_number = 0;
963b5f2d 853
854 tf->current_event_time = getEventTime(tf);
6cd62ccf 855
856 getCyclePerNsec(tf);
857
858 return 0;
859}
860
861/*****************************************************************************
862 *Function name
863 * updateTracefile : reinitialize the info of the block which is already
864 * in the buffer
865 *Input params
866 * tf : tracefile
867 ****************************************************************************/
868
963b5f2d 869void updateTracefile(LttTracefile * tf)
6cd62ccf 870{
963b5f2d 871 tf->which_event = 1;
872 tf->cur_event_pos = tf->a_block_start + sizeof(BlockStart);
873 tf->current_event_time = getEventTime(tf);
6cd62ccf 874 tf->cur_heart_beat_number = 0;
875
876 tf->prev_event_time.tv_sec = 0;
877 tf->prev_event_time.tv_nsec = 0;
878}
879
880/*****************************************************************************
881 *Function name
882 * skipEvent : go to the next event, update the fields of the current event
883 *Input params
884 * t : tracefile
885 *return value
886 * 0 : success
887 * EINVAL : lseek fail
888 * EIO : can not read from the file
889 * ENOENT : end of file
890 * ERANGE : event id is out of range
891 ****************************************************************************/
892
963b5f2d 893int skipEvent(LttTracefile * t)
6cd62ccf 894{
895 int evId, err;
896 void * evData;
963b5f2d 897 LttEventType * evT;
898 LttField * rootFld;
6cd62ccf 899
963b5f2d 900 evId = (int)(*(uint16_t *)(t->cur_event_pos));
6cd62ccf 901 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
963b5f2d 902 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
6cd62ccf 903
904 if(evT) rootFld = evT->root_field;
905 else return ERANGE;
906
907 t->prev_event_time = getEventTime(t);
908
909 //event has string/sequence or the last event is not the same event
910 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
911 && rootFld->field_fixed == 0){
912 setFieldsOffset(t, evT, evData);
913 }
914 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
915
916 evT->latest_block = t->which_block;
917 evT->latest_event = t->which_event;
918
919 //the next event is in the next block
963b5f2d 920 if(evId == TRACE_BLOCK_END){
6cd62ccf 921 if(t->which_block == t->block_number) return ENOENT;
922 err = readBlock(t, t->which_block + 1);
923 if(err) return err;
924 }else{
925 t->which_event++;
926 }
927
928 return 0;
929}
930
931/*****************************************************************************
932 *Function name
933 * getCyclePerNsec : calculate cycles per nsec for current block
934 *Input Params
935 * t : tracefile
936 ****************************************************************************/
937
963b5f2d 938void getCyclePerNsec(LttTracefile * t)
6cd62ccf 939{
963b5f2d 940 LttTime lBufTotalTime; /* Total time for this buffer */
941 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
942 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 943
944 /* Calculate the total time for this buffer */
963b5f2d 945 TimeSub(lBufTotalTime,t->a_block_end->time, t->a_block_start->time);
6cd62ccf 946
947 /* Calculate the total cycles for this bufffer */
963b5f2d 948 lBufTotalCycle = t->a_block_end->cycle_count
949 - t->a_block_start->cycle_count;
6cd62ccf 950
951 /* Convert the total time to nsecs */
952 lBufTotalNSec = lBufTotalTime.tv_sec * 1000000000 + lBufTotalTime.tv_nsec;
953
954 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
955}
956
957/****************************************************************************
958 *Function name
959 * getEventTime : obtain the time of an event
960 *Input params
961 * tf : tracefile
962 *Return value
963b5f2d 963 * LttTime : the time of the event
6cd62ccf 964 ****************************************************************************/
965
963b5f2d 966LttTime getEventTime(LttTracefile * tf)
6cd62ccf 967{
963b5f2d 968 LttTime time;
969 LttCycleCount cycle_count; // cycle count for the current event
970 LttCycleCount lEventTotalCycle; // Total cycles from start for event
971 double lEventNSec; // Total usecs from start for event
972 LttTime lTimeOffset; // Time offset in struct LttTime
6cd62ccf 973
963b5f2d 974 // Calculate total time in cycles from start of buffer for this event
975 cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE);
6cd62ccf 976 if(tf->cur_heart_beat_number)
977 cycle_count += ((uint64_t)1)<<32 * tf->cur_heart_beat_number;
963b5f2d 978 lEventTotalCycle = cycle_count - tf->a_block_start->cycle_count;
6cd62ccf 979
963b5f2d 980 // Convert it to nsecs
6cd62ccf 981 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
982
963b5f2d 983 // Determine offset in struct LttTime
6cd62ccf 984 lTimeOffset.tv_nsec = (long)lEventNSec % 1000000000;
985 lTimeOffset.tv_sec = (long)lEventNSec / 1000000000;
986
963b5f2d 987 TimeAdd(time, tf->a_block_start->time, lTimeOffset);
6cd62ccf 988
989 return time;
990}
991
992/*****************************************************************************
993 *Function name
994 * setFieldsOffset : set offset of the fields
995 *Input params
996 * tracefile : opened trace file
997 * evT : the event type
998 * evD : event data, it may be NULL
999 ****************************************************************************/
1000
963b5f2d 1001void setFieldsOffset(LttTracefile * t, LttEventType * evT, void * evD)
6cd62ccf 1002{
963b5f2d 1003 LttField * rootFld = evT->root_field;
6cd62ccf 1004 // rootFld->base_address = evD;
1005
1006 rootFld->field_size = getFieldtypeSize(t, evT, 0,0,rootFld, evD);
1007}
1008
1009/*****************************************************************************
1010 *Function name
1011 * getFieldtypeSize: get the size of the field type (primitive type)
1012 *Input params
1013 * tracefile : opened trace file
1014 * evT : event type
1015 * offsetRoot : offset from the root
1016 * offsetParent : offset from the parrent
1017 * fld : field
1018 * evD : event data, it may be NULL
1019 *Return value
1020 * int : size of the field
1021 ****************************************************************************/
1022
963b5f2d 1023int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
1024 int offsetParent, LttField * fld, void * evD)
6cd62ccf 1025{
1026 int size, size1, element_number, i, offset1, offset2;
963b5f2d 1027 LttType * type = fld->field_type;
6cd62ccf 1028
963b5f2d 1029 if(!t){
1030 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1031 return fld->field_size;
1032 }
1033 }
6cd62ccf 1034
1035 if(fld->field_fixed == 1){
1036 if(fld == evT->root_field) return fld->field_size;
1037 }
1038
1039 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1040 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1041 if(fld->field_fixed == -1){
963b5f2d 1042 size = (int) ltt_type_size(t->trace, type);
6cd62ccf 1043 fld->field_fixed = 1;
1044 }else size = fld->field_size;
1045
1046 }else if(type->type_class == LTT_ARRAY){
1047 element_number = (int) type->element_number;
1048 if(fld->field_fixed == -1){
1049 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL);
1050 if(size == 0){ //has string or sequence
1051 fld->field_fixed = 0;
1052 }else{
1053 fld->field_fixed = 1;
1054 size *= element_number;
1055 }
1056 }else if(fld->field_fixed == 0){// has string or sequence
1057 size = 0;
1058 for(i=0;i<element_number;i++){
1059 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1060 fld->child[0], evD+size);
1061 }
1062 }else size = fld->field_size;
1063
1064 }else if(type->type_class == LTT_SEQUENCE){
963b5f2d 1065 size1 = (int) ltt_type_size(t->trace, type);
6cd62ccf 1066 if(fld->field_fixed == -1){
1067 fld->field_fixed = 0;
1068 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL);
1069 fld->element_size = size;
1070 }else{//0: sequence
1071 element_number = getIntNumber(size1,evD);
1072 type->element_number = element_number;
1073 if(fld->element_size > 0){
1074 size = element_number * fld->element_size;
1075 }else{//sequence has string or sequence
1076 size = 0;
1077 for(i=0;i<element_number;i++){
1078 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1079 fld->child[0], evD+size+size1);
1080 }
1081 }
1082 size += size1;
1083 }
1084
1085 }else if(type->type_class == LTT_STRING){
1086 size = 0;
1087 if(fld->field_fixed == -1){
1088 fld->field_fixed = 0;
1089 }else{//0: string
1090 size = sizeof((char*)evD) + 1; //include end : '\0'
1091 }
1092
1093 }else if(type->type_class == LTT_STRUCT){
1094 element_number = (int) type->element_number;
1095 size = 0;
1096 if(fld->field_fixed == -1){
1097 offset1 = offsetRoot;
1098 offset2 = 0;
1099 for(i=0;i<element_number;i++){
1100 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL);
1101 if(size1 > 0 && size >= 0){
1102 size += size1;
1103 if(offset1 >= 0) offset1 += size1;
1104 offset2 += size1;
1105 }else{
1106 size = -1;
1107 offset1 = -1;
1108 offset2 = -1;
1109 }
1110 }
1111 if(size == -1){
1112 fld->field_fixed = 0;
1113 size = 0;
1114 }else fld->field_fixed = 1;
1115 }else if(fld->field_fixed == 0){
1116 offset1 = offsetRoot;
1117 offset2 = 0;
1118 for(i=0;i<element_number;i++){
1119 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2);
1120 offset1 += size;
1121 offset2 += size;
1122 }
1123 size = offset2;
1124 }else size = fld->field_size;
1125 }
1126
1127 fld->offset_root = offsetRoot;
1128 fld->offset_parent = offsetParent;
1129 if(!evD){
1130 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1131 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1132 }
1133 fld->field_size = size;
1134
1135 return size;
1136}
1137
1138/*****************************************************************************
1139 *Function name
1140 * timecmp : compare two time
1141 *Input params
1142 * t1 : first time
1143 * t2 : second time
1144 *Return value
1145 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1146 ****************************************************************************/
1147
963b5f2d 1148int timecmp(LttTime * t1, LttTime * t2)
6cd62ccf 1149{
963b5f2d 1150 LttTime T;
6cd62ccf 1151 TimeSub(T, *t1, *t2);
1152 if(T.tv_sec == 0 && T.tv_nsec == 0) return 0;
1153 else if(T.tv_sec > 0 || (T.tv_sec==0 && T.tv_nsec > 0)) return 1;
1154 else return -1;
1155}
1156
1157/*****************************************************************************
1158 *Function name
1159 * getIntNumber : get an integer number
1160 *Input params
1161 * size : the size of the integer
1162 * evD : the event data
1163 *Return value
1164 * int : an integer
1165 ****************************************************************************/
1166
1167int getIntNumber(int size, void *evD)
1168{
1169 int64_t i;
1170 if(size == 1) i = *(int8_t *)evD;
1171 else if(size == 2) i = *(int16_t *)evD;
1172 else if(size == 4) i = *(int32_t *)evD;
1173 else if(size == 8) i = *(int64_t *)evD;
1174
1175 return (int) i;
1176}
1177
1178/*****************************************************************************
1179 *Function name
1180 * getDataEndianType : get the data type size and endian type of the local
1181 * machine
1182 *Input params
1183 * size : size of data type
1184 * endian : endian type, little or big
1185 ****************************************************************************/
1186
963b5f2d 1187void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
6cd62ccf 1188{
1189 int i = 1;
1190 char c = (char) i;
1191 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1192
1193 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1194 else *endian = LTT_BIG_ENDIAN;
1195
1196 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1197 *size = LTT_LP32;
1198 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1199 *size = LTT_ILP32;
1200 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1201 *size = LTT_LP64;
1202 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1203 *size = LTT_ILP64;
1204 else *size = LTT_UNKNOWN;
1205}
1206
This page took 0.071697 seconds and 4 git commands to generate.