- Fix style(9) a bit
-- Fix whitespace -- Use err/errx -- Remove superfluous braces - Be a bit more defensive with input from the end-user - Don't throw a floating point exception by dividing by 0 when processing a zero-byte file MFC after: 1 week
This commit is contained in:
parent
51ad8d558e
commit
ae3364144a
@ -3,4 +3,6 @@
|
||||
PROG= aiop
|
||||
MAN=
|
||||
|
||||
WARNS= 6
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
@ -39,21 +39,22 @@
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/disk.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <aio.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/disk.h>
|
||||
#include <aio.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* This is a bit of a quick hack to do parallel IO testing through POSIX AIO.
|
||||
@ -84,14 +85,12 @@ disk_getsize(int fd)
|
||||
{
|
||||
off_t mediasize;
|
||||
|
||||
if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
|
||||
perror("ioctl(DIOCGMEDIASIZE)");
|
||||
exit(1);
|
||||
}
|
||||
return mediasize;
|
||||
if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)
|
||||
err(1, "ioctl(DIOCGMEDIASIZE)");
|
||||
return (mediasize);
|
||||
}
|
||||
|
||||
iot_t
|
||||
static iot_t
|
||||
choose_aio(iot_t iomask)
|
||||
{
|
||||
/* choose a random read or write event, limited by the mask */
|
||||
@ -102,7 +101,7 @@ choose_aio(iot_t iomask)
|
||||
return (random() & 0x01 ? IOT_READ : IOT_WRITE);
|
||||
}
|
||||
|
||||
void
|
||||
static void
|
||||
set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
|
||||
{
|
||||
int r;
|
||||
@ -115,10 +114,8 @@ set_aio(struct aiocb *a, iot_t iot, int fd, off_t offset, int size, char *buf)
|
||||
r = aio_read(a);
|
||||
else
|
||||
r = aio_write(a);
|
||||
if (r != 0) {
|
||||
perror("set_aio");
|
||||
exit(1);
|
||||
}
|
||||
if (r != 0)
|
||||
err(1, "set_aio call failed");
|
||||
}
|
||||
|
||||
int
|
||||
@ -134,30 +131,35 @@ main(int argc, char *argv[])
|
||||
off_t file_size, offset;
|
||||
struct aiocb *a;
|
||||
int i, n;
|
||||
struct timeval st, et, rt;
|
||||
float f_rt;
|
||||
struct timeval st, et, rt;
|
||||
float f_rt;
|
||||
iot_t iowhat;
|
||||
|
||||
|
||||
if (argc < 6) {
|
||||
printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n", argv[0]);
|
||||
printf("Usage: %s <file> <io size> <number of runs> <concurrency> <ro|wo|rw>\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fn = argv[1];
|
||||
io_size = atoi(argv[2]);
|
||||
if (io_size <= 0)
|
||||
errx(1, "the I/O size must be >0");
|
||||
nrun = atoi(argv[3]);
|
||||
if (nrun <= 0)
|
||||
errx(1, "the number of runs must be >0");
|
||||
aio_len = atoi(argv[4]);
|
||||
if (strcmp(argv[5], "ro") == 0) {
|
||||
if (aio_len <= 0)
|
||||
errx(1, "AIO concurrency must be >0");
|
||||
if (strcmp(argv[5], "ro") == 0)
|
||||
iowhat = IOT_READ;
|
||||
} else if (strcmp(argv[5], "rw") == 0) {
|
||||
else if (strcmp(argv[5], "rw") == 0)
|
||||
iowhat = IOT_READ | IOT_WRITE;
|
||||
} else if (strcmp(argv[5], "wo") == 0) {
|
||||
else if (strcmp(argv[5], "wo") == 0)
|
||||
iowhat = IOT_WRITE;
|
||||
} else {
|
||||
fprintf(stderr, "needs to be ro, rw, wo!\n");
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
errx(1, "the I/O type needs to be \"ro\", \"rw\", or \"wo\"!\n");
|
||||
|
||||
/*
|
||||
* Random returns values between 0 and (2^32)-1; only good for 4 gig.
|
||||
@ -171,35 +173,31 @@ main(int argc, char *argv[])
|
||||
else
|
||||
fd = open(fn, O_RDWR | O_DIRECT);
|
||||
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
if (fstat(fd, &sb) < 0) {
|
||||
perror("fstat");
|
||||
exit(1);
|
||||
}
|
||||
if (fd < 0)
|
||||
err(1, "open failed");
|
||||
if (fstat(fd, &sb) < 0)
|
||||
err(1, "fstat failed");
|
||||
if (S_ISREG(sb.st_mode)) {
|
||||
file_size = sb.st_size;
|
||||
} else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
|
||||
file_size = disk_getsize(fd);
|
||||
} else {
|
||||
perror("unknown file type\n");
|
||||
exit(1);
|
||||
}
|
||||
} else
|
||||
errx(1, "unknown file type");
|
||||
if (file_size <= 0)
|
||||
errx(1, "path provided too small");
|
||||
|
||||
printf("File: %s; File size %jd bytes\n", fn, (intmax_t)file_size);
|
||||
|
||||
aio = calloc(aio_len, sizeof(struct aiocb));
|
||||
abuf = calloc(aio_len, sizeof(char *));
|
||||
for (i = 0; i < aio_len; i++) {
|
||||
for (i = 0; i < aio_len; i++)
|
||||
abuf[i] = calloc(1, io_size * sizeof(char));
|
||||
}
|
||||
|
||||
/* Fill with the initial contents */
|
||||
gettimeofday(&st, NULL);
|
||||
gettimeofday(&st, NULL);
|
||||
for (i = 0; i < aio_len; i++) {
|
||||
offset = random() % (file_size / io_size);
|
||||
offset *= io_size;
|
||||
offset = random() % (file_size / io_size);
|
||||
offset *= io_size;
|
||||
set_aio(aio + i, choose_aio(iowhat), fd, offset, io_size, abuf[i]);
|
||||
}
|
||||
|
||||
@ -208,18 +206,18 @@ main(int argc, char *argv[])
|
||||
n = a - aio;
|
||||
assert(n < aio_len);
|
||||
assert(n >= 0);
|
||||
offset = random() % (file_size / io_size);
|
||||
offset *= io_size;
|
||||
offset = random() % (file_size / io_size);
|
||||
offset *= io_size;
|
||||
set_aio(aio + n, choose_aio(iowhat), fd, offset, io_size, abuf[n]);
|
||||
}
|
||||
|
||||
gettimeofday(&et, NULL);
|
||||
timersub(&et, &st, &rt);
|
||||
f_rt = ((float) (rt.tv_usec)) / 1000000.0;
|
||||
f_rt += (float) (rt.tv_sec);
|
||||
printf("Runtime: %.2f seconds, ", f_rt);
|
||||
printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt);
|
||||
printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
|
||||
gettimeofday(&et, NULL);
|
||||
timersub(&et, &st, &rt);
|
||||
f_rt = ((float) (rt.tv_usec)) / 1000000.0;
|
||||
f_rt += (float) (rt.tv_sec);
|
||||
printf("Runtime: %.2f seconds, ", f_rt);
|
||||
printf("Op rate: %.2f ops/sec, ", ((float) (nrun)) / f_rt);
|
||||
printf("Avg transfer rate: %.2f bytes/sec\n", ((float) (nrun)) * ((float)io_size) / f_rt);
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user