How to Trace Kernel Page Cache Hit Ratio and Evictions Using cachestat in Linux

The Linux kernel page cache is one of the most critical subsystems for achieving high file system performance. By caching frequently accessed disk blocks in unused physical RAM, the kernel satisfies read requests directly from memory at gigabytes per second, avoiding slow secondary storage access. However, when working set sizes exceed physical memory or background batch processes flush cache lines, page cache hit ratios collapse. The cachestat utility from the BCC toolkit samples kernel page cache hit rates, misses, and evictions in real time.

Why Monitoring the Page Cache Matters

Storage subsystem bottlenecks often masquerade as application performance problems:

  • High Cache Hit Ratio (> 95%): The vast majority of file I/O operations are satisfied directly from DRAM, yielding sub-microsecond latency and preserving disk controller bandwidth.
  • Low Cache Hit Ratio (< 80%): Applications are continuously experiencing cache misses, forcing synchronous disk reads that throttle database transaction rates.
  • Page Evictions (Marked Page Thrashing): Occurs when the kernel is forced to purge clean cache pages or write out dirty pages to disk to accommodate aggressive memory allocation requests from other processes.

Installing the BCC Tracing Tools

Ensure that BCC tools and kernel development headers are installed on your Linux distribution:

# On Debian, Ubuntu, or Pop!_OS:
sudo apt update
sudo apt install bpfcc-tools linux-headers-$(uname -r)

# On RHEL, AlmaLinux, Rocky Linux, or Fedora:
sudo dnf install bcc-tools kernel-devel-$(uname -r)

On Debian/Ubuntu environments, execute the binary as cachestat-bpfcc; on enterprise RHEL distributions, it is located at /usr/share/bcc/tools/cachestat.

Running cachestat to Measure Hit Ratios in Real Time

To start monitoring page cache efficiency, run cachestat with administrative privileges, specifying an update interval in seconds:

sudo cachestat-bpfcc 2

This command samples kernel page cache counters every 2 seconds.

Example terminal output:

    HITS   MISSES  DIRTIES HITRATIO   BUFFERS_MB  CACHED_MB
   45120      840     1200   98.17%          142       8192
   51200      310      890   99.40%          142       8194
   12400     9800     4500   55.86%          140       7950
   18900    14200     6100   57.10%          138       7820

Interpreting the Diagnostic Columns

Understanding each metric allows systems engineers to diagnose memory pressure:

  • HITS: Number of page cache hits satisfied directly from memory.
  • MISSES: Number of read operations that were not in memory, requiring disk retrieval.
  • DIRTIES: Number of pages modified in memory that must eventually be written to disk.
  • HITRATIO: Percentage of total read operations satisfied from page cache. Sudden drops (as shown in rows 3 and 4 above) indicate memory thrashing or large sequential scans.
  • BUFFERS_MB & CACHED_MB: Total memory allocated to buffer heads and active page cache.

Remediating Low Cache Hit Ratios

When cachestat reveals persistent cache degradation:

  • Isolate Memory-Intensive Batch Jobs: Use fadvise(POSIX_FADV_DONTNEED) or tools like vmtouch to prevent backup utilities or rsync runs from evicting working-set database pages.
  • Tune Swappiness and Flush Thresholds: Adjust dirty page flush ratios in sysctl to prevent heavy burst writes from stalling the system:
    sudo sysctl -w vm.dirty_background_ratio=5
    sudo sysctl -w vm.dirty_ratio=10
  • Memory Pinning: Configure databases (e.g., PostgreSQL or Redis) to lock their primary data buffer pools into RAM.

Using cachestat provides real-time visibility into Linux storage caching, empowering administrators to eliminate memory thrashing and optimize file system responsiveness.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.