/*1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2002 Adrian Chadd <[email protected]>.4* All rights reserved.5*6* This software was developed for the FreeBSD Project by Marshall7* Kirk McKusick and Network Associates Laboratories, the Security8* Research Division of Network Associates, Inc. under DARPA/SPAWAR9* contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS10* research program.11*12* Copyright (c) 1980, 1989, 199313* The Regents of the University of California. All rights reserved.14*15* Redistribution and use in source and binary forms, with or without16* modification, are permitted provided that the following conditions17* are met:18* 1. Redistributions of source code must retain the above copyright19* notice, this list of conditions and the following disclaimer.20* 2. Redistributions in binary form must reproduce the above copyright21* notice, this list of conditions and the following disclaimer in the22* documentation and/or other materials provided with the distribution.23* 3. Neither the name of the University nor the names of its contributors24* may be used to endorse or promote products derived from this software25* without specific prior written permission.26*27* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND28* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE29* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE30* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE31* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL32* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS33* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)34* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT35* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY36* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF37* SUCH DAMAGE.38*/3940#include <sys/types.h>41#include <sys/disk.h>42#include <sys/ioctl.h>43#include <sys/stat.h>44#include <sys/time.h>45#include <aio.h>46#include <assert.h>47#include <ctype.h>48#include <err.h>49#include <fcntl.h>50#include <stdint.h>51#include <stdio.h>52#include <stdlib.h>53#include <string.h>54#include <time.h>55#include <unistd.h>5657/*58* This is a bit of a quick hack to do parallel IO testing through POSIX AIO.59* Its specifically designed to work under FreeBSD and its derivatives;60* note how I cheat by using aio_waitcomplete().61*62* TODO:63*64* + Add write support; so we can make sure we're not hitting throughput issues65* with read/modify/write of entire tracks of the disk66* + Add in per-op stats - time and offset - so one could start mapping out67* the speed hotspots of the disk68* + Add in different distributions - random, normal, left/right skewed normal,69* zipf, etc - and perhaps add the ability to run concurrent distributions70* (so a normal and a zipf; and also a random read; zipf write, etc.)71*72* Adrian Chadd <[email protected]>73*/7475typedef enum {76IOT_NONE = 0x00,77IOT_READ = 0x01,78IOT_WRITE = 0x0279} iot_t;8081static size_t82disk_getsize(int fd)83{84off_t mediasize;8586if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)87err(1, "ioctl(DIOCGMEDIASIZE)");88return (mediasize);89}9091static iot_t92choose_aio(iot_t iomask)93{94/* choose a random read or write event, limited by the mask */95if (iomask == IOT_READ)96return IOT_READ;97else if (iomask == IOT_WRITE)98return IOT_WRITE;99return (random() & 0x01 ? IOT_READ : IOT_WRITE);100}101102static void103set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)104{105int r;106bzero(a, sizeof(*a));107a->aio_fildes = fd;108a->aio_nbytes = size;109a->aio_offset = offset;110a->aio_buf = buf;111if (iot == IOT_READ)112r = aio_read(a);113else114r = aio_write(a);115if (r != 0)116err(1, "set_aio call failed");117}118119int120main(int argc, char *argv[])121{122int fd;123struct stat sb;124struct aiocb *aio;125char **abuf;126const char *fn;127int aio_len;128int io_size, nrun;129off_t file_size, offset;130struct aiocb *a;131int i, n;132struct timeval st, et, rt;133float f_rt;134iot_t iowhat;135136137if (argc < 6) {138printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n",139argv[0]);140exit(1);141}142143fn = argv[1];144io_size = atoi(argv[2]);145if (io_size <= 0)146errx(1, "the I/O size must be >0");147nrun = atoi(argv[3]);148if (nrun <= 0)149errx(1, "the number of runs must be >0");150aio_len = atoi(argv[4]);151if (aio_len <= 0)152errx(1, "AIO concurrency must be >0");153if (strcmp(argv[5], "ro") == 0)154iowhat = IOT_READ;155else if (strcmp(argv[5], "rw") == 0)156iowhat = IOT_READ | IOT_WRITE;157else if (strcmp(argv[5], "wo") == 0)158iowhat = IOT_WRITE;159else160errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n");161162/*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 size165* being "io_size", so we can handle > 4 gig files.166*/167if (iowhat == IOT_READ)168fd = open(fn, O_RDONLY | O_DIRECT);169else if (iowhat == IOT_WRITE)170fd = open(fn, O_WRONLY | O_DIRECT);171else172fd = open(fn, O_RDWR | O_DIRECT);173174if (fd < 0)175err(1, "open failed");176if (fstat(fd, &sb) < 0)177err(1, "fstat failed");178if (S_ISREG(sb.st_mode)) {179file_size = sb.st_size;180} else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {181file_size = disk_getsize(fd);182} else183errx(1, "unknown file type");184if (file_size <= 0)185errx(1, "path provided too small");186187printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size);188189aio = calloc(aio_len, sizeof(struct aiocb));190abuf = calloc(aio_len, sizeof(char *));191for (i = 0; i < aio_len; i++)192abuf[i] = calloc(1, io_size * sizeof(char));193194/* Fill with the initial contents */195gettimeofday(&st, NULL);196for (i = 0; i < aio_len; i++) {197offset = random() % (file_size / io_size);198offset *= io_size;199set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);200}201202for (i = 0; i < nrun; i++) {203aio_waitcomplete(&a, NULL);204n = a - aio;205assert(n < aio_len);206assert(n >= 0);207offset = random() % (file_size / io_size);208offset *= io_size;209set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);210}211212gettimeofday(&et, NULL);213timersub(&et, &st, &rt);214f_rt = ((float) (rt.tv_usec)) / 1000000.0;215f_rt += (float) (rt.tv_sec);216printf("Runtime: %.2f seconds, ", f_rt);217printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt);218printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);219220221222exit(0);223}224225226