| 1 | /* |
| 2 | * ctf/iterator.c |
| 3 | * |
| 4 | * Babeltrace Library |
| 5 | * |
| 6 | * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * Julien Desfossez <julien.desfossez@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <babeltrace/babeltrace.h> |
| 31 | #include <babeltrace/format.h> |
| 32 | #include <babeltrace/ctf/events.h> |
| 33 | #include <babeltrace/ctf-ir/metadata.h> |
| 34 | #include <babeltrace/prio_heap.h> |
| 35 | #include <babeltrace/iterator-internal.h> |
| 36 | #include <babeltrace/ctf/events-internal.h> |
| 37 | #include <babeltrace/ctf/metadata.h> |
| 38 | #include <glib.h> |
| 39 | |
| 40 | #include "events-private.h" |
| 41 | |
| 42 | struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx, |
| 43 | const struct bt_iter_pos *begin_pos, |
| 44 | const struct bt_iter_pos *end_pos) |
| 45 | { |
| 46 | struct bt_ctf_iter *iter; |
| 47 | int ret; |
| 48 | |
| 49 | if (!ctx) |
| 50 | return NULL; |
| 51 | |
| 52 | iter = g_new0(struct bt_ctf_iter, 1); |
| 53 | ret = bt_iter_init(&iter->parent, ctx, begin_pos, end_pos); |
| 54 | if (ret) { |
| 55 | g_free(iter); |
| 56 | return NULL; |
| 57 | } |
| 58 | iter->callbacks = g_array_new(FALSE, TRUE, |
| 59 | sizeof(struct bt_stream_callbacks)); |
| 60 | iter->recalculate_dep_graph = 0; |
| 61 | iter->main_callbacks.callback = NULL; |
| 62 | iter->dep_gc = g_ptr_array_new(); |
| 63 | return iter; |
| 64 | } |
| 65 | |
| 66 | void bt_ctf_iter_destroy(struct bt_ctf_iter *iter) |
| 67 | { |
| 68 | struct bt_stream_callbacks *bt_stream_cb; |
| 69 | struct bt_callback_chain *bt_chain; |
| 70 | int i, j; |
| 71 | |
| 72 | assert(iter); |
| 73 | |
| 74 | /* free all events callbacks */ |
| 75 | if (iter->main_callbacks.callback) |
| 76 | g_array_free(iter->main_callbacks.callback, TRUE); |
| 77 | |
| 78 | /* free per-event callbacks */ |
| 79 | for (i = 0; i < iter->callbacks->len; i++) { |
| 80 | bt_stream_cb = &g_array_index(iter->callbacks, |
| 81 | struct bt_stream_callbacks, i); |
| 82 | if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks) |
| 83 | continue; |
| 84 | for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) { |
| 85 | bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks, |
| 86 | struct bt_callback_chain, j); |
| 87 | if (bt_chain->callback) { |
| 88 | g_array_free(bt_chain->callback, TRUE); |
| 89 | } |
| 90 | } |
| 91 | g_array_free(bt_stream_cb->per_id_callbacks, TRUE); |
| 92 | } |
| 93 | g_array_free(iter->callbacks, TRUE); |
| 94 | g_ptr_array_free(iter->dep_gc, TRUE); |
| 95 | |
| 96 | bt_iter_fini(&iter->parent); |
| 97 | g_free(iter); |
| 98 | } |
| 99 | |
| 100 | struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter) |
| 101 | { |
| 102 | if (!iter) |
| 103 | return NULL; |
| 104 | |
| 105 | return &iter->parent; |
| 106 | } |
| 107 | |
| 108 | struct bt_ctf_event *bt_ctf_iter_read_event_flags(struct bt_ctf_iter *iter, |
| 109 | int *flags) |
| 110 | { |
| 111 | struct ctf_file_stream *file_stream; |
| 112 | struct bt_ctf_event *ret; |
| 113 | struct ctf_stream_definition *stream; |
| 114 | struct packet_index *packet_index; |
| 115 | |
| 116 | /* |
| 117 | * We do not want to fail for any other reason than end of |
| 118 | * trace, hence the assert. |
| 119 | */ |
| 120 | assert(iter); |
| 121 | |
| 122 | if (flags) |
| 123 | *flags = 0; |
| 124 | |
| 125 | ret = &iter->current_ctf_event; |
| 126 | file_stream = bt_heap_maximum(iter->parent.stream_heap); |
| 127 | if (!file_stream) { |
| 128 | /* end of file for all streams */ |
| 129 | goto stop; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * If the packet is empty (contains only headers or is of size 0), the |
| 134 | * caller has to know that we can't read the current event and we need |
| 135 | * to do a bt_iter_next. |
| 136 | */ |
| 137 | if (file_stream->pos.data_offset == file_stream->pos.content_size |
| 138 | || file_stream->pos.content_size == 0) { |
| 139 | /* More events may come. */ |
| 140 | ret = NULL; |
| 141 | if (flags) |
| 142 | *flags |= BT_ITER_FLAG_RETRY; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | stream = &file_stream->parent; |
| 147 | if (iter->parent.end_pos && |
| 148 | iter->parent.end_pos->type == BT_SEEK_TIME && |
| 149 | stream->real_timestamp > iter->parent.end_pos->u.seek_time) { |
| 150 | goto stop; |
| 151 | } |
| 152 | ret->parent = g_ptr_array_index(stream->events_by_id, |
| 153 | stream->event_id); |
| 154 | |
| 155 | if (!file_stream->pos.packet_index) |
| 156 | packet_index = NULL; |
| 157 | else |
| 158 | packet_index = &g_array_index(file_stream->pos.packet_index, |
| 159 | struct packet_index, file_stream->pos.cur_index); |
| 160 | iter->events_lost = 0; |
| 161 | if (packet_index && packet_index->events_discarded > |
| 162 | file_stream->pos.last_events_discarded) { |
| 163 | if (flags) |
| 164 | *flags |= BT_ITER_FLAG_LOST_EVENTS; |
| 165 | iter->events_lost += packet_index->events_discarded - |
| 166 | file_stream->pos.last_events_discarded; |
| 167 | file_stream->pos.last_events_discarded = |
| 168 | packet_index->events_discarded; |
| 169 | } |
| 170 | |
| 171 | if (ret->parent->stream->stream_id > iter->callbacks->len) |
| 172 | goto end; |
| 173 | |
| 174 | process_callbacks(iter, ret->parent->stream); |
| 175 | |
| 176 | end: |
| 177 | return ret; |
| 178 | stop: |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter) |
| 183 | { |
| 184 | return bt_ctf_iter_read_event_flags(iter, NULL); |
| 185 | } |
| 186 | |
| 187 | uint64_t bt_ctf_get_lost_events_count(struct bt_ctf_iter *iter) |
| 188 | { |
| 189 | if (!iter) |
| 190 | return -1ULL; |
| 191 | |
| 192 | return iter->events_lost; |
| 193 | } |