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