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