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 */
37 /* 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
);
60 //TODO remove this when we have really mecanism
61 //s->tmpState = g_new(LttvTraceState *, 1);
62 //lttv_trace_state_init(s->tmpState,0);
64 /*Initialize iterator to the beginning of the traces*/
65 begin_pos
.type
= BT_SEEK_BEGIN
;
66 ts
->iter
= bt_ctf_iter_create(ts
->context
, &begin_pos
, NULL
);
68 ts
->event_hooks
= lttv_hooks_new();
70 ts
->state_trace_handle_index
= g_ptr_array_new();
71 ts
->has_precomputed_states
= FALSE
;
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 char * const paths
[2] = { trace_path
, NULL
};
285 gboolean metaFileFound
= FALSE
;
287 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
289 g_warning("Cannot traverse \"%s\" for reading.\n",
295 while ((node
= fts_read(tree
))) {
297 if (!(node
->fts_info
& FTS_D
))
300 dirfd
= open(node
->fts_accpath
, 0);
302 g_warning("Unable to open trace "
303 "directory file descriptor : %s.", node
->fts_accpath
);
308 // Check if a metadata file exists in the current directory
309 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
313 g_warning("Unable to open metadata "
314 "file descriptor : %s.", node
->fts_accpath
);
320 g_warning("Unable to close metadata "
321 "file descriptor : %s.", node
->fts_accpath
);
326 g_warning("Unable to close trace "
327 "directory file descriptor : %s.", node
->fts_accpath
);
331 ret
= lttv_traceset_create_trace(ts
, node
->fts_accpath
);
333 g_warning("Opening trace \"%s\" from %s "
334 "for reading.", node
->fts_accpath
, trace_path
);
337 metaFileFound
= TRUE
;
342 ret
= fts_close(tree
);
344 g_warning("Unable to close tree "
345 "file descriptor : %s.", trace_path
);
354 unsigned lttv_traceset_number(LttvTraceset
*s
)
356 return s
->traces
->len
;
360 LttvTrace
*lttv_traceset_get(LttvTraceset
*s
, unsigned i
)
362 g_assert(s
->traces
->len
> i
);
363 return ((LttvTrace
*)s
->traces
->pdata
[i
]);
367 void lttv_traceset_remove(LttvTraceset
*s
, unsigned i
)
370 g_assert(s
->traces
->len
> i
);
371 t
= (LttvTrace
*)s
->traces
->pdata
[i
];
373 bt_context_remove_trace(lttv_traceset_get_context(s
), t
->id
);
374 g_ptr_array_remove_index(s
->traces
, i
);
378 /* A set of attributes is attached to each trace set, trace and tracefile
379 to store user defined data as needed. */
381 LttvAttribute
*lttv_traceset_attribute(LttvTraceset
*s
)
387 LttvAttribute
*lttv_trace_attribute(LttvTrace
*t
)
393 gint
lttv_trace_get_id(LttvTrace
*t
)
398 guint
lttv_trace_get_ref_number(LttvTrace
* t
)
400 // todo mdenis: adapt to babeltrace
404 guint
lttv_trace_ref(LttvTrace
* t
)
411 guint
lttv_trace_unref(LttvTrace
* t
)
413 if(likely(t
->ref_count
> 0))
419 guint
lttv_trace_get_num_cpu(LttvTrace
*t
)
421 #warning "TODO - Set the right number of CPU"
425 LttvTracesetPosition
*lttv_traceset_create_current_position(LttvTraceset
*traceset
)
427 LttvTracesetPosition
*traceset_pos
;
429 traceset_pos
= g_new(LttvTracesetPosition
, 1);
431 /* Check if the new passed */
432 if(traceset_pos
== NULL
) {
436 traceset_pos
->iter
= traceset
->iter
;
437 traceset_pos
->bt_pos
= bt_iter_get_pos(bt_ctf_get_iter(traceset
->iter
));
438 traceset_pos
->timestamp
= G_MAXUINT64
;
439 traceset_pos
->cpu_id
= INT_MAX
;
444 LttvTracesetPosition
*lttv_traceset_create_time_position(LttvTraceset
*traceset
,
447 LttvTracesetPosition
*traceset_pos
;
449 traceset_pos
= g_new(LttvTracesetPosition
, 1);
451 /* Check if the new passed */
452 if(traceset_pos
== NULL
) {
456 traceset_pos
->iter
= traceset
->iter
;
457 traceset_pos
->bt_pos
= bt_iter_create_time_pos(
458 bt_ctf_get_iter(traceset_pos
->iter
),
459 ltt_time_to_uint64(timestamp
));
460 traceset_pos
->timestamp
= G_MAXUINT64
;
461 traceset_pos
->cpu_id
= INT_MAX
;
465 void lttv_traceset_destroy_position(LttvTracesetPosition
*traceset_pos
)
467 bt_iter_free_pos(traceset_pos
->bt_pos
);
468 g_free(traceset_pos
);
471 void lttv_traceset_seek_to_position(const LttvTracesetPosition
*traceset_pos
)
473 bt_iter_set_pos(bt_ctf_get_iter(traceset_pos
->iter
), traceset_pos
->bt_pos
);
476 guint
lttv_traceset_get_cpuid_from_event(LttvEvent
*event
)
478 unsigned long timestamp
;
481 struct bt_ctf_event
*ctf_event
= event
->bt_event
;
482 timestamp
= bt_ctf_get_timestamp(ctf_event
);
483 if (timestamp
== -1ULL) {
486 const struct definition
*scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_PACKET_CONTEXT
);
487 if (bt_ctf_field_get_error()) {
490 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(ctf_event
, scope
, "cpu_id"));
491 if (bt_ctf_field_get_error()) {
498 guint64
lttv_traceset_get_timestamp_first_event(LttvTraceset
*ts
)
500 LttvTracesetPosition begin_position
;
501 struct bt_iter_pos pos
;
502 begin_position
.bt_pos
= &pos
;
503 begin_position
.timestamp
= G_MAXUINT64
;
504 begin_position
.cpu_id
= INT_MAX
;
506 /* Assign iterator to the beginning of the traces */
507 begin_position
.bt_pos
->type
= BT_SEEK_BEGIN
;
508 begin_position
.iter
= ts
->iter
;
510 return lttv_traceset_position_get_timestamp(&begin_position
);
514 * lttv_traceset_get_timestamp_begin : returns the minimum timestamp of
515 * all the traces in the traceset.
518 guint64
lttv_traceset_get_timestamp_begin(LttvTraceset
*traceset
)
520 struct bt_context
*bt_ctx
;
521 bt_ctx
= lttv_traceset_get_context(traceset
);
522 guint64 timestamp_min
, timestamp_cur
= 0;
525 LttvTrace
*currentTrace
;
526 trace_count
= traceset
->traces
->len
;
530 timestamp_min
= G_MAXUINT64
;
531 for(i
= 0; i
< trace_count
;i
++)
533 currentTrace
= g_ptr_array_index(traceset
->traces
,i
);
534 timestamp_cur
= bt_trace_handle_get_timestamp_begin(bt_ctx
,
536 if(timestamp_cur
< timestamp_min
)
537 timestamp_min
= timestamp_cur
;
540 return timestamp_min
;
544 * lttv_traceset_get_timestamp_end: returns the maximum timestamp of
545 * all the traces in the traceset.
548 guint64
lttv_traceset_get_timestamp_end(LttvTraceset
*traceset
)
550 struct bt_context
*bt_ctx
;
551 bt_ctx
= lttv_traceset_get_context(traceset
);
552 guint64 timestamp_max
, timestamp_cur
= 0;
555 LttvTrace
*currentTrace
;
556 trace_count
= traceset
->traces
->len
;
558 if(trace_count
== 0){
563 for(i
=0; i
< trace_count
;i
++)
565 currentTrace
= g_ptr_array_index(traceset
->traces
,i
);
566 timestamp_cur
= bt_trace_handle_get_timestamp_end(bt_ctx
,
568 if(timestamp_cur
> timestamp_max
){
569 timestamp_max
= timestamp_cur
;
573 return timestamp_max
;
576 * lttv_traceset_get_time_span_real : return a TimeInterval representing the
577 * minimum timestamp dans le maximum timestamp of the traceset.
580 TimeInterval
lttv_traceset_get_time_span_real(LttvTraceset
*ts
)
582 TimeInterval time_span
;
583 time_span
.start_time
=ltt_time_from_uint64(lttv_traceset_get_timestamp_first_event(ts
));
584 time_span
.end_time
= ltt_time_from_uint64(lttv_traceset_get_timestamp_end(ts
));
589 * lttv_traceset_get_time_span : return a TimeInterval representing the
590 * minimum timestamp dans le maximum timestamp of the traceset.
593 TimeInterval
lttv_traceset_get_time_span(LttvTraceset
*ts
)
595 TimeInterval time_span
;
596 time_span
.start_time
=ltt_time_from_uint64(lttv_traceset_get_timestamp_begin(ts
));
597 time_span
.end_time
= ltt_time_from_uint64(lttv_traceset_get_timestamp_end(ts
));
601 const char *lttv_traceset_get_name_from_event(LttvEvent
*event
)
603 return bt_ctf_event_name(event
->bt_event
);
606 int set_values_position(const LttvTracesetPosition
*pos
)
608 LttvTracesetPosition previous_pos
;
609 previous_pos
.iter
= pos
->iter
;
610 previous_pos
.bt_pos
= bt_iter_get_pos(bt_ctf_get_iter(pos
->iter
));
611 /* Seek to the new desired position */
612 lttv_traceset_seek_to_position(pos
);
614 struct bt_ctf_event
*event
= bt_ctf_iter_read_event(pos
->iter
);
617 ((LttvTracesetPosition
*)pos
)->timestamp
= bt_ctf_get_timestamp_raw(event
);
619 LttvEvent lttv_event
;
620 lttv_event
.bt_event
= event
;
621 ((LttvTracesetPosition
*)pos
)->cpu_id
= lttv_traceset_get_cpuid_from_event(<tv_event
);
624 /* The event is null */
628 /* Reassign the previously saved position */
629 lttv_traceset_seek_to_position(&previous_pos
);
630 /*We must desallocate because the function bt_iter_get_pos() does a g_new */
631 bt_iter_free_pos(previous_pos
.bt_pos
);
634 guint64
lttv_traceset_position_get_timestamp(const LttvTracesetPosition
*pos
)
636 if(pos
->timestamp
== G_MAXUINT64
){
637 if(set_values_position(pos
) == 0){
642 return pos
->timestamp
;
645 int lttv_traceset_position_get_cpuid(const LttvTracesetPosition
*pos
){
646 if(pos
->cpu_id
== INT_MAX
){
647 if(set_values_position(pos
) == 0){
654 LttTime
lttv_traceset_position_get_time(const LttvTracesetPosition
*pos
)
656 return ltt_time_from_uint64(lttv_traceset_position_get_timestamp(pos
));
661 int lttv_traceset_position_compare(const LttvTracesetPosition
*pos1
, const LttvTracesetPosition
*pos2
)
663 #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"
664 if(pos1
== NULL
|| pos2
== NULL
){
668 guint64 timeStampPos1
,timeStampPos2
;
669 guint cpuId1
, cpuId2
;
671 timeStampPos1
= lttv_traceset_position_get_timestamp(pos1
);
672 timeStampPos2
= lttv_traceset_position_get_timestamp(pos2
);
675 cpuId1
= lttv_traceset_position_get_cpuid(pos1
);
676 cpuId2
= lttv_traceset_position_get_cpuid(pos2
);
678 if(timeStampPos1
== timeStampPos2
&& cpuId1
== cpuId2
){
686 int lttv_traceset_position_time_compare(const LttvTracesetPosition
*pos1
,
687 const LttvTracesetPosition
*pos2
)
689 guint64 timeStampPos1
,timeStampPos2
;
691 timeStampPos1
= lttv_traceset_position_get_timestamp(pos1
);
692 timeStampPos2
= lttv_traceset_position_get_timestamp(pos2
);
694 return timeStampPos1
- timeStampPos2
;
696 int lttv_traceset_position_compare_current(const LttvTraceset
*ts
,
697 const LttvTracesetPosition
*pos
)
700 LttvTracesetPosition
*curPos
= lttv_traceset_create_current_position(ts
);
702 result
= lttv_traceset_position_compare(curPos
,pos
);
704 lttv_traceset_destroy_position(curPos
);
709 LttTime
lttv_traceset_get_current_time(const LttvTraceset
*ts
)
711 LttvTracesetPosition
*curPos
= lttv_traceset_create_current_position(ts
);
712 guint64 currentTimestamp
= lttv_traceset_position_get_timestamp(curPos
);
713 lttv_traceset_destroy_position(curPos
);
715 return ltt_time_from_uint64(currentTimestamp
);