small state.c fixes
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
CommitLineData
449cb9d7 1/* This file is part of the Linux Trace Toolkit viewer
3aee1200 2 * Copyright (C) 2005 Mathieu Desnoyers
449cb9d7 3 *
3aee1200 4 * Complete rewrite from the original version made by XangXiu Yang.
5 *
449cb9d7 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation;
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
19 */
20
4e4d11b3 21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
6cd62ccf 25#include <stdio.h>
26#include <fcntl.h>
8d1e6362 27#include <string.h>
28#include <dirent.h>
6cd62ccf 29#include <sys/stat.h>
30#include <sys/types.h>
8d1e6362 31#include <errno.h>
32#include <unistd.h>
42db9bf1 33#include <math.h>
cdf90f40 34#include <glib.h>
3aee1200 35#include <malloc.h>
36#include <sys/mman.h>
6cd62ccf 37
ef35d837 38// For realpath
39#include <limits.h>
40#include <stdlib.h>
41
42
6cd62ccf 43#include "parser.h"
a5dcde2f 44#include <ltt/ltt.h>
45#include "ltt-private.h"
963b5f2d 46#include <ltt/trace.h>
a5dcde2f 47#include <ltt/facility.h>
c02ea99f 48#include <ltt/event.h>
8d1e6362 49#include <ltt/type.h>
1a2ceb63 50#include <ltt/ltt-types.h>
6cd62ccf 51
3aee1200 52
53/* Facility names used in this file */
54
55GQuark LTT_FACILITY_NAME_HEARTBEAT,
56 LTT_EVENT_NAME_HEARTBEAT;
57GQuark LTT_TRACEFILE_NAME_FACILITIES;
58
86005ded 59#ifndef g_open
60#define g_open open
61#endif
62
63
51b5991e 64#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 65
a1062ddd 66#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
3aee1200 67
68#ifndef g_debug
a1062ddd 69#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
3aee1200 70#endif
a1062ddd 71
45e14832 72#define g_close close
8959a0c8 73
b77d1b57 74/* Those macros must be called from within a function where page_size is a known
75 * variable */
76#define PAGE_MASK (~(page_size-1))
77#define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
78
8959a0c8 79/* obtain the time of an event */
80
81static inline LttTime getEventTime(LttTracefile * tf);
82
83
6cd62ccf 84/* set the offset of the fields belonging to the event,
85 need the information of the archecture */
3aee1200 86void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
eed2ef37 87//size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
6cd62ccf 88
3aee1200 89/* get the size of the field type according to
90 * The facility size information. */
91static inline void preset_field_type_size(LttTracefile *tf,
92 LttEventType *event_type,
93 off_t offset_root, off_t offset_parent,
94 enum field_status *fixed_root, enum field_status *fixed_parent,
95 LttField *field);
6cd62ccf 96
3aee1200 97/* map a fixed size or a block information from the file (fd) */
98static gint map_block(LttTracefile * tf, guint block_num);
99
100/* calculate nsec per cycles for current block */
101static double calc_nsecs_per_cycle(LttTracefile * t);
6cd62ccf 102
3aee1200 103/* go to the next event */
104static int ltt_seek_next_event(LttTracefile *tf);
6cd62ccf 105
eed2ef37 106void ltt_update_event_size(LttTracefile *tf);
107
3aee1200 108#if 0
43da6a59 109/* Functions to parse system.xml file (using glib xml parser) */
51b5991e 110static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
43da6a59 111 const gchar *element_name,
112 const gchar **attribute_names,
113 const gchar **attribute_values,
114 gpointer user_data,
115 GError **error)
116{
117 int i=0;
118 LttSystemDescription* des = (LttSystemDescription* )user_data;
119 if(strcmp("system", element_name)){
2a74fbf4 120 *error = g_error_new(G_MARKUP_ERROR,
121 G_LOG_LEVEL_WARNING,
122 "This is not system.xml file");
123 return;
43da6a59 124 }
125
126 while(attribute_names[i]){
127 if(strcmp("node_name", attribute_names[i])==0){
128 des->node_name = g_strdup(attribute_values[i]);
129 }else if(strcmp("domainname", attribute_names[i])==0){
130 des->domain_name = g_strdup(attribute_values[i]);
131 }else if(strcmp("cpu", attribute_names[i])==0){
132 des->nb_cpu = atoi(attribute_values[i]);
133 }else if(strcmp("arch_size", attribute_names[i])==0){
134 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
135 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
136 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
137 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
138 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
139 }else if(strcmp("endian", attribute_names[i])==0){
140 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
141 des->endian = LTT_LITTLE_ENDIAN;
142 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
143 des->endian = LTT_BIG_ENDIAN;
144 }else if(strcmp("kernel_name", attribute_names[i])==0){
145 des->kernel_name = g_strdup(attribute_values[i]);
146 }else if(strcmp("kernel_release", attribute_names[i])==0){
147 des->kernel_release = g_strdup(attribute_values[i]);
148 }else if(strcmp("kernel_version", attribute_names[i])==0){
149 des->kernel_version = g_strdup(attribute_values[i]);
150 }else if(strcmp("machine", attribute_names[i])==0){
151 des->machine = g_strdup(attribute_values[i]);
152 }else if(strcmp("processor", attribute_names[i])==0){
153 des->processor = g_strdup(attribute_values[i]);
154 }else if(strcmp("hardware_platform", attribute_names[i])==0){
155 des->hardware_platform = g_strdup(attribute_values[i]);
156 }else if(strcmp("operating_system", attribute_names[i])==0){
157 des->operating_system = g_strdup(attribute_values[i]);
158 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
159 des->ltt_major_version = atoi(attribute_values[i]);
160 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
161 des->ltt_minor_version = atoi(attribute_values[i]);
162 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
163 des->ltt_block_size = atoi(attribute_values[i]);
164 }else{
2a74fbf4 165 *error = g_error_new(G_MARKUP_ERROR,
166 G_LOG_LEVEL_WARNING,
167 "Not a valid attribute");
168 return;
43da6a59 169 }
170 i++;
171 }
172}
173
51b5991e 174static void parser_characters (GMarkupParseContext __UNUSED__ *context,
43da6a59 175 const gchar *text,
51b5991e 176 gsize __UNUSED__ text_len,
43da6a59 177 gpointer user_data,
51b5991e 178 GError __UNUSED__ **error)
43da6a59 179{
180 LttSystemDescription* des = (LttSystemDescription* )user_data;
181 des->description = g_strdup(text);
182}
3aee1200 183#endif //0
77175651 184LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
3aee1200 185 guint num)
186{
187 g_assert(num < t->facilities_by_num->len);
188
189 return &g_array_index(t->facilities_by_num, LttFacility, num);
190
191}
43da6a59 192
193
6cd62ccf 194/*****************************************************************************
195 *Function name
963b5f2d 196 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 197 *Input params
963b5f2d 198 * t : the trace containing the tracefile
199 * fileName : path name of the trace file
3aee1200 200 * tf : the tracefile structure
6cd62ccf 201 *Return value
3aee1200 202 * : 0 for success, -1 otherwise.
6cd62ccf 203 ****************************************************************************/
204
3aee1200 205gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
6cd62ccf 206{
963b5f2d 207 struct stat lTDFStat; /* Trace data file status */
3aee1200 208 struct ltt_block_start_header *header;
b77d1b57 209 int page_size = getpagesize();
6cd62ccf 210
211 //open the file
d3d34f49 212 tf->long_name = g_quark_from_string(fileName);
963b5f2d 213 tf->trace = t;
3865ea09 214 tf->fd = open(fileName, O_RDONLY);
6cd62ccf 215 if(tf->fd < 0){
2a74fbf4 216 g_warning("Unable to open input data file %s\n", fileName);
3aee1200 217 goto end;
6cd62ccf 218 }
219
220 // Get the file's status
221 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 222 g_warning("Unable to get the status of the input data file %s\n", fileName);
3aee1200 223 goto close_file;
6cd62ccf 224 }
225
226 // Is the file large enough to contain a trace
3aee1200 227 if(lTDFStat.st_size < (off_t)(sizeof(struct ltt_block_start_header))){
8710c6c7 228 g_print("The input data file %s does not contain a trace\n", fileName);
3aee1200 229 goto close_file;
230 }
231
232 /* Temporarily map the buffer start header to get trace information */
233 /* Multiple of pages aligned head */
b77d1b57 234 tf->buffer.head = mmap(0,
235 PAGE_ALIGN(sizeof(struct ltt_block_start_header)), PROT_READ,
3aee1200 236 MAP_PRIVATE, tf->fd, 0);
3865ea09 237 if(tf->buffer.head == MAP_FAILED) {
3aee1200 238 perror("Error in allocating memory for buffer of tracefile");
239 goto close_file;
6cd62ccf 240 }
3865ea09 241 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 242
243 header = (struct ltt_block_start_header*)tf->buffer.head;
6cd62ccf 244
3aee1200 245 if(header->trace.magic_number == LTT_MAGIC_NUMBER)
246 tf->reverse_bo = 0;
247 else if(header->trace.magic_number == LTT_REV_MAGIC_NUMBER)
248 tf->reverse_bo = 1;
249 else /* invalid magic number, bad tracefile ! */
250 goto unmap_file;
251
6cd62ccf 252 //store the size of the file
253 tf->file_size = lTDFStat.st_size;
f628823c 254 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
255 tf->num_blocks = tf->file_size / tf->buf_size;
256
257 if(munmap(tf->buffer.head,
258 PAGE_ALIGN(sizeof(struct ltt_block_start_header)))) {
259 g_warning("unmap size : %u\n",
260 PAGE_ALIGN(sizeof(struct ltt_block_start_header)));
261 perror("munmap error");
262 g_assert(0);
263 }
3aee1200 264 tf->buffer.head = NULL;
6cd62ccf 265
963b5f2d 266 //read the first block
3aee1200 267 if(map_block(tf,0)) {
268 perror("Cannot map block for tracefile");
269 goto close_file;
270 }
271
272 return 0;
6cd62ccf 273
3aee1200 274 /* Error */
275unmap_file:
f628823c 276 if(munmap(tf->buffer.head,
277 PAGE_ALIGN(sizeof(struct ltt_block_start_header)))) {
278 g_warning("unmap size : %u\n",
279 PAGE_ALIGN(sizeof(struct ltt_block_start_header)));
280 perror("munmap error");
281 g_assert(0);
282 }
3aee1200 283close_file:
3865ea09 284 close(tf->fd);
3aee1200 285end:
286 return -1;
6cd62ccf 287}
288
d3d34f49 289LttTrace *ltt_tracefile_get_trace(LttTracefile *tf)
290{
291 return tf->trace;
292}
293
3aee1200 294#if 0
6cd62ccf 295/*****************************************************************************
963b5f2d 296 *Open control and per cpu tracefiles
6cd62ccf 297 ****************************************************************************/
298
45e14832 299void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
6cd62ccf 300{
963b5f2d 301 LttTracefile * tf;
302 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 303 if(!tf) return;
963b5f2d 304 t->per_cpu_tracefile_number++;
305 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 306}
307
45e14832 308gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
6cd62ccf 309{
963b5f2d 310 LttTracefile * tf;
c02ea99f 311 LttEvent ev;
963b5f2d 312 LttFacility * f;
963b5f2d 313 void * pos;
314 FacilityLoad fLoad;
8d1e6362 315 unsigned int i;
963b5f2d 316
317 tf = ltt_tracefile_open(t,control_name);
2a74fbf4 318 if(!tf) {
319 g_warning("ltt_tracefile_open_control : bad file descriptor");
320 return -1;
321 }
963b5f2d 322 t->control_tracefile_number++;
323 g_ptr_array_add(t->control_tracefiles,tf);
324
325 //parse facilities tracefile to get base_id
542ceddd 326 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 327 while(1){
c02ea99f 328 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
40331ba8 329
c02ea99f 330 if(ev.event_id == TRACE_FACILITY_LOAD){
331 pos = ev.data;
45e14832 332 fLoad.name = (gchar*)pos;
47a166fc 333 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
cbd41522 334 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 335
336 for(i=0;i<t->facility_number;i++){
337 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
338 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
339 f->base_id = fLoad.base_code;
340 break;
341 }
342 }
2a74fbf4 343 if(i==t->facility_number) {
8d1e6362 344 g_warning("Facility: %s, checksum: %u is not found",
345 fLoad.name,(unsigned int)fLoad.checksum);
2a74fbf4 346 return -1;
347 }
c02ea99f 348 }else if(ev.event_id == TRACE_BLOCK_START){
40331ba8 349 continue;
c02ea99f 350 }else if(ev.event_id == TRACE_BLOCK_END){
40331ba8 351 break;
2a74fbf4 352 }else {
8d1e6362 353 g_warning("Not valid facilities trace file");
2a74fbf4 354 return -1;
355 }
963b5f2d 356 }
357 }
2a74fbf4 358 return 0;
6cd62ccf 359}
3aee1200 360#endif //0
6cd62ccf 361
362/*****************************************************************************
363 *Function name
963b5f2d 364 * ltt_tracefile_close: close a trace file,
6cd62ccf 365 *Input params
963b5f2d 366 * t : tracefile which will be closed
6cd62ccf 367 ****************************************************************************/
368
963b5f2d 369void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 370{
f628823c 371 int page_size = getpagesize();
372
3aee1200 373 if(t->buffer.head != NULL)
f628823c 374 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
375 g_warning("unmap size : %u\n",
376 PAGE_ALIGN(t->buf_size));
377 perror("munmap error");
378 g_assert(0);
379 }
380
3865ea09 381 close(t->fd);
963b5f2d 382}
6cd62ccf 383
6cd62ccf 384
963b5f2d 385/*****************************************************************************
386 *Get system information
387 ****************************************************************************/
3aee1200 388#if 0
45e14832 389gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
963b5f2d 390{
45e14832 391 int fd;
392 GIOChannel *iochan;
393 gchar *buf = NULL;
394 gsize length;
43da6a59 395
396 GMarkupParseContext * context;
2a74fbf4 397 GError * error = NULL;
43da6a59 398 GMarkupParser markup_parser =
399 {
400 parser_start_element,
8d1e6362 401 NULL,
43da6a59 402 parser_characters,
403 NULL, /* passthrough */
404 NULL /* error */
405 };
963b5f2d 406
45e14832 407 fd = g_open(pathname, O_RDONLY, 0);
408 if(fd == -1){
2a74fbf4 409 g_warning("Can not open file : %s\n", pathname);
410 return -1;
6cd62ccf 411 }
963b5f2d 412
45e14832 413 iochan = g_io_channel_unix_new(fd);
414
43da6a59 415 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
6cd62ccf 416
45e14832 417 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
418 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
419 != G_IO_STATUS_EOF) {
420
421 if(error != NULL) {
422 g_warning("Can not read xml file: \n%s\n", error->message);
423 g_error_free(error);
424 }
425 if(!g_markup_parse_context_parse(context, buf, length, &error)){
2a74fbf4 426 if(error != NULL) {
427 g_warning("Can not parse xml file: \n%s\n", error->message);
428 g_error_free(error);
429 }
430 g_markup_parse_context_free(context);
45e14832 431
432 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
433 if(error != NULL) {
434 g_warning("Can not close file: \n%s\n", error->message);
435 g_error_free(error);
436 }
437
438 close(fd);
2a74fbf4 439 return -1;
43da6a59 440 }
963b5f2d 441 }
2a74fbf4 442 g_markup_parse_context_free(context);
45e14832 443
444 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
445 if(error != NULL) {
446 g_warning("Can not close file: \n%s\n", error->message);
447 g_error_free(error);
448 }
449
450 g_close(fd);
451
452 g_free(buf);
2a74fbf4 453 return 0;
6cd62ccf 454}
3aee1200 455#endif //0
6cd62ccf 456
457/*****************************************************************************
963b5f2d 458 *The following functions get facility/tracefile information
6cd62ccf 459 ****************************************************************************/
3aee1200 460#if 0
45e14832 461gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
6cd62ccf 462{
45e14832 463 GDir * dir;
464 const gchar * name;
8d1e6362 465 unsigned int i,j;
963b5f2d 466 LttFacility * f;
467 LttEventType * et;
45e14832 468 gchar fullname[DIR_NAME_SIZE];
469 GError * error = NULL;
470
471 dir = g_dir_open(eventdefs, 0, &error);
963b5f2d 472
45e14832 473 if(error != NULL) {
474 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
475 g_error_free(error);
2a74fbf4 476 return -1;
477 }
963b5f2d 478
45e14832 479 while((name = g_dir_read_name(dir)) != NULL){
480 if(!g_pattern_match_simple("*.xml", name)) continue;
481 strcpy(fullname,eventdefs);
482 strcat(fullname,name);
483 ltt_facility_open(t,fullname);
484 }
485 g_dir_close(dir);
963b5f2d 486
963b5f2d 487 for(j=0;j<t->facility_number;j++){
8710c6c7 488 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 489 for(i=0; i<f->event_number; i++){
490 et = f->events[i];
40331ba8 491 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 492 }
493 }
2a74fbf4 494 return 0;
963b5f2d 495}
3aee1200 496#endif //0
6cd62ccf 497
963b5f2d 498/*****************************************************************************
499 *A trace is specified as a pathname to the directory containing all the
500 *associated data (control tracefiles, per cpu tracefiles, event
501 *descriptions...).
502 *
503 *When a trace is closed, all the associated facilities, types and fields
504 *are released as well.
8d1e6362 505 */
506
507
508/****************************************************************************
509 * get_absolute_pathname
803229fa 510 *
8d1e6362 511 * return the unique pathname in the system
512 *
ef35d837 513 * MD : Fixed this function so it uses realpath, dealing well with
514 * forgotten cases (.. were not used correctly before).
803229fa 515 *
963b5f2d 516 ****************************************************************************/
45e14832 517void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
9f797243 518{
9f797243 519 abs_pathname[0] = '\0';
803229fa 520
ef35d837 521 if ( realpath (pathname, abs_pathname) != NULL)
522 return;
523 else
524 {
8d1e6362 525 /* error, return the original path unmodified */
ef35d837 526 strcpy(abs_pathname, pathname);
9f797243 527 return;
528 }
ef35d837 529 return;
9f797243 530}
531
3aee1200 532/* Search for something like : .*_.*
533 *
534 * The left side is the name, the right side is the number.
535 */
536
537int get_tracefile_name_number(const gchar *raw_name,
538 GQuark *name,
539 guint *num)
6cd62ccf 540{
3aee1200 541 guint raw_name_len = strlen(raw_name);
542 gchar char_name[PATH_MAX];
543 gchar *digit_begin;
544 int i;
545 int underscore_pos;
546 long int cpu_num;
547 gchar *endptr;
548
549 for(i=raw_name_len-1;i>=0;i--) {
550 if(raw_name[i] == '_') break;
551 }
552 if(i==0) /* Either not found or name length is 0 */
553 return -1;
554 underscore_pos = i;
963b5f2d 555
3aee1200 556 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
557
558 if(endptr == raw_name+underscore_pos+1)
559 return -1; /* No digit */
560 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
561 return -1; /* underflow / overflow */
1a2ceb63 562
3aee1200 563 strncpy(char_name, raw_name, underscore_pos);
1a2ceb63 564
69bd59ed 565 char_name[underscore_pos] = '\0';
566
3aee1200 567 *name = g_quark_from_string(char_name);
568 *num = cpu_num;
b333a76b 569
3aee1200 570 return 0;
571}
963b5f2d 572
3aee1200 573
3865ea09 574GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
77175651 575{
3865ea09 576 return &trace->tracefiles;
77175651 577}
578
579
3865ea09 580void compute_tracefile_group(GQuark key_id,
581 GArray *group,
582 struct compute_tracefile_group_args *args)
77175651 583{
584 int i;
585 LttTracefile *tf;
586
587 for(i=0; i<group->len; i++) {
588 tf = &g_array_index (group, LttTracefile, i);
589 if(tf->cpu_online)
3865ea09 590 args->func(tf, args->func_args);
77175651 591 }
592}
593
594
3aee1200 595void ltt_tracefile_group_destroy(gpointer data)
596{
597 GArray *group = (GArray *)data;
598 int i;
599 LttTracefile *tf;
600
601 for(i=0; i<group->len; i++) {
602 tf = &g_array_index (group, LttTracefile, i);
603 if(tf->cpu_online)
604 ltt_tracefile_close(tf);
605 }
606 g_array_free(group, TRUE);
6cd62ccf 607}
608
3aee1200 609gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 610{
3aee1200 611 GArray *group = (GArray *)data;
612 int i;
613 LttTracefile *tf;
614
615 for(i=0; i<group->len; i++) {
616 tf = &g_array_index (group, LttTracefile, i);
617 if(tf->cpu_online) return 1;
618 }
619 return 0;
49bf71b5 620}
621
622
3aee1200 623/* Open each tracefile under a specific directory. Put them in a
624 * GData : permits to access them using their tracefile group pathname.
625 * i.e. access control/modules tracefile group by index :
626 * "control/module".
3865ea09 627 *
628 * relative path is the path relative to the trace root
629 * root path is the full path
3aee1200 630 *
631 * A tracefile group is simply an array where all the per cpu tracefiles sits.
632 */
633
69bd59ed 634static int open_tracefiles(LttTrace *trace, gchar *root_path,
635 gchar *relative_path)
f7afe191 636{
3aee1200 637 DIR *dir = opendir(root_path);
638 struct dirent *entry;
639 struct stat stat_buf;
640 int ret;
3865ea09 641
69bd59ed 642 gchar path[PATH_MAX];
3aee1200 643 int path_len;
69bd59ed 644 gchar *path_ptr;
3aee1200 645
3865ea09 646 int rel_path_len;
69bd59ed 647 gchar rel_path[PATH_MAX];
648 gchar *rel_path_ptr;
cb03932a 649 LttTracefile tmp_tf;
3865ea09 650
3aee1200 651 if(dir == NULL) {
652 perror(root_path);
653 return ENOENT;
654 }
655
656 strncpy(path, root_path, PATH_MAX-1);
657 path_len = strlen(path);
658 path[path_len] = '/';
659 path_len++;
660 path_ptr = path + path_len;
661
3865ea09 662 strncpy(rel_path, relative_path, PATH_MAX-1);
663 rel_path_len = strlen(rel_path);
664 rel_path[rel_path_len] = '/';
665 rel_path_len++;
666 rel_path_ptr = rel_path + rel_path_len;
667
3aee1200 668 while((entry = readdir(dir)) != NULL) {
669
670 if(entry->d_name[0] == '.') continue;
671
672 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
3865ea09 673 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
3aee1200 674
675 ret = stat(path, &stat_buf);
676 if(ret == -1) {
677 perror(path);
678 continue;
679 }
680
681 g_debug("Tracefile file or directory : %s\n", path);
682
683 if(S_ISDIR(stat_buf.st_mode)) {
684
685 g_debug("Entering subdirectory...\n");
3865ea09 686 ret = open_tracefiles(trace, path, rel_path);
3aee1200 687 if(ret < 0) continue;
688 } else if(S_ISREG(stat_buf.st_mode)) {
3aee1200 689 GQuark name;
690 guint num;
691 GArray *group;
692 LttTracefile *tf;
693 guint len;
694
3865ea09 695 if(get_tracefile_name_number(rel_path, &name, &num))
3aee1200 696 continue; /* invalid name */
3865ea09 697
cb03932a 698 g_debug("Opening file.\n");
699 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
700 g_info("Error opening tracefile %s", path);
701
702 continue; /* error opening the tracefile : bad magic number ? */
703 }
704
3865ea09 705 g_debug("Tracefile name is %s and number is %u",
706 g_quark_to_string(name), num);
707
cb03932a 708 tmp_tf.cpu_online = 1;
709 tmp_tf.cpu_num = num;
d3d34f49 710 tmp_tf.name = name;
cb03932a 711
3865ea09 712 group = g_datalist_id_get_data(&trace->tracefiles, name);
3aee1200 713 if(group == NULL) {
714 /* Elements are automatically cleared when the array is allocated.
715 * It makes the cpu_online variable set to 0 : cpu offline, by default.
716 */
717 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
3865ea09 718 g_datalist_id_set_data_full(&trace->tracefiles, name,
3aee1200 719 group, ltt_tracefile_group_destroy);
720 }
cb03932a 721
3aee1200 722 /* Add the per cpu tracefile to the named group */
723 unsigned int old_len = group->len;
724 if(num+1 > old_len)
725 group = g_array_set_size(group, num+1);
cb03932a 726 g_array_index (group, LttTracefile, num) = tmp_tf;
3aee1200 727
3aee1200 728 }
729 }
730
731 closedir(dir);
732
733 return 0;
f7afe191 734}
735
3aee1200 736/* ltt_get_facility_description
737 *
738 * Opens the trace corresponding to the requested facility (identified by fac_id
739 * and checksum).
740 *
741 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
742 *
743 * Returns 0 on success, or 1 on failure.
744 */
745
746static int ltt_get_facility_description(LttFacility *f,
747 LttTrace *t,
748 LttTracefile *fac_tf)
6cd62ccf 749{
3aee1200 750 char desc_file_name[PATH_MAX];
751 const gchar *text;
752 guint textlen;
753 gint err;
3aee1200 754
755 text = g_quark_to_string(t->pathname);
756 textlen = strlen(text);
757
758 if(textlen >= PATH_MAX) goto name_error;
759 strcpy(desc_file_name, text);
760
761 text = "/eventdefs/";
762 textlen+=strlen(text);
763 if(textlen >= PATH_MAX) goto name_error;
764 strcat(desc_file_name, text);
765
766 text = g_quark_to_string(f->name);
767 textlen+=strlen(text);
768 if(textlen >= PATH_MAX) goto name_error;
769 strcat(desc_file_name, text);
770
771 text = "_";
772 textlen+=strlen(text);
773 if(textlen >= PATH_MAX) goto name_error;
774 strcat(desc_file_name, text);
963b5f2d 775
3aee1200 776 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
777 "%u", f->checksum);
d2083cab 778 if(err < 0) goto name_error;
3aee1200 779
780 textlen=strlen(desc_file_name);
781
782 text = ".xml";
783 textlen+=strlen(text);
784 if(textlen >= PATH_MAX) goto name_error;
785 strcat(desc_file_name, text);
963b5f2d 786
3aee1200 787 err = ltt_facility_open(f, t, desc_file_name);
788 if(err) goto facility_error;
3aee1200 789
790 return 0;
791
792facility_error:
793name_error:
794 return 1;
795}
796
797static void ltt_fac_ids_destroy(gpointer data)
798{
799 GArray *fac_ids = (GArray *)data;
963b5f2d 800
3aee1200 801 g_array_free(fac_ids, TRUE);
802}
803
804
805/* Presumes the tracefile is already seeked at the beginning. It makes sense,
806 * because it must be done just after the opening */
807static int ltt_process_facility_tracefile(LttTracefile *tf)
808{
809 int err;
810 LttFacility *fac;
811 GArray *fac_ids;
cb03932a 812 guint i;
813 LttEventType *et;
3aee1200 814
815 while(1) {
816 err = ltt_tracefile_read_seek(tf);
817 if(err == EPERM) goto seek_error;
818 else if(err == ERANGE) break; /* End of tracefile */
819
820 err = ltt_tracefile_read_update_event(tf);
821 if(err) goto update_error;
822
823 /* We are on a facility load/or facility unload/ or heartbeat event */
824 /* The rules are :
825 * * facility 0 is hardcoded : this is the core facility. It will be shown
826 * in the facility array though, and is shown as "loaded builtin" in the
827 * trace.
828 * It contains event :
829 * 0 : facility load
830 * 1 : facility unload
831 * 2 : state dump facility load
d2083cab 832 * 3 : heartbeat
3aee1200 833 */
d2083cab 834 if(tf->event.facility_id != LTT_FACILITY_CORE) {
835 /* Should only contain core facility */
3aee1200 836 g_warning("Error in processing facility file %s, "
837 "should not contain facility id %u.", g_quark_to_string(tf->name),
838 tf->event.facility_id);
839 err = EPERM;
840 goto fac_id_error;
d2083cab 841 } else {
3aee1200 842
843 struct LttFacilityLoad *fac_load_data;
b77d1b57 844 struct LttStateDumpFacilityLoad *fac_state_dump_load_data;
3aee1200 845 char *fac_name;
846
847 // FIXME align
848 switch((enum ltt_core_events)tf->event.event_id) {
849 case LTT_EVENT_FACILITY_LOAD:
b77d1b57 850 fac_name = (char*)(tf->event.data);
d2083cab 851 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
852 fac_name);
3aee1200 853 fac_load_data =
b77d1b57 854 (struct LttFacilityLoad *)
855 (tf->event.data + strlen(fac_name) + 1);
3aee1200 856 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
857 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
858 g_assert(fac->exists == 0);
859 fac->name = g_quark_from_string(fac_name);
860 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
861 &fac_load_data->checksum);
862 fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
863 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
864 &fac_load_data->pointer_size);
cb03932a 865 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
866 &fac_load_data->long_size);
eed2ef37 867 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 868 &fac_load_data->size_t_size);
869 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
870 &fac_load_data->alignment);
871
872 if(ltt_get_facility_description(fac, tf->trace, tf))
cb03932a 873 continue; /* error opening description */
3aee1200 874
875 fac->trace = tf->trace;
cb03932a 876
877 /* Preset the field offsets */
878 for(i=0; i<fac->events->len; i++){
879 et = &g_array_index(fac->events, LttEventType, i);
880 set_fields_offsets(tf, et);
881 }
882
3aee1200 883 fac->exists = 1;
884
77175651 885 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
886 fac->name);
3aee1200 887 if(fac_ids == NULL) {
888 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
889 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
890 fac->name,
891 fac_ids, ltt_fac_ids_destroy);
892 }
893 g_array_append_val(fac_ids, fac->id);
894
895 break;
896 case LTT_EVENT_FACILITY_UNLOAD:
d2083cab 897 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
3aee1200 898 /* We don't care about unload : facilities ID are valid for the whole
899 * trace. They simply won't be used after the unload. */
900 break;
901 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
b77d1b57 902 fac_name = (char*)(tf->event.data);
d2083cab 903 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
904 fac_name);
b77d1b57 905 fac_state_dump_load_data =
d2083cab 906 (struct LttStateDumpFacilityLoad *)
b77d1b57 907 (tf->event.data + strlen(fac_name) + 1);
3aee1200 908 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
b77d1b57 909 ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id));
3aee1200 910 g_assert(fac->exists == 0);
911 fac->name = g_quark_from_string(fac_name);
912 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 913 &fac_state_dump_load_data->checksum);
914 fac->id = fac_state_dump_load_data->id;
3aee1200 915 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 916 &fac_state_dump_load_data->pointer_size);
cb03932a 917 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
918 &fac_state_dump_load_data->long_size);
eed2ef37 919 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 920 &fac_state_dump_load_data->size_t_size);
3aee1200 921 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
d2083cab 922 &fac_state_dump_load_data->alignment);
3aee1200 923 if(ltt_get_facility_description(fac, tf->trace, tf))
cb03932a 924 continue; /* error opening description */
3aee1200 925
926 fac->trace = tf->trace;
927
cb03932a 928 /* Preset the field offsets */
929 for(i=0; i<fac->events->len; i++){
930 et = &g_array_index(fac->events, LttEventType, i);
931 set_fields_offsets(tf, et);
932 }
933
3aee1200 934 fac->exists = 1;
935
936 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
937 fac->name);
938 if(fac_ids == NULL) {
939 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
940 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
941 fac->name,
942 fac_ids, ltt_fac_ids_destroy);
943 }
944 g_array_append_val(fac_ids, fac->id);
945
946 break;
947 case LTT_EVENT_HEARTBEAT:
948 break;
949 default:
950 g_warning("Error in processing facility file %s, "
951 "unknown event id %hhu in core facility.",
952 g_quark_to_string(tf->name),
953 tf->event.event_id);
954 err = EPERM;
955 goto event_id_error;
956 }
957 }
963b5f2d 958 }
3aee1200 959 return 0;
963b5f2d 960
3aee1200 961 /* Error handling */
962facility_error:
963event_id_error:
964fac_id_error:
965update_error:
966seek_error:
d2083cab 967 g_warning("An error occured in facility tracefile parsing");
3aee1200 968 return err;
6cd62ccf 969}
970
963b5f2d 971
3aee1200 972LttTrace *ltt_trace_open(const gchar *pathname)
973{
974 gchar abs_path[PATH_MAX];
975 LttTrace * t;
976 LttTracefile *tf;
977 GArray *group;
978 int i;
16fcbb80 979 struct ltt_block_start_header *header;
3aee1200 980
981 t = g_new(LttTrace, 1);
982 if(!t) goto alloc_error;
983
984 get_absolute_pathname(pathname, abs_path);
985 t->pathname = g_quark_from_string(abs_path);
986
987 /* Open all the tracefiles */
988 g_datalist_init(&t->tracefiles);
3865ea09 989 if(open_tracefiles(t, abs_path, ""))
3aee1200 990 goto open_error;
991
992 /* Prepare the facilities containers : array and mapping */
993 /* Array is zeroed : the "exists" field is set to false by default */
994 t->facilities_by_num = g_array_sized_new (FALSE,
995 TRUE, sizeof(LttFacility),
996 NUM_FACILITIES);
997 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
998
999 g_datalist_init(&t->facilities_by_name);
1000
1001 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1002 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1003 if(group == NULL) {
1004 g_error("Trace %s has no facility tracefile", abs_path);
3865ea09 1005 g_assert(0);
3aee1200 1006 goto facilities_error;
1007 }
1008
16fcbb80 1009 /* Get the trace information for the control/facility 0 tracefile */
1010 g_assert(group->len > 0);
1011 tf = &g_array_index (group, LttTracefile, 0);
1012 header = (struct ltt_block_start_header*)tf->buffer.head;
1013 t->arch_type = ltt_get_uint32(LTT_GET_BO(tf), &header->trace.arch_type);
1014 t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf), &header->trace.arch_variant);
1015 t->arch_size = header->trace.arch_size;
1016 t->ltt_major_version = header->trace.major_version;
1017 t->ltt_minor_version = header->trace.minor_version;
1018 t->flight_recorder = header->trace.flight_recorder;
1019 t->has_heartbeat = header->trace.has_heartbeat;
1020 t->has_alignment = header->trace.has_alignment;
1021 t->has_tsc = header->trace.has_tsc;
1022
1023
3aee1200 1024 for(i=0; i<group->len; i++) {
1025 tf = &g_array_index (group, LttTracefile, i);
1026 if(ltt_process_facility_tracefile(tf))
1027 goto facilities_error;
1028 }
3aee1200 1029
1030 return t;
1031
1032 /* Error handling */
1033facilities_error:
1034 g_datalist_clear(&t->facilities_by_name);
1035 g_array_free(t->facilities_by_num, TRUE);
1036open_error:
1037 g_datalist_clear(&t->tracefiles);
1038 g_free(t);
1039alloc_error:
1040 return NULL;
1041
1042}
6cd62ccf 1043
3aee1200 1044GQuark ltt_trace_name(LttTrace *t)
6cd62ccf 1045{
3aee1200 1046 return t->pathname;
6cd62ccf 1047}
1048
6cd62ccf 1049
3aee1200 1050/******************************************************************************
1051 * When we copy a trace, we want all the opening actions to happen again :
1052 * the trace will be reopened and totally independant from the original.
1053 * That's why we call ltt_trace_open.
1054 *****************************************************************************/
1055LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 1056{
3aee1200 1057 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 1058}
1059
3aee1200 1060void ltt_trace_close(LttTrace *t)
6cd62ccf 1061{
cb03932a 1062 guint i;
1063 LttFacility *fac;
1064
1065 for(i=0; i<t->facilities_by_num->len; i++) {
1066 fac = &g_array_index (t->facilities_by_num, LttFacility, i);
1067 if(fac->exists)
1068 ltt_facility_close(fac);
1069 }
1070
3aee1200 1071 g_datalist_clear(&t->facilities_by_name);
1072 g_array_free(t->facilities_by_num, TRUE);
1073 g_datalist_clear(&t->tracefiles);
1074 g_free(t);
6cd62ccf 1075}
1076
3aee1200 1077
6cd62ccf 1078/*****************************************************************************
3aee1200 1079 *Get the system description of the trace
6cd62ccf 1080 ****************************************************************************/
1081
3aee1200 1082LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
6cd62ccf 1083{
3aee1200 1084 g_assert(id < t->facilities_by_num->len);
1085 return &g_array_index(t->facilities_by_num, LttFacility, id);
1086}
1087
1088/* ltt_trace_facility_get_by_name
1089 *
1090 * Returns the GArray of facility indexes. All the fac_ids that matches the
1091 * requested facility name.
1092 *
1093 * If name is not found, returns NULL.
1094 */
1095GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1096{
1097 return g_datalist_id_get_data(&t->facilities_by_name, name);
6cd62ccf 1098}
1099
1100/*****************************************************************************
963b5f2d 1101 * Functions to discover all the event types in the trace
6cd62ccf 1102 ****************************************************************************/
1103
3aee1200 1104#if 0
963b5f2d 1105unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 1106{
8d1e6362 1107 unsigned int i;
963b5f2d 1108 unsigned count = 0;
dc1cad90 1109 unsigned int num = t->facility_number;
963b5f2d 1110 LttFacility * f;
dc1cad90 1111
1112 for(i=0;i<num;i++){
963b5f2d 1113 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1114 count += f->event_number;
1115 }
1116 return count;
6cd62ccf 1117}
3aee1200 1118#endif //0
6cd62ccf 1119
3aee1200 1120#if 0
1121//use an iteration on all the trace facilities, and inside iteration on all the
1122//event types in each facilities instead.
963b5f2d 1123LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 1124{
6e8c19d4 1125 LttEventType *event_type;
1126
963b5f2d 1127 LttFacility * f;
1128 f = ltt_trace_facility_by_id(t,evId);
6e8c19d4 1129
1130 if(unlikely(!f)) event_type = NULL;
1131 else event_type = f->events[evId - f->base_id];
1132
1133 return event_type;
6cd62ccf 1134}
3aee1200 1135#endif //0
6cd62ccf 1136
3aee1200 1137#if 0
6cd62ccf 1138/*****************************************************************************
3aee1200 1139 * ltt_trace_find_tracefile
1140 *
1141 * Find a tracefile by name and index in the group.
1142 *
1143 * Returns a pointer to the tracefiles, else NULL.
6cd62ccf 1144 ****************************************************************************/
6cd62ccf 1145
3aee1200 1146LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
6cd62ccf 1147{
6cd62ccf 1148}
3aee1200 1149#endif //0
6cd62ccf 1150
1151/*****************************************************************************
3aee1200 1152 * Get the start time and end time of the trace
6cd62ccf 1153 ****************************************************************************/
1154
3aee1200 1155static void ltt_tracefile_time_span_get(LttTracefile *tf,
1156 LttTime *start, LttTime *end)
6cd62ccf 1157{
3aee1200 1158 struct ltt_block_start_header * header;
1159 int err;
6cd62ccf 1160
3aee1200 1161 err = map_block(tf, 0);
1162 if(unlikely(err)) {
1163 g_error("Can not map block");
1164 *start = ltt_time_infinite;
1165 } else
1166 *start = tf->buffer.begin.timestamp;
1167
1168 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1169 if(unlikely(err)) {
1170 g_error("Can not map block");
1171 *end = ltt_time_zero;
1172 } else
1173 *end = tf->buffer.end.timestamp;
6cd62ccf 1174}
1175
3aee1200 1176struct tracefile_time_span_get_args {
1177 LttTrace *t;
1178 LttTime *start;
1179 LttTime *end;
1180};
963b5f2d 1181
3aee1200 1182static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 1183{
3aee1200 1184 struct tracefile_time_span_get_args *args =
1185 (struct tracefile_time_span_get_args*)user_data;
1186
1187 GArray *group = (GArray *)data;
1188 int i;
1189 LttTracefile *tf;
1190 LttTime tmp_start;
1191 LttTime tmp_end;
1192
1193 for(i=0; i<group->len; i++) {
1194 tf = &g_array_index (group, LttTracefile, i);
1195 if(tf->cpu_online) {
1196 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1197 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1198 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1199 }
1200 }
6cd62ccf 1201}
1202
487ad181 1203void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1204{
3aee1200 1205 LttTime min_start = ltt_time_infinite;
1206 LttTime max_end = ltt_time_zero;
1207 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 1208
3aee1200 1209 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1210
1211 if(start != NULL) *start = min_start;
1212 if(end != NULL) *end = max_end;
1213
487ad181 1214}
1215
1216
6cd62ccf 1217/*****************************************************************************
963b5f2d 1218 *Get the name of a tracefile
6cd62ccf 1219 ****************************************************************************/
1220
3aee1200 1221GQuark ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 1222{
963b5f2d 1223 return tf->name;
6cd62ccf 1224}
1225
d3d34f49 1226
1227guint ltt_tracefile_num(LttTracefile *tf)
1228{
1229 return tf->cpu_num;
1230}
1231
80da81ad 1232/*****************************************************************************
1233 * Get the number of blocks in the tracefile
1234 ****************************************************************************/
1235
3aee1200 1236guint ltt_tracefile_block_number(LttTracefile *tf)
80da81ad 1237{
3aee1200 1238 return tf->num_blocks;
80da81ad 1239}
1240
caf7a67a 1241
3aee1200 1242/* Seek to the first event in a tracefile that has a time equal or greater than
1243 * the time passed in parameter.
1244 *
1245 * If the time parameter is outside the tracefile time span, seek to the first
27304273 1246 * event or if after, return ERANGE.
3aee1200 1247 *
1248 * If the time parameter is before the first event, we have to seek specially to
1249 * there.
1250 *
27304273 1251 * If the time is after the end of the trace, return ERANGE.
3aee1200 1252 *
1253 * Do a binary search to find the right block, then a sequential search in the
1254 * block to find the event.
1255 *
1256 * In the special case where the time requested fits inside a block that has no
1257 * event corresponding to the requested time, the first event of the next block
1258 * will be seeked.
1259 *
1260 * IMPORTANT NOTE : // FIXME everywhere...
1261 *
1262 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1263 * you will jump over an event if you do.
1264 *
1265 * Return value : 0 : no error, the tf->event can be used
27304273 1266 * ERANGE : time if after the last event of the trace
3aee1200 1267 * otherwise : this is an error.
1268 *
1269 * */
1270
1271int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1272{
1273 int ret = 0;
1274 int err;
1275 unsigned int block_num, high, low;
1276
1277 /* seek at the beginning of trace */
1278 err = map_block(tf, 0); /* First block */
1279 if(unlikely(err)) {
1280 g_error("Can not map block");
1281 goto fail;
caf7a67a 1282 }
1283
3aee1200 1284 /* If the time is lower or equal the beginning of the trace,
1285 * go to the first event. */
1286 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1287 ret = ltt_tracefile_read(tf);
27304273 1288 if(ret == ERANGE) goto range;
1289 else if (ret) goto fail;
3aee1200 1290 goto found; /* There is either no event in the trace or the event points
1291 to the first event in the trace */
1292 }
caf7a67a 1293
3aee1200 1294 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1295 if(unlikely(err)) {
1296 g_error("Can not map block");
1297 goto fail;
caf7a67a 1298 }
6cd62ccf 1299
27304273 1300 /* If the time is after the end of the trace, return ERANGE. */
1301 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1302 goto range;
3aee1200 1303 }
62e55dd6 1304
3aee1200 1305 /* Binary search the block */
1306 high = tf->num_blocks - 1;
1307 low = 0;
1308
1309 while(1) {
1310 block_num = ((high-low) / 2) + low;
1311
1312 err = map_block(tf, block_num);
1313 if(unlikely(err)) {
1314 g_error("Can not map block");
1315 goto fail;
db55eaae 1316 }
3aee1200 1317 if(high == low) {
1318 /* We cannot divide anymore : this is what would happen if the time
1319 * requested was exactly between two consecutive buffers'end and start
1320 * timestamps. This is also what would happend if we didn't deal with out
1321 * of span cases prior in this function. */
1322 /* The event is right in the buffer!
1323 * (or in the next buffer first event) */
1324 while(1) {
1325 ret = ltt_tracefile_read(tf);
27304273 1326 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1327 else if(ret) goto fail;
1328
1329 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1330 break;
1331 }
1332
27304273 1333 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 1334 /* go to lower part */
1335 high = block_num;
1336 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1337 /* go to higher part */
1338 low = block_num;
1339 } else {/* The event is right in the buffer!
1340 (or in the next buffer first event) */
1341 while(1) {
27304273 1342 ret = ltt_tracefile_read(tf);
1343 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1344 else if(ret) goto fail;
1345
1346 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1347 break;
6cd62ccf 1348 }
3aee1200 1349 goto found;
6cd62ccf 1350 }
6cd62ccf 1351 }
3aee1200 1352
1353found:
1354 return 0;
27304273 1355range:
1356 return ERANGE;
3aee1200 1357
1358 /* Error handling */
1359fail:
1360 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1361 g_quark_to_string(tf->name));
1362 return EPERM;
6cd62ccf 1363}
1364
80da81ad 1365
3aee1200 1366int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
80da81ad 1367
3aee1200 1368 int err;
1369
1370 if(ep->tracefile != tf) {
1371 goto fail;
80da81ad 1372 }
1373
3aee1200 1374 err = map_block(tf, ep->block);
1375 if(unlikely(err)) {
1376 g_error("Can not map block");
1377 goto fail;
1378 }
1379
1380 tf->event.offset = ep->offset;
18206708 1381
3aee1200 1382 err = ltt_tracefile_read_update_event(tf);
1383 if(err) goto fail;
1384 err = ltt_tracefile_read_op(tf);
1385 if(err) goto fail;
80da81ad 1386
1387 return;
3aee1200 1388
1389fail:
1390 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1391 g_quark_to_string(tf->name));
1392}
1393
1394/* Calculate the real event time based on the buffer boundaries */
1395LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1396{
1397 LttTime time;
1398
1399 g_assert(tf->trace->has_tsc);
1400
1401 time = ltt_time_from_uint64(
50a17d39 1402 (guint64)(tf->buffer.tsc - tf->buffer.begin.cycle_count) *
1403 tf->buffer.nsecs_per_cycle);
3aee1200 1404 time = ltt_time_add(tf->buffer.begin.timestamp, time);
1405
1406 return time;
80da81ad 1407}
1408
eed2ef37 1409
1410/* Get the current event of the tracefile : valid until the next read */
1411LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1412{
1413 return &tf->event;
1414}
1415
1416
1417
6cd62ccf 1418/*****************************************************************************
1419 *Function name
3aee1200 1420 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1421 *Input params
1422 * t : tracefile
1423 *Return value
3aee1200 1424 *
1425 * Returns 0 if an event can be used in tf->event.
d822520b 1426 * Returns ERANGE on end of trace. The event in tf->event still can be used
1427 * (if the last block was not empty).
3aee1200 1428 * Returns EPERM on error.
1429 *
1430 * This function does make the tracefile event structure point to the event
1431 * currently pointed to by the tf->event.
1432 *
1433 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1434 * reinitialize it after an error if you want results to be coherent.
1435 * It would be the case if a end of trace last buffer has no event : the end
1436 * of trace wouldn't be returned, but an error.
1437 * We make the assumption there is at least one event per buffer.
6cd62ccf 1438 ****************************************************************************/
1439
3aee1200 1440int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1441{
963b5f2d 1442 int err;
6cd62ccf 1443
3aee1200 1444 err = ltt_tracefile_read_seek(tf);
1445 if(err) return err;
1446 err = ltt_tracefile_read_update_event(tf);
1447 if(err) return err;
1448 err = ltt_tracefile_read_op(tf);
1449 if(err) return err;
1450
1451 return 0;
1452}
1453
1454int ltt_tracefile_read_seek(LttTracefile *tf)
1455{
1456 int err;
1457
1458 /* Get next buffer until we finally have an event, or end of trace */
1459 while(1) {
1460 err = ltt_seek_next_event(tf);
1461 if(unlikely(err == ENOPROTOOPT)) {
1462 return EPERM;
bdc36259 1463 }
bdc36259 1464
3aee1200 1465 /* Are we at the end of the buffer ? */
1466 if(err == ERANGE) {
1467 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1468 return ERANGE;
1469 } else {
1470 /* get next block */
1471 err = map_block(tf, tf->buffer.index + 1);
1472 if(unlikely(err)) {
1473 g_error("Can not map block");
1474 return EPERM;
1475 }
1476 }
1477 } else break; /* We found an event ! */
1478 }
2dee981d 1479
3aee1200 1480 return 0;
1481}
18206708 1482
1483
3aee1200 1484/* do specific operation on events */
1485int ltt_tracefile_read_op(LttTracefile *tf)
1486{
1487 int err;
1488 LttFacility *f;
1489 void * pos;
1490 LttEvent *event;
1491
1492 event = &tf->event;
18206708 1493
3aee1200 1494 /* do event specific operation */
40331ba8 1495
3aee1200 1496 /* do something if its an heartbeat event : increment the heartbeat count */
1497 //if(event->facility_id == LTT_FACILITY_CORE)
1498 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1499 // tf->cur_heart_beat_number++;
1500
1501 return 0;
6cd62ccf 1502}
1503
6cd62ccf 1504
3aee1200 1505/* same as ltt_tracefile_read, but does not seek to the next event nor call
1506 * event specific operation. */
1507int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1508{
3aee1200 1509 int err;
1510 LttFacility *f;
1511 void * pos;
1512 LttEvent *event;
1513
1514 event = &tf->event;
eed2ef37 1515 pos = tf->buffer.head + event->offset;
3aee1200 1516
1517 /* Read event header */
1518
1519 //TODO align
1520
1521 if(tf->trace->has_tsc) {
16fcbb80 1522 if(tf->trace->has_heartbeat) {
1523 event->time.timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1524 pos);
1525 /* 32 bits -> 64 bits tsc */
1526 /* note : still works for seek and non seek cases. */
1527 if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1528 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1529 + 0x100000000ULL)
1530 | (guint64)event->time.timestamp;
1531 event->tsc = tf->buffer.tsc;
1532 } else {
1533 /* no overflow */
1534 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1535 | (guint64)event->time.timestamp;
1536 event->tsc = tf->buffer.tsc;
1537 }
1538 pos += sizeof(guint32);
3aee1200 1539 } else {
16fcbb80 1540 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1541 tf->buffer.tsc = event->tsc;
1542 pos += sizeof(guint64);
3aee1200 1543 }
16fcbb80 1544
3aee1200 1545 event->event_time = ltt_interpolate_time(tf, event);
3aee1200 1546 } else {
1547 event->time.delta.tv_sec = 0;
1548 event->time.delta.tv_nsec = ltt_get_uint32(LTT_GET_BO(tf),
1549 pos) * NSEC_PER_USEC;
1550 tf->buffer.tsc = 0;
1551 event->tsc = tf->buffer.tsc;
1552
1553 event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
1554 event->time.delta);
1555 pos += sizeof(guint32);
1556 }
1557
eed2ef37 1558 event->facility_id = *(guint8*)pos;
3aee1200 1559 pos += sizeof(guint8);
1560
eed2ef37 1561 event->event_id = *(guint8*)pos;
3aee1200 1562 pos += sizeof(guint8);
1563
b77d1b57 1564 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1565 pos += sizeof(guint16);
1566
3aee1200 1567 event->data = pos;
1568
77175651 1569 /* get the data size and update the event fields with the current
1570 * information */
eed2ef37 1571 ltt_update_event_size(tf);
77175651 1572
3aee1200 1573 return 0;
6cd62ccf 1574}
1575
507915ee 1576
6cd62ccf 1577/****************************************************************************
1578 *Function name
3aee1200 1579 * map_block : map a block from the file
6cd62ccf 1580 *Input Params
1581 * lttdes : ltt trace file
1582 * whichBlock : the block which will be read
1583 *return value
1584 * 0 : success
1585 * EINVAL : lseek fail
1586 * EIO : can not read from the file
1587 ****************************************************************************/
1588
3aee1200 1589static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1590{
b77d1b57 1591 int page_size = getpagesize();
3aee1200 1592 struct ltt_block_start_header *header;
1593
1594 g_assert(block_num < tf->num_blocks);
6cd62ccf 1595
f628823c 1596 if(tf->buffer.head != NULL) {
1597 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1598 g_warning("unmap size : %u\n",
1599 PAGE_ALIGN(tf->buf_size));
1600 perror("munmap error");
1601 g_assert(0);
1602 }
1603 }
1604
ac849774 1605
3aee1200 1606 /* Multiple of pages aligned head */
b77d1b57 1607 tf->buffer.head = mmap(0,
f628823c 1608 PAGE_ALIGN(tf->buf_size),
b77d1b57 1609 PROT_READ, MAP_PRIVATE, tf->fd,
f628823c 1610 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
3aee1200 1611
3865ea09 1612 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1613 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1614 g_assert(0);
3aee1200 1615 goto map_error;
6cd62ccf 1616 }
3865ea09 1617 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 1618
6cd62ccf 1619
3aee1200 1620 tf->buffer.index = block_num;
1621
1622 header = (struct ltt_block_start_header*)tf->buffer.head;
1623
1624 tf->buffer.begin.timestamp = ltt_get_time(LTT_GET_BO(tf),
1625 &header->begin.timestamp);
1626 tf->buffer.begin.timestamp.tv_nsec *= NSEC_PER_USEC;
50a17d39 1627 g_warning("block %u begin : %lu.%lu", block_num,
1628 tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
3aee1200 1629 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1630 &header->begin.cycle_count);
1631 tf->buffer.end.timestamp = ltt_get_time(LTT_GET_BO(tf),
1632 &header->end.timestamp);
1633 tf->buffer.end.timestamp.tv_nsec *= NSEC_PER_USEC;
50a17d39 1634 g_warning("block %u end : %lu.%lu", block_num,
1635 tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
3aee1200 1636 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1637 &header->end.cycle_count);
1638 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
1639 &header->lost_size);
6cd62ccf 1640
3aee1200 1641 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1642 tf->event.tsc = tf->buffer.tsc;
1643
1644 /* FIXME
1645 * eventually support variable buffer size : will need a partial pre-read of
1646 * the headers to create an index when we open the trace... eventually. */
f628823c 1647 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 1648 &header->buf_size));
507915ee 1649
3aee1200 1650 /* Now that the buffer is mapped, calculate the time interpolation for the
1651 * block. */
1652
1653 tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1654
1655 /* Make the current event point to the beginning of the buffer :
1656 * it means that the event read must get the first event. */
1657 tf->event.tracefile = tf;
1658 tf->event.block = block_num;
eed2ef37 1659 tf->event.offset = 0;
3aee1200 1660
1661 return 0;
6cd62ccf 1662
3aee1200 1663map_error:
1664 return -errno;
40331ba8 1665
6cd62ccf 1666}
1667
77175651 1668/* It will update the fields offsets too */
1669void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1670{
3aee1200 1671 ssize_t size = 0;
1672
1673 /* Specific handling of core events : necessary to read the facility control
1674 * tracefile. */
77175651 1675 LttFacility *f = ltt_trace_get_facility_by_num(tf->trace,
1676 tf->event.facility_id);
6cd62ccf 1677
3865ea09 1678 if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) {
1679 switch((enum ltt_core_events)tf->event.event_id) {
1680 case LTT_EVENT_FACILITY_LOAD:
b77d1b57 1681 size = strlen((char*)tf->event.data) + 1;
d2083cab 1682 g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
c4afd5d8 1683 size += sizeof(struct LttFacilityLoad);
3865ea09 1684 break;
1685 case LTT_EVENT_FACILITY_UNLOAD:
d2083cab 1686 g_debug("Update Event facility unload");
3865ea09 1687 size = sizeof(struct LttFacilityUnload);
1688 break;
1689 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
b77d1b57 1690 size = strlen((char*)tf->event.data) + 1;
d2083cab 1691 g_debug("Update Event facility load state dump of facility %s",
b77d1b57 1692 (char*)tf->event.data);
c4afd5d8 1693 size += sizeof(struct LttStateDumpFacilityLoad);
3865ea09 1694 break;
1695 case LTT_EVENT_HEARTBEAT:
d2083cab 1696 g_debug("Update Event heartbeat");
3865ea09 1697 size = sizeof(TimeHeartbeat);
1698 break;
1699 default:
1700 g_warning("Error in getting event size : tracefile %s, "
1701 "unknown event id %hhu in core facility.",
1702 g_quark_to_string(tf->name),
1703 tf->event.event_id);
1704 goto event_id_error;
2dee981d 1705
77175651 1706 }
3aee1200 1707 } else {
c4afd5d8 1708 if(!f->exists) {
1709 g_error("Unknown facility %hhu (0x%hhx) in tracefile %s",
1710 tf->event.facility_id,
1711 tf->event.facility_id,
1712 g_quark_to_string(tf->name));
1713 goto facility_error;
1714 }
1715
3aee1200 1716 LttEventType *event_type =
1717 ltt_facility_eventtype_get(f, tf->event.event_id);
c4afd5d8 1718
1719 if(!event_type) {
1720 g_error("Unknown event id %hhu in facility %s in tracefile %s",
1721 tf->event.event_id,
1722 g_quark_to_string(f->name),
1723 g_quark_to_string(tf->name));
1724 goto event_type_error;
1725 }
27304273 1726
1727 if(event_type->root_field)
1728 size = get_field_type_size(tf, event_type,
1729 0, 0, event_type->root_field, tf->event.data);
1730 else
1731 size = 0;
c4afd5d8 1732
f628823c 1733 g_debug("Event root field : f.e %hhu.%hhu size %zd",
1734 tf->event.facility_id,
c4afd5d8 1735 tf->event.event_id, size);
3aee1200 1736 }
3aee1200 1737
77175651 1738 tf->event.data_size = size;
eed2ef37 1739
b77d1b57 1740 /* Check consistency between kernel and LTTV structure sizes */
1741 g_assert(tf->event.data_size == tf->event.event_size);
1742
eed2ef37 1743 return;
1744
c4afd5d8 1745facility_error:
1746event_type_error:
3aee1200 1747event_id_error:
eed2ef37 1748 tf->event.data_size = 0;
6cd62ccf 1749}
1750
6cd62ccf 1751
3aee1200 1752/* Take the tf current event offset and use the event facility id and event id
1753 * to figure out where is the next event offset.
1754 *
1755 * This is an internal function not aiming at being used elsewhere : it will
1756 * not jump over the current block limits. Please consider using
1757 * ltt_tracefile_read to do this.
1758 *
1759 * Returns 0 on success
1760 * ERANGE if we are at the end of the buffer.
1761 * ENOPROTOOPT if an error occured when getting the current event size.
1762 */
1763static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1764{
3aee1200 1765 int ret = 0;
1766 void *pos;
1767 ssize_t event_size;
1768
1769 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1770 if(tf->event.offset == 0) {
3aee1200 1771 tf->event.offset += sizeof(struct ltt_block_start_header);
b77d1b57 1772
f628823c 1773 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
b77d1b57 1774 ret = ERANGE;
1775 }
3aee1200 1776 goto found;
1777 }
6cd62ccf 1778
6cd62ccf 1779
3aee1200 1780 pos = tf->event.data;
1781
77175651 1782 if(tf->event.data_size < 0) goto error;
3aee1200 1783
77175651 1784 pos += (size_t)tf->event.data_size;
3aee1200 1785
eed2ef37 1786 tf->event.offset = pos - tf->buffer.head;
cb03932a 1787
f628823c 1788 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
cb03932a 1789 ret = ERANGE;
1790 goto found;
1791 }
3aee1200 1792
1793found:
1794 return ret;
1795
1796error:
1797 g_error("Error in ltt_seek_next_event for tracefile %s",
1798 g_quark_to_string(tf->name));
1799 return ENOPROTOOPT;
6cd62ccf 1800}
1801
18206708 1802
6cd62ccf 1803/*****************************************************************************
1804 *Function name
3aee1200 1805 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
6cd62ccf 1806 *Input Params
1807 * t : tracefile
1808 ****************************************************************************/
1809
3aee1200 1810static double calc_nsecs_per_cycle(LttTracefile * tf)
6cd62ccf 1811{
963b5f2d 1812 LttTime lBufTotalTime; /* Total time for this buffer */
887208b7 1813 double lBufTotalNSec; /* Total time for this buffer in nsecs */
8ee1c3d5 1814 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1815
1816 /* Calculate the total time for this buffer */
d822520b 1817 lBufTotalTime = ltt_time_sub(tf->buffer.end.timestamp,
1818 tf->buffer.begin.timestamp);
6cd62ccf 1819
1820 /* Calculate the total cycles for this bufffer */
d822520b 1821 lBufTotalCycle = tf->buffer.end.cycle_count;
1822 lBufTotalCycle -= tf->buffer.begin.cycle_count;
6cd62ccf 1823
8ee1c3d5 1824 /* Convert the total time to double */
887208b7 1825 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
6cd62ccf 1826
3aee1200 1827 return lBufTotalNSec / (double)lBufTotalCycle;
8ee1c3d5 1828
3aee1200 1829}
1830#if 0
1831void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
1832{
1833 LttField * rootFld = evT->root_field;
1834 // rootFld->base_address = evD;
2dee981d 1835
3aee1200 1836 if(likely(rootFld))
1837 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
1838 evT, 0,0,rootFld, evD);
6cd62ccf 1839}
3aee1200 1840#endif //0
6cd62ccf 1841
3aee1200 1842/*****************************************************************************
6cd62ccf 1843 *Function name
3aee1200 1844 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 1845 *Input params
3aee1200 1846 * tracefile : opened trace file
1847 * event_type : the event type
6cd62ccf 1848 ****************************************************************************/
1849
3aee1200 1850void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 1851{
3aee1200 1852 LttField *field = event_type->root_field;
1853 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
1854
1855 if(likely(field))
1856 preset_field_type_size(tf, event_type, 0, 0,
1857 &fixed_root, &fixed_parent,
1858 field);
1859
1860}
e4eced0f 1861
bbf28e50 1862
3aee1200 1863/*****************************************************************************
1864 *Function name
1865 * preset_field_type_size : set the fixed sizes of the field type
1866 *Input params
1867 * tf : tracefile
1868 * event_type : event type
1869 * offset_root : offset from the root
1870 * offset_parent : offset from the parent
1871 * fixed_root : Do we know a fixed offset to the root ?
1872 * fixed_parent : Do we know a fixed offset to the parent ?
1873 * field : field
1874 ****************************************************************************/
1875void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
1876 off_t offset_root, off_t offset_parent,
1877 enum field_status *fixed_root, enum field_status *fixed_parent,
1878 LttField *field)
1879{
1880 enum field_status local_fixed_root, local_fixed_parent;
1881 guint i;
1882 LttType *type;
dfb73233 1883
3aee1200 1884 g_assert(field->fixed_root == FIELD_UNKNOWN);
1885 g_assert(field->fixed_parent == FIELD_UNKNOWN);
1886 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 1887
3aee1200 1888 type = field->field_type;
1889
1890 field->fixed_root = *fixed_root;
1891 if(field->fixed_root == FIELD_FIXED)
1892 field->offset_root = offset_root;
1893 else
1894 field->offset_root = 0;
1895
1896 field->fixed_parent = *fixed_parent;
1897 if(field->fixed_parent == FIELD_FIXED)
1898 field->offset_parent = offset_parent;
1899 else
1900 field->offset_parent = 0;
1901
1902 size_t current_root_offset;
1903 size_t current_offset;
1904 enum field_status current_child_status, final_child_status;
1905 size_t max_size;
1906
1907 switch(type->type_class) {
1908 case LTT_INT:
1909 case LTT_UINT:
1910 case LTT_FLOAT:
1911 case LTT_ENUM:
1912 field->field_size = ltt_type_size(tf->trace, type);
1913 field->fixed_size = FIELD_FIXED;
1914 break;
1915 case LTT_POINTER:
1916 field->field_size = (off_t)event_type->facility->pointer_size;
1917 field->fixed_size = FIELD_FIXED;
1918 break;
1919 case LTT_LONG:
1920 case LTT_ULONG:
cb03932a 1921 field->field_size = (off_t)event_type->facility->long_size;
3aee1200 1922 field->fixed_size = FIELD_FIXED;
1923 break;
1924 case LTT_SIZE_T:
1925 case LTT_SSIZE_T:
1926 case LTT_OFF_T:
1927 field->field_size = (off_t)event_type->facility->size_t_size;
1928 field->fixed_size = FIELD_FIXED;
1929 break;
1930 case LTT_SEQUENCE:
1931 local_fixed_root = FIELD_VARIABLE;
1932 local_fixed_parent = FIELD_VARIABLE;
1933 preset_field_type_size(tf, event_type,
1934 0, 0,
1935 &local_fixed_root, &local_fixed_parent,
1936 field->child[0]);
1937 field->fixed_size = FIELD_VARIABLE;
1938 field->field_size = 0;
27304273 1939 *fixed_root = FIELD_VARIABLE;
1940 *fixed_parent = FIELD_VARIABLE;
3aee1200 1941 break;
1942 case LTT_STRING:
1943 field->fixed_size = FIELD_VARIABLE;
1944 field->field_size = 0;
27304273 1945 *fixed_root = FIELD_VARIABLE;
1946 *fixed_parent = FIELD_VARIABLE;
3aee1200 1947 break;
1948 case LTT_ARRAY:
1949 local_fixed_root = FIELD_VARIABLE;
1950 local_fixed_parent = FIELD_VARIABLE;
1951 preset_field_type_size(tf, event_type,
1952 0, 0,
1953 &local_fixed_root, &local_fixed_parent,
1954 field->child[0]);
1955 field->fixed_size = field->child[0]->fixed_size;
27304273 1956 if(field->fixed_size == FIELD_FIXED) {
3aee1200 1957 field->field_size = type->element_number * field->child[0]->field_size;
27304273 1958 } else {
3aee1200 1959 field->field_size = 0;
27304273 1960 *fixed_root = FIELD_VARIABLE;
1961 *fixed_parent = FIELD_VARIABLE;
1962 }
3aee1200 1963 break;
1964 case LTT_STRUCT:
1965 current_root_offset = field->offset_root;
1966 current_offset = 0;
1967 current_child_status = FIELD_FIXED;
1968 for(i=0;i<type->element_number;i++) {
1969 preset_field_type_size(tf, event_type,
1970 current_root_offset, current_offset,
1971 fixed_root, &current_child_status,
1972 field->child[i]);
1973 if(current_child_status == FIELD_FIXED) {
1974 current_root_offset += field->child[i]->field_size;
1975 current_offset += field->child[i]->field_size;
1976 } else {
1977 current_root_offset = 0;
1978 current_offset = 0;
1979 }
1980 }
1981 if(current_child_status != FIELD_FIXED) {
1982 *fixed_parent = current_child_status;
1983 field->field_size = 0;
1984 field->fixed_size = current_child_status;
1985 } else {
1986 field->field_size = current_offset;
1987 field->fixed_size = FIELD_FIXED;
1988 }
1989 break;
1990 case LTT_UNION:
1991 current_root_offset = field->offset_root;
1992 current_offset = 0;
1993 max_size = 0;
1994 final_child_status = FIELD_FIXED;
1995 for(i=0;i<type->element_number;i++) {
1996 enum field_status current_root_child_status = FIELD_FIXED;
1997 enum field_status current_child_status = FIELD_FIXED;
1998 preset_field_type_size(tf, event_type,
1999 current_root_offset, current_offset,
2000 &current_root_child_status, &current_child_status,
2001 field->child[i]);
2002 if(current_child_status != FIELD_FIXED)
2003 final_child_status = current_child_status;
2004 else
2005 max_size = max(max_size, field->child[i]->field_size);
2006 }
2007 if(final_child_status != FIELD_FIXED) {
2008 *fixed_root = final_child_status;
2009 *fixed_parent = final_child_status;
2010 field->field_size = 0;
2011 field->fixed_size = current_child_status;
2012 } else {
2013 field->field_size = max_size;
2014 field->fixed_size = FIELD_FIXED;
2015 }
2016 break;
dfb73233 2017 }
2018
6cd62ccf 2019}
2020
3aee1200 2021
77175651 2022/*****************************************************************************
2023 *Function name
2024 * check_fields_compatibility : Check for compatibility between two fields :
2025 * do they use the same inner structure ?
2026 *Input params
2027 * event_type1 : event type
2028 * event_type2 : event type
2029 * field1 : field
2030 * field2 : field
2031 *Returns : 0 if identical
2032 * 1 if not.
2033 ****************************************************************************/
2034gint check_fields_compatibility(LttEventType *event_type1,
2035 LttEventType *event_type2,
2036 LttField *field1, LttField *field2)
2037{
2038 guint different = 0;
2039 enum field_status local_fixed_root, local_fixed_parent;
2040 guint i;
2041 LttType *type1;
2042 LttType *type2;
2043
2044 if(field1 == NULL) {
2045 if(field2 == NULL) goto end;
2046 else {
2047 different = 1;
2048 goto end;
2049 }
2050 } else if(field2 == NULL) {
2051 different = 1;
2052 goto end;
2053 }
2054
2055 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2056 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2057 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2058 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2059 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2060 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2061
2062 type1 = field1->field_type;
2063 type2 = field2->field_type;
2064
2065 size_t current_root_offset;
2066 size_t current_offset;
2067 enum field_status current_child_status, final_child_status;
2068 size_t max_size;
2069
2070 if(type1->type_class != type2->type_class) {
2071 different = 1;
2072 goto end;
2073 }
2074 if(type1->element_name != type2->element_name) {
2075 different = 1;
2076 goto end;
2077 }
2078
2079 switch(type1->type_class) {
2080 case LTT_INT:
2081 case LTT_UINT:
2082 case LTT_FLOAT:
2083 case LTT_POINTER:
2084 case LTT_LONG:
2085 case LTT_ULONG:
2086 case LTT_SIZE_T:
2087 case LTT_SSIZE_T:
2088 case LTT_OFF_T:
2089 if(field1->field_size != field2->field_size) {
2090 different = 1;
2091 goto end;
2092 }
2093 break;
2094 case LTT_ENUM:
2095 if(type1->element_number != type2->element_number) {
2096 different = 1;
2097 goto end;
2098 }
2099 for(i=0;i<type1->element_number;i++) {
2100 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2101 different = 1;
2102 goto end;
2103 }
2104 }
2105 break;
2106 case LTT_SEQUENCE:
2107 /* Two elements : size and child */
2108 g_assert(type1->element_number != type2->element_number);
2109 for(i=0;i<type1->element_number;i++) {
2110 if(check_fields_compatibility(event_type1, event_type2,
2111 field1->child[0], field2->child[0])) {
2112 different = 1;
2113 goto end;
2114 }
2115 }
2116 break;
2117 case LTT_STRING:
2118 break;
2119 case LTT_ARRAY:
2120 if(field1->field_size != field2->field_size) {
2121 different = 1;
2122 goto end;
2123 }
2124 /* Two elements : size and child */
2125 g_assert(type1->element_number != type2->element_number);
2126 for(i=0;i<type1->element_number;i++) {
2127 if(check_fields_compatibility(event_type1, event_type2,
2128 field1->child[0], field2->child[0])) {
2129 different = 1;
2130 goto end;
2131 }
2132 }
2133 break;
2134 case LTT_STRUCT:
2135 case LTT_UNION:
2136 if(type1->element_number != type2->element_number) {
2137 different = 1;
2138 break;
2139 }
2140 for(i=0;i<type1->element_number;i++) {
2141 if(check_fields_compatibility(event_type1, event_type2,
2142 field1->child[0], field2->child[0])) {
2143 different = 1;
2144 goto end;
2145 }
2146 }
2147 break;
2148 }
2149end:
2150 return different;
2151}
2152
2153
2154
3aee1200 2155
2156#if 0
6cd62ccf 2157/*****************************************************************************
2158 *Function name
2159 * getFieldtypeSize: get the size of the field type (primitive type)
2160 *Input params
6cd62ccf 2161 * evT : event type
2162 * offsetRoot : offset from the root
2163 * offsetParent : offset from the parrent
2164 * fld : field
2165 * evD : event data, it may be NULL
2166 *Return value
2167 * int : size of the field
2168 ****************************************************************************/
2169
3aee1200 2170static inline gint getFieldtypeSize(LttTracefile *tf,
21182d4a 2171 LttEventType * evT, gint offsetRoot,
3aee1200 2172 gint offsetParent, LttField * fld, void *evD)
6cd62ccf 2173{
21182d4a 2174 gint size, size1, element_number, i, offset1, offset2;
963b5f2d 2175 LttType * type = fld->field_type;
6cd62ccf 2176
3aee1200 2177 /* This likely has been tested with gcov : half of them.. */
2178 if(unlikely(fld->field_fixed == 1)){
2179 /* tested : none */
2180 if(unlikely(fld == evT->root_field)) {
2181 size = fld->field_size;
2182 goto end_getFieldtypeSize;
21182d4a 2183 }
3aee1200 2184 }
6cd62ccf 2185
3aee1200 2186 /* From gcov profiling : half string, half struct, can we gain something
2187 * from that ? (Mathieu) */
2188 switch(type->type_class) {
2189 case LTT_ARRAY:
2190 element_number = (int) type->element_number;
2191 if(fld->field_fixed == -1){
2192 size = getFieldtypeSize(tf, evT, offsetRoot,
2193 0,fld->child[0], NULL);
2194 if(size == 0){ //has string or sequence
cf74a6f1 2195 fld->field_fixed = 0;
3aee1200 2196 }else{
2197 fld->field_fixed = 1;
2198 size *= element_number;
a0d63196 2199 }
3aee1200 2200 }else if(fld->field_fixed == 0){// has string or sequence
cf74a6f1 2201 size = 0;
3aee1200 2202 for(i=0;i<element_number;i++){
2203 size += getFieldtypeSize(tf, evT, offsetRoot+size,size,
2204 fld->child[0], evD+size);
cf74a6f1 2205 }
3aee1200 2206 }else size = fld->field_size;
2207 if(unlikely(!evD)){
a0d63196 2208 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2209 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
3aee1200 2210 }
cf74a6f1 2211
3aee1200 2212 break;
2213
2214 case LTT_SEQUENCE:
2215 size1 = (int) ltt_type_size(fac, type);
2216 if(fld->field_fixed == -1){
2217 fld->sequ_number_size = size1;
2218 fld->field_fixed = 0;
2219 size = getFieldtypeSize(evT, offsetRoot,
2220 0,fld->child[0], NULL);
2221 fld->element_size = size;
2222 }else{//0: sequence
2223 element_number = getIntNumber(tf,size1,evD);
2224 type->element_number = element_number;
2225 if(fld->element_size > 0){
2226 size = element_number * fld->element_size;
2227 }else{//sequence has string or sequence
2228 size = 0;
cf74a6f1 2229 for(i=0;i<element_number;i++){
3aee1200 2230 size += getFieldtypeSize(tf, evT,
2231 offsetRoot+size+size1,size+size1,
2232 fld->child[0], evD+size+size1);
cf74a6f1 2233 }
3aee1200 2234 }
2235 size += size1;
2236 }
2237 if(unlikely(!evD)){
d37b2aaa 2238 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2239 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
3aee1200 2240 }
cf74a6f1 2241
3aee1200 2242 break;
2243
2244 case LTT_STRING:
2245 size = 0;
2246 if(fld->field_fixed == -1){
2247 fld->field_fixed = 0;
2248 }else{//0: string
2249 /* Hope my implementation is faster than strlen (Mathieu) */
2250 char *ptr=(char*)evD;
2251 size = 1;
2252 /* from gcov : many many strings are empty, make it the common case.*/
2253 while(unlikely(*ptr != '\0')) { size++; ptr++; }
2254 //size = ptr - (char*)evD + 1; //include end : '\0'
2255 }
2256 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2257 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2258
2259 break;
2260
2261 case LTT_STRUCT:
2262 element_number = (int) type->element_number;
2263 size = 0;
2264 /* tested with gcov */
2265 if(unlikely(fld->field_fixed == -1)){
2266 offset1 = offsetRoot;
2267 offset2 = 0;
2268 for(i=0;i<element_number;i++){
2269 size1=getFieldtypeSize(tf, evT,offset1,offset2,
2270 fld->child[i], NULL);
2271 if(likely(size1 > 0 && size >= 0)){
2272 size += size1;
2273 if(likely(offset1 >= 0)) offset1 += size1;
2274 offset2 += size1;
2275 }else{
2276 size = -1;
2277 offset1 = -1;
2278 offset2 = -1;
2279 }
a0d63196 2280 }
3aee1200 2281 if(unlikely(size == -1)){
2282 fld->field_fixed = 0;
2283 size = 0;
2284 }else fld->field_fixed = 1;
2285 }else if(likely(fld->field_fixed == 0)){
2286 offset1 = offsetRoot;
2287 offset2 = 0;
2288 for(i=0;unlikely(i<element_number);i++){
2289 size=getFieldtypeSize(tf, evT, offset1, offset2,
2290 fld->child[i], evD+offset2);
2291 offset1 += size;
2292 offset2 += size;
2293 }
2294 size = offset2;
2295 }else size = fld->field_size;
2296 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2297 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2298 break;
2299
2300 default:
2301 if(unlikely(fld->field_fixed == -1)){
2302 size = (int) ltt_type_size(LTT_GET_BO(tf), type);
2303 fld->field_fixed = 1;
2304 }else size = fld->field_size;
2305 if(unlikely(!evD)){
2306 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2307 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2308 }
2309 break;
cf74a6f1 2310 }
2311
6cd62ccf 2312 fld->offset_root = offsetRoot;
2313 fld->offset_parent = offsetParent;
6cd62ccf 2314 fld->field_size = size;
2315
21182d4a 2316end_getFieldtypeSize:
2317
6cd62ccf 2318 return size;
2319}
3aee1200 2320#endif //0
6cd62ccf 2321
2322/*****************************************************************************
2323 *Function name
eed2ef37 2324 * ltt_get_int : get an integer number
6cd62ccf 2325 *Input params
3aee1200 2326 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2327 * size : the size of the integer
3aee1200 2328 * ptr : the data pointer
6cd62ccf 2329 *Return value
cf74a6f1 2330 * gint64 : a 64 bits integer
6cd62ccf 2331 ****************************************************************************/
2332
eed2ef37 2333gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2334{
3aee1200 2335 gint64 val;
cf74a6f1 2336
2337 switch(size) {
3aee1200 2338 case 1: val = *((gint8*)data); break;
2339 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2340 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2341 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2342 default: val = ltt_get_int64(reverse_byte_order, data);
2343 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2344 break;
2345 }
2346
3aee1200 2347 return val;
6cd62ccf 2348}
3aee1200 2349
6cd62ccf 2350/*****************************************************************************
2351 *Function name
eed2ef37 2352 * ltt_get_uint : get an unsigned integer number
6cd62ccf 2353 *Input params
3aee1200 2354 * reverse_byte_order: must we reverse the byte order ?
2355 * size : the size of the integer
2356 * ptr : the data pointer
2357 *Return value
2358 * guint64 : a 64 bits unsigned integer
6cd62ccf 2359 ****************************************************************************/
2360
eed2ef37 2361guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2362{
3aee1200 2363 guint64 val;
2364
2365 switch(size) {
2366 case 1: val = *((gint8*)data); break;
2367 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2368 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2369 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2370 default: val = ltt_get_uint64(reverse_byte_order, data);
2371 g_critical("get_uint : unsigned integer size %d unknown",
2372 size);
2373 break;
2374 }
2375
2376 return val;
6cd62ccf 2377}
3aee1200 2378
2379
a5dcde2f 2380/* get the node name of the system */
2381
2382char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2383{
2384 return s->node_name;
2385}
2386
2387
2388/* get the domain name of the system */
2389
2390char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2391{
2392 return s->domain_name;
2393}
2394
2395
2396/* get the description of the system */
2397
2398char * ltt_trace_system_description_description (LttSystemDescription * s)
2399{
2400 return s->description;
2401}
2402
2403
2404/* get the start time of the trace */
2405
2406LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
2407{
2408 return s->trace_start;
2409}
2410
18206708 2411
2412LttTracefile *ltt_tracefile_new()
2413{
2414 return g_new(LttTracefile, 1);
2415}
2416
2417void ltt_tracefile_destroy(LttTracefile *tf)
2418{
2419 g_free(tf);
2420}
2421
2422void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
2423{
2424 *dest = *src;
2425}
2426
3aee1200 2427/* Before library loading... */
2428
2429static void __attribute__((constructor)) init(void)
2430{
2431 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2432 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2433
3865ea09 2434 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
3aee1200 2435}
2436
This page took 0.155759 seconds and 4 git commands to generate.