2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Trace IR Reference Count test
11 #include <babeltrace2/babeltrace.h>
12 #include "lib/object.h"
13 #include "compat/stdlib.h"
14 #include "common/assert.h"
15 #include <babeltrace2-ctf-writer/writer.h>
16 #include <babeltrace2-ctf-writer/clock.h>
17 #include <babeltrace2-ctf-writer/clock-class.h>
18 #include <babeltrace2-ctf-writer/stream.h>
19 #include <babeltrace2-ctf-writer/event.h>
20 #include <babeltrace2-ctf-writer/event-types.h>
21 #include <babeltrace2-ctf-writer/event-fields.h>
22 #include <babeltrace2-ctf-writer/stream-class.h>
23 #include <babeltrace2-ctf-writer/trace.h>
37 struct bt_ctf_writer
*writer
;
38 struct bt_ctf_trace
*tc
;
39 struct bt_ctf_stream_class
*sc
;
40 struct bt_ctf_event_class
*ec
;
41 struct bt_ctf_stream
*stream
;
42 struct bt_ctf_event
*event
;
45 const char *writer_user_names
[] = {
54 static const size_t WRITER_USER_NR_ELEMENTS
=
55 sizeof(struct writer_user
) / sizeof(void *);
58 * Returns a structure containing the following fields:
59 * - uint8_t payload_8;
60 * - uint16_t payload_16;
61 * - uint32_t payload_32;
63 static bt_field_class
*create_integer_struct(bt_trace_class
*trace_class
)
66 bt_field_class
*structure
= NULL
;
67 bt_field_class
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
69 structure
= bt_field_class_structure_create(trace_class
);
71 ui8
= bt_field_class_integer_unsigned_create(trace_class
);
73 bt_field_class_integer_set_field_value_range(ui8
, 8);
74 ret
= bt_field_class_structure_append_member(structure
,
77 ui16
= bt_field_class_integer_unsigned_create(trace_class
);
79 bt_field_class_integer_set_field_value_range(ui16
, 16);
80 ret
= bt_field_class_structure_append_member(structure
,
83 ui32
= bt_field_class_integer_unsigned_create(trace_class
);
85 bt_field_class_integer_set_field_value_range(ui32
, 32);
86 ret
= bt_field_class_structure_append_member(structure
,
89 BT_FIELD_CLASS_PUT_REF_AND_RESET(ui8
);
90 BT_FIELD_CLASS_PUT_REF_AND_RESET(ui16
);
91 BT_FIELD_CLASS_PUT_REF_AND_RESET(ui32
);
95 static struct bt_ctf_field_type
*create_writer_integer_struct(void)
98 struct bt_ctf_field_type
*structure
= NULL
;
99 struct bt_ctf_field_type
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
101 structure
= bt_ctf_field_type_structure_create();
102 BT_ASSERT(structure
);
103 ui8
= bt_ctf_field_type_integer_create(8);
105 ret
= bt_ctf_field_type_structure_add_field(structure
, ui8
,
108 ui16
= bt_ctf_field_type_integer_create(16);
110 ret
= bt_ctf_field_type_structure_add_field(structure
, ui16
,
113 ui32
= bt_ctf_field_type_integer_create(32);
115 ret
= bt_ctf_field_type_structure_add_field(structure
, ui32
,
118 BT_OBJECT_PUT_REF_AND_RESET(ui8
);
119 BT_OBJECT_PUT_REF_AND_RESET(ui16
);
120 BT_OBJECT_PUT_REF_AND_RESET(ui32
);
125 * A simple event has the following payload:
126 * - uint8_t payload_8;
127 * - uint16_t payload_16;
128 * - uint32_t payload_32;
130 static bt_event_class
*create_simple_event(
131 bt_stream_class
*sc
, const char *name
)
134 bt_event_class
*event
= NULL
;
135 bt_field_class
*payload
= NULL
;
138 event
= bt_event_class_create(sc
);
140 ret
= bt_event_class_set_name(event
, name
);
142 payload
= create_integer_struct(bt_stream_class_borrow_trace_class(sc
));
144 ret
= bt_event_class_set_payload_field_class(event
, payload
);
146 BT_FIELD_CLASS_PUT_REF_AND_RESET(payload
);
151 * A complex event has the following payload:
152 * - uint8_t payload_8;
153 * - uint16_t payload_16;
154 * - uint32_t payload_32;
155 * - struct payload_struct:
156 * - uint8_t payload_8;
157 * - uint16_t payload_16;
158 * - uint32_t payload_32;
160 static bt_event_class
*create_complex_event(bt_stream_class
*sc
,
164 bt_event_class
*event
= NULL
;
165 bt_field_class
*inner
= NULL
, *outer
= NULL
;
166 bt_trace_class
*trace_class
= bt_stream_class_borrow_trace_class(sc
);
169 event
= bt_event_class_create(sc
);
171 ret
= bt_event_class_set_name(event
, name
);
173 outer
= create_integer_struct(trace_class
);
175 inner
= create_integer_struct(trace_class
);
177 ret
= bt_field_class_structure_append_member(outer
,
178 "payload_struct", inner
);
180 ret
= bt_event_class_set_payload_field_class(event
, outer
);
182 BT_FIELD_CLASS_PUT_REF_AND_RESET(inner
);
183 BT_FIELD_CLASS_PUT_REF_AND_RESET(outer
);
187 static void create_sc1(bt_trace_class
*trace_class
)
190 bt_event_class
*ec1
= NULL
, *ec2
= NULL
;
191 bt_stream_class
*sc1
= NULL
, *ret_stream
= NULL
;
193 sc1
= bt_stream_class_create(trace_class
);
195 ret
= bt_stream_class_set_name(sc1
, "sc1");
197 ec1
= create_complex_event(sc1
, "ec1");
199 ec2
= create_simple_event(sc1
, "ec2");
201 ret_stream
= bt_event_class_borrow_stream_class(ec1
);
202 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC1");
203 ret_stream
= bt_event_class_borrow_stream_class(ec2
);
204 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC2");
205 BT_EVENT_CLASS_PUT_REF_AND_RESET(ec1
);
206 BT_EVENT_CLASS_PUT_REF_AND_RESET(ec2
);
207 BT_STREAM_CLASS_PUT_REF_AND_RESET(sc1
);
210 static void create_sc2(bt_trace_class
*trace_class
)
213 bt_event_class
*ec3
= NULL
;
214 bt_stream_class
*sc2
= NULL
, *ret_stream
= NULL
;
216 sc2
= bt_stream_class_create(trace_class
);
218 ret
= bt_stream_class_set_name(sc2
, "sc2");
220 ec3
= create_simple_event(sc2
, "ec3");
221 ret_stream
= bt_event_class_borrow_stream_class(ec3
);
222 ok(ret_stream
== sc2
, "Borrow parent stream SC2 from EC3");
223 BT_EVENT_CLASS_PUT_REF_AND_RESET(ec3
);
224 BT_STREAM_CLASS_PUT_REF_AND_RESET(sc2
);
227 static bt_trace_class
*create_tc1(bt_self_component_source
*self_comp
)
229 bt_trace_class
*tc1
= NULL
;
231 tc1
= bt_trace_class_create(
232 bt_self_component_source_as_self_component(self_comp
));
239 static void init_weak_refs(bt_trace_class
*tc
,
240 bt_trace_class
**tc1
,
241 bt_stream_class
**sc1
,
242 bt_stream_class
**sc2
,
243 bt_event_class
**ec1
,
244 bt_event_class
**ec2
,
245 bt_event_class
**ec3
)
248 *sc1
= bt_trace_class_borrow_stream_class_by_index(tc
, 0);
249 *sc2
= bt_trace_class_borrow_stream_class_by_index(tc
, 1);
250 *ec1
= bt_stream_class_borrow_event_class_by_index(*sc1
, 0);
251 *ec2
= bt_stream_class_borrow_event_class_by_index(*sc1
, 1);
252 *ec3
= bt_stream_class_borrow_event_class_by_index(*sc2
, 0);
255 static void test_example_scenario(bt_self_component_source
*self_comp
)
258 * Weak pointers to trace IR objects are to be used very
259 * carefully. This is NOT a good practice and is strongly
260 * discouraged; this is only done to facilitate the validation
261 * of expected reference counts without affecting them by taking
262 * "real" references to the objects.
264 bt_trace_class
*tc1
= NULL
, *weak_tc1
= NULL
;
265 bt_stream_class
*weak_sc1
= NULL
, *weak_sc2
= NULL
;
266 bt_event_class
*weak_ec1
= NULL
, *weak_ec2
= NULL
,
268 struct bt_user user_a
= { 0 }, user_b
= { 0 }, user_c
= { 0 };
270 /* The only reference which exists at this point is on TC1. */
271 tc1
= create_tc1(self_comp
);
272 ok(tc1
, "Initialize trace");
274 init_weak_refs(tc1
, &weak_tc1
, &weak_sc1
, &weak_sc2
, &weak_ec1
,
275 &weak_ec2
, &weak_ec3
);
276 ok(bt_object_get_ref_count((void *) weak_sc1
) == 0,
277 "Initial SC1 reference count is 0");
278 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
279 "Initial SC2 reference count is 0");
280 ok(bt_object_get_ref_count((void *) weak_ec1
) == 0,
281 "Initial EC1 reference count is 0");
282 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
283 "Initial EC2 reference count is 0");
284 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
285 "Initial EC3 reference count is 0");
287 /* User A has ownership of the trace. */
288 BT_OBJECT_MOVE_REF(user_a
.tc
, tc1
);
289 ok(bt_object_get_ref_count((void *) user_a
.tc
) == 1,
290 "TC1 reference count is 1");
292 /* User A acquires a reference to SC2 from TC1. */
293 user_a
.sc
= bt_trace_class_borrow_stream_class_by_index(
295 bt_stream_class_get_ref(user_a
.sc
);
296 ok(user_a
.sc
, "User A acquires SC2 from TC1");
297 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
298 "TC1 reference count is 2");
299 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
300 "SC2 reference count is 1");
302 /* User A acquires a reference to EC3 from SC2. */
303 user_a
.ec
= bt_stream_class_borrow_event_class_by_index(
305 bt_event_class_get_ref(user_a
.ec
);
306 ok(user_a
.ec
, "User A acquires EC3 from SC2");
307 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
308 "TC1 reference count is 2");
309 ok(bt_object_get_ref_count((void *) weak_sc2
) == 2,
310 "SC2 reference count is 2");
311 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
312 "EC3 reference count is 1");
314 /* User A releases its reference to SC2. */
315 diag("User A releases SC2");
316 BT_STREAM_CLASS_PUT_REF_AND_RESET(user_a
.sc
);
318 * We keep the pointer to SC2 around to validate its reference
321 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
322 "TC1 reference count is 2");
323 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
324 "SC2 reference count is 1");
325 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
326 "EC3 reference count is 1");
328 /* User A releases its reference to TC1. */
329 diag("User A releases TC1");
330 BT_TRACE_CLASS_PUT_REF_AND_RESET(user_a
.tc
);
332 * We keep the pointer to TC1 around to validate its reference
335 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
336 "TC1 reference count is 1");
337 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
338 "SC2 reference count is 1");
339 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
340 "EC3 reference count is 1");
342 /* User B acquires a reference to SC1. */
343 diag("User B acquires a reference to SC1");
344 user_b
.sc
= weak_sc1
;
345 bt_stream_class_get_ref(user_b
.sc
);
346 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
347 "TC1 reference count is 2");
348 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
349 "SC1 reference count is 1");
351 /* User C acquires a reference to EC1. */
352 diag("User C acquires a reference to EC1");
353 user_c
.ec
= bt_stream_class_borrow_event_class_by_index(
355 bt_event_class_get_ref(user_c
.ec
);
356 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
357 "EC1 reference count is 1");
358 ok(bt_object_get_ref_count((void *) weak_sc1
) == 2,
359 "SC1 reference count is 2");
361 /* User A releases its reference on EC3. */
362 diag("User A releases its reference on EC3");
363 BT_EVENT_CLASS_PUT_REF_AND_RESET(user_a
.ec
);
364 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
365 "EC3 reference count is 1");
366 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
367 "SC2 reference count is 0");
368 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
369 "TC1 reference count is 1");
371 /* User B releases its reference on SC1. */
372 diag("User B releases its reference on SC1");
373 BT_STREAM_CLASS_PUT_REF_AND_RESET(user_b
.sc
);
374 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
375 "SC1 reference count is 1");
378 * User C is the sole owner of an object and is keeping the whole
379 * trace hierarchy "alive" by holding a reference to EC1.
381 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
382 "TC1 reference count is 1");
383 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
384 "SC1 reference count is 1");
385 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
386 "SC2 reference count is 0");
387 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
388 "EC1 reference count is 1");
389 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
390 "EC2 reference count is 0");
391 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
392 "EC3 reference count is 0");
394 /* Reclaim last reference held by User C. */
395 BT_EVENT_CLASS_PUT_REF_AND_RESET(user_c
.ec
);
399 bt_component_class_initialize_method_status
src_init(
400 bt_self_component_source
*self_comp
,
401 bt_self_component_source_configuration
*config
__attribute__((unused
)),
402 const bt_value
*params
__attribute__((unused
)),
403 void *init_method_data
__attribute__((unused
)))
405 test_example_scenario(self_comp
);
406 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
410 bt_message_iterator_class_next_method_status
src_iter_next(
411 bt_self_message_iterator
*self_iterator
__attribute__((unused
)),
412 bt_message_array_const msgs
__attribute__((unused
)),
413 uint64_t capacity
__attribute__((unused
)),
414 uint64_t *count
__attribute__((unused
)))
416 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
419 static void test_example_scenario_in_graph(void)
421 bt_message_iterator_class
*msg_iter_cls
;
422 bt_component_class_source
*comp_cls
;
426 msg_iter_cls
= bt_message_iterator_class_create(src_iter_next
);
427 BT_ASSERT(msg_iter_cls
);
429 comp_cls
= bt_component_class_source_create("src", msg_iter_cls
);
431 ret
= bt_component_class_source_set_initialize_method(comp_cls
, src_init
);
433 graph
= bt_graph_create(0);
434 ret
= bt_graph_add_source_component(graph
, comp_cls
, "src-comp",
435 NULL
, BT_LOGGING_LEVEL_NONE
, NULL
);
437 bt_graph_put_ref(graph
);
438 bt_component_class_source_put_ref(comp_cls
);
439 bt_message_iterator_class_put_ref(msg_iter_cls
);
442 static void create_writer_user_full(struct writer_user
*user
)
445 struct bt_ctf_field_type
*ft
;
446 struct bt_ctf_field
*field
;
447 struct bt_ctf_clock
*clock
;
450 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
451 if (!bt_mkdtemp(trace_path
)) {
455 user
->writer
= bt_ctf_writer_create(trace_path
);
456 BT_ASSERT(user
->writer
);
457 ret
= bt_ctf_writer_set_byte_order(user
->writer
,
458 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
460 user
->tc
= bt_ctf_writer_get_trace(user
->writer
);
462 user
->sc
= bt_ctf_stream_class_create("sc");
464 clock
= bt_ctf_clock_create("the_clock");
466 ret
= bt_ctf_writer_add_clock(user
->writer
, clock
);
468 ret
= bt_ctf_stream_class_set_clock(user
->sc
, clock
);
470 BT_OBJECT_PUT_REF_AND_RESET(clock
);
471 user
->stream
= bt_ctf_writer_create_stream(user
->writer
, user
->sc
);
472 BT_ASSERT(user
->stream
);
473 user
->ec
= bt_ctf_event_class_create("ec");
475 ft
= create_writer_integer_struct();
477 ret
= bt_ctf_event_class_set_payload_field_type(user
->ec
, ft
);
478 BT_OBJECT_PUT_REF_AND_RESET(ft
);
480 ret
= bt_ctf_stream_class_add_event_class(user
->sc
, user
->ec
);
482 user
->event
= bt_ctf_event_create(user
->ec
);
483 BT_ASSERT(user
->event
);
484 field
= bt_ctf_event_get_payload(user
->event
, "payload_8");
486 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 10);
488 BT_OBJECT_PUT_REF_AND_RESET(field
);
489 field
= bt_ctf_event_get_payload(user
->event
, "payload_16");
491 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 20);
493 BT_OBJECT_PUT_REF_AND_RESET(field
);
494 field
= bt_ctf_event_get_payload(user
->event
, "payload_32");
496 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 30);
498 BT_OBJECT_PUT_REF_AND_RESET(field
);
499 ret
= bt_ctf_stream_append_event(user
->stream
, user
->event
);
501 recursive_rmdir(trace_path
);
505 static void test_put_order_swap(size_t *array
, size_t a
, size_t b
)
507 size_t temp
= array
[a
];
513 static void test_put_order_put_objects(size_t *array
, size_t size
)
516 struct writer_user user
= { 0 };
517 void **objects
= (void *) &user
;
519 create_writer_user_full(&user
);
522 for (i
= 0; i
< size
; ++i
) {
523 void *obj
= objects
[array
[i
]];
525 printf("%s", writer_user_names
[array
[i
]]);
526 BT_OBJECT_PUT_REF_AND_RESET(obj
);
536 static void test_put_order_permute(size_t *array
, int k
, size_t size
)
539 test_put_order_put_objects(array
, size
);
543 for (i
= k
- 1; i
>= 0; i
--) {
544 size_t next_k
= k
- 1;
546 test_put_order_swap(array
, i
, next_k
);
547 test_put_order_permute(array
, next_k
, size
);
548 test_put_order_swap(array
, i
, next_k
);
553 static void test_put_order(void)
556 size_t array
[WRITER_USER_NR_ELEMENTS
];
558 /* Initialize array of indexes */
559 for (i
= 0; i
< WRITER_USER_NR_ELEMENTS
; ++i
) {
563 test_put_order_permute(array
, WRITER_USER_NR_ELEMENTS
,
564 WRITER_USER_NR_ELEMENTS
);
568 * The objective of this test is to implement and expand upon the scenario
569 * described in the reference counting documentation and ensure that any node of
570 * the Trace, Stream Class, Event Class, Stream and Event hierarchy keeps all
571 * other "alive" and reachable.
573 * External tools (e.g. valgrind) should be used to confirm that this
574 * known-good test does not leak memory.
578 /* Initialize tap harness before any tests */
579 plan_tests(NR_TESTS
);
581 test_example_scenario_in_graph();
584 return exit_status();