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

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

parent 113adf08
......@@ -4,6 +4,7 @@
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int exiterr(const char *s)
{
......@@ -13,32 +14,40 @@ int exiterr(const char *s)
int main(int argc, char *argv[])
{
int i, n = 1;
int fd;
char *fn;
struct stat st;
if(argc != 2) {
fprintf(stderr, "usage: %s <file> "
"-- call fadvise(DONTNEED) on file", argv[0]);
if(argc == 4 && !strcmp("-n", argv[1])) {
n = atoi(argv[2]);
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);
} else {
fn = argv[1];
}
fd = open(argv[1], O_RDONLY);
fd = open(fn, O_RDONLY);
if(fd == -1)
exiterr("open");
if(fstat(fd, &st) == -1)
exiterr("fstat");
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;
}
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;
}
if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1)
exiterr("posix_fadvise");
for(i = 0; i < n; i++)
if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1)
exiterr("posix_fadvise");
return EXIT_SUCCESS;
}
......@@ -5,12 +5,12 @@
/* Since open() and close() are re-defined in nocache.c, it's not
* 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
posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
#endif
return posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
int i, ret;
for(i = 0; i < n; i++)
ret = posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
return ret;
}
int fadv_noreuse(int fd, off_t offset, off_t len)
......
#ifndef _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 valid_fd(int fd);
extern void sync_if_writable(int fd);
......
......@@ -8,4 +8,9 @@ else
export LD_PRELOAD="$libnocache"
fi
if [ "$1" = "-n" ]; then
export NOCACHE_NR_FADVISE="$2"
shift 2
fi
exec "$@"
......@@ -60,9 +60,13 @@ static struct fadv_info fds[_MAX_FDS];
static size_t PAGESIZE;
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)
{
int i;
char *s;
char *error;
_original_open = (int (*)(const char *, int, mode_t))
......@@ -82,6 +86,11 @@ static void init(void)
exit(EXIT_FAILURE);
}
if((s = getenv(env_nr_fadvise)) != NULL)
nr_fadvise = atoi(s);
if(nr_fadvise <= 0)
nr_fadvise = 1;
PAGESIZE = getpagesize();
for(i = 0; i < _MAX_FDS; i++)
fds[i].fd = -1;
......@@ -295,14 +304,14 @@ static void free_unclaimed_pages(int fd)
while(j < fds[i].nr_pages) {
if(fds[i].info[j] & 1) {
if(start < j)
fadv_dontneed(fd, start*PAGESIZE, (j - start) * PAGESIZE);
fadv_dontneed(fd, start*PAGESIZE, (j - start) * PAGESIZE, nr_fadvise);
start = j + 1;
}
j++;
}
/* 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);
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