summaryrefslogtreecommitdiff
path: root/aio-cancel.c
diff options
context:
space:
mode:
Diffstat (limited to 'aio-cancel.c')
-rw-r--r--aio-cancel.c34
1 files changed, 12 insertions, 22 deletions
diff --git a/aio-cancel.c b/aio-cancel.c
index 31cab17..c168224 100644
--- a/aio-cancel.c
+++ b/aio-cancel.c
@@ -27,11 +27,9 @@
#include <libaio.h>
-#define PAGE_SIZE 4096UL
-
static unsigned long canceled = 0, completed = 0;
-off_t getsize(int fd)
+static off_t getblocks(int fd)
{
off_t ret;
struct stat stat;
@@ -41,15 +39,13 @@ off_t getsize(int fd)
exit(EXIT_FAILURE);
}
- if (S_ISBLK(stat.st_mode)) {
+ ret = stat.st_size / 512;
+ if (S_ISBLK(stat.st_mode))
if (ioctl(fd, BLKGETSIZE, &ret)) {
perror("ioctl error");
exit(EXIT_FAILURE);
}
- ret *= 512;
- }
-
return ret;
}
@@ -72,8 +68,6 @@ static unsigned reap_events(io_context_t io_ctx, unsigned long iosize)
struct io_event events[16], *ev;
int nr_events = io_getevents(io_ctx, 1, 16, events, NULL);
- //printf("reaped %i events\n", nr_events);
-
if (nr_events < 0)
die("io_getevents", -nr_events);
@@ -95,14 +89,13 @@ static unsigned reap_events(io_context_t io_ctx, unsigned long iosize)
return nr_events;
}
-static char buffer[PAGE_SIZE * 4096] __attribute__ ((aligned (PAGE_SIZE)));
-
int main(int argc, char const *argv[])
{
io_context_t io_ctx;
struct stat stat;
struct iocb *iocb;
struct io_event cancelled;
+ void *buffer;
unsigned long nr_ios, iodepth, iosize, in_flight = 0;
off_t blocks;
int i, ret, fd;
@@ -110,7 +103,7 @@ int main(int argc, char const *argv[])
if (argc != 5)
usage();
- fd = open(argv[1], O_RDWR | O_DIRECT);
+ fd = open(argv[1], O_RDONLY|O_DIRECT);
if (fd < 0) {
perror("open");
exit(1);
@@ -134,20 +127,17 @@ int main(int argc, char const *argv[])
usage();
}
- if (iosize % PAGE_SIZE || iosize > sizeof(buffer)) {
- printf("Bad iosize: must be a multiple of PAGE_SIZE and not more than %zu\n",
- sizeof(buffer));
- exit(EXIT_FAILURE);
- }
-
- blocks = getsize(fd);
+ ret = posix_memalign(&buffer, 4096, iosize);
+ if (ret)
+ die("posix_memalign", ret);
- if (blocks < iosize) {
+ blocks = getblocks(fd);
+ if (blocks < iosize / 512) {
printf("File too small\n");
exit(EXIT_FAILURE);
}
- blocks = (blocks - iosize) / PAGE_SIZE;
+ blocks -= iosize / 512;
printf("Doing %lu random reads to %s with iodepth %lu, iosize %lu\n",
nr_ios, argv[1], iodepth, iosize);
@@ -163,7 +153,7 @@ int main(int argc, char const *argv[])
iocb = malloc(sizeof(struct iocb));
io_prep_pread(iocb, fd, buffer, iosize,
- (random() % blocks) * PAGE_SIZE);
+ (random() % blocks) * 512);
ret = io_submit(io_ctx, 1, &iocb);
if (ret != 1)