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