| 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/func-status.h" |
| 44 | |
| 45 | #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \ |
| 46 | BT_ASSERT_PRE_DEV_HOT((_stream), "Stream", ": %!+s", (_stream)) |
| 47 | |
| 48 | static |
| 49 | void destroy_stream(struct bt_object *obj) |
| 50 | { |
| 51 | struct bt_stream *stream = (void *) obj; |
| 52 | |
| 53 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
| 54 | |
| 55 | if (stream->name.str) { |
| 56 | g_string_free(stream->name.str, TRUE); |
| 57 | stream->name.str = NULL; |
| 58 | stream->name.value = NULL; |
| 59 | } |
| 60 | |
| 61 | BT_LOGD_STR("Putting stream's class."); |
| 62 | bt_object_put_ref(stream->class); |
| 63 | bt_object_pool_finalize(&stream->packet_pool); |
| 64 | g_free(stream); |
| 65 | } |
| 66 | |
| 67 | static |
| 68 | void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream) |
| 69 | { |
| 70 | bt_packet_destroy(packet); |
| 71 | } |
| 72 | |
| 73 | BT_ASSERT_PRE_FUNC |
| 74 | static inline |
| 75 | bool stream_id_is_unique(struct bt_trace *trace, |
| 76 | struct bt_stream_class *stream_class, uint64_t id) |
| 77 | { |
| 78 | uint64_t i; |
| 79 | bool is_unique = true; |
| 80 | |
| 81 | for (i = 0; i < trace->streams->len; i++) { |
| 82 | struct bt_stream *stream = trace->streams->pdata[i]; |
| 83 | |
| 84 | if (stream->class != stream_class) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if (stream->id == id) { |
| 89 | is_unique = false; |
| 90 | goto end; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | end: |
| 95 | return is_unique; |
| 96 | } |
| 97 | |
| 98 | static |
| 99 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
| 100 | struct bt_trace *trace, uint64_t id) |
| 101 | { |
| 102 | int ret; |
| 103 | struct bt_stream *stream; |
| 104 | |
| 105 | BT_ASSERT(stream_class); |
| 106 | BT_ASSERT(trace); |
| 107 | BT_ASSERT_PRE(trace->class == |
| 108 | bt_stream_class_borrow_trace_class_inline(stream_class), |
| 109 | "Trace's class is different from stream class's parent trace class: " |
| 110 | "%![sc-]+S, %![trace-]+t", stream_class, trace); |
| 111 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), |
| 112 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); |
| 113 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, |
| 114 | trace, id); |
| 115 | stream = g_new0(struct bt_stream, 1); |
| 116 | if (!stream) { |
| 117 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream."); |
| 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
| 122 | stream->name.str = g_string_new(NULL); |
| 123 | if (!stream->name.str) { |
| 124 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | stream->id = id; |
| 129 | ret = bt_object_pool_initialize(&stream->packet_pool, |
| 130 | (bt_object_pool_new_object_func) bt_packet_new, |
| 131 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, |
| 132 | stream); |
| 133 | if (ret) { |
| 134 | BT_LIB_LOGE_APPEND_CAUSE( |
| 135 | "Failed to initialize packet pool: ret=%d", ret); |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | stream->class = stream_class; |
| 140 | bt_object_get_no_null_check(stream_class); |
| 141 | |
| 142 | /* bt_trace_add_stream() sets the parent trace, and freezes the trace */ |
| 143 | bt_trace_add_stream(trace, stream); |
| 144 | |
| 145 | bt_stream_class_freeze(stream_class); |
| 146 | BT_LIB_LOGD("Created stream object: %!+s", stream); |
| 147 | goto end; |
| 148 | |
| 149 | error: |
| 150 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
| 151 | |
| 152 | end: |
| 153 | return stream; |
| 154 | } |
| 155 | |
| 156 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class, |
| 157 | struct bt_trace *trace) |
| 158 | { |
| 159 | uint64_t id; |
| 160 | |
| 161 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
| 162 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 163 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, |
| 164 | "Stream class does not automatically assigns stream IDs: " |
| 165 | "%![sc-]+S", stream_class); |
| 166 | id = bt_trace_get_automatic_stream_id(trace, stream_class); |
| 167 | return create_stream_with_id(stream_class, trace, id); |
| 168 | } |
| 169 | |
| 170 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
| 171 | struct bt_trace *trace, uint64_t id) |
| 172 | { |
| 173 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); |
| 174 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 175 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, |
| 176 | "Stream class automatically assigns stream IDs: " |
| 177 | "%![sc-]+S", stream_class); |
| 178 | return create_stream_with_id(stream_class, trace, id); |
| 179 | } |
| 180 | |
| 181 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
| 182 | { |
| 183 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 184 | return stream->class; |
| 185 | } |
| 186 | |
| 187 | const struct bt_stream_class *bt_stream_borrow_class_const( |
| 188 | const struct bt_stream *stream) |
| 189 | { |
| 190 | return bt_stream_borrow_class((void *) stream); |
| 191 | } |
| 192 | |
| 193 | struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream) |
| 194 | { |
| 195 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 196 | return bt_stream_borrow_trace_inline(stream); |
| 197 | } |
| 198 | |
| 199 | const struct bt_trace *bt_stream_borrow_trace_const( |
| 200 | const struct bt_stream *stream) |
| 201 | { |
| 202 | return bt_stream_borrow_trace((void *) stream); |
| 203 | } |
| 204 | |
| 205 | const char *bt_stream_get_name(const struct bt_stream *stream) |
| 206 | { |
| 207 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream"); |
| 208 | return stream->name.value; |
| 209 | } |
| 210 | |
| 211 | enum bt_stream_set_name_status bt_stream_set_name(struct bt_stream *stream, |
| 212 | const char *name) |
| 213 | { |
| 214 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 215 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 216 | BT_ASSERT_PRE_DEV_STREAM_HOT(stream); |
| 217 | g_string_assign(stream->name.str, name); |
| 218 | stream->name.value = stream->name.str->str; |
| 219 | BT_LIB_LOGD("Set stream's name: %!+s", stream); |
| 220 | return BT_FUNC_STATUS_OK; |
| 221 | } |
| 222 | |
| 223 | uint64_t bt_stream_get_id(const struct bt_stream *stream) |
| 224 | { |
| 225 | BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream class"); |
| 226 | return stream->id; |
| 227 | } |
| 228 | |
| 229 | BT_HIDDEN |
| 230 | void _bt_stream_freeze(const struct bt_stream *stream) |
| 231 | { |
| 232 | BT_ASSERT(stream); |
| 233 | BT_LIB_LOGD("Freezing stream: %!+s", stream); |
| 234 | ((struct bt_stream *) stream)->frozen = true; |
| 235 | } |
| 236 | |
| 237 | void bt_stream_get_ref(const struct bt_stream *stream) |
| 238 | { |
| 239 | bt_object_get_ref(stream); |
| 240 | } |
| 241 | |
| 242 | void bt_stream_put_ref(const struct bt_stream *stream) |
| 243 | { |
| 244 | bt_object_put_ref(stream); |
| 245 | } |