Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * stream.c | |
3 | * | |
108b91d0 | 4 | * Babeltrace trace IR - Stream |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
19abc2c6 PP |
29 | #define BT_LOG_TAG "STREAM" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
44ea72c5 | 32 | #include <babeltrace/assert-pre-internal.h> |
108b91d0 PP |
33 | #include <babeltrace/trace-ir/stream.h> |
34 | #include <babeltrace/trace-ir/stream-internal.h> | |
35 | #include <babeltrace/trace-ir/stream-class.h> | |
36 | #include <babeltrace/trace-ir/stream-class-internal.h> | |
37 | #include <babeltrace/trace-ir/trace.h> | |
38 | #include <babeltrace/trace-ir/trace-internal.h> | |
39 | #include <babeltrace/trace-ir/packet-internal.h> | |
8138bfe1 | 40 | #include <babeltrace/object.h> |
8deee039 PP |
41 | #include <babeltrace/compiler-internal.h> |
42 | #include <babeltrace/align-internal.h> | |
43 | #include <babeltrace/assert-internal.h> | |
7b33a0e0 | 44 | #include <babeltrace/property-internal.h> |
8deee039 PP |
45 | #include <inttypes.h> |
46 | #include <unistd.h> | |
12c8a1a3 | 47 | |
7b33a0e0 PP |
48 | #define BT_ASSERT_PRE_STREAM_HOT(_stream) \ |
49 | BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream)) | |
263a7df5 | 50 | |
c9af50d1 | 51 | static |
7b33a0e0 | 52 | void destroy_stream(struct bt_object *obj) |
c9af50d1 | 53 | { |
8deee039 | 54 | struct bt_stream *stream = (void *) obj; |
c9af50d1 | 55 | |
7b33a0e0 PP |
56 | BT_LIB_LOGD("Destroying stream object: %!+s", stream); |
57 | ||
58 | if (stream->name.str) { | |
59 | g_string_free(stream->name.str, TRUE); | |
60 | } | |
61 | ||
a6918753 | 62 | bt_object_pool_finalize(&stream->packet_pool); |
8deee039 | 63 | g_free(stream); |
c9af50d1 JG |
64 | } |
65 | ||
7b33a0e0 PP |
66 | static |
67 | void 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 |
72 | BT_ASSERT_PRE_FUNC |
73 | static inline | |
74 | bool 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 | 93 | end: |
7b33a0e0 | 94 | return is_unique; |
a6918753 PP |
95 | } |
96 | ||
273b65be | 97 | static |
7b33a0e0 PP |
98 | struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class, |
99 | uint64_t id) | |
273b65be | 100 | { |
8deee039 | 101 | int ret; |
7b33a0e0 PP |
102 | struct bt_stream *stream; |
103 | struct bt_trace *trace; | |
104 | ||
105 | BT_ASSERT(stream_class); | |
106 | trace = bt_stream_class_borrow_trace_inline(stream_class); | |
107 | BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id), | |
108 | "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id); | |
109 | BT_ASSERT_PRE(!trace->is_static, | |
110 | "Trace is static: %![trace-]+t", trace); | |
111 | BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64, | |
112 | trace, id); | |
8deee039 PP |
113 | stream = g_new0(struct bt_stream, 1); |
114 | if (!stream) { | |
115 | BT_LOGE_STR("Failed to allocate one stream."); | |
116 | goto error; | |
12c8a1a3 | 117 | } |
b71d7298 | 118 | |
7b33a0e0 PP |
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."); | |
8deee039 | 123 | goto error; |
b71d7298 | 124 | } |
41ac640a | 125 | |
7b33a0e0 | 126 | stream->id = id; |
a6918753 PP |
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 | ||
7b33a0e0 PP |
136 | stream->class = stream_class; |
137 | bt_trace_add_stream(trace, stream); | |
138 | bt_stream_class_freeze(stream_class); | |
139 | BT_LIB_LOGD("Created stream object: %!+s", stream); | |
8deee039 | 140 | goto end; |
3230ee6b | 141 | |
8deee039 | 142 | error: |
8138bfe1 | 143 | BT_OBJECT_PUT_REF_AND_RESET(stream); |
8deee039 PP |
144 | |
145 | end: | |
146 | return stream; | |
273b65be JG |
147 | } |
148 | ||
7b33a0e0 | 149 | struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class) |
273b65be | 150 | { |
7b33a0e0 PP |
151 | uint64_t id; |
152 | ||
153 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); | |
154 | BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id, | |
155 | "Stream class does not automatically assigns stream IDs: " | |
156 | "%![sc-]+S", stream_class); | |
157 | id = bt_trace_get_automatic_stream_id( | |
158 | bt_stream_class_borrow_trace_inline(stream_class), | |
159 | stream_class); | |
160 | return create_stream_with_id(stream_class, id); | |
161 | } | |
d7b1ea66 | 162 | |
7b33a0e0 PP |
163 | struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class, |
164 | uint64_t id) | |
165 | { | |
166 | BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id, | |
167 | "Stream class automatically assigns stream IDs: " | |
168 | "%![sc-]+S", stream_class); | |
169 | return create_stream_with_id(stream_class, id); | |
273b65be | 170 | } |
b71d7298 | 171 | |
5fe68922 | 172 | struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream) |
af9296f3 | 173 | { |
7b33a0e0 PP |
174 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
175 | return stream->class; | |
af9296f3 JG |
176 | } |
177 | ||
839d52a5 | 178 | const char *bt_stream_get_name(struct bt_stream *stream) |
b71d7298 | 179 | { |
7b33a0e0 PP |
180 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); |
181 | return stream->name.value; | |
b71d7298 | 182 | } |
98a4cbef | 183 | |
7b33a0e0 | 184 | int bt_stream_set_name(struct bt_stream *stream, const char *name) |
98a4cbef | 185 | { |
7b33a0e0 PP |
186 | BT_ASSERT_PRE_NON_NULL(stream, "Clock class"); |
187 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
188 | BT_ASSERT_PRE_STREAM_HOT(stream); | |
189 | g_string_assign(stream->name.str, name); | |
190 | stream->name.value = stream->name.str->str; | |
191 | BT_LIB_LOGV("Set stream class's name: %!+S", stream); | |
192 | return 0; | |
193 | } | |
18acc6f8 | 194 | |
7b33a0e0 PP |
195 | uint64_t bt_stream_get_id(struct bt_stream *stream) |
196 | { | |
197 | BT_ASSERT_PRE_NON_NULL(stream, "Stream class"); | |
198 | return stream->id; | |
199 | } | |
18acc6f8 | 200 | |
7b33a0e0 PP |
201 | BT_HIDDEN |
202 | void _bt_stream_freeze(struct bt_stream *stream) | |
203 | { | |
939190b3 | 204 | /* The field classes and default clock class are already frozen */ |
7b33a0e0 PP |
205 | BT_ASSERT(stream); |
206 | BT_LIB_LOGD("Freezing stream: %!+s", stream); | |
207 | stream->frozen = true; | |
98a4cbef | 208 | } |