lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / include / babeltrace / ctf-ir / trace-internal.h
1 #ifndef BABELTRACE_CTF_IR_TRACE_INTERNAL_H
2 #define BABELTRACE_CTF_IR_TRACE_INTERNAL_H
3
4 /*
5 * BabelTrace - CTF IR: Trace internal
6 *
7 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/trace.h>
32 #include <babeltrace/ctf-ir/stream-class-internal.h>
33 #include <babeltrace/ctf-ir/field-types.h>
34 #include <babeltrace/ctf-ir/fields.h>
35 #include <babeltrace/ctf-ir/validation-internal.h>
36 #include <babeltrace/ctf-ir/attributes-internal.h>
37 #include <babeltrace/ctf-ir/clock-class-internal.h>
38 #include <babeltrace/object-internal.h>
39 #include <babeltrace/object-pool-internal.h>
40 #include <babeltrace/babeltrace-internal.h>
41 #include <babeltrace/values.h>
42 #include <babeltrace/types.h>
43 #include <glib.h>
44 #include <sys/types.h>
45 #include <babeltrace/compat/uuid-internal.h>
46
47 struct bt_trace_common {
48 struct bt_object base;
49 GString *name;
50 int frozen;
51 unsigned char uuid[BABELTRACE_UUID_LEN];
52 bt_bool uuid_set;
53 enum bt_byte_order native_byte_order;
54 struct bt_value *environment;
55 GPtrArray *clock_classes; /* Array of pointers to bt_clock_class */
56 GPtrArray *stream_classes; /* Array of ptrs to bt_stream_class_common */
57 GPtrArray *streams; /* Array of ptrs to bt_stream_common */
58 struct bt_field_type_common *packet_header_field_type;
59 int64_t next_stream_id;
60
61 /*
62 * This flag indicates if the trace is valid. A valid
63 * trace is _always_ frozen.
64 */
65 int valid;
66 };
67
68 struct bt_trace {
69 struct bt_trace_common common;
70 GPtrArray *listeners; /* Array of struct listener_wrapper */
71 GArray *is_static_listeners;
72 bt_bool is_static;
73 bt_bool in_remove_listener;
74
75 /* Pool of `struct bt_field_wrapper *` */
76 struct bt_object_pool packet_header_field_pool;
77 };
78
79 BT_HIDDEN
80 int bt_trace_object_modification(struct bt_visitor_object *object,
81 void *trace_ptr);
82
83 BT_HIDDEN
84 bt_bool bt_trace_common_has_clock_class(struct bt_trace_common *trace,
85 struct bt_clock_class *clock_class);
86
87 /**
88 @brief User function type to use with bt_trace_add_listener().
89
90 @param[in] obj New CTF IR object which is part of the trace
91 class hierarchy.
92 @param[in] data User data.
93
94 @prenotnull{obj}
95 */
96 typedef void (*bt_listener_cb)(struct bt_visitor_object *obj, void *data);
97
98 /**
99 @brief Adds the trace class modification listener \p listener to
100 the CTF IR trace class \p trace_class.
101
102 Once you add \p listener to \p trace_class, whenever \p trace_class
103 is modified, \p listener is called with the new element and with
104 \p data (user data).
105
106 @param[in] trace_class Trace class to which to add \p listener.
107 @param[in] listener Modification listener function.
108 @param[in] data User data.
109 @returns 0 on success, or a negative value on error.
110
111 @prenotnull{trace_class}
112 @prenotnull{listener}
113 @postrefcountsame{trace_class}
114 */
115 BT_HIDDEN
116 int bt_trace_add_listener(struct bt_trace *trace_class,
117 bt_listener_cb listener, void *data);
118
119 BT_HIDDEN
120 int bt_trace_common_initialize(struct bt_trace_common *trace,
121 bt_object_release_func release_func);
122
123 BT_HIDDEN
124 void bt_trace_common_finalize(struct bt_trace_common *trace);
125
126 static inline
127 const char *bt_trace_common_get_name(struct bt_trace_common *trace)
128 {
129 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
130 return trace->name ? trace->name->str : NULL;
131 }
132
133 BT_HIDDEN
134 int bt_trace_common_set_name(struct bt_trace_common *trace, const char *name);
135
136 static inline
137 const unsigned char *bt_trace_common_get_uuid(struct bt_trace_common *trace)
138 {
139 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
140 return trace->uuid_set ? trace->uuid : NULL;
141 }
142
143 BT_HIDDEN
144 int bt_trace_common_set_uuid(struct bt_trace_common *trace, const unsigned char *uuid);
145
146 BT_HIDDEN
147 int bt_trace_common_set_environment_field(struct bt_trace_common *trace,
148 const char *name, struct bt_value *value);
149
150 BT_HIDDEN
151 int bt_trace_common_set_environment_field_string(struct bt_trace_common *trace,
152 const char *name, const char *value);
153
154 BT_HIDDEN
155 int bt_trace_common_set_environment_field_integer(struct bt_trace_common *trace,
156 const char *name, int64_t value);
157
158 static inline
159 int64_t bt_trace_common_get_environment_field_count(
160 struct bt_trace_common *trace)
161 {
162 int64_t ret;
163
164 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
165 ret = bt_attributes_get_count(trace->environment);
166 BT_ASSERT(ret >= 0);
167 return ret;
168 }
169
170 static inline
171 const char *
172 bt_trace_common_get_environment_field_name_by_index(
173 struct bt_trace_common *trace, uint64_t index)
174 {
175 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
176 return bt_attributes_get_field_name(trace->environment, index);
177 }
178
179 static inline
180 struct bt_value *bt_trace_common_borrow_environment_field_value_by_index(
181 struct bt_trace_common *trace, uint64_t index)
182 {
183 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
184 return bt_attributes_borrow_field_value(trace->environment, index);
185 }
186
187 static inline
188 struct bt_value *bt_trace_common_borrow_environment_field_value_by_name(
189 struct bt_trace_common *trace, const char *name)
190 {
191 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
192 BT_ASSERT_PRE_NON_NULL(name, "Name");
193 return bt_attributes_borrow_field_value_by_name(trace->environment,
194 name);
195 }
196
197 BT_HIDDEN
198 int bt_trace_common_add_clock_class(struct bt_trace_common *trace,
199 struct bt_clock_class *clock_class);
200
201 static inline
202 int64_t bt_trace_common_get_clock_class_count(struct bt_trace_common *trace)
203 {
204 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
205 return trace->clock_classes->len;
206 }
207
208 static inline
209 struct bt_clock_class *bt_trace_common_borrow_clock_class_by_index(
210 struct bt_trace_common *trace, uint64_t index)
211 {
212 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
213 BT_ASSERT_PRE(index < trace->clock_classes->len,
214 "Index is out of bounds: index=%" PRIu64 ", "
215 "count=%u",
216 index, trace->clock_classes->len);
217 return g_ptr_array_index(trace->clock_classes, index);
218 }
219
220 static inline
221 int64_t bt_trace_common_get_stream_count(struct bt_trace_common *trace)
222 {
223 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
224 return (int64_t) trace->streams->len;
225 }
226
227 static inline
228 struct bt_stream_common *bt_trace_common_borrow_stream_by_index(
229 struct bt_trace_common *trace,
230 uint64_t index)
231 {
232 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
233 BT_ASSERT_PRE(index < trace->streams->len,
234 "Index is out of bounds: index=%" PRIu64 ", "
235 "count=%u",
236 index, trace->streams->len);
237 return g_ptr_array_index(trace->streams, index);
238 }
239
240 static inline
241 int64_t bt_trace_common_get_stream_class_count(struct bt_trace_common *trace)
242 {
243 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
244 return (int64_t) trace->stream_classes->len;
245 }
246
247 static inline
248 struct bt_stream_class_common *bt_trace_common_borrow_stream_class_by_index(
249 struct bt_trace_common *trace, uint64_t index)
250 {
251 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
252 BT_ASSERT_PRE(index < trace->stream_classes->len,
253 "Index is out of bounds: index=%" PRIu64 ", "
254 "count=%u",
255 index, trace->stream_classes->len);
256 return g_ptr_array_index(trace->stream_classes, index);
257 }
258
259 static inline
260 struct bt_stream_class_common *bt_trace_common_borrow_stream_class_by_id(
261 struct bt_trace_common *trace, uint64_t id_param)
262 {
263 int i;
264 struct bt_stream_class_common *stream_class = NULL;
265 int64_t id = (int64_t) id_param;
266
267 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
268 BT_ASSERT_PRE(id >= 0,
269 "Invalid stream class ID: %" PRIu64, id_param);
270
271 for (i = 0; i < trace->stream_classes->len; i++) {
272 struct bt_stream_class_common *stream_class_candidate;
273
274 stream_class_candidate =
275 g_ptr_array_index(trace->stream_classes, i);
276
277 if (bt_stream_class_common_get_id(stream_class_candidate) ==
278 (int64_t) id) {
279 stream_class = stream_class_candidate;
280 goto end;
281 }
282 }
283
284 end:
285 return stream_class;
286 }
287
288 static inline
289 struct bt_clock_class *bt_trace_common_borrow_clock_class_by_name(
290 struct bt_trace_common *trace, const char *name)
291 {
292 size_t i;
293 struct bt_clock_class *clock_class = NULL;
294
295 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
296 BT_ASSERT_PRE_NON_NULL(name, "Name");
297
298 for (i = 0; i < trace->clock_classes->len; i++) {
299 struct bt_clock_class *cur_clk =
300 g_ptr_array_index(trace->clock_classes, i);
301 const char *cur_clk_name = bt_clock_class_get_name(cur_clk);
302
303 if (!cur_clk_name) {
304 goto end;
305 }
306
307 if (!strcmp(cur_clk_name, name)) {
308 clock_class = cur_clk;
309 goto end;
310 }
311 }
312
313 end:
314 return clock_class;
315 }
316
317 static inline
318 enum bt_byte_order bt_trace_common_get_native_byte_order(
319 struct bt_trace_common *trace)
320 {
321 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
322 return trace->native_byte_order;
323 }
324
325 BT_HIDDEN
326 int bt_trace_common_set_native_byte_order(struct bt_trace_common *trace,
327 enum bt_byte_order byte_order, bool allow_unspecified);
328
329 static inline
330 struct bt_field_type_common *bt_trace_common_borrow_packet_header_field_type(
331 struct bt_trace_common *trace)
332 {
333 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
334 return trace->packet_header_field_type;
335 }
336
337 BT_HIDDEN
338 int bt_trace_common_set_packet_header_field_type(struct bt_trace_common *trace,
339 struct bt_field_type_common *packet_header_field_type);
340
341 static inline
342 void bt_trace_common_freeze(struct bt_trace_common *trace)
343 {
344 int i;
345
346 if (trace->frozen) {
347 return;
348 }
349
350 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
351 trace, bt_trace_common_get_name(trace));
352 BT_LOGD_STR("Freezing packet header field type.");
353 bt_field_type_common_freeze(trace->packet_header_field_type);
354 BT_LOGD_STR("Freezing environment attributes.");
355 bt_attributes_freeze(trace->environment);
356
357 if (trace->clock_classes->len > 0) {
358 BT_LOGD_STR("Freezing clock classes.");
359 }
360
361 for (i = 0; i < trace->clock_classes->len; i++) {
362 struct bt_clock_class *clock_class =
363 g_ptr_array_index(trace->clock_classes, i);
364
365 bt_clock_class_freeze(clock_class);
366 }
367
368 trace->frozen = 1;
369 }
370
371 BT_HIDDEN
372 int bt_trace_common_add_stream_class(struct bt_trace_common *trace,
373 struct bt_stream_class_common *stream_class,
374 bt_validation_flag_copy_field_type_func copy_field_type_func,
375 struct bt_clock_class *init_expected_clock_class,
376 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
377 struct bt_field_type_common *packet_context_field_type,
378 struct bt_field_type_common *event_header_field_type),
379 bool check_ts_begin_end_mapped);
380
381 #endif /* BABELTRACE_CTF_IR_TRACE_INTERNAL_H */
This page took 0.036213 seconds and 4 git commands to generate.