4 * Trace IR Reference Count test
6 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <babeltrace2/babeltrace.h>
25 #include "lib/object.h"
26 #include "compat/stdlib.h"
27 #include "common/assert.h"
28 #include <babeltrace2-ctf-writer/writer.h>
29 #include <babeltrace2-ctf-writer/clock.h>
30 #include <babeltrace2-ctf-writer/clock-class.h>
31 #include <babeltrace2-ctf-writer/stream.h>
32 #include <babeltrace2-ctf-writer/event.h>
33 #include <babeltrace2-ctf-writer/event-types.h>
34 #include <babeltrace2-ctf-writer/event-fields.h>
35 #include <babeltrace2-ctf-writer/stream-class.h>
36 #include <babeltrace2-ctf-writer/trace.h>
50 struct bt_ctf_writer
*writer
;
51 struct bt_ctf_trace
*tc
;
52 struct bt_ctf_stream_class
*sc
;
53 struct bt_ctf_event_class
*ec
;
54 struct bt_ctf_stream
*stream
;
55 struct bt_ctf_event
*event
;
58 const char *writer_user_names
[] = {
67 static const size_t WRITER_USER_NR_ELEMENTS
=
68 sizeof(struct writer_user
) / sizeof(void *);
71 * Returns a structure containing the following fields:
72 * - uint8_t payload_8;
73 * - uint16_t payload_16;
74 * - uint32_t payload_32;
76 static bt_field_class
*create_integer_struct(bt_trace_class
*trace_class
)
79 bt_field_class
*structure
= NULL
;
80 bt_field_class
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
82 structure
= bt_field_class_structure_create(trace_class
);
84 ui8
= bt_field_class_integer_unsigned_create(trace_class
);
86 bt_field_class_integer_set_field_value_range(ui8
, 8);
87 ret
= bt_field_class_structure_append_member(structure
,
90 ui16
= bt_field_class_integer_unsigned_create(trace_class
);
92 bt_field_class_integer_set_field_value_range(ui16
, 16);
93 ret
= bt_field_class_structure_append_member(structure
,
96 ui32
= bt_field_class_integer_unsigned_create(trace_class
);
98 bt_field_class_integer_set_field_value_range(ui32
, 32);
99 ret
= bt_field_class_structure_append_member(structure
,
102 BT_FIELD_CLASS_PUT_REF_AND_RESET(ui8
);
103 BT_FIELD_CLASS_PUT_REF_AND_RESET(ui16
);
104 BT_FIELD_CLASS_PUT_REF_AND_RESET(ui32
);
108 static struct bt_ctf_field_type
*create_writer_integer_struct(void)
111 struct bt_ctf_field_type
*structure
= NULL
;
112 struct bt_ctf_field_type
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
114 structure
= bt_ctf_field_type_structure_create();
115 BT_ASSERT(structure
);
116 ui8
= bt_ctf_field_type_integer_create(8);
118 ret
= bt_ctf_field_type_structure_add_field(structure
, ui8
,
121 ui16
= bt_ctf_field_type_integer_create(16);
123 ret
= bt_ctf_field_type_structure_add_field(structure
, ui16
,
126 ui32
= bt_ctf_field_type_integer_create(32);
128 ret
= bt_ctf_field_type_structure_add_field(structure
, ui32
,
131 BT_OBJECT_PUT_REF_AND_RESET(ui8
);
132 BT_OBJECT_PUT_REF_AND_RESET(ui16
);
133 BT_OBJECT_PUT_REF_AND_RESET(ui32
);
138 * A simple event has the following payload:
139 * - uint8_t payload_8;
140 * - uint16_t payload_16;
141 * - uint32_t payload_32;
143 static bt_event_class
*create_simple_event(
144 bt_stream_class
*sc
, const char *name
)
147 bt_event_class
*event
= NULL
;
148 bt_field_class
*payload
= NULL
;
151 event
= bt_event_class_create(sc
);
153 ret
= bt_event_class_set_name(event
, name
);
155 payload
= create_integer_struct(bt_stream_class_borrow_trace_class(sc
));
157 ret
= bt_event_class_set_payload_field_class(event
, payload
);
159 BT_FIELD_CLASS_PUT_REF_AND_RESET(payload
);
164 * A complex event has the following payload:
165 * - uint8_t payload_8;
166 * - uint16_t payload_16;
167 * - uint32_t payload_32;
168 * - struct payload_struct:
169 * - uint8_t payload_8;
170 * - uint16_t payload_16;
171 * - uint32_t payload_32;
173 static bt_event_class
*create_complex_event(bt_stream_class
*sc
,
177 bt_event_class
*event
= NULL
;
178 bt_field_class
*inner
= NULL
, *outer
= NULL
;
179 bt_trace_class
*trace_class
= bt_stream_class_borrow_trace_class(sc
);
182 event
= bt_event_class_create(sc
);
184 ret
= bt_event_class_set_name(event
, name
);
186 outer
= create_integer_struct(trace_class
);
188 inner
= create_integer_struct(trace_class
);
190 ret
= bt_field_class_structure_append_member(outer
,
191 "payload_struct", inner
);
193 ret
= bt_event_class_set_payload_field_class(event
, outer
);
195 BT_FIELD_CLASS_PUT_REF_AND_RESET(inner
);
196 BT_FIELD_CLASS_PUT_REF_AND_RESET(outer
);
200 static void create_sc1(bt_trace_class
*trace_class
)
203 bt_event_class
*ec1
= NULL
, *ec2
= NULL
;
204 bt_stream_class
*sc1
= NULL
, *ret_stream
= NULL
;
206 sc1
= bt_stream_class_create(trace_class
);
208 ret
= bt_stream_class_set_name(sc1
, "sc1");
210 ec1
= create_complex_event(sc1
, "ec1");
212 ec2
= create_simple_event(sc1
, "ec2");
214 ret_stream
= bt_event_class_borrow_stream_class(ec1
);
215 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC1");
216 ret_stream
= bt_event_class_borrow_stream_class(ec2
);
217 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC2");
218 BT_EVENT_CLASS_PUT_REF_AND_RESET(ec1
);
219 BT_EVENT_CLASS_PUT_REF_AND_RESET(ec2
);
220 BT_STREAM_CLASS_PUT_REF_AND_RESET(sc1
);
223 static void create_sc2(bt_trace_class
*trace_class
)
226 bt_event_class
*ec3
= NULL
;
227 bt_stream_class
*sc2
= NULL
, *ret_stream
= NULL
;
229 sc2
= bt_stream_class_create(trace_class
);
231 ret
= bt_stream_class_set_name(sc2
, "sc2");
233 ec3
= create_simple_event(sc2
, "ec3");
234 ret_stream
= bt_event_class_borrow_stream_class(ec3
);
235 ok(ret_stream
== sc2
, "Borrow parent stream SC2 from EC3");
236 BT_EVENT_CLASS_PUT_REF_AND_RESET(ec3
);
237 BT_STREAM_CLASS_PUT_REF_AND_RESET(sc2
);
240 static bt_trace_class
*create_tc1(bt_self_component_source
*self_comp
)
242 bt_trace_class
*tc1
= NULL
;
244 tc1
= bt_trace_class_create(
245 bt_self_component_source_as_self_component(self_comp
));
252 static void init_weak_refs(bt_trace_class
*tc
,
253 bt_trace_class
**tc1
,
254 bt_stream_class
**sc1
,
255 bt_stream_class
**sc2
,
256 bt_event_class
**ec1
,
257 bt_event_class
**ec2
,
258 bt_event_class
**ec3
)
261 *sc1
= bt_trace_class_borrow_stream_class_by_index(tc
, 0);
262 *sc2
= bt_trace_class_borrow_stream_class_by_index(tc
, 1);
263 *ec1
= bt_stream_class_borrow_event_class_by_index(*sc1
, 0);
264 *ec2
= bt_stream_class_borrow_event_class_by_index(*sc1
, 1);
265 *ec3
= bt_stream_class_borrow_event_class_by_index(*sc2
, 0);
268 static void test_example_scenario(bt_self_component_source
*self_comp
)
271 * Weak pointers to trace IR objects are to be used very
272 * carefully. This is NOT a good practice and is strongly
273 * discouraged; this is only done to facilitate the validation
274 * of expected reference counts without affecting them by taking
275 * "real" references to the objects.
277 bt_trace_class
*tc1
= NULL
, *weak_tc1
= NULL
;
278 bt_stream_class
*weak_sc1
= NULL
, *weak_sc2
= NULL
;
279 bt_event_class
*weak_ec1
= NULL
, *weak_ec2
= NULL
,
281 struct user user_a
= { 0 }, user_b
= { 0 }, user_c
= { 0 };
283 /* The only reference which exists at this point is on TC1. */
284 tc1
= create_tc1(self_comp
);
285 ok(tc1
, "Initialize trace");
287 init_weak_refs(tc1
, &weak_tc1
, &weak_sc1
, &weak_sc2
, &weak_ec1
,
288 &weak_ec2
, &weak_ec3
);
289 ok(bt_object_get_ref_count((void *) weak_sc1
) == 0,
290 "Initial SC1 reference count is 0");
291 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
292 "Initial SC2 reference count is 0");
293 ok(bt_object_get_ref_count((void *) weak_ec1
) == 0,
294 "Initial EC1 reference count is 0");
295 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
296 "Initial EC2 reference count is 0");
297 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
298 "Initial EC3 reference count is 0");
300 /* User A has ownership of the trace. */
301 BT_OBJECT_MOVE_REF(user_a
.tc
, tc1
);
302 ok(bt_object_get_ref_count((void *) user_a
.tc
) == 1,
303 "TC1 reference count is 1");
305 /* User A acquires a reference to SC2 from TC1. */
306 user_a
.sc
= bt_trace_class_borrow_stream_class_by_index(
308 bt_stream_class_get_ref(user_a
.sc
);
309 ok(user_a
.sc
, "User A acquires SC2 from TC1");
310 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
311 "TC1 reference count is 2");
312 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
313 "SC2 reference count is 1");
315 /* User A acquires a reference to EC3 from SC2. */
316 user_a
.ec
= bt_stream_class_borrow_event_class_by_index(
318 bt_event_class_get_ref(user_a
.ec
);
319 ok(user_a
.ec
, "User A acquires EC3 from SC2");
320 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
321 "TC1 reference count is 2");
322 ok(bt_object_get_ref_count((void *) weak_sc2
) == 2,
323 "SC2 reference count is 2");
324 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
325 "EC3 reference count is 1");
327 /* User A releases its reference to SC2. */
328 diag("User A releases SC2");
329 BT_STREAM_CLASS_PUT_REF_AND_RESET(user_a
.sc
);
331 * We keep the pointer to SC2 around to validate its reference
334 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
335 "TC1 reference count is 2");
336 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
337 "SC2 reference count is 1");
338 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
339 "EC3 reference count is 1");
341 /* User A releases its reference to TC1. */
342 diag("User A releases TC1");
343 BT_TRACE_CLASS_PUT_REF_AND_RESET(user_a
.tc
);
345 * We keep the pointer to TC1 around to validate its reference
348 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
349 "TC1 reference count is 1");
350 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
351 "SC2 reference count is 1");
352 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
353 "EC3 reference count is 1");
355 /* User B acquires a reference to SC1. */
356 diag("User B acquires a reference to SC1");
357 user_b
.sc
= weak_sc1
;
358 bt_stream_class_get_ref(user_b
.sc
);
359 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
360 "TC1 reference count is 2");
361 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
362 "SC1 reference count is 1");
364 /* User C acquires a reference to EC1. */
365 diag("User C acquires a reference to EC1");
366 user_c
.ec
= bt_stream_class_borrow_event_class_by_index(
368 bt_event_class_get_ref(user_c
.ec
);
369 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
370 "EC1 reference count is 1");
371 ok(bt_object_get_ref_count((void *) weak_sc1
) == 2,
372 "SC1 reference count is 2");
374 /* User A releases its reference on EC3. */
375 diag("User A releases its reference on EC3");
376 BT_EVENT_CLASS_PUT_REF_AND_RESET(user_a
.ec
);
377 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
378 "EC3 reference count is 1");
379 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
380 "SC2 reference count is 0");
381 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
382 "TC1 reference count is 1");
384 /* User B releases its reference on SC1. */
385 diag("User B releases its reference on SC1");
386 BT_STREAM_CLASS_PUT_REF_AND_RESET(user_b
.sc
);
387 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
388 "SC1 reference count is 1");
391 * User C is the sole owner of an object and is keeping the whole
392 * trace hierarchy "alive" by holding a reference to EC1.
394 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
395 "TC1 reference count is 1");
396 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
397 "SC1 reference count is 1");
398 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
399 "SC2 reference count is 0");
400 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
401 "EC1 reference count is 1");
402 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
403 "EC2 reference count is 0");
404 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
405 "EC3 reference count is 0");
407 /* Reclaim last reference held by User C. */
408 BT_EVENT_CLASS_PUT_REF_AND_RESET(user_c
.ec
);
412 bt_component_class_initialize_method_status
src_init(
413 bt_self_component_source
*self_comp
,
414 bt_self_component_source_configuration
*config
,
415 const bt_value
*params
, void *init_method_data
)
417 test_example_scenario(self_comp
);
418 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
422 bt_message_iterator_class_next_method_status
src_iter_next(
423 bt_self_message_iterator
*self_iterator
,
424 bt_message_array_const msgs
, uint64_t capacity
,
427 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
430 static void test_example_scenario_in_graph(void)
432 bt_message_iterator_class
*msg_iter_cls
;
433 bt_component_class_source
*comp_cls
;
437 msg_iter_cls
= bt_message_iterator_class_create(src_iter_next
);
438 BT_ASSERT(msg_iter_cls
);
440 comp_cls
= bt_component_class_source_create("src", msg_iter_cls
);
442 ret
= bt_component_class_source_set_initialize_method(comp_cls
, src_init
);
444 graph
= bt_graph_create(0);
445 ret
= bt_graph_add_source_component(graph
, comp_cls
, "src-comp",
446 NULL
, BT_LOGGING_LEVEL_NONE
, NULL
);
448 bt_graph_put_ref(graph
);
449 bt_component_class_source_put_ref(comp_cls
);
450 bt_message_iterator_class_put_ref(msg_iter_cls
);
453 static void create_writer_user_full(struct writer_user
*user
)
456 struct bt_ctf_field_type
*ft
;
457 struct bt_ctf_field
*field
;
458 struct bt_ctf_clock
*clock
;
461 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
462 if (!bt_mkdtemp(trace_path
)) {
466 user
->writer
= bt_ctf_writer_create(trace_path
);
467 BT_ASSERT(user
->writer
);
468 ret
= bt_ctf_writer_set_byte_order(user
->writer
,
469 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
471 user
->tc
= bt_ctf_writer_get_trace(user
->writer
);
473 user
->sc
= bt_ctf_stream_class_create("sc");
475 clock
= bt_ctf_clock_create("the_clock");
477 ret
= bt_ctf_writer_add_clock(user
->writer
, clock
);
479 ret
= bt_ctf_stream_class_set_clock(user
->sc
, clock
);
481 BT_OBJECT_PUT_REF_AND_RESET(clock
);
482 user
->stream
= bt_ctf_writer_create_stream(user
->writer
, user
->sc
);
483 BT_ASSERT(user
->stream
);
484 user
->ec
= bt_ctf_event_class_create("ec");
486 ft
= create_writer_integer_struct();
488 ret
= bt_ctf_event_class_set_payload_field_type(user
->ec
, ft
);
489 BT_OBJECT_PUT_REF_AND_RESET(ft
);
491 ret
= bt_ctf_stream_class_add_event_class(user
->sc
, user
->ec
);
493 user
->event
= bt_ctf_event_create(user
->ec
);
494 BT_ASSERT(user
->event
);
495 field
= bt_ctf_event_get_payload(user
->event
, "payload_8");
497 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 10);
499 BT_OBJECT_PUT_REF_AND_RESET(field
);
500 field
= bt_ctf_event_get_payload(user
->event
, "payload_16");
502 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 20);
504 BT_OBJECT_PUT_REF_AND_RESET(field
);
505 field
= bt_ctf_event_get_payload(user
->event
, "payload_32");
507 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 30);
509 BT_OBJECT_PUT_REF_AND_RESET(field
);
510 ret
= bt_ctf_stream_append_event(user
->stream
, user
->event
);
512 recursive_rmdir(trace_path
);
516 static void test_put_order_swap(size_t *array
, size_t a
, size_t b
)
518 size_t temp
= array
[a
];
524 static void test_put_order_put_objects(size_t *array
, size_t size
)
527 struct writer_user user
= { 0 };
528 void **objects
= (void *) &user
;
530 create_writer_user_full(&user
);
533 for (i
= 0; i
< size
; ++i
) {
534 void *obj
= objects
[array
[i
]];
536 printf("%s", writer_user_names
[array
[i
]]);
537 BT_OBJECT_PUT_REF_AND_RESET(obj
);
547 static void test_put_order_permute(size_t *array
, int k
, size_t size
)
550 test_put_order_put_objects(array
, size
);
554 for (i
= k
- 1; i
>= 0; i
--) {
555 size_t next_k
= k
- 1;
557 test_put_order_swap(array
, i
, next_k
);
558 test_put_order_permute(array
, next_k
, size
);
559 test_put_order_swap(array
, i
, next_k
);
564 static void test_put_order(void)
567 size_t array
[WRITER_USER_NR_ELEMENTS
];
569 /* Initialize array of indexes */
570 for (i
= 0; i
< WRITER_USER_NR_ELEMENTS
; ++i
) {
574 test_put_order_permute(array
, WRITER_USER_NR_ELEMENTS
,
575 WRITER_USER_NR_ELEMENTS
);
579 * The objective of this test is to implement and expand upon the scenario
580 * described in the reference counting documentation and ensure that any node of
581 * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all
582 * other "alive" and reachable.
584 * External tools (e.g. valgrind) should be used to confirm that this
585 * known-good test does not leak memory.
587 int main(int argc
, char **argv
)
589 /* Initialize tap harness before any tests */
590 plan_tests(NR_TESTS
);
592 test_example_scenario_in_graph();
595 return exit_status();