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