インテル マニュアル Vol. 2 は、キャッシュ サイズを計算する次の式を指定します。
このキャッシュ サイズ (バイト単位)
= (ウェイ + 1) * (パーティション + 1) * (Line_Size + 1) * (セット + 1)
= (EBX[31:22] + 1) * (EBX[21:12] + 1) * (EBX[11:0] + 1) * (ECX + 1)
、、およびはWays
、set toを使用して照会されます。Partitions
Line_Size
Sets
cpuid
eax
0x04
ヘッダー ファイル宣言の提供
x86_cache_size.h
:
unsigned int get_cache_line_size(unsigned int cache_level);
実装は次のようになります。
;1st argument - the cache level
get_cache_line_size:
push rbx
;set line number argument to be used with CPUID instruction
mov ecx, edi
;set cpuid initial value
mov eax, 0x04
cpuid
;cache line size
mov eax, ebx
and eax, 0x7ff
inc eax
;partitions
shr ebx, 12
mov edx, ebx
and edx, 0x1ff
inc edx
mul edx
;ways of associativity
shr ebx, 10
mov edx, ebx
and edx, 0x1ff
inc edx
mul edx
;number of sets
inc ecx
mul ecx
pop rbx
ret
私のマシンでは次のように動作します:
#include "x86_cache_size.h"
int main(void){
unsigned int L1_cache_size = get_cache_line_size(1);
unsigned int L2_cache_size = get_cache_line_size(2);
unsigned int L3_cache_size = get_cache_line_size(3);
//L1 size = 32768, L2 size = 262144, L3 size = 8388608
printf("L1 size = %u, L2 size = %u, L3 size = %u\n", L1_cache_size, L2_cache_size, L3_cache_size);
}