Commit | Line | Data |
---|---|---|
6f3077a2 JD |
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> | |
95d36295 | 23 | #include <babeltrace/context.h> |
08c22d05 | 24 | #include <babeltrace/context-internal.h> |
6f3077a2 | 25 | #include <babeltrace/iterator-internal.h> |
6204d33c | 26 | #include <babeltrace/iterator.h> |
6f3077a2 | 27 | #include <babeltrace/prio_heap.h> |
e4195791 | 28 | #include <babeltrace/ctf/metadata.h> |
9843982d | 29 | #include <babeltrace/ctf/events.h> |
90fcbacc | 30 | #include <inttypes.h> |
6f3077a2 JD |
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. */ | |
90fcbacc | 40 | uint64_t current_timestamp; |
6f3077a2 JD |
41 | }; |
42 | ||
e8c92a62 | 43 | struct bt_saved_pos { |
6f3077a2 JD |
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) { | |
3394d22e | 56 | fprintf(stderr, "[error] Reading event failed.\n"); |
6f3077a2 JD |
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 | ||
90fcbacc JD |
75 | void bt_iter_free_pos(struct bt_iter_pos *iter_pos) |
76 | { | |
6cba487f MD |
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); | |
90fcbacc | 85 | } |
6cba487f | 86 | g_free(iter_pos->u.restore); |
90fcbacc | 87 | } |
6cba487f | 88 | g_free(iter_pos); |
90fcbacc JD |
89 | } |
90 | ||
dc81689e JD |
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); | |
48d5f76c | 113 | if (index->timestamp_end <= timestamp) |
dc81689e JD |
114 | continue; |
115 | ||
20d0dcf9 | 116 | stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET); |
dc81689e JD |
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 | ||
90fcbacc JD |
171 | int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) |
172 | { | |
dc81689e | 173 | struct trace_collection *tc; |
90fcbacc JD |
174 | int i, ret; |
175 | ||
dc81689e JD |
176 | switch (iter_pos->type) { |
177 | case BT_SEEK_RESTORE: | |
178 | if (!iter_pos->u.restore) | |
7344374e | 179 | return -EINVAL; |
dc81689e | 180 | |
90fcbacc | 181 | heap_free(iter->stream_heap); |
90fcbacc JD |
182 | ret = heap_init(iter->stream_heap, 0, stream_compare); |
183 | if (ret < 0) | |
dc81689e | 184 | goto error; |
90fcbacc JD |
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; | |
90fcbacc JD |
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; | |
90fcbacc | 197 | |
06789ffd | 198 | stream_pos->packet_seek(&stream_pos->parent, |
20d0dcf9 | 199 | saved_pos->cur_index, SEEK_SET); |
90fcbacc JD |
200 | |
201 | /* | |
202 | * the timestamp needs to be restored after | |
06789ffd | 203 | * packet_seek, because this function resets |
90fcbacc JD |
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 | } | |
dc81689e JD |
227 | case BT_SEEK_TIME: |
228 | tc = iter->ctx->tc; | |
229 | ||
dc81689e JD |
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 */ | |
7344374e | 252 | return -EINVAL; |
90fcbacc JD |
253 | } |
254 | ||
255 | return 0; | |
dc81689e | 256 | |
90fcbacc JD |
257 | error: |
258 | heap_free(iter->stream_heap); | |
dc81689e JD |
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; | |
90fcbacc JD |
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); | |
90fcbacc | 275 | pos->u.restore = g_new0(struct bt_saved_pos, 1); |
90fcbacc JD |
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 | ||
363402af | 330 | printf_debug("stream : %" PRIu64 ", cur_index : %zd, " |
90fcbacc JD |
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 | ||
dc81689e JD |
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 | ||
6f3077a2 JD |
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, | |
e8c92a62 | 363 | const struct bt_iter_pos *begin_pos, |
6f3077a2 JD |
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: | |
06789ffd | 376 | file_stream->pos.packet_seek(&file_stream->pos.parent, |
d6425aaf | 377 | 0, SEEK_SET); |
6f3077a2 JD |
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 | /* | |
e8c92a62 | 391 | * bt_iter_seek: seek iterator to given position. |
6f3077a2 | 392 | */ |
e8c92a62 JD |
393 | int bt_iter_seek(struct bt_iter *iter, |
394 | const struct bt_iter_pos *begin_pos) | |
6f3077a2 JD |
395 | { |
396 | int i, stream_id; | |
397 | int ret = 0; | |
95d36295 | 398 | struct trace_collection *tc = iter->ctx->tc; |
6f3077a2 JD |
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 | ||
e4195791 MD |
431 | int bt_iter_init(struct bt_iter *iter, |
432 | struct bt_context *ctx, | |
e8c92a62 JD |
433 | struct bt_iter_pos *begin_pos, |
434 | struct bt_iter_pos *end_pos) | |
6f3077a2 JD |
435 | { |
436 | int i, stream_id; | |
437 | int ret = 0; | |
6f3077a2 | 438 | |
6f3077a2 | 439 | iter->stream_heap = g_new(struct ptr_heap, 1); |
6f3077a2 | 440 | iter->end_pos = end_pos; |
6cba487f | 441 | bt_context_get(ctx); |
95d36295 | 442 | iter->ctx = ctx; |
6f3077a2 JD |
443 | |
444 | ret = heap_init(iter->stream_heap, 0, stream_compare); | |
445 | if (ret < 0) | |
446 | goto error_heap_init; | |
447 | ||
95d36295 | 448 | for (i = 0; i < ctx->tc->array->len; i++) { |
6f3077a2 JD |
449 | struct ctf_trace *tin; |
450 | struct trace_descriptor *td_read; | |
451 | ||
95d36295 | 452 | td_read = g_ptr_array_index(ctx->tc->array, i); |
6f3077a2 JD |
453 | tin = container_of(td_read, struct ctf_trace, parent); |
454 | ||
455 | /* Populate heap with each stream */ | |
456 | for (stream_id = 0; stream_id < tin->streams->len; | |
457 | stream_id++) { | |
458 | struct ctf_stream_class *stream; | |
459 | int filenr; | |
460 | ||
461 | stream = g_ptr_array_index(tin->streams, stream_id); | |
462 | if (!stream) | |
463 | continue; | |
464 | for (filenr = 0; filenr < stream->streams->len; | |
465 | filenr++) { | |
466 | struct ctf_file_stream *file_stream; | |
467 | ||
468 | file_stream = g_ptr_array_index(stream->streams, | |
469 | filenr); | |
470 | ||
471 | if (begin_pos) { | |
472 | ret = babeltrace_filestream_seek(file_stream, begin_pos, | |
473 | stream_id); | |
474 | if (ret == EOF) { | |
475 | ret = 0; | |
476 | continue; | |
477 | } else if (ret) { | |
478 | goto error; | |
479 | } | |
480 | } | |
481 | /* Add to heap */ | |
482 | ret = heap_insert(iter->stream_heap, file_stream); | |
483 | if (ret) | |
484 | goto error; | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
e4195791 | 489 | return 0; |
6f3077a2 JD |
490 | |
491 | error: | |
492 | heap_free(iter->stream_heap); | |
493 | error_heap_init: | |
494 | g_free(iter->stream_heap); | |
e4195791 | 495 | return ret; |
6f3077a2 JD |
496 | } |
497 | ||
e4195791 MD |
498 | struct bt_iter *bt_iter_create(struct bt_context *ctx, |
499 | struct bt_iter_pos *begin_pos, | |
500 | struct bt_iter_pos *end_pos) | |
501 | { | |
502 | struct bt_iter *iter; | |
503 | int ret; | |
504 | ||
505 | iter = g_new0(struct bt_iter, 1); | |
506 | ret = bt_iter_init(iter, ctx, begin_pos, end_pos); | |
507 | if (ret) { | |
508 | g_free(iter); | |
509 | return NULL; | |
510 | } | |
511 | return iter; | |
512 | } | |
513 | ||
514 | void bt_iter_fini(struct bt_iter *iter) | |
6f3077a2 | 515 | { |
dc81689e JD |
516 | if (iter->stream_heap) { |
517 | heap_free(iter->stream_heap); | |
518 | g_free(iter->stream_heap); | |
519 | } | |
95d36295 | 520 | bt_context_put(iter->ctx); |
e4195791 | 521 | } |
95d36295 | 522 | |
e4195791 MD |
523 | void bt_iter_destroy(struct bt_iter *iter) |
524 | { | |
525 | bt_iter_fini(iter); | |
90fcbacc | 526 | g_free(iter); |
6f3077a2 JD |
527 | } |
528 | ||
e8c92a62 | 529 | int bt_iter_next(struct bt_iter *iter) |
6f3077a2 JD |
530 | { |
531 | struct ctf_file_stream *file_stream, *removed; | |
532 | int ret; | |
533 | ||
534 | file_stream = heap_maximum(iter->stream_heap); | |
535 | if (!file_stream) { | |
536 | /* end of file for all streams */ | |
537 | ret = 0; | |
538 | goto end; | |
539 | } | |
540 | ||
541 | ret = stream_read_event(file_stream); | |
542 | if (ret == EOF) { | |
543 | removed = heap_remove(iter->stream_heap); | |
544 | assert(removed == file_stream); | |
545 | ret = 0; | |
546 | goto end; | |
547 | } else if (ret) { | |
548 | goto end; | |
549 | } | |
550 | /* Reinsert the file stream into the heap, and rebalance. */ | |
551 | removed = heap_replace_max(iter->stream_heap, file_stream); | |
552 | assert(removed == file_stream); | |
553 | ||
554 | end: | |
555 | return ret; | |
556 | } |