Commit 9461083e authored by Julius Plenz's avatar Julius Plenz

Implement "-n <n>" flag for repeated posix_fadvise()

parent 113adf08
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <error.h> #include <error.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
int exiterr(const char *s) int exiterr(const char *s)
{ {
...@@ -13,30 +14,38 @@ int exiterr(const char *s) ...@@ -13,30 +14,38 @@ int exiterr(const char *s)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, n = 1;
int fd; int fd;
char *fn;
struct stat st; struct stat st;
if(argc != 2) { if(argc == 4 && !strcmp("-n", argv[1])) {
fprintf(stderr, "usage: %s <file> " n = atoi(argv[2]);
"-- call fadvise(DONTNEED) on file", argv[0]); fn = argv[3];
} else if(argc != 2) {
fprintf(stderr, "usage: %s [-n <n>] <file> "
"-- call fadvise(DONTNEED) <n> times on file", argv[0]);
exit(1); exit(1);
} else {
fn = argv[1];
} }
fd = open(argv[1], O_RDONLY); fd = open(fn, O_RDONLY);
if(fd == -1) if(fd == -1)
exiterr("open"); exiterr("open");
if(fstat(fd, &st) == -1) if(fstat(fd, &st) == -1)
exiterr("fstat"); exiterr("fstat");
if(!S_ISREG(st.st_mode)) { if(!S_ISREG(st.st_mode)) {
fprintf(stderr, "%s: S_ISREG: not a regular file", argv[1]); fprintf(stderr, "%s: S_ISREG: not a regular file", fn);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(st.st_size == 0) { if(st.st_size == 0) {
fprintf(stderr, "%s: file size is 0!\n", argv[1]); fprintf(stderr, "%s: file size is 0!\n", fn);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
for(i = 0; i < n; i++)
if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1) if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1)
exiterr("posix_fadvise"); exiterr("posix_fadvise");
......
...@@ -5,12 +5,12 @@ ...@@ -5,12 +5,12 @@
/* Since open() and close() are re-defined in nocache.c, it's not /* Since open() and close() are re-defined in nocache.c, it's not
* possible to include <fcntl.h> there. So we do it here. */ * possible to include <fcntl.h> there. So we do it here. */
int fadv_dontneed(int fd, off_t offset, off_t len) int fadv_dontneed(int fd, off_t offset, off_t len, int n)
{ {
#ifdef DOUBLEFADVISE int i, ret;
posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED); for(i = 0; i < n; i++)
#endif ret = posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
return posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED); return ret;
} }
int fadv_noreuse(int fd, off_t offset, off_t len) int fadv_noreuse(int fd, off_t offset, off_t len)
......
#ifndef _FCNTL_HELPERS_H #ifndef _FCNTL_HELPERS_H
#define _FCNTL_HELPERS_H #define _FCNTL_HELPERS_H
extern int fadv_dontneed(int fd, off_t offset, off_t len); extern int fadv_dontneed(int fd, off_t offset, off_t len, int n);
extern int fadv_noreuse(int fd, off_t offset, off_t len); extern int fadv_noreuse(int fd, off_t offset, off_t len);
extern int valid_fd(int fd); extern int valid_fd(int fd);
extern void sync_if_writable(int fd); extern void sync_if_writable(int fd);
......
...@@ -8,4 +8,9 @@ else ...@@ -8,4 +8,9 @@ else
export LD_PRELOAD="$libnocache" export LD_PRELOAD="$libnocache"
fi fi
if [ "$1" = "-n" ]; then
export NOCACHE_NR_FADVISE="$2"
shift 2
fi
exec "$@" exec "$@"
...@@ -60,9 +60,13 @@ static struct fadv_info fds[_MAX_FDS]; ...@@ -60,9 +60,13 @@ static struct fadv_info fds[_MAX_FDS];
static size_t PAGESIZE; static size_t PAGESIZE;
static pthread_mutex_t lock; /* protects access to fds[] */ static pthread_mutex_t lock; /* protects access to fds[] */
static char *env_nr_fadvise = "NOCACHE_NR_FADVISE";
static int nr_fadvise;
static void init(void) static void init(void)
{ {
int i; int i;
char *s;
char *error; char *error;
_original_open = (int (*)(const char *, int, mode_t)) _original_open = (int (*)(const char *, int, mode_t))
...@@ -82,6 +86,11 @@ static void init(void) ...@@ -82,6 +86,11 @@ static void init(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if((s = getenv(env_nr_fadvise)) != NULL)
nr_fadvise = atoi(s);
if(nr_fadvise <= 0)
nr_fadvise = 1;
PAGESIZE = getpagesize(); PAGESIZE = getpagesize();
for(i = 0; i < _MAX_FDS; i++) for(i = 0; i < _MAX_FDS; i++)
fds[i].fd = -1; fds[i].fd = -1;
...@@ -295,14 +304,14 @@ static void free_unclaimed_pages(int fd) ...@@ -295,14 +304,14 @@ static void free_unclaimed_pages(int fd)
while(j < fds[i].nr_pages) { while(j < fds[i].nr_pages) {
if(fds[i].info[j] & 1) { if(fds[i].info[j] & 1) {
if(start < j) if(start < j)
fadv_dontneed(fd, start*PAGESIZE, (j - start) * PAGESIZE); fadv_dontneed(fd, start*PAGESIZE, (j - start) * PAGESIZE, nr_fadvise);
start = j + 1; start = j + 1;
} }
j++; j++;
} }
/* forget written contents that go beyond previous file size */ /* forget written contents that go beyond previous file size */
fadv_dontneed(fd, start < j ? start*PAGESIZE : fds[i].size, 0); fadv_dontneed(fd, start < j ? start*PAGESIZE : fds[i].size, 0, nr_fadvise);
free(fds[i].info); free(fds[i].info);
fds[i].fd = -1; fds[i].fd = -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment