tmpfs, use case

This post is also available in: French

Intro

tmpfs is a memory filesystem available on many *nix (OpenBSD, FreeBSD, GNU/Linux, Solaris, for those I’ve touched) (it’s called md on *BSD but I’ll always refer to it as tmpfs)

You’ll ask me what it is used for … Please note that the RAM is 1 millions times faster than a hard disk : access on the former are of nanoseconds, tenth of milliseconds on the latter. Okay, cache system on hard disk made those access are faster than I say, but they are far from those of RAM.

So what ? According to my small tests, without being 1000x faster access time are 10 to 50x faster, and only electronic : no mechanism. You see my point ?

But there is big counterpart : files written on tmpfs disappear when you switch off computer (which is also and advantage : security)

And a small counterpart : it use RAM, but not more than necessary : if you mount a tmpfs of 512 Mio, it will on takes in memory only the size of written files, not 512Mio

Here some few commands to use it.

Commands

*nix

As all filesystem, you can mount tmpfs with mount(8).

By default, the size of the mounted tmpfs is half of the available RAM. It’s possible to change this :

mount -o size=1G -t tmpfs myTmpfs /a/path

Et voilĂ , you have a tmpfs of 1Gio. Tips: ‘myTmpfs’ will be the name of the filesystem when you type mount(8) (without options)

Just a thing : size should not be greater than RAM+Swap, because if you fill the tmpfs, you’ll fill all your RAM and the swap. Imagine the consequences…

And what if your tmpfs is too small, full of things you don’t want to lose (like a running compilation) ?

mount -o size=2G,remount /a/path

Hop, your tmpfs is now of 2Gio.

*bsd

It’s a bit different. It’s called md : memory disk, it’s a bit more complex than on Linux (possibility on FreeBSD to work with 4 systems, including RAM to work similarly as Linux, or a file)

Commands are almost the same :

FreeBSD : mdmfs -s <size> md /a/path

Note that you have to specify the size.

(also look at mdconfig)

OpenBSD : mount_mfs -s <size> swap /a/path

As far as today, it’s not possible to hot resize an md

One difference between the one of FreeBSD and OpenBSD : these of OpenBSD offer the capability to populate your newly created md with the content of a .tar (or cpio) file, FreeBSD seems not to. What is use for ? Try to put your /var/log in tmpfs (or md) and you’ll see the point : syslogd need a fully functional directory tree out there.

So, what all those things can be used for ?

Note that tmpfs/md are available on the OS above (and mounted on a GNU/Linux out of the box). What to do with that ?

Its speed and versatility can be advantages, I quote you 3 cases in which I use it.

Use case

Cache

tmpfs is fast, tmpfs vanish on reboot, and a tmpfs-based filesystem has a fixed size (specified at mount time, by default half of your RAM. I repeat : do not put more than RAM+Swap, or you’ll go into trouble), so tmpfs can be used to put cache files.

Unsorted :

  • Firefox cache,
  • pulseaudio cache,
  • Smarty cache,
  • Some temporary files

In brief, store files whose lifetime does not exceed the one between two reboot (it’s not a good idea to put Squid’s cache, for example)

But please limit the size of the cache : if you use it for Firefox cache, lower this value (into Firefox). First to not take 50Mio of memory, second because 50Mio is a bit too much, don’t you think ?

Compile

tmpfs is fast, tmpfs can be big, so tmpfs can’t be used as a compilation directory.

I use it on my Gentoo and FreeBSD. Try this :

  • Create thousands of files in /tmp/plop
  • Do the same in /dev/shm/plop
  • Time the cleaning of those files. On tmpfs I get 0.013 ms, on the regular filesystem (reiserfs) 0.056 ms

On Gentoo, the tool ‘genlop’ can show the time taken by a compilation. I’ve made a test : compiling bash. It takes 4s less on tmpfs (on an overall of 2min20 on a regular filesystem). Not huge but I guess that the gain is not linear with the size of the compiled software.

Icing on the cake : I do not take any space on the hard disk, and has it does not use your hard disk, it does not stress it (making it last longer)

By the way …

Avoid disk access

tmpfs is un RAM, so tmpfs does not write on disk, thus the performance above mentioned.

In which case is it good to not write on disks ? When your hard disk is a compact flash (this never happened to you ?? ), as, say, on my OpenBSD firewall : low energy footprint, zero noise, low access time (but it’s not a big deal as you never connect on a firewall, isn’t it ? ), and … low lifetime of CF cells, who has a limited lifetime depending of number of read/write cycle.

Here, the idea is to mount tmpfs where the OS write often.

Here is whose I’ve mounted on tmpfs on my OpenBSD :

  • /var/log : yep ! and syslogs are also send to an other host through network. Do not forget to tune the logrotate to not fill the tmpfs, and use a skeleton when mounting to create necessary directories.
  • /var/run : as on most GNU/Linux system…
  • /var/tmp : temporary directory. /tmp is now just a soft link to this directory 😉
  • /usr/obj : compilation directory. I do not compile often, I guess there should have space problem …

Conclusion

Cheap to achieve, tmpfs are good for safety, performance, and the planet 😉

Leave a Reply

Your email address will not be published. Required fields are marked *