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