3311 Want a test framework for arbitrary OS unit tests
[illumos-gate.git] / usr / src / test / os-tests / tests / poll / poll_test.c
blobc242d5ef29c3097a6b5f4a31cb0997794244d243
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2012 by Delphix. All rights reserved.
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <poll.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <sys/stat.h>
30 #include <sys/socket.h>
31 #include <sys/devpoll.h>
34 * poll_test.c --
36 * This file implements some simple tests to verify the behavior of the
37 * poll system call and the DP_POLL ioctl on /dev/poll.
39 * Background:
41 * Several customers recently ran into an issue where threads in grizzly
42 * (java http server implementation) would randomly wake up from a java
43 * call to select against a java.nio.channels.Selector with no events ready
44 * but well before the specified timeout expired. The
45 * java.nio.channels.Selector select logic is implemented via /dev/poll.
46 * The selector opens /dev/poll, writes the file descriptors it wants to
47 * select on to the file descritpor, and then issues a DP_POLL ioctl to
48 * wait for events to be ready.
50 * The DP_POLL ioctl arguments include a relative timeout in milliseconds,
51 * according to man poll.7d the ioctl should block until events are ready,
52 * the timeout expires, or a signal was received. In this case we noticed
53 * that DP_POLL was returning before the timeout expired despite no events
54 * being ready and no signal being delivered.
56 * Using dtrace we discovered that DP_POLL was returning in cases where the
57 * system time was changed and the thread calling DP_POLL was woken up as
58 * a result of the process forking. The DP_POLL logic was in a loop
59 * checking if events were ready and then calling cv_waituntil_sig to
60 * block. cv_waituntil_sig will return -1 if the system time has changed,
61 * causing the DP_POLL to complete prematurely.
63 * Looking at the code it turns out the same problem exists in
64 * the implementation for poll.2 as well.
66 * Fix:
68 * The fix changes dpioctl and poll_common to use cv_relwaituntil_sig
69 * rather then cv_waituntil_sig. cv_reltimedwait_sig expects a
70 * relative timeout rather then an absolute timeout, so we avoid the
71 * problem.
73 * Test:
75 * The test verifies that changing the date does not wake up threads
76 * blocked processing a poll request or a DP_POLL ioctl. The test spawns
77 * one thread that changes the date and forks (to force the threads to
78 * wakeup from cv_reltimedwait_sig) every two seconds. The test spawns
79 * a second thread that issues poll / DP_POLL on an fd set that will
80 * never have events ready and verifies that it does not return until
81 * the specified timeout expires.
85 * The maximum amount of skew in seconds allowed between the
86 * expected an actual time that a test takes.
88 #define TIME_DRIFT 1
90 static pthread_mutex_t exitLock = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t exitCond = PTHREAD_COND_INITIALIZER;
92 static int terminated = 0;
95 * Set via -d to enable debug logging
97 static int debug = 0;
99 static void
100 debug_log(const char *format, ...)
102 va_list args;
104 if (!debug) {
105 return;
108 (void) printf("DEBUG: ");
110 va_start(args, format);
111 (void) vprintf(format, args);
112 va_end(args);
115 static void
116 test_start(const char *testName, const char *format, ...)
118 va_list args;
120 (void) printf("TEST STARTING %s: ", testName);
122 va_start(args, format);
123 (void) vprintf(format, args);
124 va_end(args);
125 (void) fflush(stdout);
128 static void
129 test_failed(const char *testName, const char *format, ...)
131 va_list args;
133 (void) printf("TEST FAILED %s: ", testName);
135 va_start(args, format);
136 (void) vprintf(format, args);
137 va_end(args);
139 (void) exit(-1);
142 static void
143 test_passed(const char *testName)
145 (void) printf("TEST PASS: %s\n", testName);
146 (void) fflush(stdout);
149 static int
150 check_time(time_t elapsed, time_t expected)
152 time_t diff = expected - elapsed;
155 * We may take slightly more or less time then expected,
156 * we allow for a small fudge factor if things completed
157 * before we expect them to.
159 return (elapsed >= expected || diff <= TIME_DRIFT);
162 static int
163 poll_wrapper(pollfd_t *fds, nfds_t nfds, int timeout, time_t *elapsed)
165 int ret;
166 time_t start = time(NULL);
168 debug_log("POLL start: (0x%p, %d, %d)\n", fds, nfds, timeout);
170 ret = poll(fds, nfds, timeout);
172 *elapsed = time(NULL) - start;
174 debug_log("POLL end: (0x%p, %d, %d) returns %d (elapse=%d)\n",
175 fds, nfds, timeout, ret, (*elapsed));
177 return (ret);
180 static int
181 dppoll(int pollfd, pollfd_t *fds, nfds_t nfds, int timeout, time_t *elapsed)
183 struct dvpoll arg;
184 int ret;
185 time_t start = time(NULL);
187 arg.dp_fds = fds;
188 arg.dp_nfds = nfds;
189 arg.dp_timeout = timeout;
191 debug_log("DP_POLL start: (0x%p, %d, %d)\n", fds, nfds, timeout);
193 ret = ioctl(pollfd, DP_POLL, &arg);
195 *elapsed = time(NULL) - start;
197 debug_log("DP_POLL end: (0x%p, %d, %d) returns %d (elapse=%d)\n",
198 fds, arg.dp_nfds, arg.dp_timeout, ret, (*elapsed));
200 return (ret);
203 static void
204 clear_fd(const char *testName, int pollfd, int testfd)
206 int ret;
207 pollfd_t fd;
209 fd.fd = testfd;
210 fd.events = POLLREMOVE;
211 fd.revents = 0;
213 ret = write(pollfd, &fd, sizeof (pollfd_t));
215 if (ret != sizeof (pollfd_t)) {
216 if (ret < 0) {
217 test_failed(testName, "Failed to clear fd %d: %s",
218 testfd, strerror(ret));
222 test_failed(testName, "Failed to clear fd %d: %d", testfd, ret);
227 * TEST: poll with no FDs set, verify we wait the appropriate amount of time.
229 static void
230 poll_no_fd_test(void)
232 const char *testName = __func__;
233 time_t elapsed;
234 int timeout = 10;
235 int ret;
237 test_start(testName, "poll for %d sec with no fds\n", timeout);
239 ret = poll_wrapper(NULL, 0, timeout * 1000, &elapsed);
241 if (ret != 0) {
242 test_failed(testName, "POLL returns %d (expected 0)\n", ret);
245 if (!check_time(elapsed, timeout)) {
246 test_failed(testName, "took %d (expected %d)\n",
247 elapsed, timeout);
250 test_passed(testName);
254 * TEST: POLL with a valid FD set, verify that we wait the appropriate amount
255 * of time.
257 static void
258 poll_with_fds_test(int testfd)
260 const char *testName = __func__;
261 time_t elapsed;
262 int timeout = 10;
263 int ret;
264 pollfd_t fd;
266 fd.fd = testfd;
267 fd.events = 0;
268 fd.revents = 0;
270 test_start(testName, "poll for %d sec with fds\n", timeout);
272 ret = poll_wrapper(&fd, 1, timeout * 1000, &elapsed);
274 if (ret != 0) {
275 test_failed(testName, "POLL returns %d (expected 0)\n", ret);
278 if (!check_time(elapsed, timeout)) {
279 test_failed(testName, "took %d (expected %d)\n",
280 elapsed, timeout);
283 test_passed(testName);
287 * TEST: DP_POLL with no FDs set, verify we wait the appropriate
288 * amount of time.
290 static void
291 dev_poll_no_fd_test(int pollfd)
293 const char *testName = __func__;
294 time_t elapsed;
295 int timeout = 10;
296 int ret;
298 test_start(testName, "poll for %d sec with no fds\n", timeout);
300 ret = dppoll(pollfd, NULL, 0, timeout * 1000, &elapsed);
302 if (ret != 0) {
303 test_failed(testName, "DP_POLL returns %d (expected 0)\n", ret);
306 if (!check_time(elapsed, timeout)) {
307 test_failed(testName, "took %d (expected %d)\n",
308 elapsed, timeout);
311 test_passed(testName);
315 * TEST: DP_POLL with a valid FD set, verify that we wait
316 * the appropriate amount of time.
318 static void
319 dev_poll_with_fds_test(int pollfd, int testfd)
321 const char *testName = __func__;
322 time_t elapsed;
323 int timeout = 10;
324 int ret;
325 pollfd_t fds[5];
327 test_start(testName, "poll for %d sec with fds\n", timeout);
330 * Clear the FD in case it's already in the cached set
332 clear_fd(testName, pollfd, testfd);
335 * Add the FD with POLLIN
337 fds[0].fd = testfd;
338 fds[0].events = POLLIN;
339 fds[0].revents = 0;
341 ret = write(pollfd, fds, sizeof (pollfd_t));
343 if (ret != sizeof (pollfd_t)) {
344 if (ret < 0) {
345 test_failed(testName, "Failed to set fds: %s",
346 strerror(ret));
349 test_failed(testName, "Failed to set fds: %d", ret);
352 ret = dppoll(pollfd, fds, 5, timeout * 1000, &elapsed);
354 if (ret != 0) {
355 test_failed(testName, "DP_POLL returns %d (expected 0)\n", ret);
358 if (!check_time(elapsed, timeout)) {
359 test_failed(testName, "took %d (expected %d)\n",
360 elapsed, timeout);
363 clear_fd(testName, pollfd, testfd);
365 test_passed(testName);
368 /* ARGSUSED */
369 static void *
370 poll_thread(void *data)
372 int pollfd;
373 int testfd;
375 pollfd = open("/dev/poll", O_RDWR);
377 if (pollfd < 0) {
378 perror("Failed to open /dev/poll: ");
379 exit(-1);
383 * Create a dummy FD that will never have POLLIN set
385 testfd = socket(PF_INET, SOCK_STREAM, 0);
387 poll_no_fd_test();
388 poll_with_fds_test(testfd);
390 dev_poll_no_fd_test(pollfd);
391 dev_poll_with_fds_test(pollfd, testfd);
393 (void) close(testfd);
394 (void) close(pollfd);
396 pthread_exit(0);
397 return (NULL);
401 * This function causes any threads blocked in cv_timedwait_sig_hires
402 * to wakeup, which allows us to test how dpioctl handles spurious
403 * wakeups.
405 static void
406 trigger_wakeup(void)
408 pid_t child;
411 * Forking will force all of the threads to be woken up so
412 * they can be moved to a well known state.
414 child = vfork();
416 if (child == -1) {
417 perror("Fork failed: ");
418 exit(-1);
419 } else if (child == 0) {
420 _exit(0);
421 } else {
422 pid_t result = -1;
423 int status;
425 do {
426 result = waitpid(child, &status, 0);
428 if (result == -1 && errno != EINTR) {
429 (void) printf("Waitpid for %ld failed: %s\n",
430 child, strerror(errno));
431 exit(-1);
433 } while (result != child);
435 if (status != 0) {
436 (void) printf("Child pid %ld failed: %d\n",
437 child, status);
438 exit(-1);
444 * This function changes the system time, which has the side
445 * effect of updating timechanged in the kernel.
447 static void
448 change_date(void)
450 int ret;
451 struct timeval tp;
453 ret = gettimeofday(&tp, NULL);
454 assert(ret == 0);
456 tp.tv_usec++;
457 ret = settimeofday(&tp, NULL);
458 assert(ret == 0);
462 * The helper thread runs in a loop changing the time and
463 * forcing wakeups every 2 seconds.
465 /* ARGSUSED */
466 static void *
467 helper_thread(void *data)
469 int exit;
470 struct timespec ts = {2, 0};
472 debug_log("Helper thread started ...\n");
474 /* CONSTCOND */
475 while (1) {
476 (void) pthread_mutex_lock(&exitLock);
477 (void) pthread_cond_reltimedwait_np(&exitCond, &exitLock, &ts);
478 exit = terminated;
479 (void) pthread_mutex_unlock(&exitLock);
481 if (exit) {
482 break;
485 change_date();
486 trigger_wakeup();
487 debug_log("Time changed and force wakeup issued\n");
490 debug_log("Helper thread exiting ...\n");
492 pthread_exit(0);
493 return (NULL);
496 static void
497 stop_threads(void)
499 (void) pthread_mutex_lock(&exitLock);
500 terminated = 1;
501 (void) pthread_cond_broadcast(&exitCond);
502 (void) pthread_mutex_unlock(&exitLock);
505 static void
506 run_tests(void)
508 pthread_t pollThread;
509 pthread_t helperThread;
510 int ret;
512 ret = pthread_create(&helperThread, NULL, helper_thread, NULL);
514 if (ret != 0) {
515 (void) printf("Failed to create date thread: %s",
516 strerror(ret));
517 exit(-1);
520 ret = pthread_create(&pollThread, NULL, poll_thread, NULL);
522 if (ret != 0) {
523 (void) printf("Failed to create poll thread: %s",
524 strerror(ret));
525 exit(-1);
528 (void) pthread_join(pollThread, NULL);
529 stop_threads();
530 (void) pthread_join(helperThread, NULL);
534 main(int argc, char * const argv[])
536 int c;
538 while ((c = getopt(argc, argv, "d")) != -1) {
539 switch (c) {
540 case 'd':
541 debug = 1;
542 break;
543 default:
544 break;
549 * We need to be root to change the system time
551 if (getuid() != 0 && geteuid() != 0) {
552 (void) printf("%s must be run as root\n", argv[0]);
553 exit(-1);
556 run_tests();
558 exit(0);