542101607d064d61fd322fc97409fb425bcff3c1
[lttv.git] / lttv / lttv / traceset.c
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
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <lttv/traceset.h>
24 #include <lttv/iattribute.h>
25 #include <lttv/state.h>
26 #include <lttv/event.h>
27 #include <lttv/hook.h>
28 #include <stdio.h>
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/context.h>
31 #include <babeltrace/ctf/iterator.h>
32 #include <babeltrace/ctf/events.h>
33
34 /* To traverse a tree recursively */
35 #include <fcntl.h>
36 #include <fts.h>
37 /* For the use of realpath*/
38 #include <limits.h>
39 #include <stdlib.h>
40 /* For strcpy*/
41 #include <string.h>
42
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
49
50 LttvTraceset *lttv_traceset_new(void)
51 {
52 LttvTraceset *ts;
53 struct bt_iter_pos begin_pos;
54
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);
60 //TODO remove this when we have really mecanism
61 //s->tmpState = g_new(LttvTraceState *, 1);
62 //lttv_trace_state_init(s->tmpState,0);
63
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);
67
68 ts->event_hooks = lttv_hooks_new();
69
70 ts->state_trace_handle_index = g_ptr_array_new();
71 ts->has_precomputed_states = FALSE;
72
73 return ts;
74 }
75
76 char * lttv_traceset_name(LttvTraceset * s)
77 {
78 return s->filename;
79 }
80
81 #ifdef BABEL_CLEANUP
82 LttvTrace *lttv_trace_new(LttTrace *t)
83 {
84 LttvTrace *new_trace;
85
86 new_trace = g_new(LttvTrace, 1);
87 new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
88 new_trace->id = t;
89 new_trace->ref_count = 0;
90 return new_trace;
91 }
92 #endif
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
117
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 {
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;
143
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);
151
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;
155
156 return new_trace;
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 {
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;
176 }
177
178 LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig)
179 {
180 guint i;
181 LttvTraceset *s;
182 LttvTrace * trace;
183
184 s = g_new(LttvTraceset, 1);
185 s->filename = NULL;
186 s->traces = g_ptr_array_new();
187 s->state_trace_handle_index = g_ptr_array_new();
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++;
192
193 /* WARNING: this is an alias, not a copy. */
194 g_ptr_array_add(s->traces, trace);
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
199 }
200 s->context = s_orig->context;
201 bt_context_get(s->context);
202 s->a = LTTV_ATTRIBUTE(lttv_iattribute_deep_copy(LTTV_IATTRIBUTE(s_orig->a)));
203 return s;
204 }
205
206
207 LttvTraceset *lttv_traceset_load(const gchar *filename)
208 {
209 LttvTraceset *s = g_new(LttvTraceset,1);
210 FILE *tf;
211
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;
219 }
220
221 gint lttv_traceset_save(LttvTraceset *s)
222 {
223 FILE *tf;
224
225 tf = fopen(s->filename, "w");
226
227 g_critical("NOT IMPLEMENTED : save traceset data in a XML file");
228
229 fclose(tf);
230 return 0;
231 }
232
233 void lttv_traceset_destroy(LttvTraceset *s)
234 {
235 guint i;
236
237 for(i=0;i<s->traces->len;i++) {
238 LttvTrace *trace = g_ptr_array_index(s->traces, i);
239 lttv_trace_unref(trace);
240 // todo mdenis 2012-03-27: uncomment when babeltrace gets fixed
241 //bt_context_remove_trace(lttv_traceset_get_context(s), trace->id);
242 if(lttv_trace_get_ref_number(trace) == 0)
243 lttv_trace_destroy(trace);
244 }
245 g_ptr_array_free(s->traces, TRUE);
246 bt_context_put(s->context);
247 g_object_unref(s->a);
248 g_free(s);
249 }
250
251 struct bt_context *lttv_traceset_get_context(LttvTraceset *s)
252 {
253 return s->context;
254 }
255
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
266 void lttv_trace_destroy(LttvTrace *t)
267 {
268 g_object_unref(t->a);
269 g_free(t);
270 }
271
272 void lttv_traceset_add(LttvTraceset *s, LttvTrace *t)
273 {
274 t->ref_count++;
275 g_ptr_array_add(s->traces, t);
276 }
277
278 int lttv_traceset_add_path(LttvTraceset *ts, char *trace_path)
279 {
280 FTS *tree;
281 FTSENT *node;
282 char * const paths[2] = { trace_path, NULL };
283 int ret = -1;
284
285 gboolean metaFileFound = FALSE;
286
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 }
337 metaFileFound = TRUE;
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 }
347 if(metaFileFound)
348 return ret;
349 else
350 return -1;
351 }
352
353
354 unsigned lttv_traceset_number(LttvTraceset *s)
355 {
356 return s->traces->len;
357 }
358
359
360 LttvTrace *lttv_traceset_get(LttvTraceset *s, unsigned i)
361 {
362 g_assert(s->traces->len > i);
363 return ((LttvTrace *)s->traces->pdata[i]);
364 }
365
366
367 void lttv_traceset_remove(LttvTraceset *s, unsigned i)
368 {
369 LttvTrace * t;
370 g_assert(s->traces->len > i);
371 t = (LttvTrace *)s->traces->pdata[i];
372 t->ref_count--;
373 bt_context_remove_trace(lttv_traceset_get_context(s), t->id);
374 g_ptr_array_remove_index(s->traces, i);
375 }
376
377
378 /* A set of attributes is attached to each trace set, trace and tracefile
379 to store user defined data as needed. */
380
381 LttvAttribute *lttv_traceset_attribute(LttvTraceset *s)
382 {
383 return s->a;
384 }
385
386
387 LttvAttribute *lttv_trace_attribute(LttvTrace *t)
388 {
389 return t->a;
390 }
391
392
393 gint lttv_trace_get_id(LttvTrace *t)
394 {
395 return t->id;
396 }
397
398 guint lttv_trace_get_ref_number(LttvTrace * t)
399 {
400 // todo mdenis: adapt to babeltrace
401 return t->ref_count;
402 }
403
404 guint lttv_trace_ref(LttvTrace * t)
405 {
406 t->ref_count++;
407
408 return t->ref_count;
409 }
410
411 guint lttv_trace_unref(LttvTrace * t)
412 {
413 if(likely(t->ref_count > 0))
414 t->ref_count--;
415
416 return t->ref_count;
417 }
418
419 guint lttv_trace_get_num_cpu(LttvTrace *t)
420 {
421 #warning "TODO - Set the right number of CPU"
422 return 24;
423 }
424
425 LttvTracesetPosition *lttv_traceset_create_current_position(LttvTraceset *traceset)
426 {
427 LttvTracesetPosition *traceset_pos;
428
429 traceset_pos = g_new(LttvTracesetPosition, 1);
430
431 /* Check if the new passed */
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));
438
439 return traceset_pos;
440 }
441
442 LttvTracesetPosition *lttv_traceset_create_time_position(LttvTraceset *traceset,
443 LttTime timestamp)
444 {
445 LttvTracesetPosition *traceset_pos;
446
447 traceset_pos = g_new(LttvTracesetPosition, 1);
448
449 /* Check if the new passed */
450 if(traceset_pos == NULL) {
451 return NULL;
452 }
453
454 traceset_pos->iter = traceset->iter;
455 traceset_pos->bt_pos = bt_iter_create_time_pos(
456 bt_ctf_get_iter(traceset_pos->iter),
457 ltt_time_to_uint64(timestamp));
458
459 return traceset_pos;
460 }
461
462 void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos)
463 {
464 bt_iter_free_pos(traceset_pos->bt_pos);
465 g_free(traceset_pos);
466 }
467
468 void lttv_traceset_seek_to_position(const LttvTracesetPosition *traceset_pos)
469 {
470 bt_iter_set_pos(bt_ctf_get_iter(traceset_pos->iter), traceset_pos->bt_pos);
471 }
472
473 guint lttv_traceset_get_cpuid_from_event(LttvEvent *event)
474 {
475 unsigned long timestamp;
476 unsigned int cpu_id;
477
478 struct bt_ctf_event *ctf_event = event->bt_event;
479 timestamp = bt_ctf_get_timestamp(ctf_event);
480 if (timestamp == -1ULL) {
481 return 0;
482 }
483 const struct definition *scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT);
484 if (bt_ctf_field_get_error()) {
485 return 0;
486 }
487 cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(ctf_event, scope, "cpu_id"));
488 if (bt_ctf_field_get_error()) {
489 return 0;
490 } else {
491 return cpu_id;
492 }
493 }
494
495 guint64 lttv_traceset_get_timestamp_first_event(LttvTraceset *ts)
496 {
497 LttvTracesetPosition begin_position;
498 struct bt_iter_pos pos;
499 begin_position.bt_pos = &pos;
500
501 /* Assign iterator to the beginning of the traces */
502 begin_position.bt_pos->type = BT_SEEK_BEGIN;
503 begin_position.iter = ts->iter;
504
505 return lttv_traceset_position_get_timestamp(&begin_position);
506 }
507
508 /*
509 * lttv_traceset_get_timestamp_begin : returns the minimum timestamp of
510 * all the traces in the traceset.
511 *
512 */
513 guint64 lttv_traceset_get_timestamp_begin(LttvTraceset *traceset)
514 {
515 struct bt_context *bt_ctx;
516 bt_ctx = lttv_traceset_get_context(traceset);
517 guint64 timestamp_min, timestamp_cur = 0;
518 int i;
519 int trace_count;
520 LttvTrace *currentTrace;
521 trace_count = traceset->traces->len;
522 if(trace_count == 0)
523 timestamp_min = 0;
524 else{
525 timestamp_min = G_MAXUINT64;
526 for(i = 0; i < trace_count;i++)
527 {
528 currentTrace = g_ptr_array_index(traceset->traces,i);
529 timestamp_cur = bt_trace_handle_get_timestamp_begin(bt_ctx,
530 currentTrace->id);
531 if(timestamp_cur < timestamp_min)
532 timestamp_min = timestamp_cur;
533 }
534 }
535 return timestamp_min;
536 }
537
538 /*
539 * lttv_traceset_get_timestamp_end: returns the maximum timestamp of
540 * all the traces in the traceset.
541 *
542 */
543 guint64 lttv_traceset_get_timestamp_end(LttvTraceset *traceset)
544 {
545 struct bt_context *bt_ctx;
546 bt_ctx = lttv_traceset_get_context(traceset);
547 guint64 timestamp_max, timestamp_cur = 0;
548 int i;
549 int trace_count;
550 LttvTrace *currentTrace;
551 trace_count = traceset->traces->len;
552
553 if(trace_count == 0){
554 timestamp_max = 1;
555 }
556 else{
557 timestamp_max = 0;
558 for(i =0; i < trace_count;i++)
559 {
560 currentTrace = g_ptr_array_index(traceset->traces,i);
561 timestamp_cur = bt_trace_handle_get_timestamp_end(bt_ctx,
562 currentTrace->id);
563 if(timestamp_cur > timestamp_max){
564 timestamp_max = timestamp_cur;
565 }
566 }
567 }
568 return timestamp_max;
569 }
570 /*
571 * lttv_traceset_get_time_span_real : return a TimeInterval representing the
572 * minimum timestamp dans le maximum timestamp of the traceset.
573 *
574 */
575 TimeInterval lttv_traceset_get_time_span_real(LttvTraceset *ts)
576 {
577 TimeInterval time_span;
578 time_span.start_time =ltt_time_from_uint64(lttv_traceset_get_timestamp_first_event(ts));
579 time_span.end_time = ltt_time_from_uint64(lttv_traceset_get_timestamp_end(ts));
580 return time_span;
581 }
582
583 /*
584 * lttv_traceset_get_time_span : return a TimeInterval representing the
585 * minimum timestamp dans le maximum timestamp of the traceset.
586 *
587 */
588 TimeInterval lttv_traceset_get_time_span(LttvTraceset *ts)
589 {
590 TimeInterval time_span;
591 time_span.start_time =ltt_time_from_uint64(lttv_traceset_get_timestamp_begin(ts));
592 time_span.end_time = ltt_time_from_uint64(lttv_traceset_get_timestamp_end(ts));
593 return time_span;
594 }
595
596 const char *lttv_traceset_get_name_from_event(LttvEvent *event)
597 {
598 return bt_ctf_event_name(event->bt_event);
599 }
600
601 guint64 lttv_traceset_position_get_timestamp(const LttvTracesetPosition *pos)
602 {
603 guint64 timestamp = 0;
604 /*We save the current iterator,so we can reassign it after the seek*/
605 LttvTracesetPosition previous_pos;
606 previous_pos.iter = pos->iter;
607 previous_pos.bt_pos = bt_iter_get_pos(bt_ctf_get_iter(pos->iter));
608 /* Seek to the new desired position */
609 lttv_traceset_seek_to_position(pos);
610 /*Read the event*/
611 struct bt_ctf_event *event = bt_ctf_iter_read_event(pos->iter);
612
613 if(event != NULL){
614 timestamp = bt_ctf_get_timestamp_raw(event);
615 }
616 /* Reassign the previously saved position */
617 lttv_traceset_seek_to_position(&previous_pos);
618 /*We must desallocate because the function bt_iter_get_pos() does a g_new */
619 bt_iter_free_pos(previous_pos.bt_pos);
620 return timestamp;
621 }
622
623 LttTime lttv_traceset_position_get_time(const LttvTracesetPosition *pos)
624 {
625 return ltt_time_from_uint64(lttv_traceset_position_get_timestamp(pos));
626 }
627
628
629
630 int lttv_traceset_position_compare(const LttvTracesetPosition *pos1, const LttvTracesetPosition *pos2)
631 {
632 #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"
633 if(pos1 == NULL || pos2 == NULL){
634 return -1;
635 }
636
637 guint64 timeStampPos1,timeStampPos2;
638 guint cpuId1, cpuId2;
639 LttvEvent event1, event2;
640 int ret;
641
642 timeStampPos1 = lttv_traceset_position_get_timestamp(pos1);
643 timeStampPos2 = lttv_traceset_position_get_timestamp(pos2);
644
645 event1.bt_event = bt_ctf_iter_read_event(pos1->iter);
646 event2.bt_event = bt_ctf_iter_read_event(pos2->iter);
647
648 if(event1.bt_event == NULL || event2.bt_event == NULL){
649 return -1;
650 }
651
652 cpuId1 = lttv_traceset_get_cpuid_from_event(&event1);
653 cpuId2 = lttv_traceset_get_cpuid_from_event(&event2);
654
655 if(timeStampPos1 == timeStampPos2 && cpuId1 == cpuId2){
656 return 0;
657 }
658 else{
659 return 1;
660 }
661 }
662
663 int lttv_traceset_position_time_compare(const LttvTracesetPosition *pos1,
664 const LttvTracesetPosition *pos2)
665 {
666 guint64 timeStampPos1,timeStampPos2;
667
668 timeStampPos1 = lttv_traceset_position_get_timestamp(pos1);
669 timeStampPos2 = lttv_traceset_position_get_timestamp(pos2);
670
671 return timeStampPos1 - timeStampPos2;
672 }
673 int lttv_traceset_position_compare_current(const LttvTraceset *ts,
674 const LttvTracesetPosition *pos)
675 {
676 int result = 0;
677 LttvTracesetPosition *curPos = lttv_traceset_create_current_position(ts);
678
679 result = lttv_traceset_position_compare(curPos,pos);
680
681 lttv_traceset_destroy_position(curPos);
682
683 return result;
684 }
685
686 LttTime lttv_traceset_get_current_time(const LttvTraceset *ts)
687 {
688 LttvTracesetPosition *curPos = lttv_traceset_create_current_position(ts);
689 guint64 currentTimestamp = lttv_traceset_position_get_timestamp(curPos);
690 lttv_traceset_destroy_position(curPos);
691
692 return ltt_time_from_uint64(currentTimestamp);
693 }
This page took 0.041065 seconds and 3 git commands to generate.