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