| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define BT_LOG_TAG "LIB/STREAM" |
| 25 | #include "lib/logging.h" |
| 26 | |
| 27 | #include "lib/assert-pre.h" |
| 28 | #include <babeltrace2/trace-ir/stream-const.h> |
| 29 | #include <babeltrace2/trace-ir/stream.h> |
| 30 | #include <babeltrace2/trace-ir/stream-class.h> |
| 31 | #include <babeltrace2/trace-ir/trace.h> |
| 32 | #include "compat/compiler.h" |
| 33 | #include "common/align.h" |
| 34 | #include "common/assert.h" |
| 35 | #include "lib/property.h" |
| 36 | #include <inttypes.h> |
| 37 | #include <unistd.h> |
| 38 | |
| 39 | #include "packet.h" |
| 40 | #include "stream-class.h" |
| 41 | #include "stream.h" |
| 42 | #include "trace.h" |
| 43 | #include "lib/value.h" |
| 44 | #include "lib/func-status.h" |
| 45 | |
| 46 | #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \ |
| 47 | BT_ASSERT_PRE_DEV_HOT((_stream), "Stream", ": %!+s", (_stream)) |
| 48 | |
| 49 | static |
| 50 | void destroy_stream(struct bt_object *obj) |
| 51 | { |
| 52 | struct bt_stream *stream = (void *) obj; |
| 53 | |
| 54 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
| 55 | BT_OBJECT_PUT_REF_AND_RESET(stream->user_attributes); |
| 56 | |
| 57 | if (stream->name.str) { |
| 58 | g_string_free(stream->name.str, TRUE); |
| 59 | stream->name.str = NULL; |
| 60 | stream->name.value = NULL; |
| 61 | } |
| 62 | |
| 63 | BT_LOGD_STR("Putting stream's class."); |
| 64 | bt_object_put_ref(stream->class); |
| 65 | bt_object_pool_finalize(&stream->packet_pool); |
| 66 | g_free(stream); |
| 67 | } |
| 68 | |
| 69 | static |
| 70 | void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream) |
| 71 | { |
| 72 | bt_packet_destroy(packet); |
| 73 | } |
| 74 | |
| 75 | static inline |
| 76 | bool stream_id_is_unique(struct bt_trace *trace, |
| 77 | struct bt_stream_class *stream_class, uint64_t id) |
| 78 | { |
| 79 | uint64_t i; |
| 80 | bool is_unique = true; |
| 81 | |
| 82 | for (i = 0; i < trace->streams->len; i++) { |
| 83 | struct bt_stream *stream = trace->streams->pdata[i]; |
| 84 | |
| 85 | if (stream->class != stream_class) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (stream->id == id) { |
| 90 | is_unique = false; |
| 91 | goto end; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | end: |
| 96 | return is_unique; |
| 97 | } |
| 98 | |
| 99 | static |
| 100 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
| 101 | struct bt_trace *trace, uint64_t id) |
| 102 | { |
| 103 | int ret; |
| 104 | struct bt_stream *stream; |
| 105 | |
| 106 | BT_ASSERT(stream_class); |
| 107 | BT_ASSERT(trace); |
| 108 | BT_ASSERT_PRE(trace->class == |
| 109 | bt_stream_class_borrow_trace_class_inline(stream_class), |
| 110 | "Trace's class is different from stream class's parent trace class: " |
| 111 | "%![sc-]+S, %![trace-]+t", stream_class, trace); |
| 112 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), |
| 113 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); |
| 114 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, |
| 115 | trace, id); |
| 116 | stream = g_new0(struct bt_stream, 1); |
| 117 | if (!stream) { |
| 118 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream."); |
| 119 | goto error; |
| 120 | } |
| 121 | |
| 122 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
| 123 | stream->user_attributes = bt_value_map_create(); |
| 124 | if (!stream->user_attributes) { |
| 125 | BT_LIB_LOGE_APPEND_CAUSE( |
| 126 | "Failed to create a map value object."); |
| 127 | goto error; |
| 128 | } |
| 129 | |
| 130 | stream->name.str = g_string_new(NULL); |
| 131 | if (!stream->name.str) { |
| 132 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | stream->id = id; |
| 137 | ret = bt_object_pool_initialize(&stream->packet_pool, |
| 138 | (bt_object_pool_new_object_func) bt_packet_new, |
| 139 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, |
| 140 | stream); |
| 141 | if (ret) { |
| 142 | BT_LIB_LOGE_APPEND_CAUSE( |
| 143 | "Failed to initialize packet pool: ret=%d", ret); |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | stream->class = stream_class; |
| 148 | bt_object_get_ref_no_null_check(stream_class); |
| 149 | |
| 150 | /* bt_trace_add_stream() sets the parent trace, and freezes the trace */ |
| 151 | bt_trace_add_stream(trace, stream); |
| 152 | |
| 153 | bt_stream_class_freeze(stream_class); |
| 154 | BT_LIB_LOGD("Created stream object: %!+s", stream); |
| 155 | goto end; |
| 156 | |
| 157 | error: |
| 158 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
| 159 | |
| 160 | end: |
| 161 | return stream; |
| 162 | } |
| 163 | |
| 164 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class, |
| 165 | struct bt_trace *trace) |
| 166 | { |
| 167 | uint64_t id; |
| 168 | |
| 169 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
| 170 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 171 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, |
| 172 | "Stream class does not automatically assigns stream IDs: " |
| 173 | "%![sc-]+S", stream_class); |
| 174 | id = bt_trace_get_automatic_stream_id(trace, stream_class); |
| 175 | return create_stream_with_id(stream_class, trace, id); |
| 176 | } |
| 177 | |
| 178 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
| 179 | struct bt_trace *trace, uint64_t id) |
| 180 | { |
| 181 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
| 182 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 183 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, |
| 184 | "Stream class automatically assigns stream IDs: " |
| 185 | "%![sc-]+S", stream_class); |
| 186 | return create_stream_with_id(stream_class, trace, id); |
| 187 | } |
| 188 | |
| 189 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
| 190 | { |
| 191 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 192 | return stream->class; |
| 193 | } |
| 194 | |
| 195 | const struct bt_stream_class *bt_stream_borrow_class_const( |
| 196 | const struct bt_stream *stream) |
| 197 | { |
| 198 | return bt_stream_borrow_class((void *) stream); |
| 199 | } |
| 200 | |
| 201 | struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream) |
| 202 | { |
| 203 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 204 | return bt_stream_borrow_trace_inline(stream); |
| 205 | } |
| 206 | |
| 207 | const struct bt_trace *bt_stream_borrow_trace_const( |
| 208 | const struct bt_stream *stream) |
| 209 | { |
| 210 | return bt_stream_borrow_trace((void *) stream); |
| 211 | } |
| 212 | |
| 213 | const char *bt_stream_get_name(const struct bt_stream *stream) |
| 214 | { |
| 215 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 216 | return stream->name.value; |
| 217 | } |
| 218 | |
| 219 | enum bt_stream_set_name_status bt_stream_set_name(struct bt_stream *stream, |
| 220 | const char *name) |
| 221 | { |
| 222 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 223 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 224 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); |
| 225 | g_string_assign(stream->name.str, name); |
| 226 | stream->name.value = stream->name.str->str; |
| 227 | BT_LIB_LOGD("Set stream's name: %!+s", stream); |
| 228 | return BT_FUNC_STATUS_OK; |
| 229 | } |
| 230 | |
| 231 | uint64_t bt_stream_get_id(const struct bt_stream *stream) |
| 232 | { |
| 233 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream class"); |
| 234 | return stream->id; |
| 235 | } |
| 236 | |
| 237 | BT_HIDDEN |
| 238 | void _bt_stream_freeze(const struct bt_stream *stream) |
| 239 | { |
| 240 | BT_ASSERT(stream); |
| 241 | BT_LIB_LOGD("Freezing stream's user attributes: %!+v", |
| 242 | stream->user_attributes); |
| 243 | bt_value_freeze(stream->user_attributes); |
| 244 | BT_LIB_LOGD("Freezing stream: %!+s", stream); |
| 245 | ((struct bt_stream *) stream)->frozen = true; |
| 246 | } |
| 247 | |
| 248 | const struct bt_value *bt_stream_borrow_user_attributes_const( |
| 249 | const struct bt_stream *stream) |
| 250 | { |
| 251 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 252 | return stream->user_attributes; |
| 253 | } |
| 254 | |
| 255 | struct bt_value *bt_stream_borrow_user_attributes(struct bt_stream *stream) |
| 256 | { |
| 257 | return (void *) bt_stream_borrow_user_attributes_const((void *) stream); |
| 258 | } |
| 259 | |
| 260 | void bt_stream_set_user_attributes(struct bt_stream *stream, |
| 261 | const struct bt_value *user_attributes) |
| 262 | { |
| 263 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 264 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); |
| 265 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, |
| 266 | "User attributes object is not a map value object."); |
| 267 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); |
| 268 | bt_object_put_ref_no_null_check(stream->user_attributes); |
| 269 | stream->user_attributes = (void *) user_attributes; |
| 270 | bt_object_get_ref_no_null_check(stream->user_attributes); |
| 271 | } |
| 272 | |
| 273 | void bt_stream_get_ref(const struct bt_stream *stream) |
| 274 | { |
| 275 | bt_object_get_ref(stream); |
| 276 | } |
| 277 | |
| 278 | void bt_stream_put_ref(const struct bt_stream *stream) |
| 279 | { |
| 280 | bt_object_put_ref(stream); |
| 281 | } |