/dev/shm

From ArchWiki

Jump to: navigation, search

The following introduction was taken from nixCraft's article on /dev/shm:

"/dev/shm is nothing but implementation of traditional shared memory concept. It is an efficient means of passing data between programs. One program will create a memory portion, which other processes (if permitted) can access. This will result into speeding up things on Linux. shm / shmfs is also known as tmpfs, which is a common name for a temporary file storage facility on many Unix-like operating systems. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device. If you type mount command you will see /dev/shm as a tempfs file system. Therefore, it is a file system, which keeps all files in virtual memory. Everything in tmpfs is temporary in the sense that no files will be created on your hard drive. If you unmount a tmpfs instance, everything stored therein is lost. By default almost all Linux distros configured to use /dev/shm."

Settings

Alter /etc/fstab's entry for /dev/shm and apply settings with mount -a, or reboot:

none   /dev/shm        tmpfs   defaults,size=768M,noexec,nodev,nosuid        0       0
size
Accepts 'k', 'm' or 'g' and their capitalized versions as units
noexec,nodev,nosuid
Universal options for most file-systems affecting permissions. See: man mount

Usage

Generally speaking, IO intensive tasks that benefit from fast, No-HDD-read/write-space, such as video encoding, gaming, etc. can make extensive use out of shm. However, that's not to say that simpler applications can't recieve substantial gains from offloading data onto shared memory. Firefox, for example, shows that having its profile relocated into ram makes a big difference.

Improving compile times

This is a small script that links makepkg's build space to a sub-directory in shm. Be sure to run it where the PKGBUILD is located. Also keep in mind that when it detects that a package is already cached in RAM, the script purges the package's cached build directory.

File: makepkg-shm
#!/bin/bash
BUILDSCRIPT=PKGBUILD
srcdir=src

. $BUILDSCRIPT || echo "Error: Failed to source \`$BUILDSCRIPT'"; exit 1
volatile=/dev/shm/$USER/makepkg/$pkgname

if [[ -f $srcdir/.tmpfs ]]; then
  rm -rf $volatile $srcdir
  echo "Success: Purged \`$srcdir' and \`$volatile'"
elif [[ -e $srcdir && `readlink $srcdir` != $volatile ]]; then
  echo "Error: \`$srcdir' exists and is not a link to \`$volatile', aborting"; exit 2
else
  install -dm700 $volatile
  ln -s $volatile $srcdir
  touch $srcdir/.tmpfs
  echo "Success: \`$srcdir' linked to \`$volatile'"
fi

Save it as ~/bin/makepkg-shm, for example, and make it executable:

$ chmod +x ~/bin/makepkg-shm
Personal tools