When should I use
/dev/shm/ and when should I use
/tmp/? Can I always rely on them both being there on Unices?
When should I use
Recent 2.6 Linux kernel builds have started to offer /dev/shm as shared memory in the form of a ramdisk, more specifically as a world-writable directory that is stored in memory with a defined limit in /etc/default/tmpfs. /dev/shm support is completely optional within the kernel config file. It is included by default in both Fedora and Ubuntu distributions, where it is most extensively used by the Pulseaudio application. (Emphasis added.)
Since RAM is significantly faster than disk storage, you can use
/dev/shm instead of
/tmp for the performance boost, if your process is I/O intensive and extensively uses temporary files.
To answer your questions: No, you cannot always rely on
/dev/shm being present, certainly not on machines strapped for memory. You should use
/tmp unless you have a very good reason for using
/tmp can be part of the
/ filesystem instead of a separate mount, and hence can grow as required. The size of
/dev/shm is limited by excess RAM on the system, and hence you're more likely to run out of space on this filesystem.
In descending order of
┌───────────┬──────────────┬────────────────┐ │ /dev/shm │ always tmpfs │ Linux specific │ ├───────────┼──────────────┼────────────────┤ │ /tmp │ can be tmpfs │ FHS 1.0 │ ├───────────┼──────────────┼────────────────┤ │ /var/tmp │ never tmpfs │ FHS 1.0 │ └───────────┴──────────────┴────────────────┘
Since you are asking about a Linux specific tmpfs mountpoint versus a portably defined directory that may be tmpfs (depending on your sysadmin and what's default for your distro), your question has two aspects, which other answers have emphasized differently:
- When to use these directories, based on good practice
- When it is appropriate to use tmpfs
Conservative edition (mixture of conventions from FHS and common use):
- When in doubt, use
/var/tmpfor large data that may not easily fit in ram.
/var/tmpfor data that is beneficial to keep across reboots (like a cache).
/dev/shmas a side-effect of calling
shm_open(). The intended audience is bounded buffers that are endlessly overwritten. So this is for long lived files whose content is volatile and not terribly large.
- If still in doubt, provide a way for the user to override. For example, the
mktempprogram honors the
/dev/shm when it is important to use tmpfs,
/var/tmp when it is important not to, else
Where tmpfs excels
fsync is a no-op on tmpfs. This syscall is the number one enemy of (IO) performance (and flash longevity, if you care about that), though if you find yourself using tmpfs (or eatmydata) just to defeat fsync, then you (or some other developer in the chain) are doing something wrong. It means that the transactions toward the storage device are unnecessarily fine grained for your purpose – you are clearly willing to skip some savepoints for performance, as you have now gone to the extreme of sabotaging them all – seldom the best compromise. Also, it is here in transaction performance land where some of the greatest benefits of having an SSD are – any decent SSD is going to perform out-of-this-world compared to what a spinning disk can possibly take (7200 rpm = 120 Hz, if notihing else is accessing it), not to mention flash memory cards, which vary widely on this metric (not least because it is a tradeoff with sequential performance, which is what they are rated by, e.g. SD card class rating). So beware, developers with blazing fast SSDs, not to force your users into this use case!
Wanna hear a ridiculous story? My first
fsync lesson: I had a job that involved routinely "upgrading" a bunch of Sqlite databases (kept as testcases) to an ever-changing current format. The "upgrade" framework would run a bunch of scripts, making at least one transaction each, to upgrade one database. Of course, I upgraded my databases in parallel (8 in parallel, since I was blessed with a mighty 8 core CPU). But as I found out, there was no parallelization speedup whatsoever (rather a slight hit) because the process was entirely IO bound. Hilariously, wrapping the upgrade framework in a script that copied each database to
/dev/shm, upgraded it there, and copied it back to disk was like 100 times faster (still with 8 in parallel). As a bonus, the PC was usable too, while upgrading databases.
Where tmpfs is appropriate
The appropriate use of tmpfs is to avoid unnecessary writing of volatile data. Effectively disabling writeback, like setting
/proc/sys/vm/dirty_writeback_centisecs to infinity on a regular filesystem.
This has very little to do with performance, and failing this is a much smaller concern than abusing fsync: The writeback timeout determines how lazily the disk content is updated after the pagecache content, and the default of 5 seconds is a long time for a computer – an application can overwrite a file as frequently as it wants, in pagecache, but the content on disk is only updated about once every 5 seconds. Unless the application forces it through with fsync, that is. Think about how many times an application can output a small file in this time, and you see why fsyncing every single one would be a much bigger problem.
What tmpfs can not help you with
- Read performance. If your data is hot (which it better be if you consider keeping it in tmpfs), you will hit the pagecache anyway. The difference is when not hitting the pagecache; if this is the case, go to "Where tmpfs sux", below.
- Short lived files. These can live their entire lives in the pagecache (as dirty pages) before ever being written out. Unless you force it with
Where tmpfs sux
Keeping cold data. You might be tempted to think that serving files out of swap is just as efficient as a normal filesystem, but there are a couple of reasons why it isn't:
- The simplest reason: There is nothing that contemporary storage devices (be it harddisk or flash based) loves more than reading fairly sequential files neatly organized by a proper filesystem. Swapping in 4KiB blocks is unlikely to improve on that.
- The hidden cost: Swapping out. Tmpfs pages are dirty — they need to be written somewhere (to swap) to be evicted from pagecache, as opposed to file backed clean pages that can be dropped instantly. This is an extra write penalty on everything else that competes for memory – affects something else at a different time than the use of those tmpfs pages.
Okay, here's the reality.
Both tmpfs and a normal filesystem are a memory cache over disk.
The tmpfs uses memory and swapspace as it's backing store a filesystem uses a specific area of disk, neither is limited in the size the filesystem can be, it is quite possible to have a 200GB tmpfs on a machine with less than a GB of ram if you have enough swapspace.
The difference is in when data is written to the disk. For a tmpfs the data is written ONLY when memory gets too full or the data unlikely to be used soon. OTOH most normal Linux filesystems are designed to always have a more or less consistent set of data on the disk so if the user pulls the plug they don't lose everything.
Personally, I'm used to having operating systems that don't crash and UPS systems (eg: laptop batteries) so I think the ext2/3 filesystems are too paranoid with their 5-10 second checkpoint interval. The ext4 filesystem is better with a 10 minute checkpoint, except it treats user data as second class and doesn't protect it. (ext3 is the same but you don't notice it because of the 5 second checkpoint)
This frequent checkpointing means that unnecessary data is being continually written to disk, even for /tmp.
So the result is you need to create swap space as big as you need your /tmp to be (even if you have to create a swapfile) and use that space to mount a tmpfs of the required size onto /tmp.
NEVER use /dev/shm.
Unless, you're using it for very small (probably mmap'd) IPC files and you are sure that it exists (it's not a standard) and the machine has more than enough memory + swap available.
Another time when you should use /dev/shm (for Linux 2.6 and above) is when you need a guaranteed tmpfs file system because you don't know if you can write to disk.
A monitoring system I'm familiar with needs to write out temporary files while building its report for submission to a central server. It's far more likely in practice that something will prevent writes to a file system (either out of disk space or an underlying RAID failure has pushed the system into a hardware read-only mode) but you'll still be able to limp along to alert about it than if something spirals all available memory such that tmpfs will be unusable (and the box won't be dead). In cases like this, a monitoring system will prefer writing out to RAM so as to potentially be able to send an alert about a full disk or dead/dying hardware.
/dev/shm is used for shared virtual memory system specific device drivers and programs.
If you are creating a program that requires a virtual memory heap that should be mapped to virtual memory. This goes double so if you need multiple processes or threads to be able to safely access that memory.
The fact is that just because the driver uses a special version of tmpfs for it, doesn't mean you should use it as a generic tmpfs partition. Instead, you should just create another tmpfs partition if you want one for your temporary directory.
In PERL, having 8GB minimum on any machine (all running Linux Mint), I am of what I think is a good habit of doing DB_File-based (data structure in a file) complex algorithms with millions of reads and writes using /dev/shm
In other languages, not having gigether everywhere, to avoid the starts and stops in network transfer (working locally on a file that is located on a server in a client-server atmosphere), using a batch file of some type, I will copy the whole (300-900MB) file at once to /dev/shm, run the program with output to /dev/shm, write the results back to the server, and delete from /dev/shm
Naturally, if I had less RAM, I would not be doing this. Ordinarily, the in-memory file system of /dev/shm reads as a size being one half of your available RAM. However, ordinary use of RAM is constant. So you really couldn't do this on a device with 2GB or less. To turn paraphrase to hyperbole, there is often things in RAM that even the system doesn't report well.