2005-03-02 03:32:01 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (C) 2005 IronPort Systems, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-02-22 05:13:26 +00:00
|
|
|
* Prerequisities:
|
|
|
|
* - AIO support must be compiled into the kernel (see sys/<arch>/NOTES for
|
|
|
|
* more details).
|
|
|
|
*
|
2005-03-02 03:32:01 +00:00
|
|
|
* Note: it is a good idea to run this against a physical drive to
|
|
|
|
* exercise the physio fast path (ie. aio_kqueue /dev/<something safe>)
|
|
|
|
*/
|
|
|
|
|
2011-02-22 05:13:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/time.h>
|
2005-03-02 03:32:01 +00:00
|
|
|
#include <aio.h>
|
2011-02-22 05:13:26 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2005-03-02 03:32:01 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2011-02-22 05:13:26 +00:00
|
|
|
#include <string.h>
|
2005-03-02 03:32:01 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-04-28 10:53:06 +00:00
|
|
|
#include "freebsd_test_suite/macros.h"
|
2016-03-01 18:12:14 +00:00
|
|
|
#include "local.h"
|
2005-03-02 03:32:01 +00:00
|
|
|
|
2015-04-28 10:53:06 +00:00
|
|
|
#define PATH_TEMPLATE "aio.XXXXXXXXXX"
|
|
|
|
|
|
|
|
#define MAX_IOCBS 128
|
2005-03-02 03:32:01 +00:00
|
|
|
#define MAX_RUNS 300
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
2011-02-22 05:13:26 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2015-04-28 10:53:06 +00:00
|
|
|
struct aiocb *iocb[MAX_IOCBS], *kq_iocb;
|
|
|
|
char *file, pathname[sizeof(PATH_TEMPLATE)+1];
|
2005-03-02 03:32:01 +00:00
|
|
|
struct kevent ke, kq_returned;
|
|
|
|
struct timespec ts;
|
2015-04-28 10:53:06 +00:00
|
|
|
char buffer[32768];
|
2015-12-28 02:01:41 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
int cancel, error;
|
|
|
|
#endif
|
|
|
|
int failed = 0, fd, kq, pending, result, run;
|
2015-04-28 10:53:06 +00:00
|
|
|
int tmp_file = 0;
|
|
|
|
unsigned i, j;
|
|
|
|
|
|
|
|
PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
|
2016-03-01 18:12:14 +00:00
|
|
|
PLAIN_REQUIRE_UNSAFE_AIO(0);
|
2005-03-02 03:32:01 +00:00
|
|
|
|
2015-04-28 10:53:06 +00:00
|
|
|
kq = kqueue();
|
2005-03-02 03:32:01 +00:00
|
|
|
if (kq < 0) {
|
|
|
|
perror("No kqeueue\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
strcpy(pathname, PATH_TEMPLATE);
|
|
|
|
fd = mkstemp(pathname);
|
|
|
|
file = pathname;
|
|
|
|
tmp_file = 1;
|
|
|
|
} else {
|
|
|
|
file = argv[1];
|
|
|
|
fd = open(file, O_RDWR|O_CREAT, 0666);
|
|
|
|
}
|
2011-02-22 05:13:26 +00:00
|
|
|
if (fd == -1)
|
|
|
|
err(1, "Can't open %s\n", file);
|
2005-03-02 03:32:01 +00:00
|
|
|
|
|
|
|
for (run = 0; run < MAX_RUNS; run++){
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Run %d\n", run);
|
|
|
|
#endif
|
2015-04-28 10:53:06 +00:00
|
|
|
for (i = 0; i < nitems(iocb); i++) {
|
2011-02-22 05:13:26 +00:00
|
|
|
iocb[i] = (struct aiocb *)calloc(1,
|
|
|
|
sizeof(struct aiocb));
|
|
|
|
if (iocb[i] == NULL)
|
|
|
|
err(1, "calloc");
|
2005-03-02 03:32:01 +00:00
|
|
|
}
|
2015-12-28 02:01:41 +00:00
|
|
|
|
|
|
|
pending = 0;
|
2015-04-28 10:53:06 +00:00
|
|
|
for (i = 0; i < nitems(iocb); i++) {
|
2005-03-02 03:32:01 +00:00
|
|
|
pending++;
|
|
|
|
iocb[i]->aio_nbytes = sizeof(buffer);
|
|
|
|
iocb[i]->aio_buf = buffer;
|
|
|
|
iocb[i]->aio_fildes = fd;
|
|
|
|
iocb[i]->aio_offset = iocb[i]->aio_nbytes * i * run;
|
2015-12-28 02:01:41 +00:00
|
|
|
|
2005-03-02 03:32:01 +00:00
|
|
|
iocb[i]->aio_sigevent.sigev_notify_kqueue = kq;
|
2006-01-22 03:46:03 +00:00
|
|
|
iocb[i]->aio_sigevent.sigev_value.sival_ptr = iocb[i];
|
2005-03-02 03:32:01 +00:00
|
|
|
iocb[i]->aio_sigevent.sigev_notify = SIGEV_KEVENT;
|
2015-12-28 02:01:41 +00:00
|
|
|
|
2005-03-02 03:32:01 +00:00
|
|
|
result = aio_write(iocb[i]);
|
|
|
|
if (result != 0) {
|
|
|
|
perror("aio_write");
|
2011-02-22 05:13:26 +00:00
|
|
|
printf("Result %d iteration %d\n", result, i);
|
2005-03-02 03:32:01 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("WRITE %d is at %p\n", i, iocb[i]);
|
|
|
|
#endif
|
|
|
|
result = rand();
|
|
|
|
if (result < RAND_MAX/32) {
|
|
|
|
if (result > RAND_MAX/64) {
|
|
|
|
result = aio_cancel(fd, iocb[i]);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Cancel %d %p result %d\n", i, iocb[i], result);
|
|
|
|
#endif
|
|
|
|
if (result == AIO_CANCELED) {
|
|
|
|
aio_return(iocb[i]);
|
2011-02-22 05:13:26 +00:00
|
|
|
iocb[i] = NULL;
|
2005-03-02 03:32:01 +00:00
|
|
|
pending--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 02:01:41 +00:00
|
|
|
#ifdef DEBUG
|
2015-04-28 10:53:06 +00:00
|
|
|
cancel = nitems(iocb) - pending;
|
2015-12-28 02:01:41 +00:00
|
|
|
#endif
|
2015-04-28 10:53:06 +00:00
|
|
|
|
2005-03-02 03:32:01 +00:00
|
|
|
i = 0;
|
2011-02-22 05:13:26 +00:00
|
|
|
while (pending) {
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
2005-03-02 03:32:01 +00:00
|
|
|
bzero(&ke, sizeof(ke));
|
|
|
|
bzero(&kq_returned, sizeof(ke));
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 1;
|
2015-12-28 02:01:41 +00:00
|
|
|
result = kevent(kq, NULL, 0,
|
2005-03-02 03:32:01 +00:00
|
|
|
&kq_returned, 1, &ts);
|
2015-12-28 02:01:41 +00:00
|
|
|
#ifdef DEBUG
|
2005-03-02 03:32:01 +00:00
|
|
|
error = errno;
|
2015-12-28 02:01:41 +00:00
|
|
|
#endif
|
2011-02-22 05:13:26 +00:00
|
|
|
if (result < 0)
|
2005-03-02 03:32:01 +00:00
|
|
|
perror("kevent error: ");
|
|
|
|
kq_iocb = kq_returned.udata;
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("kevent %d %d errno %d return.ident %p "
|
2015-12-28 02:01:41 +00:00
|
|
|
"return.data %p return.udata %p %p\n",
|
|
|
|
i, result, error,
|
2017-05-21 15:37:08 +00:00
|
|
|
(void*)kq_returned.ident,
|
|
|
|
(void*)kq_returned.data,
|
2015-12-28 02:01:41 +00:00
|
|
|
kq_returned.udata,
|
2005-03-02 03:32:01 +00:00
|
|
|
kq_iocb);
|
|
|
|
#endif
|
2015-12-28 02:01:41 +00:00
|
|
|
|
2011-02-22 05:13:26 +00:00
|
|
|
if (kq_iocb)
|
2005-03-02 03:32:01 +00:00
|
|
|
break;
|
|
|
|
#ifdef DEBUG
|
2017-05-21 15:37:08 +00:00
|
|
|
printf("Try again left %d out of %lu %d\n",
|
2015-04-28 10:53:06 +00:00
|
|
|
pending, nitems(iocb), cancel);
|
2005-03-02 03:32:01 +00:00
|
|
|
#endif
|
2015-12-28 02:01:41 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 10:53:06 +00:00
|
|
|
for (j = 0; j < nitems(iocb) && iocb[j] != kq_iocb;
|
2011-02-22 05:13:26 +00:00
|
|
|
j++) ;
|
2005-03-02 03:32:01 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("kq_iocb %p\n", kq_iocb);
|
2015-12-28 02:01:41 +00:00
|
|
|
|
2011-02-22 05:13:26 +00:00
|
|
|
printf("Error Result for %d is %d pending %d\n",
|
|
|
|
j, result, pending);
|
2005-03-02 03:32:01 +00:00
|
|
|
#endif
|
|
|
|
result = aio_return(kq_iocb);
|
|
|
|
#ifdef DEBUG
|
2011-02-22 05:13:26 +00:00
|
|
|
printf("Return Result for %d is %d\n\n", j, result);
|
2005-03-02 03:32:01 +00:00
|
|
|
#endif
|
|
|
|
if (result != sizeof(buffer)) {
|
2011-02-22 05:13:26 +00:00
|
|
|
printf("FAIL: run %d, operation %d, result %d "
|
2015-03-31 06:51:13 +00:00
|
|
|
" (errno=%d) should be %zu\n", run, pending,
|
2011-02-22 05:13:26 +00:00
|
|
|
result, errno, sizeof(buffer));
|
|
|
|
failed++;
|
|
|
|
} else
|
|
|
|
printf("PASS: run %d, left %d\n", run,
|
|
|
|
pending - 1);
|
2005-03-02 03:32:01 +00:00
|
|
|
|
|
|
|
free(kq_iocb);
|
|
|
|
iocb[j] = NULL;
|
|
|
|
pending--;
|
|
|
|
i++;
|
2015-12-28 02:01:41 +00:00
|
|
|
}
|
2011-02-22 05:13:26 +00:00
|
|
|
|
2015-04-28 10:53:06 +00:00
|
|
|
for (i = 0; i < nitems(iocb); i++)
|
2011-02-22 05:13:26 +00:00
|
|
|
free(iocb[i]);
|
|
|
|
|
2005-03-02 03:32:01 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 05:13:26 +00:00
|
|
|
if (tmp_file)
|
2005-03-02 03:32:01 +00:00
|
|
|
unlink(pathname);
|
|
|
|
|
2011-02-22 05:13:26 +00:00
|
|
|
if (failed != 0)
|
|
|
|
printf("FAIL: %d tests failed\n", failed);
|
|
|
|
else
|
|
|
|
printf("PASS: All tests passed\n");
|
|
|
|
|
|
|
|
exit (failed == 0 ? 0 : 1);
|
2005-03-02 03:32:01 +00:00
|
|
|
}
|