Linux系统下如何获取CPU缓存路数(ways)与组数(sets)?
Linux下查询CPU缓存路数(ways)和组数(sets)的方法
在Linux系统里,除了lscpu,还有几种可靠的方法能获取缓存的路数和组数:
1. 使用cpuid工具
cpuid直接读取CPU的CPUID指令输出,能拿到最底层的缓存参数。
- 先安装工具:
Debian/Ubuntu系:sudo apt install cpuid
RHEL/CentOS系:sudo yum install cpuid - 运行命令查看缓存详情:
cpuid -1 | grep -A 10 "Cache"
输出里的ways associativity对应路数,number of sets对应组数。如果要精准过滤,也可以用:cpuid -r | grep -E "(cache_ways|cache_sets)"
2. 读取sysfs文件系统
Linux的sysfs提供了硬件的详细属性文件,缓存信息存在/sys/devices/system/cpu/cpu0/cache/路径下:
- 每个缓存层级对应一个
index*目录(比如index0是L1数据缓存,index1是L1指令缓存,index2是L2,index3是L3,可通过cat /sys/devices/system/cpu/cpu0/cache/index*/type确认类型) - 查看路数:
cat /sys/devices/system/cpu/cpu0/cache/index3/ways_of_associativity - 查看组数:
cat /sys/devices/system/cpu/cpu0/cache/index3/number_of_sets
把index3换成对应的层级目录,就能获取对应缓存的参数。
3. 使用hwloc工具
hwloc是硬件拓扑查询工具,能直观展示缓存的关联属性:
- 安装工具:
Debian/Ubuntu系:sudo apt install hwloc
RHEL/CentOS系:sudo yum install hwloc - 命令行模式查看:
lstopo-no-graphics | grep -E "Associativity|Sets"
输出里会明确标注每个缓存层级的路数(Associativity)和组数(Sets)。
内容的提问来源于stack exchange,提问作者xdxdxd
相关产品推荐
相关产品推荐

