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