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>
44 #include <sys/mdioctl.h>
59 #define PATH_TEMPLATE "/tmp/aio.XXXXXXXXXX"
62 * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
63 * it sizes ac_buffer in the aio_context structure. It is also the default
64 * size for file I/O. For other types, we use smaller blocks or we risk
65 * blocking (and we run in a single process/thread so that would be bad).
67 #define GLOBAL_MAX 16384
69 #define BUFFER_MAX GLOBAL_MAX
72 int ac_read_fd, ac_write_fd;
74 char ac_buffer[GLOBAL_MAX];
77 void (*ac_cleanup)(void *arg);
81 static int aio_timedout;
82 static int aio_notpresent;
85 * Attempt to provide a cleaner failure mode in the event AIO support is not
86 * present by catching and reporting SIGSYS.
96 aio_sigsys_setup(void)
99 if (signal(SIGSYS, aio_sigsys) == SIG_ERR)
100 errx(-1, "FAIL: signal(SIGSYS): %s", strerror(errno));
104 * Each test run specifies a timeout in seconds. Use the somewhat obsoleted
105 * signal(3) and alarm(3) APIs to set this up.
108 aio_timeout_signal(int sig)
115 aio_timeout_start(const char *string1, const char *string2, int seconds)
119 if (signal(SIGALRM, aio_timeout_signal) == SIG_ERR)
120 errx(-1, "FAIL: %s: %s: aio_timeout_set: signal(SIGALRM): %s",
121 string1, string2, strerror(errno));
126 aio_timeout_stop(const char *string1, const char *string2)
129 if (signal(SIGALRM, NULL) == SIG_ERR)
130 errx(-1, "FAIL: %s: %s: aio_timeout_stop: signal(NULL): %s",
131 string1, string2, strerror(errno));
136 * Fill a buffer given a seed that can be fed into srandom() to initialize
137 * the PRNG in a repeatable manner.
140 aio_fill_buffer(char *buffer, int len, long seed)
146 for (i = 0; i < len; i++) {
147 ch = random() & 0xff;
153 * Test that a buffer matches a given seed. See aio_fill_buffer(). Return
154 * (1) on a match, (0) on a mismatch.
157 aio_test_buffer(char *buffer, int len, long seed)
163 for (i = 0; i < len; i++) {
164 ch = random() & 0xff;
172 * Initialize a testing context given the file descriptors provided by the
176 aio_context_init(struct aio_context *ac, const char *test, int read_fd,
177 int write_fd, int buflen, int seconds, void (*cleanup)(void *),
181 if (buflen > BUFFER_MAX)
182 errx(-1, "FAIL: %s: aio_context_init: buffer too large",
184 bzero(ac, sizeof(*ac));
186 ac->ac_read_fd = read_fd;
187 ac->ac_write_fd = write_fd;
188 ac->ac_buflen = buflen;
190 ac->ac_seed = random();
191 aio_fill_buffer(ac->ac_buffer, buflen, ac->ac_seed);
192 if (aio_test_buffer(ac->ac_buffer, buflen, ac->ac_seed) == 0)
193 errx(-1, "%s: aio_context_init: aio_test_buffer: internal "
195 ac->ac_seconds = seconds;
196 ac->ac_cleanup = cleanup;
197 ac->ac_cleanup_arg = cleanup_arg;
201 * Each tester can register a callback to clean up in the event the test
202 * fails. Preserve the value of errno so that subsequent calls to errx()
206 aio_cleanup(struct aio_context *ac)
210 if (ac->ac_cleanup == NULL)
213 (ac->ac_cleanup)(ac->ac_cleanup_arg);
218 * Perform a simple write test of our initialized data buffer to the provided
222 aio_write_test(struct aio_context *ac)
224 struct aiocb aio, *aiop;
228 bzero(&aio, sizeof(aio));
229 aio.aio_buf = ac->ac_buffer;
230 aio.aio_nbytes = ac->ac_buflen;
231 aio.aio_fildes = ac->ac_write_fd;
234 aio_timeout_start(ac->ac_test, "aio_write_test", ac->ac_seconds);
236 if (aio_write(&aio) < 0) {
237 if (errno == EINTR) {
242 errx(-1, "FAIL: %s: aio_write_test: "
243 "aio_write: timed out", ac->ac_test);
247 errx(-1, "FAIL: %s: aio_write_test: aio_write: %s",
248 ac->ac_test, strerror(errno));
251 len = aio_waitcomplete(&aiop, NULL);
253 if (errno == EINTR) {
258 errx(-1, "FAIL: %s: aio_write_test: "
259 "aio_waitcomplete: timed out",
264 errx(-1, "FAIL: %s: aio_write_test: aio_waitcomplete: %s",
265 ac->ac_test, strerror(errno));
268 aio_timeout_stop(ac->ac_test, "aio_write_test");
270 if (len != ac->ac_buflen) {
272 errx(-1, "FAIL: %s: aio_write_test: aio_waitcomplete: short "
273 "write (%jd)", ac->ac_test, (intmax_t)len);
278 * Perform a simple read test of our initialized data buffer from the
279 * provided file descriptor.
282 aio_read_test(struct aio_context *ac)
284 struct aiocb aio, *aiop;
287 bzero(ac->ac_buffer, ac->ac_buflen);
288 bzero(&aio, sizeof(aio));
289 aio.aio_buf = ac->ac_buffer;
290 aio.aio_nbytes = ac->ac_buflen;
291 aio.aio_fildes = ac->ac_read_fd;
294 aio_timeout_start(ac->ac_test, "aio_read_test", ac->ac_seconds);
296 if (aio_read(&aio) < 0) {
297 if (errno == EINTR) {
302 errx(-1, "FAIL: %s: aio_read_test: "
303 "aio_read: timed out", ac->ac_test);
307 errx(-1, "FAIL: %s: aio_read_test: aio_read %s", ac->ac_test,
311 len = aio_waitcomplete(&aiop, NULL);
313 if (errno == EINTR) {
318 errx(-1, "FAIL: %s: aio_read_test: "
319 "aio_waitcomplete: timed out",
324 errx(-1, "FAIL: %s: aio_read_test: aio_waitcomplete: %s",
325 ac->ac_test, strerror(errno));
328 aio_timeout_stop(ac->ac_test, "aio_read_test");
330 if (len != ac->ac_buflen) {
332 errx(-1, "FAIL: %s: aio_read_test: aio_waitcomplete: short "
333 "read (%jd)", ac->ac_test, (intmax_t)len);
336 if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0) {
338 errx(-1, "FAIL: %s: aio_read_test: buffer mismatch",
344 * Series of type-specific tests for AIO. For now, we just make sure we can
345 * issue a write and then a read to each type. We assume that once a write
346 * is issued, a read can follow.
350 * Test with a classic file. Assumes we can create a moderate size temporary
353 struct aio_file_arg {
359 aio_file_cleanup(void *arg)
361 struct aio_file_arg *afa;
365 unlink(afa->afa_pathname);
368 #define FILE_LEN GLOBAL_MAX
369 #define FILE_TIMEOUT 30
373 char pathname[PATH_MAX];
374 struct aio_file_arg arg;
375 struct aio_context ac;
378 strcpy(pathname, PATH_TEMPLATE);
379 fd = mkstemp(pathname);
381 errx(-1, "FAIL: aio_file_test: mkstemp: %s",
385 arg.afa_pathname = pathname;
387 aio_context_init(&ac, "aio_file_test", fd, fd, FILE_LEN,
388 FILE_TIMEOUT, aio_file_cleanup, &arg);
392 aio_file_cleanup(&arg);
394 fprintf(stderr, "PASS: aio_file_test\n");
397 struct aio_fifo_arg {
404 aio_fifo_cleanup(void *arg)
406 struct aio_fifo_arg *afa;
409 if (afa->afa_read_fd != -1)
410 close(afa->afa_read_fd);
411 if (afa->afa_write_fd != -1)
412 close(afa->afa_write_fd);
413 unlink(afa->afa_pathname);
417 #define FIFO_TIMEOUT 30
421 int error, read_fd = -1, write_fd = -1;
422 struct aio_fifo_arg arg;
423 char pathname[PATH_MAX];
424 struct aio_context ac;
427 * In theory, mktemp() can return a name that is then collided with.
428 * Because this is a regression test, we treat that as a test failure
429 * rather than retrying.
431 strcpy(pathname, PATH_TEMPLATE);
433 if (mkfifo(pathname, 0600) == -1)
434 errx(-1, "FAIL: aio_fifo_test: mkfifo: %s", strerror(errno));
435 arg.afa_pathname = pathname;
436 arg.afa_read_fd = -1;
437 arg.afa_write_fd = -1;
439 read_fd = open(pathname, O_RDONLY | O_NONBLOCK);
442 aio_fifo_cleanup(&arg);
444 errx(-1, "FAIL: aio_fifo_test: read_fd open: %s",
447 arg.afa_read_fd = read_fd;
449 write_fd = open(pathname, O_WRONLY);
450 if (write_fd == -1) {
452 aio_fifo_cleanup(&arg);
454 errx(-1, "FAIL: aio_fifo_test: write_fd open: %s",
457 arg.afa_write_fd = write_fd;
459 aio_context_init(&ac, "aio_fifo_test", read_fd, write_fd, FIFO_LEN,
460 FIFO_TIMEOUT, aio_fifo_cleanup, &arg);
464 aio_fifo_cleanup(&arg);
466 fprintf(stderr, "PASS: aio_fifo_test\n");
469 struct aio_unix_socketpair_arg {
474 aio_unix_socketpair_cleanup(void *arg)
476 struct aio_unix_socketpair_arg *asa;
479 close(asa->asa_sockets[0]);
480 close(asa->asa_sockets[1]);
483 #define UNIX_SOCKETPAIR_LEN 256
484 #define UNIX_SOCKETPAIR_TIMEOUT 30
486 aio_unix_socketpair_test(void)
488 struct aio_unix_socketpair_arg arg;
489 struct aio_context ac;
492 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) < 0)
493 errx(-1, "FAIL: aio_socketpair_test: socketpair: %s",
496 arg.asa_sockets[0] = sockets[0];
497 arg.asa_sockets[1] = sockets[1];
498 aio_context_init(&ac, "aio_unix_socketpair_test", sockets[0],
499 sockets[1], UNIX_SOCKETPAIR_LEN, UNIX_SOCKETPAIR_TIMEOUT,
500 aio_unix_socketpair_cleanup, &arg);
504 aio_unix_socketpair_cleanup(&arg);
506 fprintf(stderr, "PASS: aio_unix_socketpair_test\n");
515 aio_pty_cleanup(void *arg)
517 struct aio_pty_arg *apa;
520 close(apa->apa_read_fd);
521 close(apa->apa_write_fd);
525 #define PTY_TIMEOUT 30
529 struct aio_pty_arg arg;
530 struct aio_context ac;
531 int read_fd, write_fd;
535 if (openpty(&read_fd, &write_fd, NULL, NULL, NULL) < 0)
536 errx(-1, "FAIL: aio_pty_test: openpty: %s", strerror(errno));
538 arg.apa_read_fd = read_fd;
539 arg.apa_write_fd = write_fd;
541 if (tcgetattr(write_fd, &ts) < 0) {
543 aio_pty_cleanup(&arg);
545 errx(-1, "FAIL: aio_pty_test: tcgetattr: %s",
549 if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
551 aio_pty_cleanup(&arg);
553 errx(-1, "FAIL: aio_pty_test: tcsetattr: %s",
557 aio_context_init(&ac, "aio_pty_test", read_fd, write_fd, PTY_LEN,
558 PTY_TIMEOUT, aio_pty_cleanup, &arg);
562 aio_pty_cleanup(&arg);
564 fprintf(stderr, "PASS: aio_pty_test\n");
568 aio_pipe_cleanup(void *arg)
577 #define PIPE_TIMEOUT 30
581 struct aio_context ac;
585 errx(-1, "FAIL: aio_pipe_test: pipe: %s", strerror(errno));
587 aio_context_init(&ac, "aio_file_test", pipes[0], pipes[1], PIPE_LEN,
588 PIPE_TIMEOUT, aio_pipe_cleanup, pipes);
592 aio_pipe_cleanup(pipes);
594 fprintf(stderr, "PASS: aio_pipe_test\n");
604 aio_md_cleanup(void *arg)
606 struct aio_md_arg *ama;
607 struct md_ioctl mdio;
612 if (ama->ama_fd != -1)
615 if (ama->ama_unit != -1) {
616 bzero(&mdio, sizeof(mdio));
617 mdio.md_version = MDIOVERSION;
618 mdio.md_unit = ama->ama_unit;
619 if (ioctl(ama->ama_mdctl_fd, MDIOCDETACH, &mdio) < 0) {
621 close(ama->ama_mdctl_fd);
623 warnx("FAIL: aio_md_test: MDIOCDETACH: %s",
628 close(ama->ama_mdctl_fd);
631 #define MD_LEN GLOBAL_MAX
632 #define MD_TIMEOUT 30
636 int error, fd, i, mdctl_fd, unit;
637 char pathname[PATH_MAX];
638 struct aio_md_arg arg;
639 struct aio_context ac;
640 struct md_ioctl mdio;
642 mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
644 errx(-1, "FAIL: aio_md_test: open(/dev/%s): %s", MDCTL_NAME,
647 bzero(&mdio, sizeof(mdio));
648 mdio.md_version = MDIOVERSION;
649 mdio.md_type = MD_MALLOC;
650 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
651 mdio.md_mediasize = GLOBAL_MAX;
652 mdio.md_sectorsize = 512;
654 arg.ama_mdctl_fd = mdctl_fd;
657 if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
659 aio_md_cleanup(&arg);
661 errx(-1, "FAIL: aio_md_test: MDIOCATTACH: %s",
665 arg.ama_unit = unit = mdio.md_unit;
666 snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
667 fd = open(pathname, O_RDWR);
670 aio_md_cleanup(&arg);
672 errx(-1, "FAIL: aio_md_test: open(%s): %s", pathname,
677 aio_context_init(&ac, "aio_md_test", fd, fd, MD_LEN, MD_TIMEOUT,
678 aio_md_cleanup, &arg);
682 aio_md_cleanup(&arg);
684 fprintf(stderr, "PASS: aio_md_test\n");
688 main(int argc, char *argv[])
694 aio_unix_socketpair_test();
700 fprintf(stderr, "WARNING: aio_md_test: skipped as euid "