Commit | Line | Data |
---|---|---|
c9b3f44b JG |
1 | /* |
2 | * test_ctf_ir_ref.c | |
3 | * | |
4 | * CTF IR Reference Count test | |
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" | |
6271743c PP |
23 | #include <babeltrace/ctf-writer/writer.h> |
24 | #include <babeltrace/ctf-writer/stream.h> | |
25 | #include <babeltrace/ctf-writer/clock.h> | |
c9b3f44b | 26 | #include <babeltrace/ctf-ir/trace.h> |
6271743c | 27 | #include <babeltrace/ctf-writer/stream-class.h> |
c9b3f44b | 28 | #include <babeltrace/ctf-ir/stream.h> |
6271743c | 29 | #include <babeltrace/ctf-ir/fields.h> |
c9b3f44b | 30 | #include <babeltrace/ctf-ir/event.h> |
272df73e | 31 | #include <babeltrace/ctf-ir/event-class.h> |
c9b3f44b | 32 | #include <babeltrace/object-internal.h> |
3d9990ac | 33 | #include <babeltrace/compat/stdlib-internal.h> |
c9b3f44b | 34 | #include <assert.h> |
851299b9 | 35 | #include "common.h" |
c9b3f44b | 36 | |
8bbe269d MJ |
37 | #define NR_TESTS 41 |
38 | ||
c9b3f44b | 39 | struct user { |
6271743c | 40 | struct bt_ctf_writer *writer; |
50842bdc PP |
41 | struct bt_trace *tc; |
42 | struct bt_stream_class *sc; | |
43 | struct bt_event_class *ec; | |
44 | struct bt_stream *stream; | |
45 | struct bt_event *event; | |
c9b3f44b JG |
46 | }; |
47 | ||
6271743c PP |
48 | const char *user_names[] = { |
49 | "writer", | |
50 | "trace", | |
51 | "stream class", | |
52 | "event class", | |
53 | "stream", | |
54 | "event", | |
55 | }; | |
56 | ||
57 | static const size_t USER_NR_ELEMENTS = sizeof(struct user) / sizeof(void *); | |
58 | ||
c9b3f44b JG |
59 | /** |
60 | * Returns a structure containing the following fields: | |
61 | * - uint8_t payload_8; | |
62 | * - uint16_t payload_16; | |
63 | * - uint32_t payload_32; | |
64 | */ | |
50842bdc | 65 | static struct bt_field_type *create_integer_struct(void) |
c9b3f44b JG |
66 | { |
67 | int ret; | |
50842bdc PP |
68 | struct bt_field_type *structure = NULL; |
69 | struct bt_field_type *ui8 = NULL, *ui16 = NULL, *ui32 = NULL; | |
c9b3f44b | 70 | |
50842bdc | 71 | structure = bt_field_type_structure_create(); |
c9b3f44b JG |
72 | if (!structure) { |
73 | goto error; | |
74 | } | |
75 | ||
50842bdc | 76 | ui8 = bt_field_type_integer_create(8); |
c9b3f44b JG |
77 | if (!ui8) { |
78 | diag("Failed to create uint8_t type"); | |
79 | goto error; | |
80 | } | |
50842bdc | 81 | ret = bt_field_type_structure_add_field(structure, ui8, |
c9b3f44b JG |
82 | "payload_8"); |
83 | if (ret) { | |
84 | diag("Failed to add uint8_t to structure"); | |
85 | goto error; | |
86 | } | |
50842bdc | 87 | ui16 = bt_field_type_integer_create(16); |
c9b3f44b JG |
88 | if (!ui16) { |
89 | diag("Failed to create uint16_t type"); | |
90 | goto error; | |
91 | } | |
50842bdc | 92 | ret = bt_field_type_structure_add_field(structure, ui16, |
c9b3f44b JG |
93 | "payload_16"); |
94 | if (ret) { | |
95 | diag("Failed to add uint16_t to structure"); | |
96 | goto error; | |
97 | } | |
50842bdc | 98 | ui32 = bt_field_type_integer_create(32); |
c9b3f44b JG |
99 | if (!ui32) { |
100 | diag("Failed to create uint32_t type"); | |
101 | goto error; | |
102 | } | |
50842bdc | 103 | ret = bt_field_type_structure_add_field(structure, ui32, |
c9b3f44b JG |
104 | "payload_32"); |
105 | if (ret) { | |
106 | diag("Failed to add uint32_t to structure"); | |
107 | goto error; | |
108 | } | |
109 | end: | |
110 | BT_PUT(ui8); | |
111 | BT_PUT(ui16); | |
112 | BT_PUT(ui32); | |
113 | return structure; | |
114 | error: | |
115 | BT_PUT(structure); | |
116 | goto end; | |
117 | } | |
118 | ||
119 | /** | |
120 | * A simple event has the following payload: | |
121 | * - uint8_t payload_8; | |
122 | * - uint16_t payload_16; | |
123 | * - uint32_t payload_32; | |
124 | */ | |
50842bdc | 125 | static struct bt_event_class *create_simple_event(const char *name) |
c9b3f44b JG |
126 | { |
127 | int ret; | |
50842bdc PP |
128 | struct bt_event_class *event = NULL; |
129 | struct bt_field_type *payload = NULL; | |
c9b3f44b | 130 | |
27f4d205 | 131 | assert(name); |
50842bdc | 132 | event = bt_event_class_create(name); |
c9b3f44b JG |
133 | if (!event) { |
134 | diag("Failed to create simple event"); | |
135 | goto error; | |
136 | } | |
137 | ||
138 | payload = create_integer_struct(); | |
139 | if (!payload) { | |
140 | diag("Failed to initialize integer structure"); | |
141 | goto error; | |
142 | } | |
143 | ||
50842bdc | 144 | ret = bt_event_class_set_payload_type(event, payload); |
c9b3f44b JG |
145 | if (ret) { |
146 | diag("Failed to set simple event payload"); | |
147 | goto error; | |
148 | } | |
149 | end: | |
150 | BT_PUT(payload); | |
151 | return event; | |
152 | error: | |
153 | BT_PUT(event); | |
154 | goto end;; | |
155 | } | |
156 | ||
157 | /** | |
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; | |
166 | */ | |
50842bdc | 167 | static struct bt_event_class *create_complex_event(const char *name) |
c9b3f44b JG |
168 | { |
169 | int ret; | |
50842bdc PP |
170 | struct bt_event_class *event = NULL; |
171 | struct bt_field_type *inner = NULL, *outer = NULL; | |
c9b3f44b | 172 | |
27f4d205 | 173 | assert(name); |
50842bdc | 174 | event = bt_event_class_create(name); |
c9b3f44b JG |
175 | if (!event) { |
176 | diag("Failed to create complex event"); | |
177 | goto error; | |
178 | } | |
179 | ||
27f4d205 | 180 | outer = create_integer_struct(); |
c9b3f44b JG |
181 | if (!outer) { |
182 | diag("Failed to initialize integer structure"); | |
183 | goto error; | |
184 | } | |
185 | ||
27f4d205 | 186 | inner = create_integer_struct(); |
c9b3f44b JG |
187 | if (!inner) { |
188 | diag("Failed to initialize integer structure"); | |
189 | goto error; | |
190 | } | |
191 | ||
50842bdc | 192 | ret = bt_field_type_structure_add_field(outer, inner, |
27f4d205 | 193 | "payload_struct"); |
c9b3f44b JG |
194 | if (ret) { |
195 | diag("Failed to add inner structure to outer structure"); | |
196 | goto error; | |
197 | } | |
198 | ||
50842bdc | 199 | ret = bt_event_class_set_payload_type(event, outer); |
c9b3f44b JG |
200 | if (ret) { |
201 | diag("Failed to set complex event payload"); | |
202 | goto error; | |
203 | } | |
204 | end: | |
205 | BT_PUT(inner); | |
206 | BT_PUT(outer); | |
207 | return event; | |
208 | error: | |
209 | BT_PUT(event); | |
210 | goto end;; | |
211 | } | |
212 | ||
da1cc671 | 213 | static void set_stream_class_field_types( |
50842bdc | 214 | struct bt_stream_class *stream_class) |
da1cc671 | 215 | { |
50842bdc PP |
216 | struct bt_field_type *packet_context_type; |
217 | struct bt_field_type *event_header_type; | |
218 | struct bt_field_type *ft; | |
da1cc671 PP |
219 | int ret; |
220 | ||
50842bdc | 221 | packet_context_type = bt_field_type_structure_create(); |
da1cc671 | 222 | assert(packet_context_type); |
50842bdc | 223 | ft = bt_field_type_integer_create(32); |
da1cc671 | 224 | assert(ft); |
50842bdc | 225 | ret = bt_field_type_structure_add_field(packet_context_type, |
da1cc671 PP |
226 | ft, "packet_size"); |
227 | assert(ret == 0); | |
228 | bt_put(ft); | |
50842bdc | 229 | ft = bt_field_type_integer_create(32); |
da1cc671 | 230 | assert(ft); |
50842bdc | 231 | ret = bt_field_type_structure_add_field(packet_context_type, |
da1cc671 PP |
232 | ft, "content_size"); |
233 | assert(ret == 0); | |
234 | bt_put(ft); | |
235 | ||
50842bdc | 236 | event_header_type = bt_field_type_structure_create(); |
da1cc671 | 237 | assert(event_header_type); |
50842bdc | 238 | ft = bt_field_type_integer_create(32); |
da1cc671 | 239 | assert(ft); |
50842bdc | 240 | ret = bt_field_type_structure_add_field(event_header_type, |
da1cc671 PP |
241 | ft, "id"); |
242 | assert(ret == 0); | |
243 | bt_put(ft); | |
244 | ||
50842bdc | 245 | ret = bt_stream_class_set_packet_context_type(stream_class, |
da1cc671 PP |
246 | packet_context_type); |
247 | assert(ret == 0); | |
50842bdc | 248 | ret = bt_stream_class_set_event_header_type(stream_class, |
da1cc671 PP |
249 | event_header_type); |
250 | assert(ret == 0); | |
251 | ||
252 | bt_put(packet_context_type); | |
253 | bt_put(event_header_type); | |
254 | } | |
255 | ||
50842bdc | 256 | static struct bt_stream_class *create_sc1(void) |
c9b3f44b JG |
257 | { |
258 | int ret; | |
50842bdc PP |
259 | struct bt_event_class *ec1 = NULL, *ec2 = NULL; |
260 | struct bt_stream_class *sc1 = NULL, *ret_stream = NULL; | |
c9b3f44b | 261 | |
50842bdc | 262 | sc1 = bt_stream_class_create_empty("sc1"); |
c9b3f44b JG |
263 | if (!sc1) { |
264 | diag("Failed to create Stream Class"); | |
265 | goto error; | |
266 | } | |
267 | ||
da1cc671 | 268 | set_stream_class_field_types(sc1); |
c9b3f44b JG |
269 | ec1 = create_complex_event("ec1"); |
270 | if (!ec1) { | |
271 | diag("Failed to create complex event EC1"); | |
272 | goto error; | |
273 | } | |
50842bdc | 274 | ret = bt_stream_class_add_event_class(sc1, ec1); |
c9b3f44b JG |
275 | if (ret) { |
276 | diag("Failed to add EC1 to SC1"); | |
277 | goto error; | |
278 | } | |
279 | ||
280 | ec2 = create_simple_event("ec2"); | |
281 | if (!ec2) { | |
282 | diag("Failed to create simple event EC2"); | |
283 | goto error; | |
284 | } | |
50842bdc | 285 | ret = bt_stream_class_add_event_class(sc1, ec2); |
c9b3f44b JG |
286 | if (ret) { |
287 | diag("Failed to add EC1 to SC1"); | |
288 | goto error; | |
289 | } | |
290 | ||
50842bdc | 291 | ret_stream = bt_event_class_get_stream_class(ec1); |
c9b3f44b JG |
292 | ok(ret_stream == sc1, "Get parent stream SC1 from EC1"); |
293 | BT_PUT(ret_stream); | |
294 | ||
50842bdc | 295 | ret_stream = bt_event_class_get_stream_class(ec2); |
c9b3f44b JG |
296 | ok(ret_stream == sc1, "Get parent stream SC1 from EC2"); |
297 | end: | |
298 | BT_PUT(ret_stream); | |
299 | BT_PUT(ec1); | |
300 | BT_PUT(ec2); | |
301 | return sc1; | |
302 | error: | |
303 | BT_PUT(sc1); | |
27f4d205 | 304 | goto end; |
c9b3f44b JG |
305 | } |
306 | ||
50842bdc | 307 | static struct bt_stream_class *create_sc2(void) |
c9b3f44b JG |
308 | { |
309 | int ret; | |
50842bdc PP |
310 | struct bt_event_class *ec3 = NULL; |
311 | struct bt_stream_class *sc2 = NULL, *ret_stream = NULL; | |
c9b3f44b | 312 | |
50842bdc | 313 | sc2 = bt_stream_class_create_empty("sc2"); |
c9b3f44b JG |
314 | if (!sc2) { |
315 | diag("Failed to create Stream Class"); | |
316 | goto error; | |
317 | } | |
318 | ||
da1cc671 | 319 | set_stream_class_field_types(sc2); |
c9b3f44b JG |
320 | ec3 = create_simple_event("ec3"); |
321 | if (!ec3) { | |
322 | diag("Failed to create simple event EC3"); | |
323 | goto error; | |
324 | } | |
50842bdc | 325 | ret = bt_stream_class_add_event_class(sc2, ec3); |
c9b3f44b JG |
326 | if (ret) { |
327 | diag("Failed to add EC3 to SC2"); | |
328 | goto error; | |
329 | } | |
330 | ||
50842bdc | 331 | ret_stream = bt_event_class_get_stream_class(ec3); |
c9b3f44b JG |
332 | ok(ret_stream == sc2, "Get parent stream SC2 from EC3"); |
333 | end: | |
334 | BT_PUT(ret_stream); | |
335 | BT_PUT(ec3); | |
336 | return sc2; | |
337 | error: | |
338 | BT_PUT(sc2); | |
27f4d205 | 339 | goto end; |
c9b3f44b JG |
340 | } |
341 | ||
50842bdc | 342 | static void set_trace_packet_header(struct bt_trace *trace) |
da1cc671 | 343 | { |
50842bdc PP |
344 | struct bt_field_type *packet_header_type; |
345 | struct bt_field_type *ft; | |
da1cc671 PP |
346 | int ret; |
347 | ||
50842bdc | 348 | packet_header_type = bt_field_type_structure_create(); |
da1cc671 | 349 | assert(packet_header_type); |
50842bdc | 350 | ft = bt_field_type_integer_create(32); |
da1cc671 | 351 | assert(ft); |
50842bdc | 352 | ret = bt_field_type_structure_add_field(packet_header_type, |
da1cc671 PP |
353 | ft, "stream_id"); |
354 | assert(ret == 0); | |
355 | bt_put(ft); | |
356 | ||
50842bdc | 357 | ret = bt_trace_set_packet_header_type(trace, |
da1cc671 PP |
358 | packet_header_type); |
359 | assert(ret == 0); | |
360 | ||
361 | bt_put(packet_header_type); | |
362 | } | |
363 | ||
50842bdc | 364 | static struct bt_trace *create_tc1(void) |
c9b3f44b JG |
365 | { |
366 | int ret; | |
50842bdc PP |
367 | struct bt_trace *tc1 = NULL; |
368 | struct bt_stream_class *sc1 = NULL, *sc2 = NULL; | |
c9b3f44b | 369 | |
50842bdc | 370 | tc1 = bt_trace_create(); |
c9b3f44b | 371 | if (!tc1) { |
50842bdc | 372 | diag("bt_trace_create returned NULL"); |
c9b3f44b JG |
373 | goto error; |
374 | } | |
375 | ||
da1cc671 | 376 | set_trace_packet_header(tc1); |
c9b3f44b JG |
377 | sc1 = create_sc1(); |
378 | ok(sc1, "Create SC1"); | |
379 | if (!sc1) { | |
380 | goto error; | |
381 | } | |
50842bdc | 382 | ret = bt_trace_add_stream_class(tc1, sc1); |
c9b3f44b JG |
383 | ok(!ret, "Add SC1 to TC1"); |
384 | if (ret) { | |
385 | goto error; | |
386 | } | |
387 | ||
388 | sc2 = create_sc2(); | |
389 | ok(sc2, "Create SC2"); | |
390 | if (!sc2) { | |
391 | goto error; | |
392 | } | |
50842bdc | 393 | ret = bt_trace_add_stream_class(tc1, sc2); |
c9b3f44b JG |
394 | ok(!ret, "Add SC2 to TC1"); |
395 | if (ret) { | |
396 | goto error; | |
397 | } | |
398 | end: | |
399 | BT_PUT(sc1); | |
400 | BT_PUT(sc2); | |
401 | return tc1; | |
402 | error: | |
403 | BT_PUT(tc1); | |
27f4d205 | 404 | goto end; |
c9b3f44b JG |
405 | } |
406 | ||
50842bdc PP |
407 | static void init_weak_refs(struct bt_trace *tc, |
408 | struct bt_trace **tc1, | |
409 | struct bt_stream_class **sc1, | |
410 | struct bt_stream_class **sc2, | |
411 | struct bt_event_class **ec1, | |
412 | struct bt_event_class **ec2, | |
413 | struct bt_event_class **ec3) | |
c9b3f44b JG |
414 | { |
415 | *tc1 = tc; | |
50842bdc PP |
416 | *sc1 = bt_trace_get_stream_class_by_index(tc, 0); |
417 | *sc2 = bt_trace_get_stream_class_by_index(tc, 1); | |
418 | *ec1 = bt_stream_class_get_event_class_by_index(*sc1, 0); | |
419 | *ec2 = bt_stream_class_get_event_class_by_index(*sc1, 1); | |
420 | *ec3 = bt_stream_class_get_event_class_by_index(*sc2, 0); | |
c9b3f44b JG |
421 | bt_put(*sc1); |
422 | bt_put(*sc2); | |
423 | bt_put(*ec1); | |
424 | bt_put(*ec2); | |
425 | bt_put(*ec3); | |
426 | } | |
427 | ||
6271743c | 428 | static void test_example_scenario(void) |
c9b3f44b JG |
429 | { |
430 | /** | |
431 | * Weak pointers to CTF-IR objects are to be used very carefully. | |
432 | * This is NOT a good practice and is strongly discouraged; this | |
433 | * is only done to facilitate the validation of expected reference | |
434 | * counts without affecting them by taking "real" references to the | |
435 | * objects. | |
436 | */ | |
50842bdc PP |
437 | struct bt_trace *tc1 = NULL, *weak_tc1 = NULL; |
438 | struct bt_stream_class *weak_sc1 = NULL, *weak_sc2 = NULL; | |
439 | struct bt_event_class *weak_ec1 = NULL, *weak_ec2 = NULL, | |
c9b3f44b JG |
440 | *weak_ec3 = NULL; |
441 | struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 }; | |
442 | ||
443 | /* The only reference which exists at this point is on TC1. */ | |
444 | tc1 = create_tc1(); | |
27f4d205 | 445 | ok(tc1, "Initialize trace"); |
c9b3f44b | 446 | if (!tc1) { |
6271743c | 447 | return; |
c9b3f44b JG |
448 | } |
449 | ||
450 | init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1, | |
451 | &weak_ec2, &weak_ec3); | |
c9b3f44b JG |
452 | |
453 | ok(bt_object_get_ref_count(weak_sc1) == 0, | |
454 | "Initial SC1 reference count is 0"); | |
455 | ok(bt_object_get_ref_count(weak_sc2) == 0, | |
456 | "Initial SC2 reference count is 0"); | |
457 | ok(bt_object_get_ref_count(weak_ec1) == 0, | |
458 | "Initial EC1 reference count is 0"); | |
459 | ok(bt_object_get_ref_count(weak_ec2) == 0, | |
460 | "Initial EC2 reference count is 0"); | |
461 | ok(bt_object_get_ref_count(weak_ec3) == 0, | |
462 | "Initial EC3 reference count is 0"); | |
463 | ||
464 | /* User A has ownership of the trace. */ | |
465 | BT_MOVE(user_a.tc, tc1); | |
466 | ok(bt_object_get_ref_count(user_a.tc) == 1, | |
467 | "TC1 reference count is 1"); | |
468 | ||
469 | /* User A acquires a reference to SC2 from TC1. */ | |
50842bdc | 470 | user_a.sc = bt_trace_get_stream_class_by_index(user_a.tc, 1); |
c9b3f44b JG |
471 | ok(user_a.sc, "User A acquires SC2 from TC1"); |
472 | ok(bt_object_get_ref_count(weak_tc1) == 2, | |
473 | "TC1 reference count is 2"); | |
474 | ok(bt_object_get_ref_count(weak_sc2) == 1, | |
475 | "SC2 reference count is 1"); | |
476 | ||
477 | /* User A acquires a reference to EC3 from SC2. */ | |
50842bdc | 478 | user_a.ec = bt_stream_class_get_event_class_by_index(user_a.sc, 0); |
c9b3f44b JG |
479 | ok(user_a.ec, "User A acquires EC3 from SC2"); |
480 | ok(bt_object_get_ref_count(weak_tc1) == 2, | |
481 | "TC1 reference count is 2"); | |
482 | ok(bt_object_get_ref_count(weak_sc2) == 2, | |
483 | "SC2 reference count is 2"); | |
484 | ok(bt_object_get_ref_count(weak_ec3) == 1, | |
485 | "EC3 reference count is 1"); | |
486 | ||
487 | /* User A releases its reference to SC2. */ | |
488 | diag("User A releases SC2"); | |
489 | BT_PUT(user_a.sc); | |
490 | /* | |
491 | * We keep the pointer to SC2 around to validate its reference | |
492 | * count. | |
493 | */ | |
494 | ok(bt_object_get_ref_count(weak_tc1) == 2, | |
495 | "TC1 reference count is 2"); | |
496 | ok(bt_object_get_ref_count(weak_sc2) == 1, | |
497 | "SC2 reference count is 1"); | |
498 | ok(bt_object_get_ref_count(weak_ec3) == 1, | |
499 | "EC3 reference count is 1"); | |
500 | ||
501 | /* User A releases its reference to TC1. */ | |
502 | diag("User A releases TC1"); | |
27f4d205 | 503 | BT_PUT(user_a.tc); |
c9b3f44b JG |
504 | /* |
505 | * We keep the pointer to TC1 around to validate its reference | |
506 | * count. | |
507 | */ | |
508 | ok(bt_object_get_ref_count(weak_tc1) == 1, | |
509 | "TC1 reference count is 1"); | |
510 | ok(bt_object_get_ref_count(weak_sc2) == 1, | |
511 | "SC2 reference count is 1"); | |
512 | ok(bt_object_get_ref_count(weak_ec3) == 1, | |
513 | "EC3 reference count is 1"); | |
514 | ||
515 | /* User B acquires a reference to SC1. */ | |
516 | diag("User B acquires a reference to SC1"); | |
517 | user_b.sc = bt_get(weak_sc1); | |
518 | ok(bt_object_get_ref_count(weak_tc1) == 2, | |
519 | "TC1 reference count is 2"); | |
520 | ok(bt_object_get_ref_count(weak_sc1) == 1, | |
521 | "SC1 reference count is 1"); | |
522 | ||
523 | /* User C acquires a reference to EC1. */ | |
524 | diag("User C acquires a reference to EC1"); | |
50842bdc | 525 | user_c.ec = bt_stream_class_get_event_class_by_index(user_b.sc, 0); |
c9b3f44b JG |
526 | ok(bt_object_get_ref_count(weak_ec1) == 1, |
527 | "EC1 reference count is 1"); | |
528 | ok(bt_object_get_ref_count(weak_sc1) == 2, | |
529 | "SC1 reference count is 2"); | |
530 | ||
531 | /* User A releases its reference on EC3. */ | |
532 | diag("User A releases its reference on EC3"); | |
533 | BT_PUT(user_a.ec); | |
534 | ok(bt_object_get_ref_count(weak_ec3) == 0, | |
535 | "EC3 reference count is 1"); | |
536 | ok(bt_object_get_ref_count(weak_sc2) == 0, | |
537 | "SC2 reference count is 0"); | |
538 | ok(bt_object_get_ref_count(weak_tc1) == 1, | |
539 | "TC1 reference count is 1"); | |
540 | ||
541 | /* User B releases its reference on SC1. */ | |
542 | diag("User B releases its reference on SC1"); | |
543 | BT_PUT(user_b.sc); | |
544 | ok(bt_object_get_ref_count(weak_sc1) == 1, | |
545 | "SC1 reference count is 1"); | |
546 | ||
547 | /* | |
548 | * User C is the sole owner of an object and is keeping the whole | |
549 | * trace hierarchy "alive" by holding a reference to EC1. | |
550 | */ | |
551 | ok(bt_object_get_ref_count(weak_tc1) == 1, | |
552 | "TC1 reference count is 1"); | |
553 | ok(bt_object_get_ref_count(weak_sc1) == 1, | |
554 | "SC1 reference count is 1"); | |
555 | ok(bt_object_get_ref_count(weak_sc2) == 0, | |
556 | "SC2 reference count is 0"); | |
557 | ok(bt_object_get_ref_count(weak_ec1) == 1, | |
558 | "EC1 reference count is 1"); | |
559 | ok(bt_object_get_ref_count(weak_ec2) == 0, | |
560 | "EC2 reference count is 0"); | |
561 | ok(bt_object_get_ref_count(weak_ec3) == 0, | |
562 | "EC3 reference count is 0"); | |
563 | ||
564 | /* Reclaim last reference held by User C. */ | |
565 | BT_PUT(user_c.ec); | |
6271743c PP |
566 | } |
567 | ||
568 | static void create_user_full(struct user *user) | |
569 | { | |
32bd47d1 | 570 | gchar *trace_path; |
50842bdc PP |
571 | struct bt_field_type *ft; |
572 | struct bt_field *field; | |
6271743c PP |
573 | struct bt_ctf_clock *clock; |
574 | int ret; | |
575 | ||
32bd47d1 | 576 | trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL); |
6271743c PP |
577 | if (!bt_mkdtemp(trace_path)) { |
578 | perror("# perror"); | |
579 | } | |
580 | ||
581 | user->writer = bt_ctf_writer_create(trace_path); | |
582 | assert(user->writer); | |
dc3fffef | 583 | ret = bt_ctf_writer_set_byte_order(user->writer, |
50842bdc | 584 | BT_BYTE_ORDER_LITTLE_ENDIAN); |
dc3fffef | 585 | assert(ret == 0); |
6271743c PP |
586 | user->tc = bt_ctf_writer_get_trace(user->writer); |
587 | assert(user->tc); | |
50842bdc | 588 | user->sc = bt_stream_class_create("sc"); |
6271743c PP |
589 | assert(user->sc); |
590 | clock = bt_ctf_clock_create("the_clock"); | |
591 | assert(clock); | |
81582b6b JG |
592 | ret = bt_ctf_writer_add_clock(user->writer, clock); |
593 | assert(!ret); | |
50842bdc | 594 | ret = bt_stream_class_set_clock(user->sc, clock); |
6271743c | 595 | assert(!ret); |
dc3fffef | 596 | BT_PUT(clock); |
6271743c PP |
597 | user->stream = bt_ctf_writer_create_stream(user->writer, user->sc); |
598 | assert(user->stream); | |
50842bdc | 599 | user->ec = bt_event_class_create("ec"); |
6271743c PP |
600 | assert(user->ec); |
601 | ft = create_integer_struct(); | |
602 | assert(ft); | |
50842bdc | 603 | ret = bt_event_class_set_payload_type(user->ec, ft); |
6271743c PP |
604 | BT_PUT(ft); |
605 | assert(!ret); | |
50842bdc | 606 | ret = bt_stream_class_add_event_class(user->sc, user->ec); |
6271743c | 607 | assert(!ret); |
50842bdc | 608 | user->event = bt_event_create(user->ec); |
6271743c | 609 | assert(user->event); |
50842bdc | 610 | field = bt_event_get_payload(user->event, "payload_8"); |
6271743c | 611 | assert(field); |
50842bdc | 612 | ret = bt_field_unsigned_integer_set_value(field, 10); |
6271743c PP |
613 | assert(!ret); |
614 | BT_PUT(field); | |
50842bdc | 615 | field = bt_event_get_payload(user->event, "payload_16"); |
6271743c | 616 | assert(field); |
50842bdc | 617 | ret = bt_field_unsigned_integer_set_value(field, 20); |
6271743c PP |
618 | assert(!ret); |
619 | BT_PUT(field); | |
50842bdc | 620 | field = bt_event_get_payload(user->event, "payload_32"); |
6271743c | 621 | assert(field); |
50842bdc | 622 | ret = bt_field_unsigned_integer_set_value(field, 30); |
6271743c PP |
623 | assert(!ret); |
624 | BT_PUT(field); | |
50842bdc | 625 | ret = bt_stream_append_event(user->stream, user->event); |
6271743c | 626 | assert(!ret); |
851299b9 | 627 | recursive_rmdir(trace_path); |
32bd47d1 | 628 | g_free(trace_path); |
6271743c PP |
629 | } |
630 | ||
631 | static void test_put_order_swap(size_t *array, size_t a, size_t b) | |
632 | { | |
633 | size_t temp = array[a]; | |
634 | ||
635 | array[a] = array[b]; | |
636 | array[b] = temp; | |
637 | } | |
638 | ||
639 | static void test_put_order_put_objects(size_t *array, size_t size) | |
640 | { | |
641 | size_t i; | |
642 | struct user user = { 0 }; | |
6f2097e2 | 643 | void **objects = (void *) &user; |
6271743c PP |
644 | |
645 | create_user_full(&user); | |
646 | printf("# "); | |
647 | ||
648 | for (i = 0; i < size; ++i) { | |
6f2097e2 | 649 | void *obj = objects[array[i]]; |
6271743c PP |
650 | |
651 | printf("%s", user_names[array[i]]); | |
652 | BT_PUT(obj); | |
653 | ||
654 | if (i < size - 1) { | |
655 | printf(" -> "); | |
656 | } | |
657 | } | |
658 | ||
659 | puts(""); | |
660 | } | |
661 | ||
662 | static void test_put_order_permute(size_t *array, int k, size_t size) | |
663 | { | |
664 | if (k == 0) { | |
665 | test_put_order_put_objects(array, size); | |
666 | } else { | |
667 | int i; | |
668 | ||
669 | for (i = k - 1; i >= 0; i--) { | |
670 | size_t next_k = k - 1; | |
671 | ||
672 | test_put_order_swap(array, i, next_k); | |
673 | test_put_order_permute(array, next_k, size); | |
674 | test_put_order_swap(array, i, next_k); | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | static void test_put_order(void) | |
680 | { | |
681 | size_t i; | |
682 | size_t array[USER_NR_ELEMENTS]; | |
683 | ||
684 | /* Initialize array of indexes */ | |
685 | for (i = 0; i < USER_NR_ELEMENTS; ++i) { | |
686 | array[i] = i; | |
687 | } | |
688 | ||
689 | test_put_order_permute(array, USER_NR_ELEMENTS, USER_NR_ELEMENTS); | |
690 | } | |
691 | ||
692 | /** | |
693 | * The objective of this test is to implement and expand upon the scenario | |
694 | * described in the reference counting documentation and ensure that any node of | |
695 | * the Trace, Stream Class, Event Class, Stream and Event hiearchy keeps all | |
696 | * other "alive" and reachable. | |
697 | * | |
698 | * External tools (e.g. valgrind) should be used to confirm that this | |
699 | * known-good test does not leak memory. | |
700 | */ | |
701 | int main(int argc, char **argv) | |
702 | { | |
27f4d205 | 703 | /* Initialize tap harness before any tests */ |
6271743c PP |
704 | plan_tests(NR_TESTS); |
705 | ||
706 | test_example_scenario(); | |
707 | test_put_order(); | |
708 | ||
c9b3f44b JG |
709 | return exit_status(); |
710 | } |