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 4186067c77SAdrian Chadd #include <sys/cdefs.h> 4286067c77SAdrian Chadd __FBSDID("$FreeBSD$"); 4386067c77SAdrian Chadd 44cfbf6b69SEnji Cooper #include <sys/types.h> 45cfbf6b69SEnji Cooper #include <sys/disk.h> 46cfbf6b69SEnji Cooper #include <sys/ioctl.h> 47cfbf6b69SEnji Cooper #include <sys/stat.h> 48cfbf6b69SEnji Cooper #include <sys/time.h> 49cfbf6b69SEnji Cooper #include <aio.h> 50cfbf6b69SEnji Cooper #include <assert.h> 51cfbf6b69SEnji Cooper #include <ctype.h> 52cfbf6b69SEnji Cooper #include <err.h> 53cfbf6b69SEnji Cooper #include <fcntl.h> 54e3223429SKevin Lo #include <stdint.h> 5586067c77SAdrian Chadd #include <stdio.h> 5686067c77SAdrian Chadd #include <stdlib.h> 5786067c77SAdrian Chadd #include <string.h> 58cfbf6b69SEnji Cooper #include <time.h> 59cfbf6b69SEnji Cooper #include <unistd.h> 6086067c77SAdrian Chadd 6186067c77SAdrian Chadd /* 6286067c77SAdrian Chadd * This is a bit of a quick hack to do parallel IO testing through POSIX AIO. 6386067c77SAdrian Chadd * Its specifically designed to work under FreeBSD and its derivatives; 6486067c77SAdrian Chadd * note how I cheat by using aio_waitcomplete(). 6586067c77SAdrian Chadd * 6686067c77SAdrian Chadd * TODO: 6786067c77SAdrian Chadd * 6886067c77SAdrian Chadd * + Add write support; so we can make sure we're not hitting throughput issues 6986067c77SAdrian Chadd * with read/modify/write of entire tracks of the disk 7086067c77SAdrian Chadd * + Add in per-op stats - time and offset - so one could start mapping out 7186067c77SAdrian Chadd * the speed hotspots of the disk 7286067c77SAdrian Chadd * + Add in different distributions - random, normal, left/right skewed normal, 7386067c77SAdrian Chadd * zipf, etc - and perhaps add the ability to run concurrent distributions 7486067c77SAdrian Chadd * (so a normal and a zipf; and also a random read; zipf write, etc.) 7586067c77SAdrian Chadd * 7686067c77SAdrian Chadd * Adrian Chadd <adrian@creative.net.au> 7786067c77SAdrian Chadd */ 7886067c77SAdrian Chadd 79a47f853eSAdrian Chadd typedef enum { 80a47f853eSAdrian Chadd IOT_NONE = 0x00, 81a47f853eSAdrian Chadd IOT_READ = 0x01, 82a47f853eSAdrian Chadd IOT_WRITE = 0x02 83a47f853eSAdrian Chadd } iot_t; 84a47f853eSAdrian Chadd 8586067c77SAdrian Chadd static size_t 8686067c77SAdrian Chadd disk_getsize(int fd) 8786067c77SAdrian Chadd { 8886067c77SAdrian Chadd off_t mediasize; 8986067c77SAdrian Chadd 90cfbf6b69SEnji Cooper if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) 91cfbf6b69SEnji Cooper err(1, "ioctl(DIOCGMEDIASIZE)"); 92cfbf6b69SEnji Cooper return (mediasize); 9386067c77SAdrian Chadd } 9486067c77SAdrian Chadd 95cfbf6b69SEnji Cooper static iot_t 96a47f853eSAdrian Chadd choose_aio(iot_t iomask) 97a47f853eSAdrian Chadd { 98a47f853eSAdrian Chadd /* choose a random read or write event, limited by the mask */ 99a47f853eSAdrian Chadd if (iomask == IOT_READ) 100a47f853eSAdrian Chadd return IOT_READ; 101a47f853eSAdrian Chadd else if (iomask == IOT_WRITE) 102a47f853eSAdrian Chadd return IOT_WRITE; 103a47f853eSAdrian Chadd return (random() & 0x01 ? IOT_READ : IOT_WRITE); 104a47f853eSAdrian Chadd } 10586067c77SAdrian Chadd 106cfbf6b69SEnji Cooper static void 107a47f853eSAdrian Chadd set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf) 10886067c77SAdrian Chadd { 10986067c77SAdrian Chadd int r; 11086067c77SAdrian Chadd bzero(a, sizeof(*a)); 11186067c77SAdrian Chadd a->aio_fildes = fd; 11286067c77SAdrian Chadd a->aio_nbytes = size; 11386067c77SAdrian Chadd a->aio_offset = offset; 11486067c77SAdrian Chadd a->aio_buf = buf; 115a47f853eSAdrian Chadd if (iot == IOT_READ) 11686067c77SAdrian Chadd r = aio_read(a); 117a47f853eSAdrian Chadd else 118a47f853eSAdrian Chadd r = aio_write(a); 119cfbf6b69SEnji Cooper if (r != 0) 120cfbf6b69SEnji Cooper err(1, "set_aio call failed"); 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) { 142cfbf6b69SEnji Cooper printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", 143cfbf6b69SEnji Cooper argv[0]); 14486067c77SAdrian Chadd exit(1); 14586067c77SAdrian Chadd } 14686067c77SAdrian Chadd 14786067c77SAdrian Chadd fn = argv[1]; 14886067c77SAdrian Chadd io_size = atoi(argv[2]); 149cfbf6b69SEnji Cooper if (io_size <= 0) 150cfbf6b69SEnji Cooper errx(1, "the I/O size must be >0"); 15186067c77SAdrian Chadd nrun = atoi(argv[3]); 152cfbf6b69SEnji Cooper if (nrun <= 0) 153cfbf6b69SEnji Cooper errx(1, "the number of runs must be >0"); 15486067c77SAdrian Chadd aio_len = atoi(argv[4]); 155cfbf6b69SEnji Cooper if (aio_len <= 0) 156cfbf6b69SEnji Cooper errx(1, "AIO concurrency must be >0"); 157cfbf6b69SEnji Cooper if (strcmp(argv[5], "ro") == 0) 158a47f853eSAdrian Chadd iowhat = IOT_READ; 159cfbf6b69SEnji Cooper else if (strcmp(argv[5], "rw") == 0) 160a47f853eSAdrian Chadd iowhat = IOT_READ | IOT_WRITE; 161cfbf6b69SEnji Cooper else if (strcmp(argv[5], "wo") == 0) 162a47f853eSAdrian Chadd iowhat = IOT_WRITE; 163cfbf6b69SEnji Cooper else 164cfbf6b69SEnji Cooper errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n"); 16586067c77SAdrian Chadd 16686067c77SAdrian Chadd /* 16786067c77SAdrian Chadd * Random returns values between 0 and (2^32)-1; only good for 4 gig. 16886067c77SAdrian Chadd * Lets instead treat random() as returning a block offset w/ block size 16986067c77SAdrian Chadd * being "io_size", so we can handle > 4 gig files. 17086067c77SAdrian Chadd */ 171a47f853eSAdrian Chadd if (iowhat == IOT_READ) 17286067c77SAdrian Chadd fd = open(fn, O_RDONLY | O_DIRECT); 173a47f853eSAdrian Chadd else if (iowhat == IOT_WRITE) 174a47f853eSAdrian Chadd fd = open(fn, O_WRONLY | O_DIRECT); 175a47f853eSAdrian Chadd else 176a47f853eSAdrian Chadd fd = open(fn, O_RDWR | O_DIRECT); 177a47f853eSAdrian Chadd 178cfbf6b69SEnji Cooper if (fd < 0) 179cfbf6b69SEnji Cooper err(1, "open failed"); 180cfbf6b69SEnji Cooper if (fstat(fd, &sb) < 0) 181cfbf6b69SEnji Cooper err(1, "fstat failed"); 18286067c77SAdrian Chadd if (S_ISREG(sb.st_mode)) { 18386067c77SAdrian Chadd file_size = sb.st_size; 18486067c77SAdrian Chadd } else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { 18586067c77SAdrian Chadd file_size = disk_getsize(fd); 186cfbf6b69SEnji Cooper } else 187cfbf6b69SEnji Cooper errx(1, "unknown file type"); 188cfbf6b69SEnji Cooper if (file_size <= 0) 189cfbf6b69SEnji Cooper errx(1, "path provided too small"); 190cfbf6b69SEnji Cooper 1918ea4debeSKevin Lo printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size); 19286067c77SAdrian Chadd 19386067c77SAdrian Chadd aio = calloc(aio_len, sizeof(struct aiocb)); 19486067c77SAdrian Chadd abuf = calloc(aio_len, sizeof(char *)); 195cfbf6b69SEnji Cooper for (i = 0; i < aio_len; i++) 19686067c77SAdrian Chadd abuf[i] = calloc(1, io_size * sizeof(char)); 19786067c77SAdrian Chadd 19886067c77SAdrian Chadd /* Fill with the initial contents */ 19986067c77SAdrian Chadd gettimeofday(&st, NULL); 20086067c77SAdrian Chadd for (i = 0; i < aio_len; i++) { 20186067c77SAdrian Chadd offset = random() % (file_size / io_size); 20286067c77SAdrian Chadd offset *= io_size; 203a47f853eSAdrian Chadd set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]); 20486067c77SAdrian Chadd } 20586067c77SAdrian Chadd 20686067c77SAdrian Chadd for (i = 0; i < nrun; i++) { 20786067c77SAdrian Chadd aio_waitcomplete(&a, NULL); 20886067c77SAdrian Chadd n = a - aio; 20986067c77SAdrian Chadd assert(n < aio_len); 21086067c77SAdrian Chadd assert(n >= 0); 21186067c77SAdrian Chadd offset = random() % (file_size / io_size); 21286067c77SAdrian Chadd offset *= io_size; 213a47f853eSAdrian Chadd set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]); 21486067c77SAdrian Chadd } 21586067c77SAdrian Chadd 21686067c77SAdrian Chadd gettimeofday(&et, NULL); 21786067c77SAdrian Chadd timersub(&et, &st, &rt); 21886067c77SAdrian Chadd f_rt = ((float) (rt.tv_usec)) / 1000000.0; 21986067c77SAdrian Chadd f_rt += (float) (rt.tv_sec); 22086067c77SAdrian Chadd printf("Runtime: %.2f seconds, ", f_rt); 22186067c77SAdrian Chadd printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt); 22286067c77SAdrian Chadd printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt); 22386067c77SAdrian Chadd 22486067c77SAdrian Chadd 22586067c77SAdrian Chadd 22686067c77SAdrian Chadd exit(0); 22786067c77SAdrian Chadd } 228