Getting some information about the Linux kernel dentry cache (dcache)

December 4, 2023

The Linux kernel's dcache subsystem is its implementation of a name cache of directory entries; it holds dentries. As a (kernel) cache, it would be nice to know some information about this cache and how effective it was being for your worklog. Unfortunately the current pickings appear to be slim.

Basic information about the size of the dcache is exposed in /proc/sys/fs/dentry-state. This reports the total number of dentries, how many are 'unused', and how many are negative entries for files that don't exist (along with some other numbers). There's no information on either the lookup rate or the hit rate, and I believe that the kernel doesn't track this information at all (it sizes the dcache based on other things).

The BCC tools include a (BCC) program called dcstat. As covered in its documentation, this tool will print running dcache stats (provided that it works right on your kernel). The Storage and Filesystem Tools section of the BCC tools listings has additional tools that may be of interest in this general area. Although bpftrace has bpftrace-based versions of a lot of the BCC tools (see its tools/ subdirectory), it doesn't seem to have done a bpftrace version of dcstat.

(The other caution about dcstat is that based on comments in the dcstat source code I'm not sure that it's still right for current kernels. I think the overall usage rate is probably correct, but I'm not sure about the 'miss' numbers. I'd have to read fs/namei.c and fs/dcache.c very carefully to have much confidence.)

As far as I can see, /proc/sys/fs/dentry-state is not exposed by the Prometheus host agent. It might be exposed by the host agents for other metrics systems, or they might have left it out because there's not much you can do about the dcache anyway. If you wanted to export dcache hit and miss information, you could use the Cloudflare eBPF exporter and write an appropriate eBPF program for it, based on dcstat.

Now that I've looked at this, I suspect that while using dcstat may be interesting if you're curious about how many file lookups various operations do, it's probably not all that useful to monitor on an ongoing basis.

(In its current state, dcstat won't tell you how many hits were for negative dentries, which might be interesting to know so you can see how many futile lookups are happening on the system.)

Written on 04 December 2023.
« A bit more trivia on the Unix V6 shell and its control flow
Doing work that scales requires being able to scale your work »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Dec 4 23:36:25 2023
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.