xref: /freebsd/tools/regression/aio/aiop/aiop.c (revision a47f853e1ccaef11723e905a7c035a39fcd8f685)
186067c77SAdrian Chadd /*
286067c77SAdrian Chadd  * Copyright (c) 2002 Adrian Chadd <adrian@FreeBSD.org>.
386067c77SAdrian Chadd  * All rights reserved.
486067c77SAdrian Chadd  *
586067c77SAdrian Chadd  * This software was developed for the FreeBSD Project by Marshall
686067c77SAdrian Chadd  * Kirk McKusick and Network Associates Laboratories, the Security
786067c77SAdrian Chadd  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
886067c77SAdrian Chadd  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
986067c77SAdrian Chadd  * research program.
1086067c77SAdrian Chadd  *
1186067c77SAdrian Chadd  * Copyright (c) 1980, 1989, 1993
1286067c77SAdrian Chadd  *	The Regents of the University of California.  All rights reserved.
1386067c77SAdrian Chadd  *
1486067c77SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
1586067c77SAdrian Chadd  * modification, are permitted provided that the following conditions
1686067c77SAdrian Chadd  * are met:
1786067c77SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
1886067c77SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
1986067c77SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
2086067c77SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
2186067c77SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
2286067c77SAdrian Chadd  * 4. Neither the name of the University nor the names of its contributors
2386067c77SAdrian Chadd  *    may be used to endorse or promote products derived from this software
2486067c77SAdrian Chadd  *    without specific prior written permission.
2586067c77SAdrian Chadd  *
2686067c77SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2786067c77SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2886067c77SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2986067c77SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3086067c77SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3186067c77SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3286067c77SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3386067c77SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3486067c77SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3586067c77SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3686067c77SAdrian Chadd  * SUCH DAMAGE.
3786067c77SAdrian Chadd  */
3886067c77SAdrian Chadd 
3986067c77SAdrian Chadd #include <sys/cdefs.h>
4086067c77SAdrian Chadd __FBSDID("$FreeBSD$");
4186067c77SAdrian Chadd 
4286067c77SAdrian Chadd #include <stdio.h>
4386067c77SAdrian Chadd #include <stdlib.h>
4486067c77SAdrian Chadd #include <unistd.h>
4586067c77SAdrian Chadd #include <sys/time.h>
4686067c77SAdrian Chadd #include <sys/types.h>
4786067c77SAdrian Chadd #include <sys/stat.h>
4886067c77SAdrian Chadd #include <time.h>
4986067c77SAdrian Chadd #include <sys/ioctl.h>
5086067c77SAdrian Chadd #include <sys/disk.h>
5186067c77SAdrian Chadd #include <aio.h>
5286067c77SAdrian Chadd #include <fcntl.h>
5386067c77SAdrian Chadd #include <string.h>
5486067c77SAdrian Chadd #include <ctype.h>
5586067c77SAdrian Chadd #include <assert.h>
5686067c77SAdrian Chadd 
5786067c77SAdrian Chadd /*
5886067c77SAdrian Chadd  * This is a bit of a quick hack to do parallel IO testing through POSIX AIO.
5986067c77SAdrian Chadd  * Its specifically designed to work under FreeBSD and its derivatives;
6086067c77SAdrian Chadd  * note how I cheat by using aio_waitcomplete().
6186067c77SAdrian Chadd  *
6286067c77SAdrian Chadd  * TODO:
6386067c77SAdrian Chadd  *
6486067c77SAdrian Chadd  * + Add write support; so we can make sure we're not hitting throughput issues
6586067c77SAdrian Chadd  *   with read/modify/write of entire tracks of the disk
6686067c77SAdrian Chadd  * + Add in per-op stats - time and offset - so one could start mapping out
6786067c77SAdrian Chadd  *   the speed hotspots of the disk
6886067c77SAdrian Chadd  * + Add in different distributions - random, normal, left/right skewed normal,
6986067c77SAdrian Chadd  *   zipf, etc - and perhaps add the ability to run concurrent distributions
7086067c77SAdrian Chadd  *   (so a normal and a zipf; and also a random read; zipf write, etc.)
7186067c77SAdrian Chadd  *
7286067c77SAdrian Chadd  * Adrian Chadd <adrian@creative.net.au>
7386067c77SAdrian Chadd  */
7486067c77SAdrian Chadd 
75a47f853eSAdrian Chadd typedef enum {
76a47f853eSAdrian Chadd 	IOT_NONE = 0x00,
77a47f853eSAdrian Chadd 	IOT_READ = 0x01,
78a47f853eSAdrian Chadd 	IOT_WRITE = 0x02
79a47f853eSAdrian Chadd } iot_t;
80a47f853eSAdrian Chadd 
8186067c77SAdrian Chadd static size_t
8286067c77SAdrian Chadd disk_getsize(int fd)
8386067c77SAdrian Chadd {
8486067c77SAdrian Chadd 	off_t mediasize;
8586067c77SAdrian Chadd 
8686067c77SAdrian Chadd 	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
8786067c77SAdrian Chadd 		perror("ioctl(DIOCGMEDIASIZE)");
8886067c77SAdrian Chadd 		exit(1);
8986067c77SAdrian Chadd 	}
9086067c77SAdrian Chadd 	return mediasize;
9186067c77SAdrian Chadd }
9286067c77SAdrian Chadd 
93a47f853eSAdrian Chadd iot_t
94a47f853eSAdrian Chadd choose_aio(iot_t iomask)
95a47f853eSAdrian Chadd {
96a47f853eSAdrian Chadd 	/* choose a random read or write event, limited by the mask */
97a47f853eSAdrian Chadd 	if (iomask == IOT_READ)
98a47f853eSAdrian Chadd 		return IOT_READ;
99a47f853eSAdrian Chadd 	else if (iomask == IOT_WRITE)
100a47f853eSAdrian Chadd 		return IOT_WRITE;
101a47f853eSAdrian Chadd 	return (random() & 0x01 ? IOT_READ : IOT_WRITE);
102a47f853eSAdrian Chadd }
10386067c77SAdrian Chadd 
10486067c77SAdrian Chadd void
105a47f853eSAdrian Chadd set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
10686067c77SAdrian Chadd {
10786067c77SAdrian Chadd 	int r;
10886067c77SAdrian Chadd 	bzero(a, sizeof(*a));
10986067c77SAdrian Chadd 	a->aio_fildes = fd;
11086067c77SAdrian Chadd 	a->aio_nbytes = size;
11186067c77SAdrian Chadd 	a->aio_offset = offset;
11286067c77SAdrian Chadd 	a->aio_buf = buf;
113a47f853eSAdrian Chadd 	if (iot == IOT_READ)
11486067c77SAdrian Chadd 		r = aio_read(a);
115a47f853eSAdrian Chadd 	else
116a47f853eSAdrian Chadd 		r = aio_write(a);
11786067c77SAdrian Chadd 	if (r != 0) {
118a47f853eSAdrian Chadd 		perror("set_aio");
11986067c77SAdrian Chadd 		exit(1);
12086067c77SAdrian Chadd 	}
12186067c77SAdrian Chadd }
12286067c77SAdrian Chadd 
12386067c77SAdrian Chadd int
12486067c77SAdrian Chadd main(int argc, char *argv[])
12586067c77SAdrian Chadd {
12686067c77SAdrian Chadd 	int fd;
12786067c77SAdrian Chadd 	struct stat sb;
12886067c77SAdrian Chadd 	struct aiocb *aio;
12986067c77SAdrian Chadd 	char **abuf;
13086067c77SAdrian Chadd 	const char *fn;
13186067c77SAdrian Chadd 	int aio_len;
13286067c77SAdrian Chadd 	int io_size, nrun;
13386067c77SAdrian Chadd 	off_t file_size, offset;
13486067c77SAdrian Chadd 	struct aiocb *a;
13586067c77SAdrian Chadd 	int i, n;
13686067c77SAdrian Chadd         struct timeval st, et, rt;
13786067c77SAdrian Chadd         float f_rt;
138a47f853eSAdrian Chadd 	iot_t iowhat;
13986067c77SAdrian Chadd 
14086067c77SAdrian Chadd 
141a47f853eSAdrian Chadd 	if (argc < 6) {
142a47f853eSAdrian Chadd 		printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", argv[0]);
14386067c77SAdrian Chadd 		exit(1);
14486067c77SAdrian Chadd 	}
14586067c77SAdrian Chadd 
14686067c77SAdrian Chadd 	fn = argv[1];
14786067c77SAdrian Chadd 	io_size = atoi(argv[2]);
14886067c77SAdrian Chadd 	nrun = atoi(argv[3]);
14986067c77SAdrian Chadd 	aio_len = atoi(argv[4]);
150a47f853eSAdrian Chadd 	if (strcmp(argv[5], "ro") == 0) {
151a47f853eSAdrian Chadd 		iowhat = IOT_READ;
152a47f853eSAdrian Chadd 	} else if (strcmp(argv[5], "rw") == 0) {
153a47f853eSAdrian Chadd 		iowhat = IOT_READ | IOT_WRITE;
154a47f853eSAdrian Chadd 	} else if (strcmp(argv[5], "wo") == 0) {
155a47f853eSAdrian Chadd 		iowhat = IOT_WRITE;
156a47f853eSAdrian Chadd 	} else {
157a47f853eSAdrian Chadd 		fprintf(stderr, "needs to be ro, rw, wo!\n");
158a47f853eSAdrian Chadd 		exit(1);
159a47f853eSAdrian Chadd 	}
16086067c77SAdrian Chadd 
16186067c77SAdrian Chadd 	/*
16286067c77SAdrian Chadd 	 * Random returns values between 0 and (2^32)-1; only good for 4 gig.
16386067c77SAdrian Chadd 	 * Lets instead treat random() as returning a block offset w/ block size
16486067c77SAdrian Chadd 	 * being "io_size", so we can handle > 4 gig files.
16586067c77SAdrian Chadd 	 */
166a47f853eSAdrian Chadd 	if (iowhat == IOT_READ)
16786067c77SAdrian Chadd 		fd = open(fn, O_RDONLY | O_DIRECT);
168a47f853eSAdrian Chadd 	else if (iowhat == IOT_WRITE)
169a47f853eSAdrian Chadd 		fd = open(fn, O_WRONLY | O_DIRECT);
170a47f853eSAdrian Chadd 	else
171a47f853eSAdrian Chadd 		fd = open(fn, O_RDWR | O_DIRECT);
172a47f853eSAdrian Chadd 
17386067c77SAdrian Chadd 	if (fd < 0) {
17486067c77SAdrian Chadd 		perror("open");
17586067c77SAdrian Chadd 		exit(1);
17686067c77SAdrian Chadd 	}
17786067c77SAdrian Chadd 	if (fstat(fd, &sb) < 0) {
17886067c77SAdrian Chadd 		perror("fstat");
17986067c77SAdrian Chadd 		exit(1);
18086067c77SAdrian Chadd 	}
18186067c77SAdrian Chadd 	if (S_ISREG(sb.st_mode)) {
18286067c77SAdrian Chadd 		file_size = sb.st_size;
18386067c77SAdrian Chadd 	} else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
18486067c77SAdrian Chadd 		file_size = disk_getsize(fd);
18586067c77SAdrian Chadd 	} else {
18686067c77SAdrian Chadd 		perror("unknown file type\n");
18786067c77SAdrian Chadd 		exit(1);
18886067c77SAdrian Chadd 	}
189a47f853eSAdrian Chadd 	printf("File: %s; File size %qd bytes\n", fn, file_size);
19086067c77SAdrian Chadd 
19186067c77SAdrian Chadd 	aio = calloc(aio_len, sizeof(struct aiocb));
19286067c77SAdrian Chadd 	abuf = calloc(aio_len, sizeof(char *));
19386067c77SAdrian Chadd 	for (i = 0; i < aio_len; i++) {
19486067c77SAdrian Chadd 		abuf[i] = calloc(1, io_size * sizeof(char));
19586067c77SAdrian Chadd 	}
19686067c77SAdrian Chadd 
19786067c77SAdrian Chadd 	/* Fill with the initial contents */
19886067c77SAdrian Chadd         gettimeofday(&st, NULL);
19986067c77SAdrian Chadd 	for (i = 0; i < aio_len; i++) {
20086067c77SAdrian Chadd                 offset = random() % (file_size / io_size);
20186067c77SAdrian Chadd                 offset *= io_size;
202a47f853eSAdrian Chadd 		set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
20386067c77SAdrian Chadd 	}
20486067c77SAdrian Chadd 
20586067c77SAdrian Chadd 	for (i = 0; i < nrun; i++) {
20686067c77SAdrian Chadd 		aio_waitcomplete(&a, NULL);
20786067c77SAdrian Chadd 		n = a - aio;
20886067c77SAdrian Chadd 		assert(n < aio_len);
20986067c77SAdrian Chadd 		assert(n >= 0);
21086067c77SAdrian Chadd                 offset = random() % (file_size / io_size);
21186067c77SAdrian Chadd                 offset *= io_size;
212a47f853eSAdrian Chadd 		set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
21386067c77SAdrian Chadd 	}
21486067c77SAdrian Chadd 
21586067c77SAdrian Chadd         gettimeofday(&et, NULL);
21686067c77SAdrian Chadd         timersub(&et, &st, &rt);
21786067c77SAdrian Chadd         f_rt = ((float) (rt.tv_usec)) / 1000000.0;
21886067c77SAdrian Chadd         f_rt += (float) (rt.tv_sec);
21986067c77SAdrian Chadd         printf("Runtime: %.2f seconds, ", f_rt);
22086067c77SAdrian Chadd         printf("Op rate: %.2f ops/sec, ", ((float) (nrun))  / f_rt);
22186067c77SAdrian Chadd         printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
22286067c77SAdrian Chadd 
22386067c77SAdrian Chadd 
22486067c77SAdrian Chadd 
22586067c77SAdrian Chadd 	exit(0);
22686067c77SAdrian Chadd }
227