Commit | Line | Data |
---|---|---|
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> |
9e550e5f | 29 | #include <babeltrace/trace-ir/private-stream.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 | 48 | static |
7b33a0e0 | 49 | void 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); | |
57 | } | |
58 | ||
a6918753 | 59 | bt_object_pool_finalize(&stream->packet_pool); |
8deee039 | 60 | g_free(stream); |
c9af50d1 JG |
61 | } |
62 | ||
7b33a0e0 PP |
63 | static |
64 | void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream) | |
273b65be | 65 | { |
7b33a0e0 PP |
66 | bt_packet_destroy(packet); |
67 | } | |
19abc2c6 | 68 | |
7b33a0e0 PP |
69 | BT_ASSERT_PRE_FUNC |
70 | static inline | |
71 | bool stream_id_is_unique(struct bt_trace *trace, | |
72 | struct bt_stream_class *stream_class, uint64_t id) | |
73 | { | |
74 | uint64_t i; | |
75 | bool is_unique = true; | |
273b65be | 76 | |
7b33a0e0 PP |
77 | for (i = 0; i < trace->streams->len; i++) { |
78 | struct bt_stream *stream = trace->streams->pdata[i]; | |
273b65be | 79 | |
7b33a0e0 PP |
80 | if (stream->class != stream_class) { |
81 | continue; | |
8bfa3f9c | 82 | } |
daaa1851 | 83 | |
7b33a0e0 PP |
84 | if (stream->id == id) { |
85 | is_unique = false; | |
86 | goto end; | |
98edd02c | 87 | } |
273b65be JG |
88 | } |
89 | ||
8deee039 | 90 | end: |
7b33a0e0 | 91 | return is_unique; |
a6918753 PP |
92 | } |
93 | ||
273b65be | 94 | static |
7b33a0e0 PP |
95 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
96 | uint64_t id) | |
273b65be | 97 | { |
8deee039 | 98 | int ret; |
7b33a0e0 PP |
99 | struct bt_stream *stream; |
100 | struct bt_trace *trace; | |
101 | ||
102 | BT_ASSERT(stream_class); | |
103 | trace = bt_stream_class_borrow_trace_inline(stream_class); | |
104 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), | |
105 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); | |
106 | BT_ASSERT_PRE(!trace->is_static, | |
107 | "Trace is static: %![trace-]+t", trace); | |
108 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, | |
109 | trace, id); | |
8deee039 PP |
110 | stream = g_new0(struct bt_stream, 1); |
111 | if (!stream) { | |
112 | BT_LOGE_STR("Failed to allocate one stream."); | |
113 | goto error; | |
12c8a1a3 | 114 | } |
b71d7298 | 115 | |
7b33a0e0 PP |
116 | bt_object_init_shared_with_parent(&stream->base, destroy_stream); |
117 | stream->name.str = g_string_new(NULL); | |
118 | if (!stream->name.str) { | |
119 | BT_LOGE_STR("Failed to allocate a GString."); | |
8deee039 | 120 | goto error; |
b71d7298 | 121 | } |
41ac640a | 122 | |
7b33a0e0 | 123 | stream->id = id; |
a6918753 PP |
124 | ret = bt_object_pool_initialize(&stream->packet_pool, |
125 | (bt_object_pool_new_object_func) bt_packet_new, | |
126 | (bt_object_pool_destroy_object_func) bt_stream_free_packet, | |
127 | stream); | |
128 | if (ret) { | |
129 | BT_LOGE("Failed to initialize packet pool: ret=%d", ret); | |
130 | goto error; | |
131 | } | |
132 | ||
7b33a0e0 PP |
133 | stream->class = stream_class; |
134 | bt_trace_add_stream(trace, stream); | |
135 | bt_stream_class_freeze(stream_class); | |
136 | BT_LIB_LOGD("Created stream object: %!+s", stream); | |
8deee039 | 137 | goto end; |
3230ee6b | 138 | |
8deee039 | 139 | error: |
8138bfe1 | 140 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
8deee039 PP |
141 | |
142 | end: | |
143 | return stream; | |
273b65be JG |
144 | } |
145 | ||
9e550e5f PP |
146 | struct bt_private_stream *bt_private_stream_create( |
147 | struct bt_private_stream_class *priv_stream_class) | |
273b65be | 148 | { |
9e550e5f | 149 | struct bt_stream_class *stream_class = (void *) priv_stream_class; |
7b33a0e0 PP |
150 | uint64_t id; |
151 | ||
152 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); | |
153 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, | |
154 | "Stream class does not automatically assigns stream IDs: " | |
155 | "%![sc-]+S", stream_class); | |
156 | id = bt_trace_get_automatic_stream_id( | |
157 | bt_stream_class_borrow_trace_inline(stream_class), | |
158 | stream_class); | |
9e550e5f | 159 | return (void *) create_stream_with_id(stream_class, id); |
7b33a0e0 | 160 | } |
d7b1ea66 | 161 | |
9e550e5f PP |
162 | struct bt_private_stream *bt_private_stream_create_with_id( |
163 | struct bt_private_stream_class *priv_stream_class, | |
7b33a0e0 PP |
164 | uint64_t id) |
165 | { | |
9e550e5f PP |
166 | struct bt_stream_class *stream_class = (void *) priv_stream_class; |
167 | ||
7b33a0e0 PP |
168 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, |
169 | "Stream class automatically assigns stream IDs: " | |
170 | "%![sc-]+S", stream_class); | |
9e550e5f | 171 | return (void *) create_stream_with_id(stream_class, id); |
273b65be | 172 | } |
b71d7298 | 173 | |
5fe68922 | 174 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
af9296f3 | 175 | { |
7b33a0e0 PP |
176 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
177 | return stream->class; | |
af9296f3 JG |
178 | } |
179 | ||
96854e6a | 180 | struct bt_private_stream_class *bt_private_stream_borrow_class( |
9e550e5f PP |
181 | struct bt_private_stream *priv_stream) |
182 | { | |
183 | return (void *) bt_stream_borrow_class((void *) priv_stream); | |
184 | } | |
185 | ||
839d52a5 | 186 | const char *bt_stream_get_name(struct bt_stream *stream) |
b71d7298 | 187 | { |
7b33a0e0 PP |
188 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); |
189 | return stream->name.value; | |
b71d7298 | 190 | } |
98a4cbef | 191 | |
9e550e5f PP |
192 | int bt_private_stream_set_name(struct bt_private_stream *priv_stream, |
193 | const char *name) | |
98a4cbef | 194 | { |
9e550e5f PP |
195 | struct bt_stream *stream = (void *) priv_stream; |
196 | ||
7b33a0e0 PP |
197 | BT_ASSERT_PRE_NON_NULL(stream, "Clock class"); |
198 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
199 | BT_ASSERT_PRE_STREAM_HOT(stream); | |
200 | g_string_assign(stream->name.str, name); | |
201 | stream->name.value = stream->name.str->str; | |
9e550e5f | 202 | BT_LIB_LOGV("Set stream class's name: %!+s", stream); |
7b33a0e0 PP |
203 | return 0; |
204 | } | |
18acc6f8 | 205 | |
7b33a0e0 PP |
206 | uint64_t bt_stream_get_id(struct bt_stream *stream) |
207 | { | |
208 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); | |
209 | return stream->id; | |
210 | } | |
18acc6f8 | 211 | |
7b33a0e0 PP |
212 | BT_HIDDEN |
213 | void _bt_stream_freeze(struct bt_stream *stream) | |
214 | { | |
939190b3 | 215 | /* The field classes and default clock class are already frozen */ |
7b33a0e0 PP |
216 | BT_ASSERT(stream); |
217 | BT_LIB_LOGD("Freezing stream: %!+s", stream); | |
218 | stream->frozen = true; | |
98a4cbef | 219 | } |
9e550e5f PP |
220 | |
221 | struct bt_stream *bt_stream_borrow_from_private( | |
222 | struct bt_private_stream *priv_stream) | |
223 | { | |
224 | return (void *) priv_stream; | |
225 | } |