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