How to make files inside TMPFS more likely to swap

I’m using tmpfs for my /tmp directory. How can I make the computer decide to swap out the files inside the /tmp before swapping out anything that is being used by applications?

Basically the files inside /tmp should have a higher swappiness compared to the memory being used by processes.

It seems this answer https://unix.stackexchange.com/a/90337/56970 makes a lot of sense, but you can’t change swappiness for a single directory. I know about cgroups though, but I don’t see any way of making tmp into a cgroup?

Asked By: CMCDragonkai

||

If all goes well, your kernel should decide to “do the right thing” all by itself. It uses a lot of fancy heuristics to decide what to swap out and what to keep when there is memory pressure. Those heuristics have been carefully built by really smart people with a lot of experience in memory management and are already good enough that they’re pretty hard to improve upon.

The kernel uses a combination of things like this to decide what to swap out:

  • How recently the memory has been used.
  • Whether the memory has been modified since it was mapped. So for example a shared library will be pushed out ahead of heap memory because the heap memory is dirty and needs to be written to swap, whereas the shared library mapped memory can be loaded again from the original file on disk in case it is needed again, so no need to write those pages to swap. Here you should realize that tmpfs memory is always dirty (unless it’s a fresh page filled with zeros) because it is not backed by anything.
  • Hints from mprotect().
  • Likely many more.

Short answer: no, you can’t directly override the kernel’s decisions about how to manage memory.

Answered By: Celada

cgroup’s are meant to control applications, not filesystems. What you’re wanting would need to either be a sysctl or a mount option since it’s for an entire filesystem. Unfortunately it doesn’t look like either of those two features has been created for this problem. That’s probably due to how unique an issue like this is.

To work around this, you may try writing a cronjob such as:

*/5 * * * * find /tmp -type f -exec cat {} ; >/dev/null 2>&1

This should force the kernel to read all the files in, which forces the pages to get swapped back into memory.

That said, files frequently accessed by your workload application would do essentially the same as we’re doing in the cronjob (and only for the specific files it actually used instead of the whole mount). I can’t see much advantage to do this.

What’s the actual goal here?

Answered By: Bratchley
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.