While optimising my PHP opcode caches, I tried alternating the stat setting ( = the opcode cache checks, whether a file was modified via stat(), before using the cached results). But I did not see any difference in my application benchmarks. The filesystem in use is ext4, mounted with noatime option.

Is it correct to assume, that the metadata used by stat(), is kept in memory, or is there always some kind of harddisk interaction when stat() is used?

  • "Application benchmarks" implies there is more going on than just an internal stat() to check PHP's caches. Whatever that is, it almost certainly dwarfs the check (minimal disk I/O or not). That said, I'd guess if the system is using its own disk cache, then stat'ing a file in there will just use that -- and if you are benchmarking something on a system with some free memory to evaluate your own disk cache, the system probably has them cached in your free mem, too. – goldilocks Nov 25 '13 at 23:16
  • 1
    The answer here: unix.stackexchange.com/questions/51852/… supports my/your guess/assumption too. It would be a little weird if the one thing which can guarantee transparency in a file cache (the kernel) acted in a paranoid way and doubled checked hardware nothing else can control or access. – goldilocks Nov 25 '13 at 23:20
  • Thank you @goldilocks that makes sense regarding the double checking. I saw that linked post before, but it was not obvious for me or I did not understand from the answers there, that metadata is cached. (Regarding the app overhead/benchmarks, I get your point, but it was also the reason for my question, sometimes you have to find things not working, by questioning what other things work or not) – Martin Nov 26 '13 at 2:19

The data returned by stat (the file's metadata) is cached like any other filesystem data. If you accessed it recently enough for it to be still in the cache, then subsequent accesses are faster, until something else replaces it in RAM.

Accessing a file's content does not load its metadata into memory (or vice versa).

The stat check costs a little extra (very little if the metadata is in cache, but still a little). Whether this compensates the potential extra processing depends on how much processing you'd be doing and on your IO/CPU saturation ratio.

  • 1
    how to force clean that cache? – Zibri Apr 9 '18 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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