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