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