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.
23 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/object-internal.h>
25 #include <babeltrace/compat/stdlib-internal.h>
26 #include <babeltrace/assert-internal.h>
32 struct bt_private_trace
*tc
;
33 struct bt_private_stream_class
*sc
;
34 struct bt_private_event_class
*ec
;
35 struct bt_private_stream
*stream
;
36 struct bt_private_event
*event
;
40 struct bt_ctf_writer
*writer
;
41 struct bt_ctf_trace
*tc
;
42 struct bt_ctf_stream_class
*sc
;
43 struct bt_ctf_event_class
*ec
;
44 struct bt_ctf_stream
*stream
;
45 struct bt_ctf_event
*event
;
48 const char *writer_user_names
[] = {
57 static const size_t WRITER_USER_NR_ELEMENTS
=
58 sizeof(struct writer_user
) / sizeof(void *);
61 * Returns a structure containing the following fields:
62 * - uint8_t payload_8;
63 * - uint16_t payload_16;
64 * - uint32_t payload_32;
66 static struct bt_private_field_class
*create_integer_struct(void)
69 struct bt_private_field_class
*structure
= NULL
;
70 struct bt_private_field_class
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
72 structure
= bt_private_field_class_structure_create();
74 ui8
= bt_private_field_class_unsigned_integer_create();
76 ret
= bt_private_field_class_integer_set_field_value_range(ui8
, 8);
78 ret
= bt_private_field_class_structure_append_member(structure
,
81 ui16
= bt_private_field_class_unsigned_integer_create();
83 ret
= bt_private_field_class_integer_set_field_value_range(ui16
, 16);
85 ret
= bt_private_field_class_structure_append_member(structure
,
88 ui32
= bt_private_field_class_unsigned_integer_create();
90 ret
= bt_private_field_class_integer_set_field_value_range(ui32
, 32);
92 ret
= bt_private_field_class_structure_append_member(structure
,
95 BT_OBJECT_PUT_REF_AND_RESET(ui8
);
96 BT_OBJECT_PUT_REF_AND_RESET(ui16
);
97 BT_OBJECT_PUT_REF_AND_RESET(ui32
);
101 static struct bt_ctf_field_type
*create_writer_integer_struct(void)
104 struct bt_ctf_field_type
*structure
= NULL
;
105 struct bt_ctf_field_type
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
107 structure
= bt_ctf_field_type_structure_create();
108 BT_ASSERT(structure
);
109 ui8
= bt_ctf_field_type_integer_create(8);
111 ret
= bt_ctf_field_type_structure_add_field(structure
, ui8
,
114 ui16
= bt_ctf_field_type_integer_create(16);
116 ret
= bt_ctf_field_type_structure_add_field(structure
, ui16
,
119 ui32
= bt_ctf_field_type_integer_create(32);
121 ret
= bt_ctf_field_type_structure_add_field(structure
, ui32
,
124 BT_OBJECT_PUT_REF_AND_RESET(ui8
);
125 BT_OBJECT_PUT_REF_AND_RESET(ui16
);
126 BT_OBJECT_PUT_REF_AND_RESET(ui32
);
131 * A simple event has the following payload:
132 * - uint8_t payload_8;
133 * - uint16_t payload_16;
134 * - uint32_t payload_32;
136 static struct bt_private_event_class
*create_simple_event(
137 struct bt_private_stream_class
*sc
, const char *name
)
140 struct bt_private_event_class
*event
= NULL
;
141 struct bt_private_field_class
*payload
= NULL
;
144 event
= bt_private_event_class_create(sc
);
146 ret
= bt_private_event_class_set_name(event
, name
);
148 payload
= create_integer_struct();
150 ret
= bt_private_event_class_set_payload_field_class(event
, payload
);
152 BT_OBJECT_PUT_REF_AND_RESET(payload
);
157 * A complex event has the following payload:
158 * - uint8_t payload_8;
159 * - uint16_t payload_16;
160 * - uint32_t payload_32;
161 * - struct payload_struct:
162 * - uint8_t payload_8;
163 * - uint16_t payload_16;
164 * - uint32_t payload_32;
166 static struct bt_private_event_class
*create_complex_event(
167 struct bt_private_stream_class
*sc
,
171 struct bt_private_event_class
*event
= NULL
;
172 struct bt_private_field_class
*inner
= NULL
, *outer
= NULL
;
175 event
= bt_private_event_class_create(sc
);
177 ret
= bt_private_event_class_set_name(event
, name
);
179 outer
= create_integer_struct();
181 inner
= create_integer_struct();
183 ret
= bt_private_field_class_structure_append_member(outer
,
184 "payload_struct", inner
);
186 ret
= bt_private_event_class_set_payload_field_class(event
, outer
);
188 BT_OBJECT_PUT_REF_AND_RESET(inner
);
189 BT_OBJECT_PUT_REF_AND_RESET(outer
);
193 static void set_stream_class_field_classes(
194 struct bt_private_stream_class
*stream_class
)
196 struct bt_private_field_class
*packet_context_type
;
197 struct bt_private_field_class
*event_header_type
;
198 struct bt_private_field_class
*fc
;
201 packet_context_type
= bt_private_field_class_structure_create();
202 BT_ASSERT(packet_context_type
);
203 fc
= bt_private_field_class_unsigned_integer_create();
205 ret
= bt_private_field_class_integer_set_field_value_range(fc
, 32);
207 ret
= bt_private_field_class_structure_append_member(packet_context_type
,
210 bt_object_put_ref(fc
);
211 fc
= bt_private_field_class_unsigned_integer_create();
213 ret
= bt_private_field_class_integer_set_field_value_range(fc
, 32);
215 ret
= bt_private_field_class_structure_append_member(packet_context_type
,
218 bt_object_put_ref(fc
);
219 event_header_type
= bt_private_field_class_structure_create();
220 BT_ASSERT(event_header_type
);
221 fc
= bt_private_field_class_unsigned_integer_create();
223 ret
= bt_private_field_class_integer_set_field_value_range(fc
, 32);
225 ret
= bt_private_field_class_structure_append_member(event_header_type
,
228 bt_object_put_ref(fc
);
229 ret
= bt_private_stream_class_set_packet_context_field_class(
230 stream_class
, packet_context_type
);
232 ret
= bt_private_stream_class_set_event_header_field_class(
233 stream_class
, event_header_type
);
235 bt_object_put_ref(packet_context_type
);
236 bt_object_put_ref(event_header_type
);
239 static void create_sc1(struct bt_private_trace
*trace
)
242 struct bt_private_event_class
*ec1
= NULL
, *ec2
= NULL
;
243 struct bt_private_stream_class
*sc1
= NULL
, *ret_stream
= NULL
;
245 sc1
= bt_private_stream_class_create(trace
);
247 ret
= bt_private_stream_class_set_name(sc1
, "sc1");
249 set_stream_class_field_classes(sc1
);
250 ec1
= create_complex_event(sc1
, "ec1");
252 ec2
= create_simple_event(sc1
, "ec2");
254 ret_stream
= bt_private_event_class_borrow_stream_class(ec1
);
255 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC1");
256 ret_stream
= bt_private_event_class_borrow_stream_class(ec2
);
257 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC2");
258 BT_OBJECT_PUT_REF_AND_RESET(ec1
);
259 BT_OBJECT_PUT_REF_AND_RESET(ec2
);
260 BT_OBJECT_PUT_REF_AND_RESET(sc1
);
263 static void create_sc2(struct bt_private_trace
*trace
)
266 struct bt_private_event_class
*ec3
= NULL
;
267 struct bt_private_stream_class
*sc2
= NULL
, *ret_stream
= NULL
;
269 sc2
= bt_private_stream_class_create(trace
);
271 ret
= bt_private_stream_class_set_name(sc2
, "sc2");
273 set_stream_class_field_classes(sc2
);
274 ec3
= create_simple_event(sc2
, "ec3");
275 ret_stream
= bt_private_event_class_borrow_stream_class(ec3
);
276 ok(ret_stream
== sc2
, "Borrow parent stream SC2 from EC3");
277 BT_OBJECT_PUT_REF_AND_RESET(ec3
);
278 BT_OBJECT_PUT_REF_AND_RESET(sc2
);
281 static void set_trace_packet_header(struct bt_private_trace
*trace
)
283 struct bt_private_field_class
*packet_header_type
;
284 struct bt_private_field_class
*fc
;
287 packet_header_type
= bt_private_field_class_structure_create();
288 BT_ASSERT(packet_header_type
);
289 fc
= bt_private_field_class_unsigned_integer_create();
291 ret
= bt_private_field_class_integer_set_field_value_range(fc
, 32);
293 ret
= bt_private_field_class_structure_append_member(packet_header_type
,
296 bt_object_put_ref(fc
);
297 ret
= bt_private_trace_set_packet_header_field_class(trace
,
301 bt_object_put_ref(packet_header_type
);
304 static struct bt_private_trace
*create_tc1(void)
306 struct bt_private_trace
*tc1
= NULL
;
308 tc1
= bt_private_trace_create();
310 set_trace_packet_header(tc1
);
316 static void init_weak_refs(struct bt_private_trace
*tc
,
317 struct bt_private_trace
**tc1
,
318 struct bt_private_stream_class
**sc1
,
319 struct bt_private_stream_class
**sc2
,
320 struct bt_private_event_class
**ec1
,
321 struct bt_private_event_class
**ec2
,
322 struct bt_private_event_class
**ec3
)
325 *sc1
= bt_private_trace_borrow_stream_class_by_index(tc
, 0);
326 *sc2
= bt_private_trace_borrow_stream_class_by_index(tc
, 1);
327 *ec1
= bt_private_stream_class_borrow_event_class_by_index(*sc1
, 0);
328 *ec2
= bt_private_stream_class_borrow_event_class_by_index(*sc1
, 1);
329 *ec3
= bt_private_stream_class_borrow_event_class_by_index(*sc2
, 0);
332 static void test_example_scenario(void)
335 * Weak pointers to trace IR objects are to be used very
336 * carefully. This is NOT a good practice and is strongly
337 * discouraged; this is only done to facilitate the validation
338 * of expected reference counts without affecting them by taking
339 * "real" references to the objects.
341 struct bt_private_trace
*tc1
= NULL
, *weak_tc1
= NULL
;
342 struct bt_private_stream_class
*weak_sc1
= NULL
, *weak_sc2
= NULL
;
343 struct bt_private_event_class
*weak_ec1
= NULL
, *weak_ec2
= NULL
,
345 struct user user_a
= { 0 }, user_b
= { 0 }, user_c
= { 0 };
347 /* The only reference which exists at this point is on TC1. */
349 ok(tc1
, "Initialize trace");
351 init_weak_refs(tc1
, &weak_tc1
, &weak_sc1
, &weak_sc2
, &weak_ec1
,
352 &weak_ec2
, &weak_ec3
);
353 ok(bt_object_get_ref_count((void *) weak_sc1
) == 0,
354 "Initial SC1 reference count is 0");
355 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
356 "Initial SC2 reference count is 0");
357 ok(bt_object_get_ref_count((void *) weak_ec1
) == 0,
358 "Initial EC1 reference count is 0");
359 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
360 "Initial EC2 reference count is 0");
361 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
362 "Initial EC3 reference count is 0");
364 /* User A has ownership of the trace. */
365 BT_OBJECT_MOVE_REF(user_a
.tc
, tc1
);
366 ok(bt_object_get_ref_count((void *) user_a
.tc
) == 1,
367 "TC1 reference count is 1");
369 /* User A acquires a reference to SC2 from TC1. */
370 user_a
.sc
= bt_object_get_ref(
371 bt_private_trace_borrow_stream_class_by_index(
373 ok(user_a
.sc
, "User A acquires SC2 from TC1");
374 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
375 "TC1 reference count is 2");
376 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
377 "SC2 reference count is 1");
379 /* User A acquires a reference to EC3 from SC2. */
380 user_a
.ec
= bt_object_get_ref(
381 bt_private_stream_class_borrow_event_class_by_index(
383 ok(user_a
.ec
, "User A acquires EC3 from SC2");
384 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
385 "TC1 reference count is 2");
386 ok(bt_object_get_ref_count((void *) weak_sc2
) == 2,
387 "SC2 reference count is 2");
388 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
389 "EC3 reference count is 1");
391 /* User A releases its reference to SC2. */
392 diag("User A releases SC2");
393 BT_OBJECT_PUT_REF_AND_RESET(user_a
.sc
);
395 * We keep the pointer to SC2 around to validate its reference
398 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
399 "TC1 reference count is 2");
400 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
401 "SC2 reference count is 1");
402 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
403 "EC3 reference count is 1");
405 /* User A releases its reference to TC1. */
406 diag("User A releases TC1");
407 BT_OBJECT_PUT_REF_AND_RESET(user_a
.tc
);
409 * We keep the pointer to TC1 around to validate its reference
412 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
413 "TC1 reference count is 1");
414 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
415 "SC2 reference count is 1");
416 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
417 "EC3 reference count is 1");
419 /* User B acquires a reference to SC1. */
420 diag("User B acquires a reference to SC1");
421 user_b
.sc
= bt_object_get_ref(weak_sc1
);
422 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
423 "TC1 reference count is 2");
424 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
425 "SC1 reference count is 1");
427 /* User C acquires a reference to EC1. */
428 diag("User C acquires a reference to EC1");
429 user_c
.ec
= bt_object_get_ref(
430 bt_private_stream_class_borrow_event_class_by_index(
432 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
433 "EC1 reference count is 1");
434 ok(bt_object_get_ref_count((void *) weak_sc1
) == 2,
435 "SC1 reference count is 2");
437 /* User A releases its reference on EC3. */
438 diag("User A releases its reference on EC3");
439 BT_OBJECT_PUT_REF_AND_RESET(user_a
.ec
);
440 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
441 "EC3 reference count is 1");
442 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
443 "SC2 reference count is 0");
444 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
445 "TC1 reference count is 1");
447 /* User B releases its reference on SC1. */
448 diag("User B releases its reference on SC1");
449 BT_OBJECT_PUT_REF_AND_RESET(user_b
.sc
);
450 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
451 "SC1 reference count is 1");
454 * User C is the sole owner of an object and is keeping the whole
455 * trace hierarchy "alive" by holding a reference to EC1.
457 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
458 "TC1 reference count is 1");
459 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
460 "SC1 reference count is 1");
461 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
462 "SC2 reference count is 0");
463 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
464 "EC1 reference count is 1");
465 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
466 "EC2 reference count is 0");
467 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
468 "EC3 reference count is 0");
470 /* Reclaim last reference held by User C. */
471 BT_OBJECT_PUT_REF_AND_RESET(user_c
.ec
);
474 static void create_writer_user_full(struct writer_user
*user
)
477 struct bt_ctf_field_type
*ft
;
478 struct bt_ctf_field
*field
;
479 struct bt_ctf_clock
*clock
;
482 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
483 if (!bt_mkdtemp(trace_path
)) {
487 user
->writer
= bt_ctf_writer_create(trace_path
);
488 BT_ASSERT(user
->writer
);
489 ret
= bt_ctf_writer_set_byte_order(user
->writer
,
490 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
492 user
->tc
= bt_ctf_writer_get_trace(user
->writer
);
494 user
->sc
= bt_ctf_stream_class_create("sc");
496 clock
= bt_ctf_clock_create("the_clock");
498 ret
= bt_ctf_writer_add_clock(user
->writer
, clock
);
500 ret
= bt_ctf_stream_class_set_clock(user
->sc
, clock
);
502 BT_OBJECT_PUT_REF_AND_RESET(clock
);
503 user
->stream
= bt_ctf_writer_create_stream(user
->writer
, user
->sc
);
504 BT_ASSERT(user
->stream
);
505 user
->ec
= bt_ctf_event_class_create("ec");
507 ft
= create_writer_integer_struct();
509 ret
= bt_ctf_event_class_set_payload_field_type(user
->ec
, ft
);
510 BT_OBJECT_PUT_REF_AND_RESET(ft
);
512 ret
= bt_ctf_stream_class_add_event_class(user
->sc
, user
->ec
);
514 user
->event
= bt_ctf_event_create(user
->ec
);
515 BT_ASSERT(user
->event
);
516 field
= bt_ctf_event_get_payload(user
->event
, "payload_8");
518 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 10);
520 BT_OBJECT_PUT_REF_AND_RESET(field
);
521 field
= bt_ctf_event_get_payload(user
->event
, "payload_16");
523 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 20);
525 BT_OBJECT_PUT_REF_AND_RESET(field
);
526 field
= bt_ctf_event_get_payload(user
->event
, "payload_32");
528 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 30);
530 BT_OBJECT_PUT_REF_AND_RESET(field
);
531 ret
= bt_ctf_stream_append_event(user
->stream
, user
->event
);
533 recursive_rmdir(trace_path
);
537 static void test_put_order_swap(size_t *array
, size_t a
, size_t b
)
539 size_t temp
= array
[a
];
545 static void test_put_order_put_objects(size_t *array
, size_t size
)
548 struct writer_user user
= { 0 };
549 void **objects
= (void *) &user
;
551 create_writer_user_full(&user
);
554 for (i
= 0; i
< size
; ++i
) {
555 void *obj
= objects
[array
[i
]];
557 printf("%s", writer_user_names
[array
[i
]]);
558 BT_OBJECT_PUT_REF_AND_RESET(obj
);
568 static void test_put_order_permute(size_t *array
, int k
, size_t size
)
571 test_put_order_put_objects(array
, size
);
575 for (i
= k
- 1; i
>= 0; i
--) {
576 size_t next_k
= k
- 1;
578 test_put_order_swap(array
, i
, next_k
);
579 test_put_order_permute(array
, next_k
, size
);
580 test_put_order_swap(array
, i
, next_k
);
585 static void test_put_order(void)
588 size_t array
[WRITER_USER_NR_ELEMENTS
];
590 /* Initialize array of indexes */
591 for (i
= 0; i
< WRITER_USER_NR_ELEMENTS
; ++i
) {
595 test_put_order_permute(array
, WRITER_USER_NR_ELEMENTS
,
596 WRITER_USER_NR_ELEMENTS
);
600 * The objective of this test is to implement and expand upon the scenario
601 * described in the reference counting documentation and ensure that any node of
602 * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all
603 * other "alive" and reachable.
605 * External tools (e.g. valgrind) should be used to confirm that this
606 * known-good test does not leak memory.
608 int main(int argc
, char **argv
)
610 /* Initialize tap harness before any tests */
611 plan_tests(NR_TESTS
);
613 test_example_scenario();
616 return exit_status();