The vmtouch(8) tool allows locking a file or files in memory. It has recursive mode, but the directories are used just for discovering files and are not locked themselves.

How do I lock a directory in memory, so that readdir be always fast until I unlock it?

vmtouch is definitely NOT going to manage this. vmtouch does open(2) & mmap(2) on the files you instruct it to.

Even if you get the kernel to cache readdir(2), it's still a linear operation, and still hits disk in many cases:

In the current VFS readdir operation, the low-level file system is always called, even if the entire directory is in cache.

(2017/08/07 18:40 UTC followup):

ls -l does a lot more than just readdir. readdir(2) is obsolete anyway, readdir(3) is a wrapper for (f)stat on the directory & getdents on the directory. The -l option of ls also causes an lstat on every entry in it.

getdents in itself is just a wrapper to whatever a given filesystem provides for the .iterate_shared & .iterate pointers on directory file struct's operations. In the case of ext4, this winds up down in ext4_readdir.

Some filesystems do implement caching at that layer (NFS is a very clear example), but others might hit disk still.

For the root of your original question, you could write some daemon that repeatedly keeps running getdents on a given directory, but there's no way to pin those dentry items in the kernel cache otherwise, and it's possible that the kernel completely ignores that cache from a separate thread (depends on the filesystem).

Focusing on ext4 for a moment, if the filesystem has the dir_index feature, you can do chattr +I dir on a directory to mark a directory as being htree-indexed (very small directories use that cache as well if the filesystem feature is set)

  • Typically second, hot ls -l is much faster than the first. I want pin this "hotness" until I release it, not to wear out after time passes or other content gets accessed. For example, pressing Tab twice makes bash wait for 6 seconds before presenting Display all 9454 possibilities? the first time, but not second time. How to I tell system not to forget this state of fast tab completion? – Vi. Aug 7 '17 at 18:04
  • My response was too long for a comment, so I edited it in. – robbat2 Aug 7 '17 at 18:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.