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