Fix: remove leftover code from seek begin (unimplemented for now)
[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_TIME:
229 tc = iter->ctx->tc;
230
231 heap_free(iter->stream_heap);
232 ret = heap_init(iter->stream_heap, 0, stream_compare);
233 if (ret < 0)
234 goto error;
235
236 /* for each trace in the trace_collection */
237 for (i = 0; i < tc->array->len; i++) {
238 struct ctf_trace *tin;
239 struct trace_descriptor *td_read;
240
241 td_read = g_ptr_array_index(tc->array, i);
242 tin = container_of(td_read, struct ctf_trace, parent);
243
244 ret = seek_ctf_trace_by_timestamp(tin,
245 iter_pos->u.seek_time,
246 iter->stream_heap);
247 if (ret < 0)
248 goto error;
249 }
250 return 0;
251 default:
252 /* not implemented */
253 return -EINVAL;
254 }
255
256 return 0;
257
258 error:
259 heap_free(iter->stream_heap);
260 if (heap_init(iter->stream_heap, 0, stream_compare) < 0) {
261 heap_free(iter->stream_heap);
262 g_free(iter->stream_heap);
263 iter->stream_heap = NULL;
264 ret = -ENOMEM;
265 }
266 return ret;
267 }
268
269 struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
270 {
271 struct bt_iter_pos *pos;
272 struct trace_collection *tc = iter->ctx->tc;
273 int i, stream_class_id, stream_id;
274
275 pos = g_new0(struct bt_iter_pos, 1);
276 pos->u.restore = g_new0(struct bt_saved_pos, 1);
277 pos->u.restore->tc = tc;
278 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
279 sizeof(struct stream_saved_pos));
280 if (!pos->u.restore->stream_saved_pos)
281 goto error;
282
283 for (i = 0; i < tc->array->len; i++) {
284 struct ctf_trace *tin;
285 struct trace_descriptor *td_read;
286
287 td_read = g_ptr_array_index(tc->array, i);
288 tin = container_of(td_read, struct ctf_trace, parent);
289
290 for (stream_class_id = 0; stream_class_id < tin->streams->len;
291 stream_class_id++) {
292 struct ctf_stream_class *stream_class;
293
294 stream_class = g_ptr_array_index(tin->streams,
295 stream_class_id);
296 for (stream_id = 0;
297 stream_id < stream_class->streams->len;
298 stream_id++) {
299 struct ctf_stream *stream;
300 struct ctf_file_stream *cfs;
301 struct stream_saved_pos saved_pos;
302
303 stream = g_ptr_array_index(
304 stream_class->streams,
305 stream_id);
306 cfs = container_of(stream,
307 struct ctf_file_stream,
308 parent);
309
310 saved_pos.file_stream = cfs;
311 saved_pos.cur_index = cfs->pos.cur_index;
312
313 /*
314 * It is possible that an event was read during
315 * the last restore, never consumed and its
316 * position saved again. For this case, we
317 * need to check if the event really was
318 * consumed by the caller otherwise it is lost.
319 */
320 if (stream->consumed)
321 saved_pos.offset = cfs->pos.offset;
322 else
323 saved_pos.offset = cfs->pos.last_offset;
324
325 saved_pos.current_timestamp = stream->timestamp;
326
327 g_array_append_val(
328 pos->u.restore->stream_saved_pos,
329 saved_pos);
330
331 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
332 "offset : %zd, "
333 "timestamp = %" PRIu64 "\n",
334 stream->stream_id, saved_pos.cur_index,
335 saved_pos.offset,
336 saved_pos.current_timestamp);
337 }
338 }
339 }
340
341 return pos;
342
343 error:
344 return NULL;
345 }
346
347 struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
348 uint64_t timestamp)
349 {
350 struct bt_iter_pos *pos;
351
352 pos = g_new0(struct bt_iter_pos, 1);
353 pos->type = BT_SEEK_TIME;
354 pos->u.seek_time = timestamp;
355 return pos;
356 }
357
358 /*
359 * babeltrace_filestream_seek: seek a filestream to given position.
360 *
361 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
362 */
363 static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
364 const struct bt_iter_pos *begin_pos,
365 unsigned long stream_id)
366 {
367 int ret = 0;
368
369 switch (begin_pos->type) {
370 case BT_SEEK_CUR:
371 /*
372 * just insert into the heap we should already know
373 * the timestamps
374 */
375 break;
376 case BT_SEEK_BEGIN:
377 file_stream->pos.packet_seek(&file_stream->pos.parent,
378 0, SEEK_SET);
379 ret = stream_read_event(file_stream);
380 break;
381 case BT_SEEK_TIME:
382 case BT_SEEK_RESTORE:
383 case BT_SEEK_END:
384 default:
385 assert(0); /* Not yet defined */
386 }
387
388 return ret;
389 }
390
391 /*
392 * bt_iter_seek: seek iterator to given position.
393 */
394 int bt_iter_seek(struct bt_iter *iter,
395 const struct bt_iter_pos *begin_pos)
396 {
397 int i, stream_id;
398 int ret = 0;
399 struct trace_collection *tc = iter->ctx->tc;
400
401 for (i = 0; i < tc->array->len; i++) {
402 struct ctf_trace *tin;
403 struct trace_descriptor *td_read;
404
405 td_read = g_ptr_array_index(tc->array, i);
406 tin = container_of(td_read, struct ctf_trace, parent);
407
408 /* Populate heap with each stream */
409 for (stream_id = 0; stream_id < tin->streams->len;
410 stream_id++) {
411 struct ctf_stream_class *stream;
412 int filenr;
413
414 stream = g_ptr_array_index(tin->streams, stream_id);
415 for (filenr = 0; filenr < stream->streams->len;
416 filenr++) {
417 struct ctf_file_stream *file_stream;
418
419 file_stream = g_ptr_array_index(stream->streams,
420 filenr);
421 ret = babeltrace_filestream_seek(file_stream, begin_pos,
422 stream_id);
423 if (ret < 0)
424 goto end;
425 }
426 }
427 }
428 end:
429 return ret;
430 }
431
432 int bt_iter_init(struct bt_iter *iter,
433 struct bt_context *ctx,
434 struct bt_iter_pos *begin_pos,
435 struct bt_iter_pos *end_pos)
436 {
437 int i, stream_id;
438 int ret = 0;
439
440 iter->stream_heap = g_new(struct ptr_heap, 1);
441 iter->end_pos = end_pos;
442 bt_context_get(ctx);
443 iter->ctx = ctx;
444
445 ret = heap_init(iter->stream_heap, 0, stream_compare);
446 if (ret < 0)
447 goto error_heap_init;
448
449 for (i = 0; i < ctx->tc->array->len; i++) {
450 struct ctf_trace *tin;
451 struct trace_descriptor *td_read;
452
453 td_read = g_ptr_array_index(ctx->tc->array, i);
454 tin = container_of(td_read, struct ctf_trace, parent);
455
456 /* Populate heap with each stream */
457 for (stream_id = 0; stream_id < tin->streams->len;
458 stream_id++) {
459 struct ctf_stream_class *stream;
460 int filenr;
461
462 stream = g_ptr_array_index(tin->streams, stream_id);
463 if (!stream)
464 continue;
465 for (filenr = 0; filenr < stream->streams->len;
466 filenr++) {
467 struct ctf_file_stream *file_stream;
468
469 file_stream = g_ptr_array_index(stream->streams,
470 filenr);
471
472 if (begin_pos) {
473 ret = babeltrace_filestream_seek(file_stream, begin_pos,
474 stream_id);
475 if (ret == EOF) {
476 ret = 0;
477 continue;
478 } else if (ret) {
479 goto error;
480 }
481 }
482 /* Add to heap */
483 ret = heap_insert(iter->stream_heap, file_stream);
484 if (ret)
485 goto error;
486 }
487 }
488 }
489
490 return 0;
491
492 error:
493 heap_free(iter->stream_heap);
494 error_heap_init:
495 g_free(iter->stream_heap);
496 return ret;
497 }
498
499 struct bt_iter *bt_iter_create(struct bt_context *ctx,
500 struct bt_iter_pos *begin_pos,
501 struct bt_iter_pos *end_pos)
502 {
503 struct bt_iter *iter;
504 int ret;
505
506 iter = g_new0(struct bt_iter, 1);
507 ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
508 if (ret) {
509 g_free(iter);
510 return NULL;
511 }
512 return iter;
513 }
514
515 void bt_iter_fini(struct bt_iter *iter)
516 {
517 if (iter->stream_heap) {
518 heap_free(iter->stream_heap);
519 g_free(iter->stream_heap);
520 }
521 bt_context_put(iter->ctx);
522 }
523
524 void bt_iter_destroy(struct bt_iter *iter)
525 {
526 bt_iter_fini(iter);
527 g_free(iter);
528 }
529
530 int bt_iter_next(struct bt_iter *iter)
531 {
532 struct ctf_file_stream *file_stream, *removed;
533 int ret;
534
535 file_stream = heap_maximum(iter->stream_heap);
536 if (!file_stream) {
537 /* end of file for all streams */
538 ret = 0;
539 goto end;
540 }
541
542 ret = stream_read_event(file_stream);
543 if (ret == EOF) {
544 removed = heap_remove(iter->stream_heap);
545 assert(removed == file_stream);
546 ret = 0;
547 goto end;
548 } else if (ret) {
549 goto end;
550 }
551 /* Reinsert the file stream into the heap, and rebalance. */
552 removed = heap_replace_max(iter->stream_heap, file_stream);
553 assert(removed == file_stream);
554
555 end:
556 return ret;
557 }
This page took 0.040606 seconds and 4 git commands to generate.