xref: /xnu-8796.141.3/tests/select_stress.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions 
3*1b191cb5SApple OSS Distributions #include <errno.h>
4*1b191cb5SApple OSS Distributions #include <pthread.h>
5*1b191cb5SApple OSS Distributions #include <stdio.h>
6*1b191cb5SApple OSS Distributions #include <stdlib.h>
7*1b191cb5SApple OSS Distributions #include <unistd.h>
8*1b191cb5SApple OSS Distributions 
9*1b191cb5SApple OSS Distributions #include <mach/mach.h>
10*1b191cb5SApple OSS Distributions #include <mach/mach_time.h>
11*1b191cb5SApple OSS Distributions #include <mach/semaphore.h>
12*1b191cb5SApple OSS Distributions #include <sys/select.h>
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions /* Select parameters */
15*1b191cb5SApple OSS Distributions #define TIMEOUT_CHANCE          17      /* one in this many times, timeout */
16*1b191cb5SApple OSS Distributions #define TIMEOUT_POLLCHANCE      11      /* one in this many is a poll */
17*1b191cb5SApple OSS Distributions #define TIMEOUT_SCALE           5       /* microseconds multiplier */
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions static semaphore_t g_thread_sem;
20*1b191cb5SApple OSS Distributions static semaphore_t g_sync_sem;
21*1b191cb5SApple OSS Distributions 
22*1b191cb5SApple OSS Distributions struct endpoint {
23*1b191cb5SApple OSS Distributions 	int       fd[4];
24*1b191cb5SApple OSS Distributions 	pthread_t pth;
25*1b191cb5SApple OSS Distributions };
26*1b191cb5SApple OSS Distributions 
27*1b191cb5SApple OSS Distributions typedef void * (*thread_func)(struct endpoint *ep);
28*1b191cb5SApple OSS Distributions typedef void   (*setup_func)(struct endpoint *ep);
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions struct thread_sync_arg {
31*1b191cb5SApple OSS Distributions 	struct endpoint ep;
32*1b191cb5SApple OSS Distributions 	setup_func  setup;
33*1b191cb5SApple OSS Distributions 	thread_func work;
34*1b191cb5SApple OSS Distributions };
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions static mach_timebase_info_data_t g_timebase;
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions static int g_sleep_iterations = 150000;
39*1b191cb5SApple OSS Distributions static int g_sleep_usecs = 30;
40*1b191cb5SApple OSS Distributions static int g_stress_nthreads = 100;
41*1b191cb5SApple OSS Distributions static uint64_t g_stress_duration = 5;
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions static inline uint64_t
ns_to_abs(uint64_t ns)44*1b191cb5SApple OSS Distributions ns_to_abs(uint64_t ns)
45*1b191cb5SApple OSS Distributions {
46*1b191cb5SApple OSS Distributions 	return ns * g_timebase.denom / g_timebase.numer;
47*1b191cb5SApple OSS Distributions }
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions static inline uint64_t
abs_to_ns(uint64_t abs)50*1b191cb5SApple OSS Distributions abs_to_ns(uint64_t abs)
51*1b191cb5SApple OSS Distributions {
52*1b191cb5SApple OSS Distributions 	return abs * g_timebase.numer / g_timebase.denom;
53*1b191cb5SApple OSS Distributions }
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions 
57*1b191cb5SApple OSS Distributions /*
58*1b191cb5SApple OSS Distributions  * Synchronize the startup / initialization of a set of threads
59*1b191cb5SApple OSS Distributions  */
60*1b191cb5SApple OSS Distributions static void *
thread_sync(void * ctx)61*1b191cb5SApple OSS Distributions thread_sync(void *ctx)
62*1b191cb5SApple OSS Distributions {
63*1b191cb5SApple OSS Distributions 	struct thread_sync_arg *a = (struct thread_sync_arg *)ctx;
64*1b191cb5SApple OSS Distributions 	T_QUIET;
65*1b191cb5SApple OSS Distributions 	T_ASSERT_TRUE(((a != NULL) && (a->work != NULL)), "thread setup error");
66*1b191cb5SApple OSS Distributions 
67*1b191cb5SApple OSS Distributions 	if (a->setup) {
68*1b191cb5SApple OSS Distributions 		(a->setup)(&a->ep);
69*1b191cb5SApple OSS Distributions 	}
70*1b191cb5SApple OSS Distributions 
71*1b191cb5SApple OSS Distributions 	semaphore_wait_signal(g_thread_sem, g_sync_sem);
72*1b191cb5SApple OSS Distributions 	return (a->work)(&a->ep);
73*1b191cb5SApple OSS Distributions }
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions struct select_stress_args {
76*1b191cb5SApple OSS Distributions 	struct endpoint *ep;
77*1b191cb5SApple OSS Distributions 	int nthreads;
78*1b191cb5SApple OSS Distributions };
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions static void
setup_stress_event(struct endpoint * ep)81*1b191cb5SApple OSS Distributions setup_stress_event(struct endpoint *ep)
82*1b191cb5SApple OSS Distributions {
83*1b191cb5SApple OSS Distributions 	T_QUIET;
84*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO;
85*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pipe(&ep->fd[0]), "pipe()");
86*1b191cb5SApple OSS Distributions 
87*1b191cb5SApple OSS Distributions 	T_LOG("th[0x%lx]: fd:{%d,%d}, ep@%p",
88*1b191cb5SApple OSS Distributions 	    (uintptr_t)pthread_self(), ep->fd[0], ep->fd[1], (void *)ep);
89*1b191cb5SApple OSS Distributions }
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions /*
92*1b191cb5SApple OSS Distributions  * Cause file descriptors to be reused/replaced.  We expect that it will at
93*1b191cb5SApple OSS Distributions  * least take the lowest fd as part of the descriptor list.  This may be
94*1b191cb5SApple OSS Distributions  * optimistic, but it shows replacing an fd out from under a select() if it
95*1b191cb5SApple OSS Distributions  * happens.
96*1b191cb5SApple OSS Distributions  *
97*1b191cb5SApple OSS Distributions  * We potentially delay the open for a random amount of time so that another
98*1b191cb5SApple OSS Distributions  * thread can come in and wake up the fd_set with a bad (closed) fd in the set.
99*1b191cb5SApple OSS Distributions  */
100*1b191cb5SApple OSS Distributions static void
recycle_fds(struct endpoint * ep)101*1b191cb5SApple OSS Distributions recycle_fds(struct endpoint *ep)
102*1b191cb5SApple OSS Distributions {
103*1b191cb5SApple OSS Distributions 	/* close endpoint descriptors in random order */
104*1b191cb5SApple OSS Distributions 	if (random() % 1) {
105*1b191cb5SApple OSS Distributions 		close(ep->fd[0]);
106*1b191cb5SApple OSS Distributions 		close(ep->fd[1]);
107*1b191cb5SApple OSS Distributions 	} else {
108*1b191cb5SApple OSS Distributions 		close(ep->fd[1]);
109*1b191cb5SApple OSS Distributions 		close(ep->fd[0]);
110*1b191cb5SApple OSS Distributions 	}
111*1b191cb5SApple OSS Distributions 
112*1b191cb5SApple OSS Distributions 	/* randomize a delay */
113*1b191cb5SApple OSS Distributions 	if ((random() % ep->fd[0]) == 0) {
114*1b191cb5SApple OSS Distributions 		usleep(((random() % ep->fd[1]) + 1) * ep->fd[1]);
115*1b191cb5SApple OSS Distributions 	}
116*1b191cb5SApple OSS Distributions 
117*1b191cb5SApple OSS Distributions 	/* reopen the FDs, hopefully in the middle of select() */
118*1b191cb5SApple OSS Distributions 	T_QUIET;
119*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO;
120*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pipe(&ep->fd[0]), "pipe");
121*1b191cb5SApple OSS Distributions }
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions 
124*1b191cb5SApple OSS Distributions /*
125*1b191cb5SApple OSS Distributions  * Send a byte of data down the thread end of a pipe to wake up the select
126*1b191cb5SApple OSS Distributions  * on the other end of it.  Select will wake up normally because of this,
127*1b191cb5SApple OSS Distributions  * and read the byte out. Hopefully, another thread has closed/reopened its FDs.
128*1b191cb5SApple OSS Distributions  */
129*1b191cb5SApple OSS Distributions static void
write_data(struct endpoint * ep)130*1b191cb5SApple OSS Distributions write_data(struct endpoint *ep)
131*1b191cb5SApple OSS Distributions {
132*1b191cb5SApple OSS Distributions 	T_QUIET;
133*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO;
134*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(write(ep->fd[1], "X", 1), "th[0x%lx] write_data(fd=%d)",
135*1b191cb5SApple OSS Distributions 	    (uintptr_t)pthread_self(), ep->fd[1]);
136*1b191cb5SApple OSS Distributions }
137*1b191cb5SApple OSS Distributions 
138*1b191cb5SApple OSS Distributions static void *
do_stress_events(struct endpoint * ep)139*1b191cb5SApple OSS Distributions do_stress_events(struct endpoint *ep)
140*1b191cb5SApple OSS Distributions {
141*1b191cb5SApple OSS Distributions 	unsigned write_freq = (unsigned)(((uintptr_t)pthread_self() & 0xff0000) >> 16);
142*1b191cb5SApple OSS Distributions 
143*1b191cb5SApple OSS Distributions 	/* some default */
144*1b191cb5SApple OSS Distributions 	if (write_freq == 0) {
145*1b191cb5SApple OSS Distributions 		write_freq = 31;
146*1b191cb5SApple OSS Distributions 	}
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions 	T_LOG("th[0x%lx] write_freq:%d", (uintptr_t)pthread_self(), write_freq);
149*1b191cb5SApple OSS Distributions 
150*1b191cb5SApple OSS Distributions 	for (;;) {
151*1b191cb5SApple OSS Distributions 		/* randomized delay between events */
152*1b191cb5SApple OSS Distributions 		usleep(((random() % ep->fd[1]) + 1) * ep->fd[1]);
153*1b191cb5SApple OSS Distributions 
154*1b191cb5SApple OSS Distributions 		if ((random() % write_freq) == 0) {
155*1b191cb5SApple OSS Distributions 			write_data(ep);
156*1b191cb5SApple OSS Distributions 		} else {
157*1b191cb5SApple OSS Distributions 			recycle_fds(ep);
158*1b191cb5SApple OSS Distributions 		}
159*1b191cb5SApple OSS Distributions 	}
160*1b191cb5SApple OSS Distributions }
161*1b191cb5SApple OSS Distributions 
162*1b191cb5SApple OSS Distributions struct selarg {
163*1b191cb5SApple OSS Distributions 	struct thread_sync_arg *th;
164*1b191cb5SApple OSS Distributions 	fd_set  def_readfds;
165*1b191cb5SApple OSS Distributions 	int max_fd;
166*1b191cb5SApple OSS Distributions 	int nthreads;
167*1b191cb5SApple OSS Distributions 	int ret;
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions 	pthread_t pth;
170*1b191cb5SApple OSS Distributions };
171*1b191cb5SApple OSS Distributions 
172*1b191cb5SApple OSS Distributions /*
173*1b191cb5SApple OSS Distributions  * Put the actual call to select in its own thread so we can catch errors that
174*1b191cb5SApple OSS Distributions  * occur only the first time a thread calls select.
175*1b191cb5SApple OSS Distributions  */
176*1b191cb5SApple OSS Distributions static void *
do_select(void * arg)177*1b191cb5SApple OSS Distributions do_select(void *arg)
178*1b191cb5SApple OSS Distributions {
179*1b191cb5SApple OSS Distributions 	struct selarg *sarg = (struct selarg *)arg;
180*1b191cb5SApple OSS Distributions 	struct timeval timeout;
181*1b191cb5SApple OSS Distributions 	struct timeval *tp = NULL;
182*1b191cb5SApple OSS Distributions 	fd_set  readfds;
183*1b191cb5SApple OSS Distributions 	int nfd;
184*1b191cb5SApple OSS Distributions 
185*1b191cb5SApple OSS Distributions 	sarg->ret = 0;
186*1b191cb5SApple OSS Distributions 
187*1b191cb5SApple OSS Distributions 	FD_COPY(&sarg->def_readfds, &readfds);
188*1b191cb5SApple OSS Distributions 
189*1b191cb5SApple OSS Distributions 	/* Add a timeout probablistically */
190*1b191cb5SApple OSS Distributions 	if ((random() % TIMEOUT_CHANCE) == 0) {
191*1b191cb5SApple OSS Distributions 		timeout.tv_sec = random() % 1;
192*1b191cb5SApple OSS Distributions 		timeout.tv_usec = ((random() % TIMEOUT_POLLCHANCE) * TIMEOUT_SCALE);
193*1b191cb5SApple OSS Distributions 		tp = &timeout;
194*1b191cb5SApple OSS Distributions 	}
195*1b191cb5SApple OSS Distributions 
196*1b191cb5SApple OSS Distributions 	/* Do the select */
197*1b191cb5SApple OSS Distributions 	nfd = select(sarg->max_fd + 1, &readfds, 0, 0, tp);
198*1b191cb5SApple OSS Distributions 	if (nfd < 0) {
199*1b191cb5SApple OSS Distributions 		/* EBADF: fd_set has changed */
200*1b191cb5SApple OSS Distributions 		if (errno == EBADF) {
201*1b191cb5SApple OSS Distributions 			sarg->ret = EBADF;
202*1b191cb5SApple OSS Distributions 			return NULL;
203*1b191cb5SApple OSS Distributions 		}
204*1b191cb5SApple OSS Distributions 
205*1b191cb5SApple OSS Distributions 		/* Other errors are fatal */
206*1b191cb5SApple OSS Distributions 		T_QUIET;
207*1b191cb5SApple OSS Distributions 		T_WITH_ERRNO;
208*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(nfd, "select:stress");
209*1b191cb5SApple OSS Distributions 	}
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 	/* Fast: handle timeouts */
212*1b191cb5SApple OSS Distributions 	if (nfd == 0) {
213*1b191cb5SApple OSS Distributions 		return NULL;
214*1b191cb5SApple OSS Distributions 	}
215*1b191cb5SApple OSS Distributions 
216*1b191cb5SApple OSS Distributions 	/* Slower: discard read input thrown at us from threads */
217*1b191cb5SApple OSS Distributions 	for (int i = 0; i < sarg->nthreads; i++) {
218*1b191cb5SApple OSS Distributions 		struct endpoint *ep = &sarg->th[i].ep;
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions 		if (FD_ISSET(ep->fd[0], &readfds)) {
221*1b191cb5SApple OSS Distributions 			char c;
222*1b191cb5SApple OSS Distributions 			(void)read(ep->fd[0], &c, 1);
223*1b191cb5SApple OSS Distributions 		}
224*1b191cb5SApple OSS Distributions 	}
225*1b191cb5SApple OSS Distributions 
226*1b191cb5SApple OSS Distributions 	return NULL;
227*1b191cb5SApple OSS Distributions }
228*1b191cb5SApple OSS Distributions 
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions static void
test_select_stress(int nthreads,uint64_t duration_seconds)231*1b191cb5SApple OSS Distributions test_select_stress(int nthreads, uint64_t duration_seconds)
232*1b191cb5SApple OSS Distributions {
233*1b191cb5SApple OSS Distributions 	uint64_t deadline;
234*1b191cb5SApple OSS Distributions 	uint64_t seconds_remain, last_print_time;
235*1b191cb5SApple OSS Distributions 
236*1b191cb5SApple OSS Distributions 	struct selarg sarg;
237*1b191cb5SApple OSS Distributions 
238*1b191cb5SApple OSS Distributions 	int started_threads = 0;
239*1b191cb5SApple OSS Distributions 	struct thread_sync_arg *th;
240*1b191cb5SApple OSS Distributions 
241*1b191cb5SApple OSS Distributions 	if (nthreads < 2) {
242*1b191cb5SApple OSS Distributions 		T_LOG("forcing a minimum of 2 threads");
243*1b191cb5SApple OSS Distributions 		nthreads = 2;
244*1b191cb5SApple OSS Distributions 	}
245*1b191cb5SApple OSS Distributions 
246*1b191cb5SApple OSS Distributions 	/*
247*1b191cb5SApple OSS Distributions 	 * Allocate memory for endpoint data
248*1b191cb5SApple OSS Distributions 	 */
249*1b191cb5SApple OSS Distributions 	th = calloc(nthreads, sizeof(*th));
250*1b191cb5SApple OSS Distributions 	T_QUIET;
251*1b191cb5SApple OSS Distributions 	T_ASSERT_NOTNULL(th, "select_stress: No memory for thread endpoints");
252*1b191cb5SApple OSS Distributions 
253*1b191cb5SApple OSS Distributions 	T_LOG("Select stress test: %d threads, for %lld seconds", nthreads, duration_seconds);
254*1b191cb5SApple OSS Distributions 
255*1b191cb5SApple OSS Distributions 	/*
256*1b191cb5SApple OSS Distributions 	 * Startup all the threads
257*1b191cb5SApple OSS Distributions 	 */
258*1b191cb5SApple OSS Distributions 	T_LOG("\tcreating threads...");
259*1b191cb5SApple OSS Distributions 	for (int i = 0; i < nthreads; i++) {
260*1b191cb5SApple OSS Distributions 		struct endpoint *e = &th[i].ep;
261*1b191cb5SApple OSS Distributions 		th[i].setup = setup_stress_event;
262*1b191cb5SApple OSS Distributions 		th[i].work = do_stress_events;
263*1b191cb5SApple OSS Distributions 		T_QUIET;
264*1b191cb5SApple OSS Distributions 		T_WITH_ERRNO;
265*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_create(&e->pth, 0, thread_sync, &th[i]),
266*1b191cb5SApple OSS Distributions 		    "pthread_create:do_stress_events");
267*1b191cb5SApple OSS Distributions 	}
268*1b191cb5SApple OSS Distributions 
269*1b191cb5SApple OSS Distributions 	/*
270*1b191cb5SApple OSS Distributions 	 * Wait for all the threads to start up
271*1b191cb5SApple OSS Distributions 	 */
272*1b191cb5SApple OSS Distributions 	while (started_threads < nthreads) {
273*1b191cb5SApple OSS Distributions 		if (semaphore_wait(g_sync_sem) == KERN_SUCCESS) {
274*1b191cb5SApple OSS Distributions 			++started_threads;
275*1b191cb5SApple OSS Distributions 		}
276*1b191cb5SApple OSS Distributions 	}
277*1b191cb5SApple OSS Distributions 
278*1b191cb5SApple OSS Distributions 	/*
279*1b191cb5SApple OSS Distributions 	 * Kick everyone off
280*1b191cb5SApple OSS Distributions 	 */
281*1b191cb5SApple OSS Distributions 	semaphore_signal_all(g_thread_sem);
282*1b191cb5SApple OSS Distributions 
283*1b191cb5SApple OSS Distributions 	/*
284*1b191cb5SApple OSS Distributions 	 * Calculate a stop time
285*1b191cb5SApple OSS Distributions 	 */
286*1b191cb5SApple OSS Distributions 	deadline = mach_absolute_time() + ns_to_abs(duration_seconds * NSEC_PER_SEC);
287*1b191cb5SApple OSS Distributions 	seconds_remain = duration_seconds;
288*1b191cb5SApple OSS Distributions 	last_print_time = seconds_remain + 1;
289*1b191cb5SApple OSS Distributions 
290*1b191cb5SApple OSS Distributions 	/*
291*1b191cb5SApple OSS Distributions 	 * Perform the select and read any data that comes from the
292*1b191cb5SApple OSS Distributions 	 * constituent thread FDs.
293*1b191cb5SApple OSS Distributions 	 */
294*1b191cb5SApple OSS Distributions 
295*1b191cb5SApple OSS Distributions 	T_LOG("\ttest running!");
296*1b191cb5SApple OSS Distributions handle_ebadf:
297*1b191cb5SApple OSS Distributions 	/* (re) set up the select fd set */
298*1b191cb5SApple OSS Distributions 	sarg.max_fd = 0;
299*1b191cb5SApple OSS Distributions 	FD_ZERO(&sarg.def_readfds);
300*1b191cb5SApple OSS Distributions 	for (int i = 0; i < nthreads; i++) {
301*1b191cb5SApple OSS Distributions 		struct endpoint *ep = &th[i].ep;
302*1b191cb5SApple OSS Distributions 
303*1b191cb5SApple OSS Distributions 		FD_SET(ep->fd[0], &sarg.def_readfds);
304*1b191cb5SApple OSS Distributions 		if (ep->fd[0] > sarg.max_fd) {
305*1b191cb5SApple OSS Distributions 			sarg.max_fd = ep->fd[0];
306*1b191cb5SApple OSS Distributions 		}
307*1b191cb5SApple OSS Distributions 	}
308*1b191cb5SApple OSS Distributions 
309*1b191cb5SApple OSS Distributions 	sarg.th = th;
310*1b191cb5SApple OSS Distributions 	sarg.nthreads = nthreads;
311*1b191cb5SApple OSS Distributions 
312*1b191cb5SApple OSS Distributions 	while (mach_absolute_time() < deadline) {
313*1b191cb5SApple OSS Distributions 		void *thret = NULL;
314*1b191cb5SApple OSS Distributions 
315*1b191cb5SApple OSS Distributions 		seconds_remain = abs_to_ns(deadline - mach_absolute_time()) / NSEC_PER_SEC;
316*1b191cb5SApple OSS Distributions 		if (last_print_time > seconds_remain) {
317*1b191cb5SApple OSS Distributions 			T_LOG(" %6lld...", seconds_remain);
318*1b191cb5SApple OSS Distributions 			last_print_time = seconds_remain;
319*1b191cb5SApple OSS Distributions 		}
320*1b191cb5SApple OSS Distributions 
321*1b191cb5SApple OSS Distributions 		sarg.ret = 0;
322*1b191cb5SApple OSS Distributions 		T_QUIET;
323*1b191cb5SApple OSS Distributions 		T_WITH_ERRNO;
324*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_create(&sarg.pth, 0, do_select, &sarg),
325*1b191cb5SApple OSS Distributions 		    "pthread_create:do_select");
326*1b191cb5SApple OSS Distributions 
327*1b191cb5SApple OSS Distributions 		T_QUIET;
328*1b191cb5SApple OSS Distributions 		T_WITH_ERRNO;
329*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_cancel(sarg.pth), "pthread_cancel");
330*1b191cb5SApple OSS Distributions 		T_QUIET;
331*1b191cb5SApple OSS Distributions 		T_WITH_ERRNO;
332*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_join(sarg.pth, &thret), "pthread_join");
333*1b191cb5SApple OSS Distributions 
334*1b191cb5SApple OSS Distributions 		if (sarg.ret == EBADF) {
335*1b191cb5SApple OSS Distributions 			goto handle_ebadf;
336*1b191cb5SApple OSS Distributions 		}
337*1b191cb5SApple OSS Distributions 		T_QUIET;
338*1b191cb5SApple OSS Distributions 		T_ASSERT_GE(sarg.ret, 0, "threaded do_select returned an \
339*1b191cb5SApple OSS Distributions 		    error: %d!", sarg.ret);
340*1b191cb5SApple OSS Distributions 	}
341*1b191cb5SApple OSS Distributions 
342*1b191cb5SApple OSS Distributions 	T_PASS("select stress test passed");
343*1b191cb5SApple OSS Distributions }
344*1b191cb5SApple OSS Distributions 
345*1b191cb5SApple OSS Distributions 
346*1b191cb5SApple OSS Distributions /*
347*1b191cb5SApple OSS Distributions  * TEST: use select as sleep()
348*1b191cb5SApple OSS Distributions  */
349*1b191cb5SApple OSS Distributions static void
test_select_sleep(uint32_t niterations,unsigned long usecs)350*1b191cb5SApple OSS Distributions test_select_sleep(uint32_t niterations, unsigned long usecs)
351*1b191cb5SApple OSS Distributions {
352*1b191cb5SApple OSS Distributions 	int ret;
353*1b191cb5SApple OSS Distributions 	struct timeval tv;
354*1b191cb5SApple OSS Distributions 	tv.tv_sec = 0;
355*1b191cb5SApple OSS Distributions 	tv.tv_usec = usecs;
356*1b191cb5SApple OSS Distributions 
357*1b191cb5SApple OSS Distributions 	if (!niterations) {
358*1b191cb5SApple OSS Distributions 		T_FAIL("select sleep test skipped");
359*1b191cb5SApple OSS Distributions 		return;
360*1b191cb5SApple OSS Distributions 	}
361*1b191cb5SApple OSS Distributions 
362*1b191cb5SApple OSS Distributions 	T_LOG("Testing select as sleep (n=%d, us=%ld)...", niterations, usecs);
363*1b191cb5SApple OSS Distributions 
364*1b191cb5SApple OSS Distributions 	while (niterations--) {
365*1b191cb5SApple OSS Distributions 		ret = select(0, NULL, NULL, NULL, &tv);
366*1b191cb5SApple OSS Distributions 		if (ret < 0 && errno != EINTR) {
367*1b191cb5SApple OSS Distributions 			T_QUIET;
368*1b191cb5SApple OSS Distributions 			T_WITH_ERRNO;
369*1b191cb5SApple OSS Distributions 			T_ASSERT_POSIX_SUCCESS(ret, "select:sleep");
370*1b191cb5SApple OSS Distributions 		}
371*1b191cb5SApple OSS Distributions 	}
372*1b191cb5SApple OSS Distributions 
373*1b191cb5SApple OSS Distributions 	T_PASS("select sleep test passed");
374*1b191cb5SApple OSS Distributions }
375*1b191cb5SApple OSS Distributions 
376*1b191cb5SApple OSS Distributions #define get_env_arg(NM, sval, val) \
377*1b191cb5SApple OSS Distributions 	do { \
378*1b191cb5SApple OSS Distributions 	        sval = getenv(#NM); \
379*1b191cb5SApple OSS Distributions 	        if (sval) { \
380*1b191cb5SApple OSS Distributions 	                long v = atol(sval); \
381*1b191cb5SApple OSS Distributions 	                if (v <= 0) \
382*1b191cb5SApple OSS Distributions 	                        v =1 ; \
383*1b191cb5SApple OSS Distributions 	                val = (typeof(val))v; \
384*1b191cb5SApple OSS Distributions 	        } \
385*1b191cb5SApple OSS Distributions 	} while (0)
386*1b191cb5SApple OSS Distributions 
387*1b191cb5SApple OSS Distributions T_DECL(select_sleep, "select sleep test for rdar://problem/20804876 Gala: select with no FDs leaks waitq table objects (causes asserts/panics)")
388*1b191cb5SApple OSS Distributions {
389*1b191cb5SApple OSS Distributions 	char *env_sval = NULL;
390*1b191cb5SApple OSS Distributions 
391*1b191cb5SApple OSS Distributions 	get_env_arg(SELSLEEP_ITERATIONS, env_sval, g_sleep_iterations);
392*1b191cb5SApple OSS Distributions 	get_env_arg(SELSLEEP_INTERVAL, env_sval, g_sleep_usecs);
393*1b191cb5SApple OSS Distributions 
394*1b191cb5SApple OSS Distributions 	test_select_sleep((uint32_t)g_sleep_iterations, (unsigned long)g_sleep_usecs);
395*1b191cb5SApple OSS Distributions }
396*1b191cb5SApple OSS Distributions 
397*1b191cb5SApple OSS Distributions T_DECL(select_stress, "select stress test for rdar://problem/20804876 Gala: select with no FDs leaks waitq table objects (causes asserts/panics)")
398*1b191cb5SApple OSS Distributions {
399*1b191cb5SApple OSS Distributions 	char *env_sval = NULL;
400*1b191cb5SApple OSS Distributions 
401*1b191cb5SApple OSS Distributions 	T_QUIET;
402*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_timebase_info(&g_timebase),
403*1b191cb5SApple OSS Distributions 	    "Can't get mach_timebase_info!");
404*1b191cb5SApple OSS Distributions 
405*1b191cb5SApple OSS Distributions 	get_env_arg(SELSTRESS_THREADS, env_sval, g_stress_nthreads);
406*1b191cb5SApple OSS Distributions 	get_env_arg(SELSTRESS_DURATION, env_sval, g_stress_duration);
407*1b191cb5SApple OSS Distributions 
408*1b191cb5SApple OSS Distributions 	T_QUIET;
409*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &g_sync_sem, SYNC_POLICY_FIFO, 0),
410*1b191cb5SApple OSS Distributions 	    "semaphore_create(g_sync_sem)");
411*1b191cb5SApple OSS Distributions 	T_QUIET;
412*1b191cb5SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &g_thread_sem, SYNC_POLICY_FIFO, 0),
413*1b191cb5SApple OSS Distributions 	    "semaphore_create(g_thread_sem)");
414*1b191cb5SApple OSS Distributions 
415*1b191cb5SApple OSS Distributions 	test_select_stress(g_stress_nthreads, g_stress_duration);
416*1b191cb5SApple OSS Distributions }
417