X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=lib%2Fiterator.c;h=2cb40cbc5b166a4e4884c58f4d52e817a7a7e8a0;hp=3a07c1abcf1c0e75219528899977f45d6f357199;hb=e1d01c3911e1e9a09f92afcbd9940cfc938f3d49;hpb=6cba487f031260536d6a77acde888c8b1a876fcf diff --git a/lib/iterator.c b/lib/iterator.c index 3a07c1ab..2cb40cbc 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -22,10 +22,12 @@ #include #include #include +#include #include #include #include #include +#include #include struct stream_saved_pos { @@ -87,18 +89,103 @@ void bt_iter_free_pos(struct bt_iter_pos *iter_pos) g_free(iter_pos); } +/* + * seek_file_stream_by_timestamp + * + * Browse a filestream by index, if an index contains the timestamp passed in + * argument, seek inside the corresponding packet it until we find the event we + * are looking for (either the exact timestamp or the event just after the + * timestamp). + * + * Return 0 if the seek succeded and EOF if we didn't find any packet + * containing the timestamp. + */ +static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs, + uint64_t timestamp) +{ + struct ctf_stream_pos *stream_pos; + struct packet_index *index; + int i, ret; + + stream_pos = &cfs->pos; + for (i = 0; i < stream_pos->packet_index->len; i++) { + index = &g_array_index(stream_pos->packet_index, + struct packet_index, i); + if (index->timestamp_begin >= timestamp || + index->timestamp_end <= timestamp) + continue; + + stream_pos->cur_index = i; + stream_pos->packet_seek(&stream_pos->parent, + index->offset, SEEK_SET); + while (cfs->parent.timestamp < timestamp) { + ret = stream_read_event(cfs); + if (ret < 0) + break; + } + return 0; + } + return EOF; +} + +/* + * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with + * the corresponding timestamp + * + * Return 0 on success. + * If the timestamp is not part of any file stream, return EOF to inform the + * user the timestamp is out of the scope + */ +static int seek_ctf_trace_by_timestamp(struct ctf_trace *tin, + uint64_t timestamp, struct ptr_heap *stream_heap) +{ + int i, j, ret; + int found = EOF; + + /* for each stream_class */ + for (i = 0; i < tin->streams->len; i++) { + struct ctf_stream_class *stream_class; + + stream_class = g_ptr_array_index(tin->streams, i); + /* for each file_stream */ + for (j = 0; j < stream_class->streams->len; j++) { + struct ctf_stream *stream; + struct ctf_file_stream *cfs; + + stream = g_ptr_array_index(stream_class->streams, j); + cfs = container_of(stream, struct ctf_file_stream, + parent); + ret = seek_file_stream_by_timestamp(cfs, timestamp); + if (ret == 0) { + /* Add to heap */ + ret = heap_insert(stream_heap, cfs); + if (ret) + goto error; + found = 0; + } + } + } + + return found; + +error: + return -2; +} + int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) { + struct trace_collection *tc; int i, ret; - if (iter_pos->u.restore) { - /* clear and recreate the heap */ + switch (iter_pos->type) { + case BT_SEEK_RESTORE: + if (!iter_pos->u.restore) + goto error_arg; + heap_free(iter->stream_heap); - g_free(iter->stream_heap); - iter->stream_heap = g_new(struct ptr_heap, 1); ret = heap_init(iter->stream_heap, 0, stream_compare); if (ret < 0) - goto error_heap_init; + goto error; for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len; i++) { @@ -117,12 +204,12 @@ int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) saved_pos->cur_index); stream_pos->cur_index = saved_pos->cur_index; - stream_pos->move_pos_slow(stream_pos, index->offset, - SEEK_SET); + stream_pos->packet_seek(&stream_pos->parent, + index->offset, SEEK_SET); /* * the timestamp needs to be restored after - * move_pos_slow, because this function resets + * packet_seek, because this function resets * the timestamp to the beginning of the packet */ stream->timestamp = saved_pos->current_timestamp; @@ -146,20 +233,50 @@ int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) if (ret) goto error; } - } else if (iter_pos->u.seek_time) { - /* not yet implemented */ - return -1; - } else { - /* nowhere to seek to */ - return -1; + case BT_SEEK_TIME: + tc = iter->ctx->tc; + + if (!iter_pos->u.seek_time) + goto error_arg; + + heap_free(iter->stream_heap); + ret = heap_init(iter->stream_heap, 0, stream_compare); + if (ret < 0) + goto error; + + /* for each trace in the trace_collection */ + for (i = 0; i < tc->array->len; i++) { + struct ctf_trace *tin; + struct trace_descriptor *td_read; + + td_read = g_ptr_array_index(tc->array, i); + tin = container_of(td_read, struct ctf_trace, parent); + + ret = seek_ctf_trace_by_timestamp(tin, + iter_pos->u.seek_time, + iter->stream_heap); + if (ret < 0) + goto error; + } + return 0; + default: + /* not implemented */ + goto error_arg; } return 0; + +error_arg: + ret = -EINVAL; error: heap_free(iter->stream_heap); -error_heap_init: - g_free(iter->stream_heap); - return -1; + if (heap_init(iter->stream_heap, 0, stream_compare) < 0) { + heap_free(iter->stream_heap); + g_free(iter->stream_heap); + iter->stream_heap = NULL; + ret = -ENOMEM; + } + return ret; } struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter) @@ -224,7 +341,7 @@ struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter) pos->u.restore->stream_saved_pos, saved_pos); - printf_debug("stream : %lu, cur_index : %zd, " + printf_debug("stream : %" PRIu64 ", cur_index : %zd, " "offset : %zd, " "timestamp = %" PRIu64 "\n", stream->stream_id, saved_pos.cur_index, @@ -240,6 +357,17 @@ error: return NULL; } +struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, + uint64_t timestamp) +{ + struct bt_iter_pos *pos; + + pos = g_new0(struct bt_iter_pos, 1); + pos->type = BT_SEEK_TIME; + pos->u.seek_time = timestamp; + return pos; +} + /* * babeltrace_filestream_seek: seek a filestream to given position. * @@ -259,7 +387,8 @@ static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream, */ break; case BT_SEEK_BEGIN: - file_stream->pos.move_pos_slow(&file_stream->pos, 0, SEEK_SET); + file_stream->pos.packet_seek(&file_stream->pos.parent, + 0, SEEK_SET); ret = stream_read_event(file_stream); break; case BT_SEEK_TIME: @@ -392,8 +521,10 @@ void bt_iter_destroy(struct bt_iter *iter) struct bt_callback_chain *bt_chain; int i, j; - heap_free(iter->stream_heap); - g_free(iter->stream_heap); + if (iter->stream_heap) { + heap_free(iter->stream_heap); + g_free(iter->stream_heap); + } /* free all events callbacks */ if (iter->main_callbacks.callback) @@ -449,27 +580,27 @@ end: return ret; } -int bt_iter_read_event(struct bt_iter *iter, - struct ctf_stream **stream, - struct ctf_stream_event **event) +struct bt_ctf_event *bt_iter_read_ctf_event(struct bt_iter *iter) { struct ctf_file_stream *file_stream; - int ret = 0; + struct bt_ctf_event *ret = &iter->current_ctf_event; file_stream = heap_maximum(iter->stream_heap); if (!file_stream) { /* end of file for all streams */ - ret = EOF; - goto end; + goto stop; } - *stream = &file_stream->parent; - *event = g_ptr_array_index((*stream)->events_by_id, (*stream)->event_id); + ret->stream = &file_stream->parent; + ret->event = g_ptr_array_index(ret->stream->events_by_id, + ret->stream->event_id); - if ((*stream)->stream_id > iter->callbacks->len) + if (ret->stream->stream_id > iter->callbacks->len) goto end; - process_callbacks(iter, *stream); + process_callbacks(iter, ret->stream); end: return ret; +stop: + return NULL; }