/* Test program for Linux AIO functions. * Reads random 512-byte chunks of random files, 32 at a time. * Demonstrates io_submit() and io_getevents() delay behavior. * * Build it: gcc -g -Wall -Werror aiotest.c -o aiotest -laio * Run it: ./aiotest FILES... * Running it with 'strace -To trace.file ./aiotest FILES...' is handy, too. * Ideally, list enough files to exceed the cache/buffer memory of your * computer, so the program is forced to read from the disk. */ #include #include #include #include #include #include #include #include #include #include #define AIO_BLKSIZE 512 #define AIO_MAXIO 32 static int nfiles; static int *fd; static struct stat *st; static char buf[AIO_MAXIO][AIO_BLKSIZE]; static io_context_t myctx; static struct iocb io[AIO_MAXIO]; /* Fatal error handler */ static void io_error(const char *func, int rc) { if (rc == -ENOSYS) fprintf(stderr, "AIO not in this kernel\n"); else if (rc < 0) fprintf(stderr, "%s: %s\n", func, strerror(-rc)); exit(1); } static void queue_another(int which) { int file = rand() % nfiles; int ofs = (rand() % (st[file].st_size / AIO_BLKSIZE)) * AIO_BLKSIZE; printf("%d: buf=%p file=%d ofs=%d\n", which, buf[which], file, ofs); io_prep_pread(&io[which], fd[file], buf[which], AIO_BLKSIZE, ofs); struct iocb *iop = &io[which]; int rc = io_submit(myctx, 1, &iop); if (rc < 0) io_error("io_submit", rc); } int main(int argc, char *const *argv) { srand(time(0)); nfiles = argc-1; fd = (int*)malloc(sizeof(int) * nfiles); st = (struct stat*)malloc(sizeof(struct stat) * nfiles); int i; for (i=0; iu.c.nbytes) { fprintf(stderr, "read missing bytes expect %lu got %ld\n", events[i].obj->u.c.nbytes, events[i].res); exit(1); } queue_another(key); } } return 0; }