2 * Copyright (c) 2002 Adrian Chadd <adrian@FreeBSD.org>.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * Copyright (c) 1980, 1989, 1993
12 * The Regents of the University of California. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
50 #include <sys/ioctl.h>
59 * This is a bit of a quick hack to do parallel IO testing through POSIX AIO.
60 * Its specifically designed to work under FreeBSD and its derivatives;
61 * note how I cheat by using aio_waitcomplete().
65 * + Add write support; so we can make sure we're not hitting throughput issues
66 * with read/modify/write of entire tracks of the disk
67 * + Add in per-op stats - time and offset - so one could start mapping out
68 * the speed hotspots of the disk
69 * + Add in different distributions - random, normal, left/right skewed normal,
70 * zipf, etc - and perhaps add the ability to run concurrent distributions
71 * (so a normal and a zipf; and also a random read; zipf write, etc.)
73 * Adrian Chadd <adrian@creative.net.au>
87 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
88 perror("ioctl(DIOCGMEDIASIZE)");
95 choose_aio(iot_t iomask)
97 /* choose a random read or write event, limited by the mask */
98 if (iomask == IOT_READ)
100 else if (iomask == IOT_WRITE)
102 return (random() & 0x01 ? IOT_READ : IOT_WRITE);
106 set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
109 bzero(a, sizeof(*a));
111 a->aio_nbytes = size;
112 a->aio_offset = offset;
125 main(int argc, char *argv[])
134 off_t file_size, offset;
137 struct timeval st, et, rt;
143 printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", argv[0]);
148 io_size = atoi(argv[2]);
149 nrun = atoi(argv[3]);
150 aio_len = atoi(argv[4]);
151 if (strcmp(argv[5], "ro") == 0) {
153 } else if (strcmp(argv[5], "rw") == 0) {
154 iowhat = IOT_READ | IOT_WRITE;
155 } else if (strcmp(argv[5], "wo") == 0) {
158 fprintf(stderr, "needs to be ro, rw, wo!\n");
163 * Random returns values between 0 and (2^32)-1; only good for 4 gig.
164 * Lets instead treat random() as returning a block offset w/ block size
165 * being "io_size", so we can handle > 4 gig files.
167 if (iowhat == IOT_READ)
168 fd = open(fn, O_RDONLY | O_DIRECT);
169 else if (iowhat == IOT_WRITE)
170 fd = open(fn, O_WRONLY | O_DIRECT);
172 fd = open(fn, O_RDWR | O_DIRECT);
178 if (fstat(fd, &sb) < 0) {
182 if (S_ISREG(sb.st_mode)) {
183 file_size = sb.st_size;
184 } else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
185 file_size = disk_getsize(fd);
187 perror("unknown file type\n");
190 printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size);
192 aio = calloc(aio_len, sizeof(struct aiocb));
193 abuf = calloc(aio_len, sizeof(char *));
194 for (i = 0; i < aio_len; i++) {
195 abuf[i] = calloc(1, io_size * sizeof(char));
198 /* Fill with the initial contents */
199 gettimeofday(&st, NULL);
200 for (i = 0; i < aio_len; i++) {
201 offset = random() % (file_size / io_size);
203 set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
206 for (i = 0; i < nrun; i++) {
207 aio_waitcomplete(&a, NULL);
211 offset = random() % (file_size / io_size);
213 set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
216 gettimeofday(&et, NULL);
217 timersub(&et, &st, &rt);
218 f_rt = ((float) (rt.tv_usec)) / 1000000.0;
219 f_rt += (float) (rt.tv_sec);
220 printf("Runtime: %.2f seconds, ", f_rt);
221 printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt);
222 printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);