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