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