| 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 | |
| 22 | #include <babeltrace/babeltrace.h> |
| 23 | #include <babeltrace/format.h> |
| 24 | #include <babeltrace/ctf/events.h> |
| 25 | #include <babeltrace/ctf-ir/metadata.h> |
| 26 | #include <babeltrace/prio_heap.h> |
| 27 | #include <babeltrace/iterator-internal.h> |
| 28 | #include <babeltrace/ctf/events-internal.h> |
| 29 | #include <babeltrace/ctf/metadata.h> |
| 30 | #include <glib.h> |
| 31 | |
| 32 | #include "events-private.h" |
| 33 | |
| 34 | struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx, |
| 35 | const struct bt_iter_pos *begin_pos, |
| 36 | const struct bt_iter_pos *end_pos) |
| 37 | { |
| 38 | struct bt_ctf_iter *iter; |
| 39 | int ret; |
| 40 | |
| 41 | if (!ctx) |
| 42 | return NULL; |
| 43 | |
| 44 | iter = g_new0(struct bt_ctf_iter, 1); |
| 45 | ret = bt_iter_init(&iter->parent, ctx, begin_pos, end_pos); |
| 46 | if (ret) { |
| 47 | g_free(iter); |
| 48 | return NULL; |
| 49 | } |
| 50 | iter->callbacks = g_array_new(FALSE, TRUE, |
| 51 | sizeof(struct bt_stream_callbacks)); |
| 52 | iter->recalculate_dep_graph = 0; |
| 53 | iter->main_callbacks.callback = NULL; |
| 54 | iter->dep_gc = g_ptr_array_new(); |
| 55 | return iter; |
| 56 | } |
| 57 | |
| 58 | void bt_ctf_iter_destroy(struct bt_ctf_iter *iter) |
| 59 | { |
| 60 | struct bt_stream_callbacks *bt_stream_cb; |
| 61 | struct bt_callback_chain *bt_chain; |
| 62 | int i, j; |
| 63 | |
| 64 | assert(iter); |
| 65 | |
| 66 | /* free all events callbacks */ |
| 67 | if (iter->main_callbacks.callback) |
| 68 | g_array_free(iter->main_callbacks.callback, TRUE); |
| 69 | |
| 70 | /* free per-event callbacks */ |
| 71 | for (i = 0; i < iter->callbacks->len; i++) { |
| 72 | bt_stream_cb = &g_array_index(iter->callbacks, |
| 73 | struct bt_stream_callbacks, i); |
| 74 | if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks) |
| 75 | continue; |
| 76 | for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) { |
| 77 | bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks, |
| 78 | struct bt_callback_chain, j); |
| 79 | if (bt_chain->callback) { |
| 80 | g_array_free(bt_chain->callback, TRUE); |
| 81 | } |
| 82 | } |
| 83 | g_array_free(bt_stream_cb->per_id_callbacks, TRUE); |
| 84 | } |
| 85 | g_array_free(iter->callbacks, TRUE); |
| 86 | g_ptr_array_free(iter->dep_gc, TRUE); |
| 87 | |
| 88 | bt_iter_fini(&iter->parent); |
| 89 | g_free(iter); |
| 90 | } |
| 91 | |
| 92 | struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter) |
| 93 | { |
| 94 | if (!iter) |
| 95 | return NULL; |
| 96 | |
| 97 | return &iter->parent; |
| 98 | } |
| 99 | |
| 100 | struct bt_ctf_event *bt_ctf_iter_read_event_flags(struct bt_ctf_iter *iter, |
| 101 | int *flags) |
| 102 | { |
| 103 | struct ctf_file_stream *file_stream; |
| 104 | struct bt_ctf_event *ret; |
| 105 | struct ctf_stream_definition *stream; |
| 106 | struct packet_index *packet_index; |
| 107 | |
| 108 | /* |
| 109 | * We do not want to fail for any other reason than end of |
| 110 | * trace, hence the assert. |
| 111 | */ |
| 112 | assert(iter); |
| 113 | |
| 114 | ret = &iter->current_ctf_event; |
| 115 | file_stream = heap_maximum(iter->parent.stream_heap); |
| 116 | if (!file_stream) { |
| 117 | /* end of file for all streams */ |
| 118 | goto stop; |
| 119 | } |
| 120 | stream = &file_stream->parent; |
| 121 | ret->parent = g_ptr_array_index(stream->events_by_id, |
| 122 | stream->event_id); |
| 123 | |
| 124 | if (flags) |
| 125 | *flags = 0; |
| 126 | if (!file_stream->pos.packet_cycles_index) |
| 127 | packet_index = NULL; |
| 128 | else |
| 129 | packet_index = &g_array_index(file_stream->pos.packet_cycles_index, |
| 130 | struct packet_index, file_stream->pos.cur_index); |
| 131 | iter->events_lost = 0; |
| 132 | if (packet_index && packet_index->events_discarded > |
| 133 | file_stream->pos.last_events_discarded) { |
| 134 | if (flags) |
| 135 | *flags |= BT_ITER_FLAG_LOST_EVENTS; |
| 136 | iter->events_lost += packet_index->events_discarded - |
| 137 | file_stream->pos.last_events_discarded; |
| 138 | file_stream->pos.last_events_discarded = |
| 139 | packet_index->events_discarded; |
| 140 | } |
| 141 | |
| 142 | if (ret->parent->stream->stream_id > iter->callbacks->len) |
| 143 | goto end; |
| 144 | |
| 145 | process_callbacks(iter, ret->parent->stream); |
| 146 | |
| 147 | end: |
| 148 | return ret; |
| 149 | stop: |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter) |
| 154 | { |
| 155 | return bt_ctf_iter_read_event_flags(iter, NULL); |
| 156 | } |
| 157 | |
| 158 | uint64_t bt_ctf_get_lost_events_count(struct bt_ctf_iter *iter) |
| 159 | { |
| 160 | if (!iter) |
| 161 | return -1ULL; |
| 162 | |
| 163 | return iter->events_lost; |
| 164 | } |