L2キャッシュメモリとキャッシュラインサイズを取得する。
#include <stdio.h> void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){ __asm__ __volatile__ ("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) : "cc"); } unsigned long get_cache_size(void) { int eax, ebx, ecx, edx; int size = 0; cpuid(0x80000000, &eax, &ebx, &ecx, &edx); if ((unsigned int)eax >= 0x80000006) { cpuid(0x80000006, &eax, &ebx, &ecx, &edx); size = (ecx >> 16) & 0xffff; } return size * 1024; } unsigned long get_cache_linesize(void) { int eax, ebx, ecx, edx; int linesize = 0;
cpuid(0x80000000, &eax, &ebx, &ecx, &edx); if ((unsigned int)eax >= 0x80000006) { cpuid(0x80000006, &eax, &ebx, &ecx, &edx); linesize = (ecx >> 0) & 0xff; } return linesize; } int main(void) { printf("L2 cache\t: %ld KB\n", get_cache_size() / 1024); printf("L2 line cache\t: %ld B\n", get_cache_linesize()); return 0; }