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