tier1.jp

Utilize XDG dirs as userland tmpfs

XDG user directories, a set of sub directories under $HOME and /run/user tmpfs, are utilized by some applications now a days.

Let us (ab)use it for performance, and write I/O reductions on SSD/NVMe.

Rev 2

Add Firefox disk cache issue.

Note

XDG Base Directory Specification tells you what is.

An article in this site about mountpoint amount shows how huge write operations on /home could be (in Stretch).

$XDG_RUNTIME_DIR

Take a look at your env filesystem.

user$ findmnt
...
/run/user/1000 tmpfs ...
...
user$ env | grep XDG
XDG_...
XDG_RUNTIME_DIR=/run/user/1000

You can read about it by man xdg-user-dir and man file-hierarchy.

/run

A "tmpfs" file system for system packages to place runtime data in. This directory is flushed on boot, and generally writable for privileged programs only. Always writable.

/run/log

Runtime system logs. System components may place private logs in this directory. Always writable, even when /var/log might not be accessible yet.

/run/user

Contains per-user runtime directories, each usually individually mounted "tmpfs" instances. Always writable, flushed at each reboot and when the user logs out. User code should not reference this directory directly, but via the $XDG_RUNTIME_DIR environment variable, as documented in the XDG Base Directory Specification[3].

—from man file-hierarchy in Debian GNU/Linux 9.9

In short,

  • $XDG_RUNTIME_DIR is a tmpfs for each user session.
  • XDG handles well known directories such as "Documents", "Pictures", AND ".config", ".cache", etc.

No need to modify fstab nor install ramfs

Assuming we have a modern Linux system, it provides XDG user directories.

  1. XDG user dirs are under $HOME.
  2. $XDG_RUNTIME_DIR (/run/user/UID/) per user session.
  3. XDG provides some more features, ".config", ".cache", etc.

Warning

As mentioned in man file-hierarchy quote above, do not refer /run/user/UID path directly.

Use $XDG_RUNTIME_DIR.

As a fast (volatile) "tmpfs"

We can utilize $XDG_RUNTIME_DIR,

  1. As a ultra fast filesystem for each user.
  2. It can be used to reduce write operations on SSDs/NVMe.

Examples

Simply create some unique directory under $XDG_RUNTIME_DIR.

Put some temporary data such as caches, debug logs, etc.

Performance

In amd64 Kaby Lake with 16GB DRAM machine,

  • Creating a file from /dev/zero in $XDG_RUNTIME_DIR is very fast, nearly 3.6GB/sec (depends on size of course.)

Limitations

  • $XDG_RUNTIME_DIR amount is bit smaller than ordinal tmpfs.
  • If you need larger tmpfs, write fstab and use that instead.

Comparison with tmpfs /tmp

  • /tmp is shared among system and users.
    • Everyone has rwx permission and umask is 0022.
    • To make /tmp tmpfs, you need to edit /etc/fstab in most cases.
    • It could be an ordinal partition; See also FHS.
    • It could be mounted with mount option such as noexec.
  • $XDG_RUNTIME_DIR is user exclusive.
    • No configuration needed; normal users have their own.
    • It is very volatile , flushed on reboots and logouts.

Utilize XDG cache

Debian ships with tmpfs $XDG_RUNTIME_DIR, but the most XDG directories are not on the tmpfs;

  • They are on your $HOME directory; ".cache", ".config", etc.
  • ".cache", $XDG_CACHE_HOME, could relatively be safe to move onto tmpfs.

Caution!

NEVER MOVE OTHER XDG HOME DIRS EXCEPT CACHE.

DON'T RISK YOUR DATA AND SETTINGS.

It does not reduce writes so much, but improves application responses such as browsers.

user$ nano ~/.profile # add these lines.
export XDG_CACHE_HOME="$XDG_RUNTIME_DIR/.cache"
if [ ! -d "$XDG_CACHE_HOME" ] ; then
    mkdir $XDG_CACHE_HOME
fi

Considering what cache is, this makes sense. Cache data be written once (or few times,) and be read many times.

Warning

Not all application support XDG and/or handles its location.

Some application could have malfunction with the setting above.

Firefox ESR stores its "disk cache" under $XDG_CACHE_HOME, so it would be "totally memory cache", volatile.

Pitfalls

  • Be careful about moving other XDG directories other than ".cache".
  • Utilizing XDG tmpfs,
    • consumes DRAM of course.
    • is bit limited size. If you need more, write tmpfs entry in fstab.
  • $XDG_RUNTIME_DIR is very volatile tmpfs.
    • It would be flushed on logouts, in addition to rebooting.
    • Don't put something important. It's just a workbench.
  • AppArmor profiles needs some fixes.
    • It's better to add both $HOME and $XDG_RUNTIME_DIR cases.

Summary

  • XDG already provide us tmpfs for each user.
  • Do not change XDG envs other than ".cache"; $XDG_CACHE_HOME.
  • We can abuse XDG as a fast volatile workbench.
    • Such as compiler's object files storage, debug logs, etc., to reduce write operations on SSDs/NVMe.
    • Use via $XDG_RUNTIME_DIR; NEVER REFER /run/user/UID.
  • $XDG_RUNTIME_DIR's amount is about 10% of your DRAM.
    • if you want more, add a dedicated tmpfs entry in your fstab.

Thank you for reading. Have a nice day.

published: MODIFIED: