cachestat(2)

πŸ’… cachestat β€” bestie, let's read the page cache

okay so this one lives in section 2 of the manual (the System Calls Manual, if you're keeping score), and it's called cachestat. Yes girl, it's a syscall. Come sit with me. 🫢

✨ NAME ✨

cachestat β€” query the page cache statistics of a file

🌸 SYNOPSIS 🌸

lowkey the whole shape of it, no notes:

#include <sys/mman.h>

int cachestat(unsigned int fd, struct cachestat_range *cstat_range,
              struct cachestat *cstat, unsigned int flags);

struct cachestat_range {
    __u64 off;
    __u64 len;
};

struct cachestat {
    __u64 nr_cache;
    __u64 nr_dirty;
    __u64 nr_writeback;
    __u64 nr_evicted;
    __u64 nr_recently_evicted;
};

like. look at those structs. they're giving "I know exactly what I'm about". πŸ‘‘

πŸ¦‹ DESCRIPTION πŸ¦‹

so cachestat() is literally the gossip channel for your page cache. you ask it about a byte range, and it tells you, for that range:

  • how many pages are cached
  • how many are dirty
  • how many are marked for writeback
  • how many got evicted
  • and how many got recently evicted

and which range are we talking about? the one you set in the cachestat_range struct, using .off and .len. that's it. that's the vibe check. πŸ’–

now let's define terms because not everyone's fluent yet:

an evicted page is one that was previously in the page cache but has since been evicted. rude of the kernel honestly.

a page counts as recently evicted if its reentry into the cache would indicate active usage under memory pressure. iykyk β€” that's the kernel saying "if you come back for this, you were actually using it."

and where do the numbers go? into a cachestat structure, which you point at with the cstat argument. hand it a pointer, get your stats. simple. ✨

the rules for the range, bestie, listen carefully:

  • The .off and .len fields must be non-negative. no negatives, periodt.
  • If .len > 0, the queried range is [.off, .off+.len].
  • If len == 0, the range is from .off to the end of the file. (yes, len, not .len β€” that's how it's written, don't come for me πŸ™ˆ)

about that flags argument: it is reserved for future use and must be set to 0. it's the "we'll deal with you later" field. put a zero in it. πŸŽ€

and nn, one small thing: currently, hugetlbfs files are not supported. not me being dramatic, it's just true. πŸ’€

🌈 RETURN VALUE 🌈

  • success? cachestat() returns 0. slay.
  • error? it returns -1, and errno is set to tell you which error. that's your diagnosis, read it. 🩺

πŸ’œ ERRORS πŸ’œ

  • EFAULT β€” cstat or cstat_range point to an invalid address. your pointer is lost, babe. 😭
  • EINVAL β€” Invalid flags value. you put something in the reserved field, didn't you.
  • EBADF β€” Invalid file descriptor.
  • EOPNOTSUPP β€” the file descriptor refers to a hugetlbfs file, which is unsupported. yeah. that one. πŸ™ˆ

⭐ STANDARDS ⭐

Linux. that's it, that's the whole list. πŸ‡±

πŸŽ€ HISTORY πŸŽ€

Linux 6.5. so it's a newer-era icon. not vintage, not brand new β€” just right. ✨

πŸ‘‘ CAVEATS πŸ‘‘

heads up: the status of a page may change after cachestat() retrieves it but before the values are returned to your application. so yes, the values may be slightly outdated. it's not a bug, it's just the passage of time. the kernel is fast, you're fast, pages move. πŸ’…

and that's the whole page, bestie. you ate. πŸ“

sign-off corner: this was from Linux man-pages 6.20, dated 2025-06-28, and it's the cachestat(2) page β€” go forth and stat your cache. πŸ¦‹πŸ’–

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms