| 1 | /* |
| 2 | * stream.c |
| 3 | * |
| 4 | * Babeltrace trace IR - Stream |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@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 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "STREAM" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/assert-pre-internal.h> |
| 33 | #include <babeltrace/trace-ir/stream.h> |
| 34 | #include <babeltrace/trace-ir/stream-internal.h> |
| 35 | #include <babeltrace/trace-ir/stream-class.h> |
| 36 | #include <babeltrace/trace-ir/stream-class-internal.h> |
| 37 | #include <babeltrace/trace-ir/trace.h> |
| 38 | #include <babeltrace/trace-ir/trace-internal.h> |
| 39 | #include <babeltrace/trace-ir/packet-internal.h> |
| 40 | #include <babeltrace/ref.h> |
| 41 | #include <babeltrace/compiler-internal.h> |
| 42 | #include <babeltrace/align-internal.h> |
| 43 | #include <babeltrace/assert-internal.h> |
| 44 | #include <babeltrace/property-internal.h> |
| 45 | #include <inttypes.h> |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | #define BT_ASSERT_PRE_STREAM_HOT(_stream) \ |
| 49 | BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream)) |
| 50 | |
| 51 | static |
| 52 | void destroy_stream(struct bt_object *obj) |
| 53 | { |
| 54 | struct bt_stream *stream = (void *) obj; |
| 55 | |
| 56 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
| 57 | |
| 58 | if (stream->name.str) { |
| 59 | g_string_free(stream->name.str, TRUE); |
| 60 | } |
| 61 | |
| 62 | bt_object_pool_finalize(&stream->packet_pool); |
| 63 | g_free(stream); |
| 64 | } |
| 65 | |
| 66 | static |
| 67 | void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream) |
| 68 | { |
| 69 | bt_packet_destroy(packet); |
| 70 | } |
| 71 | |
| 72 | BT_ASSERT_PRE_FUNC |
| 73 | static inline |
| 74 | bool stream_id_is_unique(struct bt_trace *trace, |
| 75 | struct bt_stream_class *stream_class, uint64_t id) |
| 76 | { |
| 77 | uint64_t i; |
| 78 | bool is_unique = true; |
| 79 | |
| 80 | for (i = 0; i < trace->streams->len; i++) { |
| 81 | struct bt_stream *stream = trace->streams->pdata[i]; |
| 82 | |
| 83 | if (stream->class != stream_class) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (stream->id == id) { |
| 88 | is_unique = false; |
| 89 | goto end; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | end: |
| 94 | return is_unique; |
| 95 | } |
| 96 | |
| 97 | static |
| 98 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
| 99 | uint64_t id) |
| 100 | { |
| 101 | int ret; |
| 102 | struct bt_stream *stream; |
| 103 | struct bt_trace *trace; |
| 104 | |
| 105 | BT_ASSERT(stream_class); |
| 106 | trace = bt_stream_class_borrow_trace_inline(stream_class); |
| 107 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), |
| 108 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); |
| 109 | BT_ASSERT_PRE(!trace->is_static, |
| 110 | "Trace is static: %![trace-]+t", trace); |
| 111 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, |
| 112 | trace, id); |
| 113 | stream = g_new0(struct bt_stream, 1); |
| 114 | if (!stream) { |
| 115 | BT_LOGE_STR("Failed to allocate one stream."); |
| 116 | goto error; |
| 117 | } |
| 118 | |
| 119 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
| 120 | stream->name.str = g_string_new(NULL); |
| 121 | if (!stream->name.str) { |
| 122 | BT_LOGE_STR("Failed to allocate a GString."); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | stream->id = id; |
| 127 | ret = bt_object_pool_initialize(&stream->packet_pool, |
| 128 | (bt_object_pool_new_object_func) bt_packet_new, |
| 129 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, |
| 130 | stream); |
| 131 | if (ret) { |
| 132 | BT_LOGE("Failed to initialize packet pool: ret=%d", ret); |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | stream->class = stream_class; |
| 137 | bt_trace_add_stream(trace, stream); |
| 138 | bt_stream_class_freeze(stream_class); |
| 139 | BT_LIB_LOGD("Created stream object: %!+s", stream); |
| 140 | goto end; |
| 141 | |
| 142 | error: |
| 143 | BT_PUT(stream); |
| 144 | |
| 145 | end: |
| 146 | return stream; |
| 147 | } |
| 148 | |
| 149 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class) |
| 150 | { |
| 151 | uint64_t id; |
| 152 | |
| 153 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
| 154 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, |
| 155 | "Stream class does not automatically assigns stream IDs: " |
| 156 | "%![sc-]+S", stream_class); |
| 157 | id = bt_trace_get_automatic_stream_id( |
| 158 | bt_stream_class_borrow_trace_inline(stream_class), |
| 159 | stream_class); |
| 160 | return create_stream_with_id(stream_class, id); |
| 161 | } |
| 162 | |
| 163 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
| 164 | uint64_t id) |
| 165 | { |
| 166 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, |
| 167 | "Stream class automatically assigns stream IDs: " |
| 168 | "%![sc-]+S", stream_class); |
| 169 | return create_stream_with_id(stream_class, id); |
| 170 | } |
| 171 | |
| 172 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
| 173 | { |
| 174 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 175 | return stream->class; |
| 176 | } |
| 177 | |
| 178 | const char *bt_stream_get_name(struct bt_stream *stream) |
| 179 | { |
| 180 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); |
| 181 | return stream->name.value; |
| 182 | } |
| 183 | |
| 184 | int bt_stream_set_name(struct bt_stream *stream, const char *name) |
| 185 | { |
| 186 | BT_ASSERT_PRE_NON_NULL(stream, "Clock class"); |
| 187 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 188 | BT_ASSERT_PRE_STREAM_HOT(stream); |
| 189 | g_string_assign(stream->name.str, name); |
| 190 | stream->name.value = stream->name.str->str; |
| 191 | BT_LIB_LOGV("Set stream class's name: %!+S", stream); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | uint64_t bt_stream_get_id(struct bt_stream *stream) |
| 196 | { |
| 197 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); |
| 198 | return stream->id; |
| 199 | } |
| 200 | |
| 201 | BT_HIDDEN |
| 202 | void _bt_stream_freeze(struct bt_stream *stream) |
| 203 | { |
| 204 | /* The field classes and default clock class are already frozen */ |
| 205 | BT_ASSERT(stream); |
| 206 | BT_LIB_LOGD("Freezing stream: %!+s", stream); |
| 207 | stream->frozen = true; |
| 208 | } |