一、Linux下查看CPU Cache級數,每級大小
dmesg | grep cache
實例結果如下:
二、查看Cache的關聯方式
在 /sys/devices/system/cpu/中查看相應的文件夾
如查看cpu0 的一級緩存中的有多少組,
$ cat /sys/devices/system/cpu/cpu0/cache/index0/number_of_sets
$64
如查看cpu0的一級緩存中一組中的行數
$cat /sys/devices/system/cpu/cpu0/cache/index0/ways_of_associativity
$8
三、查看cache_line的大小
上面以本人電腦的cpu一級緩存為例知道了cpu0的一級緩存的大小:32k,其包含64個(sets)組,每組有8(ways),則可以算出每一個way(cache_line)的大小 cache_line = 32*1024/(64*8)=64 bytes。當然我們也可以通過以下命令查出cache_line的大小
$ cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size
或者
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 60
model name : Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
stepping : 3
cpu MHz : 3497.664
cache size : 8192 KB
physical id : 0
siblings : 8
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 fma cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt aes xsave avx lahf_lm abm ida arat tpr_shadow vnmi flexpriority ept vpid
bogomips : 6995.32
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
知道了cpu的cache 信息,在編寫高效程序時將所有幫助。
四、程序內容在cache中的存儲方式
上圖來自於深入理解計算機系統,接着我們上面的分析:cpu0的一級高速緩存中有64組,則s =log2(64)=6,即“組索引“位為6位,同理“塊偏移” b=6位,則以我的64位系統為例,”標記位“t = 64 - s -b =64-6 -6=52 位。
同時我們可以進一步分析,根據上圖DRAM地址與高速緩存地址的映射關系,可知:第一個64bytes在第0組,第二個64bytes在第1組,第64個64bytes在第63組,直到第65個64bytes才用回到第0組與第一個64bytes競爭高速緩存,