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