#include #include #include #include #include #include #include "fadvise.h" char *progname; static void usage(void) { fprintf(stderr, "Usage: %s filename offset length advice [loops]\n", progname); fprintf(stderr, " advice: normal sequential willneed noreuse " "dontneed asyncwrite writewait\n"); exit(1); } int main(int argc, char *argv[]) { int c; int fd; char *sadvice; char *filename; loff_t offset; unsigned long length; int advice = 0; int ret; int loops = 1; progname = argv[0]; while ((c = getopt(argc, argv, "")) != -1) { switch (c) { } } if (optind == argc) usage(); filename = argv[optind++]; if (optind == argc) usage(); offset = strtoull(argv[optind++], NULL, 0); if (optind == argc) usage(); length = strtol(argv[optind++], NULL, 0); if (optind == argc) usage(); sadvice = argv[optind++]; if (optind != argc) loops = strtol(argv[optind++], NULL, 0); if (optind != argc) usage(); if (!strcmp(sadvice, "normal")) advice = POSIX_FADV_NORMAL; else if (!strcmp(sadvice, "sequential")) advice = POSIX_FADV_SEQUENTIAL; else if (!strcmp(sadvice, "willneed")) advice = POSIX_FADV_WILLNEED; else if (!strcmp(sadvice, "noreuse")) advice = POSIX_FADV_NOREUSE; else if (!strcmp(sadvice, "dontneed")) advice = POSIX_FADV_DONTNEED; else if (!strcmp(sadvice, "asyncwrite")) advice = LINUX_FADV_ASYNC_WRITE; else if (!strcmp(sadvice, "writewait")) advice = LINUX_FADV_WRITE_WAIT; else usage(); fd = open(filename, O_RDONLY); if (fd < 0) { fprintf(stderr, "%s: cannot open `%s': %s\n", progname, filename, strerror(errno)); exit(1); } while (loops--) { ret = __posix_fadvise64(fd, offset, length, advice); if (ret) { fprintf(stderr, "%s: fadvise() failed: %s\n", progname, strerror(errno)); exit(1); } } close(fd); exit(0); }