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