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