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