Commit | Line | Data |
---|---|---|
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> |
f7afe191 | 25 | #include <stdio.h> |
cbb811b3 | 26 | #include <babeltrace/context.h> |
dc877563 | 27 | |
28 | /* A trace is a sequence of events gathered in the same tracing session. The | |
29 | events may be stored in several tracefiles in the same directory. | |
30 | A trace set is defined when several traces are to be analyzed together, | |
31 | possibly to study the interactions between events in the different traces. | |
32 | */ | |
33 | ||
34 | struct _LttvTraceset { | |
90e19f82 AM |
35 | char * filename; |
36 | GPtrArray *traces; | |
cbb811b3 | 37 | struct bt_context *context; |
90e19f82 | 38 | LttvAttribute *a; |
308711e5 | 39 | }; |
40 | ||
41 | ||
42 | struct _LttvTrace { | |
2bc1bcfb | 43 | // Trace id for babeltrace |
44 | gint id; | |
90e19f82 AM |
45 | LttvAttribute *a; |
46 | guint ref_count; | |
dc877563 | 47 | }; |
48 | ||
d83f6739 | 49 | LttvTraceset *lttv_traceset_new() |
dc877563 | 50 | { |
90e19f82 | 51 | LttvTraceset *s; |
dc877563 | 52 | |
90e19f82 AM |
53 | s = g_new(LttvTraceset, 1); |
54 | s->filename = NULL; | |
55 | s->traces = g_ptr_array_new(); | |
cbb811b3 | 56 | s->context = bt_context_create(); |
90e19f82 AM |
57 | s->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
58 | return s; | |
dc877563 | 59 | } |
60 | ||
49bf71b5 | 61 | char * lttv_traceset_name(LttvTraceset * s) |
62 | { | |
90e19f82 | 63 | return s->filename; |
49bf71b5 | 64 | } |
65 | ||
2bc1bcfb | 66 | #ifdef BABEL_CLEANUP |
67 | LttvTrace *lttv_trace_new(LttTrace *t) | |
308711e5 | 68 | { |
90e19f82 | 69 | LttvTrace *new_trace; |
308711e5 | 70 | |
90e19f82 AM |
71 | new_trace = g_new(LttvTrace, 1); |
72 | new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); | |
2bc1bcfb | 73 | new_trace->id = t; |
90e19f82 AM |
74 | new_trace->ref_count = 0; |
75 | return new_trace; | |
308711e5 | 76 | } |
2bc1bcfb | 77 | #endif |
308711e5 | 78 | |
2bc1bcfb | 79 | /* |
80 | * lttv_trace_create : Create a trace from a path | |
81 | * | |
82 | * ts is the traceset in which will be contained the trace | |
83 | * | |
84 | * path is the path where to find a trace. It is not recursive. | |
85 | * | |
86 | * This function is static since a trace should always be contained in a | |
87 | * traceset. | |
88 | * | |
89 | * return the created trace or NULL on failure | |
90 | */ | |
91 | static LttvTrace *lttv_trace_create(LttvTraceset *ts, const char *path) | |
92 | { | |
93 | int id = bt_context_add_trace(lttv_traceset_get_context(ts), | |
94 | path, | |
95 | "ctf", | |
96 | NULL, | |
97 | NULL, | |
98 | NULL); | |
99 | if (id < 0) { | |
100 | return NULL; | |
101 | } | |
102 | // Create the trace and save the trace handle id returned by babeltrace | |
103 | LttvTrace *new_trace; | |
104 | ||
105 | new_trace = g_new(LttvTrace, 1); | |
106 | new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); | |
107 | new_trace->id = id; | |
108 | new_trace->ref_count = 0; | |
109 | return new_trace; | |
110 | } | |
111 | ||
112 | /* | |
113 | * lttv_trace_create : Create and add a single trace to a traceset | |
114 | * | |
115 | * ts is the traceset in which will be contained the trace | |
116 | * | |
117 | * path is the path where to find a trace. It is not recursive. | |
118 | * | |
119 | * return a positive integer (>=0)on success or -1 on failure | |
120 | */ | |
121 | static int lttv_traceset_create_trace(LttvTraceset *ts, const char *path) | |
122 | { | |
123 | LttvTrace *trace = lttv_trace_create(ts, path); | |
124 | if (trace == NULL) { | |
125 | return -1; | |
126 | } | |
127 | lttv_traceset_add(ts, trace); | |
128 | return 0; | |
129 | } | |
308711e5 | 130 | |
f7afe191 | 131 | LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) |
132 | { | |
90e19f82 AM |
133 | guint i; |
134 | LttvTraceset *s; | |
135 | LttvTrace * trace; | |
f7afe191 | 136 | |
90e19f82 AM |
137 | s = g_new(LttvTraceset, 1); |
138 | s->filename = NULL; | |
139 | s->traces = g_ptr_array_new(); | |
140 | for(i=0;i<s_orig->traces->len;i++) | |
141 | { | |
142 | trace = g_ptr_array_index(s_orig->traces, i); | |
143 | trace->ref_count++; | |
2176f952 | 144 | |
90e19f82 AM |
145 | g_ptr_array_add(s->traces, trace); |
146 | } | |
cbb811b3 YB |
147 | s->context = s_orig->context; |
148 | bt_context_get(s->context); | |
90e19f82 AM |
149 | s->a = LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig->a))); |
150 | return s; | |
f7afe191 | 151 | } |
dc877563 | 152 | |
f7afe191 | 153 | |
154 | LttvTraceset *lttv_traceset_load(const gchar *filename) | |
155 | { | |
90e19f82 AM |
156 | LttvTraceset *s = g_new(LttvTraceset,1); |
157 | FILE *tf; | |
f7afe191 | 158 | |
90e19f82 AM |
159 | s->filename = g_strdup(filename); |
160 | tf = fopen(filename,"r"); | |
161 | ||
162 | g_critical("NOT IMPLEMENTED : load traceset data from a XML file"); | |
163 | ||
164 | fclose(tf); | |
165 | return s; | |
f7afe191 | 166 | } |
167 | ||
168 | gint lttv_traceset_save(LttvTraceset *s) | |
169 | { | |
90e19f82 | 170 | FILE *tf; |
f7afe191 | 171 | |
90e19f82 | 172 | tf = fopen(s->filename, "w"); |
f7afe191 | 173 | |
90e19f82 AM |
174 | g_critical("NOT IMPLEMENTED : save traceset data in a XML file"); |
175 | ||
176 | fclose(tf); | |
177 | return 0; | |
f7afe191 | 178 | } |
308711e5 | 179 | |
ba576a78 | 180 | void lttv_traceset_destroy(LttvTraceset *s) |
dc877563 | 181 | { |
90e19f82 | 182 | guint i; |
5e2c04a2 | 183 | |
90e19f82 AM |
184 | for(i=0;i<s->traces->len;i++) { |
185 | LttvTrace *trace = g_ptr_array_index(s->traces, i); | |
186 | lttv_trace_unref(trace); | |
2bc1bcfb | 187 | // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed |
188 | //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id); | |
90e19f82 AM |
189 | if(lttv_trace_get_ref_number(trace) == 0) |
190 | lttv_trace_destroy(trace); | |
191 | } | |
192 | g_ptr_array_free(s->traces, TRUE); | |
cbb811b3 | 193 | bt_context_put(s->context); |
90e19f82 AM |
194 | g_object_unref(s->a); |
195 | g_free(s); | |
dc877563 | 196 | } |
197 | ||
922581a4 YB |
198 | struct bt_context *lttv_traceset_get_context(LttvTraceset *s) |
199 | { | |
200 | return s->context; | |
201 | } | |
202 | ||
308711e5 | 203 | void lttv_trace_destroy(LttvTrace *t) |
204 | { | |
90e19f82 AM |
205 | g_object_unref(t->a); |
206 | g_free(t); | |
308711e5 | 207 | } |
208 | ||
209 | ||
210 | void lttv_traceset_add(LttvTraceset *s, LttvTrace *t) | |
dc877563 | 211 | { |
90e19f82 AM |
212 | t->ref_count++; |
213 | g_ptr_array_add(s->traces, t); | |
dc877563 | 214 | } |
215 | ||
2bc1bcfb | 216 | int lttv_traceset_add_path(LttvTraceset *ts, const char *trace_path) |
217 | { | |
218 | // todo mdenis 2012-03-27: add trace recursively and update comment | |
219 | int ret = lttv_traceset_create_trace(ts, trace_path); | |
220 | return ret; | |
221 | } | |
dc877563 | 222 | |
223 | unsigned lttv_traceset_number(LttvTraceset *s) | |
224 | { | |
90e19f82 | 225 | return s->traces->len; |
dc877563 | 226 | } |
227 | ||
228 | ||
308711e5 | 229 | LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i) |
dc877563 | 230 | { |
90e19f82 AM |
231 | g_assert(s->traces->len > i); |
232 | return ((LttvTrace *)s->traces->pdata[i]); | |
dc877563 | 233 | } |
234 | ||
235 | ||
ba576a78 | 236 | void lttv_traceset_remove(LttvTraceset *s, unsigned i) |
dc877563 | 237 | { |
90e19f82 AM |
238 | LttvTrace * t; |
239 | g_assert(s->traces->len > i); | |
240 | t = (LttvTrace *)s->traces->pdata[i]; | |
241 | t->ref_count--; | |
2bc1bcfb | 242 | bt_context_remove_trace(lttv_traceset_get_context(s), t->id); |
90e19f82 | 243 | g_ptr_array_remove_index(s->traces, i); |
dc877563 | 244 | } |
245 | ||
246 | ||
247 | /* A set of attributes is attached to each trace set, trace and tracefile | |
90e19f82 | 248 | to store user defined data as needed. */ |
dc877563 | 249 | |
250 | LttvAttribute *lttv_traceset_attribute(LttvTraceset *s) | |
251 | { | |
90e19f82 | 252 | return s->a; |
dc877563 | 253 | } |
254 | ||
255 | ||
308711e5 | 256 | LttvAttribute *lttv_trace_attribute(LttvTrace *t) |
257 | { | |
90e19f82 | 258 | return t->a; |
308711e5 | 259 | } |
260 | ||
2bc1bcfb | 261 | #ifdef BABEL_CLEANUP |
308711e5 | 262 | LttTrace *lttv_trace(LttvTrace *t) |
dc877563 | 263 | { |
90e19f82 | 264 | return t->t; |
dc877563 | 265 | } |
2bc1bcfb | 266 | #endif |
267 | ||
268 | gint lttv_trace_get_id(LttvTrace *t) | |
269 | { | |
270 | return t->id; | |
271 | } | |
308711e5 | 272 | |
2176f952 | 273 | guint lttv_trace_get_ref_number(LttvTrace * t) |
274 | { | |
2bc1bcfb | 275 | // todo mdenis: adapt to babeltrace |
90e19f82 | 276 | return t->ref_count; |
2176f952 | 277 | } |
a43d67ba | 278 | |
279 | guint lttv_trace_ref(LttvTrace * t) | |
280 | { | |
90e19f82 AM |
281 | t->ref_count++; |
282 | ||
283 | return t->ref_count; | |
a43d67ba | 284 | } |
285 | ||
286 | guint lttv_trace_unref(LttvTrace * t) | |
287 | { | |
90e19f82 AM |
288 | if(likely(t->ref_count > 0)) |
289 | t->ref_count--; | |
a43d67ba | 290 | |
90e19f82 | 291 | return t->ref_count; |
a43d67ba | 292 | } |
293 |