2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Regression test to do some very basic AIO exercising on several types of
31 * file descriptors. Currently, the tests consist of initializing a fixed
32 * size buffer with pseudo-random data, writing it to one fd using AIO, then
33 * reading it from a second descriptor using AIO. For some targets, the same
34 * fd is used for write and read (i.e., file, md device), but for others the
35 * operation is performed on a peer (pty, socket, fifo, etc). A timeout is
36 * initiated to detect undo blocking. This test does not attempt to exercise
37 * error cases or more subtle asynchronous behavior, just make sure that the
38 * basic operations work on some basic object types.
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/mdioctl.h>
55 #define PATH_TEMPLATE "/tmp/aio.XXXXXXXXXX"
58 * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
59 * it sizes ac_buffer in the aio_context structure. It is also the default
60 * size for file I/O. For other types, we use smaller blocks or we risk
61 * blocking (and we run in a single process/thread so that would be bad).
63 #define GLOBAL_MAX 16384
65 #define BUFFER_MAX GLOBAL_MAX
68 int ac_read_fd, ac_write_fd;
70 char ac_buffer[GLOBAL_MAX];
73 void (*ac_cleanup)(void *arg);
77 static int aio_timedout;
78 static int aio_notpresent;
81 * Attempt to provide a cleaner failure mode in the event AIO support is not
82 * present by catching and reporting SIGSYS.
92 aio_sigsys_setup(void)
95 if (signal(SIGSYS, aio_sigsys) == SIG_ERR)
96 errx(-1, "FAIL: signal(SIGSYS): %s", strerror(errno));
100 * Each test run specifies a timeout in seconds. Use the somewhat obsoleted
101 * signal(3) and alarm(3) APIs to set this up.
104 aio_timeout_signal(int sig)
111 aio_timeout_start(const char *string1, const char *string2, int seconds)
115 if (signal(SIGALRM, aio_timeout_signal) == SIG_ERR)
116 errx(-1, "FAIL: %s: %s: aio_timeout_set: signal(SIGALRM): %s",
117 string1, string2, strerror(errno));
122 aio_timeout_stop(const char *string1, const char *string2)
125 if (signal(SIGALRM, NULL) == SIG_ERR)
126 errx(-1, "FAIL: %s: %s: aio_timeout_stop: signal(NULL): %s",
127 string1, string2, strerror(errno));
132 * Fill a buffer given a seed that can be fed into srandom() to initialize
133 * the PRNG in a repeatable manner.
136 aio_fill_buffer(char *buffer, int len, long seed)
142 for (i = 0; i < len; i++) {
143 ch = random() & 0xff;
149 * Test that a buffer matches a given seed. See aio_fill_buffer(). Return
150 * (1) on a match, (0) on a mismatch.
153 aio_test_buffer(char *buffer, int len, long seed)
159 for (i = 0; i < len; i++) {
160 ch = random() & 0xff;
168 * Initialize a testing context given the file descriptors provided by the
172 aio_context_init(struct aio_context *ac, const char *test, int read_fd,
173 int write_fd, int buflen, int seconds, void (*cleanup)(void *),
177 if (buflen > BUFFER_MAX)
178 errx(-1, "FAIL: %s: aio_context_init: buffer too large",
180 bzero(ac, sizeof(*ac));
182 ac->ac_read_fd = read_fd;
183 ac->ac_write_fd = write_fd;
184 ac->ac_buflen = buflen;
186 ac->ac_seed = random();
187 aio_fill_buffer(ac->ac_buffer, buflen, ac->ac_seed);
188 if (aio_test_buffer(ac->ac_buffer, buflen, ac->ac_seed) == 0)
189 errx(-1, "%s: aio_context_init: aio_test_buffer: internal "
191 ac->ac_seconds = seconds;
192 ac->ac_cleanup = cleanup;
193 ac->ac_cleanup_arg = cleanup_arg;
197 * Each tester can register a callback to clean up in the event the test
198 * fails. Preserve the value of errno so that subsequent calls to errx()
202 aio_cleanup(struct aio_context *ac)
206 if (ac->ac_cleanup == NULL)
209 (ac->ac_cleanup)(ac->ac_cleanup_arg);
214 * Perform a simple write test of our initialized data buffer to the provided
218 aio_write_test(struct aio_context *ac)
220 struct aiocb aio, *aiop;
224 bzero(&aio, sizeof(aio));
225 aio.aio_buf = ac->ac_buffer;
226 aio.aio_nbytes = ac->ac_buflen;
227 aio.aio_fildes = ac->ac_write_fd;
230 aio_timeout_start(ac->ac_test, "aio_write_test", ac->ac_seconds);
232 if (aio_write(&aio) < 0) {
233 if (errno == EINTR) {
238 errx(-1, "FAIL: %s: aio_write_test: "
239 "aio_write: timed out", ac->ac_test);
243 errx(-1, "FAIL: %s: aio_write_test: aio_write: %s",
244 ac->ac_test, strerror(errno));
247 len = aio_waitcomplete(&aiop, NULL);
249 if (errno == EINTR) {
254 errx(-1, "FAIL: %s: aio_write_test: "
255 "aio_waitcomplete: timed out",
260 errx(-1, "FAIL: %s: aio_write_test: aio_waitcomplete: %s",
261 ac->ac_test, strerror(errno));
264 aio_timeout_stop(ac->ac_test, "aio_write_test");
266 if (len != ac->ac_buflen) {
268 errx(-1, "FAIL: %s: aio_write_test: aio_waitcomplete: short "
269 "write (%d)", ac->ac_test, len);
274 * Perform a simple read test of our initialized data buffer from the
275 * provided file descriptor.
278 aio_read_test(struct aio_context *ac)
280 struct aiocb aio, *aiop;
283 bzero(ac->ac_buffer, ac->ac_buflen);
284 bzero(&aio, sizeof(aio));
285 aio.aio_buf = ac->ac_buffer;
286 aio.aio_nbytes = ac->ac_buflen;
287 aio.aio_fildes = ac->ac_read_fd;
290 aio_timeout_start(ac->ac_test, "aio_read_test", ac->ac_seconds);
292 if (aio_read(&aio) < 0) {
293 if (errno == EINTR) {
298 errx(-1, "FAIL: %s: aio_read_test: "
299 "aio_read: timed out", ac->ac_test);
303 errx(-1, "FAIL: %s: aio_read_test: aio_read %s", ac->ac_test,
307 len = aio_waitcomplete(&aiop, NULL);
309 if (errno == EINTR) {
314 errx(-1, "FAIL: %s: aio_read_test: "
315 "aio_waitcomplete: timed out",
320 errx(-1, "FAIL: %s: aio_read_test: aio_waitcomplete: %s",
321 ac->ac_test, strerror(errno));
324 aio_timeout_stop(ac->ac_test, "aio_read_test");
326 if (len != ac->ac_buflen) {
328 errx(-1, "FAIL: %s: aio_read_test: aio_waitcomplete: short "
329 "read (%d)", ac->ac_test, len);
332 if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0) {
334 errx(-1, "FAIL: %s: aio_read_test: buffer mismatch",
340 * Series of type-specific tests for AIO. For now, we just make sure we can
341 * issue a write and then a read to each type. We assume that once a write
342 * is issued, a read can follow.
346 * Test with a classic file. Assumes we can create a moderate size temporary
349 struct aio_file_arg {
355 aio_file_cleanup(void *arg)
357 struct aio_file_arg *afa;
361 unlink(afa->afa_pathname);
364 #define FILE_LEN GLOBAL_MAX
365 #define FILE_TIMEOUT 30
369 char pathname[PATH_MAX];
370 struct aio_file_arg arg;
371 struct aio_context ac;
374 strcpy(pathname, PATH_TEMPLATE);
375 fd = mkstemp(pathname);
377 errx(-1, "FAIL: aio_file_test: mkstemp: %s",
381 arg.afa_pathname = pathname;
383 aio_context_init(&ac, "aio_file_test", fd, fd, FILE_LEN,
384 FILE_TIMEOUT, aio_file_cleanup, &arg);
388 aio_file_cleanup(&arg);
390 fprintf(stderr, "PASS: aio_file_test\n");
393 struct aio_fifo_arg {
400 aio_fifo_cleanup(void *arg)
402 struct aio_fifo_arg *afa;
405 if (afa->afa_read_fd != -1)
406 close(afa->afa_read_fd);
407 if (afa->afa_write_fd != -1)
408 close(afa->afa_write_fd);
409 unlink(afa->afa_pathname);
413 #define FIFO_TIMEOUT 30
417 int error, read_fd = -1, write_fd = -1;
418 struct aio_fifo_arg arg;
419 char pathname[PATH_MAX];
420 struct aio_context ac;
423 * In theory, mktemp() can return a name that is then collided with.
424 * Because this is a regression test, we treat that as a test failure
425 * rather than retrying.
427 strcpy(pathname, PATH_TEMPLATE);
429 if (mkfifo(pathname, 0600) == -1)
430 errx(-1, "FAIL: aio_fifo_test: mkfifo: %s", strerror(errno));
431 arg.afa_pathname = pathname;
432 arg.afa_read_fd = -1;
433 arg.afa_write_fd = -1;
435 read_fd = open(pathname, O_RDONLY | O_NONBLOCK);
438 aio_fifo_cleanup(&arg);
440 errx(-1, "FAIL: aio_fifo_test: read_fd open: %s",
443 arg.afa_read_fd = read_fd;
445 write_fd = open(pathname, O_WRONLY);
446 if (write_fd == -1) {
448 aio_fifo_cleanup(&arg);
450 errx(-1, "FAIL: aio_fifo_test: write_fd open: %s",
453 arg.afa_write_fd = write_fd;
455 aio_context_init(&ac, "aio_fifo_test", read_fd, write_fd, FIFO_LEN,
456 FIFO_TIMEOUT, aio_fifo_cleanup, &arg);
460 aio_fifo_cleanup(&arg);
462 fprintf(stderr, "PASS: aio_fifo_test\n");
465 struct aio_unix_socketpair_arg {
470 aio_unix_socketpair_cleanup(void *arg)
472 struct aio_unix_socketpair_arg *asa;
475 close(asa->asa_sockets[0]);
476 close(asa->asa_sockets[1]);
479 #define UNIX_SOCKETPAIR_LEN 256
480 #define UNIX_SOCKETPAIR_TIMEOUT 30
482 aio_unix_socketpair_test(void)
484 struct aio_unix_socketpair_arg arg;
485 struct aio_context ac;
488 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) < 0)
489 errx(-1, "FAIL: aio_socketpair_test: socketpair: %s",
492 arg.asa_sockets[0] = sockets[0];
493 arg.asa_sockets[1] = sockets[1];
494 aio_context_init(&ac, "aio_unix_socketpair_test", sockets[0],
495 sockets[1], UNIX_SOCKETPAIR_LEN, UNIX_SOCKETPAIR_TIMEOUT,
496 aio_unix_socketpair_cleanup, &arg);
500 aio_unix_socketpair_cleanup(&arg);
502 fprintf(stderr, "PASS: aio_unix_socketpair_test\n");
511 aio_pty_cleanup(void *arg)
513 struct aio_pty_arg *apa;
515 close(apa->apa_read_fd);
516 close(apa->apa_write_fd);
520 #define PTY_TIMEOUT 30
524 struct aio_pty_arg arg;
525 struct aio_context ac;
526 int read_fd, write_fd;
530 if (openpty(&read_fd, &write_fd, NULL, NULL, NULL) < 0)
531 errx(-1, "FAIL: aio_pty_test: openpty: %s", strerror(errno));
533 arg.apa_read_fd = read_fd;
534 arg.apa_write_fd = write_fd;
536 if (tcgetattr(write_fd, &ts) < 0) {
538 aio_pty_cleanup(&arg);
540 errx(-1, "FAIL: aio_pty_test: tcgetattr: %s",
544 if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
546 aio_pty_cleanup(&arg);
548 errx(-1, "FAIL: aio_pty_test: tcsetattr: %s",
552 aio_context_init(&ac, "aio_pty_test", read_fd, write_fd, PTY_LEN,
553 PTY_TIMEOUT, aio_pty_cleanup, &arg);
557 aio_pty_cleanup(&arg);
559 fprintf(stderr, "PASS: aio_pty_test\n");
563 aio_pipe_cleanup(void *arg)
572 #define PIPE_TIMEOUT 30
576 struct aio_context ac;
580 errx(-1, "FAIL: aio_pipe_test: pipe: %s", strerror(errno));
582 aio_context_init(&ac, "aio_file_test", pipes[0], pipes[1], PIPE_LEN,
583 PIPE_TIMEOUT, aio_pipe_cleanup, pipes);
587 aio_pipe_cleanup(pipes);
589 fprintf(stderr, "PASS: aio_pipe_test\n");
599 aio_md_cleanup(void *arg)
601 struct aio_md_arg *ama;
602 struct md_ioctl mdio;
607 if (ama->ama_fd != -1)
610 if (ama->ama_unit != -1) {
611 bzero(&mdio, sizeof(mdio));
612 mdio.md_version = MDIOVERSION;
613 mdio.md_unit = ama->ama_unit;
614 if (ioctl(ama->ama_mdctl_fd, MDIOCDETACH, &mdio) < 0) {
616 close(ama->ama_mdctl_fd);
618 warnx("FAIL: aio_md_test: MDIOCDETACH: %s",
623 close(ama->ama_mdctl_fd);
626 #define MD_LEN GLOBAL_MAX
627 #define MD_TIMEOUT 30
631 int error, fd, i, mdctl_fd, unit;
632 char pathname[PATH_MAX];
633 struct aio_md_arg arg;
634 struct aio_context ac;
635 struct md_ioctl mdio;
637 mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
639 errx(-1, "FAIL: aio_md_test: open(/dev/%s): %s", MDCTL_NAME,
642 bzero(&mdio, sizeof(mdio));
643 mdio.md_version = MDIOVERSION;
644 mdio.md_type = MD_MALLOC;
645 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
646 mdio.md_mediasize = GLOBAL_MAX;
647 mdio.md_sectorsize = 512;
649 arg.ama_mdctl_fd = mdctl_fd;
652 if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
654 aio_md_cleanup(&arg);
656 errx(-1, "FAIL: aio_md_test: MDIOCATTACH: %s",
660 arg.ama_unit = unit = mdio.md_unit;
661 snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
662 fd = open(pathname, O_RDWR);
665 aio_md_cleanup(&arg);
667 errx(-1, "FAIL: aio_md_test: open(%s): %s", pathname,
672 aio_context_init(&ac, "aio_md_test", fd, fd, MD_LEN, MD_TIMEOUT,
673 aio_md_cleanup, &arg);
677 aio_md_cleanup(&arg);
679 fprintf(stderr, "PASS: aio_md_test\n");
683 main(int argc, char *argv[])
689 aio_unix_socketpair_test();
695 fprintf(stderr, "WARNING: aio_md_test: skipped as euid "