Linux Tips

Increase the number of directories Linux can monitor (inotify) for PMS

The default size of the Linux inotify table is 8192 directories (*1). When PMS is configured to update automatically when media is added, with properly organized media, especially music, this table rapidly fills and exceeds the default allocation requiring you to increase it.

The solution to this is straight forward. Increasing the table size is easy. The first step is to determine how many directories to allocate,

A. Determine how many directories are currently in use:

sudo find /dir1 /dir2 /dir3 -type d -print | wc -l (use your actual directory names and not dir1, dir2, or dir3. You may add as many top level directories as you need)

B. Determine how many directories to actually allocate

Round up to the next multiple of 32768 (32K). This is done to maximize memory page alignment and keep the maximum amount of memory available for hardware transcoding (if used). This number is called NEW_MAX_DIRECTORIES in the example below.

C. Calculate and verify memory utlization

Multiply the number of entries from above by 512 (worst case size of an inode entry). This tells you how much kernel memory will be permanently allocated and not available to other applications such as PMS. If this results in more memory allocated than you are comfortable allocating, a decision must be made to a) Add more memory to the system or b) reduce the number of directories PMS can automatically monitor. In most cases, increasing system memory is more desirable and yields better overall performance.

D. Implement the change
With calculations complete, it is time to implement the configuration change.

Procedure:

sudo sh
echo  fs.inotify.max_user_watches=NEW_MAX_DIRECTORIES  >> /etc/sysctl.conf
sysctl -p

Example: Allocate 256K (262144) directory entries for the iNotify table


sudo sh
find /nas/movies /nas/tv /nas/music -type d -print | wc -l
201322
#
#
# Round 201322 up to 256K  (262144)
echo  fs.inotify.max_user_watches=262144  >> /etc/sysctl.conf
sysctl -p

In the above example, the table was configured for 262144 (256K) directories on a system with 8GB of main memory.

sysctl was then invoked to make the change immediate (-p).
The system can also be rebooted to make the change take effect.

Notes:

*1 - The default for older Linux systems is 8192. Effective November 2020 Linux kernel, this value is dynamic based on memory and up to 1048576 at start. Most users, with 32GB or more will see the immediate increase to 65536 without making any kernel tuning changes.

Back to top

1 Like