1f44eb013SEnji Cooper /*-
2f44eb013SEnji Cooper * Copyright (C) 2005 IronPort Systems, Inc. All rights reserved.
3f44eb013SEnji Cooper *
4f44eb013SEnji Cooper * Redistribution and use in source and binary forms, with or without
5f44eb013SEnji Cooper * modification, are permitted provided that the following conditions
6f44eb013SEnji Cooper * are met:
7f44eb013SEnji Cooper * 1. Redistributions of source code must retain the above copyright
8f44eb013SEnji Cooper * notice, this list of conditions and the following disclaimer.
9f44eb013SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
10f44eb013SEnji Cooper * notice, this list of conditions and the following disclaimer in the
11f44eb013SEnji Cooper * documentation and/or other materials provided with the distribution.
12f44eb013SEnji Cooper *
13f44eb013SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14f44eb013SEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15f44eb013SEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16f44eb013SEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17f44eb013SEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18f44eb013SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19f44eb013SEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20f44eb013SEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21f44eb013SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22f44eb013SEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23f44eb013SEnji Cooper * SUCH DAMAGE.
24f44eb013SEnji Cooper */
25f44eb013SEnji Cooper
26f44eb013SEnji Cooper /*
27f44eb013SEnji Cooper * Note: it is a good idea to run this against a physical drive to
28c52ef9bbSAlan Somers * exercise the physio fast path (ie. lio_kqueue_test /dev/<something safe>)
29f44eb013SEnji Cooper */
30f44eb013SEnji Cooper
31f44eb013SEnji Cooper #include <sys/types.h>
32f44eb013SEnji Cooper #include <sys/event.h>
33f44eb013SEnji Cooper #include <sys/time.h>
34f44eb013SEnji Cooper #include <aio.h>
35f44eb013SEnji Cooper #include <fcntl.h>
36fc8e5f5fSEnji Cooper #include <err.h>
37f44eb013SEnji Cooper #include <errno.h>
38f44eb013SEnji Cooper #include <stdio.h>
39f44eb013SEnji Cooper #include <stdlib.h>
40f44eb013SEnji Cooper #include <string.h>
41f44eb013SEnji Cooper #include <unistd.h>
42f44eb013SEnji Cooper
434e5f38d6SEnji Cooper #include "freebsd_test_suite/macros.h"
44f3215338SJohn Baldwin #include "local.h"
454e5f38d6SEnji Cooper
464e5f38d6SEnji Cooper #define PATH_TEMPLATE "aio.XXXXXXXXXX"
47f44eb013SEnji Cooper
48f44eb013SEnji Cooper #define LIO_MAX 5
49c52ef9bbSAlan Somers #define MAX_IOCBS_PER_LIO 64
50c52ef9bbSAlan Somers #define MAX_IOCBS (LIO_MAX * MAX_IOCBS_PER_LIO)
51f44eb013SEnji Cooper #define MAX_RUNS 300
52f44eb013SEnji Cooper
53f44eb013SEnji Cooper int
main(int argc,char * argv[])54fc8e5f5fSEnji Cooper main(int argc, char *argv[])
55fc8e5f5fSEnji Cooper {
56f44eb013SEnji Cooper int fd;
574e5f38d6SEnji Cooper struct aiocb *iocb[MAX_IOCBS];
580b0ac0beSEnji Cooper struct aiocb **lio[LIO_MAX], **kq_lio;
59c52ef9bbSAlan Somers int i, result, run, error, j, k, max_queue_per_proc;
60c52ef9bbSAlan Somers int max_iocbs, iocbs_per_lio;
61c52ef9bbSAlan Somers size_t max_queue_per_proc_size;
62f44eb013SEnji Cooper char buffer[32768];
63fc8e5f5fSEnji Cooper int kq;
64*f10dd8afSAlan Somers struct kevent kq_returned;
65f44eb013SEnji Cooper struct timespec ts;
66f44eb013SEnji Cooper struct sigevent sig;
67f44eb013SEnji Cooper time_t time1, time2;
680b0ac0beSEnji Cooper char *file, pathname[sizeof(PATH_TEMPLATE)];
69f44eb013SEnji Cooper int tmp_file = 0, failed = 0;
70f44eb013SEnji Cooper
714e5f38d6SEnji Cooper PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
72f3215338SJohn Baldwin PLAIN_REQUIRE_UNSAFE_AIO(0);
734e5f38d6SEnji Cooper
74c52ef9bbSAlan Somers max_queue_per_proc_size = sizeof(max_queue_per_proc);
75c52ef9bbSAlan Somers if (sysctlbyname("vfs.aio.max_aio_queue_per_proc",
76c52ef9bbSAlan Somers &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0) != 0)
77c52ef9bbSAlan Somers err(1, "sysctlbyname");
78c52ef9bbSAlan Somers iocbs_per_lio = max_queue_per_proc / LIO_MAX;
79c52ef9bbSAlan Somers max_iocbs = LIO_MAX * iocbs_per_lio;
80c52ef9bbSAlan Somers
81fc8e5f5fSEnji Cooper kq = kqueue();
82fc8e5f5fSEnji Cooper if (kq < 0)
83fc8e5f5fSEnji Cooper err(1, "kqeueue(2) failed");
84f44eb013SEnji Cooper
85f44eb013SEnji Cooper if (argc == 1) {
86f44eb013SEnji Cooper strcpy(pathname, PATH_TEMPLATE);
87f44eb013SEnji Cooper fd = mkstemp(pathname);
88f44eb013SEnji Cooper file = pathname;
89f44eb013SEnji Cooper tmp_file = 1;
90f44eb013SEnji Cooper } else {
91f44eb013SEnji Cooper file = argv[1];
92f44eb013SEnji Cooper fd = open(file, O_RDWR|O_CREAT, 0666);
93f44eb013SEnji Cooper }
94fc8e5f5fSEnji Cooper if (fd < 0)
95fc8e5f5fSEnji Cooper err(1, "can't open %s", argv[1]);
96f44eb013SEnji Cooper
97f44eb013SEnji Cooper #ifdef DEBUG
98f44eb013SEnji Cooper printf("Hello kq %d fd %d\n", kq, fd);
99f44eb013SEnji Cooper #endif
100f44eb013SEnji Cooper
101f44eb013SEnji Cooper for (run = 0; run < MAX_RUNS; run++) {
102f44eb013SEnji Cooper #ifdef DEBUG
103f44eb013SEnji Cooper printf("Run %d\n", run);
104f44eb013SEnji Cooper #endif
105f44eb013SEnji Cooper for (j = 0; j < LIO_MAX; j++) {
106fc8e5f5fSEnji Cooper lio[j] =
107c52ef9bbSAlan Somers malloc(sizeof(struct aiocb *) * iocbs_per_lio);
108c52ef9bbSAlan Somers for (i = 0; i < iocbs_per_lio; i++) {
109c52ef9bbSAlan Somers k = (iocbs_per_lio * j) + i;
110fc8e5f5fSEnji Cooper lio[j][i] = iocb[k] =
111fc8e5f5fSEnji Cooper calloc(1, sizeof(struct aiocb));
112f44eb013SEnji Cooper iocb[k]->aio_nbytes = sizeof(buffer);
113f44eb013SEnji Cooper iocb[k]->aio_buf = buffer;
114f44eb013SEnji Cooper iocb[k]->aio_fildes = fd;
115f44eb013SEnji Cooper iocb[k]->aio_offset
116f44eb013SEnji Cooper = iocb[k]->aio_nbytes * k * (run + 1);
117f44eb013SEnji Cooper
118f44eb013SEnji Cooper #ifdef DEBUG
119c4708ce8SAlan Somers printf("hello iocb[k] %jd\n",
120c4708ce8SAlan Somers (intmax_t)iocb[k]->aio_offset);
121f44eb013SEnji Cooper #endif
122f44eb013SEnji Cooper iocb[k]->aio_lio_opcode = LIO_WRITE;
123f44eb013SEnji Cooper }
124f44eb013SEnji Cooper sig.sigev_notify_kqueue = kq;
125f44eb013SEnji Cooper sig.sigev_value.sival_ptr = lio[j];
126f44eb013SEnji Cooper sig.sigev_notify = SIGEV_KEVENT;
127f44eb013SEnji Cooper time(&time1);
128f44eb013SEnji Cooper result = lio_listio(LIO_NOWAIT, lio[j],
129c52ef9bbSAlan Somers iocbs_per_lio, &sig);
130f44eb013SEnji Cooper error = errno;
131f44eb013SEnji Cooper time(&time2);
132f44eb013SEnji Cooper #ifdef DEBUG
133c4708ce8SAlan Somers printf("Time %jd %jd %jd result -> %d\n",
134c4708ce8SAlan Somers (intmax_t)time1, (intmax_t)time2,
135c4708ce8SAlan Somers (intmax_t)time2-time1, result);
136f44eb013SEnji Cooper #endif
137f44eb013SEnji Cooper if (result != 0) {
138f44eb013SEnji Cooper errno = error;
139fc8e5f5fSEnji Cooper err(1, "FAIL: Result %d iteration %d\n",
140fc8e5f5fSEnji Cooper result, j);
141f44eb013SEnji Cooper }
142f44eb013SEnji Cooper #ifdef DEBUG
143f44eb013SEnji Cooper printf("write %d is at %p\n", j, lio[j]);
144f44eb013SEnji Cooper #endif
145f44eb013SEnji Cooper }
146f44eb013SEnji Cooper
147f44eb013SEnji Cooper for (i = 0; i < LIO_MAX; i++) {
148f44eb013SEnji Cooper for (j = LIO_MAX - 1; j >=0; j--) {
149f44eb013SEnji Cooper if (lio[j])
150f44eb013SEnji Cooper break;
151f44eb013SEnji Cooper }
152f44eb013SEnji Cooper
153f44eb013SEnji Cooper for (;;) {
154*f10dd8afSAlan Somers bzero(&kq_returned, sizeof(kq_returned));
155f44eb013SEnji Cooper ts.tv_sec = 0;
156f44eb013SEnji Cooper ts.tv_nsec = 1;
157f44eb013SEnji Cooper #ifdef DEBUG
158f44eb013SEnji Cooper printf("FOO lio %d -> %p\n", j, lio[j]);
159f44eb013SEnji Cooper #endif
160f44eb013SEnji Cooper result = kevent(kq, NULL, 0,
161f44eb013SEnji Cooper &kq_returned, 1, &ts);
162f44eb013SEnji Cooper error = errno;
163f44eb013SEnji Cooper if (result < 0) {
164f44eb013SEnji Cooper perror("kevent error: ");
165f44eb013SEnji Cooper }
166f44eb013SEnji Cooper kq_lio = kq_returned.udata;
167f44eb013SEnji Cooper #ifdef DEBUG
168f44eb013SEnji Cooper printf("kevent %d %d errno %d return.ident %p "
169f44eb013SEnji Cooper "return.data %p return.udata %p %p\n",
170f44eb013SEnji Cooper i, result, error,
17195c91f3bSAlan Somers (void*)kq_returned.ident,
17295c91f3bSAlan Somers (void*)kq_returned.data,
173f44eb013SEnji Cooper kq_returned.udata,
174f44eb013SEnji Cooper lio[j]);
175f44eb013SEnji Cooper #endif
176f44eb013SEnji Cooper
177f44eb013SEnji Cooper if (kq_lio)
178f44eb013SEnji Cooper break;
179f44eb013SEnji Cooper #ifdef DEBUG
180f44eb013SEnji Cooper printf("Try again\n");
181f44eb013SEnji Cooper #endif
182f44eb013SEnji Cooper }
183f44eb013SEnji Cooper
184f44eb013SEnji Cooper #ifdef DEBUG
185f44eb013SEnji Cooper printf("lio %p\n", lio);
186f44eb013SEnji Cooper #endif
187f44eb013SEnji Cooper
188f44eb013SEnji Cooper for (j = 0; j < LIO_MAX; j++) {
189fc8e5f5fSEnji Cooper if (lio[j] == kq_lio)
190f44eb013SEnji Cooper break;
191f44eb013SEnji Cooper }
192fc8e5f5fSEnji Cooper if (j == LIO_MAX)
193fc8e5f5fSEnji Cooper errx(1, "FAIL: ");
194f44eb013SEnji Cooper
195f44eb013SEnji Cooper #ifdef DEBUG
196f44eb013SEnji Cooper printf("Error Result for %d is %d\n", j, result);
197f44eb013SEnji Cooper #endif
198f44eb013SEnji Cooper if (result < 0) {
199f44eb013SEnji Cooper printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
200fc8e5f5fSEnji Cooper failed++;
201fc8e5f5fSEnji Cooper } else
202f44eb013SEnji Cooper printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
203c52ef9bbSAlan Somers for (k = 0; k < max_iocbs / LIO_MAX; k++) {
204f44eb013SEnji Cooper result = aio_return(kq_lio[k]);
205f44eb013SEnji Cooper #ifdef DEBUG
206c52ef9bbSAlan Somers printf("Return Result for %d %d is %d\n", j, k, result);
207f44eb013SEnji Cooper #endif
208f44eb013SEnji Cooper if (result != sizeof(buffer)) {
209f44eb013SEnji Cooper printf("FAIL: run %d, operation %d sub-opt %d result %d (errno=%d) should be %zu\n",
210f44eb013SEnji Cooper run, LIO_MAX - i -1, k, result, errno, sizeof(buffer));
211f44eb013SEnji Cooper } else {
212f44eb013SEnji Cooper printf("PASS: run %d, operation %d sub-opt %d result %d\n",
213f44eb013SEnji Cooper run, LIO_MAX - i -1, k, result);
214f44eb013SEnji Cooper }
215f44eb013SEnji Cooper }
216f44eb013SEnji Cooper #ifdef DEBUG
217f44eb013SEnji Cooper printf("\n");
218f44eb013SEnji Cooper #endif
219f44eb013SEnji Cooper
220c52ef9bbSAlan Somers for (k = 0; k < max_iocbs / LIO_MAX; k++)
221f44eb013SEnji Cooper free(lio[j][k]);
222f44eb013SEnji Cooper free(lio[j]);
223f44eb013SEnji Cooper lio[j] = NULL;
224f44eb013SEnji Cooper }
225f44eb013SEnji Cooper }
226f44eb013SEnji Cooper #ifdef DEBUG
227f44eb013SEnji Cooper printf("Done\n");
228f44eb013SEnji Cooper #endif
229f44eb013SEnji Cooper
230fc8e5f5fSEnji Cooper if (tmp_file)
231f44eb013SEnji Cooper unlink(pathname);
232f44eb013SEnji Cooper
233fc8e5f5fSEnji Cooper if (failed)
234fc8e5f5fSEnji Cooper errx(1, "FAIL: %d testcases failed", failed);
235fc8e5f5fSEnji Cooper else
236fc8e5f5fSEnji Cooper errx(0, "PASS: All\n");
237fc8e5f5fSEnji Cooper
238f44eb013SEnji Cooper }
239