Go to file
Mike Jagdis 658b3adab2
All checks were successful
CI / build (push) Successful in 34s
test
2023-07-24 10:58:55 +01:00
.forgejo/workflows test 2023-07-24 10:58:55 +01:00
.gitignore Initial version 2023-07-22 18:56:43 +01:00
LICENSE Initial version 2023-07-22 18:56:43 +01:00
loadallforever.c Initial version 2023-07-22 18:56:43 +01:00
Makefile Initial version 2023-07-22 18:56:43 +01:00
mmap_test.c Initial version 2023-07-22 18:56:43 +01:00
README.md Initial version 2023-07-22 18:56:43 +01:00

loadallforever

Sometimes you just want your process to stay in memory regardless. But tuning swappiness doesn't do what you think it does (or maybe want it to do). No matter what you try either data gets written to swap or code pages get reclaimed. Yet you want your process to stay resident (and are happy for everything else to get paged backwards and forwards).

Well, there's a way.

What you can do is to lock your process into memory using the mlockall syscall. If you have the source code. And are using a language that will let you make an mlockall syscall. But what if you can't?

In that case you can preload a DSO that does it for you when your process starts. That's what loadallforever does. Just set it in your environment and it will lock your process in memory:

A normal demand-page process does not lock memory by default:

./mmap_test $(ulimit -l)00
VmLck:	       0 kB

If we preload loadallforever.so any mappings for this process will be paged in and locked as soon as the mapping is created:

ulimit -l hard; \
LD_PRELOAD=./loadallforever.so \
./mmap_test $(ulimit -l)00
VmLck:	    3376 kB

If we also set LOADALLFOREVER_ONFAULT=1 in the environment then mappings will be allowed to page in data as normal but once in pages become locked: (N.B. this requires that loadallforever.so was compiled on a system that supports the MCL_ONFAULT mlockall flag AND that it is running on a system that supports the MCL_ONFAULT flag.)

ulimit -l hard; \
LD_PRELOAD=./loadallforever.so \
LOADALLFOREVER_ONFAULT=1 \
./mmap_test $(ulimit -l)00
VmLck:	    2444 kB

These rules do no apply to sub-processes though:

ulimit -l hard; \
LD_PRELOAD=./loadallforever.so \
./mmap_test $(ulimit -l)00 with-child
VmLck:	    3376 kB
VmLck:	       0 kB