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