xref: /xnu-11417.140.69/tests/os_refcnt.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions #include <darwintest.h>
2*43a90889SApple OSS Distributions #include <darwintest_utils.h>
3*43a90889SApple OSS Distributions #include <stdio.h>
4*43a90889SApple OSS Distributions #include <assert.h>
5*43a90889SApple OSS Distributions #include <setjmp.h>
6*43a90889SApple OSS Distributions #include <os/tsd.h>
7*43a90889SApple OSS Distributions 
8*43a90889SApple OSS Distributions #define DEVELOPMENT 1
9*43a90889SApple OSS Distributions #define DEBUG 0
10*43a90889SApple OSS Distributions #define XNU_KERNEL_PRIVATE 1
11*43a90889SApple OSS Distributions 
12*43a90889SApple OSS Distributions #define OS_REFCNT_DEBUG 1
13*43a90889SApple OSS Distributions #define STRESS_TESTS 0
14*43a90889SApple OSS Distributions #define __zpercpu
15*43a90889SApple OSS Distributions 
16*43a90889SApple OSS Distributions #pragma clang diagnostic ignored "-Watomic-implicit-seq-cst"
17*43a90889SApple OSS Distributions #pragma clang diagnostic ignored "-Wc++98-compat"
18*43a90889SApple OSS Distributions 
19*43a90889SApple OSS Distributions __abortlike
20*43a90889SApple OSS Distributions void handle_panic(const char *func, char *str, ...);
21*43a90889SApple OSS Distributions #define panic(...) handle_panic(__func__, __VA_ARGS__)
22*43a90889SApple OSS Distributions 
23*43a90889SApple OSS Distributions #define ZPERCPU_STRIDE 128
24*43a90889SApple OSS Distributions 
25*43a90889SApple OSS Distributions static inline int
zpercpu_count(void)26*43a90889SApple OSS Distributions zpercpu_count(void)
27*43a90889SApple OSS Distributions {
28*43a90889SApple OSS Distributions 	static int n;
29*43a90889SApple OSS Distributions 	if (__improbable(n == 0)) {
30*43a90889SApple OSS Distributions 		n = dt_ncpu();
31*43a90889SApple OSS Distributions 	}
32*43a90889SApple OSS Distributions 	return n;
33*43a90889SApple OSS Distributions }
34*43a90889SApple OSS Distributions 
35*43a90889SApple OSS Distributions static inline void
thread_wakeup(void * event)36*43a90889SApple OSS Distributions thread_wakeup(void *event)
37*43a90889SApple OSS Distributions {
38*43a90889SApple OSS Distributions 	abort();
39*43a90889SApple OSS Distributions }
40*43a90889SApple OSS Distributions 
41*43a90889SApple OSS Distributions #define zalloc_percpu(zone, flags) \
42*43a90889SApple OSS Distributions 	(uint64_t _Atomic *)calloc((size_t)zpercpu_count(), ZPERCPU_STRIDE)
43*43a90889SApple OSS Distributions 
44*43a90889SApple OSS Distributions #define zfree_percpu(zone, ptr) \
45*43a90889SApple OSS Distributions 	free(ptr)
46*43a90889SApple OSS Distributions 
47*43a90889SApple OSS Distributions static inline uint64_t _Atomic *
zpercpu_get_cpu(uint64_t _Atomic * ptr,int cpu)48*43a90889SApple OSS Distributions zpercpu_get_cpu(uint64_t _Atomic *ptr, int cpu)
49*43a90889SApple OSS Distributions {
50*43a90889SApple OSS Distributions 	return (uint64_t _Atomic *)((uintptr_t)ptr + (uintptr_t)cpu * ZPERCPU_STRIDE);
51*43a90889SApple OSS Distributions }
52*43a90889SApple OSS Distributions 
53*43a90889SApple OSS Distributions #define zpercpu_get(ptr)  zpercpu_get_cpu(ptr, 0)
54*43a90889SApple OSS Distributions 
55*43a90889SApple OSS Distributions #define zpercpu_foreach_cpu(cpu) \
56*43a90889SApple OSS Distributions 	for (int cpu = 0, __n = zpercpu_count(); cpu < __n; cpu++)
57*43a90889SApple OSS Distributions 
58*43a90889SApple OSS Distributions #define zpercpu_foreach(cpu) \
59*43a90889SApple OSS Distributions 	for (int cpu = 0, __n = zpercpu_count(); cpu < __n; cpu++)
60*43a90889SApple OSS Distributions 
61*43a90889SApple OSS Distributions #define cpu_number() (int)_os_cpu_number()
62*43a90889SApple OSS Distributions 
63*43a90889SApple OSS Distributions #include "../libkern/os/refcnt.h"
64*43a90889SApple OSS Distributions #include "../libkern/os/refcnt.c"
65*43a90889SApple OSS Distributions 
66*43a90889SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
67*43a90889SApple OSS Distributions 
68*43a90889SApple OSS Distributions /* import some of the refcnt internal state for testing */
69*43a90889SApple OSS Distributions extern bool ref_debug_enable;
70*43a90889SApple OSS Distributions os_refgrp_decl_extern(global_ref_group);
71*43a90889SApple OSS Distributions 
72*43a90889SApple OSS Distributions T_GLOBAL_META(
73*43a90889SApple OSS Distributions 	T_META_NAMESPACE("os_refcnt"),
74*43a90889SApple OSS Distributions 	T_META_CHECK_LEAKS(false)
75*43a90889SApple OSS Distributions 	);
76*43a90889SApple OSS Distributions 
77*43a90889SApple OSS Distributions T_DECL(os_refcnt, "Basic atomic refcount")
78*43a90889SApple OSS Distributions {
79*43a90889SApple OSS Distributions 	struct os_refcnt rc;
80*43a90889SApple OSS Distributions 	os_ref_init(&rc, NULL);
81*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "refcount correctly initialized");
82*43a90889SApple OSS Distributions 
83*43a90889SApple OSS Distributions 	os_ref_retain(&rc);
84*43a90889SApple OSS Distributions 	os_ref_retain(&rc);
85*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
86*43a90889SApple OSS Distributions 
87*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release(&rc);
88*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
89*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
90*43a90889SApple OSS Distributions 
91*43a90889SApple OSS Distributions 	os_ref_release_live(&rc);
92*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "release_live decreased count");
93*43a90889SApple OSS Distributions 
94*43a90889SApple OSS Distributions 	x = os_ref_release(&rc);
95*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
96*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
97*43a90889SApple OSS Distributions 
98*43a90889SApple OSS Distributions 	os_ref_init(&rc, NULL);
99*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(os_ref_retain_try(&rc), "try retained");
100*43a90889SApple OSS Distributions 
101*43a90889SApple OSS Distributions 	(void)os_ref_release(&rc);
102*43a90889SApple OSS Distributions 	(void)os_ref_release(&rc);
103*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "release");
104*43a90889SApple OSS Distributions 
105*43a90889SApple OSS Distributions 	T_ASSERT_FALSE(os_ref_retain_try(&rc), "try failed");
106*43a90889SApple OSS Distributions }
107*43a90889SApple OSS Distributions 
108*43a90889SApple OSS Distributions T_DECL(os_pcpu_refcnt, "Basic atomic refcount")
109*43a90889SApple OSS Distributions {
110*43a90889SApple OSS Distributions 	dispatch_queue_t rq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
111*43a90889SApple OSS Distributions 	dispatch_group_t g = dispatch_group_create();
112*43a90889SApple OSS Distributions 	os_pcpu_ref_t rc;
113*43a90889SApple OSS Distributions 
114*43a90889SApple OSS Distributions 	os_pcpu_ref_init(&rc, NULL);
115*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), OS_REFCNT_MAX_COUNT,
116*43a90889SApple OSS Distributions 	    "refcount correctly initialized");
117*43a90889SApple OSS Distributions 
118*43a90889SApple OSS Distributions 	dispatch_group_async(g, rq, ^{
119*43a90889SApple OSS Distributions 		os_pcpu_ref_retain(rc, NULL);
120*43a90889SApple OSS Distributions 	});
121*43a90889SApple OSS Distributions 	dispatch_group_async(g, rq, ^{
122*43a90889SApple OSS Distributions 		T_ASSERT_TRUE(os_pcpu_ref_retain_try(rc, NULL), "try succeeded");
123*43a90889SApple OSS Distributions 	});
124*43a90889SApple OSS Distributions 	dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
125*43a90889SApple OSS Distributions 
126*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), OS_REFCNT_MAX_COUNT,
127*43a90889SApple OSS Distributions 	    "retain increased count");
128*43a90889SApple OSS Distributions 
129*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_kill(rc, NULL), 2,
130*43a90889SApple OSS Distributions 	    "kill decreased count");
131*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), 2,
132*43a90889SApple OSS Distributions 	    "kill decreased count");
133*43a90889SApple OSS Distributions 
134*43a90889SApple OSS Distributions 	T_ASSERT_FALSE(os_pcpu_ref_retain_try(rc, NULL), "try failed");
135*43a90889SApple OSS Distributions 
136*43a90889SApple OSS Distributions 	os_pcpu_ref_release_live(rc, NULL);
137*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), 1, "release_live decreased count");
138*43a90889SApple OSS Distributions 
139*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_release(rc, NULL), 0, "returned released");
140*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), 0, "released");
141*43a90889SApple OSS Distributions 
142*43a90889SApple OSS Distributions 	os_pcpu_ref_destroy(&rc, NULL);
143*43a90889SApple OSS Distributions }
144*43a90889SApple OSS Distributions 
145*43a90889SApple OSS Distributions T_DECL(refcnt_raw, "Raw refcount")
146*43a90889SApple OSS Distributions {
147*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
148*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
149*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "refcount correctly initialized");
150*43a90889SApple OSS Distributions 
151*43a90889SApple OSS Distributions 	os_ref_retain_raw(&rc, NULL);
152*43a90889SApple OSS Distributions 	os_ref_retain_raw(&rc, NULL);
153*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
154*43a90889SApple OSS Distributions 
155*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release_raw(&rc, NULL);
156*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
157*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
158*43a90889SApple OSS Distributions 
159*43a90889SApple OSS Distributions 	os_ref_release_live_raw(&rc, NULL);
160*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "release_live decreased count");
161*43a90889SApple OSS Distributions 
162*43a90889SApple OSS Distributions 	x = os_ref_release_raw(&rc, NULL);
163*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
164*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
165*43a90889SApple OSS Distributions 
166*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
167*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(os_ref_retain_try_raw(&rc, NULL), "try retained");
168*43a90889SApple OSS Distributions 
169*43a90889SApple OSS Distributions 	(void)os_ref_release_raw(&rc, NULL);
170*43a90889SApple OSS Distributions 	(void)os_ref_release_raw(&rc, NULL);
171*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "release");
172*43a90889SApple OSS Distributions 
173*43a90889SApple OSS Distributions 	T_ASSERT_FALSE(os_ref_retain_try_raw(&rc, NULL), "try failed");
174*43a90889SApple OSS Distributions }
175*43a90889SApple OSS Distributions 
176*43a90889SApple OSS Distributions T_DECL(refcnt_locked, "Locked refcount")
177*43a90889SApple OSS Distributions {
178*43a90889SApple OSS Distributions 	struct os_refcnt rc;
179*43a90889SApple OSS Distributions 	os_ref_init(&rc, NULL);
180*43a90889SApple OSS Distributions 
181*43a90889SApple OSS Distributions 	os_ref_retain_locked(&rc);
182*43a90889SApple OSS Distributions 	os_ref_retain_locked(&rc);
183*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
184*43a90889SApple OSS Distributions 
185*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release_locked(&rc);
186*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
187*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
188*43a90889SApple OSS Distributions 
189*43a90889SApple OSS Distributions 	(void)os_ref_release_locked(&rc);
190*43a90889SApple OSS Distributions 	x = os_ref_release_locked(&rc);
191*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
192*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
193*43a90889SApple OSS Distributions }
194*43a90889SApple OSS Distributions 
195*43a90889SApple OSS Distributions T_DECL(refcnt_raw_locked, "Locked raw refcount")
196*43a90889SApple OSS Distributions {
197*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
198*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
199*43a90889SApple OSS Distributions 
200*43a90889SApple OSS Distributions 	os_ref_retain_locked_raw(&rc, NULL);
201*43a90889SApple OSS Distributions 	os_ref_retain_locked_raw(&rc, NULL);
202*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
203*43a90889SApple OSS Distributions 
204*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release_locked_raw(&rc, NULL);
205*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
206*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
207*43a90889SApple OSS Distributions 
208*43a90889SApple OSS Distributions 	(void)os_ref_release_locked_raw(&rc, NULL);
209*43a90889SApple OSS Distributions 	x = os_ref_release_locked_raw(&rc, NULL);
210*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
211*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
212*43a90889SApple OSS Distributions }
213*43a90889SApple OSS Distributions 
214*43a90889SApple OSS Distributions static void
do_bitwise_test(const os_ref_count_t bits)215*43a90889SApple OSS Distributions do_bitwise_test(const os_ref_count_t bits)
216*43a90889SApple OSS Distributions {
217*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
218*43a90889SApple OSS Distributions 	os_ref_count_t reserved = 0xaaaaaaaaU & ((1U << bits) - 1);
219*43a90889SApple OSS Distributions 
220*43a90889SApple OSS Distributions 	T_LOG("do_bitwise_test(nbits:%d, reserved:%#x)", bits, reserved);
221*43a90889SApple OSS Distributions 
222*43a90889SApple OSS Distributions 	os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
223*43a90889SApple OSS Distributions 
224*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "[%u bits] refcount initialized", bits);
225*43a90889SApple OSS Distributions 
226*43a90889SApple OSS Distributions 	os_ref_retain_mask(&rc, bits, NULL);
227*43a90889SApple OSS Distributions 	os_ref_retain_mask(&rc, bits, NULL);
228*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 3, "retain increased count");
229*43a90889SApple OSS Distributions 
230*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release_mask(&rc, bits, NULL);
231*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
232*43a90889SApple OSS Distributions 
233*43a90889SApple OSS Distributions 	os_ref_release_live_mask(&rc, bits, NULL);
234*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "release_live decreased count");
235*43a90889SApple OSS Distributions 
236*43a90889SApple OSS Distributions 	x = os_ref_release_mask(&rc, bits, NULL);
237*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "released");
238*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
239*43a90889SApple OSS Distributions 
240*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(rc & ((1U << bits) - 1), reserved, "Reserved bits not modified");
241*43a90889SApple OSS Distributions 
242*43a90889SApple OSS Distributions 	os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
243*43a90889SApple OSS Distributions 	T_ASSERT_TRUE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try retained");
244*43a90889SApple OSS Distributions 	if (reserved) {
245*43a90889SApple OSS Distributions 		T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, reserved, NULL), "try reject");
246*43a90889SApple OSS Distributions 	}
247*43a90889SApple OSS Distributions 
248*43a90889SApple OSS Distributions 	(void)os_ref_release_mask(&rc, bits, NULL);
249*43a90889SApple OSS Distributions 	(void)os_ref_release_mask(&rc, bits, NULL);
250*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "release");
251*43a90889SApple OSS Distributions 
252*43a90889SApple OSS Distributions 	T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try fail");
253*43a90889SApple OSS Distributions 
254*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_bits_mask(&rc, bits), reserved, "Reserved bits not modified");
255*43a90889SApple OSS Distributions }
256*43a90889SApple OSS Distributions 
257*43a90889SApple OSS Distributions T_DECL(refcnt_bitwise, "Bitwise refcount")
258*43a90889SApple OSS Distributions {
259*43a90889SApple OSS Distributions 	do_bitwise_test(0);
260*43a90889SApple OSS Distributions 	do_bitwise_test(1);
261*43a90889SApple OSS Distributions 	do_bitwise_test(8);
262*43a90889SApple OSS Distributions 	do_bitwise_test(26);
263*43a90889SApple OSS Distributions 
264*43a90889SApple OSS Distributions 	os_ref_atomic_t rc = 0xaaaaaaaa;
265*43a90889SApple OSS Distributions 
266*43a90889SApple OSS Distributions 	const os_ref_count_t nbits = 3;
267*43a90889SApple OSS Distributions 	const os_ref_count_t count = 5;
268*43a90889SApple OSS Distributions 	const os_ref_count_t bits = 7;
269*43a90889SApple OSS Distributions 	os_ref_init_count_mask(&rc, nbits, NULL, count, bits);
270*43a90889SApple OSS Distributions 
271*43a90889SApple OSS Distributions 	os_ref_count_t mask = (1U << nbits) - 1;
272*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(rc & mask, bits, "bits correctly initialized");
273*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(rc >> nbits, count, "count correctly initialized");
274*43a90889SApple OSS Distributions }
275*43a90889SApple OSS Distributions 
276*43a90889SApple OSS Distributions os_refgrp_decl(static, g1, "test group", NULL);
277*43a90889SApple OSS Distributions os_refgrp_decl_extern(g1);
278*43a90889SApple OSS Distributions 
279*43a90889SApple OSS Distributions T_DECL(refcnt_groups, "Group accounting")
280*43a90889SApple OSS Distributions {
281*43a90889SApple OSS Distributions #if OS_REFCNT_DEBUG
282*43a90889SApple OSS Distributions 	ref_debug_enable = true;
283*43a90889SApple OSS Distributions 
284*43a90889SApple OSS Distributions 	struct os_refcnt rc;
285*43a90889SApple OSS Distributions 	os_ref_init(&rc, &g1);
286*43a90889SApple OSS Distributions 
287*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_children, 1, "group attached");
288*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(global_ref_group.grp_children, 1, "global group attached");
289*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_count, 1, "group count");
290*43a90889SApple OSS Distributions 	T_ASSERT_EQ_ULLONG(g1.grp_retain_total, 1ULL, "group retains");
291*43a90889SApple OSS Distributions 	T_ASSERT_EQ_ULLONG(g1.grp_release_total, 0ULL, "group releases");
292*43a90889SApple OSS Distributions 
293*43a90889SApple OSS Distributions 	os_ref_retain(&rc);
294*43a90889SApple OSS Distributions 	os_ref_retain(&rc);
295*43a90889SApple OSS Distributions 	os_ref_release_live(&rc);
296*43a90889SApple OSS Distributions 	os_ref_release_live(&rc);
297*43a90889SApple OSS Distributions 
298*43a90889SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(g1.grp_retain_total, 3ULL, "group retains");
299*43a90889SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(g1.grp_release_total, 2ULL, "group releases");
300*43a90889SApple OSS Distributions 
301*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release(&rc);
302*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(x, 0, "released");
303*43a90889SApple OSS Distributions 
304*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_children, 0, "group detatched");
305*43a90889SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_count, 0, "group count");
306*43a90889SApple OSS Distributions #else
307*43a90889SApple OSS Distributions 	T_SKIP("Refcount debugging disabled");
308*43a90889SApple OSS Distributions #endif
309*43a90889SApple OSS Distributions }
310*43a90889SApple OSS Distributions 
311*43a90889SApple OSS Distributions enum {
312*43a90889SApple OSS Distributions 	OSREF_UNDERFLOW    = 1,
313*43a90889SApple OSS Distributions 	OSREF_OVERFLOW     = 2,
314*43a90889SApple OSS Distributions 	OSREF_RETAIN       = 3,
315*43a90889SApple OSS Distributions 	OSREF_DEALLOC_LIVE = 4,
316*43a90889SApple OSS Distributions };
317*43a90889SApple OSS Distributions 
318*43a90889SApple OSS Distributions static jmp_buf jb;
319*43a90889SApple OSS Distributions static bool expect_panic = false;
320*43a90889SApple OSS Distributions 
321*43a90889SApple OSS Distributions void
handle_panic(const char * func,char * __unused str,...)322*43a90889SApple OSS Distributions handle_panic(const char *func, char *__unused str, ...)
323*43a90889SApple OSS Distributions {
324*43a90889SApple OSS Distributions 	int ret = -1;
325*43a90889SApple OSS Distributions 	if (!expect_panic) {
326*43a90889SApple OSS Distributions 		T_FAIL("unexpected panic from %s", func);
327*43a90889SApple OSS Distributions 		T_LOG("corrupt program state, aborting");
328*43a90889SApple OSS Distributions 		abort();
329*43a90889SApple OSS Distributions 	}
330*43a90889SApple OSS Distributions 	expect_panic = false;
331*43a90889SApple OSS Distributions 
332*43a90889SApple OSS Distributions 	if (strcmp(func, "os_ref_panic_underflow") == 0) {
333*43a90889SApple OSS Distributions 		ret = OSREF_UNDERFLOW;
334*43a90889SApple OSS Distributions 	} else if (strcmp(func, "os_ref_panic_overflow") == 0) {
335*43a90889SApple OSS Distributions 		ret = OSREF_OVERFLOW;
336*43a90889SApple OSS Distributions 	} else if (strcmp(func, "os_ref_panic_retain") == 0) {
337*43a90889SApple OSS Distributions 		ret = OSREF_RETAIN;
338*43a90889SApple OSS Distributions 	} else if (strcmp(func, "os_ref_panic_live") == 0) {
339*43a90889SApple OSS Distributions 		ret = OSREF_DEALLOC_LIVE;
340*43a90889SApple OSS Distributions 	} else {
341*43a90889SApple OSS Distributions 		T_LOG("unexpected panic from %s", func);
342*43a90889SApple OSS Distributions 	}
343*43a90889SApple OSS Distributions 
344*43a90889SApple OSS Distributions 	longjmp(jb, ret);
345*43a90889SApple OSS Distributions }
346*43a90889SApple OSS Distributions 
347*43a90889SApple OSS Distributions T_DECL(refcnt_underflow, "Underflow")
348*43a90889SApple OSS Distributions {
349*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
350*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
351*43a90889SApple OSS Distributions 	(void)os_ref_release_raw(&rc, NULL);
352*43a90889SApple OSS Distributions 
353*43a90889SApple OSS Distributions 	int x = setjmp(jb);
354*43a90889SApple OSS Distributions 	if (x == 0) {
355*43a90889SApple OSS Distributions 		expect_panic = true;
356*43a90889SApple OSS Distributions 		(void)os_ref_release_raw(&rc, NULL);
357*43a90889SApple OSS Distributions 		T_FAIL("underflow not caught");
358*43a90889SApple OSS Distributions 	} else {
359*43a90889SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_UNDERFLOW, "underflow caught");
360*43a90889SApple OSS Distributions 	}
361*43a90889SApple OSS Distributions }
362*43a90889SApple OSS Distributions 
363*43a90889SApple OSS Distributions T_DECL(refcnt_overflow, "Overflow")
364*43a90889SApple OSS Distributions {
365*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
366*43a90889SApple OSS Distributions 	os_ref_init_count_raw(&rc, NULL, 0x0fffffffU);
367*43a90889SApple OSS Distributions 
368*43a90889SApple OSS Distributions 	int x = setjmp(jb);
369*43a90889SApple OSS Distributions 	if (x == 0) {
370*43a90889SApple OSS Distributions 		expect_panic = true;
371*43a90889SApple OSS Distributions 		(void)os_ref_retain_raw(&rc, NULL);
372*43a90889SApple OSS Distributions 		T_FAIL("overflow not caught");
373*43a90889SApple OSS Distributions 	} else {
374*43a90889SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_RETAIN, "overflow caught");
375*43a90889SApple OSS Distributions 	}
376*43a90889SApple OSS Distributions }
377*43a90889SApple OSS Distributions 
378*43a90889SApple OSS Distributions T_DECL(refcnt_resurrection, "Resurrection")
379*43a90889SApple OSS Distributions {
380*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
381*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
382*43a90889SApple OSS Distributions 	os_ref_count_t n = os_ref_release_raw(&rc, NULL);
383*43a90889SApple OSS Distributions 
384*43a90889SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ_UINT(n, 0, "reference not released");
385*43a90889SApple OSS Distributions 
386*43a90889SApple OSS Distributions 	int x = setjmp(jb);
387*43a90889SApple OSS Distributions 	if (x == 0) {
388*43a90889SApple OSS Distributions 		expect_panic = true;
389*43a90889SApple OSS Distributions 		(void)os_ref_retain_raw(&rc, NULL);
390*43a90889SApple OSS Distributions 		T_FAIL("resurrection not caught");
391*43a90889SApple OSS Distributions 	} else {
392*43a90889SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_RETAIN, "resurrection caught");
393*43a90889SApple OSS Distributions 	}
394*43a90889SApple OSS Distributions }
395*43a90889SApple OSS Distributions 
396*43a90889SApple OSS Distributions T_DECL(refcnt_dealloc_live, "Dealloc expected live object")
397*43a90889SApple OSS Distributions {
398*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
399*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
400*43a90889SApple OSS Distributions 
401*43a90889SApple OSS Distributions 	expect_panic = true;
402*43a90889SApple OSS Distributions 	int x = setjmp(jb);
403*43a90889SApple OSS Distributions 	if (x == 0) {
404*43a90889SApple OSS Distributions 		expect_panic = true;
405*43a90889SApple OSS Distributions 		os_ref_release_live_raw(&rc, NULL);
406*43a90889SApple OSS Distributions 		T_FAIL("dealloc live not caught");
407*43a90889SApple OSS Distributions 	} else {
408*43a90889SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_DEALLOC_LIVE, "dealloc live caught");
409*43a90889SApple OSS Distributions 	}
410*43a90889SApple OSS Distributions }
411*43a90889SApple OSS Distributions 
412*43a90889SApple OSS Distributions T_DECL(refcnt_initializer, "Static intializers")
413*43a90889SApple OSS Distributions {
414*43a90889SApple OSS Distributions 	struct os_refcnt rc = OS_REF_INITIALIZER;
415*43a90889SApple OSS Distributions 	os_ref_atomic_t rca = OS_REF_ATOMIC_INITIALIZER;
416*43a90889SApple OSS Distributions 
417*43a90889SApple OSS Distributions 	T_ASSERT_EQ_INT(0, os_ref_retain_try(&rc), NULL);
418*43a90889SApple OSS Distributions 	T_ASSERT_EQ_INT(0, os_ref_get_count_raw(&rca), NULL);
419*43a90889SApple OSS Distributions }
420*43a90889SApple OSS Distributions 
421*43a90889SApple OSS Distributions #if STRESS_TESTS
422*43a90889SApple OSS Distributions 
423*43a90889SApple OSS Distributions static unsigned pcpu_perf_step = 0;
424*43a90889SApple OSS Distributions 
425*43a90889SApple OSS Distributions static void
worker_ref(os_ref_atomic_t * rc,unsigned long * count)426*43a90889SApple OSS Distributions worker_ref(os_ref_atomic_t *rc, unsigned long *count)
427*43a90889SApple OSS Distributions {
428*43a90889SApple OSS Distributions 	unsigned long n = 0;
429*43a90889SApple OSS Distributions 
430*43a90889SApple OSS Distributions 	while (os_atomic_load(&pcpu_perf_step, relaxed) == 0) {
431*43a90889SApple OSS Distributions 	}
432*43a90889SApple OSS Distributions 
433*43a90889SApple OSS Distributions 	while (os_atomic_load(&pcpu_perf_step, relaxed) == 1) {
434*43a90889SApple OSS Distributions 		os_ref_retain_raw(rc, NULL);
435*43a90889SApple OSS Distributions 		os_ref_release_live_raw(rc, NULL);
436*43a90889SApple OSS Distributions 		n++;
437*43a90889SApple OSS Distributions 	}
438*43a90889SApple OSS Distributions 
439*43a90889SApple OSS Distributions 	os_atomic_add(count, n, relaxed);
440*43a90889SApple OSS Distributions }
441*43a90889SApple OSS Distributions 
442*43a90889SApple OSS Distributions static void
worker_pcpu_ref(os_pcpu_ref_t rc,unsigned long * count)443*43a90889SApple OSS Distributions worker_pcpu_ref(os_pcpu_ref_t rc, unsigned long *count)
444*43a90889SApple OSS Distributions {
445*43a90889SApple OSS Distributions 	unsigned long n = 0;
446*43a90889SApple OSS Distributions 
447*43a90889SApple OSS Distributions 	while (os_atomic_load(&pcpu_perf_step, relaxed) == 0) {
448*43a90889SApple OSS Distributions 	}
449*43a90889SApple OSS Distributions 
450*43a90889SApple OSS Distributions 	while (os_atomic_load(&pcpu_perf_step, relaxed) == 1) {
451*43a90889SApple OSS Distributions 		os_pcpu_ref_retain(rc, NULL);
452*43a90889SApple OSS Distributions 		os_pcpu_ref_release_live(rc, NULL);
453*43a90889SApple OSS Distributions 		n++;
454*43a90889SApple OSS Distributions 	}
455*43a90889SApple OSS Distributions 
456*43a90889SApple OSS Distributions 	os_atomic_add(count, n, relaxed);
457*43a90889SApple OSS Distributions }
458*43a90889SApple OSS Distributions 
459*43a90889SApple OSS Distributions #define PCPU_BENCH_LEN   2
460*43a90889SApple OSS Distributions 
461*43a90889SApple OSS Distributions static void
warmup_thread_pool(dispatch_group_t g,dispatch_queue_t rq)462*43a90889SApple OSS Distributions warmup_thread_pool(dispatch_group_t g, dispatch_queue_t rq)
463*43a90889SApple OSS Distributions {
464*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 1, relaxed);
465*43a90889SApple OSS Distributions 
466*43a90889SApple OSS Distributions 	zpercpu_foreach_cpu(cpu) {
467*43a90889SApple OSS Distributions 		dispatch_group_async(g, rq, ^{
468*43a90889SApple OSS Distributions 			while (os_atomic_load(&pcpu_perf_step, relaxed) == 1) {
469*43a90889SApple OSS Distributions 			}
470*43a90889SApple OSS Distributions 		});
471*43a90889SApple OSS Distributions 	}
472*43a90889SApple OSS Distributions 
473*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 0, relaxed);
474*43a90889SApple OSS Distributions 	dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
475*43a90889SApple OSS Distributions }
476*43a90889SApple OSS Distributions 
477*43a90889SApple OSS Distributions T_DECL(pcpu_perf, "Performance per-cpu")
478*43a90889SApple OSS Distributions {
479*43a90889SApple OSS Distributions 	os_ref_atomic_t rc;
480*43a90889SApple OSS Distributions 	os_pcpu_ref_t prc;
481*43a90889SApple OSS Distributions 	__block unsigned long count = 0;
482*43a90889SApple OSS Distributions 	double scale = PCPU_BENCH_LEN * 1e6;
483*43a90889SApple OSS Distributions 	dispatch_queue_t rq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
484*43a90889SApple OSS Distributions 	dispatch_group_t g = dispatch_group_create();
485*43a90889SApple OSS Distributions 
486*43a90889SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
487*43a90889SApple OSS Distributions 	os_pcpu_ref_init(&prc, NULL);
488*43a90889SApple OSS Distributions 
489*43a90889SApple OSS Distributions 	T_LOG("uncontended benchmark");
490*43a90889SApple OSS Distributions 
491*43a90889SApple OSS Distributions 	dispatch_group_async(g, rq, ^{
492*43a90889SApple OSS Distributions 		worker_ref(&rc, &count);
493*43a90889SApple OSS Distributions 	});
494*43a90889SApple OSS Distributions 
495*43a90889SApple OSS Distributions 	count = 0;
496*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 1, relaxed);
497*43a90889SApple OSS Distributions 	sleep(PCPU_BENCH_LEN);
498*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 0, relaxed);
499*43a90889SApple OSS Distributions 	dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
500*43a90889SApple OSS Distributions 
501*43a90889SApple OSS Distributions 	T_PASS("%.2fM rounds per thread per second (atomic)", count / scale);
502*43a90889SApple OSS Distributions 
503*43a90889SApple OSS Distributions 	dispatch_group_async(g, rq, ^{
504*43a90889SApple OSS Distributions 		worker_pcpu_ref(prc, &count);
505*43a90889SApple OSS Distributions 	});
506*43a90889SApple OSS Distributions 
507*43a90889SApple OSS Distributions 	count = 0;
508*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 1, relaxed);
509*43a90889SApple OSS Distributions 	sleep(PCPU_BENCH_LEN);
510*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 0, relaxed);
511*43a90889SApple OSS Distributions 	dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
512*43a90889SApple OSS Distributions 
513*43a90889SApple OSS Distributions 	T_PASS("%.2fM rounds per thread per second (pcpu)", count / scale);
514*43a90889SApple OSS Distributions 
515*43a90889SApple OSS Distributions 	T_LOG("contended benchmark");
516*43a90889SApple OSS Distributions 
517*43a90889SApple OSS Distributions 	warmup_thread_pool(g, rq);
zpercpu_foreach_cpu(cpu)518*43a90889SApple OSS Distributions 	zpercpu_foreach_cpu(cpu) {
519*43a90889SApple OSS Distributions 		dispatch_group_async(g, rq, ^{
520*43a90889SApple OSS Distributions 			worker_ref(&rc, &count);
521*43a90889SApple OSS Distributions 		});
522*43a90889SApple OSS Distributions 	}
523*43a90889SApple OSS Distributions 
524*43a90889SApple OSS Distributions 	count = 0;
525*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 1, relaxed);
526*43a90889SApple OSS Distributions 	sleep(PCPU_BENCH_LEN);
527*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 0, relaxed);
528*43a90889SApple OSS Distributions 	dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
529*43a90889SApple OSS Distributions 
530*43a90889SApple OSS Distributions 	T_PASS("%.2fM rounds per thread per second (atomic)", count / (zpercpu_count() * scale));
531*43a90889SApple OSS Distributions 
532*43a90889SApple OSS Distributions 	warmup_thread_pool(g, rq);
zpercpu_foreach_cpu(cpu)533*43a90889SApple OSS Distributions 	zpercpu_foreach_cpu(cpu) {
534*43a90889SApple OSS Distributions 		dispatch_group_async(g, rq, ^{
535*43a90889SApple OSS Distributions 			worker_pcpu_ref(prc, &count);
536*43a90889SApple OSS Distributions 		});
537*43a90889SApple OSS Distributions 	}
538*43a90889SApple OSS Distributions 
539*43a90889SApple OSS Distributions 	count = 0;
540*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 1, relaxed);
541*43a90889SApple OSS Distributions 	sleep(PCPU_BENCH_LEN);
542*43a90889SApple OSS Distributions 	os_atomic_store(&pcpu_perf_step, 0, relaxed);
543*43a90889SApple OSS Distributions 	dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
544*43a90889SApple OSS Distributions 
545*43a90889SApple OSS Distributions 	T_PASS("%.2fM rounds per thread per second (pcpu)", count / (zpercpu_count() * scale));
546*43a90889SApple OSS Distributions 
547*43a90889SApple OSS Distributions 	(void)os_pcpu_ref_kill(prc, NULL);
548*43a90889SApple OSS Distributions 	os_pcpu_ref_destroy(&prc, NULL);
549*43a90889SApple OSS Distributions }
550*43a90889SApple OSS Distributions 
551*43a90889SApple OSS Distributions static const unsigned long iters = 1024 * 1024 * 32;
552*43a90889SApple OSS Distributions 
553*43a90889SApple OSS Distributions static void *
func(void * _rc)554*43a90889SApple OSS Distributions func(void *_rc)
555*43a90889SApple OSS Distributions {
556*43a90889SApple OSS Distributions 	struct os_refcnt *rc = _rc;
557*43a90889SApple OSS Distributions 	for (unsigned long i = 0; i < iters; i++) {
558*43a90889SApple OSS Distributions 		os_ref_retain(rc);
559*43a90889SApple OSS Distributions 		os_ref_release_live(rc);
560*43a90889SApple OSS Distributions 	}
561*43a90889SApple OSS Distributions 	return NULL;
562*43a90889SApple OSS Distributions }
563*43a90889SApple OSS Distributions 
564*43a90889SApple OSS Distributions T_DECL(refcnt_stress, "Stress test")
565*43a90889SApple OSS Distributions {
566*43a90889SApple OSS Distributions 	pthread_t th1, th2;
567*43a90889SApple OSS Distributions 
568*43a90889SApple OSS Distributions 	struct os_refcnt rc;
569*43a90889SApple OSS Distributions 	os_ref_init(&rc, NULL);
570*43a90889SApple OSS Distributions 
571*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&th1, NULL, func, &rc), "pthread_create");
572*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&th2, NULL, func, &rc), "pthread_create");
573*43a90889SApple OSS Distributions 
574*43a90889SApple OSS Distributions 	void *r1, *r2;
575*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_join(th1, &r1), "pthread_join");
576*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_join(th2, &r2), "pthread_join");
577*43a90889SApple OSS Distributions 
578*43a90889SApple OSS Distributions 	os_ref_count_t x = os_ref_release(&rc);
579*43a90889SApple OSS Distributions 	T_ASSERT_EQ_INT(x, 0, "Consistent refcount");
580*43a90889SApple OSS Distributions }
581*43a90889SApple OSS Distributions 
582*43a90889SApple OSS Distributions #endif
583