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