| 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 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <lttv/traceset.h> |
| 24 | #include <lttv/iattribute.h> |
| 25 | #include <stdio.h> |
| 26 | #include <babeltrace/context.h> |
| 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 { |
| 35 | char * filename; |
| 36 | GPtrArray *traces; |
| 37 | struct bt_context *context; |
| 38 | LttvAttribute *a; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | struct _LttvTrace { |
| 43 | // Trace id for babeltrace |
| 44 | gint id; |
| 45 | LttvAttribute *a; |
| 46 | guint ref_count; |
| 47 | }; |
| 48 | |
| 49 | LttvTraceset *lttv_traceset_new() |
| 50 | { |
| 51 | LttvTraceset *s; |
| 52 | |
| 53 | s = g_new(LttvTraceset, 1); |
| 54 | s->filename = NULL; |
| 55 | s->traces = g_ptr_array_new(); |
| 56 | s->context = bt_context_create(); |
| 57 | s->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
| 58 | return s; |
| 59 | } |
| 60 | |
| 61 | char * lttv_traceset_name(LttvTraceset * s) |
| 62 | { |
| 63 | return s->filename; |
| 64 | } |
| 65 | |
| 66 | #ifdef BABEL_CLEANUP |
| 67 | LttvTrace *lttv_trace_new(LttTrace *t) |
| 68 | { |
| 69 | LttvTrace *new_trace; |
| 70 | |
| 71 | new_trace = g_new(LttvTrace, 1); |
| 72 | new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
| 73 | new_trace->id = t; |
| 74 | new_trace->ref_count = 0; |
| 75 | return new_trace; |
| 76 | } |
| 77 | #endif |
| 78 | |
| 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 | } |
| 130 | |
| 131 | LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) |
| 132 | { |
| 133 | guint i; |
| 134 | LttvTraceset *s; |
| 135 | LttvTrace * trace; |
| 136 | |
| 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++; |
| 144 | |
| 145 | g_ptr_array_add(s->traces, trace); |
| 146 | } |
| 147 | s->context = s_orig->context; |
| 148 | bt_context_get(s->context); |
| 149 | s->a = LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig->a))); |
| 150 | return s; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | LttvTraceset *lttv_traceset_load(const gchar *filename) |
| 155 | { |
| 156 | LttvTraceset *s = g_new(LttvTraceset,1); |
| 157 | FILE *tf; |
| 158 | |
| 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; |
| 166 | } |
| 167 | |
| 168 | gint lttv_traceset_save(LttvTraceset *s) |
| 169 | { |
| 170 | FILE *tf; |
| 171 | |
| 172 | tf = fopen(s->filename, "w"); |
| 173 | |
| 174 | g_critical("NOT IMPLEMENTED : save traceset data in a XML file"); |
| 175 | |
| 176 | fclose(tf); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | void lttv_traceset_destroy(LttvTraceset *s) |
| 181 | { |
| 182 | guint i; |
| 183 | |
| 184 | for(i=0;i<s->traces->len;i++) { |
| 185 | LttvTrace *trace = g_ptr_array_index(s->traces, i); |
| 186 | lttv_trace_unref(trace); |
| 187 | // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed |
| 188 | //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id); |
| 189 | if(lttv_trace_get_ref_number(trace) == 0) |
| 190 | lttv_trace_destroy(trace); |
| 191 | } |
| 192 | g_ptr_array_free(s->traces, TRUE); |
| 193 | bt_context_put(s->context); |
| 194 | g_object_unref(s->a); |
| 195 | g_free(s); |
| 196 | } |
| 197 | |
| 198 | struct bt_context *lttv_traceset_get_context(LttvTraceset *s) |
| 199 | { |
| 200 | return s->context; |
| 201 | } |
| 202 | |
| 203 | void lttv_trace_destroy(LttvTrace *t) |
| 204 | { |
| 205 | g_object_unref(t->a); |
| 206 | g_free(t); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void lttv_traceset_add(LttvTraceset *s, LttvTrace *t) |
| 211 | { |
| 212 | t->ref_count++; |
| 213 | g_ptr_array_add(s->traces, t); |
| 214 | } |
| 215 | |
| 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 | } |
| 222 | |
| 223 | unsigned lttv_traceset_number(LttvTraceset *s) |
| 224 | { |
| 225 | return s->traces->len; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i) |
| 230 | { |
| 231 | g_assert(s->traces->len > i); |
| 232 | return ((LttvTrace *)s->traces->pdata[i]); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | void lttv_traceset_remove(LttvTraceset *s, unsigned i) |
| 237 | { |
| 238 | LttvTrace * t; |
| 239 | g_assert(s->traces->len > i); |
| 240 | t = (LttvTrace *)s->traces->pdata[i]; |
| 241 | t->ref_count--; |
| 242 | bt_context_remove_trace(lttv_traceset_get_context(s), t->id); |
| 243 | g_ptr_array_remove_index(s->traces, i); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* A set of attributes is attached to each trace set, trace and tracefile |
| 248 | to store user defined data as needed. */ |
| 249 | |
| 250 | LttvAttribute *lttv_traceset_attribute(LttvTraceset *s) |
| 251 | { |
| 252 | return s->a; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | LttvAttribute *lttv_trace_attribute(LttvTrace *t) |
| 257 | { |
| 258 | return t->a; |
| 259 | } |
| 260 | |
| 261 | #ifdef BABEL_CLEANUP |
| 262 | LttTrace *lttv_trace(LttvTrace *t) |
| 263 | { |
| 264 | return t->t; |
| 265 | } |
| 266 | #endif |
| 267 | |
| 268 | gint lttv_trace_get_id(LttvTrace *t) |
| 269 | { |
| 270 | return t->id; |
| 271 | } |
| 272 | |
| 273 | guint lttv_trace_get_ref_number(LttvTrace * t) |
| 274 | { |
| 275 | // todo mdenis: adapt to babeltrace |
| 276 | return t->ref_count; |
| 277 | } |
| 278 | |
| 279 | guint lttv_trace_ref(LttvTrace * t) |
| 280 | { |
| 281 | t->ref_count++; |
| 282 | |
| 283 | return t->ref_count; |
| 284 | } |
| 285 | |
| 286 | guint lttv_trace_unref(LttvTrace * t) |
| 287 | { |
| 288 | if(likely(t->ref_count > 0)) |
| 289 | t->ref_count--; |
| 290 | |
| 291 | return t->ref_count; |
| 292 | } |
| 293 | |