Initial port of the state system to the LTTng 2.0 trace format
[lttv.git] / lttv / lttv / traceset.c
CommitLineData
9c312311 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
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;
7 *
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.
12 *
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,
16 * MA 02111-1307, USA.
17 */
18
4e4d11b3 19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
dc877563 22
23#include <lttv/traceset.h>
3e67c985 24#include <lttv/iattribute.h>
7a4bdb54
YB
25#include <lttv/state.h>
26#include <lttv/hook.h>
f7afe191 27#include <stdio.h>
cbb811b3 28#include <babeltrace/context.h>
7a4bdb54
YB
29#include <babeltrace/iterator.h>
30#include <babeltrace/ctf/events.h>
dc877563 31/* A trace is a sequence of events gathered in the same tracing session. The
32 events may be stored in several tracefiles in the same directory.
33 A trace set is defined when several traces are to be analyzed together,
34 possibly to study the interactions between events in the different traces.
35*/
36
dc877563 37
7a4bdb54 38LttvTraceset *lttv_traceset_new(void)
dc877563 39{
90e19f82 40 LttvTraceset *s;
7a4bdb54 41 struct bt_iter_pos begin_pos;
dc877563 42
90e19f82
AM
43 s = g_new(LttvTraceset, 1);
44 s->filename = NULL;
45 s->traces = g_ptr_array_new();
cbb811b3 46 s->context = bt_context_create();
90e19f82 47 s->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
7a4bdb54
YB
48 //TODO remove this when we have really mecanism
49 //s->tmpState = g_new(LttvTraceState *, 1);
50 //lttv_trace_state_init(s->tmpState,0);
51 begin_pos.type = BT_SEEK_BEGIN;
52
53 //s->iter = bt_ctf_iter_create(lttv_traceset_get_context(s),
54 // &begin_pos,
55 // NULL);
56 s->iter = 0;
57 s->event_hooks = lttv_hooks_new();
58
59
60
61
90e19f82 62 return s;
dc877563 63}
64
49bf71b5 65char * lttv_traceset_name(LttvTraceset * s)
66{
90e19f82 67 return s->filename;
49bf71b5 68}
69
2bc1bcfb 70#ifdef BABEL_CLEANUP
71LttvTrace *lttv_trace_new(LttTrace *t)
308711e5 72{
90e19f82 73 LttvTrace *new_trace;
308711e5 74
90e19f82
AM
75 new_trace = g_new(LttvTrace, 1);
76 new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
2bc1bcfb 77 new_trace->id = t;
90e19f82
AM
78 new_trace->ref_count = 0;
79 return new_trace;
308711e5 80}
2bc1bcfb 81#endif
308711e5 82
2bc1bcfb 83/*
84 * lttv_trace_create : Create a trace from a path
85 *
86 * ts is the traceset in which will be contained the trace
87 *
88 * path is the path where to find a trace. It is not recursive.
89 *
90 * This function is static since a trace should always be contained in a
91 * traceset.
92 *
93 * return the created trace or NULL on failure
94 */
95static LttvTrace *lttv_trace_create(LttvTraceset *ts, const char *path)
96{
97 int id = bt_context_add_trace(lttv_traceset_get_context(ts),
98 path,
99 "ctf",
100 NULL,
101 NULL,
102 NULL);
103 if (id < 0) {
104 return NULL;
105 }
106 // Create the trace and save the trace handle id returned by babeltrace
107 LttvTrace *new_trace;
108
109 new_trace = g_new(LttvTrace, 1);
110 new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
111 new_trace->id = id;
112 new_trace->ref_count = 0;
7a4bdb54
YB
113 new_trace->traceset = ts;
114 new_trace->state = g_new(LttvTraceState,1);
115 lttv_trace_state_init(new_trace->state,new_trace);
116 ts->tmpState = new_trace->state;
2bc1bcfb 117 return new_trace;
118}
119
120/*
121 * lttv_trace_create : Create and add a single trace to a traceset
122 *
123 * ts is the traceset in which will be contained the trace
124 *
125 * path is the path where to find a trace. It is not recursive.
126 *
127 * return a positive integer (>=0)on success or -1 on failure
128 */
129static int lttv_traceset_create_trace(LttvTraceset *ts, const char *path)
130{
131 LttvTrace *trace = lttv_trace_create(ts, path);
132 if (trace == NULL) {
133 return -1;
134 }
135 lttv_traceset_add(ts, trace);
136 return 0;
137}
308711e5 138
f7afe191 139LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig)
140{
90e19f82
AM
141 guint i;
142 LttvTraceset *s;
143 LttvTrace * trace;
f7afe191 144
90e19f82
AM
145 s = g_new(LttvTraceset, 1);
146 s->filename = NULL;
147 s->traces = g_ptr_array_new();
148 for(i=0;i<s_orig->traces->len;i++)
149 {
150 trace = g_ptr_array_index(s_orig->traces, i);
151 trace->ref_count++;
2176f952 152
7a4bdb54 153 /* WARNING: this is an alias, not a copy. */
90e19f82
AM
154 g_ptr_array_add(s->traces, trace);
155 }
cbb811b3
YB
156 s->context = s_orig->context;
157 bt_context_get(s->context);
90e19f82
AM
158 s->a = LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig->a)));
159 return s;
f7afe191 160}
dc877563 161
f7afe191 162
163LttvTraceset *lttv_traceset_load(const gchar *filename)
164{
90e19f82
AM
165 LttvTraceset *s = g_new(LttvTraceset,1);
166 FILE *tf;
f7afe191 167
90e19f82
AM
168 s->filename = g_strdup(filename);
169 tf = fopen(filename,"r");
170
171 g_critical("NOT IMPLEMENTED : load traceset data from a XML file");
172
173 fclose(tf);
174 return s;
f7afe191 175}
176
177gint lttv_traceset_save(LttvTraceset *s)
178{
90e19f82 179 FILE *tf;
f7afe191 180
90e19f82 181 tf = fopen(s->filename, "w");
f7afe191 182
90e19f82
AM
183 g_critical("NOT IMPLEMENTED : save traceset data in a XML file");
184
185 fclose(tf);
186 return 0;
f7afe191 187}
308711e5 188
ba576a78 189void lttv_traceset_destroy(LttvTraceset *s)
dc877563 190{
90e19f82 191 guint i;
5e2c04a2 192
90e19f82
AM
193 for(i=0;i<s->traces->len;i++) {
194 LttvTrace *trace = g_ptr_array_index(s->traces, i);
195 lttv_trace_unref(trace);
2bc1bcfb 196 // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed
197 //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id);
90e19f82
AM
198 if(lttv_trace_get_ref_number(trace) == 0)
199 lttv_trace_destroy(trace);
200 }
201 g_ptr_array_free(s->traces, TRUE);
cbb811b3 202 bt_context_put(s->context);
90e19f82
AM
203 g_object_unref(s->a);
204 g_free(s);
dc877563 205}
206
922581a4
YB
207struct bt_context *lttv_traceset_get_context(LttvTraceset *s)
208{
209 return s->context;
210}
211
7a4bdb54
YB
212LttvTraceset *lttv_trace_get_traceset(LttvTrace *trace)
213{
214 return trace->traceset;
215}
216
217LttvHooks *lttv_traceset_get_hooks(LttvTraceset *s)
218{
219 return s->event_hooks;
220}
221
308711e5 222void lttv_trace_destroy(LttvTrace *t)
223{
90e19f82
AM
224 g_object_unref(t->a);
225 g_free(t);
308711e5 226}
227
228
229void lttv_traceset_add(LttvTraceset *s, LttvTrace *t)
dc877563 230{
90e19f82
AM
231 t->ref_count++;
232 g_ptr_array_add(s->traces, t);
dc877563 233}
234
2bc1bcfb 235int lttv_traceset_add_path(LttvTraceset *ts, const char *trace_path)
236{
237 // todo mdenis 2012-03-27: add trace recursively and update comment
238 int ret = lttv_traceset_create_trace(ts, trace_path);
239 return ret;
240}
dc877563 241
242unsigned lttv_traceset_number(LttvTraceset *s)
243{
90e19f82 244 return s->traces->len;
dc877563 245}
246
247
308711e5 248LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i)
dc877563 249{
90e19f82
AM
250 g_assert(s->traces->len > i);
251 return ((LttvTrace *)s->traces->pdata[i]);
dc877563 252}
253
254
ba576a78 255void lttv_traceset_remove(LttvTraceset *s, unsigned i)
dc877563 256{
90e19f82
AM
257 LttvTrace * t;
258 g_assert(s->traces->len > i);
259 t = (LttvTrace *)s->traces->pdata[i];
260 t->ref_count--;
2bc1bcfb 261 bt_context_remove_trace(lttv_traceset_get_context(s), t->id);
90e19f82 262 g_ptr_array_remove_index(s->traces, i);
dc877563 263}
264
265
266/* A set of attributes is attached to each trace set, trace and tracefile
90e19f82 267 to store user defined data as needed. */
dc877563 268
269LttvAttribute *lttv_traceset_attribute(LttvTraceset *s)
270{
90e19f82 271 return s->a;
dc877563 272}
273
274
308711e5 275LttvAttribute *lttv_trace_attribute(LttvTrace *t)
276{
90e19f82 277 return t->a;
308711e5 278}
279
2bc1bcfb 280#ifdef BABEL_CLEANUP
308711e5 281LttTrace *lttv_trace(LttvTrace *t)
dc877563 282{
90e19f82 283 return t->t;
dc877563 284}
2bc1bcfb 285#endif
286
287gint lttv_trace_get_id(LttvTrace *t)
288{
289 return t->id;
290}
308711e5 291
2176f952 292guint lttv_trace_get_ref_number(LttvTrace * t)
293{
2bc1bcfb 294 // todo mdenis: adapt to babeltrace
90e19f82 295 return t->ref_count;
2176f952 296}
a43d67ba 297
298guint lttv_trace_ref(LttvTrace * t)
299{
90e19f82
AM
300 t->ref_count++;
301
302 return t->ref_count;
a43d67ba 303}
304
305guint lttv_trace_unref(LttvTrace * t)
306{
90e19f82
AM
307 if(likely(t->ref_count > 0))
308 t->ref_count--;
a43d67ba 309
90e19f82 310 return t->ref_count;
a43d67ba 311}
312
7a4bdb54
YB
313guint lttv_trace_get_num_cpu(LttvTrace *t)
314{
315#warning "TODO - Set the right number of CPU"
316 return 24;
317}
318
319LttvTracesetPosition *lttv_traceset_create_position(LttvTraceset *traceset)
320{
321#warning "TODO"
322 return NULL;
323}
324
325void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos)
326{
327#warning "TODO"
328 return NULL;
329}
330
331void lttv_traceset_seek_to_position(LttvTracesetPosition *traceset_pos)
332{
333#warning "TODO"
334}
335
336guint lttv_traceset_get_cpuid_from_event(LttvEvent *event)
337{
338 struct definition *scope;
339 unsigned long timestamp;
340 unsigned int cpu_id;
341
342 struct bt_ctf_event *ctf_event = event->bt_event;
343 timestamp = bt_ctf_get_timestamp(ctf_event);
344 if (timestamp == -1ULL) {
345 return 0;
346 }
347 scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT);
348 if (bt_ctf_field_get_error()) {
349 return 0;
350 }
351 cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(ctf_event, scope, "cpu_id"));
352 if (bt_ctf_field_get_error()) {
353 return 0;
354 } else {
355 return cpu_id;
356 }
357}
358
359const char *lttv_traceset_get_name_from_event(LttvEvent *event)
360{
361 return bt_ctf_event_name(event->bt_event);
362}
This page took 0.084043 seconds and 4 git commands to generate.