1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <lttv/traceset.h>
24 #include <lttv/iattribute.h>
25 #include <lttv/state.h>
26 #include <lttv/event.h>
27 #include <lttv/hook.h>
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/context.h>
31 #include <babeltrace/ctf/iterator.h>
32 #include <babeltrace/ctf/events.h>
34 /* To traverse a tree recursively */
36 /* For the use of realpath*/
43 /* A trace is a sequence of events gathered in the same tracing session. The
44 events may be stored in several tracefiles in the same directory.
45 A trace set is defined when several traces are to be analyzed together,
46 possibly to study the interactions between events in the different traces.
50 LttvTraceset
*lttv_traceset_new(void)
53 struct bt_iter_pos begin_pos
;
55 ts
= g_new(LttvTraceset
, 1);
57 ts
->traces
= g_ptr_array_new();
58 ts
->context
= bt_context_create();
59 ts
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
61 /*Initialize iterator to the beginning of the traces*/
62 begin_pos
.type
= BT_SEEK_BEGIN
;
63 ts
->iter
= bt_ctf_iter_create(ts
->context
, &begin_pos
, NULL
);
65 ts
->event_hooks
= lttv_hooks_new();
67 ts
->state_trace_handle_index
= g_ptr_array_new();
68 ts
->has_precomputed_states
= FALSE
;
70 ts
->time_span
.start_time
= ltt_time_zero
;
71 ts
->time_span
.end_time
= ltt_time_zero
;
72 lttv_traceset_get_time_span_real(ts
);
76 char * lttv_traceset_name(LttvTraceset
* s
)
82 LttvTrace
*lttv_trace_new(LttTrace
*t
)
86 new_trace
= g_new(LttvTrace
, 1);
87 new_trace
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
89 new_trace
->ref_count
= 0;
94 * get_absolute_pathname : Return the unique pathname in the system
96 * pathname is the relative path.
98 * abs_pathname is being set to the absolute path.
101 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
103 abs_pathname
[0] = '\0';
105 if (realpath(pathname
, abs_pathname
) != NULL
)
109 /* error, return the original path unmodified */
110 strcpy(abs_pathname
, pathname
);
119 * lttv_trace_create : Create a trace from a path
121 * ts is the traceset in which will be contained the trace
123 * path is the path where to find a trace. It is not recursive.
125 * This function is static since a trace should always be contained in a
128 * return the created trace or NULL on failure
130 static LttvTrace
*lttv_trace_create(LttvTraceset
*ts
, const char *path
)
132 int id
= bt_context_add_trace(lttv_traceset_get_context(ts
),
141 // Create the trace and save the trace handle id returned by babeltrace
142 LttvTrace
*new_trace
;
144 new_trace
= g_new(LttvTrace
, 1);
145 new_trace
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
147 new_trace
->ref_count
= 0;
148 new_trace
->traceset
= ts
;
149 new_trace
->state
= g_new(LttvTraceState
,1);
150 lttv_trace_state_init(new_trace
->state
,new_trace
);
152 /* Add the state to the trace_handle to state index */
153 g_ptr_array_set_size(ts
->state_trace_handle_index
,id
+1);
154 g_ptr_array_index(ts
->state_trace_handle_index
,id
) = new_trace
->state
;
160 * lttv_trace_create : Create and add a single trace to a traceset
162 * ts is the traceset in which will be contained the trace
164 * path is the path where to find a trace. It is not recursive.
166 * return a positive integer (>=0)on success or -1 on failure
168 static int lttv_traceset_create_trace(LttvTraceset
*ts
, const char *path
)
170 LttvTrace
*trace
= lttv_trace_create(ts
, path
);
174 lttv_traceset_add(ts
, trace
);
178 LttvTraceset
*lttv_traceset_copy(LttvTraceset
*s_orig
)
184 s
= g_new(LttvTraceset
, 1);
186 s
->traces
= g_ptr_array_new();
187 s
->state_trace_handle_index
= g_ptr_array_new();
188 for(i
=0;i
<s_orig
->traces
->len
;i
++)
190 trace
= g_ptr_array_index(s_orig
->traces
, i
);
193 /* WARNING: this is an alias, not a copy. */
194 g_ptr_array_add(s
->traces
, trace
);
196 g_ptr_array_set_size(s
->state_trace_handle_index
,trace
->id
+1);
197 g_ptr_array_index(s
->state_trace_handle_index
,trace
->id
) = trace
->state
;
200 s
->context
= s_orig
->context
;
201 bt_context_get(s
->context
);
202 s
->a
= LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig
->a
)));
207 LttvTraceset
*lttv_traceset_load(const gchar
*filename
)
209 LttvTraceset
*s
= g_new(LttvTraceset
,1);
212 s
->filename
= g_strdup(filename
);
213 tf
= fopen(filename
,"r");
215 g_critical("NOT IMPLEMENTED : load traceset data from a XML file");
221 gint
lttv_traceset_save(LttvTraceset
*s
)
225 tf
= fopen(s
->filename
, "w");
227 g_critical("NOT IMPLEMENTED : save traceset data in a XML file");
233 void lttv_traceset_destroy(LttvTraceset
*s
)
237 for(i
=0;i
<s
->traces
->len
;i
++) {
238 LttvTrace
*trace
= g_ptr_array_index(s
->traces
, i
);
239 lttv_trace_unref(trace
);
240 // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed
241 //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id);
242 if(lttv_trace_get_ref_number(trace
) == 0)
243 lttv_trace_destroy(trace
);
245 g_ptr_array_free(s
->traces
, TRUE
);
246 bt_context_put(s
->context
);
247 g_object_unref(s
->a
);
251 struct bt_context
*lttv_traceset_get_context(LttvTraceset
*s
)
256 LttvTraceset
*lttv_trace_get_traceset(LttvTrace
*trace
)
258 return trace
->traceset
;
261 LttvHooks
*lttv_traceset_get_hooks(LttvTraceset
*s
)
263 return s
->event_hooks
;
266 void lttv_trace_destroy(LttvTrace
*t
)
268 g_object_unref(t
->a
);
272 void lttv_traceset_add(LttvTraceset
*s
, LttvTrace
*t
)
275 g_ptr_array_add(s
->traces
, t
);
278 int lttv_traceset_add_path(LttvTraceset
*ts
, char *trace_path
)
282 gboolean metaFileFound
= FALSE
;
284 /* Open top level directory */
285 curdir
= opendir(trace_path
);
286 if (curdir
== NULL
) {
287 g_warning("Cannot open directory %s (%s)", trace_path
, strerror(errno
));
291 // Check if a metadata file exists in the current directory
292 int metafd
= openat(dirfd(curdir
), "metadata", O_RDONLY
);
298 g_warning("Unable to close metadata "
299 "file descriptor : %s.", trace_path
);
303 ret
= lttv_traceset_create_trace(ts
, trace_path
);
305 g_warning("Opening trace \"%s\" "
306 "for reading.", trace_path
);
309 metaFileFound
= TRUE
;
312 struct dirent curentry
;
313 struct dirent
*resultentry
;
314 while ((ret
= readdir_r(curdir
, &curentry
, &resultentry
)) == 0) {
315 if (resultentry
== NULL
) {
319 if (curentry
.d_name
[0] != '.') {
320 if (curentry
.d_type
== DT_DIR
) {
322 char curpath
[PATH_MAX
];
323 snprintf(curpath
, PATH_MAX
, "%s/%s", trace_path
, curentry
.d_name
);
324 ret
= lttv_traceset_add_path(ts
, curpath
);
326 metaFileFound
= TRUE
;
333 g_warning("Invalid readdir");
343 unsigned lttv_traceset_number(LttvTraceset
*s
)
345 return s
->traces
->len
;
349 LttvTrace
*lttv_traceset_get(LttvTraceset
*s
, unsigned i
)
351 g_assert(s
->traces
->len
> i
);
352 return ((LttvTrace
*)s
->traces
->pdata
[i
]);
356 void lttv_traceset_remove(LttvTraceset
*s
, unsigned i
)
359 g_assert(s
->traces
->len
> i
);
360 t
= (LttvTrace
*)s
->traces
->pdata
[i
];
362 bt_context_remove_trace(lttv_traceset_get_context(s
), t
->id
);
363 g_ptr_array_remove_index(s
->traces
, i
);
367 /* A set of attributes is attached to each trace set, trace and tracefile
368 to store user defined data as needed. */
370 LttvAttribute
*lttv_traceset_attribute(LttvTraceset
*s
)
376 LttvAttribute
*lttv_trace_attribute(LttvTrace
*t
)
382 gint
lttv_trace_get_id(LttvTrace
*t
)
387 guint
lttv_trace_get_ref_number(LttvTrace
* t
)
389 // todo mdenis: adapt to babeltrace
393 guint
lttv_trace_ref(LttvTrace
* t
)
400 guint
lttv_trace_unref(LttvTrace
* t
)
402 if(likely(t
->ref_count
> 0))
408 guint
lttv_trace_get_num_cpu(LttvTrace
*t
)
410 #warning "TODO - Set the right number of CPU"
414 LttvTracesetPosition
*lttv_traceset_create_current_position(LttvTraceset
*traceset
)
416 LttvTracesetPosition
*traceset_pos
;
418 traceset_pos
= g_new(LttvTracesetPosition
, 1);
420 /* Check if the new passed */
421 if(traceset_pos
== NULL
) {
425 traceset_pos
->iter
= traceset
->iter
;
426 traceset_pos
->bt_pos
= bt_iter_get_pos(bt_ctf_get_iter(traceset
->iter
));
427 traceset_pos
->timestamp
= G_MAXUINT64
;
428 traceset_pos
->cpu_id
= INT_MAX
;
433 LttvTracesetPosition
*lttv_traceset_create_time_position(LttvTraceset
*traceset
,
436 LttvTracesetPosition
*traceset_pos
;
438 traceset_pos
= g_new(LttvTracesetPosition
, 1);
440 /* Check if the new passed */
441 if(traceset_pos
== NULL
) {
445 traceset_pos
->iter
= traceset
->iter
;
446 traceset_pos
->bt_pos
= bt_iter_create_time_pos(
447 bt_ctf_get_iter(traceset_pos
->iter
),
448 ltt_time_to_uint64(timestamp
));
449 traceset_pos
->timestamp
= G_MAXUINT64
;
450 traceset_pos
->cpu_id
= INT_MAX
;
454 void lttv_traceset_destroy_position(LttvTracesetPosition
*traceset_pos
)
456 bt_iter_free_pos(traceset_pos
->bt_pos
);
457 g_free(traceset_pos
);
460 void lttv_traceset_seek_to_position(const LttvTracesetPosition
*traceset_pos
)
462 bt_iter_set_pos(bt_ctf_get_iter(traceset_pos
->iter
), traceset_pos
->bt_pos
);
465 guint
lttv_traceset_get_cpuid_from_event(LttvEvent
*event
)
467 unsigned long timestamp
;
470 struct bt_ctf_event
*ctf_event
= event
->bt_event
;
471 timestamp
= bt_ctf_get_timestamp(ctf_event
);
472 if (timestamp
== -1ULL) {
475 const struct bt_definition
*scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_PACKET_CONTEXT
);
476 if (bt_ctf_field_get_error()) {
479 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(ctf_event
, scope
, "cpu_id"));
480 if (bt_ctf_field_get_error()) {
487 guint64
lttv_traceset_get_timestamp_first_event(LttvTraceset
*ts
)
489 LttvTracesetPosition begin_position
;
490 struct bt_iter_pos pos
;
491 begin_position
.bt_pos
= &pos
;
492 begin_position
.timestamp
= G_MAXUINT64
;
493 begin_position
.cpu_id
= INT_MAX
;
495 /* Assign iterator to the beginning of the traces */
496 begin_position
.bt_pos
->type
= BT_SEEK_BEGIN
;
497 begin_position
.iter
= ts
->iter
;
499 return lttv_traceset_position_get_timestamp(&begin_position
);
502 guint64
lttv_traceset_get_timestamp_last_event(LttvTraceset
*ts
)
504 LttvTracesetPosition last_position
;
505 struct bt_iter_pos pos
;
506 last_position
.bt_pos
= &pos
;
507 last_position
.timestamp
= G_MAXUINT64
;
508 last_position
.cpu_id
= INT_MAX
;
510 /* Assign iterator to the last event of the traces */
511 last_position
.bt_pos
->type
= BT_SEEK_LAST
;
512 last_position
.iter
= ts
->iter
;
514 return lttv_traceset_position_get_timestamp(&last_position
);
518 * lttv_traceset_get_timestamp_begin : returns the minimum timestamp of
519 * all the traces in the traceset.
522 guint64
lttv_traceset_get_timestamp_begin(LttvTraceset
*traceset
)
524 struct bt_context
*bt_ctx
;
525 bt_ctx
= lttv_traceset_get_context(traceset
);
526 guint64 timestamp_min
, timestamp_cur
= 0;
529 LttvTrace
*currentTrace
;
530 trace_count
= traceset
->traces
->len
;
534 timestamp_min
= G_MAXUINT64
;
535 for(i
= 0; i
< trace_count
;i
++)
537 currentTrace
= g_ptr_array_index(traceset
->traces
,i
);
538 timestamp_cur
= bt_trace_handle_get_timestamp_begin(bt_ctx
,
541 if(timestamp_cur
< timestamp_min
)
542 timestamp_min
= timestamp_cur
;
545 return timestamp_min
;
549 * lttv_traceset_get_timestamp_end: returns the maximum timestamp of
550 * all the traces in the traceset.
553 guint64
lttv_traceset_get_timestamp_end(LttvTraceset
*traceset
)
555 struct bt_context
*bt_ctx
;
556 bt_ctx
= lttv_traceset_get_context(traceset
);
557 guint64 timestamp_max
, timestamp_cur
= 0;
560 LttvTrace
*currentTrace
;
561 trace_count
= traceset
->traces
->len
;
563 if(trace_count
== 0){
568 for(i
=0; i
< trace_count
;i
++)
570 currentTrace
= g_ptr_array_index(traceset
->traces
,i
);
571 timestamp_cur
= bt_trace_handle_get_timestamp_end(bt_ctx
,
574 if(timestamp_cur
> timestamp_max
){
575 timestamp_max
= timestamp_cur
;
579 return timestamp_max
;
582 * lttv_traceset_get_time_span_real : return a TimeInterval representing the
583 * minimum timestamp and the maximum timestamp of the traceset.
586 TimeInterval
lttv_traceset_get_time_span_real(LttvTraceset
*ts
)
590 if(ltt_time_compare(ts
->time_span
.start_time
,
591 ltt_time_zero
) == 0 && ts
->traces
->len
> 0){
592 ts
->time_span
.start_time
= ltt_time_from_uint64(
593 lttv_traceset_get_timestamp_first_event(ts
));
594 ts
->time_span
.end_time
= ltt_time_from_uint64(
595 lttv_traceset_get_timestamp_last_event(ts
));
597 return ts
->time_span
;
601 * lttv_traceset_get_time_span : return a TimeInterval representing the
602 * minimum timestamp and the maximum timestamp of the traceset.
605 TimeInterval
lttv_traceset_get_time_span(LttvTraceset
*ts
)
607 if(ltt_time_compare(ts
->time_span
.start_time
, ltt_time_zero
) == 0){
608 ts
->time_span
.start_time
=ltt_time_from_uint64(
609 lttv_traceset_get_timestamp_begin(ts
));
610 ts
->time_span
.end_time
= ltt_time_from_uint64(
611 lttv_traceset_get_timestamp_end(ts
));
613 return ts
->time_span
;
616 const char *lttv_traceset_get_name_from_event(LttvEvent
*event
)
618 return bt_ctf_event_name(event
->bt_event
);
621 int set_values_position(const LttvTracesetPosition
*pos
)
623 LttvTracesetPosition previous_pos
;
624 previous_pos
.iter
= pos
->iter
;
625 previous_pos
.bt_pos
= bt_iter_get_pos(bt_ctf_get_iter(pos
->iter
));
626 /* Seek to the new desired position */
627 lttv_traceset_seek_to_position(pos
);
629 struct bt_ctf_event
*event
= bt_ctf_iter_read_event(pos
->iter
);
632 ((LttvTracesetPosition
*)pos
)->timestamp
= bt_ctf_get_timestamp(event
);
634 LttvEvent lttv_event
;
635 lttv_event
.bt_event
= event
;
636 ((LttvTracesetPosition
*)pos
)->cpu_id
= lttv_traceset_get_cpuid_from_event(<tv_event
);
639 /* The event is null */
643 /* Reassign the previously saved position */
644 lttv_traceset_seek_to_position(&previous_pos
);
645 /*We must desallocate because the function bt_iter_get_pos() does a g_new */
646 bt_iter_free_pos(previous_pos
.bt_pos
);
647 if (pos
->timestamp
== G_MAXUINT64
) {
653 guint64
lttv_traceset_position_get_timestamp(const LttvTracesetPosition
*pos
)
655 if(pos
->timestamp
== G_MAXUINT64
){
656 if(set_values_position(pos
) == 0){
661 return pos
->timestamp
;
664 int lttv_traceset_position_get_cpuid(const LttvTracesetPosition
*pos
){
665 if(pos
->cpu_id
== INT_MAX
){
666 if(set_values_position(pos
) == 0){
673 LttTime
lttv_traceset_position_get_time(const LttvTracesetPosition
*pos
)
675 return ltt_time_from_uint64(lttv_traceset_position_get_timestamp(pos
));
679 /* 0 if equals, other is different */
680 int lttv_traceset_position_compare(const LttvTracesetPosition
*pos1
, const LttvTracesetPosition
*pos2
)
682 #warning " TODO :Rename for lttv_traceset_position_equals && Must return COMPARAISON OF THE 2 POSITION && verify if it is the best way to compare position"
683 if(pos1
== NULL
|| pos2
== NULL
){
688 #ifdef HAVE_BT_ITER_EQUALS_POS
689 if(pos1
->timestamp
== G_MAXUINT64
|| pos2
->timestamp
== G_MAXUINT64
) {
690 res
= bt_iter_equals_pos(pos1
->bt_pos
, pos2
->bt_pos
);
695 guint64 timeStampPos1
,timeStampPos2
;
696 guint cpuId1
, cpuId2
;
698 timeStampPos1
= lttv_traceset_position_get_timestamp(pos1
);
699 timeStampPos2
= lttv_traceset_position_get_timestamp(pos2
);
701 if (timeStampPos1
== timeStampPos2
) {
703 cpuId1
= lttv_traceset_position_get_cpuid(pos1
);
704 cpuId2
= lttv_traceset_position_get_cpuid(pos2
);
706 if(cpuId1
== cpuId2
){
717 int lttv_traceset_position_time_compare(const LttvTracesetPosition
*pos1
,
718 const LttvTracesetPosition
*pos2
)
720 guint64 timeStampPos1
,timeStampPos2
;
722 timeStampPos1
= lttv_traceset_position_get_timestamp(pos1
);
723 timeStampPos2
= lttv_traceset_position_get_timestamp(pos2
);
725 return timeStampPos1
- timeStampPos2
;
727 int lttv_traceset_position_compare_current(const LttvTraceset
*ts
,
728 const LttvTracesetPosition
*pos
)
731 LttvTracesetPosition
*curPos
= lttv_traceset_create_current_position(ts
);
733 result
= lttv_traceset_position_compare(curPos
,pos
);
735 lttv_traceset_destroy_position(curPos
);
740 LttTime
lttv_traceset_get_current_time(const LttvTraceset
*ts
)
742 LttvTracesetPosition
*curPos
= lttv_traceset_create_current_position(ts
);
743 guint64 currentTimestamp
= lttv_traceset_position_get_timestamp(curPos
);
744 lttv_traceset_destroy_position(curPos
);
746 return ltt_time_from_uint64(currentTimestamp
);