tap-driver.sh: flush stdout after each test result
[babeltrace.git] / lib / trace-ir / stream.c
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 "STREAM"
25 #include <babeltrace2/lib-logging-internal.h>
26
27 #include <babeltrace2/assert-pre-internal.h>
28 #include <babeltrace2/trace-ir/stream-const.h>
29 #include <babeltrace2/trace-ir/stream.h>
30 #include <babeltrace2/trace-ir/stream-internal.h>
31 #include <babeltrace2/trace-ir/stream-class.h>
32 #include <babeltrace2/trace-ir/stream-class-internal.h>
33 #include <babeltrace2/trace-ir/trace.h>
34 #include <babeltrace2/trace-ir/trace-internal.h>
35 #include <babeltrace2/trace-ir/packet-internal.h>
36 #include <babeltrace2/compiler-internal.h>
37 #include <babeltrace2/align-internal.h>
38 #include <babeltrace2/assert-internal.h>
39 #include <babeltrace2/property-internal.h>
40 #include <inttypes.h>
41 #include <unistd.h>
42
43 #define BT_ASSERT_PRE_STREAM_HOT(_stream) \
44 BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream))
45
46 static
47 void destroy_stream(struct bt_object *obj)
48 {
49 struct bt_stream *stream = (void *) obj;
50
51 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
52
53 if (stream->name.str) {
54 g_string_free(stream->name.str, TRUE);
55 stream->name.str = NULL;
56 stream->name.value = NULL;
57 }
58
59 BT_LOGD_STR("Putting stream's class.");
60 bt_object_put_ref(stream->class);
61 bt_object_pool_finalize(&stream->packet_pool);
62 g_free(stream);
63 }
64
65 static
66 void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream)
67 {
68 bt_packet_destroy(packet);
69 }
70
71 BT_ASSERT_PRE_FUNC
72 static inline
73 bool stream_id_is_unique(struct bt_trace *trace,
74 struct bt_stream_class *stream_class, uint64_t id)
75 {
76 uint64_t i;
77 bool is_unique = true;
78
79 for (i = 0; i < trace->streams->len; i++) {
80 struct bt_stream *stream = trace->streams->pdata[i];
81
82 if (stream->class != stream_class) {
83 continue;
84 }
85
86 if (stream->id == id) {
87 is_unique = false;
88 goto end;
89 }
90 }
91
92 end:
93 return is_unique;
94 }
95
96 static
97 struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
98 struct bt_trace *trace, uint64_t id)
99 {
100 int ret;
101 struct bt_stream *stream;
102
103 BT_ASSERT(stream_class);
104 BT_ASSERT(trace);
105 BT_ASSERT_PRE(trace->class ==
106 bt_stream_class_borrow_trace_class_inline(stream_class),
107 "Trace's class is different from stream class's parent trace class: "
108 "%![sc-]+S, %![trace-]+t", stream_class, trace);
109 BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id),
110 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
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_object_get_no_null_check(stream_class);
138
139 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
140 bt_trace_add_stream(trace, stream);
141
142 bt_stream_class_freeze(stream_class);
143 BT_LIB_LOGD("Created stream object: %!+s", stream);
144 goto end;
145
146 error:
147 BT_OBJECT_PUT_REF_AND_RESET(stream);
148
149 end:
150 return stream;
151 }
152
153 struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class,
154 struct bt_trace *trace)
155 {
156 uint64_t id;
157
158 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
159 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
160 BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id,
161 "Stream class does not automatically assigns stream IDs: "
162 "%![sc-]+S", stream_class);
163 id = bt_trace_get_automatic_stream_id(trace, stream_class);
164 return create_stream_with_id(stream_class, trace, id);
165 }
166
167 struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class,
168 struct bt_trace *trace, uint64_t id)
169 {
170 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
171 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
172 BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id,
173 "Stream class automatically assigns stream IDs: "
174 "%![sc-]+S", stream_class);
175 return create_stream_with_id(stream_class, trace, id);
176 }
177
178 struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
179 {
180 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
181 return stream->class;
182 }
183
184 const struct bt_stream_class *bt_stream_borrow_class_const(
185 const struct bt_stream *stream)
186 {
187 return bt_stream_borrow_class((void *) stream);
188 }
189
190 struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream)
191 {
192 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
193 return bt_stream_borrow_trace_inline(stream);
194 }
195
196 const struct bt_trace *bt_stream_borrow_trace_const(
197 const struct bt_stream *stream)
198 {
199 return bt_stream_borrow_trace((void *) stream);
200 }
201
202 const char *bt_stream_get_name(const struct bt_stream *stream)
203 {
204 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
205 return stream->name.value;
206 }
207
208 enum bt_stream_status bt_stream_set_name(struct bt_stream *stream,
209 const char *name)
210 {
211 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
212 BT_ASSERT_PRE_NON_NULL(name, "Name");
213 BT_ASSERT_PRE_STREAM_HOT(stream);
214 g_string_assign(stream->name.str, name);
215 stream->name.value = stream->name.str->str;
216 BT_LIB_LOGV("Set stream's name: %!+s", stream);
217 return BT_STREAM_STATUS_OK;
218 }
219
220 uint64_t bt_stream_get_id(const struct bt_stream *stream)
221 {
222 BT_ASSERT_PRE_NON_NULL(stream, "Stream class");
223 return stream->id;
224 }
225
226 BT_HIDDEN
227 void _bt_stream_freeze(const struct bt_stream *stream)
228 {
229 BT_ASSERT(stream);
230 BT_LIB_LOGD("Freezing stream: %!+s", stream);
231 ((struct bt_stream *) stream)->frozen = true;
232 }
233
234 void bt_stream_get_ref(const struct bt_stream *stream)
235 {
236 bt_object_get_ref(stream);
237 }
238
239 void bt_stream_put_ref(const struct bt_stream *stream)
240 {
241 bt_object_put_ref(stream);
242 }
This page took 0.035 seconds and 4 git commands to generate.