Logging: standardize logging tags
[babeltrace.git] / src / lib / trace-ir / stream.c
CommitLineData
273b65be 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be 4 *
273b65be
JG
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
b03487ab 24#define BT_LOG_TAG "LIB/STREAM"
57952005 25#include "lib/lib-logging.h"
19abc2c6 26
57952005 27#include "lib/assert-pre.h"
71c5da58
MJ
28#include <babeltrace2/trace-ir/stream-const.h>
29#include <babeltrace2/trace-ir/stream.h>
71c5da58 30#include <babeltrace2/trace-ir/stream-class.h>
71c5da58 31#include <babeltrace2/trace-ir/trace.h>
57952005
MJ
32#include "compat/compiler.h"
33#include "common/align.h"
34#include "common/assert.h"
35#include "lib/property.h"
8deee039
PP
36#include <inttypes.h>
37#include <unistd.h>
12c8a1a3 38
57952005
MJ
39#include "packet.h"
40#include "stream-class.h"
41#include "stream.h"
42#include "trace.h"
43
7b33a0e0
PP
44#define BT_ASSERT_PRE_STREAM_HOT(_stream) \
45 BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream))
263a7df5 46
c9af50d1 47static
7b33a0e0 48void destroy_stream(struct bt_object *obj)
c9af50d1 49{
8deee039 50 struct bt_stream *stream = (void *) obj;
c9af50d1 51
7b33a0e0
PP
52 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
53
54 if (stream->name.str) {
55 g_string_free(stream->name.str, TRUE);
1248f5ea
PP
56 stream->name.str = NULL;
57 stream->name.value = NULL;
7b33a0e0
PP
58 }
59
10b7a2e4
PP
60 BT_LOGD_STR("Putting stream's class.");
61 bt_object_put_ref(stream->class);
a6918753 62 bt_object_pool_finalize(&stream->packet_pool);
8deee039 63 g_free(stream);
c9af50d1
JG
64}
65
7b33a0e0
PP
66static
67void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream)
273b65be 68{
7b33a0e0
PP
69 bt_packet_destroy(packet);
70}
19abc2c6 71
7b33a0e0
PP
72BT_ASSERT_PRE_FUNC
73static inline
74bool 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;
273b65be 79
7b33a0e0
PP
80 for (i = 0; i < trace->streams->len; i++) {
81 struct bt_stream *stream = trace->streams->pdata[i];
273b65be 82
7b33a0e0
PP
83 if (stream->class != stream_class) {
84 continue;
8bfa3f9c 85 }
daaa1851 86
7b33a0e0
PP
87 if (stream->id == id) {
88 is_unique = false;
89 goto end;
98edd02c 90 }
273b65be
JG
91 }
92
8deee039 93end:
7b33a0e0 94 return is_unique;
a6918753
PP
95}
96
273b65be 97static
7b33a0e0 98struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
10b7a2e4 99 struct bt_trace *trace, uint64_t id)
273b65be 100{
8deee039 101 int ret;
7b33a0e0 102 struct bt_stream *stream;
7b33a0e0
PP
103
104 BT_ASSERT(stream_class);
10b7a2e4
PP
105 BT_ASSERT(trace);
106 BT_ASSERT_PRE(trace->class ==
107 bt_stream_class_borrow_trace_class_inline(stream_class),
108 "Trace's class is different from stream class's parent trace class: "
109 "%![sc-]+S, %![trace-]+t", stream_class, trace);
7b33a0e0
PP
110 BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id),
111 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
7b33a0e0
PP
112 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64,
113 trace, id);
8deee039
PP
114 stream = g_new0(struct bt_stream, 1);
115 if (!stream) {
116 BT_LOGE_STR("Failed to allocate one stream.");
117 goto error;
12c8a1a3 118 }
b71d7298 119
7b33a0e0
PP
120 bt_object_init_shared_with_parent(&stream->base, destroy_stream);
121 stream->name.str = g_string_new(NULL);
122 if (!stream->name.str) {
123 BT_LOGE_STR("Failed to allocate a GString.");
8deee039 124 goto error;
b71d7298 125 }
41ac640a 126
7b33a0e0 127 stream->id = id;
a6918753
PP
128 ret = bt_object_pool_initialize(&stream->packet_pool,
129 (bt_object_pool_new_object_func) bt_packet_new,
130 (bt_object_pool_destroy_object_func) bt_stream_free_packet,
131 stream);
132 if (ret) {
133 BT_LOGE("Failed to initialize packet pool: ret=%d", ret);
134 goto error;
135 }
136
7b33a0e0 137 stream->class = stream_class;
10b7a2e4
PP
138 bt_object_get_no_null_check(stream_class);
139
140 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
7b33a0e0 141 bt_trace_add_stream(trace, stream);
10b7a2e4 142
7b33a0e0
PP
143 bt_stream_class_freeze(stream_class);
144 BT_LIB_LOGD("Created stream object: %!+s", stream);
8deee039 145 goto end;
3230ee6b 146
8deee039 147error:
8138bfe1 148 BT_OBJECT_PUT_REF_AND_RESET(stream);
8deee039
PP
149
150end:
151 return stream;
273b65be
JG
152}
153
10b7a2e4
PP
154struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class,
155 struct bt_trace *trace)
273b65be 156{
7b33a0e0
PP
157 uint64_t id;
158
159 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
10b7a2e4 160 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
161 BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id,
162 "Stream class does not automatically assigns stream IDs: "
163 "%![sc-]+S", stream_class);
10b7a2e4
PP
164 id = bt_trace_get_automatic_stream_id(trace, stream_class);
165 return create_stream_with_id(stream_class, trace, id);
7b33a0e0 166}
d7b1ea66 167
78cf9df6 168struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class,
10b7a2e4 169 struct bt_trace *trace, uint64_t id)
7b33a0e0 170{
10b7a2e4
PP
171 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
172 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
173 BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id,
174 "Stream class automatically assigns stream IDs: "
175 "%![sc-]+S", stream_class);
10b7a2e4 176 return create_stream_with_id(stream_class, trace, id);
273b65be 177}
b71d7298 178
5fe68922 179struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
af9296f3 180{
7b33a0e0
PP
181 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
182 return stream->class;
af9296f3
JG
183}
184
78cf9df6
PP
185const struct bt_stream_class *bt_stream_borrow_class_const(
186 const struct bt_stream *stream)
9e550e5f 187{
78cf9df6 188 return bt_stream_borrow_class((void *) stream);
9e550e5f
PP
189}
190
10b7a2e4
PP
191struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream)
192{
193 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
194 return bt_stream_borrow_trace_inline(stream);
195}
196
197const struct bt_trace *bt_stream_borrow_trace_const(
198 const struct bt_stream *stream)
199{
200 return bt_stream_borrow_trace((void *) stream);
201}
202
78cf9df6 203const char *bt_stream_get_name(const struct bt_stream *stream)
b71d7298 204{
c471045c 205 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
7b33a0e0 206 return stream->name.value;
b71d7298 207}
98a4cbef 208
45490f7f
PP
209enum bt_stream_status bt_stream_set_name(struct bt_stream *stream,
210 const char *name)
98a4cbef 211{
c471045c 212 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
7b33a0e0
PP
213 BT_ASSERT_PRE_NON_NULL(name, "Name");
214 BT_ASSERT_PRE_STREAM_HOT(stream);
215 g_string_assign(stream->name.str, name);
216 stream->name.value = stream->name.str->str;
a684a357 217 BT_LIB_LOGD("Set stream's name: %!+s", stream);
45490f7f 218 return BT_STREAM_STATUS_OK;
7b33a0e0 219}
18acc6f8 220
78cf9df6 221uint64_t bt_stream_get_id(const struct bt_stream *stream)
7b33a0e0
PP
222{
223 BT_ASSERT_PRE_NON_NULL(stream, "Stream class");
224 return stream->id;
225}
18acc6f8 226
7b33a0e0 227BT_HIDDEN
78cf9df6 228void _bt_stream_freeze(const struct bt_stream *stream)
7b33a0e0 229{
7b33a0e0
PP
230 BT_ASSERT(stream);
231 BT_LIB_LOGD("Freezing stream: %!+s", stream);
78cf9df6 232 ((struct bt_stream *) stream)->frozen = true;
98a4cbef 233}
8c6884d9
PP
234
235void bt_stream_get_ref(const struct bt_stream *stream)
236{
237 bt_object_get_ref(stream);
238}
239
240void bt_stream_put_ref(const struct bt_stream *stream)
241{
242 bt_object_put_ref(stream);
243}
This page took 0.082027 seconds and 4 git commands to generate.