温馨提示×

linux缓存命中率怎么看

小樊
35
2026-10-27 01:36:16
栏目: 智能运维

在 Linux 下查看缓存命中率(Cache Hit Ratio),要看你指的是哪一类缓存。常见有:CPU 缓存、Page Cache(文件缓存)、磁盘/块设备缓存、应用层缓存(如 Redis)。下面按常见场景说明。


一、Page Cache(文件系统缓存)命中率

1️⃣ 使用 cachestat(推荐,最直观)

cachestat 来自 perf-tools / bcc,可直接看命中率:

cachestat 1

示例输出:

HITS   MISSES  DIRTIES  READ_HIT%  WRITE_HIT%
12033    342      12        95.3%      10.2%
  • HITS:缓存命中
  • MISSES:缓存未命中
  • READ_HIT%:读缓存命中率

没安装可用:

apt install bcc
# 或
git clone https://github.com/brendangregg/perf-tools

2️⃣ 用 /proc/vmstat 手动算(无工具时)

grep -E "pgpgin|pgpgout|pswpin|pswpout" /proc/vmstat

更常用的是:

grep -E "cache" /proc/meminfo

但 /proc/meminfo 只能看缓存大小,不能直接看命中率。


3️⃣ 使用 sar(需 sysstat)

sar -B 1

示例:

pgpgin/s pgpgout/s fault/s majflt/s pgfree/s
  • majflt/s 高 → 缓存未命中(需读磁盘)
  • fault/s 高 → 页面错误

二、磁盘 / 块设备缓存命中率

1️⃣ iostat

iostat -x 1

关注:

  • rrqm/s(读合并)
  • wrqm/s
  • await、svctm

⚠️ iostat 不显示缓存命中率,只能间接判断。


2️⃣ blktrace + blkparse(高级)

blktrace -d /dev/sda -o trace
blkparse trace

适合性能分析,但复杂。


三、CPU 缓存命中率(L1/L2/L3)

1️⃣ perf

perf stat -e cache-references,cache-misses ls

命中率:

cache-misses / cache-references

示例:

cache-references: 1,000,000
cache-misses:       50,000
命中率 ≈ 95%

四、应用缓存(如 Redis)

redis-cli info stats

关注:

keyspace_hits
keyspace_misses

命中率:

hits / (hits + misses)

五、快速判断缓存是否“够用”

free -h
              total        used        free      shared  buff/cache   available
Mem:           16Gi        3.2Gi        2.1Gi      200Mi       10Gi        12Gi
  • buff/cache 大
  • available 高
  • swap 没用
    ✅ 缓存命中率一般较好

总结(常用命令)

场景 命令
Page Cache cachestat 1
无工具 /proc/vmstat
CPU 缓存 perf stat
系统整体 sar -B
Redis redis-cli info

如果你能说明:

  • 是 系统、磁盘、CPU 还是某个应用
  • 内核版本 / 是否有 root 权限

我可以直接给你最合适的一条命令。

0 踩