ir: rename event-fields/event-types -> fields/field-types
[babeltrace.git] / tests / lib / test_ctf_ir_ref.c
CommitLineData
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"
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>
28#include <assert.h>
29
8bbe269d
MJ
30#define NR_TESTS 41
31
c9b3f44b
JG
32struct user {
33 struct bt_ctf_trace *tc;
34 struct bt_ctf_stream_class *sc;
35 struct bt_ctf_event_class *ec;
36};
37
38/**
39 * Returns a structure containing the following fields:
40 * - uint8_t payload_8;
41 * - uint16_t payload_16;
42 * - uint32_t payload_32;
43 */
44static struct bt_ctf_field_type *create_integer_struct(void)
45{
46 int ret;
47 struct bt_ctf_field_type *structure = NULL;
48 struct bt_ctf_field_type *ui8 = NULL, *ui16 = NULL, *ui32 = NULL;
49
50 structure = bt_ctf_field_type_structure_create();
51 if (!structure) {
52 goto error;
53 }
54
55 ui8 = bt_ctf_field_type_integer_create(8);
56 if (!ui8) {
57 diag("Failed to create uint8_t type");
58 goto error;
59 }
60 ret = bt_ctf_field_type_structure_add_field(structure, ui8,
61 "payload_8");
62 if (ret) {
63 diag("Failed to add uint8_t to structure");
64 goto error;
65 }
66 ui16 = bt_ctf_field_type_integer_create(16);
67 if (!ui16) {
68 diag("Failed to create uint16_t type");
69 goto error;
70 }
71 ret = bt_ctf_field_type_structure_add_field(structure, ui16,
72 "payload_16");
73 if (ret) {
74 diag("Failed to add uint16_t to structure");
75 goto error;
76 }
77 ui32 = bt_ctf_field_type_integer_create(32);
78 if (!ui32) {
79 diag("Failed to create uint32_t type");
80 goto error;
81 }
82 ret = bt_ctf_field_type_structure_add_field(structure, ui32,
83 "payload_32");
84 if (ret) {
85 diag("Failed to add uint32_t to structure");
86 goto error;
87 }
88end:
89 BT_PUT(ui8);
90 BT_PUT(ui16);
91 BT_PUT(ui32);
92 return structure;
93error:
94 BT_PUT(structure);
95 goto end;
96}
97
98/**
99 * A simple event has the following payload:
100 * - uint8_t payload_8;
101 * - uint16_t payload_16;
102 * - uint32_t payload_32;
103 */
104static struct bt_ctf_event_class *create_simple_event(const char *name)
105{
106 int ret;
107 struct bt_ctf_event_class *event = NULL;
108 struct bt_ctf_field_type *payload = NULL;
109
110 assert(name);
111 event = bt_ctf_event_class_create(name);
112 if (!event) {
113 diag("Failed to create simple event");
114 goto error;
115 }
116
117 payload = create_integer_struct();
118 if (!payload) {
119 diag("Failed to initialize integer structure");
120 goto error;
121 }
122
123 ret = bt_ctf_event_class_set_payload_type(event, payload);
124 if (ret) {
125 diag("Failed to set simple event payload");
126 goto error;
127 }
128end:
129 BT_PUT(payload);
130 return event;
131error:
132 BT_PUT(event);
133 goto end;;
134}
135
136/**
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;
145 */
146static struct bt_ctf_event_class *create_complex_event(const char *name)
147{
148 int ret;
149 struct bt_ctf_event_class *event = NULL;
150 struct bt_ctf_field_type *inner = NULL, *outer = NULL;
151
152 assert(name);
153 event = bt_ctf_event_class_create(name);
154 if (!event) {
155 diag("Failed to create complex event");
156 goto error;
157 }
158
159 outer = create_integer_struct();
160 if (!outer) {
161 diag("Failed to initialize integer structure");
162 goto error;
163 }
164
165 inner = create_integer_struct();
166 if (!inner) {
167 diag("Failed to initialize integer structure");
168 goto error;
169 }
170
171 ret = bt_ctf_field_type_structure_add_field(outer, inner,
172 "payload_struct");
173 if (ret) {
174 diag("Failed to add inner structure to outer structure");
175 goto error;
176 }
177
178 ret = bt_ctf_event_class_set_payload_type(event, outer);
179 if (ret) {
180 diag("Failed to set complex event payload");
181 goto error;
182 }
183end:
184 BT_PUT(inner);
185 BT_PUT(outer);
186 return event;
187error:
188 BT_PUT(event);
189 goto end;;
190}
191
192static struct bt_ctf_stream_class *create_sc1(void)
193{
194 int ret;
195 struct bt_ctf_event_class *ec1 = NULL, *ec2 = NULL;
196 struct bt_ctf_stream_class *sc1 = NULL, *ret_stream = NULL;
197
198 sc1 = bt_ctf_stream_class_create("sc1");
199 if (!sc1) {
200 diag("Failed to create Stream Class");
201 goto error;
202 }
203
204 ec1 = create_complex_event("ec1");
205 if (!ec1) {
206 diag("Failed to create complex event EC1");
207 goto error;
208 }
209 ret = bt_ctf_stream_class_add_event_class(sc1, ec1);
210 if (ret) {
211 diag("Failed to add EC1 to SC1");
212 goto error;
213 }
214
215 ec2 = create_simple_event("ec2");
216 if (!ec2) {
217 diag("Failed to create simple event EC2");
218 goto error;
219 }
220 ret = bt_ctf_stream_class_add_event_class(sc1, ec2);
221 if (ret) {
222 diag("Failed to add EC1 to SC1");
223 goto error;
224 }
225
226 ret_stream = bt_ctf_event_class_get_stream_class(ec1);
227 ok(ret_stream == sc1, "Get parent stream SC1 from EC1");
228 BT_PUT(ret_stream);
229
230 ret_stream = bt_ctf_event_class_get_stream_class(ec2);
231 ok(ret_stream == sc1, "Get parent stream SC1 from EC2");
232end:
233 BT_PUT(ret_stream);
234 BT_PUT(ec1);
235 BT_PUT(ec2);
236 return sc1;
237error:
238 BT_PUT(sc1);
239 goto end;
240}
241
242static struct bt_ctf_stream_class *create_sc2(void)
243{
244 int ret;
245 struct bt_ctf_event_class *ec3 = NULL;
246 struct bt_ctf_stream_class *sc2 = NULL, *ret_stream = NULL;
247
248 sc2 = bt_ctf_stream_class_create("sc2");
249 if (!sc2) {
250 diag("Failed to create Stream Class");
251 goto error;
252 }
253
254 ec3 = create_simple_event("ec3");
255 if (!ec3) {
256 diag("Failed to create simple event EC3");
257 goto error;
258 }
259 ret = bt_ctf_stream_class_add_event_class(sc2, ec3);
260 if (ret) {
261 diag("Failed to add EC3 to SC2");
262 goto error;
263 }
264
265 ret_stream = bt_ctf_event_class_get_stream_class(ec3);
266 ok(ret_stream == sc2, "Get parent stream SC2 from EC3");
267end:
268 BT_PUT(ret_stream);
269 BT_PUT(ec3);
270 return sc2;
271error:
272 BT_PUT(sc2);
273 goto end;
274}
275
276static struct bt_ctf_trace *create_tc1(void)
277{
278 int ret;
279 struct bt_ctf_trace *tc1 = NULL;
280 struct bt_ctf_stream_class *sc1 = NULL, *sc2 = NULL;
281
282 tc1 = bt_ctf_trace_create();
283 if (!tc1) {
284 diag("bt_ctf_trace_create returned NULL");
285 goto error;
286 }
287
288 sc1 = create_sc1();
289 ok(sc1, "Create SC1");
290 if (!sc1) {
291 goto error;
292 }
293 ret = bt_ctf_trace_add_stream_class(tc1, sc1);
294 ok(!ret, "Add SC1 to TC1");
295 if (ret) {
296 goto error;
297 }
298
299 sc2 = create_sc2();
300 ok(sc2, "Create SC2");
301 if (!sc2) {
302 goto error;
303 }
304 ret = bt_ctf_trace_add_stream_class(tc1, sc2);
305 ok(!ret, "Add SC2 to TC1");
306 if (ret) {
307 goto error;
308 }
309end:
310 BT_PUT(sc1);
311 BT_PUT(sc2);
312 return tc1;
313error:
314 BT_PUT(tc1);
315 goto end;
316}
317
318static 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)
325{
326 *tc1 = tc;
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);
332 bt_put(*sc1);
333 bt_put(*sc2);
334 bt_put(*ec1);
335 bt_put(*ec2);
336 bt_put(*ec3);
337}
338
339/**
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.
344 *
345 * External tools (e.g. valgrind) should be used to confirm that this
346 * known-good test does not leak memory.
347 */
348int main(int argc, char **argv)
349{
350 /**
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
355 * objects.
356 */
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,
360 *weak_ec3 = NULL;
361 struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 };
362
8bbe269d
MJ
363 /* Initialize tap harness before any tests */
364 plan_tests(NR_TESTS);
365
c9b3f44b
JG
366 /* The only reference which exists at this point is on TC1. */
367 tc1 = create_tc1();
368 ok(tc1, "Initialize trace");
369 if (!tc1) {
370 goto end;
371 }
372
373 init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1,
374 &weak_ec2, &weak_ec3);
c9b3f44b
JG
375
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");
386
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");
391
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");
399
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");
409
410 /* User A releases its reference to SC2. */
411 diag("User A releases SC2");
412 BT_PUT(user_a.sc);
413 /*
414 * We keep the pointer to SC2 around to validate its reference
415 * count.
416 */
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");
423
424 /* User A releases its reference to TC1. */
425 diag("User A releases TC1");
426 BT_PUT(user_a.tc);
427 /*
428 * We keep the pointer to TC1 around to validate its reference
429 * count.
430 */
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");
437
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");
445
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");
453
454 /* User A releases its reference on EC3. */
455 diag("User A releases its reference on EC3");
456 BT_PUT(user_a.ec);
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");
463
464 /* User B releases its reference on SC1. */
465 diag("User B releases its reference on SC1");
466 BT_PUT(user_b.sc);
467 ok(bt_object_get_ref_count(weak_sc1) == 1,
468 "SC1 reference count is 1");
469
470 /*
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.
473 */
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");
486
487 /* Reclaim last reference held by User C. */
488 BT_PUT(user_c.ec);
489end:
490 return exit_status();
491}
This page took 0.057547 seconds and 4 git commands to generate.