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> 531d35db81SKevin Lo #include <inttypes.h> 5486067c77SAdrian Chadd #include <string.h> 5586067c77SAdrian Chadd #include <ctype.h> 5686067c77SAdrian Chadd #include <assert.h> 5786067c77SAdrian Chadd 5886067c77SAdrian Chadd /* 5986067c77SAdrian Chadd * This is a bit of a quick hack to do parallel IO testing through POSIX AIO. 6086067c77SAdrian Chadd * Its specifically designed to work under FreeBSD and its derivatives; 6186067c77SAdrian Chadd * note how I cheat by using aio_waitcomplete(). 6286067c77SAdrian Chadd * 6386067c77SAdrian Chadd * TODO: 6486067c77SAdrian Chadd * 6586067c77SAdrian Chadd * + Add write support; so we can make sure we're not hitting throughput issues 6686067c77SAdrian Chadd * with read/modify/write of entire tracks of the disk 6786067c77SAdrian Chadd * + Add in per-op stats - time and offset - so one could start mapping out 6886067c77SAdrian Chadd * the speed hotspots of the disk 6986067c77SAdrian Chadd * + Add in different distributions - random, normal, left/right skewed normal, 7086067c77SAdrian Chadd * zipf, etc - and perhaps add the ability to run concurrent distributions 7186067c77SAdrian Chadd * (so a normal and a zipf; and also a random read; zipf write, etc.) 7286067c77SAdrian Chadd * 7386067c77SAdrian Chadd * Adrian Chadd <adrian@creative.net.au> 7486067c77SAdrian Chadd */ 7586067c77SAdrian Chadd 76a47f853eSAdrian Chadd typedef enum { 77a47f853eSAdrian Chadd IOT_NONE = 0x00, 78a47f853eSAdrian Chadd IOT_READ = 0x01, 79a47f853eSAdrian Chadd IOT_WRITE = 0x02 80a47f853eSAdrian Chadd } iot_t; 81a47f853eSAdrian Chadd 8286067c77SAdrian Chadd static size_t 8386067c77SAdrian Chadd disk_getsize(int fd) 8486067c77SAdrian Chadd { 8586067c77SAdrian Chadd off_t mediasize; 8686067c77SAdrian Chadd 8786067c77SAdrian Chadd if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) { 8886067c77SAdrian Chadd perror("ioctl(DIOCGMEDIASIZE)"); 8986067c77SAdrian Chadd exit(1); 9086067c77SAdrian Chadd } 9186067c77SAdrian Chadd return mediasize; 9286067c77SAdrian Chadd } 9386067c77SAdrian Chadd 94a47f853eSAdrian Chadd iot_t 95a47f853eSAdrian Chadd choose_aio(iot_t iomask) 96a47f853eSAdrian Chadd { 97a47f853eSAdrian Chadd /* choose a random read or write event, limited by the mask */ 98a47f853eSAdrian Chadd if (iomask == IOT_READ) 99a47f853eSAdrian Chadd return IOT_READ; 100a47f853eSAdrian Chadd else if (iomask == IOT_WRITE) 101a47f853eSAdrian Chadd return IOT_WRITE; 102a47f853eSAdrian Chadd return (random() & 0x01 ? IOT_READ : IOT_WRITE); 103a47f853eSAdrian Chadd } 10486067c77SAdrian Chadd 10586067c77SAdrian Chadd void 106a47f853eSAdrian Chadd set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf) 10786067c77SAdrian Chadd { 10886067c77SAdrian Chadd int r; 10986067c77SAdrian Chadd bzero(a, sizeof(*a)); 11086067c77SAdrian Chadd a->aio_fildes = fd; 11186067c77SAdrian Chadd a->aio_nbytes = size; 11286067c77SAdrian Chadd a->aio_offset = offset; 11386067c77SAdrian Chadd a->aio_buf = buf; 114a47f853eSAdrian Chadd if (iot == IOT_READ) 11586067c77SAdrian Chadd r = aio_read(a); 116a47f853eSAdrian Chadd else 117a47f853eSAdrian Chadd r = aio_write(a); 11886067c77SAdrian Chadd if (r != 0) { 119a47f853eSAdrian Chadd perror("set_aio"); 12086067c77SAdrian Chadd exit(1); 12186067c77SAdrian Chadd } 12286067c77SAdrian Chadd } 12386067c77SAdrian Chadd 12486067c77SAdrian Chadd int 12586067c77SAdrian Chadd main(int argc, char *argv[]) 12686067c77SAdrian Chadd { 12786067c77SAdrian Chadd int fd; 12886067c77SAdrian Chadd struct stat sb; 12986067c77SAdrian Chadd struct aiocb *aio; 13086067c77SAdrian Chadd char **abuf; 13186067c77SAdrian Chadd const char *fn; 13286067c77SAdrian Chadd int aio_len; 13386067c77SAdrian Chadd int io_size, nrun; 13486067c77SAdrian Chadd off_t file_size, offset; 13586067c77SAdrian Chadd struct aiocb *a; 13686067c77SAdrian Chadd int i, n; 13786067c77SAdrian Chadd struct timeval st, et, rt; 13886067c77SAdrian Chadd float f_rt; 139a47f853eSAdrian Chadd iot_t iowhat; 14086067c77SAdrian Chadd 14186067c77SAdrian Chadd 142a47f853eSAdrian Chadd if (argc < 6) { 143a47f853eSAdrian Chadd printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", argv[0]); 14486067c77SAdrian Chadd exit(1); 14586067c77SAdrian Chadd } 14686067c77SAdrian Chadd 14786067c77SAdrian Chadd fn = argv[1]; 14886067c77SAdrian Chadd io_size = atoi(argv[2]); 14986067c77SAdrian Chadd nrun = atoi(argv[3]); 15086067c77SAdrian Chadd aio_len = atoi(argv[4]); 151a47f853eSAdrian Chadd if (strcmp(argv[5], "ro") == 0) { 152a47f853eSAdrian Chadd iowhat = IOT_READ; 153a47f853eSAdrian Chadd } else if (strcmp(argv[5], "rw") == 0) { 154a47f853eSAdrian Chadd iowhat = IOT_READ | IOT_WRITE; 155a47f853eSAdrian Chadd } else if (strcmp(argv[5], "wo") == 0) { 156a47f853eSAdrian Chadd iowhat = IOT_WRITE; 157a47f853eSAdrian Chadd } else { 158a47f853eSAdrian Chadd fprintf(stderr, "needs to be ro, rw, wo!\n"); 159a47f853eSAdrian Chadd exit(1); 160a47f853eSAdrian Chadd } 16186067c77SAdrian Chadd 16286067c77SAdrian Chadd /* 16386067c77SAdrian Chadd * Random returns values between 0 and (2^32)-1; only good for 4 gig. 16486067c77SAdrian Chadd * Lets instead treat random() as returning a block offset w/ block size 16586067c77SAdrian Chadd * being "io_size", so we can handle > 4 gig files. 16686067c77SAdrian Chadd */ 167a47f853eSAdrian Chadd if (iowhat == IOT_READ) 16886067c77SAdrian Chadd fd = open(fn, O_RDONLY | O_DIRECT); 169a47f853eSAdrian Chadd else if (iowhat == IOT_WRITE) 170a47f853eSAdrian Chadd fd = open(fn, O_WRONLY | O_DIRECT); 171a47f853eSAdrian Chadd else 172a47f853eSAdrian Chadd fd = open(fn, O_RDWR | O_DIRECT); 173a47f853eSAdrian Chadd 17486067c77SAdrian Chadd if (fd < 0) { 17586067c77SAdrian Chadd perror("open"); 17686067c77SAdrian Chadd exit(1); 17786067c77SAdrian Chadd } 17886067c77SAdrian Chadd if (fstat(fd, &sb) < 0) { 17986067c77SAdrian Chadd perror("fstat"); 18086067c77SAdrian Chadd exit(1); 18186067c77SAdrian Chadd } 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); 18686067c77SAdrian Chadd } else { 18786067c77SAdrian Chadd perror("unknown file type\n"); 18886067c77SAdrian Chadd exit(1); 18986067c77SAdrian Chadd } 190*8ea4debeSKevin Lo printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size); 19186067c77SAdrian Chadd 19286067c77SAdrian Chadd aio = calloc(aio_len, sizeof(struct aiocb)); 19386067c77SAdrian Chadd abuf = calloc(aio_len, sizeof(char *)); 19486067c77SAdrian Chadd for (i = 0; i < aio_len; i++) { 19586067c77SAdrian Chadd abuf[i] = calloc(1, io_size * sizeof(char)); 19686067c77SAdrian Chadd } 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