| 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 <lttv/state.h> |
| 26 | #include <lttv/hook.h> |
| 27 | #include <stdio.h> |
| 28 | #include <babeltrace/context.h> |
| 29 | #include <babeltrace/iterator.h> |
| 30 | #include <babeltrace/ctf/events.h> |
| 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 | |
| 37 | |
| 38 | LttvTraceset *lttv_traceset_new(void) |
| 39 | { |
| 40 | LttvTraceset *s; |
| 41 | struct bt_iter_pos begin_pos; |
| 42 | |
| 43 | s = g_new(LttvTraceset, 1); |
| 44 | s->filename = NULL; |
| 45 | s->traces = g_ptr_array_new(); |
| 46 | s->context = bt_context_create(); |
| 47 | s->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
| 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 | |
| 62 | return s; |
| 63 | } |
| 64 | |
| 65 | char * lttv_traceset_name(LttvTraceset * s) |
| 66 | { |
| 67 | return s->filename; |
| 68 | } |
| 69 | |
| 70 | #ifdef BABEL_CLEANUP |
| 71 | LttvTrace *lttv_trace_new(LttTrace *t) |
| 72 | { |
| 73 | LttvTrace *new_trace; |
| 74 | |
| 75 | new_trace = g_new(LttvTrace, 1); |
| 76 | new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); |
| 77 | new_trace->id = t; |
| 78 | new_trace->ref_count = 0; |
| 79 | return new_trace; |
| 80 | } |
| 81 | #endif |
| 82 | |
| 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 | */ |
| 95 | static 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; |
| 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; |
| 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 | */ |
| 129 | static 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 | } |
| 138 | |
| 139 | LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) |
| 140 | { |
| 141 | guint i; |
| 142 | LttvTraceset *s; |
| 143 | LttvTrace * trace; |
| 144 | |
| 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++; |
| 152 | |
| 153 | /* WARNING: this is an alias, not a copy. */ |
| 154 | g_ptr_array_add(s->traces, trace); |
| 155 | } |
| 156 | s->context = s_orig->context; |
| 157 | bt_context_get(s->context); |
| 158 | s->a = LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig->a))); |
| 159 | return s; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | LttvTraceset *lttv_traceset_load(const gchar *filename) |
| 164 | { |
| 165 | LttvTraceset *s = g_new(LttvTraceset,1); |
| 166 | FILE *tf; |
| 167 | |
| 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; |
| 175 | } |
| 176 | |
| 177 | gint lttv_traceset_save(LttvTraceset *s) |
| 178 | { |
| 179 | FILE *tf; |
| 180 | |
| 181 | tf = fopen(s->filename, "w"); |
| 182 | |
| 183 | g_critical("NOT IMPLEMENTED : save traceset data in a XML file"); |
| 184 | |
| 185 | fclose(tf); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | void lttv_traceset_destroy(LttvTraceset *s) |
| 190 | { |
| 191 | guint i; |
| 192 | |
| 193 | for(i=0;i<s->traces->len;i++) { |
| 194 | LttvTrace *trace = g_ptr_array_index(s->traces, i); |
| 195 | lttv_trace_unref(trace); |
| 196 | // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed |
| 197 | //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id); |
| 198 | if(lttv_trace_get_ref_number(trace) == 0) |
| 199 | lttv_trace_destroy(trace); |
| 200 | } |
| 201 | g_ptr_array_free(s->traces, TRUE); |
| 202 | bt_context_put(s->context); |
| 203 | g_object_unref(s->a); |
| 204 | g_free(s); |
| 205 | } |
| 206 | |
| 207 | struct bt_context *lttv_traceset_get_context(LttvTraceset *s) |
| 208 | { |
| 209 | return s->context; |
| 210 | } |
| 211 | |
| 212 | LttvTraceset *lttv_trace_get_traceset(LttvTrace *trace) |
| 213 | { |
| 214 | return trace->traceset; |
| 215 | } |
| 216 | |
| 217 | LttvHooks *lttv_traceset_get_hooks(LttvTraceset *s) |
| 218 | { |
| 219 | return s->event_hooks; |
| 220 | } |
| 221 | |
| 222 | void lttv_trace_destroy(LttvTrace *t) |
| 223 | { |
| 224 | g_object_unref(t->a); |
| 225 | g_free(t); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void lttv_traceset_add(LttvTraceset *s, LttvTrace *t) |
| 230 | { |
| 231 | t->ref_count++; |
| 232 | g_ptr_array_add(s->traces, t); |
| 233 | } |
| 234 | |
| 235 | int 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 | } |
| 241 | |
| 242 | unsigned lttv_traceset_number(LttvTraceset *s) |
| 243 | { |
| 244 | return s->traces->len; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i) |
| 249 | { |
| 250 | g_assert(s->traces->len > i); |
| 251 | return ((LttvTrace *)s->traces->pdata[i]); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void lttv_traceset_remove(LttvTraceset *s, unsigned i) |
| 256 | { |
| 257 | LttvTrace * t; |
| 258 | g_assert(s->traces->len > i); |
| 259 | t = (LttvTrace *)s->traces->pdata[i]; |
| 260 | t->ref_count--; |
| 261 | bt_context_remove_trace(lttv_traceset_get_context(s), t->id); |
| 262 | g_ptr_array_remove_index(s->traces, i); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /* A set of attributes is attached to each trace set, trace and tracefile |
| 267 | to store user defined data as needed. */ |
| 268 | |
| 269 | LttvAttribute *lttv_traceset_attribute(LttvTraceset *s) |
| 270 | { |
| 271 | return s->a; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | LttvAttribute *lttv_trace_attribute(LttvTrace *t) |
| 276 | { |
| 277 | return t->a; |
| 278 | } |
| 279 | |
| 280 | #ifdef BABEL_CLEANUP |
| 281 | LttTrace *lttv_trace(LttvTrace *t) |
| 282 | { |
| 283 | return t->t; |
| 284 | } |
| 285 | #endif |
| 286 | |
| 287 | gint lttv_trace_get_id(LttvTrace *t) |
| 288 | { |
| 289 | return t->id; |
| 290 | } |
| 291 | |
| 292 | guint lttv_trace_get_ref_number(LttvTrace * t) |
| 293 | { |
| 294 | // todo mdenis: adapt to babeltrace |
| 295 | return t->ref_count; |
| 296 | } |
| 297 | |
| 298 | guint lttv_trace_ref(LttvTrace * t) |
| 299 | { |
| 300 | t->ref_count++; |
| 301 | |
| 302 | return t->ref_count; |
| 303 | } |
| 304 | |
| 305 | guint lttv_trace_unref(LttvTrace * t) |
| 306 | { |
| 307 | if(likely(t->ref_count > 0)) |
| 308 | t->ref_count--; |
| 309 | |
| 310 | return t->ref_count; |
| 311 | } |
| 312 | |
| 313 | guint lttv_trace_get_num_cpu(LttvTrace *t) |
| 314 | { |
| 315 | #warning "TODO - Set the right number of CPU" |
| 316 | return 24; |
| 317 | } |
| 318 | |
| 319 | LttvTracesetPosition *lttv_traceset_create_position(LttvTraceset *traceset) |
| 320 | { |
| 321 | #warning "TODO" |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos) |
| 326 | { |
| 327 | #warning "TODO" |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | void lttv_traceset_seek_to_position(LttvTracesetPosition *traceset_pos) |
| 332 | { |
| 333 | #warning "TODO" |
| 334 | } |
| 335 | |
| 336 | guint 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 | |
| 359 | const char *lttv_traceset_get_name_from_event(LttvEvent *event) |
| 360 | { |
| 361 | return bt_ctf_event_name(event->bt_event); |
| 362 | } |