Fix : callback chain len
[babeltrace.git] / lib / iterator.c
... / ...
CommitLineData
1/*
2 * iterator.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21#include <stdlib.h>
22#include <babeltrace/babeltrace.h>
23#include <babeltrace/callbacks-internal.h>
24#include <babeltrace/context.h>
25#include <babeltrace/context-internal.h>
26#include <babeltrace/ctf/metadata.h>
27#include <babeltrace/iterator-internal.h>
28#include <babeltrace/iterator.h>
29#include <babeltrace/prio_heap.h>
30#include <babeltrace/ctf/events.h>
31#include <inttypes.h>
32
33struct stream_saved_pos {
34 /*
35 * Use file_stream pointer to check if the trace collection we
36 * restore to match the one we saved from, for each stream.
37 */
38 struct ctf_file_stream *file_stream;
39 size_t cur_index; /* current index in packet index */
40 ssize_t offset; /* offset from base, in bits. EOF for end of file. */
41 uint64_t current_timestamp;
42};
43
44struct bt_saved_pos {
45 struct trace_collection *tc;
46 GArray *stream_saved_pos; /* Contains struct stream_saved_pos */
47};
48
49static int stream_read_event(struct ctf_file_stream *sin)
50{
51 int ret;
52
53 ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent);
54 if (ret == EOF)
55 return EOF;
56 else if (ret) {
57 fprintf(stderr, "[error] Reading event failed.\n");
58 return ret;
59 }
60 return 0;
61}
62
63/*
64 * returns true if a < b, false otherwise.
65 */
66int stream_compare(void *a, void *b)
67{
68 struct ctf_file_stream *s_a = a, *s_b = b;
69
70 if (s_a->parent.timestamp < s_b->parent.timestamp)
71 return 1;
72 else
73 return 0;
74}
75
76void bt_iter_free_pos(struct bt_iter_pos *iter_pos)
77{
78 if (!iter_pos)
79 return;
80
81 if (iter_pos->u.restore) {
82 if (iter_pos->u.restore->stream_saved_pos) {
83 g_array_free(
84 iter_pos->u.restore->stream_saved_pos,
85 TRUE);
86 }
87 g_free(iter_pos->u.restore);
88 }
89 g_free(iter_pos);
90}
91
92/*
93 * seek_file_stream_by_timestamp
94 *
95 * Browse a filestream by index, if an index contains the timestamp passed in
96 * argument, seek inside the corresponding packet it until we find the event we
97 * are looking for (either the exact timestamp or the event just after the
98 * timestamp).
99 *
100 * Return 0 if the seek succeded and EOF if we didn't find any packet
101 * containing the timestamp.
102 */
103static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs,
104 uint64_t timestamp)
105{
106 struct ctf_stream_pos *stream_pos;
107 struct packet_index *index;
108 int i, ret;
109
110 stream_pos = &cfs->pos;
111 for (i = 0; i < stream_pos->packet_index->len; i++) {
112 index = &g_array_index(stream_pos->packet_index,
113 struct packet_index, i);
114 if (index->timestamp_begin >= timestamp ||
115 index->timestamp_end <= timestamp)
116 continue;
117
118 stream_pos->cur_index = i;
119 stream_pos->move_pos_slow(stream_pos, index->offset, SEEK_SET);
120 while (cfs->parent.timestamp < timestamp) {
121 ret = stream_read_event(cfs);
122 if (ret < 0)
123 break;
124 }
125 return 0;
126 }
127 return EOF;
128}
129
130/*
131 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
132 * the corresponding timestamp
133 *
134 * Return 0 on success.
135 * If the timestamp is not part of any file stream, return EOF to inform the
136 * user the timestamp is out of the scope
137 */
138static int seek_ctf_trace_by_timestamp(struct ctf_trace *tin,
139 uint64_t timestamp, struct ptr_heap *stream_heap)
140{
141 int i, j, ret;
142 int found = EOF;
143
144 /* for each stream_class */
145 for (i = 0; i < tin->streams->len; i++) {
146 struct ctf_stream_class *stream_class;
147
148 stream_class = g_ptr_array_index(tin->streams, i);
149 /* for each file_stream */
150 for (j = 0; j < stream_class->streams->len; j++) {
151 struct ctf_stream *stream;
152 struct ctf_file_stream *cfs;
153
154 stream = g_ptr_array_index(stream_class->streams, j);
155 cfs = container_of(stream, struct ctf_file_stream,
156 parent);
157 ret = seek_file_stream_by_timestamp(cfs, timestamp);
158 if (ret == 0) {
159 /* Add to heap */
160 ret = heap_insert(stream_heap, cfs);
161 if (ret)
162 goto error;
163 found = 0;
164 }
165 }
166 }
167
168 return found;
169
170error:
171 return -2;
172}
173
174int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
175{
176 struct trace_collection *tc;
177 int i, ret;
178
179 switch (iter_pos->type) {
180 case BT_SEEK_RESTORE:
181 if (!iter_pos->u.restore)
182 goto error_arg;
183
184 heap_free(iter->stream_heap);
185 ret = heap_init(iter->stream_heap, 0, stream_compare);
186 if (ret < 0)
187 goto error;
188
189 for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len;
190 i++) {
191 struct stream_saved_pos *saved_pos;
192 struct ctf_stream_pos *stream_pos;
193 struct packet_index *index;
194 struct ctf_stream *stream;
195
196 saved_pos = &g_array_index(
197 iter_pos->u.restore->stream_saved_pos,
198 struct stream_saved_pos, i);
199 stream = &saved_pos->file_stream->parent;
200 stream_pos = &saved_pos->file_stream->pos;
201 index = &g_array_index(stream_pos->packet_index,
202 struct packet_index,
203 saved_pos->cur_index);
204
205 stream_pos->cur_index = saved_pos->cur_index;
206 stream_pos->move_pos_slow(stream_pos, index->offset,
207 SEEK_SET);
208
209 /*
210 * the timestamp needs to be restored after
211 * move_pos_slow, because this function resets
212 * the timestamp to the beginning of the packet
213 */
214 stream->timestamp = saved_pos->current_timestamp;
215 stream_pos->offset = saved_pos->offset;
216 stream_pos->last_offset = saved_pos->offset;
217
218 stream->prev_timestamp = 0;
219 stream->prev_timestamp_end = 0;
220 stream->consumed = 0;
221
222 printf_debug("restored to cur_index = %zd and "
223 "offset = %zd, timestamp = %" PRIu64 "\n",
224 stream_pos->cur_index,
225 stream_pos->offset, stream->timestamp);
226
227 stream_read_event(saved_pos->file_stream);
228
229 /* Add to heap */
230 ret = heap_insert(iter->stream_heap,
231 saved_pos->file_stream);
232 if (ret)
233 goto error;
234 }
235 case BT_SEEK_TIME:
236 tc = iter->ctx->tc;
237
238 if (!iter_pos->u.seek_time)
239 goto error_arg;
240
241 heap_free(iter->stream_heap);
242 ret = heap_init(iter->stream_heap, 0, stream_compare);
243 if (ret < 0)
244 goto error;
245
246 /* for each trace in the trace_collection */
247 for (i = 0; i < tc->array->len; i++) {
248 struct ctf_trace *tin;
249 struct trace_descriptor *td_read;
250
251 td_read = g_ptr_array_index(tc->array, i);
252 tin = container_of(td_read, struct ctf_trace, parent);
253
254 ret = seek_ctf_trace_by_timestamp(tin,
255 iter_pos->u.seek_time,
256 iter->stream_heap);
257 if (ret < 0)
258 goto error;
259 }
260 return 0;
261 default:
262 /* not implemented */
263 goto error_arg;
264 }
265
266 return 0;
267
268error_arg:
269 ret = -EINVAL;
270error:
271 heap_free(iter->stream_heap);
272 if (heap_init(iter->stream_heap, 0, stream_compare) < 0) {
273 heap_free(iter->stream_heap);
274 g_free(iter->stream_heap);
275 iter->stream_heap = NULL;
276 ret = -ENOMEM;
277 }
278 return ret;
279}
280
281struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
282{
283 struct bt_iter_pos *pos;
284 struct trace_collection *tc = iter->ctx->tc;
285 int i, stream_class_id, stream_id;
286
287 pos = g_new0(struct bt_iter_pos, 1);
288 pos->u.restore = g_new0(struct bt_saved_pos, 1);
289 pos->u.restore->tc = tc;
290 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
291 sizeof(struct stream_saved_pos));
292 if (!pos->u.restore->stream_saved_pos)
293 goto error;
294
295 for (i = 0; i < tc->array->len; i++) {
296 struct ctf_trace *tin;
297 struct trace_descriptor *td_read;
298
299 td_read = g_ptr_array_index(tc->array, i);
300 tin = container_of(td_read, struct ctf_trace, parent);
301
302 for (stream_class_id = 0; stream_class_id < tin->streams->len;
303 stream_class_id++) {
304 struct ctf_stream_class *stream_class;
305
306 stream_class = g_ptr_array_index(tin->streams,
307 stream_class_id);
308 for (stream_id = 0;
309 stream_id < stream_class->streams->len;
310 stream_id++) {
311 struct ctf_stream *stream;
312 struct ctf_file_stream *cfs;
313 struct stream_saved_pos saved_pos;
314
315 stream = g_ptr_array_index(
316 stream_class->streams,
317 stream_id);
318 cfs = container_of(stream,
319 struct ctf_file_stream,
320 parent);
321
322 saved_pos.file_stream = cfs;
323 saved_pos.cur_index = cfs->pos.cur_index;
324
325 /*
326 * It is possible that an event was read during
327 * the last restore, never consumed and its
328 * position saved again. For this case, we
329 * need to check if the event really was
330 * consumed by the caller otherwise it is lost.
331 */
332 if (stream->consumed)
333 saved_pos.offset = cfs->pos.offset;
334 else
335 saved_pos.offset = cfs->pos.last_offset;
336
337 saved_pos.current_timestamp = stream->timestamp;
338
339 g_array_append_val(
340 pos->u.restore->stream_saved_pos,
341 saved_pos);
342
343 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
344 "offset : %zd, "
345 "timestamp = %" PRIu64 "\n",
346 stream->stream_id, saved_pos.cur_index,
347 saved_pos.offset,
348 saved_pos.current_timestamp);
349 }
350 }
351 }
352
353 return pos;
354
355error:
356 return NULL;
357}
358
359struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
360 uint64_t timestamp)
361{
362 struct bt_iter_pos *pos;
363
364 pos = g_new0(struct bt_iter_pos, 1);
365 pos->type = BT_SEEK_TIME;
366 pos->u.seek_time = timestamp;
367 return pos;
368}
369
370/*
371 * babeltrace_filestream_seek: seek a filestream to given position.
372 *
373 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
374 */
375static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
376 const struct bt_iter_pos *begin_pos,
377 unsigned long stream_id)
378{
379 int ret = 0;
380
381 switch (begin_pos->type) {
382 case BT_SEEK_CUR:
383 /*
384 * just insert into the heap we should already know
385 * the timestamps
386 */
387 break;
388 case BT_SEEK_BEGIN:
389 file_stream->pos.move_pos_slow(&file_stream->pos, 0, SEEK_SET);
390 ret = stream_read_event(file_stream);
391 break;
392 case BT_SEEK_TIME:
393 case BT_SEEK_RESTORE:
394 case BT_SEEK_END:
395 default:
396 assert(0); /* Not yet defined */
397 }
398
399 return ret;
400}
401
402/*
403 * bt_iter_seek: seek iterator to given position.
404 */
405int bt_iter_seek(struct bt_iter *iter,
406 const struct bt_iter_pos *begin_pos)
407{
408 int i, stream_id;
409 int ret = 0;
410 struct trace_collection *tc = iter->ctx->tc;
411
412 for (i = 0; i < tc->array->len; i++) {
413 struct ctf_trace *tin;
414 struct trace_descriptor *td_read;
415
416 td_read = g_ptr_array_index(tc->array, i);
417 tin = container_of(td_read, struct ctf_trace, parent);
418
419 /* Populate heap with each stream */
420 for (stream_id = 0; stream_id < tin->streams->len;
421 stream_id++) {
422 struct ctf_stream_class *stream;
423 int filenr;
424
425 stream = g_ptr_array_index(tin->streams, stream_id);
426 for (filenr = 0; filenr < stream->streams->len;
427 filenr++) {
428 struct ctf_file_stream *file_stream;
429
430 file_stream = g_ptr_array_index(stream->streams,
431 filenr);
432 ret = babeltrace_filestream_seek(file_stream, begin_pos,
433 stream_id);
434 if (ret < 0)
435 goto end;
436 }
437 }
438 }
439end:
440 return ret;
441}
442
443struct bt_iter *bt_iter_create(struct bt_context *ctx,
444 struct bt_iter_pos *begin_pos,
445 struct bt_iter_pos *end_pos)
446{
447 int i, stream_id;
448 int ret = 0;
449 struct bt_iter *iter;
450
451 iter = g_new0(struct bt_iter, 1);
452 iter->stream_heap = g_new(struct ptr_heap, 1);
453 iter->end_pos = end_pos;
454 iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks));
455 iter->recalculate_dep_graph = 0;
456 iter->main_callbacks.callback = NULL;
457 iter->dep_gc = g_ptr_array_new();
458 bt_context_get(ctx);
459 iter->ctx = ctx;
460
461 ret = heap_init(iter->stream_heap, 0, stream_compare);
462 if (ret < 0)
463 goto error_heap_init;
464
465 for (i = 0; i < ctx->tc->array->len; i++) {
466 struct ctf_trace *tin;
467 struct trace_descriptor *td_read;
468
469 td_read = g_ptr_array_index(ctx->tc->array, i);
470 tin = container_of(td_read, struct ctf_trace, parent);
471
472 /* Populate heap with each stream */
473 for (stream_id = 0; stream_id < tin->streams->len;
474 stream_id++) {
475 struct ctf_stream_class *stream;
476 int filenr;
477
478 stream = g_ptr_array_index(tin->streams, stream_id);
479 if (!stream)
480 continue;
481 for (filenr = 0; filenr < stream->streams->len;
482 filenr++) {
483 struct ctf_file_stream *file_stream;
484
485 file_stream = g_ptr_array_index(stream->streams,
486 filenr);
487
488 if (begin_pos) {
489 ret = babeltrace_filestream_seek(file_stream, begin_pos,
490 stream_id);
491 if (ret == EOF) {
492 ret = 0;
493 continue;
494 } else if (ret) {
495 goto error;
496 }
497 }
498 /* Add to heap */
499 ret = heap_insert(iter->stream_heap, file_stream);
500 if (ret)
501 goto error;
502 }
503 }
504 }
505
506 return iter;
507
508error:
509 heap_free(iter->stream_heap);
510error_heap_init:
511 g_free(iter->stream_heap);
512 g_free(iter);
513 return NULL;
514}
515
516void bt_iter_destroy(struct bt_iter *iter)
517{
518 struct bt_stream_callbacks *bt_stream_cb;
519 struct bt_callback_chain *bt_chain;
520 int i, j;
521
522 if (iter->stream_heap) {
523 heap_free(iter->stream_heap);
524 g_free(iter->stream_heap);
525 }
526
527 /* free all events callbacks */
528 if (iter->main_callbacks.callback)
529 g_array_free(iter->main_callbacks.callback, TRUE);
530
531 /* free per-event callbacks */
532 for (i = 0; i < iter->callbacks->len; i++) {
533 bt_stream_cb = &g_array_index(iter->callbacks,
534 struct bt_stream_callbacks, i);
535 if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
536 continue;
537 for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) {
538 bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
539 struct bt_callback_chain, j);
540 if (bt_chain->callback) {
541 g_array_free(bt_chain->callback, TRUE);
542 }
543 }
544 g_array_free(bt_stream_cb->per_id_callbacks, TRUE);
545 }
546
547 bt_context_put(iter->ctx);
548
549 g_free(iter);
550}
551
552int bt_iter_next(struct bt_iter *iter)
553{
554 struct ctf_file_stream *file_stream, *removed;
555 int ret;
556
557 file_stream = heap_maximum(iter->stream_heap);
558 if (!file_stream) {
559 /* end of file for all streams */
560 ret = 0;
561 goto end;
562 }
563
564 ret = stream_read_event(file_stream);
565 if (ret == EOF) {
566 removed = heap_remove(iter->stream_heap);
567 assert(removed == file_stream);
568 ret = 0;
569 goto end;
570 } else if (ret) {
571 goto end;
572 }
573 /* Reinsert the file stream into the heap, and rebalance. */
574 removed = heap_replace_max(iter->stream_heap, file_stream);
575 assert(removed == file_stream);
576
577end:
578 return ret;
579}
580
581struct bt_ctf_event *bt_iter_read_ctf_event(struct bt_iter *iter)
582{
583 struct ctf_file_stream *file_stream;
584 struct bt_ctf_event *ret = &iter->current_ctf_event;
585
586 file_stream = heap_maximum(iter->stream_heap);
587 if (!file_stream) {
588 /* end of file for all streams */
589 goto stop;
590 }
591 ret->stream = &file_stream->parent;
592 ret->event = g_ptr_array_index(ret->stream->events_by_id,
593 ret->stream->event_id);
594
595 if (ret->stream->stream_id > iter->callbacks->len)
596 goto end;
597
598 process_callbacks(iter, ret->stream);
599
600end:
601 return ret;
602stop:
603 return NULL;
604}
This page took 0.024104 seconds and 4 git commands to generate.