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