1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Adrian Chadd <adrian@FreeBSD.org>.
5 * All rights reserved.
6 *
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * research program.
12 *
13 * Copyright (c) 1980, 1989, 1993
14 * The Regents of the University of California. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #include <sys/types.h>
42 #include <sys/disk.h>
43 #include <sys/ioctl.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <aio.h>
47 #include <assert.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <fcntl.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 /*
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().
62 *
63 * TODO:
64 *
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.)
72 *
73 * Adrian Chadd <adrian@creative.net.au>
74 */
75
76 typedef enum {
77 IOT_NONE = 0x00,
78 IOT_READ = 0x01,
79 IOT_WRITE = 0x02
80 } iot_t;
81
82 static size_t
disk_getsize(int fd)83 disk_getsize(int fd)
84 {
85 off_t mediasize;
86
87 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)
88 err(1, "ioctl(DIOCGMEDIASIZE)");
89 return (mediasize);
90 }
91
92 static iot_t
choose_aio(iot_t iomask)93 choose_aio(iot_t iomask)
94 {
95 /* choose a random read or write event, limited by the mask */
96 if (iomask == IOT_READ)
97 return IOT_READ;
98 else if (iomask == IOT_WRITE)
99 return IOT_WRITE;
100 return (random() & 0x01 ? IOT_READ : IOT_WRITE);
101 }
102
103 static void
set_aio(struct aiocb * a,iot_t iot,int fd,off_t offset,int size,char * buf)104 set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
105 {
106 int r;
107 bzero(a, sizeof(*a));
108 a->aio_fildes = fd;
109 a->aio_nbytes = size;
110 a->aio_offset = offset;
111 a->aio_buf = buf;
112 if (iot == IOT_READ)
113 r = aio_read(a);
114 else
115 r = aio_write(a);
116 if (r != 0)
117 err(1, "set_aio call failed");
118 }
119
120 int
main(int argc,char * argv[])121 main(int argc, char *argv[])
122 {
123 int fd;
124 struct stat sb;
125 struct aiocb *aio;
126 char **abuf;
127 const char *fn;
128 int aio_len;
129 int io_size, nrun;
130 off_t file_size, offset;
131 struct aiocb *a;
132 int i, n;
133 struct timeval st, et, rt;
134 float f_rt;
135 iot_t iowhat;
136
137
138 if (argc < 6) {
139 printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n",
140 argv[0]);
141 exit(1);
142 }
143
144 fn = argv[1];
145 io_size = atoi(argv[2]);
146 if (io_size <= 0)
147 errx(1, "the I/O size must be >0");
148 nrun = atoi(argv[3]);
149 if (nrun <= 0)
150 errx(1, "the number of runs must be >0");
151 aio_len = atoi(argv[4]);
152 if (aio_len <= 0)
153 errx(1, "AIO concurrency must be >0");
154 if (strcmp(argv[5], "ro") == 0)
155 iowhat = IOT_READ;
156 else if (strcmp(argv[5], "rw") == 0)
157 iowhat = IOT_READ | IOT_WRITE;
158 else if (strcmp(argv[5], "wo") == 0)
159 iowhat = IOT_WRITE;
160 else
161 errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n");
162
163 /*
164 * Random returns values between 0 and (2^32)-1; only good for 4 gig.
165 * Lets instead treat random() as returning a block offset w/ block size
166 * being "io_size", so we can handle > 4 gig files.
167 */
168 if (iowhat == IOT_READ)
169 fd = open(fn, O_RDONLY | O_DIRECT);
170 else if (iowhat == IOT_WRITE)
171 fd = open(fn, O_WRONLY | O_DIRECT);
172 else
173 fd = open(fn, O_RDWR | O_DIRECT);
174
175 if (fd < 0)
176 err(1, "open failed");
177 if (fstat(fd, &sb) < 0)
178 err(1, "fstat failed");
179 if (S_ISREG(sb.st_mode)) {
180 file_size = sb.st_size;
181 } else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
182 file_size = disk_getsize(fd);
183 } else
184 errx(1, "unknown file type");
185 if (file_size <= 0)
186 errx(1, "path provided too small");
187
188 printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size);
189
190 aio = calloc(aio_len, sizeof(struct aiocb));
191 abuf = calloc(aio_len, sizeof(char *));
192 for (i = 0; i < aio_len; i++)
193 abuf[i] = calloc(1, io_size * sizeof(char));
194
195 /* Fill with the initial contents */
196 gettimeofday(&st, NULL);
197 for (i = 0; i < aio_len; i++) {
198 offset = random() % (file_size / io_size);
199 offset *= io_size;
200 set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
201 }
202
203 for (i = 0; i < nrun; i++) {
204 aio_waitcomplete(&a, NULL);
205 n = a - aio;
206 assert(n < aio_len);
207 assert(n >= 0);
208 offset = random() % (file_size / io_size);
209 offset *= io_size;
210 set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
211 }
212
213 gettimeofday(&et, NULL);
214 timersub(&et, &st, &rt);
215 f_rt = ((float) (rt.tv_usec)) / 1000000.0;
216 f_rt += (float) (rt.tv_sec);
217 printf("Runtime: %.2f seconds, ", f_rt);
218 printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt);
219 printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
220
221
222
223 exit(0);
224 }
225