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 | 60 | |
9a366873 FD |
61 | /*Initialize iterator to the beginning of the traces*/ |
62 | begin_pos.type = BT_SEEK_BEGIN; | |
63 | ts->iter = bt_ctf_iter_create(ts->context, &begin_pos, NULL); | |
7a4bdb54 | 64 | |
9a366873 FD |
65 | ts->event_hooks = lttv_hooks_new(); |
66 | ||
67 | ts->state_trace_handle_index = g_ptr_array_new(); | |
58b4e4ae | 68 | ts->has_precomputed_states = FALSE; |
9a366873 | 69 | |
eda74fe8 FD |
70 | ts->time_span.start_time = ltt_time_zero; |
71 | ts->time_span.end_time = ltt_time_zero; | |
762e15b0 | 72 | lttv_traceset_get_time_span_real(ts); |
9a366873 | 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 | int ret = -1; |
9b55aba0 | 281 | DIR *curdir = NULL; |
861fbe5f | 282 | gboolean metaFileFound = FALSE; |
9b55aba0 YB |
283 | |
284 | /* Open top level directory */ | |
285 | curdir = opendir(trace_path); | |
286 | if (curdir == NULL) { | |
287 | g_warning("Cannot open directory %s (%s)", trace_path, strerror(errno)); | |
3685e022 | 288 | return ret; |
289 | } | |
290 | ||
9b55aba0 YB |
291 | // Check if a metadata file exists in the current directory |
292 | int metafd = openat(dirfd(curdir), "metadata", O_RDONLY); | |
293 | if (metafd < 0) { | |
294 | ||
295 | } else { | |
296 | ret = close(metafd); | |
297 | if (ret < 0) { | |
298 | g_warning("Unable to close metadata " | |
299 | "file descriptor : %s.", trace_path); | |
3685e022 | 300 | goto error; |
301 | } | |
9b55aba0 YB |
302 | |
303 | ret = lttv_traceset_create_trace(ts, trace_path); | |
304 | if (ret < 0) { | |
305 | g_warning("Opening trace \"%s\" " | |
306 | "for reading.", trace_path); | |
307 | goto error; | |
308 | } | |
309 | metaFileFound = TRUE; | |
310 | } | |
3685e022 | 311 | |
9b55aba0 YB |
312 | struct dirent curentry; |
313 | struct dirent *resultentry; | |
314 | while ((ret = readdir_r(curdir, &curentry, &resultentry)) == 0) { | |
315 | if (resultentry == NULL) { | |
316 | /* No more entry*/ | |
317 | break; | |
318 | } | |
319 | if (curentry.d_name[0] != '.') { | |
320 | if (curentry.d_type == DT_DIR) { | |
321 | ||
322 | char curpath[PATH_MAX]; | |
323 | snprintf(curpath, PATH_MAX, "%s/%s", trace_path, curentry.d_name); | |
324 | ret = lttv_traceset_add_path(ts, curpath); | |
325 | if (ret >= 0) { | |
326 | metaFileFound = TRUE; | |
327 | } | |
3685e022 | 328 | } |
329 | } | |
330 | } | |
331 | ||
9b55aba0 YB |
332 | if (ret != 0) { |
333 | g_warning("Invalid readdir"); | |
334 | } | |
335 | ||
3685e022 | 336 | error: |
861fbe5f FD |
337 | if(metaFileFound) |
338 | return ret; | |
339 | else | |
340 | return -1; | |
2bc1bcfb | 341 | } |
dc877563 | 342 | |
343 | unsigned lttv_traceset_number(LttvTraceset *s) | |
344 | { | |
90e19f82 | 345 | return s->traces->len; |
dc877563 | 346 | } |
347 | ||
348 | ||
308711e5 | 349 | LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i) |
dc877563 | 350 | { |
90e19f82 AM |
351 | g_assert(s->traces->len > i); |
352 | return ((LttvTrace *)s->traces->pdata[i]); | |
dc877563 | 353 | } |
354 | ||
355 | ||
ba576a78 | 356 | void lttv_traceset_remove(LttvTraceset *s, unsigned i) |
dc877563 | 357 | { |
90e19f82 AM |
358 | LttvTrace * t; |
359 | g_assert(s->traces->len > i); | |
360 | t = (LttvTrace *)s->traces->pdata[i]; | |
361 | t->ref_count--; | |
2bc1bcfb | 362 | bt_context_remove_trace(lttv_traceset_get_context(s), t->id); |
90e19f82 | 363 | g_ptr_array_remove_index(s->traces, i); |
dc877563 | 364 | } |
365 | ||
366 | ||
367 | /* A set of attributes is attached to each trace set, trace and tracefile | |
90e19f82 | 368 | to store user defined data as needed. */ |
dc877563 | 369 | |
370 | LttvAttribute *lttv_traceset_attribute(LttvTraceset *s) | |
371 | { | |
90e19f82 | 372 | return s->a; |
dc877563 | 373 | } |
374 | ||
375 | ||
308711e5 | 376 | LttvAttribute *lttv_trace_attribute(LttvTrace *t) |
377 | { | |
90e19f82 | 378 | return t->a; |
308711e5 | 379 | } |
380 | ||
2bc1bcfb | 381 | |
382 | gint lttv_trace_get_id(LttvTrace *t) | |
383 | { | |
9a366873 | 384 | return t->id; |
2bc1bcfb | 385 | } |
308711e5 | 386 | |
2176f952 | 387 | guint lttv_trace_get_ref_number(LttvTrace * t) |
388 | { | |
2bc1bcfb | 389 | // todo mdenis: adapt to babeltrace |
90e19f82 | 390 | return t->ref_count; |
2176f952 | 391 | } |
a43d67ba | 392 | |
393 | guint lttv_trace_ref(LttvTrace * t) | |
394 | { | |
90e19f82 AM |
395 | t->ref_count++; |
396 | ||
397 | return t->ref_count; | |
a43d67ba | 398 | } |
399 | ||
400 | guint lttv_trace_unref(LttvTrace * t) | |
401 | { | |
90e19f82 AM |
402 | if(likely(t->ref_count > 0)) |
403 | t->ref_count--; | |
a43d67ba | 404 | |
90e19f82 | 405 | return t->ref_count; |
a43d67ba | 406 | } |
407 | ||
7a4bdb54 YB |
408 | guint lttv_trace_get_num_cpu(LttvTrace *t) |
409 | { | |
410 | #warning "TODO - Set the right number of CPU" | |
411 | return 24; | |
412 | } | |
413 | ||
68573dd0 | 414 | LttvTracesetPosition *lttv_traceset_create_current_position(LttvTraceset *traceset) |
7a4bdb54 | 415 | { |
3d1e7ee5 YB |
416 | LttvTracesetPosition *traceset_pos; |
417 | ||
418 | traceset_pos = g_new(LttvTracesetPosition, 1); | |
419 | ||
9a366873 | 420 | /* Check if the new passed */ |
3d1e7ee5 YB |
421 | if(traceset_pos == NULL) { |
422 | return NULL; | |
423 | } | |
424 | ||
425 | traceset_pos->iter = traceset->iter; | |
426 | traceset_pos->bt_pos = bt_iter_get_pos(bt_ctf_get_iter(traceset->iter)); | |
c73ce169 FD |
427 | traceset_pos->timestamp = G_MAXUINT64; |
428 | traceset_pos->cpu_id = INT_MAX; | |
429 | ||
3d1e7ee5 | 430 | return traceset_pos; |
7a4bdb54 YB |
431 | } |
432 | ||
9a366873 FD |
433 | LttvTracesetPosition *lttv_traceset_create_time_position(LttvTraceset *traceset, |
434 | LttTime timestamp) | |
435 | { | |
436 | LttvTracesetPosition *traceset_pos; | |
437 | ||
438 | traceset_pos = g_new(LttvTracesetPosition, 1); | |
439 | ||
440 | /* Check if the new passed */ | |
441 | if(traceset_pos == NULL) { | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | traceset_pos->iter = traceset->iter; | |
c73ce169 | 446 | traceset_pos->bt_pos = bt_iter_create_time_pos( |
9a366873 FD |
447 | bt_ctf_get_iter(traceset_pos->iter), |
448 | ltt_time_to_uint64(timestamp)); | |
c73ce169 FD |
449 | traceset_pos->timestamp = G_MAXUINT64; |
450 | traceset_pos->cpu_id = INT_MAX; | |
9a366873 FD |
451 | return traceset_pos; |
452 | } | |
453 | ||
7a4bdb54 YB |
454 | void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos) |
455 | { | |
3d1e7ee5 YB |
456 | bt_iter_free_pos(traceset_pos->bt_pos); |
457 | g_free(traceset_pos); | |
7a4bdb54 YB |
458 | } |
459 | ||
9a366873 | 460 | void lttv_traceset_seek_to_position(const LttvTracesetPosition *traceset_pos) |
7a4bdb54 | 461 | { |
9a366873 | 462 | bt_iter_set_pos(bt_ctf_get_iter(traceset_pos->iter), traceset_pos->bt_pos); |
7a4bdb54 YB |
463 | } |
464 | ||
465 | guint lttv_traceset_get_cpuid_from_event(LttvEvent *event) | |
466 | { | |
7a4bdb54 YB |
467 | unsigned long timestamp; |
468 | unsigned int cpu_id; | |
469 | ||
470 | struct bt_ctf_event *ctf_event = event->bt_event; | |
471 | timestamp = bt_ctf_get_timestamp(ctf_event); | |
472 | if (timestamp == -1ULL) { | |
473 | return 0; | |
474 | } | |
cfddb03e | 475 | const struct bt_definition *scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT); |
7a4bdb54 YB |
476 | if (bt_ctf_field_get_error()) { |
477 | return 0; | |
478 | } | |
479 | cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(ctf_event, scope, "cpu_id")); | |
480 | if (bt_ctf_field_get_error()) { | |
481 | return 0; | |
482 | } else { | |
483 | return cpu_id; | |
484 | } | |
485 | } | |
9a366873 FD |
486 | |
487 | guint64 lttv_traceset_get_timestamp_first_event(LttvTraceset *ts) | |
488 | { | |
489 | LttvTracesetPosition begin_position; | |
490 | struct bt_iter_pos pos; | |
491 | begin_position.bt_pos = &pos; | |
c73ce169 FD |
492 | begin_position.timestamp = G_MAXUINT64; |
493 | begin_position.cpu_id = INT_MAX; | |
9a366873 FD |
494 | |
495 | /* Assign iterator to the beginning of the traces */ | |
496 | begin_position.bt_pos->type = BT_SEEK_BEGIN; | |
497 | begin_position.iter = ts->iter; | |
498 | ||
499 | return lttv_traceset_position_get_timestamp(&begin_position); | |
500 | } | |
501 | ||
8924e3e4 FD |
502 | guint64 lttv_traceset_get_timestamp_last_event(LttvTraceset *ts) |
503 | { | |
504 | LttvTracesetPosition last_position; | |
505 | struct bt_iter_pos pos; | |
506 | last_position.bt_pos = &pos; | |
507 | last_position.timestamp = G_MAXUINT64; | |
508 | last_position.cpu_id = INT_MAX; | |
c0b8f3e8 | 509 | |
8924e3e4 FD |
510 | /* Assign iterator to the last event of the traces */ |
511 | last_position.bt_pos->type = BT_SEEK_LAST; | |
512 | last_position.iter = ts->iter; | |
c0b8f3e8 | 513 | |
8924e3e4 FD |
514 | return lttv_traceset_position_get_timestamp(&last_position); |
515 | } | |
516 | ||
451aaf27 FD |
517 | /* |
518 | * lttv_traceset_get_timestamp_begin : returns the minimum timestamp of | |
519 | * all the traces in the traceset. | |
520 | * | |
521 | */ | |
451aaf27 FD |
522 | guint64 lttv_traceset_get_timestamp_begin(LttvTraceset *traceset) |
523 | { | |
9a366873 FD |
524 | struct bt_context *bt_ctx; |
525 | bt_ctx = lttv_traceset_get_context(traceset); | |
526 | guint64 timestamp_min, timestamp_cur = 0; | |
527 | int i; | |
528 | int trace_count; | |
529 | LttvTrace *currentTrace; | |
530 | trace_count = traceset->traces->len; | |
531 | if(trace_count == 0) | |
532 | timestamp_min = 0; | |
533 | else{ | |
534 | timestamp_min = G_MAXUINT64; | |
535 | for(i = 0; i < trace_count;i++) | |
536 | { | |
537 | currentTrace = g_ptr_array_index(traceset->traces,i); | |
538 | timestamp_cur = bt_trace_handle_get_timestamp_begin(bt_ctx, | |
762e15b0 FD |
539 | currentTrace->id, |
540 | BT_CLOCK_REAL); | |
9a366873 FD |
541 | if(timestamp_cur < timestamp_min) |
542 | timestamp_min = timestamp_cur; | |
543 | } | |
544 | } | |
545 | return timestamp_min; | |
451aaf27 FD |
546 | } |
547 | ||
548 | /* | |
549 | * lttv_traceset_get_timestamp_end: returns the maximum timestamp of | |
550 | * all the traces in the traceset. | |
551 | * | |
552 | */ | |
553 | guint64 lttv_traceset_get_timestamp_end(LttvTraceset *traceset) | |
554 | { | |
9a366873 FD |
555 | struct bt_context *bt_ctx; |
556 | bt_ctx = lttv_traceset_get_context(traceset); | |
557 | guint64 timestamp_max, timestamp_cur = 0; | |
558 | int i; | |
559 | int trace_count; | |
560 | LttvTrace *currentTrace; | |
561 | trace_count = traceset->traces->len; | |
562 | ||
563 | if(trace_count == 0){ | |
564 | timestamp_max = 1; | |
565 | } | |
566 | else{ | |
567 | timestamp_max = 0; | |
568 | for(i =0; i < trace_count;i++) | |
569 | { | |
570 | currentTrace = g_ptr_array_index(traceset->traces,i); | |
571 | timestamp_cur = bt_trace_handle_get_timestamp_end(bt_ctx, | |
762e15b0 FD |
572 | currentTrace->id, |
573 | BT_CLOCK_REAL); | |
9a366873 FD |
574 | if(timestamp_cur > timestamp_max){ |
575 | timestamp_max = timestamp_cur; | |
576 | } | |
577 | } | |
578 | } | |
579 | return timestamp_max; | |
580 | } | |
581 | /* | |
582 | * lttv_traceset_get_time_span_real : return a TimeInterval representing the | |
8924e3e4 | 583 | * minimum timestamp and the maximum timestamp of the traceset. |
9a366873 FD |
584 | * |
585 | */ | |
586 | TimeInterval lttv_traceset_get_time_span_real(LttvTraceset *ts) | |
587 | { | |
b9410cf1 | 588 | |
979bddf1 | 589 | |
8924e3e4 | 590 | if(ltt_time_compare(ts->time_span.start_time, |
eda74fe8 | 591 | ltt_time_zero) == 0 && ts->traces->len > 0){ |
8924e3e4 FD |
592 | ts->time_span.start_time = ltt_time_from_uint64( |
593 | lttv_traceset_get_timestamp_first_event(ts)); | |
594 | ts->time_span.end_time = ltt_time_from_uint64( | |
c0b8f3e8 | 595 | lttv_traceset_get_timestamp_last_event(ts)); |
b9410cf1 YB |
596 | } |
597 | return ts->time_span; | |
9aaa78dc FD |
598 | } |
599 | ||
600 | /* | |
601 | * lttv_traceset_get_time_span : return a TimeInterval representing the | |
8924e3e4 | 602 | * minimum timestamp and the maximum timestamp of the traceset. |
9aaa78dc FD |
603 | * |
604 | */ | |
605 | TimeInterval lttv_traceset_get_time_span(LttvTraceset *ts) | |
606 | { | |
eda74fe8 | 607 | if(ltt_time_compare(ts->time_span.start_time, ltt_time_zero) == 0){ |
8924e3e4 FD |
608 | ts->time_span.start_time =ltt_time_from_uint64( |
609 | lttv_traceset_get_timestamp_begin(ts)); | |
610 | ts->time_span.end_time = ltt_time_from_uint64( | |
611 | lttv_traceset_get_timestamp_end(ts)); | |
612 | } | |
613 | return ts->time_span; | |
451aaf27 | 614 | } |
7a4bdb54 YB |
615 | |
616 | const char *lttv_traceset_get_name_from_event(LttvEvent *event) | |
617 | { | |
618 | return bt_ctf_event_name(event->bt_event); | |
619 | } | |
9a366873 | 620 | |
c73ce169 FD |
621 | int set_values_position(const LttvTracesetPosition *pos) |
622 | { | |
623 | LttvTracesetPosition previous_pos; | |
624 | previous_pos.iter = pos->iter; | |
625 | previous_pos.bt_pos = bt_iter_get_pos(bt_ctf_get_iter(pos->iter)); | |
9a366873 | 626 | /* Seek to the new desired position */ |
c73ce169 | 627 | lttv_traceset_seek_to_position(pos); |
9a366873 | 628 | /*Read the event*/ |
c73ce169 FD |
629 | struct bt_ctf_event *event = bt_ctf_iter_read_event(pos->iter); |
630 | ||
9a366873 | 631 | if(event != NULL){ |
762e15b0 | 632 | ((LttvTracesetPosition *)pos)->timestamp = bt_ctf_get_timestamp(event); |
c73ce169 FD |
633 | |
634 | LttvEvent lttv_event; | |
635 | lttv_event.bt_event = event; | |
636 | ((LttvTracesetPosition *)pos)->cpu_id = lttv_traceset_get_cpuid_from_event(<tv_event); | |
637 | } | |
638 | else { | |
639 | /* The event is null */ | |
640 | return 0; | |
9a366873 | 641 | } |
c73ce169 FD |
642 | |
643 | /* Reassign the previously saved position */ | |
644 | lttv_traceset_seek_to_position(&previous_pos); | |
645 | /*We must desallocate because the function bt_iter_get_pos() does a g_new */ | |
646 | bt_iter_free_pos(previous_pos.bt_pos); | |
3200b7f3 YB |
647 | if (pos->timestamp == G_MAXUINT64) { |
648 | return 0; | |
649 | } | |
762e15b0 | 650 | return 1; |
c73ce169 FD |
651 | } |
652 | ||
653 | guint64 lttv_traceset_position_get_timestamp(const LttvTracesetPosition *pos) | |
654 | { | |
655 | if(pos->timestamp == G_MAXUINT64){ | |
656 | if(set_values_position(pos) == 0){ | |
657 | return 0; | |
658 | } | |
659 | } | |
660 | ||
661 | return pos->timestamp; | |
662 | } | |
663 | ||
664 | int lttv_traceset_position_get_cpuid(const LttvTracesetPosition *pos){ | |
665 | if(pos->cpu_id == INT_MAX ){ | |
666 | if(set_values_position(pos) == 0){ | |
667 | return 0; | |
668 | } | |
669 | } | |
670 | return pos->cpu_id; | |
9a366873 FD |
671 | } |
672 | ||
673 | LttTime lttv_traceset_position_get_time(const LttvTracesetPosition *pos) | |
674 | { | |
675 | return ltt_time_from_uint64(lttv_traceset_position_get_timestamp(pos)); | |
676 | } | |
677 | ||
ff5c41f1 | 678 | |
3200b7f3 | 679 | /* 0 if equals, other is different */ |
9a366873 FD |
680 | int lttv_traceset_position_compare(const LttvTracesetPosition *pos1, const LttvTracesetPosition *pos2) |
681 | { | |
682 | #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" | |
683 | if(pos1 == NULL || pos2 == NULL){ | |
684 | return -1; | |
685 | } | |
3200b7f3 | 686 | |
cfddb03e YB |
687 | int res = -1; |
688 | #ifdef HAVE_BT_ITER_EQUALS_POS | |
689 | if(pos1->timestamp == G_MAXUINT64 || pos2->timestamp == G_MAXUINT64) { | |
690 | res = bt_iter_equals_pos(pos1->bt_pos, pos2->bt_pos); | |
691 | } | |
692 | #endif | |
3200b7f3 YB |
693 | if (res < 0) { |
694 | ||
695 | guint64 timeStampPos1,timeStampPos2; | |
696 | guint cpuId1, cpuId2; | |
697 | ||
698 | timeStampPos1 = lttv_traceset_position_get_timestamp(pos1); | |
699 | timeStampPos2 = lttv_traceset_position_get_timestamp(pos2); | |
700 | ||
cfddb03e YB |
701 | if (timeStampPos1 == timeStampPos2) { |
702 | ||
703 | cpuId1 = lttv_traceset_position_get_cpuid(pos1); | |
704 | cpuId2 = lttv_traceset_position_get_cpuid(pos2); | |
3200b7f3 | 705 | |
cfddb03e YB |
706 | if(cpuId1 == cpuId2){ |
707 | return 0; | |
708 | } | |
3200b7f3 | 709 | } |
cfddb03e | 710 | return 1; |
3200b7f3 YB |
711 | } else { |
712 | ||
713 | return !res; | |
9a366873 FD |
714 | } |
715 | } | |
ff5c41f1 YB |
716 | |
717 | int lttv_traceset_position_time_compare(const LttvTracesetPosition *pos1, | |
718 | const LttvTracesetPosition *pos2) | |
719 | { | |
720 | guint64 timeStampPos1,timeStampPos2; | |
721 | ||
722 | timeStampPos1 = lttv_traceset_position_get_timestamp(pos1); | |
723 | timeStampPos2 = lttv_traceset_position_get_timestamp(pos2); | |
724 | ||
725 | return timeStampPos1 - timeStampPos2; | |
726 | } | |
727 | int lttv_traceset_position_compare_current(const LttvTraceset *ts, | |
728 | const LttvTracesetPosition *pos) | |
729 | { | |
730 | int result = 0; | |
731 | LttvTracesetPosition *curPos = lttv_traceset_create_current_position(ts); | |
732 | ||
733 | result = lttv_traceset_position_compare(curPos,pos); | |
734 | ||
735 | lttv_traceset_destroy_position(curPos); | |
736 | ||
737 | return result; | |
738 | } | |
739 | ||
740 | LttTime lttv_traceset_get_current_time(const LttvTraceset *ts) | |
741 | { | |
742 | LttvTracesetPosition *curPos = lttv_traceset_create_current_position(ts); | |
743 | guint64 currentTimestamp = lttv_traceset_position_get_timestamp(curPos); | |
744 | lttv_traceset_destroy_position(curPos); | |
745 | ||
746 | return ltt_time_from_uint64(currentTimestamp); | |
747 | } |