xref: /freebsd/tools/regression/aio/aiop/aiop.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
186067c77SAdrian Chadd /*
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
486067c77SAdrian Chadd  * Copyright (c) 2002 Adrian Chadd <adrian@FreeBSD.org>.
586067c77SAdrian Chadd  * All rights reserved.
686067c77SAdrian Chadd  *
786067c77SAdrian Chadd  * This software was developed for the FreeBSD Project by Marshall
886067c77SAdrian Chadd  * Kirk McKusick and Network Associates Laboratories, the Security
986067c77SAdrian Chadd  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
1086067c77SAdrian Chadd  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
1186067c77SAdrian Chadd  * research program.
1286067c77SAdrian Chadd  *
1386067c77SAdrian Chadd  * Copyright (c) 1980, 1989, 1993
1486067c77SAdrian Chadd  *	The Regents of the University of California.  All rights reserved.
1586067c77SAdrian Chadd  *
1686067c77SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
1786067c77SAdrian Chadd  * modification, are permitted provided that the following conditions
1886067c77SAdrian Chadd  * are met:
1986067c77SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
2086067c77SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
2186067c77SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
2286067c77SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
2386067c77SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
24fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2586067c77SAdrian Chadd  *    may be used to endorse or promote products derived from this software
2686067c77SAdrian Chadd  *    without specific prior written permission.
2786067c77SAdrian Chadd  *
2886067c77SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2986067c77SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3086067c77SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3186067c77SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3286067c77SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3386067c77SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3486067c77SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3586067c77SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3686067c77SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3786067c77SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3886067c77SAdrian Chadd  * SUCH DAMAGE.
3986067c77SAdrian Chadd  */
4086067c77SAdrian Chadd 
41cfbf6b69SEnji Cooper #include <sys/types.h>
42cfbf6b69SEnji Cooper #include <sys/disk.h>
43cfbf6b69SEnji Cooper #include <sys/ioctl.h>
44cfbf6b69SEnji Cooper #include <sys/stat.h>
45cfbf6b69SEnji Cooper #include <sys/time.h>
46cfbf6b69SEnji Cooper #include <aio.h>
47cfbf6b69SEnji Cooper #include <assert.h>
48cfbf6b69SEnji Cooper #include <ctype.h>
49cfbf6b69SEnji Cooper #include <err.h>
50cfbf6b69SEnji Cooper #include <fcntl.h>
51e3223429SKevin Lo #include <stdint.h>
5286067c77SAdrian Chadd #include <stdio.h>
5386067c77SAdrian Chadd #include <stdlib.h>
5486067c77SAdrian Chadd #include <string.h>
55cfbf6b69SEnji Cooper #include <time.h>
56cfbf6b69SEnji Cooper #include <unistd.h>
5786067c77SAdrian Chadd 
5886067c77SAdrian Chadd /*
5986067c77SAdrian Chadd  * This is a bit of a quick hack to do parallel IO testing through POSIX AIO.
6086067c77SAdrian Chadd  * Its specifically designed to work under FreeBSD and its derivatives;
6186067c77SAdrian Chadd  * note how I cheat by using aio_waitcomplete().
6286067c77SAdrian Chadd  *
6386067c77SAdrian Chadd  * TODO:
6486067c77SAdrian Chadd  *
6586067c77SAdrian Chadd  * + Add write support; so we can make sure we're not hitting throughput issues
6686067c77SAdrian Chadd  *   with read/modify/write of entire tracks of the disk
6786067c77SAdrian Chadd  * + Add in per-op stats - time and offset - so one could start mapping out
6886067c77SAdrian Chadd  *   the speed hotspots of the disk
6986067c77SAdrian Chadd  * + Add in different distributions - random, normal, left/right skewed normal,
7086067c77SAdrian Chadd  *   zipf, etc - and perhaps add the ability to run concurrent distributions
7186067c77SAdrian Chadd  *   (so a normal and a zipf; and also a random read; zipf write, etc.)
7286067c77SAdrian Chadd  *
7386067c77SAdrian Chadd  * Adrian Chadd <adrian@creative.net.au>
7486067c77SAdrian Chadd  */
7586067c77SAdrian Chadd 
76a47f853eSAdrian Chadd typedef enum {
77a47f853eSAdrian Chadd 	IOT_NONE = 0x00,
78a47f853eSAdrian Chadd 	IOT_READ = 0x01,
79a47f853eSAdrian Chadd 	IOT_WRITE = 0x02
80a47f853eSAdrian Chadd } iot_t;
81a47f853eSAdrian Chadd 
8286067c77SAdrian Chadd static size_t
disk_getsize(int fd)8386067c77SAdrian Chadd disk_getsize(int fd)
8486067c77SAdrian Chadd {
8586067c77SAdrian Chadd 	off_t mediasize;
8686067c77SAdrian Chadd 
87cfbf6b69SEnji Cooper 	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)
88cfbf6b69SEnji Cooper 		err(1, "ioctl(DIOCGMEDIASIZE)");
89cfbf6b69SEnji Cooper 	return (mediasize);
9086067c77SAdrian Chadd }
9186067c77SAdrian Chadd 
92cfbf6b69SEnji Cooper static iot_t
choose_aio(iot_t iomask)93a47f853eSAdrian Chadd choose_aio(iot_t iomask)
94a47f853eSAdrian Chadd {
95a47f853eSAdrian Chadd 	/* choose a random read or write event, limited by the mask */
96a47f853eSAdrian Chadd 	if (iomask == IOT_READ)
97a47f853eSAdrian Chadd 		return IOT_READ;
98a47f853eSAdrian Chadd 	else if (iomask == IOT_WRITE)
99a47f853eSAdrian Chadd 		return IOT_WRITE;
100a47f853eSAdrian Chadd 	return (random() & 0x01 ? IOT_READ : IOT_WRITE);
101a47f853eSAdrian Chadd }
10286067c77SAdrian Chadd 
103cfbf6b69SEnji Cooper static void
set_aio(struct aiocb * a,iot_t iot,int fd,off_t offset,int size,char * buf)104a47f853eSAdrian Chadd set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
10586067c77SAdrian Chadd {
10686067c77SAdrian Chadd 	int r;
10786067c77SAdrian Chadd 	bzero(a, sizeof(*a));
10886067c77SAdrian Chadd 	a->aio_fildes = fd;
10986067c77SAdrian Chadd 	a->aio_nbytes = size;
11086067c77SAdrian Chadd 	a->aio_offset = offset;
11186067c77SAdrian Chadd 	a->aio_buf = buf;
112a47f853eSAdrian Chadd 	if (iot == IOT_READ)
11386067c77SAdrian Chadd 		r = aio_read(a);
114a47f853eSAdrian Chadd 	else
115a47f853eSAdrian Chadd 		r = aio_write(a);
116cfbf6b69SEnji Cooper 	if (r != 0)
117cfbf6b69SEnji Cooper 		err(1, "set_aio call failed");
11886067c77SAdrian Chadd }
11986067c77SAdrian Chadd 
12086067c77SAdrian Chadd int
main(int argc,char * argv[])12186067c77SAdrian Chadd main(int argc, char *argv[])
12286067c77SAdrian Chadd {
12386067c77SAdrian Chadd 	int fd;
12486067c77SAdrian Chadd 	struct stat sb;
12586067c77SAdrian Chadd 	struct aiocb *aio;
12686067c77SAdrian Chadd 	char **abuf;
12786067c77SAdrian Chadd 	const char *fn;
12886067c77SAdrian Chadd 	int aio_len;
12986067c77SAdrian Chadd 	int io_size, nrun;
13086067c77SAdrian Chadd 	off_t file_size, offset;
13186067c77SAdrian Chadd 	struct aiocb *a;
13286067c77SAdrian Chadd 	int i, n;
13386067c77SAdrian Chadd 	struct timeval st, et, rt;
13486067c77SAdrian Chadd 	float f_rt;
135a47f853eSAdrian Chadd 	iot_t iowhat;
13686067c77SAdrian Chadd 
13786067c77SAdrian Chadd 
138a47f853eSAdrian Chadd 	if (argc < 6) {
139cfbf6b69SEnji Cooper 		printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n",
140cfbf6b69SEnji Cooper 		    argv[0]);
14186067c77SAdrian Chadd 		exit(1);
14286067c77SAdrian Chadd 	}
14386067c77SAdrian Chadd 
14486067c77SAdrian Chadd 	fn = argv[1];
14586067c77SAdrian Chadd 	io_size = atoi(argv[2]);
146cfbf6b69SEnji Cooper 	if (io_size <= 0)
147cfbf6b69SEnji Cooper 		errx(1, "the I/O size must be >0");
14886067c77SAdrian Chadd 	nrun = atoi(argv[3]);
149cfbf6b69SEnji Cooper 	if (nrun <= 0)
150cfbf6b69SEnji Cooper 		errx(1, "the number of runs must be >0");
15186067c77SAdrian Chadd 	aio_len = atoi(argv[4]);
152cfbf6b69SEnji Cooper 	if (aio_len <= 0)
153cfbf6b69SEnji Cooper 		errx(1, "AIO concurrency must be >0");
154cfbf6b69SEnji Cooper 	if (strcmp(argv[5], "ro") == 0)
155a47f853eSAdrian Chadd 		iowhat = IOT_READ;
156cfbf6b69SEnji Cooper 	else if (strcmp(argv[5], "rw") == 0)
157a47f853eSAdrian Chadd 		iowhat = IOT_READ | IOT_WRITE;
158cfbf6b69SEnji Cooper 	else if (strcmp(argv[5], "wo") == 0)
159a47f853eSAdrian Chadd 		iowhat = IOT_WRITE;
160cfbf6b69SEnji Cooper 	else
161cfbf6b69SEnji Cooper 		errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n");
16286067c77SAdrian Chadd 
16386067c77SAdrian Chadd 	/*
16486067c77SAdrian Chadd 	 * Random returns values between 0 and (2^32)-1; only good for 4 gig.
16586067c77SAdrian Chadd 	 * Lets instead treat random() as returning a block offset w/ block size
16686067c77SAdrian Chadd 	 * being "io_size", so we can handle > 4 gig files.
16786067c77SAdrian Chadd 	 */
168a47f853eSAdrian Chadd 	if (iowhat == IOT_READ)
16986067c77SAdrian Chadd 		fd = open(fn, O_RDONLY | O_DIRECT);
170a47f853eSAdrian Chadd 	else if (iowhat == IOT_WRITE)
171a47f853eSAdrian Chadd 		fd = open(fn, O_WRONLY | O_DIRECT);
172a47f853eSAdrian Chadd 	else
173a47f853eSAdrian Chadd 		fd = open(fn, O_RDWR | O_DIRECT);
174a47f853eSAdrian Chadd 
175cfbf6b69SEnji Cooper 	if (fd < 0)
176cfbf6b69SEnji Cooper 		err(1, "open failed");
177cfbf6b69SEnji Cooper 	if (fstat(fd, &sb) < 0)
178cfbf6b69SEnji Cooper 		err(1, "fstat failed");
17986067c77SAdrian Chadd 	if (S_ISREG(sb.st_mode)) {
18086067c77SAdrian Chadd 		file_size = sb.st_size;
18186067c77SAdrian Chadd 	} else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
18286067c77SAdrian Chadd 		file_size = disk_getsize(fd);
183cfbf6b69SEnji Cooper 	} else
184cfbf6b69SEnji Cooper 		errx(1, "unknown file type");
185cfbf6b69SEnji Cooper 	if (file_size <= 0)
186cfbf6b69SEnji Cooper 		errx(1, "path provided too small");
187cfbf6b69SEnji Cooper 
1888ea4debeSKevin Lo 	printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size);
18986067c77SAdrian Chadd 
19086067c77SAdrian Chadd 	aio = calloc(aio_len, sizeof(struct aiocb));
19186067c77SAdrian Chadd 	abuf = calloc(aio_len, sizeof(char *));
192cfbf6b69SEnji Cooper 	for (i = 0; i < aio_len; i++)
19386067c77SAdrian Chadd 		abuf[i] = calloc(1, io_size * sizeof(char));
19486067c77SAdrian Chadd 
19586067c77SAdrian Chadd 	/* Fill with the initial contents */
19686067c77SAdrian Chadd 	gettimeofday(&st, NULL);
19786067c77SAdrian Chadd 	for (i = 0; i < aio_len; i++) {
19886067c77SAdrian Chadd 		offset = random() % (file_size / io_size);
19986067c77SAdrian Chadd 		offset *= io_size;
200a47f853eSAdrian Chadd 		set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
20186067c77SAdrian Chadd 	}
20286067c77SAdrian Chadd 
20386067c77SAdrian Chadd 	for (i = 0; i < nrun; i++) {
20486067c77SAdrian Chadd 		aio_waitcomplete(&a, NULL);
20586067c77SAdrian Chadd 		n = a - aio;
20686067c77SAdrian Chadd 		assert(n < aio_len);
20786067c77SAdrian Chadd 		assert(n >= 0);
20886067c77SAdrian Chadd 		offset = random() % (file_size / io_size);
20986067c77SAdrian Chadd 		offset *= io_size;
210a47f853eSAdrian Chadd 		set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
21186067c77SAdrian Chadd 	}
21286067c77SAdrian Chadd 
21386067c77SAdrian Chadd 	gettimeofday(&et, NULL);
21486067c77SAdrian Chadd 	timersub(&et, &st, &rt);
21586067c77SAdrian Chadd 	f_rt = ((float) (rt.tv_usec)) / 1000000.0;
21686067c77SAdrian Chadd 	f_rt += (float) (rt.tv_sec);
21786067c77SAdrian Chadd 	printf("Runtime: %.2f seconds, ", f_rt);
21886067c77SAdrian Chadd 	printf("Op rate: %.2f ops/sec, ", ((float) (nrun))  / f_rt);
21986067c77SAdrian Chadd 	printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
22086067c77SAdrian Chadd 
22186067c77SAdrian Chadd 
22286067c77SAdrian Chadd 
22386067c77SAdrian Chadd 	exit(0);
22486067c77SAdrian Chadd }
225