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