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