Remove warnings when using bt_plugin_create_all_from_dir()
[babeltrace.git] / tests / lib / test_ctf_ir_ref.c
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"
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>
34 #include <assert.h>
35 #include "common.h"
36
37 #define NR_TESTS 41
38
39 struct 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 *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
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 */
65 static struct bt_ctf_field_type *create_integer_struct(void)
66 {
67 int ret;
68 struct bt_ctf_field_type *structure = NULL;
69 struct bt_ctf_field_type *ui8 = NULL, *ui16 = NULL, *ui32 = NULL;
70
71 structure = bt_ctf_field_type_structure_create();
72 if (!structure) {
73 goto error;
74 }
75
76 ui8 = bt_ctf_field_type_integer_create(8);
77 if (!ui8) {
78 diag("Failed to create uint8_t type");
79 goto error;
80 }
81 ret = bt_ctf_field_type_structure_add_field(structure, ui8,
82 "payload_8");
83 if (ret) {
84 diag("Failed to add uint8_t to structure");
85 goto error;
86 }
87 ui16 = bt_ctf_field_type_integer_create(16);
88 if (!ui16) {
89 diag("Failed to create uint16_t type");
90 goto error;
91 }
92 ret = bt_ctf_field_type_structure_add_field(structure, ui16,
93 "payload_16");
94 if (ret) {
95 diag("Failed to add uint16_t to structure");
96 goto error;
97 }
98 ui32 = bt_ctf_field_type_integer_create(32);
99 if (!ui32) {
100 diag("Failed to create uint32_t type");
101 goto error;
102 }
103 ret = bt_ctf_field_type_structure_add_field(structure, ui32,
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 */
125 static struct bt_ctf_event_class *create_simple_event(const char *name)
126 {
127 int ret;
128 struct bt_ctf_event_class *event = NULL;
129 struct bt_ctf_field_type *payload = NULL;
130
131 assert(name);
132 event = bt_ctf_event_class_create(name);
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
144 ret = bt_ctf_event_class_set_payload_type(event, payload);
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 */
167 static struct bt_ctf_event_class *create_complex_event(const char *name)
168 {
169 int ret;
170 struct bt_ctf_event_class *event = NULL;
171 struct bt_ctf_field_type *inner = NULL, *outer = NULL;
172
173 assert(name);
174 event = bt_ctf_event_class_create(name);
175 if (!event) {
176 diag("Failed to create complex event");
177 goto error;
178 }
179
180 outer = create_integer_struct();
181 if (!outer) {
182 diag("Failed to initialize integer structure");
183 goto error;
184 }
185
186 inner = create_integer_struct();
187 if (!inner) {
188 diag("Failed to initialize integer structure");
189 goto error;
190 }
191
192 ret = bt_ctf_field_type_structure_add_field(outer, inner,
193 "payload_struct");
194 if (ret) {
195 diag("Failed to add inner structure to outer structure");
196 goto error;
197 }
198
199 ret = bt_ctf_event_class_set_payload_type(event, outer);
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
213 static struct bt_ctf_stream_class *create_sc1(void)
214 {
215 int ret;
216 struct bt_ctf_event_class *ec1 = NULL, *ec2 = NULL;
217 struct bt_ctf_stream_class *sc1 = NULL, *ret_stream = NULL;
218
219 sc1 = bt_ctf_stream_class_create("sc1");
220 if (!sc1) {
221 diag("Failed to create Stream Class");
222 goto error;
223 }
224
225 ec1 = create_complex_event("ec1");
226 if (!ec1) {
227 diag("Failed to create complex event EC1");
228 goto error;
229 }
230 ret = bt_ctf_stream_class_add_event_class(sc1, ec1);
231 if (ret) {
232 diag("Failed to add EC1 to SC1");
233 goto error;
234 }
235
236 ec2 = create_simple_event("ec2");
237 if (!ec2) {
238 diag("Failed to create simple event EC2");
239 goto error;
240 }
241 ret = bt_ctf_stream_class_add_event_class(sc1, ec2);
242 if (ret) {
243 diag("Failed to add EC1 to SC1");
244 goto error;
245 }
246
247 ret_stream = bt_ctf_event_class_get_stream_class(ec1);
248 ok(ret_stream == sc1, "Get parent stream SC1 from EC1");
249 BT_PUT(ret_stream);
250
251 ret_stream = bt_ctf_event_class_get_stream_class(ec2);
252 ok(ret_stream == sc1, "Get parent stream SC1 from EC2");
253 end:
254 BT_PUT(ret_stream);
255 BT_PUT(ec1);
256 BT_PUT(ec2);
257 return sc1;
258 error:
259 BT_PUT(sc1);
260 goto end;
261 }
262
263 static struct bt_ctf_stream_class *create_sc2(void)
264 {
265 int ret;
266 struct bt_ctf_event_class *ec3 = NULL;
267 struct bt_ctf_stream_class *sc2 = NULL, *ret_stream = NULL;
268
269 sc2 = bt_ctf_stream_class_create("sc2");
270 if (!sc2) {
271 diag("Failed to create Stream Class");
272 goto error;
273 }
274
275 ec3 = create_simple_event("ec3");
276 if (!ec3) {
277 diag("Failed to create simple event EC3");
278 goto error;
279 }
280 ret = bt_ctf_stream_class_add_event_class(sc2, ec3);
281 if (ret) {
282 diag("Failed to add EC3 to SC2");
283 goto error;
284 }
285
286 ret_stream = bt_ctf_event_class_get_stream_class(ec3);
287 ok(ret_stream == sc2, "Get parent stream SC2 from EC3");
288 end:
289 BT_PUT(ret_stream);
290 BT_PUT(ec3);
291 return sc2;
292 error:
293 BT_PUT(sc2);
294 goto end;
295 }
296
297 static struct bt_ctf_trace *create_tc1(void)
298 {
299 int ret;
300 struct bt_ctf_trace *tc1 = NULL;
301 struct bt_ctf_stream_class *sc1 = NULL, *sc2 = NULL;
302
303 tc1 = bt_ctf_trace_create();
304 if (!tc1) {
305 diag("bt_ctf_trace_create returned NULL");
306 goto error;
307 }
308
309 ret = bt_ctf_trace_set_native_byte_order(tc1,
310 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
311 assert(ret == 0);
312 sc1 = create_sc1();
313 ok(sc1, "Create SC1");
314 if (!sc1) {
315 goto error;
316 }
317 ret = bt_ctf_trace_add_stream_class(tc1, sc1);
318 ok(!ret, "Add SC1 to TC1");
319 if (ret) {
320 goto error;
321 }
322
323 sc2 = create_sc2();
324 ok(sc2, "Create SC2");
325 if (!sc2) {
326 goto error;
327 }
328 ret = bt_ctf_trace_add_stream_class(tc1, sc2);
329 ok(!ret, "Add SC2 to TC1");
330 if (ret) {
331 goto error;
332 }
333 end:
334 BT_PUT(sc1);
335 BT_PUT(sc2);
336 return tc1;
337 error:
338 BT_PUT(tc1);
339 goto end;
340 }
341
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)
349 {
350 *tc1 = tc;
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);
356 bt_put(*sc1);
357 bt_put(*sc2);
358 bt_put(*ec1);
359 bt_put(*ec2);
360 bt_put(*ec3);
361 }
362
363 static void test_example_scenario(void)
364 {
365 /**
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
370 * objects.
371 */
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,
375 *weak_ec3 = NULL;
376 struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 };
377
378 /* The only reference which exists at this point is on TC1. */
379 tc1 = create_tc1();
380 ok(tc1, "Initialize trace");
381 if (!tc1) {
382 return;
383 }
384
385 init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1,
386 &weak_ec2, &weak_ec3);
387
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");
398
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");
403
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");
411
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");
421
422 /* User A releases its reference to SC2. */
423 diag("User A releases SC2");
424 BT_PUT(user_a.sc);
425 /*
426 * We keep the pointer to SC2 around to validate its reference
427 * count.
428 */
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");
435
436 /* User A releases its reference to TC1. */
437 diag("User A releases TC1");
438 BT_PUT(user_a.tc);
439 /*
440 * We keep the pointer to TC1 around to validate its reference
441 * count.
442 */
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");
449
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");
457
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");
465
466 /* User A releases its reference on EC3. */
467 diag("User A releases its reference on EC3");
468 BT_PUT(user_a.ec);
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");
475
476 /* User B releases its reference on SC1. */
477 diag("User B releases its reference on SC1");
478 BT_PUT(user_b.sc);
479 ok(bt_object_get_ref_count(weak_sc1) == 1,
480 "SC1 reference count is 1");
481
482 /*
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.
485 */
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");
498
499 /* Reclaim last reference held by User C. */
500 BT_PUT(user_c.ec);
501 }
502
503 static void create_user_full(struct user *user)
504 {
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;
509 int ret;
510
511 if (!bt_mkdtemp(trace_path)) {
512 perror("# perror");
513 }
514
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);
519 assert(ret == 0);
520 user->tc = bt_ctf_writer_get_trace(user->writer);
521 assert(user->tc);
522 user->sc = bt_ctf_stream_class_create("sc");
523 assert(user->sc);
524 clock = bt_ctf_clock_create("the_clock");
525 assert(clock);
526 ret = bt_ctf_writer_add_clock(user->writer, clock);
527 assert(!ret);
528 ret = bt_ctf_stream_class_set_clock(user->sc, clock);
529 assert(!ret);
530 BT_PUT(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");
534 assert(user->ec);
535 ft = create_integer_struct();
536 assert(ft);
537 ret = bt_ctf_event_class_set_payload_type(user->ec, ft);
538 BT_PUT(ft);
539 assert(!ret);
540 ret = bt_ctf_stream_class_add_event_class(user->sc, user->ec);
541 assert(!ret);
542 user->event = bt_ctf_event_create(user->ec);
543 assert(user->event);
544 field = bt_ctf_event_get_payload(user->event, "payload_8");
545 assert(field);
546 ret = bt_ctf_field_unsigned_integer_set_value(field, 10);
547 assert(!ret);
548 BT_PUT(field);
549 field = bt_ctf_event_get_payload(user->event, "payload_16");
550 assert(field);
551 ret = bt_ctf_field_unsigned_integer_set_value(field, 20);
552 assert(!ret);
553 BT_PUT(field);
554 field = bt_ctf_event_get_payload(user->event, "payload_32");
555 assert(field);
556 ret = bt_ctf_field_unsigned_integer_set_value(field, 30);
557 assert(!ret);
558 BT_PUT(field);
559 ret = bt_ctf_stream_append_event(user->stream, user->event);
560 assert(!ret);
561 recursive_rmdir(trace_path);
562 }
563
564 static void test_put_order_swap(size_t *array, size_t a, size_t b)
565 {
566 size_t temp = array[a];
567
568 array[a] = array[b];
569 array[b] = temp;
570 }
571
572 static void test_put_order_put_objects(size_t *array, size_t size)
573 {
574 size_t i;
575 struct user user = { 0 };
576 void **objects = (void *) &user;
577
578 create_user_full(&user);
579 printf("# ");
580
581 for (i = 0; i < size; ++i) {
582 void *obj = objects[array[i]];
583
584 printf("%s", user_names[array[i]]);
585 BT_PUT(obj);
586
587 if (i < size - 1) {
588 printf(" -> ");
589 }
590 }
591
592 puts("");
593 }
594
595 static void test_put_order_permute(size_t *array, int k, size_t size)
596 {
597 if (k == 0) {
598 test_put_order_put_objects(array, size);
599 } else {
600 int i;
601
602 for (i = k - 1; i >= 0; i--) {
603 size_t next_k = k - 1;
604
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);
608 }
609 }
610 }
611
612 static void test_put_order(void)
613 {
614 size_t i;
615 size_t array[USER_NR_ELEMENTS];
616
617 /* Initialize array of indexes */
618 for (i = 0; i < USER_NR_ELEMENTS; ++i) {
619 array[i] = i;
620 }
621
622 test_put_order_permute(array, USER_NR_ELEMENTS, USER_NR_ELEMENTS);
623 }
624
625 /**
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.
630 *
631 * External tools (e.g. valgrind) should be used to confirm that this
632 * known-good test does not leak memory.
633 */
634 int main(int argc, char **argv)
635 {
636 /* Initialize tap harness before any tests */
637 plan_tests(NR_TESTS);
638
639 test_example_scenario();
640 test_put_order();
641
642 return exit_status();
643 }
This page took 0.043291 seconds and 5 git commands to generate.