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 bt_private_field_class_integer_set_field_value_range(ui8
, 8);
77 ret
= bt_private_field_class_structure_append_member(structure
,
80 ui16
= bt_private_field_class_unsigned_integer_create();
82 bt_private_field_class_integer_set_field_value_range(ui16
, 16);
83 ret
= bt_private_field_class_structure_append_member(structure
,
86 ui32
= bt_private_field_class_unsigned_integer_create();
88 bt_private_field_class_integer_set_field_value_range(ui32
, 32);
89 ret
= bt_private_field_class_structure_append_member(structure
,
92 BT_OBJECT_PUT_REF_AND_RESET(ui8
);
93 BT_OBJECT_PUT_REF_AND_RESET(ui16
);
94 BT_OBJECT_PUT_REF_AND_RESET(ui32
);
98 static struct bt_ctf_field_type
*create_writer_integer_struct(void)
101 struct bt_ctf_field_type
*structure
= NULL
;
102 struct bt_ctf_field_type
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
104 structure
= bt_ctf_field_type_structure_create();
105 BT_ASSERT(structure
);
106 ui8
= bt_ctf_field_type_integer_create(8);
108 ret
= bt_ctf_field_type_structure_add_field(structure
, ui8
,
111 ui16
= bt_ctf_field_type_integer_create(16);
113 ret
= bt_ctf_field_type_structure_add_field(structure
, ui16
,
116 ui32
= bt_ctf_field_type_integer_create(32);
118 ret
= bt_ctf_field_type_structure_add_field(structure
, ui32
,
121 BT_OBJECT_PUT_REF_AND_RESET(ui8
);
122 BT_OBJECT_PUT_REF_AND_RESET(ui16
);
123 BT_OBJECT_PUT_REF_AND_RESET(ui32
);
128 * A simple event has the following payload:
129 * - uint8_t payload_8;
130 * - uint16_t payload_16;
131 * - uint32_t payload_32;
133 static struct bt_private_event_class
*create_simple_event(
134 struct bt_private_stream_class
*sc
, const char *name
)
137 struct bt_private_event_class
*event
= NULL
;
138 struct bt_private_field_class
*payload
= NULL
;
141 event
= bt_private_event_class_create(sc
);
143 ret
= bt_private_event_class_set_name(event
, name
);
145 payload
= create_integer_struct();
147 ret
= bt_private_event_class_set_payload_field_class(event
, payload
);
149 BT_OBJECT_PUT_REF_AND_RESET(payload
);
154 * A complex event has the following payload:
155 * - uint8_t payload_8;
156 * - uint16_t payload_16;
157 * - uint32_t payload_32;
158 * - struct payload_struct:
159 * - uint8_t payload_8;
160 * - uint16_t payload_16;
161 * - uint32_t payload_32;
163 static struct bt_private_event_class
*create_complex_event(
164 struct bt_private_stream_class
*sc
,
168 struct bt_private_event_class
*event
= NULL
;
169 struct bt_private_field_class
*inner
= NULL
, *outer
= NULL
;
172 event
= bt_private_event_class_create(sc
);
174 ret
= bt_private_event_class_set_name(event
, name
);
176 outer
= create_integer_struct();
178 inner
= create_integer_struct();
180 ret
= bt_private_field_class_structure_append_member(outer
,
181 "payload_struct", inner
);
183 ret
= bt_private_event_class_set_payload_field_class(event
, outer
);
185 BT_OBJECT_PUT_REF_AND_RESET(inner
);
186 BT_OBJECT_PUT_REF_AND_RESET(outer
);
190 static void set_stream_class_field_classes(
191 struct bt_private_stream_class
*stream_class
)
193 struct bt_private_field_class
*packet_context_type
;
194 struct bt_private_field_class
*event_header_type
;
195 struct bt_private_field_class
*fc
;
198 packet_context_type
= bt_private_field_class_structure_create();
199 BT_ASSERT(packet_context_type
);
200 fc
= bt_private_field_class_unsigned_integer_create();
202 bt_private_field_class_integer_set_field_value_range(fc
, 32);
203 ret
= bt_private_field_class_structure_append_member(packet_context_type
,
206 bt_object_put_ref(fc
);
207 fc
= bt_private_field_class_unsigned_integer_create();
209 bt_private_field_class_integer_set_field_value_range(fc
, 32);
210 ret
= bt_private_field_class_structure_append_member(packet_context_type
,
213 bt_object_put_ref(fc
);
214 event_header_type
= bt_private_field_class_structure_create();
215 BT_ASSERT(event_header_type
);
216 fc
= bt_private_field_class_unsigned_integer_create();
218 bt_private_field_class_integer_set_field_value_range(fc
, 32);
219 ret
= bt_private_field_class_structure_append_member(event_header_type
,
222 bt_object_put_ref(fc
);
223 ret
= bt_private_stream_class_set_packet_context_field_class(
224 stream_class
, packet_context_type
);
226 ret
= bt_private_stream_class_set_event_header_field_class(
227 stream_class
, event_header_type
);
229 bt_object_put_ref(packet_context_type
);
230 bt_object_put_ref(event_header_type
);
233 static void create_sc1(struct bt_private_trace
*trace
)
236 struct bt_private_event_class
*ec1
= NULL
, *ec2
= NULL
;
237 struct bt_private_stream_class
*sc1
= NULL
, *ret_stream
= NULL
;
239 sc1
= bt_private_stream_class_create(trace
);
241 ret
= bt_private_stream_class_set_name(sc1
, "sc1");
243 set_stream_class_field_classes(sc1
);
244 ec1
= create_complex_event(sc1
, "ec1");
246 ec2
= create_simple_event(sc1
, "ec2");
248 ret_stream
= bt_private_event_class_borrow_stream_class(ec1
);
249 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC1");
250 ret_stream
= bt_private_event_class_borrow_stream_class(ec2
);
251 ok(ret_stream
== sc1
, "Borrow parent stream SC1 from EC2");
252 BT_OBJECT_PUT_REF_AND_RESET(ec1
);
253 BT_OBJECT_PUT_REF_AND_RESET(ec2
);
254 BT_OBJECT_PUT_REF_AND_RESET(sc1
);
257 static void create_sc2(struct bt_private_trace
*trace
)
260 struct bt_private_event_class
*ec3
= NULL
;
261 struct bt_private_stream_class
*sc2
= NULL
, *ret_stream
= NULL
;
263 sc2
= bt_private_stream_class_create(trace
);
265 ret
= bt_private_stream_class_set_name(sc2
, "sc2");
267 set_stream_class_field_classes(sc2
);
268 ec3
= create_simple_event(sc2
, "ec3");
269 ret_stream
= bt_private_event_class_borrow_stream_class(ec3
);
270 ok(ret_stream
== sc2
, "Borrow parent stream SC2 from EC3");
271 BT_OBJECT_PUT_REF_AND_RESET(ec3
);
272 BT_OBJECT_PUT_REF_AND_RESET(sc2
);
275 static void set_trace_packet_header(struct bt_private_trace
*trace
)
277 struct bt_private_field_class
*packet_header_type
;
278 struct bt_private_field_class
*fc
;
281 packet_header_type
= bt_private_field_class_structure_create();
282 BT_ASSERT(packet_header_type
);
283 fc
= bt_private_field_class_unsigned_integer_create();
285 bt_private_field_class_integer_set_field_value_range(fc
, 32);
286 ret
= bt_private_field_class_structure_append_member(packet_header_type
,
289 bt_object_put_ref(fc
);
290 ret
= bt_private_trace_set_packet_header_field_class(trace
,
294 bt_object_put_ref(packet_header_type
);
297 static struct bt_private_trace
*create_tc1(void)
299 struct bt_private_trace
*tc1
= NULL
;
301 tc1
= bt_private_trace_create();
303 set_trace_packet_header(tc1
);
309 static void init_weak_refs(struct bt_private_trace
*tc
,
310 struct bt_private_trace
**tc1
,
311 struct bt_private_stream_class
**sc1
,
312 struct bt_private_stream_class
**sc2
,
313 struct bt_private_event_class
**ec1
,
314 struct bt_private_event_class
**ec2
,
315 struct bt_private_event_class
**ec3
)
318 *sc1
= bt_private_trace_borrow_stream_class_by_index(tc
, 0);
319 *sc2
= bt_private_trace_borrow_stream_class_by_index(tc
, 1);
320 *ec1
= bt_private_stream_class_borrow_event_class_by_index(*sc1
, 0);
321 *ec2
= bt_private_stream_class_borrow_event_class_by_index(*sc1
, 1);
322 *ec3
= bt_private_stream_class_borrow_event_class_by_index(*sc2
, 0);
325 static void test_example_scenario(void)
328 * Weak pointers to trace IR objects are to be used very
329 * carefully. This is NOT a good practice and is strongly
330 * discouraged; this is only done to facilitate the validation
331 * of expected reference counts without affecting them by taking
332 * "real" references to the objects.
334 struct bt_private_trace
*tc1
= NULL
, *weak_tc1
= NULL
;
335 struct bt_private_stream_class
*weak_sc1
= NULL
, *weak_sc2
= NULL
;
336 struct bt_private_event_class
*weak_ec1
= NULL
, *weak_ec2
= NULL
,
338 struct user user_a
= { 0 }, user_b
= { 0 }, user_c
= { 0 };
340 /* The only reference which exists at this point is on TC1. */
342 ok(tc1
, "Initialize trace");
344 init_weak_refs(tc1
, &weak_tc1
, &weak_sc1
, &weak_sc2
, &weak_ec1
,
345 &weak_ec2
, &weak_ec3
);
346 ok(bt_object_get_ref_count((void *) weak_sc1
) == 0,
347 "Initial SC1 reference count is 0");
348 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
349 "Initial SC2 reference count is 0");
350 ok(bt_object_get_ref_count((void *) weak_ec1
) == 0,
351 "Initial EC1 reference count is 0");
352 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
353 "Initial EC2 reference count is 0");
354 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
355 "Initial EC3 reference count is 0");
357 /* User A has ownership of the trace. */
358 BT_OBJECT_MOVE_REF(user_a
.tc
, tc1
);
359 ok(bt_object_get_ref_count((void *) user_a
.tc
) == 1,
360 "TC1 reference count is 1");
362 /* User A acquires a reference to SC2 from TC1. */
363 user_a
.sc
= bt_object_get_ref(
364 bt_private_trace_borrow_stream_class_by_index(
366 ok(user_a
.sc
, "User A acquires SC2 from TC1");
367 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
368 "TC1 reference count is 2");
369 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
370 "SC2 reference count is 1");
372 /* User A acquires a reference to EC3 from SC2. */
373 user_a
.ec
= bt_object_get_ref(
374 bt_private_stream_class_borrow_event_class_by_index(
376 ok(user_a
.ec
, "User A acquires EC3 from SC2");
377 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
378 "TC1 reference count is 2");
379 ok(bt_object_get_ref_count((void *) weak_sc2
) == 2,
380 "SC2 reference count is 2");
381 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
382 "EC3 reference count is 1");
384 /* User A releases its reference to SC2. */
385 diag("User A releases SC2");
386 BT_OBJECT_PUT_REF_AND_RESET(user_a
.sc
);
388 * We keep the pointer to SC2 around to validate its reference
391 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
392 "TC1 reference count is 2");
393 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
394 "SC2 reference count is 1");
395 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
396 "EC3 reference count is 1");
398 /* User A releases its reference to TC1. */
399 diag("User A releases TC1");
400 BT_OBJECT_PUT_REF_AND_RESET(user_a
.tc
);
402 * We keep the pointer to TC1 around to validate its reference
405 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
406 "TC1 reference count is 1");
407 ok(bt_object_get_ref_count((void *) weak_sc2
) == 1,
408 "SC2 reference count is 1");
409 ok(bt_object_get_ref_count((void *) weak_ec3
) == 1,
410 "EC3 reference count is 1");
412 /* User B acquires a reference to SC1. */
413 diag("User B acquires a reference to SC1");
414 user_b
.sc
= bt_object_get_ref(weak_sc1
);
415 ok(bt_object_get_ref_count((void *) weak_tc1
) == 2,
416 "TC1 reference count is 2");
417 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
418 "SC1 reference count is 1");
420 /* User C acquires a reference to EC1. */
421 diag("User C acquires a reference to EC1");
422 user_c
.ec
= bt_object_get_ref(
423 bt_private_stream_class_borrow_event_class_by_index(
425 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
426 "EC1 reference count is 1");
427 ok(bt_object_get_ref_count((void *) weak_sc1
) == 2,
428 "SC1 reference count is 2");
430 /* User A releases its reference on EC3. */
431 diag("User A releases its reference on EC3");
432 BT_OBJECT_PUT_REF_AND_RESET(user_a
.ec
);
433 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
434 "EC3 reference count is 1");
435 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
436 "SC2 reference count is 0");
437 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
438 "TC1 reference count is 1");
440 /* User B releases its reference on SC1. */
441 diag("User B releases its reference on SC1");
442 BT_OBJECT_PUT_REF_AND_RESET(user_b
.sc
);
443 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
444 "SC1 reference count is 1");
447 * User C is the sole owner of an object and is keeping the whole
448 * trace hierarchy "alive" by holding a reference to EC1.
450 ok(bt_object_get_ref_count((void *) weak_tc1
) == 1,
451 "TC1 reference count is 1");
452 ok(bt_object_get_ref_count((void *) weak_sc1
) == 1,
453 "SC1 reference count is 1");
454 ok(bt_object_get_ref_count((void *) weak_sc2
) == 0,
455 "SC2 reference count is 0");
456 ok(bt_object_get_ref_count((void *) weak_ec1
) == 1,
457 "EC1 reference count is 1");
458 ok(bt_object_get_ref_count((void *) weak_ec2
) == 0,
459 "EC2 reference count is 0");
460 ok(bt_object_get_ref_count((void *) weak_ec3
) == 0,
461 "EC3 reference count is 0");
463 /* Reclaim last reference held by User C. */
464 BT_OBJECT_PUT_REF_AND_RESET(user_c
.ec
);
467 static void create_writer_user_full(struct writer_user
*user
)
470 struct bt_ctf_field_type
*ft
;
471 struct bt_ctf_field
*field
;
472 struct bt_ctf_clock
*clock
;
475 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
476 if (!bt_mkdtemp(trace_path
)) {
480 user
->writer
= bt_ctf_writer_create(trace_path
);
481 BT_ASSERT(user
->writer
);
482 ret
= bt_ctf_writer_set_byte_order(user
->writer
,
483 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
485 user
->tc
= bt_ctf_writer_get_trace(user
->writer
);
487 user
->sc
= bt_ctf_stream_class_create("sc");
489 clock
= bt_ctf_clock_create("the_clock");
491 ret
= bt_ctf_writer_add_clock(user
->writer
, clock
);
493 ret
= bt_ctf_stream_class_set_clock(user
->sc
, clock
);
495 BT_OBJECT_PUT_REF_AND_RESET(clock
);
496 user
->stream
= bt_ctf_writer_create_stream(user
->writer
, user
->sc
);
497 BT_ASSERT(user
->stream
);
498 user
->ec
= bt_ctf_event_class_create("ec");
500 ft
= create_writer_integer_struct();
502 ret
= bt_ctf_event_class_set_payload_field_type(user
->ec
, ft
);
503 BT_OBJECT_PUT_REF_AND_RESET(ft
);
505 ret
= bt_ctf_stream_class_add_event_class(user
->sc
, user
->ec
);
507 user
->event
= bt_ctf_event_create(user
->ec
);
508 BT_ASSERT(user
->event
);
509 field
= bt_ctf_event_get_payload(user
->event
, "payload_8");
511 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 10);
513 BT_OBJECT_PUT_REF_AND_RESET(field
);
514 field
= bt_ctf_event_get_payload(user
->event
, "payload_16");
516 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 20);
518 BT_OBJECT_PUT_REF_AND_RESET(field
);
519 field
= bt_ctf_event_get_payload(user
->event
, "payload_32");
521 ret
= bt_ctf_field_integer_unsigned_set_value(field
, 30);
523 BT_OBJECT_PUT_REF_AND_RESET(field
);
524 ret
= bt_ctf_stream_append_event(user
->stream
, user
->event
);
526 recursive_rmdir(trace_path
);
530 static void test_put_order_swap(size_t *array
, size_t a
, size_t b
)
532 size_t temp
= array
[a
];
538 static void test_put_order_put_objects(size_t *array
, size_t size
)
541 struct writer_user user
= { 0 };
542 void **objects
= (void *) &user
;
544 create_writer_user_full(&user
);
547 for (i
= 0; i
< size
; ++i
) {
548 void *obj
= objects
[array
[i
]];
550 printf("%s", writer_user_names
[array
[i
]]);
551 BT_OBJECT_PUT_REF_AND_RESET(obj
);
561 static void test_put_order_permute(size_t *array
, int k
, size_t size
)
564 test_put_order_put_objects(array
, size
);
568 for (i
= k
- 1; i
>= 0; i
--) {
569 size_t next_k
= k
- 1;
571 test_put_order_swap(array
, i
, next_k
);
572 test_put_order_permute(array
, next_k
, size
);
573 test_put_order_swap(array
, i
, next_k
);
578 static void test_put_order(void)
581 size_t array
[WRITER_USER_NR_ELEMENTS
];
583 /* Initialize array of indexes */
584 for (i
= 0; i
< WRITER_USER_NR_ELEMENTS
; ++i
) {
588 test_put_order_permute(array
, WRITER_USER_NR_ELEMENTS
,
589 WRITER_USER_NR_ELEMENTS
);
593 * The objective of this test is to implement and expand upon the scenario
594 * described in the reference counting documentation and ensure that any node of
595 * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all
596 * other "alive" and reachable.
598 * External tools (e.g. valgrind) should be used to confirm that this
599 * known-good test does not leak memory.
601 int main(int argc
, char **argv
)
603 /* Initialize tap harness before any tests */
604 plan_tests(NR_TESTS
);
606 test_example_scenario();
609 return exit_status();