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