From 60fa5b0931b2bfffd7503c2b74704b446f3d523f Mon Sep 17 00:00:00 2001 From: Gert Hulselmans Date: Wed, 4 May 2022 14:16:45 +0200 Subject: [PATCH] Use mlock2() instead of mlock() on Linux and use mlockall(MCL_FUTURE). Use mlock2() instead of mlock() on Linux: - mlock2() allows a third argument MLOCK_ONFAULT, which allow to lock the pages in a range before touching them, so mlock2() can be called before touching all pages instead of after. - "vmtouch -t -l" some_file would not keep the pages in cache after touching them, so at the lock() step they needed to be read again from disk. After this patch files are cached twice as fast on this system. Use mlockall(MCL_FUTURE) instead of mlockall(MCL_CURRENT): - Set mlockall(MCL_FUTURE) before touching pages to have similar behaviour as the mlock2() approach above. --- vmtouch.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/vmtouch.c b/vmtouch.c index 8accef3..55ae8fb 100644 --- a/vmtouch.c +++ b/vmtouch.c @@ -169,7 +169,11 @@ void usage() { printf("Usage: vmtouch [OPTIONS] ... FILES OR DIRECTORIES ...\n\nOptions:\n"); printf(" -t touch pages into memory\n"); printf(" -e evict pages from memory\n"); +#if defined(__linux__) + printf(" -l lock pages in physical memory with mlock2(2)\n"); +#else printf(" -l lock pages in physical memory with mlock(2)\n"); +#endif printf(" -L lock pages in physical memory with mlockall(2)\n"); printf(" -d daemon mode\n"); printf(" -m max file size to touch\n"); @@ -614,6 +618,13 @@ void vmtouch_file(char *path) { char *mincore_array = malloc(pages_in_range); if (mincore_array == NULL) fatal("Failed to allocate memory for mincore array (%s)", strerror(errno)); +#if defined(__linux__) + if (o_lock) { + if (mlock2(mem, len_of_range, MLOCK_ONFAULT)) + fatal("mlock2: %s (%s)", path, strerror(errno)); + } +#endif + // 3rd arg to mincore is char* on BSD and unsigned char* on linux if (mincore(mem, len_of_range, (void*)mincore_array)) fatal("mincore %s (%s)", path, strerror(errno)); for (i=0; i