get rid of LTTTypes.h
[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
f7afe191 461/******************************************************************************
462 * When we copy a trace, we want all the opening actions to happen again :
463 * the trace will be reopened and totally independant from the original.
464 * That's why we call ltt_trace_open.
465 *****************************************************************************/
466LttTrace *ltt_trace_copy(LttTrace *self)
467{
468 return ltt_trace_open(self->pathname);
469}
470
963b5f2d 471void ltt_trace_close(LttTrace *t)
6cd62ccf 472{
963b5f2d 473 int i;
474 LttTracefile * tf;
475 LttFacility * f;
476
477 g_free(t->pathname);
478
479 //free system_description
480 g_free(t->system_description->description);
481 g_free(t->system_description->node_name);
482 g_free(t->system_description->domain_name);
483 g_free(t->system_description->kernel_name);
484 g_free(t->system_description->kernel_release);
485 g_free(t->system_description->kernel_version);
486 g_free(t->system_description->machine);
487 g_free(t->system_description->processor);
488 g_free(t->system_description->hardware_platform);
489 g_free(t->system_description->operating_system);
490 g_free(t->system_description);
491
492 //free control_tracefiles
493 for(i=0;i<t->control_tracefile_number;i++){
494 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
495 ltt_tracefile_close(tf);
496 }
09ad4797 497 g_ptr_array_free(t->control_tracefiles, TRUE);
963b5f2d 498
499 //free per_cpu_tracefiles
500 for(i=0;i<t->per_cpu_tracefile_number;i++){
501 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
502 ltt_tracefile_close(tf);
503 }
09ad4797 504 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
963b5f2d 505
506 //free facilities
507 for(i=0;i<t->facility_number;i++){
508 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
509 ltt_facility_close(f);
510 }
09ad4797 511 g_ptr_array_free(t->facilities, TRUE);
963b5f2d 512
513 g_free(t);
09ad4797 514
515 g_blow_chunks();
6cd62ccf 516}
517
963b5f2d 518
6cd62ccf 519/*****************************************************************************
963b5f2d 520 *Get the system description of the trace
6cd62ccf 521 ****************************************************************************/
522
963b5f2d 523LttSystemDescription *ltt_trace_system_description(LttTrace *t)
6cd62ccf 524{
963b5f2d 525 return t->system_description;
6cd62ccf 526}
527
528/*****************************************************************************
963b5f2d 529 * The following functions discover the facilities of the trace
6cd62ccf 530 ****************************************************************************/
531
963b5f2d 532unsigned ltt_trace_facility_number(LttTrace *t)
533{
534 return (unsigned)(t->facility_number);
535}
536
537LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
6cd62ccf 538{
963b5f2d 539 return (LttFacility*)g_ptr_array_index(t->facilities, i);
6cd62ccf 540}
541
542/*****************************************************************************
543 *Function name
963b5f2d 544 * ltt_trace_facility_find : find facilities in the trace
6cd62ccf 545 *Input params
963b5f2d 546 * t : the trace
547 * name : facility name
548 *Output params
549 * position : position of the facility in the trace
6cd62ccf 550 *Return value
963b5f2d 551 * : the number of facilities
6cd62ccf 552 ****************************************************************************/
553
963b5f2d 554unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
6cd62ccf 555{
963b5f2d 556 int i, count=0;
557 LttFacility * f;
8a3005f3 558 for(i=0;i<t->facility_number;i++){
963b5f2d 559 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
560 if(strcmp(f->name,name)==0){
561 count++;
562 if(count==1) *position = i;
563 }else{
564 if(count) break;
565 }
566 }
567 return count;
6cd62ccf 568}
569
570/*****************************************************************************
963b5f2d 571 * Functions to discover all the event types in the trace
6cd62ccf 572 ****************************************************************************/
573
963b5f2d 574unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 575{
963b5f2d 576 int i;
577 unsigned count = 0;
578 LttFacility * f;
b445142a 579 for(i=0;i<t->facility_number;i++){
963b5f2d 580 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
581 count += f->event_number;
582 }
583 return count;
6cd62ccf 584}
585
963b5f2d 586LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
6cd62ccf 587{
963b5f2d 588 LttFacility * facility;
589 int i;
590 for(i=0;i<trace->facility_number;i++){
591 facility = (LttFacility*) g_ptr_array_index(trace->facilities,i);
592 if(id >= facility->base_id &&
593 id < facility->base_id + facility->event_number)
594 break;
595 }
596 if(i==trace->facility_number) return NULL;
597 else return facility;
6cd62ccf 598}
599
963b5f2d 600LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 601{
963b5f2d 602 LttFacility * f;
603 f = ltt_trace_facility_by_id(t,evId);
604 if(!f) return NULL;
605 return f->events[evId - f->base_id];
6cd62ccf 606}
607
608/*****************************************************************************
963b5f2d 609 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
610 *the maximum number of CPU in the system. When the number of CPU installed
611 *is less than the maximum, some positions are unused. There are also a
612 *number of "control" tracefiles (facilities, interrupts...).
6cd62ccf 613 ****************************************************************************/
963b5f2d 614unsigned ltt_trace_control_tracefile_number(LttTrace *t)
6cd62ccf 615{
963b5f2d 616 return t->control_tracefile_number;
6cd62ccf 617}
618
963b5f2d 619unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
6cd62ccf 620{
963b5f2d 621 return t->per_cpu_tracefile_number;
6cd62ccf 622}
623
624/*****************************************************************************
963b5f2d 625 *It is possible to search for the tracefiles by name or by CPU position.
626 *The index within the tracefiles of the same type is returned if found
627 *and a negative value otherwise.
6cd62ccf 628 ****************************************************************************/
629
963b5f2d 630int ltt_trace_control_tracefile_find(LttTrace *t, char *name)
6cd62ccf 631{
963b5f2d 632 LttTracefile * tracefile;
633 int i;
634 for(i=0;i<t->control_tracefile_number;i++){
635 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
636 if(strcmp(tracefile->name, name)==0)break;
637 }
638 if(i == t->control_tracefile_number) return -1;
639 return i;
6cd62ccf 640}
641
963b5f2d 642int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i)
6cd62ccf 643{
963b5f2d 644 LttTracefile * tracefile;
645 int j, name;
646 for(j=0;j<t->per_cpu_tracefile_number;j++){
647 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j);
648 name = atoi(tracefile->name);
649 if(name == (int)i)break;
650 }
651 if(j == t->per_cpu_tracefile_number) return -1;
652 return j;
6cd62ccf 653}
654
655/*****************************************************************************
963b5f2d 656 *Get a specific tracefile
6cd62ccf 657 ****************************************************************************/
658
963b5f2d 659LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
6cd62ccf 660{
5598cfe3 661 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
963b5f2d 662}
663
664LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
665{
666 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
6cd62ccf 667}
668
487ad181 669/*****************************************************************************
670 * Get the start time and end time of the trace
671 ****************************************************************************/
672
673void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
674{
675 LttTime startSmall, startTmp, endBig, endTmp;
676 int i, j=0;
677 LttTracefile * tf;
678
679 for(i=0;i<t->control_tracefile_number;i++){
680 tf = g_ptr_array_index(t->control_tracefiles, i);
681 readBlock(tf,1);
682 startTmp = tf->a_block_start->time;
683 readBlock(tf,tf->block_number);
684 endTmp = tf->a_block_end->time;
685 if(i==0){
686 startSmall = startTmp;
687 endBig = endTmp;
688 j = 1;
689 continue;
690 }
308711e5 691 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
692 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
487ad181 693 }
694
695 for(i=0;i<t->per_cpu_tracefile_number;i++){
696 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
697 readBlock(tf,1);
698 startTmp = tf->a_block_start->time;
699 readBlock(tf,tf->block_number);
700 endTmp = tf->a_block_end->time;
701 if(j == 0 && i==0){
702 startSmall = startTmp;
703 endBig = endTmp;
704 continue;
705 }
308711e5 706 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
707 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
487ad181 708 }
709
710 *start = startSmall;
711 *end = endBig;
712}
713
714
6cd62ccf 715/*****************************************************************************
963b5f2d 716 *Get the name of a tracefile
6cd62ccf 717 ****************************************************************************/
718
963b5f2d 719char *ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 720{
963b5f2d 721 return tf->name;
6cd62ccf 722}
723
80da81ad 724/*****************************************************************************
725 * Get the number of blocks in the tracefile
726 ****************************************************************************/
727
728unsigned ltt_tracefile_block_number(LttTracefile *tf)
729{
730 return tf->block_number;
731}
732
6cd62ccf 733/*****************************************************************************
734 *Function name
735 * ltt_tracefile_seek_time: seek to the first event of the trace with time
736 * larger or equal to time
737 *Input params
738 * t : tracefile
739 * time : criteria of the time
6cd62ccf 740 ****************************************************************************/
741
963b5f2d 742void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
6cd62ccf 743{
744 int err;
963b5f2d 745 LttTime lttTime;
308711e5 746 int headTime = ltt_time_compare(t->a_block_start->time, time);
747 int tailTime = ltt_time_compare(t->a_block_end->time, time);
62e55dd6 748 LttEvent * ev;
749
6cd62ccf 750 if(headTime < 0 && tailTime > 0){
308711e5 751 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
db55eaae 752 lttTime = getEventTime(t);
308711e5 753 err = ltt_time_compare(lttTime, time);
db55eaae 754 if(err > 0){
308711e5 755 if(t->which_event==2 || (&t->prev_event_time,&time)<0){
1a3b8cbd 756 return;
db55eaae 757 }else{
758 updateTracefile(t);
759 return ltt_tracefile_seek_time(t, time);
1a3b8cbd 760 }
db55eaae 761 }else if(err < 0){
762 while(1){
763 ev = ltt_tracefile_read(t);
764 if(ev == NULL){
765 g_print("End of file\n");
766 return;
767 }
768 lttTime = getEventTime(t);
308711e5 769 err = ltt_time_compare(lttTime, time);
db55eaae 770 if(err >= 0)return;
771 }
772 }else return;
773 }else{//we are at the end of the block
774 updateTracefile(t);
775 return ltt_tracefile_seek_time(t, time);
776 }
e37c1372 777 }else if(headTime >= 0){
6cd62ccf 778 if(t->which_block == 1){
779 updateTracefile(t);
780 }else{
1ee6a9af 781 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
782 (t->prev_block_end_time.tv_sec == 0 &&
783 t->prev_block_end_time.tv_nsec == 0 )){
6cd62ccf 784 err=readBlock(t,t->which_block-1);
963b5f2d 785 if(err) g_error("Can not read tracefile: %s\n", t->name);
6cd62ccf 786 return ltt_tracefile_seek_time(t, time) ;
787 }else{
788 updateTracefile(t);
789 }
790 }
40331ba8 791 }else if(tailTime < 0){
6cd62ccf 792 if(t->which_block != t->block_number){
793 err=readBlock(t,t->which_block+1);
963b5f2d 794 if(err) g_error("Can not read tracefile: %s\n", t->name);
795 }else {
25975c93 796 g_print("End of file\n");
963b5f2d 797 return;
798 }
6cd62ccf 799 if(tailTime < 0) return ltt_tracefile_seek_time(t, time);
40331ba8 800 }else if(tailTime == 0){
e37c1372 801 t->cur_event_pos = t->last_event_pos;
62e55dd6 802 t->current_event_time = time;
803 t->cur_heart_beat_number = 0;
804 t->prev_event_time.tv_sec = 0;
805 t->prev_event_time.tv_nsec = 0;
40331ba8 806 return;
6cd62ccf 807 }
6cd62ccf 808}
809
80da81ad 810/*****************************************************************************
811 * Seek to the first event with position equal or larger to ep
812 ****************************************************************************/
813
814void ltt_tracefile_seek_position(LttTracefile *t, LttEventPosition *ep)
815{
816 //if we are at the right place, just return
817 if(t->which_block == ep->block_num && t->which_event == ep->event_num)
818 return;
819
820 if(t->which_block == ep->block_num) updateTracefile(t);
821 else readBlock(t,ep->block_num);
822
823 //event offset is availiable
824 if(ep->old_position){
825 t->cur_heart_beat_number = ep->heart_beat_number;
826 t->cur_event_pos = t->buffer + ep->event_offset;
827 return;
828 }
829
830 //only block number and event index are availiable
831 while(t->which_event < ep->event_num) ltt_tracefile_read(t);
832
833 return;
834}
835
6cd62ccf 836/*****************************************************************************
837 *Function name
40331ba8 838 * ltt_tracefile_read : read the current event, set the pointer to the next
6cd62ccf 839 *Input params
840 * t : tracefile
841 *Return value
963b5f2d 842 * LttEvent * : an event to be processed
6cd62ccf 843 ****************************************************************************/
844
963b5f2d 845LttEvent *ltt_tracefile_read(LttTracefile *t)
6cd62ccf 846{
7525f9e5 847 LttEvent * lttEvent = &t->an_event;
963b5f2d 848 int err;
6cd62ccf 849
bdc36259 850 if(t->cur_event_pos == t->buffer + t->block_size){
851 if(t->which_block == t->block_number){
bdc36259 852 return NULL;
853 }
854 err = readBlock(t, t->which_block + 1);
855 if(err)g_error("Can not read tracefile");
856 }
857
cbd41522 858 lttEvent->event_id = (int)(*(guint16 *)(t->cur_event_pos));
963b5f2d 859 if(lttEvent->event_id == TRACE_TIME_HEARTBEAT)
860 t->cur_heart_beat_number++;
6cd62ccf 861
40331ba8 862 t->prev_event_time = t->current_event_time;
62e55dd6 863 // t->current_event_time = getEventTime(t);
6cd62ccf 864
cbd41522 865 lttEvent->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
963b5f2d 866 lttEvent->event_time = t->current_event_time;
867
6cd62ccf 868 lttEvent->tracefile = t;
869 lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE;
908f42fa 870 lttEvent->which_block = t->which_block;
871 lttEvent->which_event = t->which_event;
6cd62ccf 872
40331ba8 873 //update the fields of the current event and go to the next event
874 err = skipEvent(t);
40331ba8 875 if(err == ERANGE) g_error("event id is out of range\n");
40331ba8 876
e4eced0f 877 lttEvent->event_cycle_count = t->cur_cycle_count;
878
6cd62ccf 879 return lttEvent;
880}
881
882/****************************************************************************
883 *Function name
884 * readFile : wrap function to read from a file
885 *Input Params
886 * fd : file descriptor
887 * buf : buf to contain the content
888 * size : number of bytes to be read
889 * mesg : message to be printed if some thing goes wrong
890 *return value
891 * 0 : success
892 * EIO : can not read from the file
893 ****************************************************************************/
894
895int readFile(int fd, void * buf, size_t size, char * mesg)
896{
897 ssize_t nbBytes;
898 nbBytes = read(fd, buf, size);
899 if(nbBytes != size){
900 printf("%s\n",mesg);
901 return EIO;
902 }
903 return 0;
904}
905
906/****************************************************************************
907 *Function name
908 * readBlock : read a block from the file
909 *Input Params
910 * lttdes : ltt trace file
911 * whichBlock : the block which will be read
912 *return value
913 * 0 : success
914 * EINVAL : lseek fail
915 * EIO : can not read from the file
916 ****************************************************************************/
917
963b5f2d 918int readBlock(LttTracefile * tf, int whichBlock)
6cd62ccf 919{
920 off_t nbBytes;
cbd41522 921 guint32 lostSize;
6cd62ccf 922
923 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
963b5f2d 924 tf->prev_block_end_time = tf->a_block_end->time;
40331ba8 925 tf->prev_event_time = tf->a_block_end->time;
6cd62ccf 926 }else{
927 tf->prev_block_end_time.tv_sec = 0;
928 tf->prev_block_end_time.tv_nsec = 0;
40331ba8 929 tf->prev_event_time.tv_sec = 0;
930 tf->prev_event_time.tv_nsec = 0;
6cd62ccf 931 }
6cd62ccf 932
963b5f2d 933 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
6cd62ccf 934 if(nbBytes == -1) return EINVAL;
935
963b5f2d 936 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
937 return EIO;
6cd62ccf 938
963b5f2d 939 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
cbd41522 940 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
963b5f2d 941 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
942 lostSize + EVENT_HEADER_SIZE);
e37c1372 943 tf->last_event_pos = tf->buffer + tf->block_size - lostSize;
6cd62ccf 944
6cd62ccf 945 tf->which_block = whichBlock;
963b5f2d 946 tf->which_event = 1;
40331ba8 947 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
6cd62ccf 948 tf->cur_heart_beat_number = 0;
963b5f2d 949
6cd62ccf 950 getCyclePerNsec(tf);
951
62e55dd6 952 tf->current_event_time = getEventTime(tf);
40331ba8 953
6cd62ccf 954 return 0;
955}
956
957/*****************************************************************************
958 *Function name
959 * updateTracefile : reinitialize the info of the block which is already
960 * in the buffer
961 *Input params
962 * tf : tracefile
963 ****************************************************************************/
964
963b5f2d 965void updateTracefile(LttTracefile * tf)
6cd62ccf 966{
963b5f2d 967 tf->which_event = 1;
40331ba8 968 tf->cur_event_pos = tf->buffer;
62e55dd6 969 tf->current_event_time = getEventTime(tf);
6cd62ccf 970 tf->cur_heart_beat_number = 0;
971
972 tf->prev_event_time.tv_sec = 0;
973 tf->prev_event_time.tv_nsec = 0;
974}
975
976/*****************************************************************************
977 *Function name
978 * skipEvent : go to the next event, update the fields of the current event
979 *Input params
980 * t : tracefile
981 *return value
982 * 0 : success
6cd62ccf 983 * ERANGE : event id is out of range
984 ****************************************************************************/
985
963b5f2d 986int skipEvent(LttTracefile * t)
6cd62ccf 987{
988 int evId, err;
989 void * evData;
963b5f2d 990 LttEventType * evT;
991 LttField * rootFld;
6cd62ccf 992
cbd41522 993 evId = (int)(*(guint16 *)(t->cur_event_pos));
6cd62ccf 994 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
6cd62ccf 995
908f42fa 996 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
47a166fc 997
908f42fa 998 if(evT) rootFld = evT->root_field;
999 else return ERANGE;
6cd62ccf 1000
908f42fa 1001 if(rootFld){
1002 //event has string/sequence or the last event is not the same event
1003 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1004 && rootFld->field_fixed == 0){
1005 setFieldsOffset(t, evT, evData, t->trace);
47a166fc 1006 }
908f42fa 1007 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1008 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1009
1010 evT->latest_block = t->which_block;
1011 evT->latest_event = t->which_event;
1012
6cd62ccf 1013 //the next event is in the next block
963b5f2d 1014 if(evId == TRACE_BLOCK_END){
bdc36259 1015 t->cur_event_pos = t->buffer + t->block_size;
6cd62ccf 1016 }else{
1017 t->which_event++;
62e55dd6 1018 t->current_event_time = getEventTime(t);
6cd62ccf 1019 }
1020
1021 return 0;
1022}
1023
1024/*****************************************************************************
1025 *Function name
1026 * getCyclePerNsec : calculate cycles per nsec for current block
1027 *Input Params
1028 * t : tracefile
1029 ****************************************************************************/
1030
963b5f2d 1031void getCyclePerNsec(LttTracefile * t)
6cd62ccf 1032{
963b5f2d 1033 LttTime lBufTotalTime; /* Total time for this buffer */
1034 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
1035 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1036
1037 /* Calculate the total time for this buffer */
308711e5 1038 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
6cd62ccf 1039
1040 /* Calculate the total cycles for this bufffer */
e4eced0f 1041 lBufTotalCycle = t->a_block_end->cycle_count;
1042 lBufTotalCycle -= t->a_block_start->cycle_count;
6cd62ccf 1043
1044 /* Convert the total time to nsecs */
e4eced0f 1045 lBufTotalNSec = lBufTotalTime.tv_sec;
308711e5 1046 lBufTotalNSec *= NANOSECONDS_PER_SECOND;
e4eced0f 1047 lBufTotalNSec += lBufTotalTime.tv_nsec;
6cd62ccf 1048
1049 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
1050}
1051
1052/****************************************************************************
1053 *Function name
1054 * getEventTime : obtain the time of an event
1055 *Input params
1056 * tf : tracefile
1057 *Return value
963b5f2d 1058 * LttTime : the time of the event
6cd62ccf 1059 ****************************************************************************/
1060
963b5f2d 1061LttTime getEventTime(LttTracefile * tf)
6cd62ccf 1062{
963b5f2d 1063 LttTime time;
1064 LttCycleCount cycle_count; // cycle count for the current event
1065 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1066 double lEventNSec; // Total usecs from start for event
1067 LttTime lTimeOffset; // Time offset in struct LttTime
cbd41522 1068 guint16 evId;
1069 gint64 nanoSec, tmpCycleCount = (((guint64)1)<<32);
e4eced0f 1070 static LttCycleCount preCycleCount = 0;
1071 static int count = 0;
1072
cbd41522 1073 evId = *(guint16 *)tf->cur_event_pos;
e4eced0f 1074 if(evId == TRACE_BLOCK_START){
1075 count = 0;
1076 preCycleCount = 0;
1077 tf->cur_cycle_count = tf->a_block_start->cycle_count;
40331ba8 1078 return tf->a_block_start->time;
e4eced0f 1079 }else if(evId == TRACE_BLOCK_END){
1080 count = 0;
1081 preCycleCount = 0;
1082 tf->cur_cycle_count = tf->a_block_end->cycle_count;
40331ba8 1083 return tf->a_block_end->time;
e4eced0f 1084 }
40331ba8 1085
e4eced0f 1086 // Calculate total time in cycles from start of buffer for this event
cbd41522 1087 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
e4eced0f 1088
1089 if(cycle_count < preCycleCount)count++;
1090 preCycleCount = cycle_count;
1091 cycle_count += tmpCycleCount * count;
1092
1093 if(tf->cur_heart_beat_number > count)
1094 cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - count);
1095
1096 tf->cur_cycle_count = cycle_count;
1097
1098 lEventTotalCycle = cycle_count;
1099 lEventTotalCycle -= tf->a_block_start->cycle_count;
6cd62ccf 1100
963b5f2d 1101 // Convert it to nsecs
6cd62ccf 1102 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
e4eced0f 1103 nanoSec = lEventNSec;
1104
963b5f2d 1105 // Determine offset in struct LttTime
308711e5 1106 lTimeOffset.tv_nsec = nanoSec % NANOSECONDS_PER_SECOND;
1107 lTimeOffset.tv_sec = nanoSec / NANOSECONDS_PER_SECOND;
6cd62ccf 1108
308711e5 1109 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
e4eced0f 1110
6cd62ccf 1111 return time;
1112}
1113
1114/*****************************************************************************
1115 *Function name
1116 * setFieldsOffset : set offset of the fields
1117 *Input params
1118 * tracefile : opened trace file
1119 * evT : the event type
1120 * evD : event data, it may be NULL
1121 ****************************************************************************/
1122
40331ba8 1123void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
6cd62ccf 1124{
963b5f2d 1125 LttField * rootFld = evT->root_field;
6cd62ccf 1126 // rootFld->base_address = evD;
1127
8710c6c7 1128 if(rootFld)
1129 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
6cd62ccf 1130}
1131
1132/*****************************************************************************
1133 *Function name
1134 * getFieldtypeSize: get the size of the field type (primitive type)
1135 *Input params
1136 * tracefile : opened trace file
1137 * evT : event type
1138 * offsetRoot : offset from the root
1139 * offsetParent : offset from the parrent
1140 * fld : field
1141 * evD : event data, it may be NULL
1142 *Return value
1143 * int : size of the field
1144 ****************************************************************************/
1145
963b5f2d 1146int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
40331ba8 1147 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
6cd62ccf 1148{
1149 int size, size1, element_number, i, offset1, offset2;
963b5f2d 1150 LttType * type = fld->field_type;
6cd62ccf 1151
8710c6c7 1152 if(t){
963b5f2d 1153 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1154 return fld->field_size;
1155 }
1156 }
6cd62ccf 1157
1158 if(fld->field_fixed == 1){
1159 if(fld == evT->root_field) return fld->field_size;
1160 }
1161
1162 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1163 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1164 if(fld->field_fixed == -1){
40331ba8 1165 size = (int) ltt_type_size(trace, type);
6cd62ccf 1166 fld->field_fixed = 1;
1167 }else size = fld->field_size;
1168
1169 }else if(type->type_class == LTT_ARRAY){
1170 element_number = (int) type->element_number;
1171 if(fld->field_fixed == -1){
40331ba8 1172 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1173 if(size == 0){ //has string or sequence
1174 fld->field_fixed = 0;
1175 }else{
1176 fld->field_fixed = 1;
1177 size *= element_number;
1178 }
1179 }else if(fld->field_fixed == 0){// has string or sequence
1180 size = 0;
1181 for(i=0;i<element_number;i++){
1182 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
40331ba8 1183 fld->child[0], evD+size, trace);
6cd62ccf 1184 }
1185 }else size = fld->field_size;
1186
1187 }else if(type->type_class == LTT_SEQUENCE){
40331ba8 1188 size1 = (int) ltt_type_size(trace, type);
6cd62ccf 1189 if(fld->field_fixed == -1){
908f42fa 1190 fld->sequ_number_size = size1;
6cd62ccf 1191 fld->field_fixed = 0;
40331ba8 1192 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1193 fld->element_size = size;
1194 }else{//0: sequence
1195 element_number = getIntNumber(size1,evD);
1196 type->element_number = element_number;
1197 if(fld->element_size > 0){
1198 size = element_number * fld->element_size;
1199 }else{//sequence has string or sequence
1200 size = 0;
1201 for(i=0;i<element_number;i++){
1202 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
40331ba8 1203 fld->child[0], evD+size+size1, trace);
6cd62ccf 1204 }
1205 }
1206 size += size1;
1207 }
1208
1209 }else if(type->type_class == LTT_STRING){
1210 size = 0;
1211 if(fld->field_fixed == -1){
1212 fld->field_fixed = 0;
1213 }else{//0: string
47a166fc 1214 size = strlen((char*)evD) + 1; //include end : '\0'
6cd62ccf 1215 }
1216
1217 }else if(type->type_class == LTT_STRUCT){
1218 element_number = (int) type->element_number;
1219 size = 0;
1220 if(fld->field_fixed == -1){
1221 offset1 = offsetRoot;
1222 offset2 = 0;
1223 for(i=0;i<element_number;i++){
40331ba8 1224 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
6cd62ccf 1225 if(size1 > 0 && size >= 0){
1226 size += size1;
1227 if(offset1 >= 0) offset1 += size1;
1228 offset2 += size1;
1229 }else{
1230 size = -1;
1231 offset1 = -1;
1232 offset2 = -1;
1233 }
1234 }
1235 if(size == -1){
1236 fld->field_fixed = 0;
1237 size = 0;
1238 }else fld->field_fixed = 1;
1239 }else if(fld->field_fixed == 0){
1240 offset1 = offsetRoot;
1241 offset2 = 0;
1242 for(i=0;i<element_number;i++){
40331ba8 1243 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
6cd62ccf 1244 offset1 += size;
1245 offset2 += size;
1246 }
1247 size = offset2;
1248 }else size = fld->field_size;
1249 }
1250
1251 fld->offset_root = offsetRoot;
1252 fld->offset_parent = offsetParent;
1253 if(!evD){
1254 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1255 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1256 }
1257 fld->field_size = size;
1258
1259 return size;
1260}
1261
6cd62ccf 1262
1263/*****************************************************************************
1264 *Function name
1265 * getIntNumber : get an integer number
1266 *Input params
1267 * size : the size of the integer
1268 * evD : the event data
1269 *Return value
1270 * int : an integer
1271 ****************************************************************************/
1272
1273int getIntNumber(int size, void *evD)
1274{
cbd41522 1275 gint64 i;
1276 if(size == 1) i = *(gint8 *)evD;
1277 else if(size == 2) i = *(gint16 *)evD;
1278 else if(size == 4) i = *(gint32 *)evD;
1279 else if(size == 8) i = *(gint64 *)evD;
6cd62ccf 1280
1281 return (int) i;
1282}
1283
1284/*****************************************************************************
1285 *Function name
1286 * getDataEndianType : get the data type size and endian type of the local
1287 * machine
1288 *Input params
1289 * size : size of data type
1290 * endian : endian type, little or big
1291 ****************************************************************************/
1292
963b5f2d 1293void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
6cd62ccf 1294{
1295 int i = 1;
1296 char c = (char) i;
1297 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1298
1299 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1300 else *endian = LTT_BIG_ENDIAN;
1301
1302 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1303 *size = LTT_LP32;
1304 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1305 *size = LTT_ILP32;
1306 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1307 *size = LTT_LP64;
1308 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1309 *size = LTT_ILP64;
1310 else *size = LTT_UNKNOWN;
1311}
1312
This page took 0.087012 seconds and 4 git commands to generate.