bfab0368db84547e0a01718165d56926cb20bae3
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/hook.h>
28 #include <babeltrace/context.h>
29 #include <babeltrace/iterator.h>
30 #include <babeltrace/ctf/events.h>
32 /* To traverse a tree recursively */
36 /* A trace is a sequence of events gathered in the same tracing session. The
37 events may be stored in several tracefiles in the same directory.
38 A trace set is defined when several traces are to be analyzed together,
39 possibly to study the interactions between events in the different traces.
43 LttvTraceset
*lttv_traceset_new(void)
46 struct bt_iter_pos begin_pos
;
48 s
= g_new(LttvTraceset
, 1);
50 s
->traces
= g_ptr_array_new();
51 s
->context
= bt_context_create();
52 s
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
53 //TODO remove this when we have really mecanism
54 //s->tmpState = g_new(LttvTraceState *, 1);
55 //lttv_trace_state_init(s->tmpState,0);
56 begin_pos
.type
= BT_SEEK_BEGIN
;
58 //s->iter = bt_ctf_iter_create(lttv_traceset_get_context(s),
62 s
->event_hooks
= lttv_hooks_new();
63 s
->state_trace_handle_index
= g_ptr_array_new();
70 char * lttv_traceset_name(LttvTraceset
* s
)
76 LttvTrace
*lttv_trace_new(LttTrace
*t
)
80 new_trace
= g_new(LttvTrace
, 1);
81 new_trace
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
83 new_trace
->ref_count
= 0;
89 * lttv_trace_create : Create a trace from a path
91 * ts is the traceset in which will be contained the trace
93 * path is the path where to find a trace. It is not recursive.
95 * This function is static since a trace should always be contained in a
98 * return the created trace or NULL on failure
100 static LttvTrace
*lttv_trace_create(LttvTraceset
*ts
, const char *path
)
102 int id
= bt_context_add_trace(lttv_traceset_get_context(ts
),
111 // Create the trace and save the trace handle id returned by babeltrace
112 LttvTrace
*new_trace
;
114 new_trace
= g_new(LttvTrace
, 1);
115 new_trace
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
117 new_trace
->ref_count
= 0;
118 new_trace
->traceset
= ts
;
119 new_trace
->state
= g_new(LttvTraceState
,1);
120 lttv_trace_state_init(new_trace
->state
,new_trace
);
122 /* Add the state to the trace_handle to state index */
123 g_ptr_array_set_size(ts
->state_trace_handle_index
,id
+1);
124 g_ptr_array_index(ts
->state_trace_handle_index
,id
) = new_trace
->state
;
130 * lttv_trace_create : Create and add a single trace to a traceset
132 * ts is the traceset in which will be contained the trace
134 * path is the path where to find a trace. It is not recursive.
136 * return a positive integer (>=0)on success or -1 on failure
138 static int lttv_traceset_create_trace(LttvTraceset
*ts
, const char *path
)
140 LttvTrace
*trace
= lttv_trace_create(ts
, path
);
144 lttv_traceset_add(ts
, trace
);
148 LttvTraceset
*lttv_traceset_copy(LttvTraceset
*s_orig
)
154 s
= g_new(LttvTraceset
, 1);
156 s
->traces
= g_ptr_array_new();
157 s
->state_trace_handle_index
= g_ptr_array_new();
158 for(i
=0;i
<s_orig
->traces
->len
;i
++)
160 trace
= g_ptr_array_index(s_orig
->traces
, i
);
163 /* WARNING: this is an alias, not a copy. */
164 g_ptr_array_add(s
->traces
, trace
);
166 g_ptr_array_set_size(s
->state_trace_handle_index
,trace
->id
+1);
167 g_ptr_array_index(s
->state_trace_handle_index
,trace
->id
) = trace
->state
;
170 s
->context
= s_orig
->context
;
171 bt_context_get(s
->context
);
172 s
->a
= LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig
->a
)));
177 LttvTraceset
*lttv_traceset_load(const gchar
*filename
)
179 LttvTraceset
*s
= g_new(LttvTraceset
,1);
182 s
->filename
= g_strdup(filename
);
183 tf
= fopen(filename
,"r");
185 g_critical("NOT IMPLEMENTED : load traceset data from a XML file");
191 gint
lttv_traceset_save(LttvTraceset
*s
)
195 tf
= fopen(s
->filename
, "w");
197 g_critical("NOT IMPLEMENTED : save traceset data in a XML file");
203 void lttv_traceset_destroy(LttvTraceset
*s
)
207 for(i
=0;i
<s
->traces
->len
;i
++) {
208 LttvTrace
*trace
= g_ptr_array_index(s
->traces
, i
);
209 lttv_trace_unref(trace
);
210 // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed
211 //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id);
212 if(lttv_trace_get_ref_number(trace
) == 0)
213 lttv_trace_destroy(trace
);
215 g_ptr_array_free(s
->traces
, TRUE
);
216 bt_context_put(s
->context
);
217 g_object_unref(s
->a
);
221 struct bt_context
*lttv_traceset_get_context(LttvTraceset
*s
)
226 LttvTraceset
*lttv_trace_get_traceset(LttvTrace
*trace
)
228 return trace
->traceset
;
231 LttvHooks
*lttv_traceset_get_hooks(LttvTraceset
*s
)
233 return s
->event_hooks
;
236 void lttv_trace_destroy(LttvTrace
*t
)
238 g_object_unref(t
->a
);
243 void lttv_traceset_add(LttvTraceset
*s
, LttvTrace
*t
)
246 g_ptr_array_add(s
->traces
, t
);
249 int lttv_traceset_add_path(LttvTraceset
*ts
, char *trace_path
)
253 char * const paths
[2] = { trace_path
, NULL
};
256 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
258 g_warning("Cannot traverse \"%s\" for reading.\n",
264 while ((node
= fts_read(tree
))) {
266 if (!(node
->fts_info
& FTS_D
))
269 dirfd
= open(node
->fts_accpath
, 0);
271 g_warning("Unable to open trace "
272 "directory file descriptor : %s.", node
->fts_accpath
);
277 // Check if a metadata file exists in the current directory
278 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
282 g_warning("Unable to open metadata "
283 "file descriptor : %s.", node
->fts_accpath
);
289 g_warning("Unable to close metadata "
290 "file descriptor : %s.", node
->fts_accpath
);
295 g_warning("Unable to close trace "
296 "directory file descriptor : %s.", node
->fts_accpath
);
300 ret
= lttv_traceset_create_trace(ts
, node
->fts_accpath
);
302 g_warning("Opening trace \"%s\" from %s "
303 "for reading.", node
->fts_accpath
, trace_path
);
310 ret
= fts_close(tree
);
312 g_warning("Unable to close tree "
313 "file descriptor : %s.", trace_path
);
318 unsigned lttv_traceset_number(LttvTraceset
*s
)
320 return s
->traces
->len
;
324 LttvTrace
*lttv_traceset_get(LttvTraceset
*s
, unsigned i
)
326 g_assert(s
->traces
->len
> i
);
327 return ((LttvTrace
*)s
->traces
->pdata
[i
]);
331 void lttv_traceset_remove(LttvTraceset
*s
, unsigned i
)
334 g_assert(s
->traces
->len
> i
);
335 t
= (LttvTrace
*)s
->traces
->pdata
[i
];
337 bt_context_remove_trace(lttv_traceset_get_context(s
), t
->id
);
338 g_ptr_array_remove_index(s
->traces
, i
);
342 /* A set of attributes is attached to each trace set, trace and tracefile
343 to store user defined data as needed. */
345 LttvAttribute
*lttv_traceset_attribute(LttvTraceset
*s
)
351 LttvAttribute
*lttv_trace_attribute(LttvTrace
*t
)
357 LttTrace
*lttv_trace(LttvTrace
*t
)
363 gint
lttv_trace_get_id(LttvTrace
*t
)
368 guint
lttv_trace_get_ref_number(LttvTrace
* t
)
370 // todo mdenis: adapt to babeltrace
374 guint
lttv_trace_ref(LttvTrace
* t
)
381 guint
lttv_trace_unref(LttvTrace
* t
)
383 if(likely(t
->ref_count
> 0))
389 guint
lttv_trace_get_num_cpu(LttvTrace
*t
)
391 #warning "TODO - Set the right number of CPU"
395 LttvTracesetPosition
*lttv_traceset_create_position(LttvTraceset
*traceset
)
401 void lttv_traceset_destroy_position(LttvTracesetPosition
*traceset_pos
)
407 void lttv_traceset_seek_to_position(LttvTracesetPosition
*traceset_pos
)
412 guint
lttv_traceset_get_cpuid_from_event(LttvEvent
*event
)
414 struct definition
*scope
;
415 unsigned long timestamp
;
418 struct bt_ctf_event
*ctf_event
= event
->bt_event
;
419 timestamp
= bt_ctf_get_timestamp(ctf_event
);
420 if (timestamp
== -1ULL) {
423 scope
= bt_ctf_get_top_level_scope(ctf_event
, BT_STREAM_PACKET_CONTEXT
);
424 if (bt_ctf_field_get_error()) {
427 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(ctf_event
, scope
, "cpu_id"));
428 if (bt_ctf_field_get_error()) {
435 const char *lttv_traceset_get_name_from_event(LttvEvent
*event
)
437 return bt_ctf_event_name(event
->bt_event
);
This page took 0.059784 seconds and 4 git commands to generate.