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