Commit | Line | Data |
---|---|---|
c9b3f44b | 1 | /* |
56e18c4c | 2 | * test_trace_ir_ref.c |
c9b3f44b | 3 | * |
56e18c4c | 4 | * Trace IR Reference Count test |
c9b3f44b JG |
5 | * |
6 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | ||
22 | #include "tap/tap.h" | |
3fadfbc0 MJ |
23 | #include <babeltrace2/babeltrace.h> |
24 | #include <babeltrace2/object-internal.h> | |
25 | #include <babeltrace2/compat/stdlib-internal.h> | |
26 | #include <babeltrace2/assert-internal.h> | |
851299b9 | 27 | #include "common.h" |
c9b3f44b | 28 | |
44c440bc | 29 | #define NR_TESTS 37 |
8bbe269d | 30 | |
c9b3f44b | 31 | struct user { |
b19ff26f PP |
32 | bt_trace_class *tc; |
33 | bt_stream_class *sc; | |
34 | bt_event_class *ec; | |
35 | bt_stream *stream; | |
36 | bt_event *event; | |
c9b3f44b JG |
37 | }; |
38 | ||
3dca2276 PP |
39 | struct writer_user { |
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; | |
46 | }; | |
47 | ||
48 | const char *writer_user_names[] = { | |
6271743c PP |
49 | "writer", |
50 | "trace", | |
51 | "stream class", | |
52 | "event class", | |
53 | "stream", | |
54 | "event", | |
55 | }; | |
56 | ||
3dca2276 PP |
57 | static const size_t WRITER_USER_NR_ELEMENTS = |
58 | sizeof(struct writer_user) / sizeof(void *); | |
6271743c | 59 | |
c9b3f44b JG |
60 | /** |
61 | * Returns a structure containing the following fields: | |
62 | * - uint8_t payload_8; | |
63 | * - uint16_t payload_16; | |
64 | * - uint32_t payload_32; | |
65 | */ | |
1122a43a | 66 | static bt_field_class *create_integer_struct(bt_trace_class *trace_class) |
c9b3f44b JG |
67 | { |
68 | int ret; | |
b19ff26f PP |
69 | bt_field_class *structure = NULL; |
70 | bt_field_class *ui8 = NULL, *ui16 = NULL, *ui32 = NULL; | |
c9b3f44b | 71 | |
1122a43a | 72 | structure = bt_field_class_structure_create(trace_class); |
44c440bc | 73 | BT_ASSERT(structure); |
1122a43a | 74 | ui8 = bt_field_class_unsigned_integer_create(trace_class); |
44c440bc | 75 | BT_ASSERT(ui8); |
40f4ba76 PP |
76 | bt_field_class_integer_set_field_value_range(ui8, 8); |
77 | ret = bt_field_class_structure_append_member(structure, | |
44c440bc PP |
78 | "payload_8", ui8); |
79 | BT_ASSERT(ret == 0); | |
1122a43a | 80 | ui16 = bt_field_class_unsigned_integer_create(trace_class); |
44c440bc | 81 | BT_ASSERT(ui16); |
40f4ba76 PP |
82 | bt_field_class_integer_set_field_value_range(ui16, 16); |
83 | ret = bt_field_class_structure_append_member(structure, | |
44c440bc PP |
84 | "payload_16", ui16); |
85 | BT_ASSERT(ret == 0); | |
1122a43a | 86 | ui32 = bt_field_class_unsigned_integer_create(trace_class); |
44c440bc | 87 | BT_ASSERT(ui32); |
40f4ba76 PP |
88 | bt_field_class_integer_set_field_value_range(ui32, 32); |
89 | ret = bt_field_class_structure_append_member(structure, | |
44c440bc PP |
90 | "payload_32", ui32); |
91 | BT_ASSERT(ret == 0); | |
c5b9b441 PP |
92 | BT_FIELD_CLASS_PUT_REF_AND_RESET(ui8); |
93 | BT_FIELD_CLASS_PUT_REF_AND_RESET(ui16); | |
94 | BT_FIELD_CLASS_PUT_REF_AND_RESET(ui32); | |
c9b3f44b | 95 | return structure; |
c9b3f44b JG |
96 | } |
97 | ||
3dca2276 PP |
98 | static struct bt_ctf_field_type *create_writer_integer_struct(void) |
99 | { | |
100 | int ret; | |
101 | struct bt_ctf_field_type *structure = NULL; | |
102 | struct bt_ctf_field_type *ui8 = NULL, *ui16 = NULL, *ui32 = NULL; | |
103 | ||
104 | structure = bt_ctf_field_type_structure_create(); | |
44c440bc | 105 | BT_ASSERT(structure); |
3dca2276 | 106 | ui8 = bt_ctf_field_type_integer_create(8); |
44c440bc | 107 | BT_ASSERT(ui8); |
3dca2276 PP |
108 | ret = bt_ctf_field_type_structure_add_field(structure, ui8, |
109 | "payload_8"); | |
44c440bc | 110 | BT_ASSERT(ret == 0); |
3dca2276 | 111 | ui16 = bt_ctf_field_type_integer_create(16); |
44c440bc | 112 | BT_ASSERT(ui16); |
3dca2276 PP |
113 | ret = bt_ctf_field_type_structure_add_field(structure, ui16, |
114 | "payload_16"); | |
44c440bc | 115 | BT_ASSERT(ret == 0); |
3dca2276 | 116 | ui32 = bt_ctf_field_type_integer_create(32); |
44c440bc | 117 | BT_ASSERT(ui32); |
3dca2276 PP |
118 | ret = bt_ctf_field_type_structure_add_field(structure, ui32, |
119 | "payload_32"); | |
44c440bc | 120 | BT_ASSERT(ret == 0); |
65300d60 PP |
121 | BT_OBJECT_PUT_REF_AND_RESET(ui8); |
122 | BT_OBJECT_PUT_REF_AND_RESET(ui16); | |
123 | BT_OBJECT_PUT_REF_AND_RESET(ui32); | |
3dca2276 | 124 | return structure; |
3dca2276 PP |
125 | } |
126 | ||
c9b3f44b JG |
127 | /** |
128 | * A simple event has the following payload: | |
129 | * - uint8_t payload_8; | |
130 | * - uint16_t payload_16; | |
131 | * - uint32_t payload_32; | |
132 | */ | |
b19ff26f PP |
133 | static bt_event_class *create_simple_event( |
134 | bt_stream_class *sc, const char *name) | |
c9b3f44b JG |
135 | { |
136 | int ret; | |
b19ff26f PP |
137 | bt_event_class *event = NULL; |
138 | bt_field_class *payload = NULL; | |
c9b3f44b | 139 | |
25583cd0 | 140 | BT_ASSERT(name); |
40f4ba76 | 141 | event = bt_event_class_create(sc); |
44c440bc | 142 | BT_ASSERT(event); |
40f4ba76 | 143 | ret = bt_event_class_set_name(event, name); |
44c440bc | 144 | BT_ASSERT(ret == 0); |
1122a43a | 145 | payload = create_integer_struct(bt_stream_class_borrow_trace_class(sc)); |
44c440bc | 146 | BT_ASSERT(payload); |
40f4ba76 | 147 | ret = bt_event_class_set_payload_field_class(event, payload); |
44c440bc | 148 | BT_ASSERT(ret == 0); |
c5b9b441 | 149 | BT_FIELD_CLASS_PUT_REF_AND_RESET(payload); |
c9b3f44b | 150 | return event; |
c9b3f44b JG |
151 | } |
152 | ||
153 | /** | |
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; | |
162 | */ | |
1122a43a | 163 | static bt_event_class *create_complex_event(bt_stream_class *sc, |
44c440bc | 164 | const char *name) |
c9b3f44b JG |
165 | { |
166 | int ret; | |
b19ff26f PP |
167 | bt_event_class *event = NULL; |
168 | bt_field_class *inner = NULL, *outer = NULL; | |
1122a43a | 169 | bt_trace_class *trace_class = bt_stream_class_borrow_trace_class(sc); |
c9b3f44b | 170 | |
25583cd0 | 171 | BT_ASSERT(name); |
40f4ba76 | 172 | event = bt_event_class_create(sc); |
44c440bc | 173 | BT_ASSERT(event); |
40f4ba76 | 174 | ret = bt_event_class_set_name(event, name); |
44c440bc | 175 | BT_ASSERT(ret == 0); |
1122a43a | 176 | outer = create_integer_struct(trace_class); |
44c440bc | 177 | BT_ASSERT(outer); |
1122a43a | 178 | inner = create_integer_struct(trace_class); |
44c440bc | 179 | BT_ASSERT(inner); |
40f4ba76 | 180 | ret = bt_field_class_structure_append_member(outer, |
44c440bc PP |
181 | "payload_struct", inner); |
182 | BT_ASSERT(ret == 0); | |
40f4ba76 | 183 | ret = bt_event_class_set_payload_field_class(event, outer); |
44c440bc | 184 | BT_ASSERT(ret == 0); |
c5b9b441 PP |
185 | BT_FIELD_CLASS_PUT_REF_AND_RESET(inner); |
186 | BT_FIELD_CLASS_PUT_REF_AND_RESET(outer); | |
c9b3f44b | 187 | return event; |
c9b3f44b JG |
188 | } |
189 | ||
5cd6d0e5 | 190 | static void set_stream_class_field_classes( |
b19ff26f | 191 | bt_stream_class *stream_class) |
da1cc671 | 192 | { |
1122a43a PP |
193 | bt_trace_class *trace_class = |
194 | bt_stream_class_borrow_trace_class(stream_class); | |
b19ff26f | 195 | bt_field_class *packet_context_type; |
b19ff26f | 196 | bt_field_class *fc; |
da1cc671 PP |
197 | int ret; |
198 | ||
1122a43a | 199 | packet_context_type = bt_field_class_structure_create(trace_class); |
25583cd0 | 200 | BT_ASSERT(packet_context_type); |
1122a43a | 201 | fc = bt_field_class_unsigned_integer_create(trace_class); |
5cd6d0e5 | 202 | BT_ASSERT(fc); |
40f4ba76 PP |
203 | bt_field_class_integer_set_field_value_range(fc, 32); |
204 | ret = bt_field_class_structure_append_member(packet_context_type, | |
5cd6d0e5 | 205 | "packet_size", fc); |
25583cd0 | 206 | BT_ASSERT(ret == 0); |
c5b9b441 | 207 | bt_field_class_put_ref(fc); |
1122a43a | 208 | fc = bt_field_class_unsigned_integer_create(trace_class); |
5cd6d0e5 | 209 | BT_ASSERT(fc); |
40f4ba76 PP |
210 | bt_field_class_integer_set_field_value_range(fc, 32); |
211 | ret = bt_field_class_structure_append_member(packet_context_type, | |
5cd6d0e5 | 212 | "content_size", fc); |
25583cd0 | 213 | BT_ASSERT(ret == 0); |
c5b9b441 | 214 | bt_field_class_put_ref(fc); |
40f4ba76 | 215 | ret = bt_stream_class_set_packet_context_field_class( |
e5be10ef | 216 | stream_class, packet_context_type); |
25583cd0 | 217 | BT_ASSERT(ret == 0); |
c5b9b441 | 218 | bt_field_class_put_ref(packet_context_type); |
da1cc671 PP |
219 | } |
220 | ||
b19ff26f | 221 | static void create_sc1(bt_trace_class *trace_class) |
c9b3f44b JG |
222 | { |
223 | int ret; | |
b19ff26f PP |
224 | bt_event_class *ec1 = NULL, *ec2 = NULL; |
225 | bt_stream_class *sc1 = NULL, *ret_stream = NULL; | |
c9b3f44b | 226 | |
862ca4ed | 227 | sc1 = bt_stream_class_create(trace_class); |
44c440bc | 228 | BT_ASSERT(sc1); |
40f4ba76 | 229 | ret = bt_stream_class_set_name(sc1, "sc1"); |
44c440bc | 230 | BT_ASSERT(ret == 0); |
5cd6d0e5 | 231 | set_stream_class_field_classes(sc1); |
44c440bc PP |
232 | ec1 = create_complex_event(sc1, "ec1"); |
233 | BT_ASSERT(ec1); | |
234 | ec2 = create_simple_event(sc1, "ec2"); | |
235 | BT_ASSERT(ec2); | |
40f4ba76 | 236 | ret_stream = bt_event_class_borrow_stream_class(ec1); |
44c440bc | 237 | ok(ret_stream == sc1, "Borrow parent stream SC1 from EC1"); |
40f4ba76 | 238 | ret_stream = bt_event_class_borrow_stream_class(ec2); |
44c440bc | 239 | ok(ret_stream == sc1, "Borrow parent stream SC1 from EC2"); |
c5b9b441 PP |
240 | BT_EVENT_CLASS_PUT_REF_AND_RESET(ec1); |
241 | BT_EVENT_CLASS_PUT_REF_AND_RESET(ec2); | |
242 | BT_STREAM_CLASS_PUT_REF_AND_RESET(sc1); | |
c9b3f44b JG |
243 | } |
244 | ||
b19ff26f | 245 | static void create_sc2(bt_trace_class *trace_class) |
c9b3f44b JG |
246 | { |
247 | int ret; | |
b19ff26f PP |
248 | bt_event_class *ec3 = NULL; |
249 | bt_stream_class *sc2 = NULL, *ret_stream = NULL; | |
c9b3f44b | 250 | |
862ca4ed | 251 | sc2 = bt_stream_class_create(trace_class); |
44c440bc | 252 | BT_ASSERT(sc2); |
40f4ba76 | 253 | ret = bt_stream_class_set_name(sc2, "sc2"); |
44c440bc | 254 | BT_ASSERT(ret == 0); |
5cd6d0e5 | 255 | set_stream_class_field_classes(sc2); |
44c440bc | 256 | ec3 = create_simple_event(sc2, "ec3"); |
40f4ba76 | 257 | ret_stream = bt_event_class_borrow_stream_class(ec3); |
44c440bc | 258 | ok(ret_stream == sc2, "Borrow parent stream SC2 from EC3"); |
c5b9b441 PP |
259 | BT_EVENT_CLASS_PUT_REF_AND_RESET(ec3); |
260 | BT_STREAM_CLASS_PUT_REF_AND_RESET(sc2); | |
c9b3f44b JG |
261 | } |
262 | ||
41693723 | 263 | static bt_trace_class *create_tc1(bt_self_component_source *self_comp) |
c9b3f44b | 264 | { |
b19ff26f | 265 | bt_trace_class *tc1 = NULL; |
c9b3f44b | 266 | |
41693723 PP |
267 | tc1 = bt_trace_class_create( |
268 | bt_self_component_source_as_self_component(self_comp)); | |
44c440bc | 269 | BT_ASSERT(tc1); |
44c440bc PP |
270 | create_sc1(tc1); |
271 | create_sc2(tc1); | |
c9b3f44b | 272 | return tc1; |
c9b3f44b JG |
273 | } |
274 | ||
b19ff26f PP |
275 | static void init_weak_refs(bt_trace_class *tc, |
276 | bt_trace_class **tc1, | |
277 | bt_stream_class **sc1, | |
278 | bt_stream_class **sc2, | |
279 | bt_event_class **ec1, | |
280 | bt_event_class **ec2, | |
281 | bt_event_class **ec3) | |
c9b3f44b JG |
282 | { |
283 | *tc1 = tc; | |
862ca4ed PP |
284 | *sc1 = bt_trace_class_borrow_stream_class_by_index(tc, 0); |
285 | *sc2 = bt_trace_class_borrow_stream_class_by_index(tc, 1); | |
40f4ba76 PP |
286 | *ec1 = bt_stream_class_borrow_event_class_by_index(*sc1, 0); |
287 | *ec2 = bt_stream_class_borrow_event_class_by_index(*sc1, 1); | |
288 | *ec3 = bt_stream_class_borrow_event_class_by_index(*sc2, 0); | |
c9b3f44b JG |
289 | } |
290 | ||
41693723 | 291 | static void test_example_scenario(bt_self_component_source *self_comp) |
c9b3f44b | 292 | { |
56e18c4c PP |
293 | /* |
294 | * Weak pointers to trace IR objects are to be used very | |
295 | * carefully. This is NOT a good practice and is strongly | |
296 | * discouraged; this is only done to facilitate the validation | |
297 | * of expected reference counts without affecting them by taking | |
298 | * "real" references to the objects. | |
c9b3f44b | 299 | */ |
b19ff26f PP |
300 | bt_trace_class *tc1 = NULL, *weak_tc1 = NULL; |
301 | bt_stream_class *weak_sc1 = NULL, *weak_sc2 = NULL; | |
302 | bt_event_class *weak_ec1 = NULL, *weak_ec2 = NULL, | |
c9b3f44b JG |
303 | *weak_ec3 = NULL; |
304 | struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 }; | |
305 | ||
306 | /* The only reference which exists at this point is on TC1. */ | |
41693723 | 307 | tc1 = create_tc1(self_comp); |
27f4d205 | 308 | ok(tc1, "Initialize trace"); |
44c440bc | 309 | BT_ASSERT(tc1); |
c9b3f44b JG |
310 | init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1, |
311 | &weak_ec2, &weak_ec3); | |
3fea54f6 | 312 | ok(bt_object_get_ref_count((void *) weak_sc1) == 0, |
c9b3f44b | 313 | "Initial SC1 reference count is 0"); |
3fea54f6 | 314 | ok(bt_object_get_ref_count((void *) weak_sc2) == 0, |
c9b3f44b | 315 | "Initial SC2 reference count is 0"); |
3fea54f6 | 316 | ok(bt_object_get_ref_count((void *) weak_ec1) == 0, |
c9b3f44b | 317 | "Initial EC1 reference count is 0"); |
3fea54f6 | 318 | ok(bt_object_get_ref_count((void *) weak_ec2) == 0, |
c9b3f44b | 319 | "Initial EC2 reference count is 0"); |
3fea54f6 | 320 | ok(bt_object_get_ref_count((void *) weak_ec3) == 0, |
c9b3f44b JG |
321 | "Initial EC3 reference count is 0"); |
322 | ||
323 | /* User A has ownership of the trace. */ | |
65300d60 | 324 | BT_OBJECT_MOVE_REF(user_a.tc, tc1); |
3fea54f6 | 325 | ok(bt_object_get_ref_count((void *) user_a.tc) == 1, |
c9b3f44b JG |
326 | "TC1 reference count is 1"); |
327 | ||
328 | /* User A acquires a reference to SC2 from TC1. */ | |
862ca4ed | 329 | user_a.sc = bt_trace_class_borrow_stream_class_by_index( |
398454ed | 330 | user_a.tc, 1); |
c5b9b441 | 331 | bt_stream_class_get_ref(user_a.sc); |
c9b3f44b | 332 | ok(user_a.sc, "User A acquires SC2 from TC1"); |
3fea54f6 | 333 | ok(bt_object_get_ref_count((void *) weak_tc1) == 2, |
c9b3f44b | 334 | "TC1 reference count is 2"); |
3fea54f6 | 335 | ok(bt_object_get_ref_count((void *) weak_sc2) == 1, |
c9b3f44b JG |
336 | "SC2 reference count is 1"); |
337 | ||
338 | /* User A acquires a reference to EC3 from SC2. */ | |
40f4ba76 | 339 | user_a.ec = bt_stream_class_borrow_event_class_by_index( |
398454ed | 340 | user_a.sc, 0); |
c5b9b441 | 341 | bt_event_class_get_ref(user_a.ec); |
c9b3f44b | 342 | ok(user_a.ec, "User A acquires EC3 from SC2"); |
3fea54f6 | 343 | ok(bt_object_get_ref_count((void *) weak_tc1) == 2, |
c9b3f44b | 344 | "TC1 reference count is 2"); |
3fea54f6 | 345 | ok(bt_object_get_ref_count((void *) weak_sc2) == 2, |
c9b3f44b | 346 | "SC2 reference count is 2"); |
3fea54f6 | 347 | ok(bt_object_get_ref_count((void *) weak_ec3) == 1, |
c9b3f44b JG |
348 | "EC3 reference count is 1"); |
349 | ||
350 | /* User A releases its reference to SC2. */ | |
351 | diag("User A releases SC2"); | |
c5b9b441 | 352 | BT_STREAM_CLASS_PUT_REF_AND_RESET(user_a.sc); |
c9b3f44b JG |
353 | /* |
354 | * We keep the pointer to SC2 around to validate its reference | |
355 | * count. | |
356 | */ | |
3fea54f6 | 357 | ok(bt_object_get_ref_count((void *) weak_tc1) == 2, |
c9b3f44b | 358 | "TC1 reference count is 2"); |
3fea54f6 | 359 | ok(bt_object_get_ref_count((void *) weak_sc2) == 1, |
c9b3f44b | 360 | "SC2 reference count is 1"); |
3fea54f6 | 361 | ok(bt_object_get_ref_count((void *) weak_ec3) == 1, |
c9b3f44b JG |
362 | "EC3 reference count is 1"); |
363 | ||
364 | /* User A releases its reference to TC1. */ | |
365 | diag("User A releases TC1"); | |
c5b9b441 | 366 | BT_TRACE_CLASS_PUT_REF_AND_RESET(user_a.tc); |
c9b3f44b JG |
367 | /* |
368 | * We keep the pointer to TC1 around to validate its reference | |
369 | * count. | |
370 | */ | |
3fea54f6 | 371 | ok(bt_object_get_ref_count((void *) weak_tc1) == 1, |
c9b3f44b | 372 | "TC1 reference count is 1"); |
3fea54f6 | 373 | ok(bt_object_get_ref_count((void *) weak_sc2) == 1, |
c9b3f44b | 374 | "SC2 reference count is 1"); |
3fea54f6 | 375 | ok(bt_object_get_ref_count((void *) weak_ec3) == 1, |
c9b3f44b JG |
376 | "EC3 reference count is 1"); |
377 | ||
378 | /* User B acquires a reference to SC1. */ | |
379 | diag("User B acquires a reference to SC1"); | |
398454ed | 380 | user_b.sc = weak_sc1; |
c5b9b441 | 381 | bt_stream_class_get_ref(user_b.sc); |
3fea54f6 | 382 | ok(bt_object_get_ref_count((void *) weak_tc1) == 2, |
c9b3f44b | 383 | "TC1 reference count is 2"); |
3fea54f6 | 384 | ok(bt_object_get_ref_count((void *) weak_sc1) == 1, |
c9b3f44b JG |
385 | "SC1 reference count is 1"); |
386 | ||
387 | /* User C acquires a reference to EC1. */ | |
388 | diag("User C acquires a reference to EC1"); | |
40f4ba76 | 389 | user_c.ec = bt_stream_class_borrow_event_class_by_index( |
398454ed | 390 | user_b.sc, 0); |
c5b9b441 | 391 | bt_event_class_get_ref(user_c.ec); |
3fea54f6 | 392 | ok(bt_object_get_ref_count((void *) weak_ec1) == 1, |
c9b3f44b | 393 | "EC1 reference count is 1"); |
3fea54f6 | 394 | ok(bt_object_get_ref_count((void *) weak_sc1) == 2, |
c9b3f44b JG |
395 | "SC1 reference count is 2"); |
396 | ||
397 | /* User A releases its reference on EC3. */ | |
398 | diag("User A releases its reference on EC3"); | |
c5b9b441 | 399 | BT_EVENT_CLASS_PUT_REF_AND_RESET(user_a.ec); |
3fea54f6 | 400 | ok(bt_object_get_ref_count((void *) weak_ec3) == 0, |
c9b3f44b | 401 | "EC3 reference count is 1"); |
3fea54f6 | 402 | ok(bt_object_get_ref_count((void *) weak_sc2) == 0, |
c9b3f44b | 403 | "SC2 reference count is 0"); |
3fea54f6 | 404 | ok(bt_object_get_ref_count((void *) weak_tc1) == 1, |
c9b3f44b JG |
405 | "TC1 reference count is 1"); |
406 | ||
407 | /* User B releases its reference on SC1. */ | |
408 | diag("User B releases its reference on SC1"); | |
c5b9b441 | 409 | BT_STREAM_CLASS_PUT_REF_AND_RESET(user_b.sc); |
3fea54f6 | 410 | ok(bt_object_get_ref_count((void *) weak_sc1) == 1, |
c9b3f44b JG |
411 | "SC1 reference count is 1"); |
412 | ||
413 | /* | |
414 | * User C is the sole owner of an object and is keeping the whole | |
415 | * trace hierarchy "alive" by holding a reference to EC1. | |
416 | */ | |
3fea54f6 | 417 | ok(bt_object_get_ref_count((void *) weak_tc1) == 1, |
c9b3f44b | 418 | "TC1 reference count is 1"); |
3fea54f6 | 419 | ok(bt_object_get_ref_count((void *) weak_sc1) == 1, |
c9b3f44b | 420 | "SC1 reference count is 1"); |
3fea54f6 | 421 | ok(bt_object_get_ref_count((void *) weak_sc2) == 0, |
c9b3f44b | 422 | "SC2 reference count is 0"); |
3fea54f6 | 423 | ok(bt_object_get_ref_count((void *) weak_ec1) == 1, |
c9b3f44b | 424 | "EC1 reference count is 1"); |
3fea54f6 | 425 | ok(bt_object_get_ref_count((void *) weak_ec2) == 0, |
c9b3f44b | 426 | "EC2 reference count is 0"); |
3fea54f6 | 427 | ok(bt_object_get_ref_count((void *) weak_ec3) == 0, |
c9b3f44b JG |
428 | "EC3 reference count is 0"); |
429 | ||
430 | /* Reclaim last reference held by User C. */ | |
c5b9b441 | 431 | BT_EVENT_CLASS_PUT_REF_AND_RESET(user_c.ec); |
6271743c PP |
432 | } |
433 | ||
41693723 PP |
434 | static |
435 | bt_self_component_status src_init( | |
436 | bt_self_component_source *self_comp, | |
437 | const bt_value *params, void *init_method_data) | |
438 | { | |
439 | test_example_scenario(self_comp); | |
440 | return BT_SELF_COMPONENT_STATUS_OK; | |
441 | } | |
442 | ||
443 | static | |
444 | bt_self_message_iterator_status src_iter_next( | |
445 | bt_self_message_iterator *self_iterator, | |
446 | bt_message_array_const msgs, uint64_t capacity, | |
447 | uint64_t *count) | |
448 | { | |
449 | return BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
450 | } | |
451 | ||
452 | static void test_example_scenario_in_graph(void) | |
453 | { | |
454 | bt_component_class_source *comp_cls; | |
455 | bt_graph *graph; | |
456 | int ret; | |
457 | ||
458 | comp_cls = bt_component_class_source_create("src", src_iter_next); | |
459 | BT_ASSERT(comp_cls); | |
460 | ret = bt_component_class_source_set_init_method(comp_cls, src_init); | |
461 | BT_ASSERT(ret == 0); | |
462 | graph = bt_graph_create(); | |
463 | ret = bt_graph_add_source_component(graph, comp_cls, "src-comp", | |
464 | NULL, NULL); | |
465 | BT_ASSERT(ret == 0); | |
466 | bt_graph_put_ref(graph); | |
467 | bt_component_class_source_put_ref(comp_cls); | |
468 | } | |
469 | ||
3dca2276 | 470 | static void create_writer_user_full(struct writer_user *user) |
6271743c | 471 | { |
32bd47d1 | 472 | gchar *trace_path; |
3dca2276 PP |
473 | struct bt_ctf_field_type *ft; |
474 | struct bt_ctf_field *field; | |
6271743c PP |
475 | struct bt_ctf_clock *clock; |
476 | int ret; | |
477 | ||
32bd47d1 | 478 | trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL); |
6271743c PP |
479 | if (!bt_mkdtemp(trace_path)) { |
480 | perror("# perror"); | |
481 | } | |
482 | ||
483 | user->writer = bt_ctf_writer_create(trace_path); | |
25583cd0 | 484 | BT_ASSERT(user->writer); |
dc3fffef | 485 | ret = bt_ctf_writer_set_byte_order(user->writer, |
3dca2276 | 486 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN); |
25583cd0 | 487 | BT_ASSERT(ret == 0); |
6271743c | 488 | user->tc = bt_ctf_writer_get_trace(user->writer); |
25583cd0 | 489 | BT_ASSERT(user->tc); |
3dca2276 | 490 | user->sc = bt_ctf_stream_class_create("sc"); |
25583cd0 | 491 | BT_ASSERT(user->sc); |
6271743c | 492 | clock = bt_ctf_clock_create("the_clock"); |
25583cd0 | 493 | BT_ASSERT(clock); |
81582b6b | 494 | ret = bt_ctf_writer_add_clock(user->writer, clock); |
25583cd0 | 495 | BT_ASSERT(!ret); |
3dca2276 | 496 | ret = bt_ctf_stream_class_set_clock(user->sc, clock); |
25583cd0 | 497 | BT_ASSERT(!ret); |
65300d60 | 498 | BT_OBJECT_PUT_REF_AND_RESET(clock); |
6271743c | 499 | user->stream = bt_ctf_writer_create_stream(user->writer, user->sc); |
25583cd0 | 500 | BT_ASSERT(user->stream); |
3dca2276 | 501 | user->ec = bt_ctf_event_class_create("ec"); |
25583cd0 | 502 | BT_ASSERT(user->ec); |
3dca2276 | 503 | ft = create_writer_integer_struct(); |
25583cd0 | 504 | BT_ASSERT(ft); |
3dca2276 | 505 | ret = bt_ctf_event_class_set_payload_field_type(user->ec, ft); |
65300d60 | 506 | BT_OBJECT_PUT_REF_AND_RESET(ft); |
25583cd0 | 507 | BT_ASSERT(!ret); |
3dca2276 | 508 | ret = bt_ctf_stream_class_add_event_class(user->sc, user->ec); |
25583cd0 | 509 | BT_ASSERT(!ret); |
3dca2276 | 510 | user->event = bt_ctf_event_create(user->ec); |
25583cd0 | 511 | BT_ASSERT(user->event); |
3dca2276 | 512 | field = bt_ctf_event_get_payload(user->event, "payload_8"); |
25583cd0 | 513 | BT_ASSERT(field); |
3dca2276 | 514 | ret = bt_ctf_field_integer_unsigned_set_value(field, 10); |
25583cd0 | 515 | BT_ASSERT(!ret); |
65300d60 | 516 | BT_OBJECT_PUT_REF_AND_RESET(field); |
3dca2276 | 517 | field = bt_ctf_event_get_payload(user->event, "payload_16"); |
25583cd0 | 518 | BT_ASSERT(field); |
3dca2276 | 519 | ret = bt_ctf_field_integer_unsigned_set_value(field, 20); |
25583cd0 | 520 | BT_ASSERT(!ret); |
65300d60 | 521 | BT_OBJECT_PUT_REF_AND_RESET(field); |
3dca2276 | 522 | field = bt_ctf_event_get_payload(user->event, "payload_32"); |
25583cd0 | 523 | BT_ASSERT(field); |
3dca2276 | 524 | ret = bt_ctf_field_integer_unsigned_set_value(field, 30); |
25583cd0 | 525 | BT_ASSERT(!ret); |
65300d60 | 526 | BT_OBJECT_PUT_REF_AND_RESET(field); |
3dca2276 | 527 | ret = bt_ctf_stream_append_event(user->stream, user->event); |
25583cd0 | 528 | BT_ASSERT(!ret); |
851299b9 | 529 | recursive_rmdir(trace_path); |
32bd47d1 | 530 | g_free(trace_path); |
6271743c PP |
531 | } |
532 | ||
533 | static void test_put_order_swap(size_t *array, size_t a, size_t b) | |
534 | { | |
535 | size_t temp = array[a]; | |
536 | ||
537 | array[a] = array[b]; | |
538 | array[b] = temp; | |
539 | } | |
540 | ||
541 | static void test_put_order_put_objects(size_t *array, size_t size) | |
542 | { | |
543 | size_t i; | |
3dca2276 | 544 | struct writer_user user = { 0 }; |
6f2097e2 | 545 | void **objects = (void *) &user; |
6271743c | 546 | |
3dca2276 | 547 | create_writer_user_full(&user); |
6271743c PP |
548 | printf("# "); |
549 | ||
550 | for (i = 0; i < size; ++i) { | |
6f2097e2 | 551 | void *obj = objects[array[i]]; |
6271743c | 552 | |
3dca2276 | 553 | printf("%s", writer_user_names[array[i]]); |
65300d60 | 554 | BT_OBJECT_PUT_REF_AND_RESET(obj); |
6271743c PP |
555 | |
556 | if (i < size - 1) { | |
557 | printf(" -> "); | |
558 | } | |
559 | } | |
560 | ||
561 | puts(""); | |
562 | } | |
563 | ||
564 | static void test_put_order_permute(size_t *array, int k, size_t size) | |
565 | { | |
566 | if (k == 0) { | |
567 | test_put_order_put_objects(array, size); | |
568 | } else { | |
569 | int i; | |
570 | ||
571 | for (i = k - 1; i >= 0; i--) { | |
572 | size_t next_k = k - 1; | |
573 | ||
574 | test_put_order_swap(array, i, next_k); | |
575 | test_put_order_permute(array, next_k, size); | |
576 | test_put_order_swap(array, i, next_k); | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
581 | static void test_put_order(void) | |
582 | { | |
583 | size_t i; | |
3dca2276 | 584 | size_t array[WRITER_USER_NR_ELEMENTS]; |
6271743c PP |
585 | |
586 | /* Initialize array of indexes */ | |
3dca2276 | 587 | for (i = 0; i < WRITER_USER_NR_ELEMENTS; ++i) { |
6271743c PP |
588 | array[i] = i; |
589 | } | |
590 | ||
3dca2276 PP |
591 | test_put_order_permute(array, WRITER_USER_NR_ELEMENTS, |
592 | WRITER_USER_NR_ELEMENTS); | |
6271743c PP |
593 | } |
594 | ||
595 | /** | |
596 | * The objective of this test is to implement and expand upon the scenario | |
597 | * described in the reference counting documentation and ensure that any node of | |
598 | * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all | |
599 | * other "alive" and reachable. | |
600 | * | |
601 | * External tools (e.g. valgrind) should be used to confirm that this | |
602 | * known-good test does not leak memory. | |
603 | */ | |
604 | int main(int argc, char **argv) | |
605 | { | |
27f4d205 | 606 | /* Initialize tap harness before any tests */ |
6271743c PP |
607 | plan_tests(NR_TESTS); |
608 | ||
41693723 | 609 | test_example_scenario_in_graph(); |
6271743c PP |
610 | test_put_order(); |
611 | ||
c9b3f44b JG |
612 | return exit_status(); |
613 | } |