sysfs
for d in /sys/devices/system/cpu/cpu0/cache/index*;
do tail -c+1 $d/{level,type,size}
echo
done
Dá:
==> /sys/devices/system/cpu/cpu0/cache/index0/level <==
1
==> /sys/devices/system/cpu/cpu0/cache/index0/type <==
Data
==> /sys/devices/system/cpu/cpu0/cache/index0/size <==
32K
==> /sys/devices/system/cpu/cpu0/cache/index1/level <==
1
==> /sys/devices/system/cpu/cpu0/cache/index1/type <==
Instruction
==> /sys/devices/system/cpu/cpu0/cache/index1/size <==
32K
==> /sys/devices/system/cpu/cpu0/cache/index2/level <==
2
==> /sys/devices/system/cpu/cpu0/cache/index2/type <==
Unified
==> /sys/devices/system/cpu/cpu0/cache/index2/size <==
256K
==> /sys/devices/system/cpu/cpu0/cache/index3/level <==
3
==> /sys/devices/system/cpu/cpu0/cache/index3/type <==
Unified
==> /sys/devices/system/cpu/cpu0/cache/index3/size <==
8192K
getconf
getconf -a | grep CACHE
dá:
LEVEL1_ICACHE_SIZE 32768
LEVEL1_ICACHE_ASSOC 8
LEVEL1_ICACHE_LINESIZE 64
LEVEL1_DCACHE_SIZE 32768
LEVEL1_DCACHE_ASSOC 8
LEVEL1_DCACHE_LINESIZE 64
LEVEL2_CACHE_SIZE 262144
LEVEL2_CACHE_ASSOC 8
LEVEL2_CACHE_LINESIZE 64
LEVEL3_CACHE_SIZE 20971520
LEVEL3_CACHE_ASSOC 20
LEVEL3_CACHE_LINESIZE 64
LEVEL4_CACHE_SIZE 0
LEVEL4_CACHE_ASSOC 0
LEVEL4_CACHE_LINESIZE 0
Ou para um único nível:
getconf LEVEL2_CACHE_SIZE
O legal dessa interface é que ela é apenas um wrapper em torno da função POSIX
sysconf
C (argumentos de cache são extensões não POSIX) e, portanto, ela também pode ser usada a partir do código C.
Testado no Ubuntu 16.04.