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/cdefs.h> 42 #include <sys/types.h> 43 #include <sys/disk.h> 44 #include <sys/ioctl.h> 45 #include <sys/stat.h> 46 #include <sys/time.h> 47 #include <aio.h> 48 #include <assert.h> 49 #include <ctype.h> 50 #include <err.h> 51 #include <fcntl.h> 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 59 /* 60 * This is a bit of a quick hack to do parallel IO testing through POSIX AIO. 61 * Its specifically designed to work under FreeBSD and its derivatives; 62 * note how I cheat by using aio_waitcomplete(). 63 * 64 * TODO: 65 * 66 * + Add write support; so we can make sure we're not hitting throughput issues 67 * with read/modify/write of entire tracks of the disk 68 * + Add in per-op stats - time and offset - so one could start mapping out 69 * the speed hotspots of the disk 70 * + Add in different distributions - random, normal, left/right skewed normal, 71 * zipf, etc - and perhaps add the ability to run concurrent distributions 72 * (so a normal and a zipf; and also a random read; zipf write, etc.) 73 * 74 * Adrian Chadd <adrian@creative.net.au> 75 */ 76 77 typedef enum { 78 IOT_NONE = 0x00, 79 IOT_READ = 0x01, 80 IOT_WRITE = 0x02 81 } iot_t; 82 83 static size_t 84 disk_getsize(int fd) 85 { 86 off_t mediasize; 87 88 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) 89 err(1, "ioctl(DIOCGMEDIASIZE)"); 90 return (mediasize); 91 } 92 93 static iot_t 94 choose_aio(iot_t iomask) 95 { 96 /* choose a random read or write event, limited by the mask */ 97 if (iomask == IOT_READ) 98 return IOT_READ; 99 else if (iomask == IOT_WRITE) 100 return IOT_WRITE; 101 return (random() & 0x01 ? IOT_READ : IOT_WRITE); 102 } 103 104 static void 105 set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf) 106 { 107 int r; 108 bzero(a, sizeof(*a)); 109 a->aio_fildes = fd; 110 a->aio_nbytes = size; 111 a->aio_offset = offset; 112 a->aio_buf = buf; 113 if (iot == IOT_READ) 114 r = aio_read(a); 115 else 116 r = aio_write(a); 117 if (r != 0) 118 err(1, "set_aio call failed"); 119 } 120 121 int 122 main(int argc, char *argv[]) 123 { 124 int fd; 125 struct stat sb; 126 struct aiocb *aio; 127 char **abuf; 128 const char *fn; 129 int aio_len; 130 int io_size, nrun; 131 off_t file_size, offset; 132 struct aiocb *a; 133 int i, n; 134 struct timeval st, et, rt; 135 float f_rt; 136 iot_t iowhat; 137 138 139 if (argc < 6) { 140 printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", 141 argv[0]); 142 exit(1); 143 } 144 145 fn = argv[1]; 146 io_size = atoi(argv[2]); 147 if (io_size <= 0) 148 errx(1, "the I/O size must be >0"); 149 nrun = atoi(argv[3]); 150 if (nrun <= 0) 151 errx(1, "the number of runs must be >0"); 152 aio_len = atoi(argv[4]); 153 if (aio_len <= 0) 154 errx(1, "AIO concurrency must be >0"); 155 if (strcmp(argv[5], "ro") == 0) 156 iowhat = IOT_READ; 157 else if (strcmp(argv[5], "rw") == 0) 158 iowhat = IOT_READ | IOT_WRITE; 159 else if (strcmp(argv[5], "wo") == 0) 160 iowhat = IOT_WRITE; 161 else 162 errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n"); 163 164 /* 165 * Random returns values between 0 and (2^32)-1; only good for 4 gig. 166 * Lets instead treat random() as returning a block offset w/ block size 167 * being "io_size", so we can handle > 4 gig files. 168 */ 169 if (iowhat == IOT_READ) 170 fd = open(fn, O_RDONLY | O_DIRECT); 171 else if (iowhat == IOT_WRITE) 172 fd = open(fn, O_WRONLY | O_DIRECT); 173 else 174 fd = open(fn, O_RDWR | O_DIRECT); 175 176 if (fd < 0) 177 err(1, "open failed"); 178 if (fstat(fd, &sb) < 0) 179 err(1, "fstat failed"); 180 if (S_ISREG(sb.st_mode)) { 181 file_size = sb.st_size; 182 } else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { 183 file_size = disk_getsize(fd); 184 } else 185 errx(1, "unknown file type"); 186 if (file_size <= 0) 187 errx(1, "path provided too small"); 188 189 printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size); 190 191 aio = calloc(aio_len, sizeof(struct aiocb)); 192 abuf = calloc(aio_len, sizeof(char *)); 193 for (i = 0; i < aio_len; i++) 194 abuf[i] = calloc(1, io_size * sizeof(char)); 195 196 /* Fill with the initial contents */ 197 gettimeofday(&st, NULL); 198 for (i = 0; i < aio_len; i++) { 199 offset = random() % (file_size / io_size); 200 offset *= io_size; 201 set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]); 202 } 203 204 for (i = 0; i < nrun; i++) { 205 aio_waitcomplete(&a, NULL); 206 n = a - aio; 207 assert(n < aio_len); 208 assert(n >= 0); 209 offset = random() % (file_size / io_size); 210 offset *= io_size; 211 set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]); 212 } 213 214 gettimeofday(&et, NULL); 215 timersub(&et, &st, &rt); 216 f_rt = ((float) (rt.tv_usec)) / 1000000.0; 217 f_rt += (float) (rt.tv_sec); 218 printf("Runtime: %.2f seconds, ", f_rt); 219 printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt); 220 printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt); 221 222 223 224 exit(0); 225 } 226