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