facility work in progress
[lttv.git] / ltt / branches / poly / ltt-newlib / tracefile.c
CommitLineData
54ecbf38 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;
57
58#ifndef g_open
59#define g_open open
60#endif
61
62
63#define __UNUSED__ __attribute__((__unused__))
64
65#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
66#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
67
68#define g_close close
69
70/* obtain the time of an event */
71
72static inline LttTime getEventTime(LttTracefile * tf);
73
74
75/* set the offset of the fields belonging to the event,
76 need the information of the archecture */
77void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
78
79/* get the size of the field type according to the archtecture's
80 size and endian type(info of the archecture) */
81static inline gint getFieldtypeSize(LttTracefile * tf,
82 LttEventType * evT, gint offsetRoot,
83 gint offsetParent, LttField *fld, void *evD, LttTrace* t);
84
2896f39c 85/* map a fixed size or a block information from the file (fd) */
54ecbf38 86int map_block(LttTracefile * tf, unsigned int block_num);
87
88/* calculate cycles per nsec for current block */
89void getCyclePerNsec(LttTracefile * t);
90
54ecbf38 91/* go to the next event */
2896f39c 92static int ltt_seek_next_event(LttTracefile *tf);
54ecbf38 93
94/* Functions to parse system.xml file (using glib xml parser) */
95static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
96 const gchar *element_name,
97 const gchar **attribute_names,
98 const gchar **attribute_values,
99 gpointer user_data,
100 GError **error)
101{
102 int i=0;
103 LttSystemDescription* des = (LttSystemDescription* )user_data;
104 if(strcmp("system", element_name)){
105 *error = g_error_new(G_MARKUP_ERROR,
106 G_LOG_LEVEL_WARNING,
107 "This is not system.xml file");
108 return;
109 }
110
111 while(attribute_names[i]){
112 if(strcmp("node_name", attribute_names[i])==0){
113 des->node_name = g_strdup(attribute_values[i]);
114 }else if(strcmp("domainname", attribute_names[i])==0){
115 des->domain_name = g_strdup(attribute_values[i]);
116 }else if(strcmp("cpu", attribute_names[i])==0){
117 des->nb_cpu = atoi(attribute_values[i]);
118 }else if(strcmp("arch_size", attribute_names[i])==0){
119 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
120 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
121 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
122 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
123 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
124 }else if(strcmp("endian", attribute_names[i])==0){
125 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
126 des->endian = LTT_LITTLE_ENDIAN;
127 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
128 des->endian = LTT_BIG_ENDIAN;
129 }else if(strcmp("kernel_name", attribute_names[i])==0){
130 des->kernel_name = g_strdup(attribute_values[i]);
131 }else if(strcmp("kernel_release", attribute_names[i])==0){
132 des->kernel_release = g_strdup(attribute_values[i]);
133 }else if(strcmp("kernel_version", attribute_names[i])==0){
134 des->kernel_version = g_strdup(attribute_values[i]);
135 }else if(strcmp("machine", attribute_names[i])==0){
136 des->machine = g_strdup(attribute_values[i]);
137 }else if(strcmp("processor", attribute_names[i])==0){
138 des->processor = g_strdup(attribute_values[i]);
139 }else if(strcmp("hardware_platform", attribute_names[i])==0){
140 des->hardware_platform = g_strdup(attribute_values[i]);
141 }else if(strcmp("operating_system", attribute_names[i])==0){
142 des->operating_system = g_strdup(attribute_values[i]);
143 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
144 des->ltt_major_version = atoi(attribute_values[i]);
145 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
146 des->ltt_minor_version = atoi(attribute_values[i]);
147 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
148 des->ltt_block_size = atoi(attribute_values[i]);
149 }else{
150 *error = g_error_new(G_MARKUP_ERROR,
151 G_LOG_LEVEL_WARNING,
152 "Not a valid attribute");
153 return;
154 }
155 i++;
156 }
157}
158
159static void parser_characters (GMarkupParseContext __UNUSED__ *context,
160 const gchar *text,
161 gsize __UNUSED__ text_len,
162 gpointer user_data,
163 GError __UNUSED__ **error)
164{
165 LttSystemDescription* des = (LttSystemDescription* )user_data;
166 des->description = g_strdup(text);
167}
168
169
170/*****************************************************************************
171 *Function name
172 * ltt_tracefile_open : open a trace file, construct a LttTracefile
173 *Input params
174 * t : the trace containing the tracefile
175 * fileName : path name of the trace file
176 * tf : the tracefile structure
177 *Return value
178 * : 0 for success, -1 otherwise.
179 ****************************************************************************/
180
181int ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
182{
183 struct stat lTDFStat; /* Trace data file status */
184 struct ltt_block_start_header *header;
185
186 //open the file
187 tf->name = g_quark_from_string(fileName);
188 tf->trace = t;
189 tf->fd = g_open(fileName, O_RDONLY, 0);
190 if(tf->fd < 0){
191 g_warning("Unable to open input data file %s\n", fileName);
192 goto end;
193 }
194
195 // Get the file's status
196 if(fstat(tf->fd, &lTDFStat) < 0){
197 g_warning("Unable to get the status of the input data file %s\n", fileName);
198 goto close_file;
199 }
200
201 // Is the file large enough to contain a trace
202 if(lTDFStat.st_size < (off_t)(sizeof(BlockStart))){
203 g_print("The input data file %s does not contain a trace\n", fileName);
204 goto close_file;
205 }
206
207 /* Temporarily map the buffer start header to get trace information */
208 /* Multiple of pages aligned head */
209 tf->buffer.head = mmap(0, sizeof(struct ltt_block_start_header), PROT_READ,
210 tf->fd, 0);
211 if(tf->buffer == NULL) {
212 perror("Error in allocating memory for buffer of tracefile %s\n", fileName);
213 goto close_file;
214 }
215 g_assert(tf->buffer.head & (8-1) == 0); // make sure it's aligned.
216
217 header = (struct ltt_block_start_header*)tf->buffer.head;
218
219 if(header->traceset.magic_number == LTT_MAGIC_NUMBER)
220 tf->reverse_bo = 0;
221 else if(header->traceset.magic_number == LTT_REV_MAGIC_NUMBER)
222 tf->reverse_bo = 1;
223 else /* invalid magic number, bad tracefile ! */
224 goto unmap_file;
225
226 //store the size of the file
227 tf->file_size = lTDFStat.st_size;
228 tf->block_size = header->buf_size;
229 tf->block_number = tf->file_size / tf->block_size;
230
231 vfree(tf->buffer.head);
232 tf->buffer.head = NULL;
233
234 //read the first block
235 if(map_block(tf,0)) {
236 perror("Cannot map block %u for tracefile %s\n", 0, fileName);
237 goto close_file;
238 }
239
240 return 0;
241
242 /* Error */
243unmap_file:
244 munmap(tf->buffer.head, sizeof(struct ltt_block_start_header));
245close_file:
246 g_close(tf->fd);
247end:
248 return -1;
249}
250
251
252/*****************************************************************************
253 *Open control and per cpu tracefiles
254 ****************************************************************************/
255
256void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
257{
258 LttTracefile * tf;
259 tf = ltt_tracefile_open(t,tracefile_name);
260 if(!tf) return;
261 t->per_cpu_tracefile_number++;
262 g_ptr_array_add(t->per_cpu_tracefiles, tf);
263}
264
265gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
266{
267 LttTracefile * tf;
268 LttEvent ev;
269 LttFacility * f;
270 void * pos;
271 FacilityLoad fLoad;
272 unsigned int i;
273
274 tf = ltt_tracefile_open(t,control_name);
275 if(!tf) {
276 g_warning("ltt_tracefile_open_control : bad file descriptor");
277 return -1;
278 }
279 t->control_tracefile_number++;
280 g_ptr_array_add(t->control_tracefiles,tf);
281
282 //parse facilities tracefile to get base_id
283 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
284 while(1){
285 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
286
287 if(ev.event_id == TRACE_FACILITY_LOAD){
288 pos = ev.data;
289 fLoad.name = (gchar*)pos;
290 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
291 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
292
293 for(i=0;i<t->facility_number;i++){
294 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
295 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
296 f->base_id = fLoad.base_code;
297 break;
298 }
299 }
300 if(i==t->facility_number) {
301 g_warning("Facility: %s, checksum: %u is not found",
302 fLoad.name,(unsigned int)fLoad.checksum);
303 return -1;
304 }
305 }else if(ev.event_id == TRACE_BLOCK_START){
306 continue;
307 }else if(ev.event_id == TRACE_BLOCK_END){
308 break;
309 }else {
310 g_warning("Not valid facilities trace file");
311 return -1;
312 }
313 }
314 }
315 return 0;
316}
317
318/*****************************************************************************
319 *Function name
320 * ltt_tracefile_close: close a trace file,
321 *Input params
322 * t : tracefile which will be closed
323 ****************************************************************************/
324
325void ltt_tracefile_close(LttTracefile *t)
326{
327 if(t->buffer.head != NULL)
328 munmap(t->buffer.head, t->buf_size);
329 g_close(t->fd);
330}
331
332
333/*****************************************************************************
334 *Get system information
335 ****************************************************************************/
336gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
337{
338 int fd;
339 GIOChannel *iochan;
340 gchar *buf = NULL;
341 gsize length;
342
343 GMarkupParseContext * context;
344 GError * error = NULL;
345 GMarkupParser markup_parser =
346 {
347 parser_start_element,
348 NULL,
349 parser_characters,
350 NULL, /* passthrough */
351 NULL /* error */
352 };
353
354 fd = g_open(pathname, O_RDONLY, 0);
355 if(fd == -1){
356 g_warning("Can not open file : %s\n", pathname);
357 return -1;
358 }
359
360 iochan = g_io_channel_unix_new(fd);
361
362 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
363
364 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
365 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
366 != G_IO_STATUS_EOF) {
367
368 if(error != NULL) {
369 g_warning("Can not read xml file: \n%s\n", error->message);
370 g_error_free(error);
371 }
372 if(!g_markup_parse_context_parse(context, buf, length, &error)){
373 if(error != NULL) {
374 g_warning("Can not parse xml file: \n%s\n", error->message);
375 g_error_free(error);
376 }
377 g_markup_parse_context_free(context);
378
379 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
380 if(error != NULL) {
381 g_warning("Can not close file: \n%s\n", error->message);
382 g_error_free(error);
383 }
384
385 close(fd);
386 return -1;
387 }
388 }
389 g_markup_parse_context_free(context);
390
391 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
392 if(error != NULL) {
393 g_warning("Can not close file: \n%s\n", error->message);
394 g_error_free(error);
395 }
396
397 g_close(fd);
398
399 g_free(buf);
400 return 0;
401}
402
403/*****************************************************************************
404 *The following functions get facility/tracefile information
405 ****************************************************************************/
406
407gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
408{
409 GDir * dir;
410 const gchar * name;
411 unsigned int i,j;
412 LttFacility * f;
413 LttEventType * et;
414 gchar fullname[DIR_NAME_SIZE];
415 GError * error = NULL;
416
417 dir = g_dir_open(eventdefs, 0, &error);
418
419 if(error != NULL) {
420 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
421 g_error_free(error);
422 return -1;
423 }
424
425 while((name = g_dir_read_name(dir)) != NULL){
426 if(!g_pattern_match_simple("*.xml", name)) continue;
427 strcpy(fullname,eventdefs);
428 strcat(fullname,name);
429 ltt_facility_open(t,fullname);
430 }
431 g_dir_close(dir);
432
433 for(j=0;j<t->facility_number;j++){
434 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
435 for(i=0; i<f->event_number; i++){
436 et = f->events[i];
437 setFieldsOffset(NULL, et, NULL, t);
438 }
439 }
440 return 0;
441}
442
443/*****************************************************************************
444 *A trace is specified as a pathname to the directory containing all the
445 *associated data (control tracefiles, per cpu tracefiles, event
446 *descriptions...).
447 *
448 *When a trace is closed, all the associated facilities, types and fields
449 *are released as well.
450 */
451
452
453/****************************************************************************
454 * get_absolute_pathname
455 *
456 * return the unique pathname in the system
457 *
458 * MD : Fixed this function so it uses realpath, dealing well with
459 * forgotten cases (.. were not used correctly before).
460 *
461 ****************************************************************************/
462void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
463{
464 abs_pathname[0] = '\0';
465
466 if ( realpath (pathname, abs_pathname) != NULL)
467 return;
468 else
469 {
470 /* error, return the original path unmodified */
471 strcpy(abs_pathname, pathname);
472 return;
473 }
474 return;
475}
476
477/* Search for something like : .*_.*
478 *
479 * The left side is the name, the right side is the number.
480 */
481
482int get_tracefile_name_number(const gchar *raw_name,
483 GQuark *name,
484 guint *num)
485{
486 guint raw_name_len = strlen(raw_name);
487 gchar char_name[PATH_MAX]
488 gchar *digit_begin;
489 int i;
490 int underscore_pos;
491 long int cpu_num;
492 gchar *endptr;
493
494 for(i=raw_name_len-1;i>=0;i--) {
495 if(raw_name[i] == '_') break;
496 }
497 if(i==0) /* Either not found or name length is 0 */
498 return -1;
499 underscore_pos = i;
500
501 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
502
503 if(endptr == raw_name+underscore_pos+1)
504 return -1; /* No digit */
505 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
506 return -1; /* underflow / overflow */
507
508 char_name = strncpy(char_name, raw_name, underscore_pos);
509
510 *name = g_quark_from_string(char_name);
511 *num = cpu_num;
512
513 return 0;
514}
515
516
517void ltt_tracefile_group_destroy(gpointer data)
518{
519 GArray *group = (GArray *)data;
520 int i;
521 LttTracefile *tf;
522
523 for(i=0; i<group->len; i++) {
524 tf = &g_array_index (group, LttTracefile, i);
525 if(tf->cpu_online)
526 ltt_tracefile_close(tf);
527 }
2896f39c 528 g_array_free(group, TRUE);
54ecbf38 529}
530
531gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
532{
533 GArray *group = (GArray *)data;
534 int i;
535 LttTracefile *tf;
536
537 for(i=0; i<group->len; i++) {
538 tf = &g_array_index (group, LttTracefile, i);
539 if(tf->cpu_online) return 1;
540 }
541 return 0;
542}
543
544
545/* Open each tracefile under a specific directory. Put them in a
546 * GData : permits to access them using their tracefile group pathname.
547 * i.e. access control/modules tracefile group by index :
548 * "control/module".
549 *
550 * A tracefile group is simply an array where all the per cpu tracefiles sits.
551 */
552
553static int open_tracefiles(LttTrace *trace, char *root_path, GData *tracefiles)
554{
555 DIR *dir = opendir(root_path);
556 struct dirent *entry;
557 struct stat stat_buf;
558 int ret;
559 char path[PATH_MAX];
560 int path_len;
561 char *path_ptr;
562
563 if(channel_dir == NULL) {
564 perror(subchannel_name);
565 return ENOENT;
566 }
567
568 strncpy(path, root_path, PATH_MAX-1);
569 path_len = strlen(path);
570 path[path_len] = '/';
571 path_len++;
572 path_ptr = path + path_len;
573
574 while((entry = readdir(channel_dir)) != NULL) {
575
576 if(entry->d_name[0] == '.') continue;
577
578 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
579
580 ret = stat(path, &stat_buf);
581 if(ret == -1) {
582 perror(path);
583 continue;
584 }
585
586 g_debug("Tracefile file or directory : %s\n", path);
587
588 if(S_ISDIR(stat_buf.st_mode)) {
589
590 g_debug("Entering subdirectory...\n");
591 ret = open_tracefiles(path, tracefiles);
592 if(ret < 0) continue;
593 } else if(S_ISREG(stat_buf.st_mode)) {
594 g_debug("Opening file.\n");
595
596 GQuark name;
597 guint num;
598 GArray *group;
599 LttTracefile *tf;
600 guint len;
601
602 if(get_tracefile_name_number(path, &name, &num))
603 continue; /* invalid name */
604
605 group = g_datalist_get_data(tracefiles, name);
606 if(group == NULL) {
607 /* Elements are automatically cleared when the array is allocated.
608 * It makes the cpu_online variable set to 0 : cpu offline, by default.
609 */
610 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
611 g_datalist_set_data_full(tracefiles, name,
612 group, ltt_tracefile_group_destroy);
613 }
614 /* Add the per cpu tracefile to the named group */
615 unsigned int old_len = group->len;
616 if(num+1 > old_len)
2896f39c 617 group = g_array_set_size(group, num+1);
54ecbf38 618 tf = &g_array_index (group, LttTracefile, num);
619
620 if(ltt_tracefile_open(trace, path, tf)) {
621 g_info("Error opening tracefile %s", path);
622 g_array_set_size(group, old_len);
623
624 if(!ltt_tracefile_group_has_cpu_online(group))
625 g_datalist_remove_data(tracefiles, name);
626
627 continue; /* error opening the tracefile : bad magic number ? */
628 }
629 tf->cpu_online = 1;
630 }
631 }
632
633 closedir(dir);
634
635 return 0;
636}
637
2896f39c 638/* ltt_get_facility_description
639 *
640 * Opens the trace corresponding to the requested facility (identified by fac_id
641 * and checksum).
642 *
643 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
644 *
645 * Returns 0 on success, or 1 on failure.
646 */
647
648static int ltt_get_facility_description(LttFacility *f,
649 LttTrace *t)
650{
651 char desc_file_name[PATH_MAX];
652 char *text;
653 guint textlen;
654 gint err;
655
656 text = g_quark_to_string(t->pathname);
657 textlen = strlen(text);
658
659 if(textlen >= PATH_MAX) goto name_error;
660 strcpy(desc_file_name, text);
661
662 text = "/eventdefs/";
663 textlen+=strlen(text);
664 if(textlen >= PATH_MAX) goto name_error;
665 strcat(desc_file_name, text);
666
667 text = g_quark_to_string(f->name);
668 textlen+=strlen(text);
669 if(textlen >= PATH_MAX) goto name_error;
670 strcat(desc_file_name, text);
671
672 text = "_";
673 textlen+=strlen(text);
674 if(textlen >= PATH_MAX) goto name_error;
675 strcat(desc_file_name, text);
676
677 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
678 "%u", f->checksum);
679 if(err) goto name_error;
680
681 textlen=strlen(desc_file_name);
682
683 text = ".xml";
684 textlen+=strlen(text);
685 if(textlen >= PATH_MAX) goto name_error;
686 strcat(desc_file_name, text);
687
688 err = ltt_facility_open(f, t, desc_file_name);
689 if(err) goto facility_error;
690
691 return 0;
692
693facility_error:
694name_error:
695 return 1;
696}
697
698static void ltt_tracefile_ids_destroy(gpointer data)
699{
700 GArray *fac_ids = (GArray *)data;
701 int i;
702 LttFacility *fac;
703
704 for(i=0; i<group->len; i++) {
705 fac = &g_array_index (fac_ids, LttFacility, i);
706 ltt_facility_close(fac);
707 }
708
709 g_array_free(array, TRUE);
710}
711
712
713/* Presumes the tracefile is already seeked at the beginning. It makes sense,
714 * because it must be done just after the opening */
715static int ltt_process_facility_tracefile(LttTracefile *tf)
716{
717 int err;
718 LttFacility *fac;
719 GArray *fac_ids;
720
721 while(1) {
722 err = ltt_tracefile_read_seek(tf);
723 if(err == EPERM) goto seek_error;;
724 else if(err == ERANGE) break; /* End of tracefile */
725
726 err = ltt_tracefile_read_update_event(tf);
727 if(err) goto update_error;
728
729 /* We are on a facility load/or facility unload/ or heartbeat event */
730 /* The rules are :
731 * * facility 0 is hardcoded : this is the core facility. It will be shown
732 * in the facility array though, and is shown as "loaded builtin" in the
733 * trace.
734 * It contains event :
735 * 0 : facility load
736 * 1 : facility unload
737 * 2 : state dump facility load
738 * Facility 1 : (heartbeat)
739 * 0 : heartbeat
740 */
741 if(tf->event.facility_id > 1) { /* Should only contain core and heartbeat
742 facilities */
743 g_warning("Error in processing facility file %s, "
744 "should not contain facility id %u.", g_quark_to_string(tf->name),
745 tf->event.facility_id);
746 err = EPERM;
747 goto fac_id_error;
748 } else if(tf->event.facility_id == 0) {
749
750 // FIXME align
751 switch((enum ltt_core_events)tf->event.event_id) {
752 case LTT_EVENT_FACILITY_LOAD:
753 struct LttFacilityLoad *fac_load_data =
754 (struct LttFacilityLoad *)tf->event.data;
755 char *fac_name =
756 (char*)(tf->event.data + sizeof(struct LttFacilityLoad));
757 fac = &g_array_index (tf->facilities_by_num, LttTracefile,
758 tf->event.);
759 g_assert(fac->exists == 0);
760 fac->name = g_quark_from_string(fac_name);
761 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
762 fac_load_data->checksum);
763 fac->id = ltt_get_uint8(LTT_GET_BO(tf), fac_load_data->id);
764 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
765 fac_load_data->pointer_size);
766 fac->size_t_size = ltt_get_uin32(LTT_GET_BO(tf),
767 fac_load_data->size_t_size);
768 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
769 fac_load_data->alignment);
770
771 if(ltt_get_facility_description(fac, tf->trace))
772 goto facility_error;
773
774 fac->exists = 1;
775
776 fac_ids = g_datalist_get_data(tf->facilities_by_name, fac->name);
777 if(fac_ids == NULL) {
778 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
779 g_datalist_set_data_full(tf->facilities_by_name, fac->name,
780 fac_ids, ltt_fac_ids_destroy);
781 }
782 g_array_append_val(fac_ids, fac->id);
783
784 break;
785 case LTT_EVENT_FACILITY_UNLOAD:
786 /* We don't care about unload : facilities ID are valid for the whole
787 * trace. They simply won't be used after the unload. */
788 break;
789 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
790 struct LttFacilityLoad *fac_load_data =
791 (struct LttFacilityLoad *)tf->event.data;
792 char *fac_name =
793 (char*)(tf->event.data + sizeof(struct LttFacilityLoad));
794 fac = &g_array_index (tf->facilities_by_num, LttTracefile,
795 tf->event.);
796 g_assert(fac->exists == 0);
797 fac->name = g_quark_from_string(fac_name);
798 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
799 fac_load_data->checksum);
800 fac->id = ltt_get_uint8(LTT_GET_BO(tf), fac_load_data->id);
801 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
802 fac_load_data->pointer_size);
803 fac->size_t_size = ltt_get_uin32(LTT_GET_BO(tf),
804 fac_load_data->size_t_size);
805 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
806 fac_load_data->alignment);
807 fac->events;
808 fac->named_types;
809 fac->named_types_number;
810 fac->exists = 1;
811
812 fac_ids = g_datalist_get_data(tf->facilities_by_name, fac->name);
813 if(fac_ids == NULL) {
814 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
815 g_datalist_set_data_full(tf->facilities_by_name, fac->name,
816 fac_ids, ltt_fac_ids_destroy);
817 }
818 g_array_append_val(fac_ids, fac->id);
819
820 break;
821 default:
822 g_warning("Error in processing facility file %s, "
823 "unknown event id %hhu in core facility.",
824 g_quark_to_string(tf->name),
825 tf->event.event_id);
826 err = EPERM;
827 goto event_id_error;
828 }
829 } else if(tf->event.facility_id == 1) {
830
831 switch((enum ltt_heartbeat_events)tf->event.event_id) {
832 case LTT_EVENT_HEARTBEAT:
833 break;
834 default:
835 g_warning("Error in processing facility file %s, "
836 "unknown event id %hhu in heartbeat facility.",
837 g_quark_to_string(tf->name),
838 tf->event.event_id);
839 err = EPERM;
840 goto event_id_error;
841 }
842 }
843 }
844 return 0;
845
846 /* Error handling */
847facility_error:
848event_id_error:
849fac_id_error:
850update_error:
851seek_error:
852 return err;
853}
854
855
54ecbf38 856LttTrace *ltt_trace_open(const gchar *pathname)
857{
858 gchar abs_path[PATH_MAX];
859 LttTrace * t;
2896f39c 860 LttTracefile *tf;
54ecbf38 861 GArray *group;
862 int i;
863
864 LttTrace * t = g_new(LttTrace, 1);
865 if(!t) goto alloc_error;
866
867 get_absolute_pathname(pathname, abs_path);
868 t->pathname = g_quark_from_string(abs_path);
869
870 /* Open all the tracefiles */
871 g_datalist_init(t->tracefiles);
872 if(open_tracefiles(t, abs_path, t->tracefiles))
873 goto open_error;
874
2896f39c 875 /* Prepare the facilities containers : array and mapping */
876 /* Array is zeroed : the "exists" field is set to false by default */
877 t->facilities_by_num = g_array_sized_new (FALSE,
878 TRUE, sizeof(LttFacility),
879 NUM_FACILITIES);
880 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
881
882 g_datalist_init(t->tracefiles_by_name);
883
54ecbf38 884 /* Load trace XML event descriptions */
885 //TODO
886
887 /* Parse each trace control/facilitiesN files : get runtime fac. info */
888 group = g_datalist_get_data(t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
889 if(group == NULL) {
890 g_error("Trace %s has no facility tracefile", abs_path);
891 goto facilities_error;
892 }
893
894 for(i=0; i<group->len; i++) {
895 tf = &g_array_index (group, LttTracefile, i);
2896f39c 896 if(ltt_process_facility_tracefile(tf))
897 goto facilities_error;
54ecbf38 898 }
899
900
901
902 return t;
903
904 /* Error handling */
905facilities_error:
2896f39c 906 g_datalist_free(t->tracefiles_by_name);
907 g_array_free(t->facilities_by_num, TRUE);
54ecbf38 908open_error:
909 g_datalist_clear(t->tracefiles);
910 g_free(t);
911alloc_error:
912 return NULL;
913
54ecbf38 914}
915
2896f39c 916GQuark ltt_trace_name(LttTrace *t)
54ecbf38 917{
918 return t->pathname;
919}
920
921
922/******************************************************************************
923 * When we copy a trace, we want all the opening actions to happen again :
924 * the trace will be reopened and totally independant from the original.
925 * That's why we call ltt_trace_open.
926 *****************************************************************************/
927LttTrace *ltt_trace_copy(LttTrace *self)
928{
929 return ltt_trace_open(self->pathname);
930}
931
2896f39c 932//FIXME TODO
54ecbf38 933void ltt_trace_close(LttTrace *t)
934{
935 unsigned int i;
936 LttTracefile * tf;
937 LttFacility * f;
938
939 g_free(t->pathname);
940
941 //free system_description
942 g_free(t->system_description->description);
943 g_free(t->system_description->node_name);
944 g_free(t->system_description->domain_name);
945 g_free(t->system_description->kernel_name);
946 g_free(t->system_description->kernel_release);
947 g_free(t->system_description->kernel_version);
948 g_free(t->system_description->machine);
949 g_free(t->system_description->processor);
950 g_free(t->system_description->hardware_platform);
951 g_free(t->system_description->operating_system);
952 g_free(t->system_description);
953
954 //free control_tracefiles
955 for(i=0;i<t->control_tracefile_number;i++){
956 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
957 ltt_tracefile_close(tf);
958 }
959 g_ptr_array_free(t->control_tracefiles, TRUE);
960
961 //free per_cpu_tracefiles
962 for(i=0;i<t->per_cpu_tracefile_number;i++){
963 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
964 ltt_tracefile_close(tf);
965 }
966 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
967
968 //free facilities
969 for(i=0;i<t->facility_number;i++){
970 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
971 ltt_facility_close(f);
972 }
973 g_ptr_array_free(t->facilities, TRUE);
974
975 g_free(t);
976
977 g_blow_chunks();
978}
979
980
981/*****************************************************************************
982 *Get the system description of the trace
983 ****************************************************************************/
984
2896f39c 985LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
54ecbf38 986{
2896f39c 987 g_assert(index < t->facilities_by_num->len);
988 return &g_array_index(t->facilities_by_num, LttFacility, id);
54ecbf38 989}
990
2896f39c 991/* ltt_trace_facility_get_by_name
992 *
993 * Returns the GArray of facility indexes. All the fac_ids that matches the
994 * requested facility name.
995 *
996 * If name is not found, returns NULL.
997 */
998GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
54ecbf38 999{
2896f39c 1000 return g_datalist_id_get_data(t->facilities_by_name, name);
54ecbf38 1001}
1002
1003/*****************************************************************************
1004 * Functions to discover all the event types in the trace
1005 ****************************************************************************/
1006
1007unsigned ltt_trace_eventtype_number(LttTrace *t)
1008{
1009 unsigned int i;
1010 unsigned count = 0;
1011 unsigned int num = t->facility_number;
1012 LttFacility * f;
1013
1014 for(i=0;i<num;i++){
1015 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1016 count += f->event_number;
1017 }
1018 return count;
1019}
1020
54ecbf38 1021LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
1022{
1023 LttEventType *event_type;
1024
1025 LttFacility * f;
1026 f = ltt_trace_facility_by_id(t,evId);
1027
1028 if(unlikely(!f)) event_type = NULL;
1029 else event_type = f->events[evId - f->base_id];
1030
1031 return event_type;
1032}
1033
2896f39c 1034#if 0
54ecbf38 1035/*****************************************************************************
2896f39c 1036 * ltt_trace_find_tracefile
1037 *
1038 * Find a tracefile by name and index in the group.
1039 *
1040 * Returns a pointer to the tracefiles, else NULL.
54ecbf38 1041 ****************************************************************************/
1042
2896f39c 1043LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
54ecbf38 1044{
54ecbf38 1045}
2896f39c 1046#endif //0
54ecbf38 1047
1048/*****************************************************************************
1049 * Get the start time and end time of the trace
1050 ****************************************************************************/
1051
1052static void ltt_tracefile_time_span_get(LttTracefile *tf,
1053 LttTime *start, LttTime *end)
1054{
1055 struct ltt_block_start_header * header;
1056 int err;
1057
1058 err = map_block(tf, 0);
1059 if(unlikely(err)) {
1060 g_error("Can not map block");
1061 *start = { 0xFFFFFFFF, 0xFFFFFFFF };
1062 } else
1063 *start = tf->buffer.begin.timestamp;
1064
1065 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1066 if(unlikely(err)) {
1067 g_error("Can not map block");
1068 *end = { 0, 0 };
1069 } else
1070 *end = tf->buffer.end.timestamp;
1071}
1072
1073struct tracefile_time_span_get_args {
1074 LttTrace *t;
1075 LttTime *start;
1076 LttTime *end;
1077};
1078
1079static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
1080{
1081 struct tracefile_time_span_get_args *args =
1082 (struct tracefile_time_span_get_args*)user_data;
1083
1084 GArray *group = (GArray *)data;
1085 int i;
1086 LttTracefile *tf;
1087 LttTime tmp_start;
1088 LttTime tmp_end;
1089
1090 for(i=0; i<group->len; i++) {
1091 tf = &g_array_index (group, LttTracefile, i);
1092 if(tf->cpu_online) {
1093 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1094 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1095 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1096 }
1097 }
1098}
1099
1100void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1101{
1102 LttTime min_start = { 0xFFFFFFFF, 0xFFFFFFFF };
1103 LttTime max_end = { 0, 0 };
1104 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
1105
1106 g_datalist_foreach(t->tracefiles, &group_time_span_get, &args);
1107
1108 if(start != NULL) *start = min_start;
1109 if(end != NULL) *end = max_end;
1110
1111}
1112
1113
1114/*****************************************************************************
1115 *Get the name of a tracefile
1116 ****************************************************************************/
1117
2896f39c 1118GQuark ltt_tracefile_name(LttTracefile *tf)
54ecbf38 1119{
1120 return tf->name;
1121}
1122
1123/*****************************************************************************
1124 * Get the number of blocks in the tracefile
1125 ****************************************************************************/
1126
2896f39c 1127guint ltt_tracefile_block_number(LttTracefile *tf)
54ecbf38 1128{
1129 return tf->block_number;
1130}
1131
1132
1133/* Seek to the first event in a tracefile that has a time equal or greater than
1134 * the time passed in parameter.
1135 *
1136 * If the time parameter is outside the tracefile time span, seek to the first
1137 * or the last event of the tracefile.
1138 *
1139 * If the time parameter is before the first event, we have to seek specially to
1140 * there.
1141 *
1142 * If the time is after the end of the trace, get the last event.
1143 *
1144 * Do a binary search to find the right block, then a sequential search in the
1145 * block to find the event.
1146 *
1147 * In the special case where the time requested fits inside a block that has no
1148 * event corresponding to the requested time, the first event of the next block
1149 * will be seeked.
1150 *
1151 * IMPORTANT NOTE : // FIXME everywhere...
1152 *
1153 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1154 * you will jump over an event if you do.
1155 *
2896f39c 1156 * Return value : 0 : no error, the tf->event can be used
1157 * otherwise : this is an error.
1158 *
54ecbf38 1159 * */
1160
2896f39c 1161int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
54ecbf38 1162{
2896f39c 1163 int ret = 0;
54ecbf38 1164 int err;
1165 unsigned int block_num, high, low;
1166
1167 /* seek at the beginning of trace */
1168 err = map_block(tf, 0); /* First block */
1169 if(unlikely(err)) {
1170 g_error("Can not map block");
1171 goto fail;
1172 }
1173
1174 /* If the time is lower or equal the beginning of the trace,
1175 * go to the first event. */
1176 if(ltt_time_compare(time, tf->buffer.start.timestamp) <= 0) {
2896f39c 1177 ret = ltt_tracefile_read(tf)
54ecbf38 1178 goto found; /* There is either no event in the trace or the event points
1179 to the first event in the trace */
1180 }
1181
1182 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1183 if(unlikely(err)) {
1184 g_error("Can not map block");
1185 goto fail;
1186 }
1187
1188 /* If the time is after the end of the trace, get the last event. */
1189 if(ltt_time_compare(time, tf->buffer.end.timestamp) >= 0) {
2896f39c 1190 /* While the ltt_tracefile_read doesn't return ERANGE or EPERM,
1191 * continue reading.
54ecbf38 1192 */
2896f39c 1193 while(1) {
1194 ret = ltt_tracefile_read(tf);
1195 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1196 else if(ret) goto fail;
1197 }
54ecbf38 1198 }
1199
1200 /* Binary search the block */
1201 high = tf->num_blocks - 1;
1202 low = 0;
1203
1204 while(1) {
1205 block_num = ((high-low) / 2) + low;
1206
1207 err = map_block(tf, block_num);
1208 if(unlikely(err)) {
1209 g_error("Can not map block");
1210 goto fail;
1211 }
bed09760 1212 if(high == low) {
1213 /* We cannot divide anymore : this is what would happen if the time
1214 * requested was exactly between two consecutive buffers'end and start
1215 * timestamps. This is also what would happend if we didn't deal with out
1216 * of span cases prior in this function. */
1217 /* The event is right in the buffer!
1218 * (or in the next buffer first event) */
1219 while(1) {
2896f39c 1220 ret = ltt_tracefile_read(tf);
1221 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1222 else if(ret) goto fail;
1223
bed09760 1224 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1225 break;
1226 }
1227
1228 } if(ltt_time_compare(time, tf->buffer.start.timestamp) < 0) {
54ecbf38 1229 /* go to lower part */
1230 high = block_num;
1231 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1232 /* go to higher part */
1233 low = block_num;
1234 } else {/* The event is right in the buffer!
1235 (or in the next buffer first event) */
1236 while(1) {
1237 ltt_tracefile_read(tf);
2896f39c 1238 if(ret == ERANGE) goto found; /* ERANGE or EPERM */
1239 else if(ret) goto fail;
1240
54ecbf38 1241 if(ltt_time_compare(time, tf->event.event_time) >= 0)
1242 break;
1243 }
1244 goto found;
1245 }
1246 }
1247
1248found:
2896f39c 1249 return 0;
54ecbf38 1250
1251 /* Error handling */
1252fail:
1253 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1254 g_quark_to_string(tf->name));
2896f39c 1255 return EPERM;
54ecbf38 1256}
1257
1258
1259int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
1260
1261 int err;
1262
1263 if(ep->tracefile != tf) {
1264 goto fail;
1265 }
1266
1267 err = map_block(tf, ep->block);
1268 if(unlikely(err)) {
1269 g_error("Can not map block");
1270 goto fail;
1271 }
1272
1273 tf->event.offset = ep->offset;
1274
2896f39c 1275 err = ltt_tracefile_read_update_event(tf);
1276 if(err) goto fail;
1277 err = ltt_tracefile_read_op(tf);
1278 if(err) goto fail;
1279
54ecbf38 1280 return;
1281
1282fail:
1283 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1284 g_quark_to_string(tf->name));
1285}
1286
54ecbf38 1287/* Calculate the real event time based on the buffer boundaries */
1288LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1289{
1290 LttTime time;
1291
1292 g_assert(t->trace->has_tsc);
1293
1294 time = ltt_time_from_uint64(
1295 (guint64)tf->buffer.tsc*tf->buffer.nsecs_per_cycle);
1296 time = ltt_time_add(tf->buffer.begin.timestamp, time);
1297
1298 return time;
1299}
1300
54ecbf38 1301/*****************************************************************************
1302 *Function name
1303 * ltt_tracefile_read : Read the next event in the tracefile
1304 *Input params
1305 * t : tracefile
1306 *Return value
54ecbf38 1307 *
2896f39c 1308 * Returns 0 if an event can be used in tf->event.
1309 * Returns ERANGE on end of trace. The event in tf->event still can be used.
1310 * Returns EPERM on error.
54ecbf38 1311 *
1312 * This function does make the tracefile event structure point to the event
2896f39c 1313 * currently pointed to by the tf->event.
1314 *
1315 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1316 * reinitialize it after an error if you want results to be coherent.
1317 * It would be the case if a end of trace last buffer has no event : the end
1318 * of trace wouldn't be returned, but an error.
1319 * We make the assumption there is at least one event per buffer.
54ecbf38 1320 ****************************************************************************/
1321
2896f39c 1322int ltt_tracefile_read(LttTracefile *tf)
1323{
1324 int err;
1325
1326 err = ltt_tracefile_read_seek(tf);
1327 if(err) return err;
1328 err = ltt_tracefile_read_update_event(tf);
1329 if(err) return err;
1330 err = ltt_tracefile_read_op(tf);
1331 if(err) return err;
1332
1333 return 0;
1334}
1335
1336int ltt_tracefile_read_seek(LttTracefile *tf)
1337{
1338 int err;
1339
1340 /* Get next buffer until we finally have an event, or end of trace */
1341 while(1) {
1342 err = ltt_seek_next_event(tf);
1343 if(unlikely(err == ENOPROTOOPT)) {
1344 return EPERM;
1345 }
1346
1347 /* Are we at the end of the buffer ? */
1348 if(err == ERANGE) {
1349 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1350 return ERANGE;
1351 } else {
1352 /* get next block */
1353 err = map_block(tf, tf->buffer.index + 1);
1354 if(unlikely(err)) {
1355 g_error("Can not map block");
1356 return EPERM;
1357 }
1358 }
1359 } else break; /* We found an event ! */
1360 }
1361
1362 return 0;
1363}
1364
1365
1366/* do specific operation on events */
1367int ltt_tracefile_read_op(LttTracefile *tf)
54ecbf38 1368{
1369 int err;
1370 LttFacility *f;
1371 void * pos;
2896f39c 1372 LttEvent *event;
54ecbf38 1373
2896f39c 1374 event = &tf->event;
54ecbf38 1375
2896f39c 1376 /* do event specific operation */
1377
1378 /* do something if its an heartbeat event : increment the heartbeat count */
1379 if(event->facility_id != 0) { /* except core */
1380 f = (LttFacility*)g_ptr_array_index(tf->trace->facilities,
1381 event->facility_id);
1382 g_assert(f != NULL);
ea45889a 1383
2896f39c 1384 if(unlikely(ltt_facility_name(f)
1385 != LTT_FACILITY_NAME_HEARTBEAT)) {
1386 LttEventType *et = ltt_facility_eventtype_get_by_name(f,
1387 LTT_EVENT_NAME_HEARTBEAT);
1388 if(et->id == event->event_id)
1389 t->cur_heart_beat_number++;
54ecbf38 1390 }
1391 }
2896f39c 1392
1393 return 0;
1394}
1395
1396
1397/* same as ltt_tracefile_read, but does not seek to the next event nor call
1398 * event specific operation. */
1399int ltt_tracefile_read_update_event(LttTracefile *tf)
1400{
1401 int err;
1402 LttFacility *f;
1403 void * pos;
1404 LttEvent *event;
1405
1406 event = &tf->event;
1407 pos = event->offset;
54ecbf38 1408
1409 /* Read event header */
1410
1411 //TODO align
1412
1413 if(tf->trace->has_tsc) {
1414 event->time.timestamp = ltt_get_uint32(LTT_GET_BO(t),
2896f39c 1415 pos);
54ecbf38 1416 /* 32 bits -> 64 bits tsc */
2896f39c 1417 /* note : still works for seek and non seek cases. */
54ecbf38 1418 if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1419 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1420 + 0x100000000ULL)
1421 | (guint64)event->time.timestamp;
54ecbf38 1422 } else {
1423 /* no overflow */
1424 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1425 | (guint64)event->time.timestamp;
1426 }
2896f39c 1427
1428 event->event_time = ltt_interpolate_time(tf, event);
1429
54ecbf38 1430 pos += sizeof(uint32);
1431 } else {
1432 event->time.delta = ltt_get_uint32(LTT_GET_BO(tf),
2896f39c 1433 pos);
54ecbf38 1434 tf->buffer.tsc = 0;
1435
1436 event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
1437 event->time_delta);
1438 pos += sizeof(uint32);
1439 }
1440
1441 event->facility_id = ltt_get_uint8(LTT_GET_BO(tf),
1442 tf->cur_event_pos);
1443 pos += sizeof(uint8);
1444
1445 event->event_id = ltt_get_uint8(LTT_GET_BO(tf),
1446 tf->cur_event_pos);
1447 pos += sizeof(uint8);
1448
1449 event->data = tf->cur_event_pos + EVENT_HEADER_SIZE;
1450
1451 event->data = pos;
1452
2896f39c 1453 return 0;
54ecbf38 1454}
1455
54ecbf38 1456
1457/****************************************************************************
1458 *Function name
1459 * map_block : map a block from the file
1460 *Input Params
1461 * lttdes : ltt trace file
1462 * whichBlock : the block which will be read
1463 *return value
1464 * 0 : success
1465 * EINVAL : lseek fail
1466 * EIO : can not read from the file
1467 ****************************************************************************/
1468
1469static int map_block(LttTracefile * tf, int block_num)
1470{
1471 struct ltt_block_start_header *header;
1472
1473 g_assert(block_num < tf->num_blocks);
1474
1475 if(tf->buffer.head != NULL)
1476 munmap(tf->buffer.head, tf->buf_size);
1477
1478 /* Multiple of pages aligned head */
1479 tf->buffer.head = mmap(0, tf->block_size, PROT_READ, tf->fd,
1480 (off_t)tf->block_size * (off_t)block_num);
1481
1482 if(tf->buffer.head == NULL) {
1483 perror("Error in allocating memory for buffer of tracefile %s\n", fileName);
1484 goto map_error;
1485 }
1486 g_assert(tf->buffer.head & (8-1) == 0); // make sure it's aligned.
1487
1488
1489 tf->buffer.index = block_num;
1490
1491 header = (struct ltt_block_start_header*)tf->buffer.head;
1492
1493 tf->buffer.begin.timestamp = ltt_get_uint64(LTT_GET_BO(tf),
1494 header->begin.timestamp)
1495 * NSEC_PER_USEC;
1496 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1497 header->begin.cycle_count);
1498 tf->buffer.end.timestamp = ltt_get_uint64(LTT_GET_BO(tf),
1499 header->end.timestamp)
1500 * NSEC_PER_USEC;
1501 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1502 header->end.cycle_count);
1503 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
1504 header->lost_size);
1505
1506 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1507
1508 /* FIXME
1509 * eventually support variable buffer size : will need a partial pre-read of
1510 * the headers to create an index when we open the trace... eventually. */
1511 g_assert(tf->block_size == ltt_get_uint32(header->buf_size));
1512
1513 /* Now that the buffer is mapped, calculate the time interpolation for the
1514 * block. */
1515
1516 tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(&tf->buffer);
1517
1518 /* Make the current event point to the beginning of the buffer :
1519 * it means that the event read must get the first event. */
1520 tf->event.tracefile = tf;
1521 tf->event.block = block_num;
1522 tf->event.offset = tf->buffer.head;
1523
1524 return 0;
1525
1526map_error:
1527 return -errno;
1528
1529}
1530
54ecbf38 1531/* Take the tf current event offset and use the event facility id and event id
1532 * to figure out where is the next event offset.
1533 *
1534 * This is an internal function not aiming at being used elsewhere : it will
1535 * not jump over the current block limits. Please consider using
1536 * ltt_tracefile_read to do this.
1537 *
1538 * Returns 0 on success
1539 * ERANGE if we are at the end of the buffer.
2896f39c 1540 * ENOPROTOOPT if an error occured when getting the current event size.
54ecbf38 1541 */
1542static int ltt_seek_next_event(LttTracefile *tf)
1543{
2896f39c 1544 int ret = 0;
54ecbf38 1545 void *pos;
1546
1547 /* seek over the buffer header if we are at the buffer start */
1548 if(tf->event.offset == tf->buffer.head) {
1549 tf->event.offset += sizeof(struct ltt_block_start_header);
1550 goto found;
1551 }
1552
2896f39c 1553
1554 if(tf->event.offset == tf->buffer.head + tf->buffer.lost_size) {
1555 ret = ERANGE;
1556 goto found;
1557 }
54ecbf38 1558
1559 pos = tf->event.data;
1560
2896f39c 1561 /* FIXME : do this function. Remember to hardcode the sizes of heartbeat and
1562 * core */
54ecbf38 1563 pos += ltt_facility_get_event_size(tf->event.facility_id, tf->event.event_id);
2896f39c 1564 on error : goto error;
54ecbf38 1565
1566 tf->event.offset = pos;
1567
1568found:
2896f39c 1569 return ret;
54ecbf38 1570
1571error:
1572 g_error("Error in ltt_seek_next_event for tracefile %s",
1573 g_quark_to_string(tf->name));
2896f39c 1574 return ENOPROTOOPT;
54ecbf38 1575}
1576
1577
1578/*****************************************************************************
1579 *Function name
1580 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1581 *Input Params
1582 * t : tracefile
1583 ****************************************************************************/
1584
1585static double calc_nsecs_per_cycle(LttTracefile * t)
1586{
1587 LttTime lBufTotalTime; /* Total time for this buffer */
1588 double lBufTotalNSec; /* Total time for this buffer in nsecs */
1589 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
1590
1591 /* Calculate the total time for this buffer */
1592 lBufTotalTime = ltt_time_sub(
1593 ltt_get_time(t->buffer.end.timestamp),
1594 ltt_get_time(t->buffer.begin.timestamp));
1595
1596 /* Calculate the total cycles for this bufffer */
1597 lBufTotalCycle = t->buffer.end.cycle_count;
1598 lBufTotalCycle -= t->buffer.start.cycle_count;
1599
1600 /* Convert the total time to double */
1601 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
1602
1603 return lBufTotalNSec / (double)lBufTotalCycle;
1604
1605}
1606
1607/*****************************************************************************
1608 *Function name
1609 * setFieldsOffset : set offset of the fields
1610 *Input params
1611 * tracefile : opened trace file
1612 * evT : the event type
1613 * evD : event data, it may be NULL
1614 ****************************************************************************/
1615
1616void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1617{
1618 LttField * rootFld = evT->root_field;
1619 // rootFld->base_address = evD;
1620
1621 if(likely(rootFld))
1622 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1623}
1624
1625/*****************************************************************************
1626 *Function name
1627 * getFieldtypeSize: get the size of the field type (primitive type)
1628 *Input params
1629 * tracefile : opened trace file
1630 * evT : event type
1631 * offsetRoot : offset from the root
1632 * offsetParent : offset from the parrent
1633 * fld : field
1634 * evD : event data, it may be NULL
1635 *Return value
1636 * int : size of the field
1637 ****************************************************************************/
1638
1639static inline gint getFieldtypeSize(LttTracefile * t,
1640 LttEventType * evT, gint offsetRoot,
1641 gint offsetParent, LttField * fld, void *evD, LttTrace *trace)
1642{
1643 gint size, size1, element_number, i, offset1, offset2;
1644 LttType * type = fld->field_type;
1645
1646 if(unlikely(t && evT->latest_block==t->which_block &&
1647 evT->latest_event==t->which_event)){
1648 size = fld->field_size;
1649 goto end_getFieldtypeSize;
1650 } else {
1651 /* This likely has been tested with gcov : half of them.. */
1652 if(unlikely(fld->field_fixed == 1)){
1653 /* tested : none */
1654 if(unlikely(fld == evT->root_field)) {
1655 size = fld->field_size;
1656 goto end_getFieldtypeSize;
1657 }
1658 }
1659
1660 /* From gcov profiling : half string, half struct, can we gain something
1661 * from that ? (Mathieu) */
1662 switch(type->type_class) {
1663 case LTT_ARRAY:
1664 element_number = (int) type->element_number;
1665 if(fld->field_fixed == -1){
1666 size = getFieldtypeSize(t, evT, offsetRoot,
1667 0,fld->child[0], NULL, trace);
1668 if(size == 0){ //has string or sequence
1669 fld->field_fixed = 0;
1670 }else{
1671 fld->field_fixed = 1;
1672 size *= element_number;
1673 }
1674 }else if(fld->field_fixed == 0){// has string or sequence
1675 size = 0;
1676 for(i=0;i<element_number;i++){
1677 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1678 fld->child[0], evD+size, trace);
1679 }
1680 }else size = fld->field_size;
1681 if(unlikely(!evD)){
1682 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1683 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1684 }
1685
1686 break;
1687
1688 case LTT_SEQUENCE:
1689 size1 = (int) ltt_type_size(trace, type);
1690 if(fld->field_fixed == -1){
1691 fld->sequ_number_size = size1;
1692 fld->field_fixed = 0;
1693 size = getFieldtypeSize(t, evT, offsetRoot,
1694 0,fld->child[0], NULL, trace);
1695 fld->element_size = size;
1696 }else{//0: sequence
1697 element_number = getIntNumber(t->trace->reverse_byte_order,size1,evD);
1698 type->element_number = element_number;
1699 if(fld->element_size > 0){
1700 size = element_number * fld->element_size;
1701 }else{//sequence has string or sequence
1702 size = 0;
1703 for(i=0;i<element_number;i++){
1704 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1705 fld->child[0], evD+size+size1, trace);
1706 }
1707 }
1708 size += size1;
1709 }
1710 if(unlikely(!evD)){
1711 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1712 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1713 }
1714
1715 break;
1716
1717 case LTT_STRING:
1718 size = 0;
1719 if(fld->field_fixed == -1){
1720 fld->field_fixed = 0;
1721 }else{//0: string
1722 /* Hope my implementation is faster than strlen (Mathieu) */
1723 char *ptr=(char*)evD;
1724 size = 1;
1725 /* from gcov : many many strings are empty, make it the common case.*/
1726 while(unlikely(*ptr != '\0')) { size++; ptr++; }
1727 //size = ptr - (char*)evD + 1; //include end : '\0'
1728 }
1729 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1730 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1731
1732 break;
1733
1734 case LTT_STRUCT:
1735 element_number = (int) type->element_number;
1736 size = 0;
1737 /* tested with gcov */
1738 if(unlikely(fld->field_fixed == -1)){
1739 offset1 = offsetRoot;
1740 offset2 = 0;
1741 for(i=0;i<element_number;i++){
1742 size1=getFieldtypeSize(t, evT,offset1,offset2,
1743 fld->child[i], NULL, trace);
1744 if(likely(size1 > 0 && size >= 0)){
1745 size += size1;
1746 if(likely(offset1 >= 0)) offset1 += size1;
1747 offset2 += size1;
1748 }else{
1749 size = -1;
1750 offset1 = -1;
1751 offset2 = -1;
1752 }
1753 }
1754 if(unlikely(size == -1)){
1755 fld->field_fixed = 0;
1756 size = 0;
1757 }else fld->field_fixed = 1;
1758 }else if(likely(fld->field_fixed == 0)){
1759 offset1 = offsetRoot;
1760 offset2 = 0;
1761 for(i=0;unlikely(i<element_number);i++){
1762 size=getFieldtypeSize(t,evT,offset1,offset2,
1763 fld->child[i],evD+offset2, trace);
1764 offset1 += size;
1765 offset2 += size;
1766 }
1767 size = offset2;
1768 }else size = fld->field_size;
1769 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1770 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1771 break;
1772
1773 default:
1774 if(unlikely(fld->field_fixed == -1)){
1775 size = (int) ltt_type_size(trace, type);
1776 fld->field_fixed = 1;
1777 }else size = fld->field_size;
1778 if(unlikely(!evD)){
1779 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1780 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1781 }
1782 break;
1783 }
1784 }
1785
1786 fld->offset_root = offsetRoot;
1787 fld->offset_parent = offsetParent;
1788 fld->field_size = size;
1789
1790end_getFieldtypeSize:
1791
1792 return size;
1793}
1794
1795
1796/*****************************************************************************
1797 *Function name
1798 * getIntNumber : get an integer number
1799 *Input params
1800 * size : the size of the integer
1801 * evD : the event data
1802 *Return value
1803 * gint64 : a 64 bits integer
1804 ****************************************************************************/
1805
1806gint64 getIntNumber(gboolean reverse_byte_order, int size, void *evD)
1807{
1808 gint64 i;
1809
1810 switch(size) {
1811 case 1: i = *((gint8*)evD); break;
1812 case 2: i = ltt_get_int16(reverse_byte_order, evD); break;
1813 case 4: i = ltt_get_int32(reverse_byte_order, evD); break;
1814 case 8: i = ltt_get_int64(reverse_byte_order, evD); break;
1815 default: i = ltt_get_int64(reverse_byte_order, evD);
1816 g_critical("getIntNumber : integer size %d unknown", size);
1817 break;
1818 }
1819
1820 return i;
1821}
54ecbf38 1822
54ecbf38 1823/* get the node name of the system */
1824
1825char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1826{
1827 return s->node_name;
1828}
1829
1830
1831/* get the domain name of the system */
1832
1833char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1834{
1835 return s->domain_name;
1836}
1837
1838
1839/* get the description of the system */
1840
1841char * ltt_trace_system_description_description (LttSystemDescription * s)
1842{
1843 return s->description;
1844}
1845
1846
1847/* get the start time of the trace */
1848
1849LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
1850{
1851 return s->trace_start;
1852}
1853
1854
1855LttTracefile *ltt_tracefile_new()
1856{
1857 return g_new(LttTracefile, 1);
1858}
1859
1860void ltt_tracefile_destroy(LttTracefile *tf)
1861{
1862 g_free(tf);
1863}
1864
1865void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1866{
1867 *dest = *src;
1868}
1869
1870/* Before library loading... */
1871
1872static void __attribute__((constructor)) init(void)
1873{
1874 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
1875 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
1876
1877 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("control/facilities");
1878}
1879
This page took 0.091725 seconds and 4 git commands to generate.