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