8

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?

5
  • since kernel 3.5 - vm.swappiness=0 – mikeserv Jul 25 '14 at 6:36
  • I got the answer from here: unix.stackexchange.com/a/90337/56970 vm.swappiness makes sense, but you can't seem to limit it to a single directory. – CMCDragonkai Jul 25 '14 at 6:38
  • @mikeserv But I still want applications to be able to swap. – CMCDragonkai Jul 25 '14 at 6:38
  • This patch changes the behavior with swappiness==0. If we set swappiness==0, the kernel does not swap out completely (for global reclaim until the amount of free pages and filebacked pages in a zone has been reduced to something very very small (nr_free + nr_filebacked < high watermark)). – mikeserv Jul 25 '14 at 6:40
  • your question has very little to do with the directory /tmp but far more to do with which is swapped first - process memory or pagecache? Because tmpfs is mounted page cache. look at this comment at the page linked above. – mikeserv Jul 25 '14 at 6:54
6

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.

1
  • 1
    Guess I'll have to write up an experiment to see how tmpfs gets swapped out. – CMCDragonkai Nov 10 '15 at 5:06
2

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.