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