lib: split trace API into trace class and trace APIs
[babeltrace.git] / lib / trace-ir / stream.c
CommitLineData
273b65be 1/*
de9dd397 2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
19abc2c6
PP
25#define BT_LOG_TAG "STREAM"
26#include <babeltrace/lib-logging-internal.h>
27
44ea72c5 28#include <babeltrace/assert-pre-internal.h>
78cf9df6 29#include <babeltrace/trace-ir/stream-const.h>
108b91d0
PP
30#include <babeltrace/trace-ir/stream.h>
31#include <babeltrace/trace-ir/stream-internal.h>
32#include <babeltrace/trace-ir/stream-class.h>
33#include <babeltrace/trace-ir/stream-class-internal.h>
34#include <babeltrace/trace-ir/trace.h>
35#include <babeltrace/trace-ir/trace-internal.h>
36#include <babeltrace/trace-ir/packet-internal.h>
8138bfe1 37#include <babeltrace/object.h>
8deee039
PP
38#include <babeltrace/compiler-internal.h>
39#include <babeltrace/align-internal.h>
40#include <babeltrace/assert-internal.h>
7b33a0e0 41#include <babeltrace/property-internal.h>
8deee039
PP
42#include <inttypes.h>
43#include <unistd.h>
12c8a1a3 44
7b33a0e0
PP
45#define BT_ASSERT_PRE_STREAM_HOT(_stream) \
46 BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream))
263a7df5 47
c9af50d1 48static
7b33a0e0 49void destroy_stream(struct bt_object *obj)
c9af50d1 50{
8deee039 51 struct bt_stream *stream = (void *) obj;
c9af50d1 52
7b33a0e0
PP
53 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
54
55 if (stream->name.str) {
56 g_string_free(stream->name.str, TRUE);
1248f5ea
PP
57 stream->name.str = NULL;
58 stream->name.value = NULL;
7b33a0e0
PP
59 }
60
10b7a2e4
PP
61 BT_LOGD_STR("Putting stream's class.");
62 bt_object_put_ref(stream->class);
a6918753 63 bt_object_pool_finalize(&stream->packet_pool);
8deee039 64 g_free(stream);
c9af50d1
JG
65}
66
7b33a0e0
PP
67static
68void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream)
273b65be 69{
7b33a0e0
PP
70 bt_packet_destroy(packet);
71}
19abc2c6 72
7b33a0e0
PP
73BT_ASSERT_PRE_FUNC
74static inline
75bool 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;
273b65be 80
7b33a0e0
PP
81 for (i = 0; i < trace->streams->len; i++) {
82 struct bt_stream *stream = trace->streams->pdata[i];
273b65be 83
7b33a0e0
PP
84 if (stream->class != stream_class) {
85 continue;
8bfa3f9c 86 }
daaa1851 87
7b33a0e0
PP
88 if (stream->id == id) {
89 is_unique = false;
90 goto end;
98edd02c 91 }
273b65be
JG
92 }
93
8deee039 94end:
7b33a0e0 95 return is_unique;
a6918753
PP
96}
97
273b65be 98static
7b33a0e0 99struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
10b7a2e4 100 struct bt_trace *trace, uint64_t id)
273b65be 101{
8deee039 102 int ret;
7b33a0e0 103 struct bt_stream *stream;
7b33a0e0
PP
104
105 BT_ASSERT(stream_class);
10b7a2e4
PP
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);
7b33a0e0
PP
111 BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id),
112 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
113 BT_ASSERT_PRE(!trace->is_static,
114 "Trace is static: %![trace-]+t", trace);
115 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64,
116 trace, id);
8deee039
PP
117 stream = g_new0(struct bt_stream, 1);
118 if (!stream) {
119 BT_LOGE_STR("Failed to allocate one stream.");
120 goto error;
12c8a1a3 121 }
b71d7298 122
7b33a0e0
PP
123 bt_object_init_shared_with_parent(&stream->base, destroy_stream);
124 stream->name.str = g_string_new(NULL);
125 if (!stream->name.str) {
126 BT_LOGE_STR("Failed to allocate a GString.");
8deee039 127 goto error;
b71d7298 128 }
41ac640a 129
7b33a0e0 130 stream->id = id;
a6918753
PP
131 ret = bt_object_pool_initialize(&stream->packet_pool,
132 (bt_object_pool_new_object_func) bt_packet_new,
133 (bt_object_pool_destroy_object_func) bt_stream_free_packet,
134 stream);
135 if (ret) {
136 BT_LOGE("Failed to initialize packet pool: ret=%d", ret);
137 goto error;
138 }
139
7b33a0e0 140 stream->class = stream_class;
10b7a2e4
PP
141 bt_object_get_no_null_check(stream_class);
142
143 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
7b33a0e0 144 bt_trace_add_stream(trace, stream);
10b7a2e4 145
7b33a0e0
PP
146 bt_stream_class_freeze(stream_class);
147 BT_LIB_LOGD("Created stream object: %!+s", stream);
8deee039 148 goto end;
3230ee6b 149
8deee039 150error:
8138bfe1 151 BT_OBJECT_PUT_REF_AND_RESET(stream);
8deee039
PP
152
153end:
154 return stream;
273b65be
JG
155}
156
10b7a2e4
PP
157struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class,
158 struct bt_trace *trace)
273b65be 159{
7b33a0e0
PP
160 uint64_t id;
161
162 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
10b7a2e4 163 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
164 BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id,
165 "Stream class does not automatically assigns stream IDs: "
166 "%![sc-]+S", stream_class);
10b7a2e4
PP
167 id = bt_trace_get_automatic_stream_id(trace, stream_class);
168 return create_stream_with_id(stream_class, trace, id);
7b33a0e0 169}
d7b1ea66 170
78cf9df6 171struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class,
10b7a2e4 172 struct bt_trace *trace, uint64_t id)
7b33a0e0 173{
10b7a2e4
PP
174 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
175 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
176 BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id,
177 "Stream class automatically assigns stream IDs: "
178 "%![sc-]+S", stream_class);
10b7a2e4 179 return create_stream_with_id(stream_class, trace, id);
273b65be 180}
b71d7298 181
5fe68922 182struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
af9296f3 183{
7b33a0e0
PP
184 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
185 return stream->class;
af9296f3
JG
186}
187
78cf9df6
PP
188const struct bt_stream_class *bt_stream_borrow_class_const(
189 const struct bt_stream *stream)
9e550e5f 190{
78cf9df6 191 return bt_stream_borrow_class((void *) stream);
9e550e5f
PP
192}
193
10b7a2e4
PP
194struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream)
195{
196 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
197 return bt_stream_borrow_trace_inline(stream);
198}
199
200const struct bt_trace *bt_stream_borrow_trace_const(
201 const struct bt_stream *stream)
202{
203 return bt_stream_borrow_trace((void *) stream);
204}
205
78cf9df6 206const char *bt_stream_get_name(const struct bt_stream *stream)
b71d7298 207{
7b33a0e0
PP
208 BT_ASSERT_PRE_NON_NULL(stream, "Stream class");
209 return stream->name.value;
b71d7298 210}
98a4cbef 211
78cf9df6 212int bt_stream_set_name(struct bt_stream *stream, const char *name)
98a4cbef 213{
7b33a0e0
PP
214 BT_ASSERT_PRE_NON_NULL(stream, "Clock class");
215 BT_ASSERT_PRE_NON_NULL(name, "Name");
216 BT_ASSERT_PRE_STREAM_HOT(stream);
217 g_string_assign(stream->name.str, name);
218 stream->name.value = stream->name.str->str;
9e550e5f 219 BT_LIB_LOGV("Set stream class's name: %!+s", stream);
7b33a0e0
PP
220 return 0;
221}
18acc6f8 222
78cf9df6 223uint64_t bt_stream_get_id(const struct bt_stream *stream)
7b33a0e0
PP
224{
225 BT_ASSERT_PRE_NON_NULL(stream, "Stream class");
226 return stream->id;
227}
18acc6f8 228
7b33a0e0 229BT_HIDDEN
78cf9df6 230void _bt_stream_freeze(const struct bt_stream *stream)
7b33a0e0 231{
939190b3 232 /* The field classes and default clock class are already frozen */
7b33a0e0
PP
233 BT_ASSERT(stream);
234 BT_LIB_LOGD("Freezing stream: %!+s", stream);
78cf9df6 235 ((struct bt_stream *) stream)->frozen = true;
98a4cbef 236}
This page took 0.067433 seconds and 4 git commands to generate.