83f0bd4136161fda5edf210a6349e8acfe041100
[babeltrace.git] / include / babeltrace / ctf-writer / stream-class-internal.h
1 #ifndef BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H
3
4 /*
5 * Copyright 2014 EfficiOS Inc.
6 *
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * The Common Trace Format (CTF) Specification is available at
28 * http://www.efficios.com/ctf
29 */
30
31 #include <babeltrace/assert-internal.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/common-internal.h>
34 #include <babeltrace/ctf-writer/clock-internal.h>
35 #include <babeltrace/ctf-writer/field-types-internal.h>
36 #include <babeltrace/ctf-writer/field-types.h>
37 #include <babeltrace/ctf-writer/stream-class-internal.h>
38 #include <babeltrace/ctf-writer/utils-internal.h>
39 #include <babeltrace/ctf-writer/validation-internal.h>
40 #include <babeltrace/ctf-writer/visitor.h>
41 #include <babeltrace/ctf-writer/object-internal.h>
42 #include <inttypes.h>
43
44 struct bt_ctf_stream_class_common {
45 struct bt_ctf_object base;
46 GString *name;
47
48 /* Array of pointers to event class addresses */
49 GPtrArray *event_classes;
50
51 /* event class id (int64_t) to event class address */
52 GHashTable *event_classes_ht;
53 int id_set;
54 int64_t id;
55 int64_t next_event_id;
56 struct bt_ctf_field_type_common *packet_context_field_type;
57 struct bt_ctf_field_type_common *event_header_field_type;
58 struct bt_ctf_field_type_common *event_context_field_type;
59 int frozen;
60 int byte_order;
61
62 /*
63 * This flag indicates if the stream class is valid. A valid
64 * stream class is _always_ frozen.
65 */
66 int valid;
67
68 /*
69 * Unique clock class mapped to any field type within this
70 * stream class, including all the stream class's event class
71 * field types. This is only set if the stream class is frozen.
72 *
73 * If the stream class is frozen and this is still NULL, it is
74 * still possible that it becomes non-NULL because
75 * bt_ctf_stream_class_add_event_class() can add an event class
76 * containing a field type mapped to some clock class. In this
77 * case, this is the mapped clock class, and at this point, both
78 * the new event class and the stream class are frozen, so the
79 * next added event classes are expected to contain field types
80 * which only map to this specific clock class.
81 *
82 * If this is a CTF writer stream class, then this is the
83 * backing clock class of the `clock` member above.
84 */
85 struct bt_ctf_clock_class *clock_class;
86 };
87
88 struct bt_ctf_event_class_common;
89
90 BT_HIDDEN
91 int bt_ctf_stream_class_common_initialize(struct bt_ctf_stream_class_common *stream_class,
92 const char *name, bt_ctf_object_release_func release_func);
93
94 BT_HIDDEN
95 void bt_ctf_stream_class_common_finalize(struct bt_ctf_stream_class_common *stream_class);
96
97 BT_HIDDEN
98 void bt_ctf_stream_class_common_freeze(struct bt_ctf_stream_class_common *stream_class);
99
100 static inline
101 const char *bt_ctf_stream_class_common_get_name(
102 struct bt_ctf_stream_class_common *stream_class)
103 {
104 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
105 return stream_class->name->len > 0 ? stream_class->name->str : NULL;
106 }
107
108 static inline
109 int64_t bt_ctf_stream_class_common_get_id(
110 struct bt_ctf_stream_class_common *stream_class)
111 {
112 int64_t ret;
113
114 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
115
116 if (!stream_class->id_set) {
117 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
118 stream_class,
119 bt_ctf_stream_class_common_get_name(stream_class));
120 ret = (int64_t) -1;
121 goto end;
122 }
123
124 ret = stream_class->id;
125
126 end:
127 return ret;
128 }
129
130 BT_HIDDEN
131 void bt_ctf_stream_class_common_set_byte_order(
132 struct bt_ctf_stream_class_common *stream_class, int byte_order);
133
134 BT_HIDDEN
135 int bt_ctf_stream_class_common_validate_single_clock_class(
136 struct bt_ctf_stream_class_common *stream_class,
137 struct bt_ctf_clock_class **expected_clock_class);
138
139 BT_HIDDEN
140 int bt_ctf_stream_class_common_add_event_class(
141 struct bt_ctf_stream_class_common *stream_class,
142 struct bt_ctf_event_class_common *event_class,
143 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func);
144
145 BT_HIDDEN
146 int bt_ctf_stream_class_common_visit(struct bt_ctf_stream_class_common *stream_class,
147 bt_ctf_visitor visitor, void *data);
148
149 BT_HIDDEN
150 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
151 bt_ctf_visitor visitor, void *data);
152
153 static inline
154 struct bt_ctf_trace_common *bt_ctf_stream_class_common_borrow_trace(
155 struct bt_ctf_stream_class_common *stream_class)
156 {
157 BT_ASSERT(stream_class);
158 return (void *) bt_ctf_object_borrow_parent(&stream_class->base);
159 }
160
161 static inline
162 int bt_ctf_stream_class_common_set_name(struct bt_ctf_stream_class_common *stream_class,
163 const char *name)
164 {
165 int ret = 0;
166
167 if (!stream_class) {
168 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
169 ret = -1;
170 goto end;
171 }
172
173 if (stream_class->frozen) {
174 BT_LOGW("Invalid parameter: stream class is frozen: "
175 "addr=%p, name=\"%s\", id=%" PRId64,
176 stream_class,
177 bt_ctf_stream_class_common_get_name(stream_class),
178 bt_ctf_stream_class_common_get_id(stream_class));
179 ret = -1;
180 goto end;
181 }
182
183 if (!name) {
184 g_string_assign(stream_class->name, "");
185 } else {
186 if (strlen(name) == 0) {
187 BT_LOGW("Invalid parameter: name is empty.");
188 ret = -1;
189 goto end;
190 }
191
192 g_string_assign(stream_class->name, name);
193 }
194
195 BT_LOGV("Set stream class's name: "
196 "addr=%p, name=\"%s\", id=%" PRId64,
197 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
198 bt_ctf_stream_class_common_get_id(stream_class));
199 end:
200 return ret;
201 }
202
203 static inline
204 void _bt_ctf_stream_class_common_set_id(
205 struct bt_ctf_stream_class_common *stream_class, int64_t id)
206 {
207 BT_ASSERT(stream_class);
208 stream_class->id = id;
209 stream_class->id_set = 1;
210 BT_LOGV("Set stream class's ID (internal): "
211 "addr=%p, name=\"%s\", id=%" PRId64,
212 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
213 bt_ctf_stream_class_common_get_id(stream_class));
214 }
215
216 static inline
217 int bt_ctf_stream_class_common_set_id_no_check(
218 struct bt_ctf_stream_class_common *stream_class, int64_t id)
219 {
220 _bt_ctf_stream_class_common_set_id(stream_class, id);
221 return 0;
222 }
223
224 static inline
225 int bt_ctf_stream_class_common_set_id(struct bt_ctf_stream_class_common *stream_class,
226 uint64_t id_param)
227 {
228 int ret = 0;
229 int64_t id = (int64_t) id_param;
230
231 if (!stream_class) {
232 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
233 ret = -1;
234 goto end;
235 }
236
237 if (stream_class->frozen) {
238 BT_LOGW("Invalid parameter: stream class is frozen: "
239 "addr=%p, name=\"%s\", id=%" PRId64,
240 stream_class,
241 bt_ctf_stream_class_common_get_name(stream_class),
242 bt_ctf_stream_class_common_get_id(stream_class));
243 ret = -1;
244 goto end;
245 }
246
247 if (id < 0) {
248 BT_LOGW("Invalid parameter: invalid stream class's ID: "
249 "stream-class-addr=%p, stream-class-name=\"%s\", "
250 "stream-class-id=%" PRId64 ", id=%" PRIu64,
251 stream_class,
252 bt_ctf_stream_class_common_get_name(stream_class),
253 bt_ctf_stream_class_common_get_id(stream_class),
254 id_param);
255 ret = -1;
256 goto end;
257 }
258
259 ret = bt_ctf_stream_class_common_set_id_no_check(stream_class, id);
260 if (ret == 0) {
261 BT_LOGV("Set stream class's ID: "
262 "addr=%p, name=\"%s\", id=%" PRId64,
263 stream_class,
264 bt_ctf_stream_class_common_get_name(stream_class),
265 bt_ctf_stream_class_common_get_id(stream_class));
266 }
267 end:
268 return ret;
269 }
270
271 static inline
272 int64_t bt_ctf_stream_class_common_get_event_class_count(
273 struct bt_ctf_stream_class_common *stream_class)
274 {
275 int64_t ret;
276
277 if (!stream_class) {
278 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
279 ret = (int64_t) -1;
280 goto end;
281 }
282
283 ret = (int64_t) stream_class->event_classes->len;
284 end:
285 return ret;
286 }
287
288 static inline
289 struct bt_ctf_event_class_common *bt_ctf_stream_class_common_borrow_event_class_by_index(
290 struct bt_ctf_stream_class_common *stream_class, uint64_t index)
291 {
292 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
293 BT_ASSERT_PRE(index < stream_class->event_classes->len,
294 "Index is out of bounds: index=%" PRIu64 ", "
295 "count=%u",
296 index, stream_class->event_classes->len);
297 return g_ptr_array_index(stream_class->event_classes, index);
298 }
299
300 static inline
301 struct bt_ctf_event_class_common *bt_ctf_stream_class_common_borrow_event_class_by_id(
302 struct bt_ctf_stream_class_common *stream_class, uint64_t id)
303 {
304 int64_t id_key = (int64_t) id;
305
306 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
307 BT_ASSERT_PRE(id_key >= 0,
308 "Invalid event class ID: %" PRIu64, id);
309 return g_hash_table_lookup(stream_class->event_classes_ht,
310 &id_key);
311 }
312
313 static inline
314 struct bt_ctf_field_type_common *
315 bt_ctf_stream_class_common_borrow_packet_context_field_type(
316 struct bt_ctf_stream_class_common *stream_class)
317 {
318 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
319 return stream_class->packet_context_field_type;
320 }
321
322 static inline
323 int bt_ctf_stream_class_common_set_packet_context_field_type(
324 struct bt_ctf_stream_class_common *stream_class,
325 struct bt_ctf_field_type_common *packet_context_type)
326 {
327 int ret = 0;
328
329 if (!stream_class) {
330 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
331 ret = -1;
332 goto end;
333 }
334
335 if (stream_class->frozen) {
336 BT_LOGW("Invalid parameter: stream class is frozen: "
337 "addr=%p, name=\"%s\", id=%" PRId64,
338 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
339 bt_ctf_stream_class_common_get_id(stream_class));
340 ret = -1;
341 goto end;
342 }
343
344 if (packet_context_type &&
345 bt_ctf_field_type_common_get_type_id(packet_context_type) !=
346 BT_CTF_FIELD_TYPE_ID_STRUCT) {
347 /* A packet context must be a structure. */
348 BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: "
349 "addr=%p, name=\"%s\", id=%" PRId64 ", "
350 "packet-context-ft-addr=%p, packet-context-ft-id=%s",
351 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
352 bt_ctf_stream_class_common_get_id(stream_class),
353 packet_context_type,
354 bt_ctf_field_type_id_string(
355 bt_ctf_field_type_common_get_type_id(packet_context_type)));
356 ret = -1;
357 goto end;
358 }
359
360 bt_ctf_object_put_ref(stream_class->packet_context_field_type);
361 stream_class->packet_context_field_type = packet_context_type;
362 bt_ctf_object_get_ref(stream_class->packet_context_field_type);
363 BT_LOGV("Set stream class's packet context field type: "
364 "addr=%p, name=\"%s\", id=%" PRId64 ", "
365 "packet-context-ft-addr=%p",
366 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
367 bt_ctf_stream_class_common_get_id(stream_class),
368 packet_context_type);
369
370 end:
371 return ret;
372 }
373
374 static inline
375 struct bt_ctf_field_type_common *
376 bt_ctf_stream_class_common_borrow_event_header_field_type(
377 struct bt_ctf_stream_class_common *stream_class)
378 {
379 struct bt_ctf_field_type_common *ret = NULL;
380
381 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
382
383 if (!stream_class->event_header_field_type) {
384 BT_LOGV("Stream class has no event header field type: "
385 "addr=%p, name=\"%s\", id=%" PRId64,
386 stream_class,
387 bt_ctf_stream_class_common_get_name(stream_class),
388 bt_ctf_stream_class_common_get_id(stream_class));
389 goto end;
390 }
391
392 ret = stream_class->event_header_field_type;
393
394 end:
395 return ret;
396 }
397
398 static inline
399 int bt_ctf_stream_class_common_set_event_header_field_type(
400 struct bt_ctf_stream_class_common *stream_class,
401 struct bt_ctf_field_type_common *event_header_type)
402 {
403 int ret = 0;
404
405 if (!stream_class) {
406 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
407 ret = -1;
408 goto end;
409 }
410
411 if (stream_class->frozen) {
412 BT_LOGW("Invalid parameter: stream class is frozen: "
413 "addr=%p, name=\"%s\", id=%" PRId64,
414 stream_class,
415 bt_ctf_stream_class_common_get_name(stream_class),
416 bt_ctf_stream_class_common_get_id(stream_class));
417 ret = -1;
418 goto end;
419 }
420
421 if (event_header_type &&
422 bt_ctf_field_type_common_get_type_id(event_header_type) !=
423 BT_CTF_FIELD_TYPE_ID_STRUCT) {
424 /* An event header must be a structure. */
425 BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: "
426 "addr=%p, name=\"%s\", id=%" PRId64 ", "
427 "event-header-ft-addr=%p, event-header-ft-id=%s",
428 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
429 bt_ctf_stream_class_common_get_id(stream_class),
430 event_header_type,
431 bt_ctf_field_type_id_string(
432 bt_ctf_field_type_common_get_type_id(event_header_type)));
433 ret = -1;
434 goto end;
435 }
436
437 bt_ctf_object_put_ref(stream_class->event_header_field_type);
438 stream_class->event_header_field_type = event_header_type;
439 bt_ctf_object_get_ref(stream_class->event_header_field_type);
440 BT_LOGV("Set stream class's event header field type: "
441 "addr=%p, name=\"%s\", id=%" PRId64 ", "
442 "event-header-ft-addr=%p",
443 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
444 bt_ctf_stream_class_common_get_id(stream_class),
445 event_header_type);
446 end:
447 return ret;
448 }
449
450 static inline
451 struct bt_ctf_field_type_common *
452 bt_ctf_stream_class_common_borrow_event_context_field_type(
453 struct bt_ctf_stream_class_common *stream_class)
454 {
455 struct bt_ctf_field_type_common *ret = NULL;
456
457 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
458
459 if (!stream_class->event_context_field_type) {
460 goto end;
461 }
462
463 ret = stream_class->event_context_field_type;
464
465 end:
466 return ret;
467 }
468
469 static inline
470 int bt_ctf_stream_class_common_set_event_context_field_type(
471 struct bt_ctf_stream_class_common *stream_class,
472 struct bt_ctf_field_type_common *event_context_type)
473 {
474 int ret = 0;
475
476 if (!stream_class) {
477 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
478 ret = -1;
479 goto end;
480 }
481
482 if (stream_class->frozen) {
483 BT_LOGW("Invalid parameter: stream class is frozen: "
484 "addr=%p, name=\"%s\", id=%" PRId64,
485 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
486 bt_ctf_stream_class_common_get_id(stream_class));
487 ret = -1;
488 goto end;
489 }
490
491 if (event_context_type &&
492 bt_ctf_field_type_common_get_type_id(event_context_type) !=
493 BT_CTF_FIELD_TYPE_ID_STRUCT) {
494 /* A packet context must be a structure. */
495 BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: "
496 "addr=%p, name=\"%s\", id=%" PRId64 ", "
497 "event-context-ft-addr=%p, event-context-ft-id=%s",
498 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
499 bt_ctf_stream_class_common_get_id(stream_class),
500 event_context_type,
501 bt_ctf_field_type_id_string(
502 bt_ctf_field_type_common_get_type_id(event_context_type)));
503 ret = -1;
504 goto end;
505 }
506
507 bt_ctf_object_put_ref(stream_class->event_context_field_type);
508 stream_class->event_context_field_type = event_context_type;
509 bt_ctf_object_get_ref(stream_class->event_context_field_type);
510 BT_LOGV("Set stream class's event context field type: "
511 "addr=%p, name=\"%s\", id=%" PRId64 ", "
512 "event-context-ft-addr=%p",
513 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
514 bt_ctf_stream_class_common_get_id(stream_class),
515 event_context_type);
516 end:
517 return ret;
518 }
519
520 struct bt_ctf_stream_class {
521 struct bt_ctf_stream_class_common common;
522 struct bt_ctf_clock *clock;
523 int64_t next_stream_id;
524 };
525
526 struct metadata_context;
527
528 BT_HIDDEN
529 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
530 struct metadata_context *context);
531
532 BT_HIDDEN
533 int bt_ctf_stream_class_map_clock_class(
534 struct bt_ctf_stream_class *stream_class,
535 struct bt_ctf_field_type *packet_context_type,
536 struct bt_ctf_field_type *event_header_type);
537
538 #endif /* BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H */
This page took 0.0401550000000001 seconds and 3 git commands to generate.