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