4 * Babeltrace CTF IR - Stream
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_TAG "STREAM"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/stream.h>
34 #include <babeltrace/ctf-ir/stream-internal.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/trace.h>
38 #include <babeltrace/ctf-ir/trace-internal.h>
39 #include <babeltrace/ctf-ir/packet-internal.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/align-internal.h>
43 #include <babeltrace/assert-internal.h>
44 #include <babeltrace/property-internal.h>
48 #define BT_ASSERT_PRE_STREAM_HOT(_stream) \
49 BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream))
52 void destroy_stream(struct bt_object
*obj
)
54 struct bt_stream
*stream
= (void *) obj
;
56 BT_LIB_LOGD("Destroying stream object: %!+s", stream
);
58 if (stream
->name
.str
) {
59 g_string_free(stream
->name
.str
, TRUE
);
62 bt_object_pool_finalize(&stream
->packet_pool
);
67 void bt_stream_free_packet(struct bt_packet
*packet
, struct bt_stream
*stream
)
69 bt_packet_destroy(packet
);
74 bool stream_id_is_unique(struct bt_trace
*trace
,
75 struct bt_stream_class
*stream_class
, uint64_t id
)
78 bool is_unique
= true;
80 for (i
= 0; i
< trace
->streams
->len
; i
++) {
81 struct bt_stream
*stream
= trace
->streams
->pdata
[i
];
83 if (stream
->class != stream_class
) {
87 if (stream
->id
== id
) {
98 struct bt_stream
*create_stream_with_id(struct bt_stream_class
*stream_class
,
102 struct bt_stream
*stream
;
103 struct bt_trace
*trace
;
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
,
113 stream
= g_new0(struct bt_stream
, 1);
115 BT_LOGE_STR("Failed to allocate one stream.");
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.");
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
,
132 BT_LOGE("Failed to initialize packet pool: ret=%d", ret
);
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
);
149 struct bt_stream
*bt_stream_create(struct bt_stream_class
*stream_class
)
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
),
160 return create_stream_with_id(stream_class
, id
);
163 struct bt_stream
*bt_stream_create_with_id(struct bt_stream_class
*stream_class
,
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
);
172 struct bt_stream_class
*bt_stream_borrow_class(struct bt_stream
*stream
)
174 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
175 return stream
->class;
178 const char *bt_stream_get_name(struct bt_stream
*stream
)
180 BT_ASSERT_PRE_NON_NULL(stream
, "Stream class");
181 return stream
->name
.value
;
184 int bt_stream_set_name(struct bt_stream
*stream
, const char *name
)
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
);
195 uint64_t bt_stream_get_id(struct bt_stream
*stream
)
197 BT_ASSERT_PRE_NON_NULL(stream
, "Stream class");
202 void _bt_stream_freeze(struct bt_stream
*stream
)
204 /* The field types and default clock class are already frozen */
206 BT_LIB_LOGD("Freezing stream: %!+s", stream
);
207 stream
->frozen
= true;