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