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