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