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