From 4de60d28ca1a034d9713ed0fc84ae6f93b0ffcc4 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Wed, 25 Jan 2023 13:29:29 +0100 Subject: Initial import --- .gitignore | 1 + Makefile | 2 ++ README.md | 9 +++++++++ dontneed.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 dontneed.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd1c827 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/dontneed diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cfc02c --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +dontneed: dontneed.c + gcc -Wall dontneed.c -o dontneed diff --git a/README.md b/README.md new file mode 100644 index 0000000..c164345 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# dontneed: tell linux you don't need some files in RAM cache + +Maybe used to prevent swapping about files you write but don't read, like somedump.sql.gz on somelog.1.gz. + + Usage: ./dontneed ... + Calls posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) on each + + Tip: you may find all cached files from rootfs with the costly one-liner: + # find / -mount -type f -print0 | xargs -r0 fincore | grep -vE '^ *0B' | sort -hr | uniq | less diff --git a/dontneed.c b/dontneed.c new file mode 100644 index 0000000..29b25d8 --- /dev/null +++ b/dontneed.c @@ -0,0 +1,32 @@ +#include // posix_fadvise +#include // errno +#include // strerror +#include // fprintf +#include // open,close + +int usage(char progname[]) { + fprintf(stderr, "Usage: %s ...\n", progname); + fprintf(stderr, " Calls posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) on each \n\n"); + fprintf(stderr, " Tip: you may find all cached files from rootfs with the costly one-liner:\n"); + fprintf(stderr, " # find / -mount -type f -print0 | xargs -r0 fincore | grep -vE '^ *0B' | sort -hr | uniq | less\n"); + return 1; +} + +int main(int argc, char** argv) { + int i, fd, advise_errno, errors=0; + if ( argc < 2 ) return usage(argv[0]); + for (i=1; i