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