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