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> |
7a4bdb54 | 25 | #include <lttv/state.h> |
9a366873 | 26 | #include <lttv/event.h> |
7a4bdb54 | 27 | #include <lttv/hook.h> |
f7afe191 | 28 | #include <stdio.h> |
451aaf27 | 29 | #include <babeltrace/babeltrace.h> |
cbb811b3 | 30 | #include <babeltrace/context.h> |
451aaf27 | 31 | #include <babeltrace/ctf/iterator.h> |
7a4bdb54 | 32 | #include <babeltrace/ctf/events.h> |
3685e022 | 33 | |
34 | /* To traverse a tree recursively */ | |
35 | #include <fcntl.h> | |
451aaf27 FD |
36 | /* For the use of realpath*/ |
37 | #include <limits.h> | |
38 | #include <stdlib.h> | |
39 | /* For strcpy*/ | |
40 | #include <string.h> | |
9b55aba0 YB |
41 | #include <errno.h> |
42 | #include <dirent.h> | |
dc877563 | 43 | /* A trace is a sequence of events gathered in the same tracing session. The |
44 | events may be stored in several tracefiles in the same directory. | |
45 | A trace set is defined when several traces are to be analyzed together, | |
46 | possibly to study the interactions between events in the different traces. | |
47 | */ | |
48 | ||
dc877563 | 49 | |
7a4bdb54 | 50 | LttvTraceset *lttv_traceset_new(void) |
dc877563 | 51 | { |
9a366873 FD |
52 | LttvTraceset *ts; |
53 | struct bt_iter_pos begin_pos; | |
dc877563 | 54 | |
9a366873 FD |
55 | ts = g_new(LttvTraceset, 1); |
56 | ts->filename = NULL; | |
57 | ts->traces = g_ptr_array_new(); | |
58 | ts->context = bt_context_create(); | |
59 | ts->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); | |
7a4bdb54 YB |
60 | //TODO remove this when we have really mecanism |
61 | //s->tmpState = g_new(LttvTraceState *, 1); | |
62 | //lttv_trace_state_init(s->tmpState,0); | |
7a4bdb54 | 63 | |
9a366873 FD |
64 | /*Initialize iterator to the beginning of the traces*/ |
65 | begin_pos.type = BT_SEEK_BEGIN; | |
66 | ts->iter = bt_ctf_iter_create(ts->context, &begin_pos, NULL); | |
7a4bdb54 | 67 | |
9a366873 FD |
68 | ts->event_hooks = lttv_hooks_new(); |
69 | ||
70 | ts->state_trace_handle_index = g_ptr_array_new(); | |
58b4e4ae | 71 | ts->has_precomputed_states = FALSE; |
9a366873 | 72 | |
eda74fe8 FD |
73 | ts->time_span.start_time = ltt_time_zero; |
74 | ts->time_span.end_time = ltt_time_zero; | |
762e15b0 | 75 | lttv_traceset_get_time_span_real(ts); |
9a366873 | 76 | return ts; |
dc877563 | 77 | } |
78 | ||
49bf71b5 | 79 | char * lttv_traceset_name(LttvTraceset * s) |
80 | { | |
90e19f82 | 81 | return s->filename; |
49bf71b5 | 82 | } |
83 | ||
2bc1bcfb | 84 | #ifdef BABEL_CLEANUP |
85 | LttvTrace *lttv_trace_new(LttTrace *t) | |
308711e5 | 86 | { |
90e19f82 | 87 | LttvTrace *new_trace; |
308711e5 | 88 | |
90e19f82 AM |
89 | new_trace = g_new(LttvTrace, 1); |
90 | new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); | |
2bc1bcfb | 91 | new_trace->id = t; |
90e19f82 AM |
92 | new_trace->ref_count = 0; |
93 | return new_trace; | |
308711e5 | 94 | } |
2bc1bcfb | 95 | #endif |
451aaf27 FD |
96 | /* |
97 | * get_absolute_pathname : Return the unique pathname in the system | |
98 | * | |
99 | * pathname is the relative path. | |
100 | * | |
101 | * abs_pathname is being set to the absolute path. | |
102 | * | |
103 | */ | |
104 | void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname) | |
105 | { | |
106 | abs_pathname[0] = '\0'; | |
107 | ||
108 | if (realpath(pathname, abs_pathname) != NULL) | |
109 | return; | |
110 | else | |
111 | { | |
112 | /* error, return the original path unmodified */ | |
113 | strcpy(abs_pathname, pathname); | |
114 | return; | |
115 | } | |
116 | return; | |
117 | } | |
118 | ||
119 | ||
308711e5 | 120 | |
2bc1bcfb | 121 | /* |
122 | * lttv_trace_create : Create a trace from a path | |
123 | * | |
124 | * ts is the traceset in which will be contained the trace | |
125 | * | |
126 | * path is the path where to find a trace. It is not recursive. | |
127 | * | |
128 | * This function is static since a trace should always be contained in a | |
129 | * traceset. | |
130 | * | |
131 | * return the created trace or NULL on failure | |
132 | */ | |
133 | static LttvTrace *lttv_trace_create(LttvTraceset *ts, const char *path) | |
134 | { | |
9a366873 FD |
135 | int id = bt_context_add_trace(lttv_traceset_get_context(ts), |
136 | path, | |
137 | "ctf", | |
138 | NULL, | |
139 | NULL, | |
140 | NULL); | |
141 | if (id < 0) { | |
142 | return NULL; | |
143 | } | |
144 | // Create the trace and save the trace handle id returned by babeltrace | |
145 | LttvTrace *new_trace; | |
2bc1bcfb | 146 | |
9a366873 FD |
147 | new_trace = g_new(LttvTrace, 1); |
148 | new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); | |
149 | new_trace->id = id; | |
150 | new_trace->ref_count = 0; | |
151 | new_trace->traceset = ts; | |
152 | new_trace->state = g_new(LttvTraceState,1); | |
153 | lttv_trace_state_init(new_trace->state,new_trace); | |
115c78c2 | 154 | |
9a366873 FD |
155 | /* Add the state to the trace_handle to state index */ |
156 | g_ptr_array_set_size(ts->state_trace_handle_index,id+1); | |
157 | g_ptr_array_index(ts->state_trace_handle_index,id) = new_trace->state; | |
115c78c2 | 158 | |
9a366873 | 159 | return new_trace; |
2bc1bcfb | 160 | } |
161 | ||
162 | /* | |
163 | * lttv_trace_create : Create and add a single trace to a traceset | |
164 | * | |
165 | * ts is the traceset in which will be contained the trace | |
166 | * | |
167 | * path is the path where to find a trace. It is not recursive. | |
168 | * | |
169 | * return a positive integer (>=0)on success or -1 on failure | |
170 | */ | |
171 | static int lttv_traceset_create_trace(LttvTraceset *ts, const char *path) | |
172 | { | |
9a366873 FD |
173 | LttvTrace *trace = lttv_trace_create(ts, path); |
174 | if (trace == NULL) { | |
175 | return -1; | |
176 | } | |
177 | lttv_traceset_add(ts, trace); | |
178 | return 0; | |
2bc1bcfb | 179 | } |
308711e5 | 180 | |
f7afe191 | 181 | LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) |
182 | { | |
90e19f82 AM |
183 | guint i; |
184 | LttvTraceset *s; | |
185 | LttvTrace * trace; | |
f7afe191 | 186 | |
90e19f82 AM |
187 | s = g_new(LttvTraceset, 1); |
188 | s->filename = NULL; | |
189 | s->traces = g_ptr_array_new(); | |
115c78c2 | 190 | s->state_trace_handle_index = g_ptr_array_new(); |
90e19f82 AM |
191 | for(i=0;i<s_orig->traces->len;i++) |
192 | { | |
193 | trace = g_ptr_array_index(s_orig->traces, i); | |
194 | trace->ref_count++; | |
2176f952 | 195 | |
7a4bdb54 | 196 | /* WARNING: this is an alias, not a copy. */ |
90e19f82 | 197 | g_ptr_array_add(s->traces, trace); |
115c78c2 YB |
198 | |
199 | g_ptr_array_set_size(s->state_trace_handle_index,trace->id+1); | |
200 | g_ptr_array_index(s->state_trace_handle_index,trace->id) = trace->state; | |
201 | ||
90e19f82 | 202 | } |
cbb811b3 YB |
203 | s->context = s_orig->context; |
204 | bt_context_get(s->context); | |
90e19f82 AM |
205 | s->a = LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig->a))); |
206 | return s; | |
f7afe191 | 207 | } |
dc877563 | 208 | |
f7afe191 | 209 | |
210 | LttvTraceset *lttv_traceset_load(const gchar *filename) | |
211 | { | |
90e19f82 AM |
212 | LttvTraceset *s = g_new(LttvTraceset,1); |
213 | FILE *tf; | |
f7afe191 | 214 | |
90e19f82 AM |
215 | s->filename = g_strdup(filename); |
216 | tf = fopen(filename,"r"); | |
217 | ||
218 | g_critical("NOT IMPLEMENTED : load traceset data from a XML file"); | |
219 | ||
220 | fclose(tf); | |
221 | return s; | |
f7afe191 | 222 | } |
223 | ||
224 | gint lttv_traceset_save(LttvTraceset *s) | |
225 | { | |
90e19f82 | 226 | FILE *tf; |
f7afe191 | 227 | |
90e19f82 | 228 | tf = fopen(s->filename, "w"); |
f7afe191 | 229 | |
90e19f82 AM |
230 | g_critical("NOT IMPLEMENTED : save traceset data in a XML file"); |
231 | ||
232 | fclose(tf); | |
233 | return 0; | |
f7afe191 | 234 | } |
308711e5 | 235 | |
ba576a78 | 236 | void lttv_traceset_destroy(LttvTraceset *s) |
dc877563 | 237 | { |
90e19f82 | 238 | guint i; |
5e2c04a2 | 239 | |
90e19f82 AM |
240 | for(i=0;i<s->traces->len;i++) { |
241 | LttvTrace *trace = g_ptr_array_index(s->traces, i); | |
242 | lttv_trace_unref(trace); | |
2bc1bcfb | 243 | // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed |
244 | //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id); | |
90e19f82 AM |
245 | if(lttv_trace_get_ref_number(trace) == 0) |
246 | lttv_trace_destroy(trace); | |
247 | } | |
248 | g_ptr_array_free(s->traces, TRUE); | |
cbb811b3 | 249 | bt_context_put(s->context); |
90e19f82 AM |
250 | g_object_unref(s->a); |
251 | g_free(s); | |
dc877563 | 252 | } |
253 | ||
922581a4 YB |
254 | struct bt_context *lttv_traceset_get_context(LttvTraceset *s) |
255 | { | |
256 | return s->context; | |
257 | } | |
258 | ||
7a4bdb54 YB |
259 | LttvTraceset *lttv_trace_get_traceset(LttvTrace *trace) |
260 | { | |
261 | return trace->traceset; | |
262 | } | |
263 | ||
264 | LttvHooks *lttv_traceset_get_hooks(LttvTraceset *s) | |
265 | { | |
266 | return s->event_hooks; | |
267 | } | |
268 | ||
308711e5 | 269 | void lttv_trace_destroy(LttvTrace *t) |
270 | { | |
90e19f82 AM |
271 | g_object_unref(t->a); |
272 | g_free(t); | |
308711e5 | 273 | } |
274 | ||
308711e5 | 275 | void lttv_traceset_add(LttvTraceset *s, LttvTrace *t) |
dc877563 | 276 | { |
90e19f82 AM |
277 | t->ref_count++; |
278 | g_ptr_array_add(s->traces, t); | |
dc877563 | 279 | } |
280 | ||
3685e022 | 281 | int lttv_traceset_add_path(LttvTraceset *ts, char *trace_path) |
2bc1bcfb | 282 | { |
3685e022 | 283 | int ret = -1; |
9b55aba0 | 284 | DIR *curdir = NULL; |
861fbe5f | 285 | gboolean metaFileFound = FALSE; |
9b55aba0 YB |
286 | |
287 | /* Open top level directory */ | |
288 | curdir = opendir(trace_path); | |
289 | if (curdir == NULL) { | |
290 | g_warning("Cannot open directory %s (%s)", trace_path, strerror(errno)); | |
3685e022 | 291 | return ret; |
292 | } | |
293 | ||
9b55aba0 YB |
294 | // Check if a metadata file exists in the current directory |
295 | int metafd = openat(dirfd(curdir), "metadata", O_RDONLY); | |
296 | if (metafd < 0) { | |
297 | ||
298 | } else { | |
299 | ret = close(metafd); | |
300 | if (ret < 0) { | |
301 | g_warning("Unable to close metadata " | |
302 | "file descriptor : %s.", trace_path); | |
3685e022 | 303 | goto error; |
304 | } | |
9b55aba0 YB |
305 | |
306 | ret = lttv_traceset_create_trace(ts, trace_path); | |
307 | if (ret < 0) { | |
308 | g_warning("Opening trace \"%s\" " | |
309 | "for reading.", trace_path); | |
310 | goto error; | |
311 | } | |
312 | metaFileFound = TRUE; | |
313 | } | |
3685e022 | 314 | |
9b55aba0 YB |
315 | struct dirent curentry; |
316 | struct dirent *resultentry; | |
317 | while ((ret = readdir_r(curdir, &curentry, &resultentry)) == 0) { | |
318 | if (resultentry == NULL) { | |
319 | /* No more entry*/ | |
320 | break; | |
321 | } | |
322 | if (curentry.d_name[0] != '.') { | |
323 | if (curentry.d_type == DT_DIR) { | |
324 | ||
325 | char curpath[PATH_MAX]; | |
326 | snprintf(curpath, PATH_MAX, "%s/%s", trace_path, curentry.d_name); | |
327 | ret = lttv_traceset_add_path(ts, curpath); | |
328 | if (ret >= 0) { | |
329 | metaFileFound = TRUE; | |
330 | } | |
3685e022 | 331 | } |
332 | } | |
333 | } | |
334 | ||
9b55aba0 YB |
335 | if (ret != 0) { |
336 | g_warning("Invalid readdir"); | |
337 | } | |
338 | ||
3685e022 | 339 | error: |
861fbe5f FD |
340 | if(metaFileFound) |
341 | return ret; | |
342 | else | |
343 | return -1; | |
2bc1bcfb | 344 | } |
dc877563 | 345 | |
346 | unsigned lttv_traceset_number(LttvTraceset *s) | |
347 | { | |
90e19f82 | 348 | return s->traces->len; |
dc877563 | 349 | } |
350 | ||
351 | ||
308711e5 | 352 | LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i) |
dc877563 | 353 | { |
90e19f82 AM |
354 | g_assert(s->traces->len > i); |
355 | return ((LttvTrace *)s->traces->pdata[i]); | |
dc877563 | 356 | } |
357 | ||
358 | ||
ba576a78 | 359 | void lttv_traceset_remove(LttvTraceset *s, unsigned i) |
dc877563 | 360 | { |
90e19f82 AM |
361 | LttvTrace * t; |
362 | g_assert(s->traces->len > i); | |
363 | t = (LttvTrace *)s->traces->pdata[i]; | |
364 | t->ref_count--; | |
2bc1bcfb | 365 | bt_context_remove_trace(lttv_traceset_get_context(s), t->id); |
90e19f82 | 366 | g_ptr_array_remove_index(s->traces, i); |
dc877563 | 367 | } |
368 | ||
369 | ||
370 | /* A set of attributes is attached to each trace set, trace and tracefile | |
90e19f82 | 371 | to store user defined data as needed. */ |
dc877563 | 372 | |
373 | LttvAttribute *lttv_traceset_attribute(LttvTraceset *s) | |
374 | { | |
90e19f82 | 375 | return s->a; |
dc877563 | 376 | } |
377 | ||
378 | ||
308711e5 | 379 | LttvAttribute *lttv_trace_attribute(LttvTrace *t) |
380 | { | |
90e19f82 | 381 | return t->a; |
308711e5 | 382 | } |
383 | ||
2bc1bcfb | 384 | |
385 | gint lttv_trace_get_id(LttvTrace *t) | |
386 | { | |
9a366873 | 387 | return t->id; |
2bc1bcfb | 388 | } |
308711e5 | 389 | |
2176f952 | 390 | guint lttv_trace_get_ref_number(LttvTrace * t) |
391 | { | |
2bc1bcfb | 392 | // todo mdenis: adapt to babeltrace |
90e19f82 | 393 | return t->ref_count; |
2176f952 | 394 | } |
a43d67ba | 395 | |
396 | guint lttv_trace_ref(LttvTrace * t) | |
397 | { | |
90e19f82 AM |
398 | t->ref_count++; |
399 | ||
400 | return t->ref_count; | |
a43d67ba | 401 | } |
402 | ||
403 | guint lttv_trace_unref(LttvTrace * t) | |
404 | { | |
90e19f82 AM |
405 | if(likely(t->ref_count > 0)) |
406 | t->ref_count--; | |
a43d67ba | 407 | |
90e19f82 | 408 | return t->ref_count; |
a43d67ba | 409 | } |
410 | ||
7a4bdb54 YB |
411 | guint lttv_trace_get_num_cpu(LttvTrace *t) |
412 | { | |
413 | #warning "TODO - Set the right number of CPU" | |
414 | return 24; | |
415 | } | |
416 | ||
68573dd0 | 417 | LttvTracesetPosition *lttv_traceset_create_current_position(LttvTraceset *traceset) |
7a4bdb54 | 418 | { |
3d1e7ee5 YB |
419 | LttvTracesetPosition *traceset_pos; |
420 | ||
421 | traceset_pos = g_new(LttvTracesetPosition, 1); | |
422 | ||
9a366873 | 423 | /* Check if the new passed */ |
3d1e7ee5 YB |
424 | if(traceset_pos == NULL) { |
425 | return NULL; | |
426 | } | |
427 | ||
428 | traceset_pos->iter = traceset->iter; | |
429 | traceset_pos->bt_pos = bt_iter_get_pos(bt_ctf_get_iter(traceset->iter)); | |
c73ce169 FD |
430 | traceset_pos->timestamp = G_MAXUINT64; |
431 | traceset_pos->cpu_id = INT_MAX; | |
432 | ||
3d1e7ee5 | 433 | return traceset_pos; |
7a4bdb54 YB |
434 | } |
435 | ||
9a366873 FD |
436 | LttvTracesetPosition *lttv_traceset_create_time_position(LttvTraceset *traceset, |
437 | LttTime timestamp) | |
438 | { | |
439 | LttvTracesetPosition *traceset_pos; | |
440 | ||
441 | traceset_pos = g_new(LttvTracesetPosition, 1); | |
442 | ||
443 | /* Check if the new passed */ | |
444 | if(traceset_pos == NULL) { | |
445 | return NULL; | |
446 | } | |
447 | ||
448 | traceset_pos->iter = traceset->iter; | |
c73ce169 | 449 | traceset_pos->bt_pos = bt_iter_create_time_pos( |
9a366873 FD |
450 | bt_ctf_get_iter(traceset_pos->iter), |
451 | ltt_time_to_uint64(timestamp)); | |
c73ce169 FD |
452 | traceset_pos->timestamp = G_MAXUINT64; |
453 | traceset_pos->cpu_id = INT_MAX; | |
9a366873 FD |
454 | return traceset_pos; |
455 | } | |
456 | ||
7a4bdb54 YB |
457 | void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos) |
458 | { | |
3d1e7ee5 YB |
459 | bt_iter_free_pos(traceset_pos->bt_pos); |
460 | g_free(traceset_pos); | |
7a4bdb54 YB |
461 | } |
462 | ||
9a366873 | 463 | void lttv_traceset_seek_to_position(const LttvTracesetPosition *traceset_pos) |
7a4bdb54 | 464 | { |
9a366873 | 465 | bt_iter_set_pos(bt_ctf_get_iter(traceset_pos->iter), traceset_pos->bt_pos); |
7a4bdb54 YB |
466 | } |
467 | ||
468 | guint lttv_traceset_get_cpuid_from_event(LttvEvent *event) | |
469 | { | |
7a4bdb54 YB |
470 | unsigned long timestamp; |
471 | unsigned int cpu_id; | |
472 | ||
473 | struct bt_ctf_event *ctf_event = event->bt_event; | |
474 | timestamp = bt_ctf_get_timestamp(ctf_event); | |
475 | if (timestamp == -1ULL) { | |
476 | return 0; | |
477 | } | |
cfddb03e | 478 | const struct bt_definition *scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT); |
7a4bdb54 YB |
479 | if (bt_ctf_field_get_error()) { |
480 | return 0; | |
481 | } | |
482 | cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(ctf_event, scope, "cpu_id")); | |
483 | if (bt_ctf_field_get_error()) { | |
484 | return 0; | |
485 | } else { | |
486 | return cpu_id; | |
487 | } | |
488 | } | |
9a366873 FD |
489 | |
490 | guint64 lttv_traceset_get_timestamp_first_event(LttvTraceset *ts) | |
491 | { | |
492 | LttvTracesetPosition begin_position; | |
493 | struct bt_iter_pos pos; | |
494 | begin_position.bt_pos = &pos; | |
c73ce169 FD |
495 | begin_position.timestamp = G_MAXUINT64; |
496 | begin_position.cpu_id = INT_MAX; | |
9a366873 FD |
497 | |
498 | /* Assign iterator to the beginning of the traces */ | |
499 | begin_position.bt_pos->type = BT_SEEK_BEGIN; | |
500 | begin_position.iter = ts->iter; | |
501 | ||
502 | return lttv_traceset_position_get_timestamp(&begin_position); | |
503 | } | |
504 | ||
8924e3e4 FD |
505 | guint64 lttv_traceset_get_timestamp_last_event(LttvTraceset *ts) |
506 | { | |
507 | LttvTracesetPosition last_position; | |
508 | struct bt_iter_pos pos; | |
509 | last_position.bt_pos = &pos; | |
510 | last_position.timestamp = G_MAXUINT64; | |
511 | last_position.cpu_id = INT_MAX; | |
c0b8f3e8 | 512 | |
8924e3e4 FD |
513 | /* Assign iterator to the last event of the traces */ |
514 | last_position.bt_pos->type = BT_SEEK_LAST; | |
515 | last_position.iter = ts->iter; | |
c0b8f3e8 | 516 | |
8924e3e4 FD |
517 | return lttv_traceset_position_get_timestamp(&last_position); |
518 | } | |
519 | ||
451aaf27 FD |
520 | /* |
521 | * lttv_traceset_get_timestamp_begin : returns the minimum timestamp of | |
522 | * all the traces in the traceset. | |
523 | * | |
524 | */ | |
451aaf27 FD |
525 | guint64 lttv_traceset_get_timestamp_begin(LttvTraceset *traceset) |
526 | { | |
9a366873 FD |
527 | struct bt_context *bt_ctx; |
528 | bt_ctx = lttv_traceset_get_context(traceset); | |
529 | guint64 timestamp_min, timestamp_cur = 0; | |
530 | int i; | |
531 | int trace_count; | |
532 | LttvTrace *currentTrace; | |
533 | trace_count = traceset->traces->len; | |
534 | if(trace_count == 0) | |
535 | timestamp_min = 0; | |
536 | else{ | |
537 | timestamp_min = G_MAXUINT64; | |
538 | for(i = 0; i < trace_count;i++) | |
539 | { | |
540 | currentTrace = g_ptr_array_index(traceset->traces,i); | |
541 | timestamp_cur = bt_trace_handle_get_timestamp_begin(bt_ctx, | |
762e15b0 FD |
542 | currentTrace->id, |
543 | BT_CLOCK_REAL); | |
9a366873 FD |
544 | if(timestamp_cur < timestamp_min) |
545 | timestamp_min = timestamp_cur; | |
546 | } | |
547 | } | |
548 | return timestamp_min; | |
451aaf27 FD |
549 | } |
550 | ||
551 | /* | |
552 | * lttv_traceset_get_timestamp_end: returns the maximum timestamp of | |
553 | * all the traces in the traceset. | |
554 | * | |
555 | */ | |
556 | guint64 lttv_traceset_get_timestamp_end(LttvTraceset *traceset) | |
557 | { | |
9a366873 FD |
558 | struct bt_context *bt_ctx; |
559 | bt_ctx = lttv_traceset_get_context(traceset); | |
560 | guint64 timestamp_max, timestamp_cur = 0; | |
561 | int i; | |
562 | int trace_count; | |
563 | LttvTrace *currentTrace; | |
564 | trace_count = traceset->traces->len; | |
565 | ||
566 | if(trace_count == 0){ | |
567 | timestamp_max = 1; | |
568 | } | |
569 | else{ | |
570 | timestamp_max = 0; | |
571 | for(i =0; i < trace_count;i++) | |
572 | { | |
573 | currentTrace = g_ptr_array_index(traceset->traces,i); | |
574 | timestamp_cur = bt_trace_handle_get_timestamp_end(bt_ctx, | |
762e15b0 FD |
575 | currentTrace->id, |
576 | BT_CLOCK_REAL); | |
9a366873 FD |
577 | if(timestamp_cur > timestamp_max){ |
578 | timestamp_max = timestamp_cur; | |
579 | } | |
580 | } | |
581 | } | |
582 | return timestamp_max; | |
583 | } | |
584 | /* | |
585 | * lttv_traceset_get_time_span_real : return a TimeInterval representing the | |
8924e3e4 | 586 | * minimum timestamp and the maximum timestamp of the traceset. |
9a366873 FD |
587 | * |
588 | */ | |
589 | TimeInterval lttv_traceset_get_time_span_real(LttvTraceset *ts) | |
590 | { | |
b9410cf1 | 591 | |
979bddf1 | 592 | |
8924e3e4 | 593 | if(ltt_time_compare(ts->time_span.start_time, |
eda74fe8 | 594 | ltt_time_zero) == 0 && ts->traces->len > 0){ |
8924e3e4 FD |
595 | ts->time_span.start_time = ltt_time_from_uint64( |
596 | lttv_traceset_get_timestamp_first_event(ts)); | |
597 | ts->time_span.end_time = ltt_time_from_uint64( | |
c0b8f3e8 | 598 | lttv_traceset_get_timestamp_last_event(ts)); |
b9410cf1 YB |
599 | } |
600 | return ts->time_span; | |
9aaa78dc FD |
601 | } |
602 | ||
603 | /* | |
604 | * lttv_traceset_get_time_span : return a TimeInterval representing the | |
8924e3e4 | 605 | * minimum timestamp and the maximum timestamp of the traceset. |
9aaa78dc FD |
606 | * |
607 | */ | |
608 | TimeInterval lttv_traceset_get_time_span(LttvTraceset *ts) | |
609 | { | |
eda74fe8 | 610 | if(ltt_time_compare(ts->time_span.start_time, ltt_time_zero) == 0){ |
8924e3e4 FD |
611 | ts->time_span.start_time =ltt_time_from_uint64( |
612 | lttv_traceset_get_timestamp_begin(ts)); | |
613 | ts->time_span.end_time = ltt_time_from_uint64( | |
614 | lttv_traceset_get_timestamp_end(ts)); | |
615 | } | |
616 | return ts->time_span; | |
451aaf27 | 617 | } |
7a4bdb54 YB |
618 | |
619 | const char *lttv_traceset_get_name_from_event(LttvEvent *event) | |
620 | { | |
621 | return bt_ctf_event_name(event->bt_event); | |
622 | } | |
9a366873 | 623 | |
c73ce169 FD |
624 | int set_values_position(const LttvTracesetPosition *pos) |
625 | { | |
626 | LttvTracesetPosition previous_pos; | |
627 | previous_pos.iter = pos->iter; | |
628 | previous_pos.bt_pos = bt_iter_get_pos(bt_ctf_get_iter(pos->iter)); | |
9a366873 | 629 | /* Seek to the new desired position */ |
c73ce169 | 630 | lttv_traceset_seek_to_position(pos); |
9a366873 | 631 | /*Read the event*/ |
c73ce169 FD |
632 | struct bt_ctf_event *event = bt_ctf_iter_read_event(pos->iter); |
633 | ||
9a366873 | 634 | if(event != NULL){ |
762e15b0 | 635 | ((LttvTracesetPosition *)pos)->timestamp = bt_ctf_get_timestamp(event); |
c73ce169 FD |
636 | |
637 | LttvEvent lttv_event; | |
638 | lttv_event.bt_event = event; | |
639 | ((LttvTracesetPosition *)pos)->cpu_id = lttv_traceset_get_cpuid_from_event(<tv_event); | |
640 | } | |
641 | else { | |
642 | /* The event is null */ | |
643 | return 0; | |
9a366873 | 644 | } |
c73ce169 FD |
645 | |
646 | /* Reassign the previously saved position */ | |
647 | lttv_traceset_seek_to_position(&previous_pos); | |
648 | /*We must desallocate because the function bt_iter_get_pos() does a g_new */ | |
649 | bt_iter_free_pos(previous_pos.bt_pos); | |
3200b7f3 YB |
650 | if (pos->timestamp == G_MAXUINT64) { |
651 | return 0; | |
652 | } | |
762e15b0 | 653 | return 1; |
c73ce169 FD |
654 | } |
655 | ||
656 | guint64 lttv_traceset_position_get_timestamp(const LttvTracesetPosition *pos) | |
657 | { | |
658 | if(pos->timestamp == G_MAXUINT64){ | |
659 | if(set_values_position(pos) == 0){ | |
660 | return 0; | |
661 | } | |
662 | } | |
663 | ||
664 | return pos->timestamp; | |
665 | } | |
666 | ||
667 | int lttv_traceset_position_get_cpuid(const LttvTracesetPosition *pos){ | |
668 | if(pos->cpu_id == INT_MAX ){ | |
669 | if(set_values_position(pos) == 0){ | |
670 | return 0; | |
671 | } | |
672 | } | |
673 | return pos->cpu_id; | |
9a366873 FD |
674 | } |
675 | ||
676 | LttTime lttv_traceset_position_get_time(const LttvTracesetPosition *pos) | |
677 | { | |
678 | return ltt_time_from_uint64(lttv_traceset_position_get_timestamp(pos)); | |
679 | } | |
680 | ||
ff5c41f1 | 681 | |
3200b7f3 | 682 | /* 0 if equals, other is different */ |
9a366873 FD |
683 | int lttv_traceset_position_compare(const LttvTracesetPosition *pos1, const LttvTracesetPosition *pos2) |
684 | { | |
685 | #warning " TODO :Rename for lttv_traceset_position_equals && Must return COMPARAISON OF THE 2 POSITION && verify if it is the best way to compare position" | |
686 | if(pos1 == NULL || pos2 == NULL){ | |
687 | return -1; | |
688 | } | |
3200b7f3 | 689 | |
cfddb03e YB |
690 | int res = -1; |
691 | #ifdef HAVE_BT_ITER_EQUALS_POS | |
692 | if(pos1->timestamp == G_MAXUINT64 || pos2->timestamp == G_MAXUINT64) { | |
693 | res = bt_iter_equals_pos(pos1->bt_pos, pos2->bt_pos); | |
694 | } | |
695 | #endif | |
3200b7f3 YB |
696 | if (res < 0) { |
697 | ||
698 | guint64 timeStampPos1,timeStampPos2; | |
699 | guint cpuId1, cpuId2; | |
700 | ||
701 | timeStampPos1 = lttv_traceset_position_get_timestamp(pos1); | |
702 | timeStampPos2 = lttv_traceset_position_get_timestamp(pos2); | |
703 | ||
cfddb03e YB |
704 | if (timeStampPos1 == timeStampPos2) { |
705 | ||
706 | cpuId1 = lttv_traceset_position_get_cpuid(pos1); | |
707 | cpuId2 = lttv_traceset_position_get_cpuid(pos2); | |
3200b7f3 | 708 | |
cfddb03e YB |
709 | if(cpuId1 == cpuId2){ |
710 | return 0; | |
711 | } | |
3200b7f3 | 712 | } |
cfddb03e | 713 | return 1; |
3200b7f3 YB |
714 | } else { |
715 | ||
716 | return !res; | |
9a366873 FD |
717 | } |
718 | } | |
ff5c41f1 YB |
719 | |
720 | int lttv_traceset_position_time_compare(const LttvTracesetPosition *pos1, | |
721 | const LttvTracesetPosition *pos2) | |
722 | { | |
723 | guint64 timeStampPos1,timeStampPos2; | |
724 | ||
725 | timeStampPos1 = lttv_traceset_position_get_timestamp(pos1); | |
726 | timeStampPos2 = lttv_traceset_position_get_timestamp(pos2); | |
727 | ||
728 | return timeStampPos1 - timeStampPos2; | |
729 | } | |
730 | int lttv_traceset_position_compare_current(const LttvTraceset *ts, | |
731 | const LttvTracesetPosition *pos) | |
732 | { | |
733 | int result = 0; | |
734 | LttvTracesetPosition *curPos = lttv_traceset_create_current_position(ts); | |
735 | ||
736 | result = lttv_traceset_position_compare(curPos,pos); | |
737 | ||
738 | lttv_traceset_destroy_position(curPos); | |
739 | ||
740 | return result; | |
741 | } | |
742 | ||
743 | LttTime lttv_traceset_get_current_time(const LttvTraceset *ts) | |
744 | { | |
745 | LttvTracesetPosition *curPos = lttv_traceset_create_current_position(ts); | |
746 | guint64 currentTimestamp = lttv_traceset_position_get_timestamp(curPos); | |
747 | lttv_traceset_destroy_position(curPos); | |
748 | ||
749 | return ltt_time_from_uint64(currentTimestamp); | |
750 | } |