fix: move read event from iterator to event.h
[babeltrace.git] / lib / iterator.c
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/iterator-internal.h>
27 #include <babeltrace/iterator.h>
28 #include <babeltrace/prio_heap.h>
29 #include <babeltrace/ctf/metadata.h>
30 #include <babeltrace/ctf/events.h>
31 #include <inttypes.h>
32
33 struct 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
44 struct bt_saved_pos {
45 struct trace_collection *tc;
46 GArray *stream_saved_pos; /* Contains struct stream_saved_pos */
47 };
48
49 static 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 */
66 int 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
76 void 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 */
103 static 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->packet_seek(&stream_pos->parent, i, SEEK_SET);
119 while (cfs->parent.timestamp < timestamp) {
120 ret = stream_read_event(cfs);
121 if (ret < 0)
122 break;
123 }
124 return 0;
125 }
126 return EOF;
127 }
128
129 /*
130 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
131 * the corresponding timestamp
132 *
133 * Return 0 on success.
134 * If the timestamp is not part of any file stream, return EOF to inform the
135 * user the timestamp is out of the scope
136 */
137 static int seek_ctf_trace_by_timestamp(struct ctf_trace *tin,
138 uint64_t timestamp, struct ptr_heap *stream_heap)
139 {
140 int i, j, ret;
141 int found = EOF;
142
143 /* for each stream_class */
144 for (i = 0; i < tin->streams->len; i++) {
145 struct ctf_stream_class *stream_class;
146
147 stream_class = g_ptr_array_index(tin->streams, i);
148 /* for each file_stream */
149 for (j = 0; j < stream_class->streams->len; j++) {
150 struct ctf_stream *stream;
151 struct ctf_file_stream *cfs;
152
153 stream = g_ptr_array_index(stream_class->streams, j);
154 cfs = container_of(stream, struct ctf_file_stream,
155 parent);
156 ret = seek_file_stream_by_timestamp(cfs, timestamp);
157 if (ret == 0) {
158 /* Add to heap */
159 ret = heap_insert(stream_heap, cfs);
160 if (ret)
161 goto error;
162 found = 0;
163 }
164 }
165 }
166
167 return found;
168
169 error:
170 return -2;
171 }
172
173 int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
174 {
175 struct trace_collection *tc;
176 int i, ret;
177
178 switch (iter_pos->type) {
179 case BT_SEEK_RESTORE:
180 if (!iter_pos->u.restore)
181 goto error_arg;
182
183 heap_free(iter->stream_heap);
184 ret = heap_init(iter->stream_heap, 0, stream_compare);
185 if (ret < 0)
186 goto error;
187
188 for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len;
189 i++) {
190 struct stream_saved_pos *saved_pos;
191 struct ctf_stream_pos *stream_pos;
192 struct ctf_stream *stream;
193
194 saved_pos = &g_array_index(
195 iter_pos->u.restore->stream_saved_pos,
196 struct stream_saved_pos, i);
197 stream = &saved_pos->file_stream->parent;
198 stream_pos = &saved_pos->file_stream->pos;
199
200 stream_pos->packet_seek(&stream_pos->parent,
201 saved_pos->cur_index, SEEK_SET);
202
203 /*
204 * the timestamp needs to be restored after
205 * packet_seek, because this function resets
206 * the timestamp to the beginning of the packet
207 */
208 stream->timestamp = saved_pos->current_timestamp;
209 stream_pos->offset = saved_pos->offset;
210 stream_pos->last_offset = saved_pos->offset;
211
212 stream->prev_timestamp = 0;
213 stream->prev_timestamp_end = 0;
214 stream->consumed = 0;
215
216 printf_debug("restored to cur_index = %zd and "
217 "offset = %zd, timestamp = %" PRIu64 "\n",
218 stream_pos->cur_index,
219 stream_pos->offset, stream->timestamp);
220
221 stream_read_event(saved_pos->file_stream);
222
223 /* Add to heap */
224 ret = heap_insert(iter->stream_heap,
225 saved_pos->file_stream);
226 if (ret)
227 goto error;
228 }
229 case BT_SEEK_TIME:
230 tc = iter->ctx->tc;
231
232 if (!iter_pos->u.seek_time)
233 goto error_arg;
234
235 heap_free(iter->stream_heap);
236 ret = heap_init(iter->stream_heap, 0, stream_compare);
237 if (ret < 0)
238 goto error;
239
240 /* for each trace in the trace_collection */
241 for (i = 0; i < tc->array->len; i++) {
242 struct ctf_trace *tin;
243 struct trace_descriptor *td_read;
244
245 td_read = g_ptr_array_index(tc->array, i);
246 tin = container_of(td_read, struct ctf_trace, parent);
247
248 ret = seek_ctf_trace_by_timestamp(tin,
249 iter_pos->u.seek_time,
250 iter->stream_heap);
251 if (ret < 0)
252 goto error;
253 }
254 return 0;
255 default:
256 /* not implemented */
257 goto error_arg;
258 }
259
260 return 0;
261
262 error_arg:
263 ret = -EINVAL;
264 error:
265 heap_free(iter->stream_heap);
266 if (heap_init(iter->stream_heap, 0, stream_compare) < 0) {
267 heap_free(iter->stream_heap);
268 g_free(iter->stream_heap);
269 iter->stream_heap = NULL;
270 ret = -ENOMEM;
271 }
272 return ret;
273 }
274
275 struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
276 {
277 struct bt_iter_pos *pos;
278 struct trace_collection *tc = iter->ctx->tc;
279 int i, stream_class_id, stream_id;
280
281 pos = g_new0(struct bt_iter_pos, 1);
282 pos->u.restore = g_new0(struct bt_saved_pos, 1);
283 pos->u.restore->tc = tc;
284 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
285 sizeof(struct stream_saved_pos));
286 if (!pos->u.restore->stream_saved_pos)
287 goto error;
288
289 for (i = 0; i < tc->array->len; i++) {
290 struct ctf_trace *tin;
291 struct trace_descriptor *td_read;
292
293 td_read = g_ptr_array_index(tc->array, i);
294 tin = container_of(td_read, struct ctf_trace, parent);
295
296 for (stream_class_id = 0; stream_class_id < tin->streams->len;
297 stream_class_id++) {
298 struct ctf_stream_class *stream_class;
299
300 stream_class = g_ptr_array_index(tin->streams,
301 stream_class_id);
302 for (stream_id = 0;
303 stream_id < stream_class->streams->len;
304 stream_id++) {
305 struct ctf_stream *stream;
306 struct ctf_file_stream *cfs;
307 struct stream_saved_pos saved_pos;
308
309 stream = g_ptr_array_index(
310 stream_class->streams,
311 stream_id);
312 cfs = container_of(stream,
313 struct ctf_file_stream,
314 parent);
315
316 saved_pos.file_stream = cfs;
317 saved_pos.cur_index = cfs->pos.cur_index;
318
319 /*
320 * It is possible that an event was read during
321 * the last restore, never consumed and its
322 * position saved again. For this case, we
323 * need to check if the event really was
324 * consumed by the caller otherwise it is lost.
325 */
326 if (stream->consumed)
327 saved_pos.offset = cfs->pos.offset;
328 else
329 saved_pos.offset = cfs->pos.last_offset;
330
331 saved_pos.current_timestamp = stream->timestamp;
332
333 g_array_append_val(
334 pos->u.restore->stream_saved_pos,
335 saved_pos);
336
337 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
338 "offset : %zd, "
339 "timestamp = %" PRIu64 "\n",
340 stream->stream_id, saved_pos.cur_index,
341 saved_pos.offset,
342 saved_pos.current_timestamp);
343 }
344 }
345 }
346
347 return pos;
348
349 error:
350 return NULL;
351 }
352
353 struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
354 uint64_t timestamp)
355 {
356 struct bt_iter_pos *pos;
357
358 pos = g_new0(struct bt_iter_pos, 1);
359 pos->type = BT_SEEK_TIME;
360 pos->u.seek_time = timestamp;
361 return pos;
362 }
363
364 /*
365 * babeltrace_filestream_seek: seek a filestream to given position.
366 *
367 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
368 */
369 static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
370 const struct bt_iter_pos *begin_pos,
371 unsigned long stream_id)
372 {
373 int ret = 0;
374
375 switch (begin_pos->type) {
376 case BT_SEEK_CUR:
377 /*
378 * just insert into the heap we should already know
379 * the timestamps
380 */
381 break;
382 case BT_SEEK_BEGIN:
383 file_stream->pos.packet_seek(&file_stream->pos.parent,
384 0, SEEK_SET);
385 ret = stream_read_event(file_stream);
386 break;
387 case BT_SEEK_TIME:
388 case BT_SEEK_RESTORE:
389 case BT_SEEK_END:
390 default:
391 assert(0); /* Not yet defined */
392 }
393
394 return ret;
395 }
396
397 /*
398 * bt_iter_seek: seek iterator to given position.
399 */
400 int bt_iter_seek(struct bt_iter *iter,
401 const struct bt_iter_pos *begin_pos)
402 {
403 int i, stream_id;
404 int ret = 0;
405 struct trace_collection *tc = iter->ctx->tc;
406
407 for (i = 0; i < tc->array->len; i++) {
408 struct ctf_trace *tin;
409 struct trace_descriptor *td_read;
410
411 td_read = g_ptr_array_index(tc->array, i);
412 tin = container_of(td_read, struct ctf_trace, parent);
413
414 /* Populate heap with each stream */
415 for (stream_id = 0; stream_id < tin->streams->len;
416 stream_id++) {
417 struct ctf_stream_class *stream;
418 int filenr;
419
420 stream = g_ptr_array_index(tin->streams, stream_id);
421 for (filenr = 0; filenr < stream->streams->len;
422 filenr++) {
423 struct ctf_file_stream *file_stream;
424
425 file_stream = g_ptr_array_index(stream->streams,
426 filenr);
427 ret = babeltrace_filestream_seek(file_stream, begin_pos,
428 stream_id);
429 if (ret < 0)
430 goto end;
431 }
432 }
433 }
434 end:
435 return ret;
436 }
437
438 int bt_iter_init(struct bt_iter *iter,
439 struct bt_context *ctx,
440 struct bt_iter_pos *begin_pos,
441 struct bt_iter_pos *end_pos)
442 {
443 int i, stream_id;
444 int ret = 0;
445
446 iter->stream_heap = g_new(struct ptr_heap, 1);
447 iter->end_pos = end_pos;
448 iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks));
449 iter->recalculate_dep_graph = 0;
450 iter->main_callbacks.callback = NULL;
451 iter->dep_gc = g_ptr_array_new();
452 bt_context_get(ctx);
453 iter->ctx = ctx;
454
455 ret = heap_init(iter->stream_heap, 0, stream_compare);
456 if (ret < 0)
457 goto error_heap_init;
458
459 for (i = 0; i < ctx->tc->array->len; i++) {
460 struct ctf_trace *tin;
461 struct trace_descriptor *td_read;
462
463 td_read = g_ptr_array_index(ctx->tc->array, i);
464 tin = container_of(td_read, struct ctf_trace, parent);
465
466 /* Populate heap with each stream */
467 for (stream_id = 0; stream_id < tin->streams->len;
468 stream_id++) {
469 struct ctf_stream_class *stream;
470 int filenr;
471
472 stream = g_ptr_array_index(tin->streams, stream_id);
473 if (!stream)
474 continue;
475 for (filenr = 0; filenr < stream->streams->len;
476 filenr++) {
477 struct ctf_file_stream *file_stream;
478
479 file_stream = g_ptr_array_index(stream->streams,
480 filenr);
481
482 if (begin_pos) {
483 ret = babeltrace_filestream_seek(file_stream, begin_pos,
484 stream_id);
485 if (ret == EOF) {
486 ret = 0;
487 continue;
488 } else if (ret) {
489 goto error;
490 }
491 }
492 /* Add to heap */
493 ret = heap_insert(iter->stream_heap, file_stream);
494 if (ret)
495 goto error;
496 }
497 }
498 }
499
500 return 0;
501
502 error:
503 heap_free(iter->stream_heap);
504 error_heap_init:
505 g_free(iter->stream_heap);
506 return ret;
507 }
508
509 struct bt_iter *bt_iter_create(struct bt_context *ctx,
510 struct bt_iter_pos *begin_pos,
511 struct bt_iter_pos *end_pos)
512 {
513 struct bt_iter *iter;
514 int ret;
515
516 iter = g_new0(struct bt_iter, 1);
517 ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
518 if (ret) {
519 g_free(iter);
520 return NULL;
521 }
522 return iter;
523 }
524
525 void bt_iter_fini(struct bt_iter *iter)
526 {
527 struct bt_stream_callbacks *bt_stream_cb;
528 struct bt_callback_chain *bt_chain;
529 int i, j;
530
531 if (iter->stream_heap) {
532 heap_free(iter->stream_heap);
533 g_free(iter->stream_heap);
534 }
535
536 /* free all events callbacks */
537 if (iter->main_callbacks.callback)
538 g_array_free(iter->main_callbacks.callback, TRUE);
539
540 /* free per-event callbacks */
541 for (i = 0; i < iter->callbacks->len; i++) {
542 bt_stream_cb = &g_array_index(iter->callbacks,
543 struct bt_stream_callbacks, i);
544 if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
545 continue;
546 for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) {
547 bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
548 struct bt_callback_chain, j);
549 if (bt_chain->callback) {
550 g_array_free(bt_chain->callback, TRUE);
551 }
552 }
553 g_array_free(bt_stream_cb->per_id_callbacks, TRUE);
554 }
555
556 bt_context_put(iter->ctx);
557 }
558
559 void bt_iter_destroy(struct bt_iter *iter)
560 {
561 bt_iter_fini(iter);
562 g_free(iter);
563 }
564
565 int bt_iter_next(struct bt_iter *iter)
566 {
567 struct ctf_file_stream *file_stream, *removed;
568 int ret;
569
570 file_stream = heap_maximum(iter->stream_heap);
571 if (!file_stream) {
572 /* end of file for all streams */
573 ret = 0;
574 goto end;
575 }
576
577 ret = stream_read_event(file_stream);
578 if (ret == EOF) {
579 removed = heap_remove(iter->stream_heap);
580 assert(removed == file_stream);
581 ret = 0;
582 goto end;
583 } else if (ret) {
584 goto end;
585 }
586 /* Reinsert the file stream into the heap, and rebalance. */
587 removed = heap_replace_max(iter->stream_heap, file_stream);
588 assert(removed == file_stream);
589
590 end:
591 return ret;
592 }
This page took 0.064687 seconds and 5 git commands to generate.