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