How to tell when a file is read from disk or cache?

James Wilkinson fedora at aprilcottage.co.uk
Sun Nov 18 23:15:25 UTC 2012


Philip Rhoades wrote:
> Is there some way of doing this? - even for a simple situation like
> opening a text file in Vim?

Depends on what you want.

If what you’re after is to tell after the fact that a file has been
accessed, you should look into auditd, or just turn on atime on your
filesystem (note that this has been turned off for good reasons). You
may even find that relatime does what you need: I think
https://lwn.net/Articles/244829/ is still accurate.

If you want to run a program the instant a file has been accessed, you
will need to look into fanotify (and probably do some programming): see
https://lwn.net/Articles/360955/ for details.

These will let you know when a program is opened for reading: if you
then want to know each time a program reads a byte, you’re pretty much
out of luck: the mmap API means the OS doesn’t get to see precisely when
a particular byte is read (if it’s already in cache).

Hope this helps,

James.

-- 
E-mail:     james@ | A wood-pigeon would, If a wood-pigeon could,
aprilcottage.co.uk | But a wood-pigeon can’t, So it won’t.
                   | A wood-pigeon could, If a wood-pigeon would,
                   | But a wood-pigeon doesn’t want to. So it doesn’t.


More information about the users mailing list