xref: /aosp_15_r20/external/ltp/testcases/kernel/io/aio/aio01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2003
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * NAME
22*49cdfc7eSAndroid Build Coastguard Worker  *      aiotest1.c
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker  *      Perform aio read, write operations for given number of requests.
26*49cdfc7eSAndroid Build Coastguard Worker  *      Submit i/o for each request individually.
27*49cdfc7eSAndroid Build Coastguard Worker  *      Repeat the test for each of the following cases and measure time.
28*49cdfc7eSAndroid Build Coastguard Worker  *              Testblock1: Write one request at a time.
29*49cdfc7eSAndroid Build Coastguard Worker  *              Testblock2: Read one request at a time.
30*49cdfc7eSAndroid Build Coastguard Worker  *              Testblock3: Prepare, Write one request at a time.
31*49cdfc7eSAndroid Build Coastguard Worker  *              Testblock4: Prepare, Read one request at a time.
32*49cdfc7eSAndroid Build Coastguard Worker  *              Testblock5: Prepare, Write/Read one request at a time.
33*49cdfc7eSAndroid Build Coastguard Worker  *              Testblock6: Prepare, Write/Read/Verify one request at a time.
34*49cdfc7eSAndroid Build Coastguard Worker  *
35*49cdfc7eSAndroid Build Coastguard Worker  * Author
36*49cdfc7eSAndroid Build Coastguard Worker  * 08/24/2002   Narasimha Sharoff       [email protected]
37*49cdfc7eSAndroid Build Coastguard Worker */
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker /*
40*49cdfc7eSAndroid Build Coastguard Worker  * History
41*49cdfc7eSAndroid Build Coastguard Worker  *      04/18/2003      [email protected]
42*49cdfc7eSAndroid Build Coastguard Worker  *      		Updated
43*49cdfc7eSAndroid Build Coastguard Worker  *      05/21/2003      Paul Larson	[email protected]
44*49cdfc7eSAndroid Build Coastguard Worker  *      		Rewrote the test under LTP, using LTP test harness
45*49cdfc7eSAndroid Build Coastguard Worker  *      		and other minor improvements and fixes
46*49cdfc7eSAndroid Build Coastguard Worker */
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #define _XOPEN_SOURCE 600
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
57*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
58*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
59*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
62*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "aio01";
65*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 6;
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_LIBAIO
68*49cdfc7eSAndroid Build Coastguard Worker #include <libaio.h>
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker static void help(void);
71*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
72*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker #define mapsize (1 << 14)
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker int fd;
77*49cdfc7eSAndroid Build Coastguard Worker char *maddr;
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker size_t bufsize;			/* Size of I/O, 8k default */
80*49cdfc7eSAndroid Build Coastguard Worker io_context_t io_ctx;		/* I/O Context */
81*49cdfc7eSAndroid Build Coastguard Worker struct iocb **iocbs;		/* I/O Control Blocks */
82*49cdfc7eSAndroid Build Coastguard Worker char *srcbuf, *dstbuf;
83*49cdfc7eSAndroid Build Coastguard Worker char fname[128];
84*49cdfc7eSAndroid Build Coastguard Worker char tbuf[80];
85*49cdfc7eSAndroid Build Coastguard Worker int pos, nr;
86*49cdfc7eSAndroid Build Coastguard Worker struct stat s;
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
89*49cdfc7eSAndroid Build Coastguard Worker 	off_t newsize;
90*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
91*49cdfc7eSAndroid Build Coastguard Worker } TC[] = {
92*49cdfc7eSAndroid Build Coastguard Worker 	{
93*49cdfc7eSAndroid Build Coastguard Worker 	mapsize - 8192, "ftruncate mmaped file to a smaller size"}, {
94*49cdfc7eSAndroid Build Coastguard Worker 	mapsize + 1024, "ftruncate mmaped file to a larger size"}, {
95*49cdfc7eSAndroid Build Coastguard Worker 0, "ftruncate mmaped file to 0 size"},};
96*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char ** argv)97*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
98*49cdfc7eSAndroid Build Coastguard Worker {
99*49cdfc7eSAndroid Build Coastguard Worker 	int i, j, sec, usec;
100*49cdfc7eSAndroid Build Coastguard Worker 	int failflag = 0;
101*49cdfc7eSAndroid Build Coastguard Worker 	int bflag = 0, nflag = 0, Fflag = 0;
102*49cdfc7eSAndroid Build Coastguard Worker 	char *optb, *optn, *optF;
103*49cdfc7eSAndroid Build Coastguard Worker 	struct io_event event;
104*49cdfc7eSAndroid Build Coastguard Worker 	static struct timespec ts;
105*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval stv, etv;
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	option_t options[] = {
108*49cdfc7eSAndroid Build Coastguard Worker 		{"b:", &bflag, &optb},
109*49cdfc7eSAndroid Build Coastguard Worker 		{"n:", &nflag, &optn},
110*49cdfc7eSAndroid Build Coastguard Worker 		{"F:", &Fflag, &optF},
111*49cdfc7eSAndroid Build Coastguard Worker 		{NULL, NULL, NULL}
112*49cdfc7eSAndroid Build Coastguard Worker 	};
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, options, &help);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	bufsize = (bflag ? atoi(optb) : 8192);
117*49cdfc7eSAndroid Build Coastguard Worker 	nr = (nflag ? atoi(optn) : 10);
118*49cdfc7eSAndroid Build Coastguard Worker 	if (Fflag) {
119*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(fname, "%s", optF);
120*49cdfc7eSAndroid Build Coastguard Worker 	} else {
121*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(fname, "aiofile");
122*49cdfc7eSAndroid Build Coastguard Worker 	}
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	setup();
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker /* TEST 1 */
127*49cdfc7eSAndroid Build Coastguard Worker 	pos = 0;
128*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&stv, NULL);
129*49cdfc7eSAndroid Build Coastguard Worker 	io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
130*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nr; i++) {
131*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
132*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
133*49cdfc7eSAndroid Build Coastguard Worker 		do {
134*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
135*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
136*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
137*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 1: io_submit failed - retval=%ld"
138*49cdfc7eSAndroid Build Coastguard Worker 				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
139*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
140*49cdfc7eSAndroid Build Coastguard Worker 			continue;
141*49cdfc7eSAndroid Build Coastguard Worker 		}
142*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
143*49cdfc7eSAndroid Build Coastguard Worker 		gettimeofday(&etv, NULL);
144*49cdfc7eSAndroid Build Coastguard Worker 	}
145*49cdfc7eSAndroid Build Coastguard Worker 	if (!failflag) {
146*49cdfc7eSAndroid Build Coastguard Worker 		sec = etv.tv_sec - stv.tv_sec;
147*49cdfc7eSAndroid Build Coastguard Worker 		usec = etv.tv_usec - stv.tv_usec;
148*49cdfc7eSAndroid Build Coastguard Worker 		if (usec < 0) {
149*49cdfc7eSAndroid Build Coastguard Worker 			usec += 1000000;
150*49cdfc7eSAndroid Build Coastguard Worker 			sec--;
151*49cdfc7eSAndroid Build Coastguard Worker 		}
152*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test 1: %d writes in %3d.%06d sec",
153*49cdfc7eSAndroid Build Coastguard Worker 			 nr, sec, usec);
154*49cdfc7eSAndroid Build Coastguard Worker 	}
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker /* TEST 2 */
157*49cdfc7eSAndroid Build Coastguard Worker 	pos = 0;
158*49cdfc7eSAndroid Build Coastguard Worker 	failflag = 0;
159*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&stv, NULL);
160*49cdfc7eSAndroid Build Coastguard Worker 	io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
161*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nr; i++) {
162*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
163*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
164*49cdfc7eSAndroid Build Coastguard Worker 		do {
165*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
166*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
167*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
168*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 2: io_submit failed - retval=%ld"
169*49cdfc7eSAndroid Build Coastguard Worker 				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
170*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
171*49cdfc7eSAndroid Build Coastguard Worker 			continue;
172*49cdfc7eSAndroid Build Coastguard Worker 		}
173*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
174*49cdfc7eSAndroid Build Coastguard Worker 		gettimeofday(&etv, NULL);
175*49cdfc7eSAndroid Build Coastguard Worker 	}
176*49cdfc7eSAndroid Build Coastguard Worker 	if (!failflag) {
177*49cdfc7eSAndroid Build Coastguard Worker 		sec = etv.tv_sec - stv.tv_sec;
178*49cdfc7eSAndroid Build Coastguard Worker 		usec = etv.tv_usec - stv.tv_usec;
179*49cdfc7eSAndroid Build Coastguard Worker 		if (usec < 0) {
180*49cdfc7eSAndroid Build Coastguard Worker 			usec += 1000000;
181*49cdfc7eSAndroid Build Coastguard Worker 			sec--;
182*49cdfc7eSAndroid Build Coastguard Worker 		}
183*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test 2: %d reads in %3d.%06d sec",
184*49cdfc7eSAndroid Build Coastguard Worker 			 nr, sec, usec);
185*49cdfc7eSAndroid Build Coastguard Worker 	}
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker /* TEST 3 */
188*49cdfc7eSAndroid Build Coastguard Worker 	pos = 0;
189*49cdfc7eSAndroid Build Coastguard Worker 	failflag = 0;
190*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&stv, NULL);
191*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nr; i++) {
192*49cdfc7eSAndroid Build Coastguard Worker 		io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
193*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
194*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
195*49cdfc7eSAndroid Build Coastguard Worker 		do {
196*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
197*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
198*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
199*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 3: io_submit failed - retval=%ld"
200*49cdfc7eSAndroid Build Coastguard Worker 				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
201*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
202*49cdfc7eSAndroid Build Coastguard Worker 			continue;
203*49cdfc7eSAndroid Build Coastguard Worker 		}
204*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
205*49cdfc7eSAndroid Build Coastguard Worker 		gettimeofday(&etv, NULL);
206*49cdfc7eSAndroid Build Coastguard Worker 	}
207*49cdfc7eSAndroid Build Coastguard Worker 	if (!failflag) {
208*49cdfc7eSAndroid Build Coastguard Worker 		sec = etv.tv_sec - stv.tv_sec;
209*49cdfc7eSAndroid Build Coastguard Worker 		usec = etv.tv_usec - stv.tv_usec;
210*49cdfc7eSAndroid Build Coastguard Worker 		if (usec < 0) {
211*49cdfc7eSAndroid Build Coastguard Worker 			usec += 1000000;
212*49cdfc7eSAndroid Build Coastguard Worker 			sec--;
213*49cdfc7eSAndroid Build Coastguard Worker 		}
214*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test 3: %d prep,writes in %3d.%06d sec",
215*49cdfc7eSAndroid Build Coastguard Worker 			 nr, sec, usec);
216*49cdfc7eSAndroid Build Coastguard Worker 	}
217*49cdfc7eSAndroid Build Coastguard Worker 
218*49cdfc7eSAndroid Build Coastguard Worker /* TEST 4 */
219*49cdfc7eSAndroid Build Coastguard Worker 	pos = 0;
220*49cdfc7eSAndroid Build Coastguard Worker 	failflag = 0;
221*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&stv, NULL);
222*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nr; i++) {
223*49cdfc7eSAndroid Build Coastguard Worker 		io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
224*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
225*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
226*49cdfc7eSAndroid Build Coastguard Worker 		do {
227*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
228*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
229*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
230*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 4: io_submit failed - retval=%ld"
231*49cdfc7eSAndroid Build Coastguard Worker 				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
232*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
233*49cdfc7eSAndroid Build Coastguard Worker 			continue;
234*49cdfc7eSAndroid Build Coastguard Worker 		}
235*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
236*49cdfc7eSAndroid Build Coastguard Worker 		gettimeofday(&etv, NULL);
237*49cdfc7eSAndroid Build Coastguard Worker 	}
238*49cdfc7eSAndroid Build Coastguard Worker 	if (!failflag) {
239*49cdfc7eSAndroid Build Coastguard Worker 		sec = etv.tv_sec - stv.tv_sec;
240*49cdfc7eSAndroid Build Coastguard Worker 		usec = etv.tv_usec - stv.tv_usec;
241*49cdfc7eSAndroid Build Coastguard Worker 		if (usec < 0) {
242*49cdfc7eSAndroid Build Coastguard Worker 			usec += 1000000;
243*49cdfc7eSAndroid Build Coastguard Worker 			sec--;
244*49cdfc7eSAndroid Build Coastguard Worker 		}
245*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test 4: %d prep,reads in %3d.%06d sec",
246*49cdfc7eSAndroid Build Coastguard Worker 			 nr, sec, usec);
247*49cdfc7eSAndroid Build Coastguard Worker 	}
248*49cdfc7eSAndroid Build Coastguard Worker 
249*49cdfc7eSAndroid Build Coastguard Worker /* TEST 5 */
250*49cdfc7eSAndroid Build Coastguard Worker 	pos = 0;
251*49cdfc7eSAndroid Build Coastguard Worker 	failflag = 0;
252*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&stv, NULL);
253*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nr; i++) {
254*49cdfc7eSAndroid Build Coastguard Worker 		io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
255*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
256*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
257*49cdfc7eSAndroid Build Coastguard Worker 		do {
258*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
259*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
260*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
261*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 5: write io_submit failed - "
262*49cdfc7eSAndroid Build Coastguard Worker 				 "retval=%ld, errno=%d", TEST_RETURN,
263*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO);
264*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
265*49cdfc7eSAndroid Build Coastguard Worker 			continue;
266*49cdfc7eSAndroid Build Coastguard Worker 		}
267*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
268*49cdfc7eSAndroid Build Coastguard Worker 		io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
269*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
270*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
271*49cdfc7eSAndroid Build Coastguard Worker 		do {
272*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
273*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
274*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
275*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 5: read io_submit failed - "
276*49cdfc7eSAndroid Build Coastguard Worker 				 "retval=%ld, errno=%d", TEST_RETURN,
277*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO);
278*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
279*49cdfc7eSAndroid Build Coastguard Worker 			continue;
280*49cdfc7eSAndroid Build Coastguard Worker 		}
281*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
282*49cdfc7eSAndroid Build Coastguard Worker 		gettimeofday(&etv, NULL);
283*49cdfc7eSAndroid Build Coastguard Worker 	}
284*49cdfc7eSAndroid Build Coastguard Worker 	if (!failflag) {
285*49cdfc7eSAndroid Build Coastguard Worker 		sec = etv.tv_sec - stv.tv_sec;
286*49cdfc7eSAndroid Build Coastguard Worker 		usec = etv.tv_usec - stv.tv_usec;
287*49cdfc7eSAndroid Build Coastguard Worker 		if (usec < 0) {
288*49cdfc7eSAndroid Build Coastguard Worker 			usec += 1000000;
289*49cdfc7eSAndroid Build Coastguard Worker 			sec--;
290*49cdfc7eSAndroid Build Coastguard Worker 		}
291*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test 5: %d reads and writes in %3d.%06d sec",
292*49cdfc7eSAndroid Build Coastguard Worker 			 nr, sec, usec);
293*49cdfc7eSAndroid Build Coastguard Worker 	}
294*49cdfc7eSAndroid Build Coastguard Worker 
295*49cdfc7eSAndroid Build Coastguard Worker /* TEST 6 */
296*49cdfc7eSAndroid Build Coastguard Worker 	pos = 0;
297*49cdfc7eSAndroid Build Coastguard Worker 	failflag = 0;
298*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&stv, NULL);
299*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nr; i++) {
300*49cdfc7eSAndroid Build Coastguard Worker 		io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
301*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
302*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
303*49cdfc7eSAndroid Build Coastguard Worker 		do {
304*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
305*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
306*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
307*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 6: write io_submit failed - "
308*49cdfc7eSAndroid Build Coastguard Worker 				 "retval=%ld, errno=%d", TEST_RETURN,
309*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO);
310*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
311*49cdfc7eSAndroid Build Coastguard Worker 			continue;
312*49cdfc7eSAndroid Build Coastguard Worker 		}
313*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
314*49cdfc7eSAndroid Build Coastguard Worker 		io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
315*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_sec = 30;
316*49cdfc7eSAndroid Build Coastguard Worker 		ts.tv_nsec = 0;
317*49cdfc7eSAndroid Build Coastguard Worker 		do {
318*49cdfc7eSAndroid Build Coastguard Worker 			TEST(io_submit(io_ctx, 1, iocbs));
319*49cdfc7eSAndroid Build Coastguard Worker 		} while (TEST_RETURN == -EAGAIN);
320*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN < 0) {
321*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test 6: read io_submit failed - "
322*49cdfc7eSAndroid Build Coastguard Worker 				 "retval=%ld, errno=%d", TEST_RETURN,
323*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO);
324*49cdfc7eSAndroid Build Coastguard Worker 			failflag = 1;
325*49cdfc7eSAndroid Build Coastguard Worker 			continue;
326*49cdfc7eSAndroid Build Coastguard Worker 		}
327*49cdfc7eSAndroid Build Coastguard Worker 		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
328*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < (int)bufsize; j++) {
329*49cdfc7eSAndroid Build Coastguard Worker 			if (srcbuf[j] != dstbuf[j]) {
330*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Test 6: compare failed - "
331*49cdfc7eSAndroid Build Coastguard Worker 					 "read: %c, " "actual: %c",
332*49cdfc7eSAndroid Build Coastguard Worker 					 dstbuf[j], srcbuf[j]);
333*49cdfc7eSAndroid Build Coastguard Worker 				break;
334*49cdfc7eSAndroid Build Coastguard Worker 			}
335*49cdfc7eSAndroid Build Coastguard Worker 		}
336*49cdfc7eSAndroid Build Coastguard Worker 		gettimeofday(&etv, NULL);
337*49cdfc7eSAndroid Build Coastguard Worker 	}
338*49cdfc7eSAndroid Build Coastguard Worker 	if (!failflag) {
339*49cdfc7eSAndroid Build Coastguard Worker 		sec = etv.tv_sec - stv.tv_sec;
340*49cdfc7eSAndroid Build Coastguard Worker 		usec = etv.tv_usec - stv.tv_usec;
341*49cdfc7eSAndroid Build Coastguard Worker 		if (usec < 0) {
342*49cdfc7eSAndroid Build Coastguard Worker 			usec += 1000000;
343*49cdfc7eSAndroid Build Coastguard Worker 			sec--;
344*49cdfc7eSAndroid Build Coastguard Worker 		}
345*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test 6: %d read,write,verify in %d.%06d sec",
346*49cdfc7eSAndroid Build Coastguard Worker 			 i, sec, usec);
347*49cdfc7eSAndroid Build Coastguard Worker 	}
348*49cdfc7eSAndroid Build Coastguard Worker 
349*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
350*49cdfc7eSAndroid Build Coastguard Worker 
351*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
352*49cdfc7eSAndroid Build Coastguard Worker }
353*49cdfc7eSAndroid Build Coastguard Worker 
help(void)354*49cdfc7eSAndroid Build Coastguard Worker static void help(void)
355*49cdfc7eSAndroid Build Coastguard Worker {
356*49cdfc7eSAndroid Build Coastguard Worker 	printf("  -b n    Buffersize\n");
357*49cdfc7eSAndroid Build Coastguard Worker 	printf("  -n n    Number of requests\n");
358*49cdfc7eSAndroid Build Coastguard Worker 	printf("  -F s    Filename to run the tests against\n");
359*49cdfc7eSAndroid Build Coastguard Worker }
360*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)361*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
362*49cdfc7eSAndroid Build Coastguard Worker {
363*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
364*49cdfc7eSAndroid Build Coastguard Worker 
365*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
366*49cdfc7eSAndroid Build Coastguard Worker 
367*49cdfc7eSAndroid Build Coastguard Worker 	/* Pause if option was specified */
368*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
369*49cdfc7eSAndroid Build Coastguard Worker 
370*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
371*49cdfc7eSAndroid Build Coastguard Worker 
372*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0)
373*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup, "failed to open %s "
374*49cdfc7eSAndroid Build Coastguard Worker 			 "file, errno: %d", fname, errno);
375*49cdfc7eSAndroid Build Coastguard Worker 	stat(fname, &s);
376*49cdfc7eSAndroid Build Coastguard Worker 	if ((iocbs = malloc(sizeof(int) * nr)) == NULL)
377*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup, "malloc for iocbs failed - "
378*49cdfc7eSAndroid Build Coastguard Worker 			 "errno: %d", errno);
379*49cdfc7eSAndroid Build Coastguard Worker 	if ((iocbs[0] = malloc(sizeof(struct iocb))) == NULL)
380*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup, "malloc for iocbs elements failed - "
381*49cdfc7eSAndroid Build Coastguard Worker 			 "errno: %d", errno);
382*49cdfc7eSAndroid Build Coastguard Worker 	if (S_ISCHR(s.st_mode)) {
383*49cdfc7eSAndroid Build Coastguard Worker 		if ((ret =
384*49cdfc7eSAndroid Build Coastguard Worker 		     posix_memalign((void **)&srcbuf, bufsize, bufsize)) != 0)
385*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, cleanup,
386*49cdfc7eSAndroid Build Coastguard Worker 				 "posix_memalign for srcbuf "
387*49cdfc7eSAndroid Build Coastguard Worker 				 "failed - errno: %d", errno);
388*49cdfc7eSAndroid Build Coastguard Worker 		if ((ret =
389*49cdfc7eSAndroid Build Coastguard Worker 		     posix_memalign((void **)&dstbuf, bufsize, bufsize)) != 0)
390*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, cleanup,
391*49cdfc7eSAndroid Build Coastguard Worker 				 "posix_memalign for dstbuf "
392*49cdfc7eSAndroid Build Coastguard Worker 				 "failed - errno: %d", errno);
393*49cdfc7eSAndroid Build Coastguard Worker 	} else {
394*49cdfc7eSAndroid Build Coastguard Worker 		if ((srcbuf = malloc(sizeof(char) * bufsize)) == NULL)
395*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, cleanup, "malloc for srcbuf "
396*49cdfc7eSAndroid Build Coastguard Worker 				 "failed - errno: %d", errno);
397*49cdfc7eSAndroid Build Coastguard Worker 		if ((dstbuf = malloc(sizeof(char) * bufsize)) == NULL)
398*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, cleanup, "malloc for dstbuf "
399*49cdfc7eSAndroid Build Coastguard Worker 				 "failed - errno: %d", errno);
400*49cdfc7eSAndroid Build Coastguard Worker 	}
401*49cdfc7eSAndroid Build Coastguard Worker 	memset((void *)srcbuf, 65, bufsize);
402*49cdfc7eSAndroid Build Coastguard Worker 	if ((ret = io_queue_init(1, &io_ctx)) != 0)
403*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup, "io_queue_init failed: %s",
404*49cdfc7eSAndroid Build Coastguard Worker 			 strerror(ret));
405*49cdfc7eSAndroid Build Coastguard Worker }
406*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)407*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
408*49cdfc7eSAndroid Build Coastguard Worker {
409*49cdfc7eSAndroid Build Coastguard Worker 	free(dstbuf);
410*49cdfc7eSAndroid Build Coastguard Worker 	free(srcbuf);
411*49cdfc7eSAndroid Build Coastguard Worker 	free(iocbs[0]);
412*49cdfc7eSAndroid Build Coastguard Worker 	free(iocbs);
413*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
414*49cdfc7eSAndroid Build Coastguard Worker 	io_queue_release(io_ctx);
415*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
416*49cdfc7eSAndroid Build Coastguard Worker }
417*49cdfc7eSAndroid Build Coastguard Worker 
418*49cdfc7eSAndroid Build Coastguard Worker #else
main(void)419*49cdfc7eSAndroid Build Coastguard Worker int main(void)
420*49cdfc7eSAndroid Build Coastguard Worker {
421*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "test requires libaio and it's development packages");
422*49cdfc7eSAndroid Build Coastguard Worker }
423*49cdfc7eSAndroid Build Coastguard Worker #endif
424