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-ir/trace.h>
24 #include <babeltrace/ctf-ir/stream-class.h>
25 #include <babeltrace/ctf-ir/stream.h>
26 #include <babeltrace/ctf-ir/event.h>
27 #include <babeltrace/object-internal.h>
33 struct bt_ctf_trace
*tc
;
34 struct bt_ctf_stream_class
*sc
;
35 struct bt_ctf_event_class
*ec
;
39 * Returns a structure containing the following fields:
40 * - uint8_t payload_8;
41 * - uint16_t payload_16;
42 * - uint32_t payload_32;
44 static struct bt_ctf_field_type
*create_integer_struct(void)
47 struct bt_ctf_field_type
*structure
= NULL
;
48 struct bt_ctf_field_type
*ui8
= NULL
, *ui16
= NULL
, *ui32
= NULL
;
50 structure
= bt_ctf_field_type_structure_create();
55 ui8
= bt_ctf_field_type_integer_create(8);
57 diag("Failed to create uint8_t type");
60 ret
= bt_ctf_field_type_structure_add_field(structure
, ui8
,
63 diag("Failed to add uint8_t to structure");
66 ui16
= bt_ctf_field_type_integer_create(16);
68 diag("Failed to create uint16_t type");
71 ret
= bt_ctf_field_type_structure_add_field(structure
, ui16
,
74 diag("Failed to add uint16_t to structure");
77 ui32
= bt_ctf_field_type_integer_create(32);
79 diag("Failed to create uint32_t type");
82 ret
= bt_ctf_field_type_structure_add_field(structure
, ui32
,
85 diag("Failed to add uint32_t to structure");
99 * A simple event has the following payload:
100 * - uint8_t payload_8;
101 * - uint16_t payload_16;
102 * - uint32_t payload_32;
104 static struct bt_ctf_event_class
*create_simple_event(const char *name
)
107 struct bt_ctf_event_class
*event
= NULL
;
108 struct bt_ctf_field_type
*payload
= NULL
;
111 event
= bt_ctf_event_class_create(name
);
113 diag("Failed to create simple event");
117 payload
= create_integer_struct();
119 diag("Failed to initialize integer structure");
123 ret
= bt_ctf_event_class_set_payload_type(event
, payload
);
125 diag("Failed to set simple event payload");
137 * A complex event has the following payload:
138 * - uint8_t payload_8;
139 * - uint16_t payload_16;
140 * - uint32_t payload_32;
141 * - struct payload_struct:
142 * - uint8_t payload_8;
143 * - uint16_t payload_16;
144 * - uint32_t payload_32;
146 static struct bt_ctf_event_class
*create_complex_event(const char *name
)
149 struct bt_ctf_event_class
*event
= NULL
;
150 struct bt_ctf_field_type
*inner
= NULL
, *outer
= NULL
;
153 event
= bt_ctf_event_class_create(name
);
155 diag("Failed to create complex event");
159 outer
= create_integer_struct();
161 diag("Failed to initialize integer structure");
165 inner
= create_integer_struct();
167 diag("Failed to initialize integer structure");
171 ret
= bt_ctf_field_type_structure_add_field(outer
, inner
,
174 diag("Failed to add inner structure to outer structure");
178 ret
= bt_ctf_event_class_set_payload_type(event
, outer
);
180 diag("Failed to set complex event payload");
192 static struct bt_ctf_stream_class
*create_sc1(void)
195 struct bt_ctf_event_class
*ec1
= NULL
, *ec2
= NULL
;
196 struct bt_ctf_stream_class
*sc1
= NULL
, *ret_stream
= NULL
;
198 sc1
= bt_ctf_stream_class_create("sc1");
200 diag("Failed to create Stream Class");
204 ec1
= create_complex_event("ec1");
206 diag("Failed to create complex event EC1");
209 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec1
);
211 diag("Failed to add EC1 to SC1");
215 ec2
= create_simple_event("ec2");
217 diag("Failed to create simple event EC2");
220 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec2
);
222 diag("Failed to add EC1 to SC1");
226 ret_stream
= bt_ctf_event_class_get_stream_class(ec1
);
227 ok(ret_stream
== sc1
, "Get parent stream SC1 from EC1");
230 ret_stream
= bt_ctf_event_class_get_stream_class(ec2
);
231 ok(ret_stream
== sc1
, "Get parent stream SC1 from EC2");
242 static struct bt_ctf_stream_class
*create_sc2(void)
245 struct bt_ctf_event_class
*ec3
= NULL
;
246 struct bt_ctf_stream_class
*sc2
= NULL
, *ret_stream
= NULL
;
248 sc2
= bt_ctf_stream_class_create("sc2");
250 diag("Failed to create Stream Class");
254 ec3
= create_simple_event("ec3");
256 diag("Failed to create simple event EC3");
259 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec3
);
261 diag("Failed to add EC3 to SC2");
265 ret_stream
= bt_ctf_event_class_get_stream_class(ec3
);
266 ok(ret_stream
== sc2
, "Get parent stream SC2 from EC3");
276 static struct bt_ctf_trace
*create_tc1(void)
279 struct bt_ctf_trace
*tc1
= NULL
;
280 struct bt_ctf_stream_class
*sc1
= NULL
, *sc2
= NULL
;
282 tc1
= bt_ctf_trace_create();
284 diag("bt_ctf_trace_create returned NULL");
289 ok(sc1
, "Create SC1");
293 ret
= bt_ctf_trace_add_stream_class(tc1
, sc1
);
294 ok(!ret
, "Add SC1 to TC1");
300 ok(sc2
, "Create SC2");
304 ret
= bt_ctf_trace_add_stream_class(tc1
, sc2
);
305 ok(!ret
, "Add SC2 to TC1");
318 static void init_weak_refs(struct bt_ctf_trace
*tc
,
319 struct bt_ctf_trace
**tc1
,
320 struct bt_ctf_stream_class
**sc1
,
321 struct bt_ctf_stream_class
**sc2
,
322 struct bt_ctf_event_class
**ec1
,
323 struct bt_ctf_event_class
**ec2
,
324 struct bt_ctf_event_class
**ec3
)
327 *sc1
= bt_ctf_trace_get_stream_class(tc
, 0);
328 *sc2
= bt_ctf_trace_get_stream_class(tc
, 1);
329 *ec1
= bt_ctf_stream_class_get_event_class(*sc1
, 0);
330 *ec2
= bt_ctf_stream_class_get_event_class(*sc1
, 1);
331 *ec3
= bt_ctf_stream_class_get_event_class(*sc2
, 0);
340 * The objective of this test is to implement and expand upon the scenario
341 * described in the reference counting documentation and ensure that any node of
342 * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all
343 * other "alive" and reachable.
345 * External tools (e.g. valgrind) should be used to confirm that this
346 * known-good test does not leak memory.
348 int main(int argc
, char **argv
)
351 * Weak pointers to CTF-IR objects are to be used very carefully.
352 * This is NOT a good practice and is strongly discouraged; this
353 * is only done to facilitate the validation of expected reference
354 * counts without affecting them by taking "real" references to the
357 struct bt_ctf_trace
*tc1
= NULL
, *weak_tc1
= NULL
;
358 struct bt_ctf_stream_class
*weak_sc1
= NULL
, *weak_sc2
= NULL
;
359 struct bt_ctf_event_class
*weak_ec1
= NULL
, *weak_ec2
= NULL
,
361 struct user user_a
= { 0 }, user_b
= { 0 }, user_c
= { 0 };
363 /* Initialize tap harness before any tests */
364 plan_tests(NR_TESTS
);
366 /* The only reference which exists at this point is on TC1. */
368 ok(tc1
, "Initialize trace");
373 init_weak_refs(tc1
, &weak_tc1
, &weak_sc1
, &weak_sc2
, &weak_ec1
,
374 &weak_ec2
, &weak_ec3
);
376 ok(bt_object_get_ref_count(weak_sc1
) == 0,
377 "Initial SC1 reference count is 0");
378 ok(bt_object_get_ref_count(weak_sc2
) == 0,
379 "Initial SC2 reference count is 0");
380 ok(bt_object_get_ref_count(weak_ec1
) == 0,
381 "Initial EC1 reference count is 0");
382 ok(bt_object_get_ref_count(weak_ec2
) == 0,
383 "Initial EC2 reference count is 0");
384 ok(bt_object_get_ref_count(weak_ec3
) == 0,
385 "Initial EC3 reference count is 0");
387 /* User A has ownership of the trace. */
388 BT_MOVE(user_a
.tc
, tc1
);
389 ok(bt_object_get_ref_count(user_a
.tc
) == 1,
390 "TC1 reference count is 1");
392 /* User A acquires a reference to SC2 from TC1. */
393 user_a
.sc
= bt_ctf_trace_get_stream_class(user_a
.tc
, 1);
394 ok(user_a
.sc
, "User A acquires SC2 from TC1");
395 ok(bt_object_get_ref_count(weak_tc1
) == 2,
396 "TC1 reference count is 2");
397 ok(bt_object_get_ref_count(weak_sc2
) == 1,
398 "SC2 reference count is 1");
400 /* User A acquires a reference to EC3 from SC2. */
401 user_a
.ec
= bt_ctf_stream_class_get_event_class(user_a
.sc
, 0);
402 ok(user_a
.ec
, "User A acquires EC3 from SC2");
403 ok(bt_object_get_ref_count(weak_tc1
) == 2,
404 "TC1 reference count is 2");
405 ok(bt_object_get_ref_count(weak_sc2
) == 2,
406 "SC2 reference count is 2");
407 ok(bt_object_get_ref_count(weak_ec3
) == 1,
408 "EC3 reference count is 1");
410 /* User A releases its reference to SC2. */
411 diag("User A releases SC2");
414 * We keep the pointer to SC2 around to validate its reference
417 ok(bt_object_get_ref_count(weak_tc1
) == 2,
418 "TC1 reference count is 2");
419 ok(bt_object_get_ref_count(weak_sc2
) == 1,
420 "SC2 reference count is 1");
421 ok(bt_object_get_ref_count(weak_ec3
) == 1,
422 "EC3 reference count is 1");
424 /* User A releases its reference to TC1. */
425 diag("User A releases TC1");
428 * We keep the pointer to TC1 around to validate its reference
431 ok(bt_object_get_ref_count(weak_tc1
) == 1,
432 "TC1 reference count is 1");
433 ok(bt_object_get_ref_count(weak_sc2
) == 1,
434 "SC2 reference count is 1");
435 ok(bt_object_get_ref_count(weak_ec3
) == 1,
436 "EC3 reference count is 1");
438 /* User B acquires a reference to SC1. */
439 diag("User B acquires a reference to SC1");
440 user_b
.sc
= bt_get(weak_sc1
);
441 ok(bt_object_get_ref_count(weak_tc1
) == 2,
442 "TC1 reference count is 2");
443 ok(bt_object_get_ref_count(weak_sc1
) == 1,
444 "SC1 reference count is 1");
446 /* User C acquires a reference to EC1. */
447 diag("User C acquires a reference to EC1");
448 user_c
.ec
= bt_ctf_stream_class_get_event_class(user_b
.sc
, 0);
449 ok(bt_object_get_ref_count(weak_ec1
) == 1,
450 "EC1 reference count is 1");
451 ok(bt_object_get_ref_count(weak_sc1
) == 2,
452 "SC1 reference count is 2");
454 /* User A releases its reference on EC3. */
455 diag("User A releases its reference on EC3");
457 ok(bt_object_get_ref_count(weak_ec3
) == 0,
458 "EC3 reference count is 1");
459 ok(bt_object_get_ref_count(weak_sc2
) == 0,
460 "SC2 reference count is 0");
461 ok(bt_object_get_ref_count(weak_tc1
) == 1,
462 "TC1 reference count is 1");
464 /* User B releases its reference on SC1. */
465 diag("User B releases its reference on SC1");
467 ok(bt_object_get_ref_count(weak_sc1
) == 1,
468 "SC1 reference count is 1");
471 * User C is the sole owner of an object and is keeping the whole
472 * trace hierarchy "alive" by holding a reference to EC1.
474 ok(bt_object_get_ref_count(weak_tc1
) == 1,
475 "TC1 reference count is 1");
476 ok(bt_object_get_ref_count(weak_sc1
) == 1,
477 "SC1 reference count is 1");
478 ok(bt_object_get_ref_count(weak_sc2
) == 0,
479 "SC2 reference count is 0");
480 ok(bt_object_get_ref_count(weak_ec1
) == 1,
481 "EC1 reference count is 1");
482 ok(bt_object_get_ref_count(weak_ec2
) == 0,
483 "EC2 reference count is 0");
484 ok(bt_object_get_ref_count(weak_ec3
) == 0,
485 "EC3 reference count is 0");
487 /* Reclaim last reference held by User C. */
490 return exit_status();
This page took 0.046567 seconds and 6 git commands to generate.