gtkcustom --> gtkmultivpaned
[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 ****************************************************************************/
caf7a67a 741void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time,
742 int start_block, int end_block)
743{
744 int err, tmp_block, s, e;
745 int headTime;
746 int tailTime;
747
748 err=readBlock(t,start_block);
749 if(err) g_error("Can not read tracefile: %s\n", t->name);
750 if(start_block == end_block)return;
751
752 tailTime = ltt_time_compare(t->a_block_end->time, time);
753 if(tailTime >= 0) return;
754
755 err=readBlock(t,end_block);
756 if(err) g_error("Can not read tracefile: %s\n", t->name);
757 if(start_block+1 == end_block)return;
758
759 headTime = ltt_time_compare(t->a_block_start->time, time);
760 if(headTime <= 0 ) return;
761
762 tmp_block = (end_block + start_block)/2;
763 err=readBlock(t,tmp_block);
764 if(err) g_error("Can not read tracefile: %s\n", t->name);
765
766 headTime = ltt_time_compare(t->a_block_start->time, time);
767 tailTime = ltt_time_compare(t->a_block_end->time, time);
768 if(headTime <= 0 && tailTime >= 0) return;
769
770 if(headTime > 0){
771 s = start_block + 1;
772 e = tmp_block - 1;
773 if(s <= e)
774 ltt_tracefile_find_time_block(t, time, s, e);
775 else return;
776 }
777
778 if(tailTime < 0){
779 s = tmp_block + 1;
780 e = end_block - 1;
781 if(s <= e)
782 ltt_tracefile_find_time_block(t, time, s, e);
783 else return;
784 }
785}
786
787void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
788{
789 int t_time, h_time, err;
790 err=readBlock(t,t->which_block-1);
791 if(err) g_error("Can not read tracefile: %s\n", t->name);
792 h_time = ltt_time_compare(t->a_block_start->time, time);
793 t_time = ltt_time_compare(t->a_block_end->time, time);
794 if(h_time == 0){
795 int tmp;
796 if(t->which_block == 1) return;
797 err=readBlock(t,t->which_block-1);
798 if(err) g_error("Can not read tracefile: %s\n", t->name);
799 tmp = ltt_time_compare(t->a_block_end->time, time);
800 if(tmp == 0) return ltt_tracefile_seek_time(t, time);
801 err=readBlock(t,t->which_block+1);
802 if(err) g_error("Can not read tracefile: %s\n", t->name);
803 }else if(h_time > 0){
804 ltt_tracefile_find_time_block(t, time, 1, t->which_block);
805 return ltt_tracefile_seek_time(t, time) ;
806 }else{
807 if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
808 err=readBlock(t,t->which_block+1);
809 if(err) g_error("Can not read tracefile: %s\n", t->name);
810 }
811}
6cd62ccf 812
963b5f2d 813void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
6cd62ccf 814{
815 int err;
963b5f2d 816 LttTime lttTime;
308711e5 817 int headTime = ltt_time_compare(t->a_block_start->time, time);
818 int tailTime = ltt_time_compare(t->a_block_end->time, time);
62e55dd6 819 LttEvent * ev;
820
6cd62ccf 821 if(headTime < 0 && tailTime > 0){
308711e5 822 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
db55eaae 823 lttTime = getEventTime(t);
308711e5 824 err = ltt_time_compare(lttTime, time);
db55eaae 825 if(err > 0){
308711e5 826 if(t->which_event==2 || (&t->prev_event_time,&time)<0){
1a3b8cbd 827 return;
db55eaae 828 }else{
829 updateTracefile(t);
830 return ltt_tracefile_seek_time(t, time);
1a3b8cbd 831 }
db55eaae 832 }else if(err < 0){
833 while(1){
834 ev = ltt_tracefile_read(t);
835 if(ev == NULL){
836 g_print("End of file\n");
837 return;
838 }
839 lttTime = getEventTime(t);
308711e5 840 err = ltt_time_compare(lttTime, time);
db55eaae 841 if(err >= 0)return;
842 }
843 }else return;
844 }else{//we are at the end of the block
845 updateTracefile(t);
846 return ltt_tracefile_seek_time(t, time);
847 }
e37c1372 848 }else if(headTime >= 0){
6cd62ccf 849 if(t->which_block == 1){
850 updateTracefile(t);
851 }else{
1ee6a9af 852 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
853 (t->prev_block_end_time.tv_sec == 0 &&
854 t->prev_block_end_time.tv_nsec == 0 )){
caf7a67a 855 ltt_tracefile_backward_find_time_block(t, time);
6cd62ccf 856 }else{
857 updateTracefile(t);
858 }
859 }
40331ba8 860 }else if(tailTime < 0){
6cd62ccf 861 if(t->which_block != t->block_number){
caf7a67a 862 ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
863 return ltt_tracefile_seek_time(t, time);
963b5f2d 864 }else {
25975c93 865 g_print("End of file\n");
963b5f2d 866 return;
867 }
40331ba8 868 }else if(tailTime == 0){
e37c1372 869 t->cur_event_pos = t->last_event_pos;
62e55dd6 870 t->current_event_time = time;
871 t->cur_heart_beat_number = 0;
872 t->prev_event_time.tv_sec = 0;
873 t->prev_event_time.tv_nsec = 0;
40331ba8 874 return;
6cd62ccf 875 }
6cd62ccf 876}
877
80da81ad 878/*****************************************************************************
879 * Seek to the first event with position equal or larger to ep
880 ****************************************************************************/
881
882void ltt_tracefile_seek_position(LttTracefile *t, LttEventPosition *ep)
883{
884 //if we are at the right place, just return
885 if(t->which_block == ep->block_num && t->which_event == ep->event_num)
886 return;
887
888 if(t->which_block == ep->block_num) updateTracefile(t);
889 else readBlock(t,ep->block_num);
890
891 //event offset is availiable
892 if(ep->old_position){
893 t->cur_heart_beat_number = ep->heart_beat_number;
894 t->cur_event_pos = t->buffer + ep->event_offset;
895 return;
896 }
897
898 //only block number and event index are availiable
899 while(t->which_event < ep->event_num) ltt_tracefile_read(t);
900
901 return;
902}
903
6cd62ccf 904/*****************************************************************************
905 *Function name
40331ba8 906 * ltt_tracefile_read : read the current event, set the pointer to the next
6cd62ccf 907 *Input params
908 * t : tracefile
909 *Return value
963b5f2d 910 * LttEvent * : an event to be processed
6cd62ccf 911 ****************************************************************************/
912
963b5f2d 913LttEvent *ltt_tracefile_read(LttTracefile *t)
6cd62ccf 914{
7525f9e5 915 LttEvent * lttEvent = &t->an_event;
963b5f2d 916 int err;
6cd62ccf 917
bdc36259 918 if(t->cur_event_pos == t->buffer + t->block_size){
919 if(t->which_block == t->block_number){
bdc36259 920 return NULL;
921 }
922 err = readBlock(t, t->which_block + 1);
923 if(err)g_error("Can not read tracefile");
924 }
925
cbd41522 926 lttEvent->event_id = (int)(*(guint16 *)(t->cur_event_pos));
963b5f2d 927 if(lttEvent->event_id == TRACE_TIME_HEARTBEAT)
928 t->cur_heart_beat_number++;
6cd62ccf 929
40331ba8 930 t->prev_event_time = t->current_event_time;
62e55dd6 931 // t->current_event_time = getEventTime(t);
6cd62ccf 932
cbd41522 933 lttEvent->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
963b5f2d 934 lttEvent->event_time = t->current_event_time;
935
6cd62ccf 936 lttEvent->tracefile = t;
937 lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE;
908f42fa 938 lttEvent->which_block = t->which_block;
939 lttEvent->which_event = t->which_event;
6cd62ccf 940
40331ba8 941 //update the fields of the current event and go to the next event
942 err = skipEvent(t);
40331ba8 943 if(err == ERANGE) g_error("event id is out of range\n");
40331ba8 944
e4eced0f 945 lttEvent->event_cycle_count = t->cur_cycle_count;
946
6cd62ccf 947 return lttEvent;
948}
949
950/****************************************************************************
951 *Function name
952 * readFile : wrap function to read from a file
953 *Input Params
954 * fd : file descriptor
955 * buf : buf to contain the content
956 * size : number of bytes to be read
957 * mesg : message to be printed if some thing goes wrong
958 *return value
959 * 0 : success
960 * EIO : can not read from the file
961 ****************************************************************************/
962
963int readFile(int fd, void * buf, size_t size, char * mesg)
964{
965 ssize_t nbBytes;
966 nbBytes = read(fd, buf, size);
967 if(nbBytes != size){
968 printf("%s\n",mesg);
969 return EIO;
970 }
971 return 0;
972}
973
974/****************************************************************************
975 *Function name
976 * readBlock : read a block from the file
977 *Input Params
978 * lttdes : ltt trace file
979 * whichBlock : the block which will be read
980 *return value
981 * 0 : success
982 * EINVAL : lseek fail
983 * EIO : can not read from the file
984 ****************************************************************************/
985
963b5f2d 986int readBlock(LttTracefile * tf, int whichBlock)
6cd62ccf 987{
988 off_t nbBytes;
cbd41522 989 guint32 lostSize;
6cd62ccf 990
991 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
963b5f2d 992 tf->prev_block_end_time = tf->a_block_end->time;
40331ba8 993 tf->prev_event_time = tf->a_block_end->time;
6cd62ccf 994 }else{
995 tf->prev_block_end_time.tv_sec = 0;
996 tf->prev_block_end_time.tv_nsec = 0;
40331ba8 997 tf->prev_event_time.tv_sec = 0;
998 tf->prev_event_time.tv_nsec = 0;
6cd62ccf 999 }
6cd62ccf 1000
963b5f2d 1001 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
6cd62ccf 1002 if(nbBytes == -1) return EINVAL;
1003
963b5f2d 1004 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
1005 return EIO;
6cd62ccf 1006
963b5f2d 1007 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
cbd41522 1008 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
963b5f2d 1009 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
1010 lostSize + EVENT_HEADER_SIZE);
e37c1372 1011 tf->last_event_pos = tf->buffer + tf->block_size - lostSize;
6cd62ccf 1012
6cd62ccf 1013 tf->which_block = whichBlock;
963b5f2d 1014 tf->which_event = 1;
40331ba8 1015 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
6cd62ccf 1016 tf->cur_heart_beat_number = 0;
963b5f2d 1017
6cd62ccf 1018 getCyclePerNsec(tf);
1019
62e55dd6 1020 tf->current_event_time = getEventTime(tf);
40331ba8 1021
6cd62ccf 1022 return 0;
1023}
1024
1025/*****************************************************************************
1026 *Function name
1027 * updateTracefile : reinitialize the info of the block which is already
1028 * in the buffer
1029 *Input params
1030 * tf : tracefile
1031 ****************************************************************************/
1032
963b5f2d 1033void updateTracefile(LttTracefile * tf)
6cd62ccf 1034{
963b5f2d 1035 tf->which_event = 1;
40331ba8 1036 tf->cur_event_pos = tf->buffer;
62e55dd6 1037 tf->current_event_time = getEventTime(tf);
6cd62ccf 1038 tf->cur_heart_beat_number = 0;
1039
1040 tf->prev_event_time.tv_sec = 0;
1041 tf->prev_event_time.tv_nsec = 0;
1042}
1043
1044/*****************************************************************************
1045 *Function name
1046 * skipEvent : go to the next event, update the fields of the current event
1047 *Input params
1048 * t : tracefile
1049 *return value
1050 * 0 : success
6cd62ccf 1051 * ERANGE : event id is out of range
1052 ****************************************************************************/
1053
963b5f2d 1054int skipEvent(LttTracefile * t)
6cd62ccf 1055{
1056 int evId, err;
1057 void * evData;
963b5f2d 1058 LttEventType * evT;
1059 LttField * rootFld;
6cd62ccf 1060
cbd41522 1061 evId = (int)(*(guint16 *)(t->cur_event_pos));
6cd62ccf 1062 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
6cd62ccf 1063
908f42fa 1064 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
47a166fc 1065
908f42fa 1066 if(evT) rootFld = evT->root_field;
1067 else return ERANGE;
6cd62ccf 1068
908f42fa 1069 if(rootFld){
1070 //event has string/sequence or the last event is not the same event
1071 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1072 && rootFld->field_fixed == 0){
1073 setFieldsOffset(t, evT, evData, t->trace);
47a166fc 1074 }
908f42fa 1075 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1076 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1077
1078 evT->latest_block = t->which_block;
1079 evT->latest_event = t->which_event;
1080
6cd62ccf 1081 //the next event is in the next block
963b5f2d 1082 if(evId == TRACE_BLOCK_END){
bdc36259 1083 t->cur_event_pos = t->buffer + t->block_size;
6cd62ccf 1084 }else{
1085 t->which_event++;
62e55dd6 1086 t->current_event_time = getEventTime(t);
6cd62ccf 1087 }
1088
1089 return 0;
1090}
1091
1092/*****************************************************************************
1093 *Function name
1094 * getCyclePerNsec : calculate cycles per nsec for current block
1095 *Input Params
1096 * t : tracefile
1097 ****************************************************************************/
1098
963b5f2d 1099void getCyclePerNsec(LttTracefile * t)
6cd62ccf 1100{
963b5f2d 1101 LttTime lBufTotalTime; /* Total time for this buffer */
1102 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
1103 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1104
1105 /* Calculate the total time for this buffer */
308711e5 1106 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
6cd62ccf 1107
1108 /* Calculate the total cycles for this bufffer */
e4eced0f 1109 lBufTotalCycle = t->a_block_end->cycle_count;
1110 lBufTotalCycle -= t->a_block_start->cycle_count;
6cd62ccf 1111
1112 /* Convert the total time to nsecs */
e4eced0f 1113 lBufTotalNSec = lBufTotalTime.tv_sec;
308711e5 1114 lBufTotalNSec *= NANOSECONDS_PER_SECOND;
e4eced0f 1115 lBufTotalNSec += lBufTotalTime.tv_nsec;
6cd62ccf 1116
1117 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
1118}
1119
1120/****************************************************************************
1121 *Function name
1122 * getEventTime : obtain the time of an event
1123 *Input params
1124 * tf : tracefile
1125 *Return value
963b5f2d 1126 * LttTime : the time of the event
6cd62ccf 1127 ****************************************************************************/
1128
963b5f2d 1129LttTime getEventTime(LttTracefile * tf)
6cd62ccf 1130{
963b5f2d 1131 LttTime time;
1132 LttCycleCount cycle_count; // cycle count for the current event
1133 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1134 double lEventNSec; // Total usecs from start for event
1135 LttTime lTimeOffset; // Time offset in struct LttTime
cbd41522 1136 guint16 evId;
1137 gint64 nanoSec, tmpCycleCount = (((guint64)1)<<32);
e4eced0f 1138 static LttCycleCount preCycleCount = 0;
1139 static int count = 0;
1140
cbd41522 1141 evId = *(guint16 *)tf->cur_event_pos;
e4eced0f 1142 if(evId == TRACE_BLOCK_START){
1143 count = 0;
1144 preCycleCount = 0;
1145 tf->cur_cycle_count = tf->a_block_start->cycle_count;
40331ba8 1146 return tf->a_block_start->time;
e4eced0f 1147 }else if(evId == TRACE_BLOCK_END){
1148 count = 0;
1149 preCycleCount = 0;
1150 tf->cur_cycle_count = tf->a_block_end->cycle_count;
40331ba8 1151 return tf->a_block_end->time;
e4eced0f 1152 }
40331ba8 1153
e4eced0f 1154 // Calculate total time in cycles from start of buffer for this event
cbd41522 1155 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
e4eced0f 1156
1157 if(cycle_count < preCycleCount)count++;
1158 preCycleCount = cycle_count;
1159 cycle_count += tmpCycleCount * count;
1160
1161 if(tf->cur_heart_beat_number > count)
1162 cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - count);
1163
1164 tf->cur_cycle_count = cycle_count;
1165
1166 lEventTotalCycle = cycle_count;
1167 lEventTotalCycle -= tf->a_block_start->cycle_count;
6cd62ccf 1168
963b5f2d 1169 // Convert it to nsecs
6cd62ccf 1170 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
e4eced0f 1171 nanoSec = lEventNSec;
1172
963b5f2d 1173 // Determine offset in struct LttTime
308711e5 1174 lTimeOffset.tv_nsec = nanoSec % NANOSECONDS_PER_SECOND;
1175 lTimeOffset.tv_sec = nanoSec / NANOSECONDS_PER_SECOND;
6cd62ccf 1176
308711e5 1177 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
e4eced0f 1178
6cd62ccf 1179 return time;
1180}
1181
1182/*****************************************************************************
1183 *Function name
1184 * setFieldsOffset : set offset of the fields
1185 *Input params
1186 * tracefile : opened trace file
1187 * evT : the event type
1188 * evD : event data, it may be NULL
1189 ****************************************************************************/
1190
40331ba8 1191void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
6cd62ccf 1192{
963b5f2d 1193 LttField * rootFld = evT->root_field;
6cd62ccf 1194 // rootFld->base_address = evD;
1195
8710c6c7 1196 if(rootFld)
1197 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
6cd62ccf 1198}
1199
1200/*****************************************************************************
1201 *Function name
1202 * getFieldtypeSize: get the size of the field type (primitive type)
1203 *Input params
1204 * tracefile : opened trace file
1205 * evT : event type
1206 * offsetRoot : offset from the root
1207 * offsetParent : offset from the parrent
1208 * fld : field
1209 * evD : event data, it may be NULL
1210 *Return value
1211 * int : size of the field
1212 ****************************************************************************/
1213
963b5f2d 1214int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
40331ba8 1215 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
6cd62ccf 1216{
1217 int size, size1, element_number, i, offset1, offset2;
963b5f2d 1218 LttType * type = fld->field_type;
6cd62ccf 1219
8710c6c7 1220 if(t){
963b5f2d 1221 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1222 return fld->field_size;
1223 }
1224 }
6cd62ccf 1225
1226 if(fld->field_fixed == 1){
1227 if(fld == evT->root_field) return fld->field_size;
1228 }
1229
1230 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1231 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1232 if(fld->field_fixed == -1){
40331ba8 1233 size = (int) ltt_type_size(trace, type);
6cd62ccf 1234 fld->field_fixed = 1;
1235 }else size = fld->field_size;
1236
1237 }else if(type->type_class == LTT_ARRAY){
1238 element_number = (int) type->element_number;
1239 if(fld->field_fixed == -1){
40331ba8 1240 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1241 if(size == 0){ //has string or sequence
1242 fld->field_fixed = 0;
1243 }else{
1244 fld->field_fixed = 1;
1245 size *= element_number;
1246 }
1247 }else if(fld->field_fixed == 0){// has string or sequence
1248 size = 0;
1249 for(i=0;i<element_number;i++){
1250 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
40331ba8 1251 fld->child[0], evD+size, trace);
6cd62ccf 1252 }
1253 }else size = fld->field_size;
1254
1255 }else if(type->type_class == LTT_SEQUENCE){
40331ba8 1256 size1 = (int) ltt_type_size(trace, type);
6cd62ccf 1257 if(fld->field_fixed == -1){
908f42fa 1258 fld->sequ_number_size = size1;
6cd62ccf 1259 fld->field_fixed = 0;
40331ba8 1260 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1261 fld->element_size = size;
1262 }else{//0: sequence
1263 element_number = getIntNumber(size1,evD);
1264 type->element_number = element_number;
1265 if(fld->element_size > 0){
1266 size = element_number * fld->element_size;
1267 }else{//sequence has string or sequence
1268 size = 0;
1269 for(i=0;i<element_number;i++){
1270 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
40331ba8 1271 fld->child[0], evD+size+size1, trace);
6cd62ccf 1272 }
1273 }
1274 size += size1;
1275 }
1276
1277 }else if(type->type_class == LTT_STRING){
1278 size = 0;
1279 if(fld->field_fixed == -1){
1280 fld->field_fixed = 0;
1281 }else{//0: string
47a166fc 1282 size = strlen((char*)evD) + 1; //include end : '\0'
6cd62ccf 1283 }
1284
1285 }else if(type->type_class == LTT_STRUCT){
1286 element_number = (int) type->element_number;
1287 size = 0;
1288 if(fld->field_fixed == -1){
1289 offset1 = offsetRoot;
1290 offset2 = 0;
1291 for(i=0;i<element_number;i++){
40331ba8 1292 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
6cd62ccf 1293 if(size1 > 0 && size >= 0){
1294 size += size1;
1295 if(offset1 >= 0) offset1 += size1;
1296 offset2 += size1;
1297 }else{
1298 size = -1;
1299 offset1 = -1;
1300 offset2 = -1;
1301 }
1302 }
1303 if(size == -1){
1304 fld->field_fixed = 0;
1305 size = 0;
1306 }else fld->field_fixed = 1;
1307 }else if(fld->field_fixed == 0){
1308 offset1 = offsetRoot;
1309 offset2 = 0;
1310 for(i=0;i<element_number;i++){
40331ba8 1311 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
6cd62ccf 1312 offset1 += size;
1313 offset2 += size;
1314 }
1315 size = offset2;
1316 }else size = fld->field_size;
1317 }
1318
1319 fld->offset_root = offsetRoot;
1320 fld->offset_parent = offsetParent;
1321 if(!evD){
1322 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1323 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1324 }
1325 fld->field_size = size;
1326
1327 return size;
1328}
1329
6cd62ccf 1330
1331/*****************************************************************************
1332 *Function name
1333 * getIntNumber : get an integer number
1334 *Input params
1335 * size : the size of the integer
1336 * evD : the event data
1337 *Return value
1338 * int : an integer
1339 ****************************************************************************/
1340
1341int getIntNumber(int size, void *evD)
1342{
cbd41522 1343 gint64 i;
1344 if(size == 1) i = *(gint8 *)evD;
1345 else if(size == 2) i = *(gint16 *)evD;
1346 else if(size == 4) i = *(gint32 *)evD;
1347 else if(size == 8) i = *(gint64 *)evD;
6cd62ccf 1348
1349 return (int) i;
1350}
1351
1352/*****************************************************************************
1353 *Function name
1354 * getDataEndianType : get the data type size and endian type of the local
1355 * machine
1356 *Input params
1357 * size : size of data type
1358 * endian : endian type, little or big
1359 ****************************************************************************/
1360
963b5f2d 1361void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
6cd62ccf 1362{
1363 int i = 1;
1364 char c = (char) i;
1365 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1366
1367 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1368 else *endian = LTT_BIG_ENDIAN;
1369
1370 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1371 *size = LTT_LP32;
1372 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1373 *size = LTT_ILP32;
1374 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1375 *size = LTT_LP64;
1376 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1377 *size = LTT_ILP64;
1378 else *size = LTT_UNKNOWN;
1379}
1380
This page took 0.089689 seconds and 4 git commands to generate.