4 * CTF 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/ctf-writer/writer.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/clock.h>
26 #include <babeltrace/ctf-ir/trace.h>
27 #include <babeltrace/ctf-writer/stream-class.h>
28 #include <babeltrace/ctf-ir/stream.h>
29 #include <babeltrace/ctf-ir/fields.h>
30 #include <babeltrace/ctf-ir/event.h>
31 #include <babeltrace/ctf-ir/event-class.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/compat/stdlib-internal.h>
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 *user_names
[] = {
57 static const size_t USER_NR_ELEMENTS
= sizeof(struct user
) / sizeof(void *);
60 * Returns a structure containing the following fields:
61 * - uint8_t payload_8;
62 * - uint16_t payload_16;
63 * - uint32_t payload_32;
65 static struct bt_ctf_field_type
*create_integer_struct(void)
68 struct bt_ctf_field_type
*structure
= NULL
;
69 struct bt_ctf_field_type
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
71 structure
= bt_ctf_field_type_structure_create();
76 ui8
= bt_ctf_field_type_integer_create(8);
78 diag("Failed to create uint8_t type");
81 ret
= bt_ctf_field_type_structure_add_field(structure
, ui8
,
84 diag("Failed to add uint8_t to structure");
87 ui16
= bt_ctf_field_type_integer_create(16);
89 diag("Failed to create uint16_t type");
92 ret
= bt_ctf_field_type_structure_add_field(structure
, ui16
,
95 diag("Failed to add uint16_t to structure");
98 ui32
= bt_ctf_field_type_integer_create(32);
100 diag("Failed to create uint32_t type");
103 ret
= bt_ctf_field_type_structure_add_field(structure
, ui32
,
106 diag("Failed to add uint32_t to structure");
120 * A simple event has the following payload:
121 * - uint8_t payload_8;
122 * - uint16_t payload_16;
123 * - uint32_t payload_32;
125 static struct bt_ctf_event_class
*create_simple_event(const char *name
)
128 struct bt_ctf_event_class
*event
= NULL
;
129 struct bt_ctf_field_type
*payload
= NULL
;
132 event
= bt_ctf_event_class_create(name
);
134 diag("Failed to create simple event");
138 payload
= create_integer_struct();
140 diag("Failed to initialize integer structure");
144 ret
= bt_ctf_event_class_set_payload_type(event
, payload
);
146 diag("Failed to set simple event payload");
158 * A complex event has the following payload:
159 * - uint8_t payload_8;
160 * - uint16_t payload_16;
161 * - uint32_t payload_32;
162 * - struct payload_struct:
163 * - uint8_t payload_8;
164 * - uint16_t payload_16;
165 * - uint32_t payload_32;
167 static struct bt_ctf_event_class
*create_complex_event(const char *name
)
170 struct bt_ctf_event_class
*event
= NULL
;
171 struct bt_ctf_field_type
*inner
= NULL
, *outer
= NULL
;
174 event
= bt_ctf_event_class_create(name
);
176 diag("Failed to create complex event");
180 outer
= create_integer_struct();
182 diag("Failed to initialize integer structure");
186 inner
= create_integer_struct();
188 diag("Failed to initialize integer structure");
192 ret
= bt_ctf_field_type_structure_add_field(outer
, inner
,
195 diag("Failed to add inner structure to outer structure");
199 ret
= bt_ctf_event_class_set_payload_type(event
, outer
);
201 diag("Failed to set complex event payload");
213 static struct bt_ctf_stream_class
*create_sc1(void)
216 struct bt_ctf_event_class
*ec1
= NULL
, *ec2
= NULL
;
217 struct bt_ctf_stream_class
*sc1
= NULL
, *ret_stream
= NULL
;
219 sc1
= bt_ctf_stream_class_create("sc1");
221 diag("Failed to create Stream Class");
225 ec1
= create_complex_event("ec1");
227 diag("Failed to create complex event EC1");
230 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec1
);
232 diag("Failed to add EC1 to SC1");
236 ec2
= create_simple_event("ec2");
238 diag("Failed to create simple event EC2");
241 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec2
);
243 diag("Failed to add EC1 to SC1");
247 ret_stream
= bt_ctf_event_class_get_stream_class(ec1
);
248 ok(ret_stream
== sc1
, "Get parent stream SC1 from EC1");
251 ret_stream
= bt_ctf_event_class_get_stream_class(ec2
);
252 ok(ret_stream
== sc1
, "Get parent stream SC1 from EC2");
263 static struct bt_ctf_stream_class
*create_sc2(void)
266 struct bt_ctf_event_class
*ec3
= NULL
;
267 struct bt_ctf_stream_class
*sc2
= NULL
, *ret_stream
= NULL
;
269 sc2
= bt_ctf_stream_class_create("sc2");
271 diag("Failed to create Stream Class");
275 ec3
= create_simple_event("ec3");
277 diag("Failed to create simple event EC3");
280 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec3
);
282 diag("Failed to add EC3 to SC2");
286 ret_stream
= bt_ctf_event_class_get_stream_class(ec3
);
287 ok(ret_stream
== sc2
, "Get parent stream SC2 from EC3");
297 static struct bt_ctf_trace
*create_tc1(void)
300 struct bt_ctf_trace
*tc1
= NULL
;
301 struct bt_ctf_stream_class
*sc1
= NULL
, *sc2
= NULL
;
303 tc1
= bt_ctf_trace_create();
305 diag("bt_ctf_trace_create returned NULL");
309 ret
= bt_ctf_trace_set_native_byte_order(tc1
,
310 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
313 ok(sc1
, "Create SC1");
317 ret
= bt_ctf_trace_add_stream_class(tc1
, sc1
);
318 ok(!ret
, "Add SC1 to TC1");
324 ok(sc2
, "Create SC2");
328 ret
= bt_ctf_trace_add_stream_class(tc1
, sc2
);
329 ok(!ret
, "Add SC2 to TC1");
342 static void init_weak_refs(struct bt_ctf_trace
*tc
,
343 struct bt_ctf_trace
**tc1
,
344 struct bt_ctf_stream_class
**sc1
,
345 struct bt_ctf_stream_class
**sc2
,
346 struct bt_ctf_event_class
**ec1
,
347 struct bt_ctf_event_class
**ec2
,
348 struct bt_ctf_event_class
**ec3
)
351 *sc1
= bt_ctf_trace_get_stream_class_by_index(tc
, 0);
352 *sc2
= bt_ctf_trace_get_stream_class_by_index(tc
, 1);
353 *ec1
= bt_ctf_stream_class_get_event_class_by_index(*sc1
, 0);
354 *ec2
= bt_ctf_stream_class_get_event_class_by_index(*sc1
, 1);
355 *ec3
= bt_ctf_stream_class_get_event_class_by_index(*sc2
, 0);
363 static void test_example_scenario(void)
366 * Weak pointers to CTF-IR objects are to be used very carefully.
367 * This is NOT a good practice and is strongly discouraged; this
368 * is only done to facilitate the validation of expected reference
369 * counts without affecting them by taking "real" references to the
372 struct bt_ctf_trace
*tc1
= NULL
, *weak_tc1
= NULL
;
373 struct bt_ctf_stream_class
*weak_sc1
= NULL
, *weak_sc2
= NULL
;
374 struct bt_ctf_event_class
*weak_ec1
= NULL
, *weak_ec2
= NULL
,
376 struct user user_a
= { 0 }, user_b
= { 0 }, user_c
= { 0 };
378 /* The only reference which exists at this point is on TC1. */
380 ok(tc1
, "Initialize trace");
385 init_weak_refs(tc1
, &weak_tc1
, &weak_sc1
, &weak_sc2
, &weak_ec1
,
386 &weak_ec2
, &weak_ec3
);
388 ok(bt_object_get_ref_count(weak_sc1
) == 0,
389 "Initial SC1 reference count is 0");
390 ok(bt_object_get_ref_count(weak_sc2
) == 0,
391 "Initial SC2 reference count is 0");
392 ok(bt_object_get_ref_count(weak_ec1
) == 0,
393 "Initial EC1 reference count is 0");
394 ok(bt_object_get_ref_count(weak_ec2
) == 0,
395 "Initial EC2 reference count is 0");
396 ok(bt_object_get_ref_count(weak_ec3
) == 0,
397 "Initial EC3 reference count is 0");
399 /* User A has ownership of the trace. */
400 BT_MOVE(user_a
.tc
, tc1
);
401 ok(bt_object_get_ref_count(user_a
.tc
) == 1,
402 "TC1 reference count is 1");
404 /* User A acquires a reference to SC2 from TC1. */
405 user_a
.sc
= bt_ctf_trace_get_stream_class_by_index(user_a
.tc
, 1);
406 ok(user_a
.sc
, "User A acquires SC2 from TC1");
407 ok(bt_object_get_ref_count(weak_tc1
) == 2,
408 "TC1 reference count is 2");
409 ok(bt_object_get_ref_count(weak_sc2
) == 1,
410 "SC2 reference count is 1");
412 /* User A acquires a reference to EC3 from SC2. */
413 user_a
.ec
= bt_ctf_stream_class_get_event_class_by_index(user_a
.sc
, 0);
414 ok(user_a
.ec
, "User A acquires EC3 from SC2");
415 ok(bt_object_get_ref_count(weak_tc1
) == 2,
416 "TC1 reference count is 2");
417 ok(bt_object_get_ref_count(weak_sc2
) == 2,
418 "SC2 reference count is 2");
419 ok(bt_object_get_ref_count(weak_ec3
) == 1,
420 "EC3 reference count is 1");
422 /* User A releases its reference to SC2. */
423 diag("User A releases SC2");
426 * We keep the pointer to SC2 around to validate its reference
429 ok(bt_object_get_ref_count(weak_tc1
) == 2,
430 "TC1 reference count is 2");
431 ok(bt_object_get_ref_count(weak_sc2
) == 1,
432 "SC2 reference count is 1");
433 ok(bt_object_get_ref_count(weak_ec3
) == 1,
434 "EC3 reference count is 1");
436 /* User A releases its reference to TC1. */
437 diag("User A releases TC1");
440 * We keep the pointer to TC1 around to validate its reference
443 ok(bt_object_get_ref_count(weak_tc1
) == 1,
444 "TC1 reference count is 1");
445 ok(bt_object_get_ref_count(weak_sc2
) == 1,
446 "SC2 reference count is 1");
447 ok(bt_object_get_ref_count(weak_ec3
) == 1,
448 "EC3 reference count is 1");
450 /* User B acquires a reference to SC1. */
451 diag("User B acquires a reference to SC1");
452 user_b
.sc
= bt_get(weak_sc1
);
453 ok(bt_object_get_ref_count(weak_tc1
) == 2,
454 "TC1 reference count is 2");
455 ok(bt_object_get_ref_count(weak_sc1
) == 1,
456 "SC1 reference count is 1");
458 /* User C acquires a reference to EC1. */
459 diag("User C acquires a reference to EC1");
460 user_c
.ec
= bt_ctf_stream_class_get_event_class_by_index(user_b
.sc
, 0);
461 ok(bt_object_get_ref_count(weak_ec1
) == 1,
462 "EC1 reference count is 1");
463 ok(bt_object_get_ref_count(weak_sc1
) == 2,
464 "SC1 reference count is 2");
466 /* User A releases its reference on EC3. */
467 diag("User A releases its reference on EC3");
469 ok(bt_object_get_ref_count(weak_ec3
) == 0,
470 "EC3 reference count is 1");
471 ok(bt_object_get_ref_count(weak_sc2
) == 0,
472 "SC2 reference count is 0");
473 ok(bt_object_get_ref_count(weak_tc1
) == 1,
474 "TC1 reference count is 1");
476 /* User B releases its reference on SC1. */
477 diag("User B releases its reference on SC1");
479 ok(bt_object_get_ref_count(weak_sc1
) == 1,
480 "SC1 reference count is 1");
483 * User C is the sole owner of an object and is keeping the whole
484 * trace hierarchy "alive" by holding a reference to EC1.
486 ok(bt_object_get_ref_count(weak_tc1
) == 1,
487 "TC1 reference count is 1");
488 ok(bt_object_get_ref_count(weak_sc1
) == 1,
489 "SC1 reference count is 1");
490 ok(bt_object_get_ref_count(weak_sc2
) == 0,
491 "SC2 reference count is 0");
492 ok(bt_object_get_ref_count(weak_ec1
) == 1,
493 "EC1 reference count is 1");
494 ok(bt_object_get_ref_count(weak_ec2
) == 0,
495 "EC2 reference count is 0");
496 ok(bt_object_get_ref_count(weak_ec3
) == 0,
497 "EC3 reference count is 0");
499 /* Reclaim last reference held by User C. */
503 static void create_user_full(struct user
*user
)
505 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
506 struct bt_ctf_field_type
*ft
;
507 struct bt_ctf_field
*field
;
508 struct bt_ctf_clock
*clock
;
511 if (!bt_mkdtemp(trace_path
)) {
515 user
->writer
= bt_ctf_writer_create(trace_path
);
516 assert(user
->writer
);
517 ret
= bt_ctf_writer_set_byte_order(user
->writer
,
518 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
520 user
->tc
= bt_ctf_writer_get_trace(user
->writer
);
522 user
->sc
= bt_ctf_stream_class_create("sc");
524 clock
= bt_ctf_clock_create("the_clock");
526 ret
= bt_ctf_writer_add_clock(user
->writer
, clock
);
528 ret
= bt_ctf_stream_class_set_clock(user
->sc
, clock
);
531 user
->stream
= bt_ctf_writer_create_stream(user
->writer
, user
->sc
);
532 assert(user
->stream
);
533 user
->ec
= bt_ctf_event_class_create("ec");
535 ft
= create_integer_struct();
537 ret
= bt_ctf_event_class_set_payload_type(user
->ec
, ft
);
540 ret
= bt_ctf_stream_class_add_event_class(user
->sc
, user
->ec
);
542 user
->event
= bt_ctf_event_create(user
->ec
);
544 field
= bt_ctf_event_get_payload(user
->event
, "payload_8");
546 ret
= bt_ctf_field_unsigned_integer_set_value(field
, 10);
549 field
= bt_ctf_event_get_payload(user
->event
, "payload_16");
551 ret
= bt_ctf_field_unsigned_integer_set_value(field
, 20);
554 field
= bt_ctf_event_get_payload(user
->event
, "payload_32");
556 ret
= bt_ctf_field_unsigned_integer_set_value(field
, 30);
559 ret
= bt_ctf_stream_append_event(user
->stream
, user
->event
);
561 recursive_rmdir(trace_path
);
564 static void test_put_order_swap(size_t *array
, size_t a
, size_t b
)
566 size_t temp
= array
[a
];
572 static void test_put_order_put_objects(size_t *array
, size_t size
)
575 struct user user
= { 0 };
576 void **objects
= (void *) &user
;
578 create_user_full(&user
);
581 for (i
= 0; i
< size
; ++i
) {
582 void *obj
= objects
[array
[i
]];
584 printf("%s", user_names
[array
[i
]]);
595 static void test_put_order_permute(size_t *array
, int k
, size_t size
)
598 test_put_order_put_objects(array
, size
);
602 for (i
= k
- 1; i
>= 0; i
--) {
603 size_t next_k
= k
- 1;
605 test_put_order_swap(array
, i
, next_k
);
606 test_put_order_permute(array
, next_k
, size
);
607 test_put_order_swap(array
, i
, next_k
);
612 static void test_put_order(void)
615 size_t array
[USER_NR_ELEMENTS
];
617 /* Initialize array of indexes */
618 for (i
= 0; i
< USER_NR_ELEMENTS
; ++i
) {
622 test_put_order_permute(array
, USER_NR_ELEMENTS
, USER_NR_ELEMENTS
);
626 * The objective of this test is to implement and expand upon the scenario
627 * described in the reference counting documentation and ensure that any node of
628 * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all
629 * other "alive" and reachable.
631 * External tools (e.g. valgrind) should be used to confirm that this
632 * known-good test does not leak memory.
634 int main(int argc
, char **argv
)
636 /* Initialize tap harness before any tests */
637 plan_tests(NR_TESTS
);
639 test_example_scenario();
642 return exit_status();