FS Cache

19 December 2016

FS Cache is persistent local cache of file system mounter over network (typically using NFS). It can minimize network traffic. When network bandwidth is low or delay between server and host is hight it can also improve performance.

Note: Keep in mind that this documentation is still work in progress.

When you use NFS share and you want to improve performace with FS Cache, then you need not to do any changes at NFS server. All you have to do is prepare some disk partition (new and empty is the best) for caching and install caching daemon called: cachefilesd

When you use RedHat 7 or CentOS 7, then you can install cachefilesd using following command:

$ yum -y install cachefilesd

Configuration

You can use existing disk partition for caching, but it is recommended to use special partition only for caching. The first reason is a little bit better performance and the second is no need to set up cache cull limits.

The partition used for caching has to support extended attributes, because it is used for storing coherence maintenance information. Following file systems support extended attributes y default: ext4, xfs, btrfs. When you prefer to use ext3, then extended attributes has to be enabled:

$ tune2fs -o user_xattr /dev/device  # ext3 only

To format some empty partition using e.g. ext4 you have to type following command:

$ mkfs.ext4 /dev/device

Try to mount device to some directory:

$ mount /dev/device /some/directory

We will used it for caching of FS Cache and default directory used for caching is /var/cache/fscache. So we can use this directory as mount point. Don’t forget to add one record to /etc/fstab to automatically mount partition during start of the system:

/dev/device /var/cache/fscache   ext4     defaults        0 0

When you will use different mount point, then you will have to change recored in configuration file of cachefilesd. Configuration file /etc/cachefilesd.conf contains by default something like this:

dir /var/cache/fscache
tag mycache
brun 10%
bcull 7%
bstop 3%
frun 10%
fcull 7%
fstop 3%
secctx system_u:system_r:cachefiles_kernel_t:s0

It is assumed that you are able to mount NFS share using following command:

$ mount nfsshare.company.com:/ /nfsshare/local/mountpoint

Caching of NFS Share

When you configured FS Cache daemon, then you can try to start it using:

$ systemctl start cachefilesd

To automatically start this service during start of system type:

$ systemctl enable cachefilesd

To try caching of NFS share you have to unmount NFS share:

$ umount /nfsshare/local/mountpoint

and then you have to mount it again with following option:

$ mount nfsshare.company.com:/  /nfsshare/local/mountpoint -o fsc

When NFS share is mounted, then you can check statistics using:

$ watch -n 1 "cat /proc/fs/fscache/stats"

The cachefilesd will also create local caches in /var/cache/fscache

To use FS Caching automatically, when NFS share is mounted, you have to add fsc option to your /etc/fstab e.g.:

nfserver.company.cz:/	/nfsshare/local/mountpoint		nfs4	rw,soft,intr,noatime,rsize=32768,wsize=32768,fsc,comment=systemd.automount 0 0

You can check that your NFS share is mounted with fcs option using:

$ mount | grep "nfserver.company.cz"

Testing of FS Cache

To test disk performance you can use several tools:

  • dd
  • Bonnie
  • IOZone
comments powered by Disqus