lib: assign a unique ID to each pre/postcond. and report it on failure
[babeltrace.git] / src / lib / trace-ir / stream.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/STREAM"
9 #include "lib/logging.h"
10
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/stream.h>
13 #include <babeltrace2/trace-ir/stream-class.h>
14 #include <babeltrace2/trace-ir/trace.h>
15 #include "compat/compiler.h"
16 #include "common/align.h"
17 #include "common/assert.h"
18 #include "lib/property.h"
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <unistd.h>
22
23 #include "packet.h"
24 #include "stream-class.h"
25 #include "stream.h"
26 #include "trace.h"
27 #include "lib/value.h"
28 #include "lib/func-status.h"
29
30 #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \
31 BT_ASSERT_PRE_DEV_HOT("stream", (_stream), "Stream", \
32 ": %!+s", (_stream))
33
34 static
35 void destroy_stream(struct bt_object *obj)
36 {
37 struct bt_stream *stream = (void *) obj;
38
39 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
40 BT_OBJECT_PUT_REF_AND_RESET(stream->user_attributes);
41
42 if (stream->name.str) {
43 g_string_free(stream->name.str, TRUE);
44 stream->name.str = NULL;
45 stream->name.value = NULL;
46 }
47
48 BT_LOGD_STR("Putting stream's class.");
49 bt_object_put_ref(stream->class);
50 bt_object_pool_finalize(&stream->packet_pool);
51 g_free(stream);
52 }
53
54 static
55 void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream)
56 {
57 bt_packet_destroy(packet);
58 }
59
60 static inline
61 bool stream_id_is_unique(struct bt_trace *trace,
62 struct bt_stream_class *stream_class, uint64_t id)
63 {
64 uint64_t i;
65 bool is_unique = true;
66
67 for (i = 0; i < trace->streams->len; i++) {
68 struct bt_stream *stream = trace->streams->pdata[i];
69
70 if (stream->class != stream_class) {
71 continue;
72 }
73
74 if (stream->id == id) {
75 is_unique = false;
76 goto end;
77 }
78 }
79
80 end:
81 return is_unique;
82 }
83
84 static
85 struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
86 struct bt_trace *trace, uint64_t id, const char *api_func)
87 {
88 int ret;
89 struct bt_stream *stream;
90
91 BT_ASSERT(stream_class);
92 BT_ASSERT(trace);
93 BT_ASSERT_PRE_FROM_FUNC(api_func,
94 "trace-class-is-stream-class-trace-class",
95 trace->class ==
96 bt_stream_class_borrow_trace_class_inline(stream_class),
97 "Trace's class is different from stream class's parent trace class: "
98 "%![sc-]+S, %![trace-]+t", stream_class, trace);
99 BT_ASSERT_PRE_FROM_FUNC(api_func, "stream-id-is-unique",
100 stream_id_is_unique(trace, stream_class, id),
101 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
102 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64,
103 trace, id);
104 stream = g_new0(struct bt_stream, 1);
105 if (!stream) {
106 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream.");
107 goto error;
108 }
109
110 bt_object_init_shared_with_parent(&stream->base, destroy_stream);
111 stream->user_attributes = bt_value_map_create();
112 if (!stream->user_attributes) {
113 BT_LIB_LOGE_APPEND_CAUSE(
114 "Failed to create a map value object.");
115 goto error;
116 }
117
118 stream->name.str = g_string_new(NULL);
119 if (!stream->name.str) {
120 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
121 goto error;
122 }
123
124 stream->id = id;
125 ret = bt_object_pool_initialize(&stream->packet_pool,
126 (bt_object_pool_new_object_func) bt_packet_new,
127 (bt_object_pool_destroy_object_func) bt_stream_free_packet,
128 stream);
129 if (ret) {
130 BT_LIB_LOGE_APPEND_CAUSE(
131 "Failed to initialize packet pool: ret=%d", ret);
132 goto error;
133 }
134
135 stream->class = stream_class;
136 bt_object_get_ref_no_null_check(stream_class);
137
138 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
139 bt_trace_add_stream(trace, stream);
140
141 bt_stream_class_freeze(stream_class);
142 BT_LIB_LOGD("Created stream object: %!+s", stream);
143 goto end;
144
145 error:
146 BT_OBJECT_PUT_REF_AND_RESET(stream);
147
148 end:
149 return stream;
150 }
151
152 struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class,
153 struct bt_trace *trace)
154 {
155 uint64_t id;
156
157 BT_ASSERT_PRE_NO_ERROR();
158 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
159 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
160 BT_ASSERT_PRE("stream-class-automatically-assigns-stream-ids",
161 stream_class->assigns_automatic_stream_id,
162 "Stream class does not automatically assigns stream IDs: "
163 "%![sc-]+S", stream_class);
164 id = bt_trace_get_automatic_stream_id(trace, stream_class);
165 return create_stream_with_id(stream_class, trace, id, __func__);
166 }
167
168 struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class,
169 struct bt_trace *trace, uint64_t id)
170 {
171 BT_ASSERT_PRE_NO_ERROR();
172 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
173 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
174 BT_ASSERT_PRE("stream-class-does-not-automatically-assigns-stream-ids",
175 !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, __func__);
179 }
180
181 struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
182 {
183 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(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_STREAM_NON_NULL(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_STREAM_NON_NULL(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_NO_ERROR();
215 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
216 BT_ASSERT_PRE_NAME_NON_NULL(name);
217 BT_ASSERT_PRE_DEV_STREAM_HOT(stream);
218 g_string_assign(stream->name.str, name);
219 stream->name.value = stream->name.str->str;
220 BT_LIB_LOGD("Set stream's name: %!+s", stream);
221 return BT_FUNC_STATUS_OK;
222 }
223
224 uint64_t bt_stream_get_id(const struct bt_stream *stream)
225 {
226 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream);
227 return stream->id;
228 }
229
230 BT_HIDDEN
231 void _bt_stream_freeze(const struct bt_stream *stream)
232 {
233 BT_ASSERT(stream);
234 BT_LIB_LOGD("Freezing stream's user attributes: %!+v",
235 stream->user_attributes);
236 bt_value_freeze(stream->user_attributes);
237 BT_LIB_LOGD("Freezing stream: %!+s", stream);
238 ((struct bt_stream *) stream)->frozen = true;
239 }
240
241 const struct bt_value *bt_stream_borrow_user_attributes_const(
242 const struct bt_stream *stream)
243 {
244 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream);
245 return stream->user_attributes;
246 }
247
248 struct bt_value *bt_stream_borrow_user_attributes(struct bt_stream *stream)
249 {
250 return (void *) bt_stream_borrow_user_attributes_const((void *) stream);
251 }
252
253 void bt_stream_set_user_attributes(struct bt_stream *stream,
254 const struct bt_value *user_attributes)
255 {
256 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
257 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
258 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
259 BT_ASSERT_PRE_DEV_STREAM_HOT(stream);
260 bt_object_put_ref_no_null_check(stream->user_attributes);
261 stream->user_attributes = (void *) user_attributes;
262 bt_object_get_ref_no_null_check(stream->user_attributes);
263 }
264
265 void bt_stream_get_ref(const struct bt_stream *stream)
266 {
267 bt_object_get_ref(stream);
268 }
269
270 void bt_stream_put_ref(const struct bt_stream *stream)
271 {
272 bt_object_put_ref(stream);
273 }
This page took 0.035169 seconds and 4 git commands to generate.