gpt4 book ai didi

caching - 如何找到 x86 处理器的 cpu 缓存大小

转载 作者:行者123 更新时间:2023-12-04 00:48:30 24 4
gpt4 key购买 nike

我想使用 x86 汇编语言查找 L1 或 L2 缓存的 cpu 缓存大小。我听说 cpuid 和 MSR 寄存器有系统特定的数据。请问有人可以帮我怎样才能得到尺寸。

最佳答案

这是一个简单的例子,说明如何使用 CPUID 指令找出它:

#include <stdio.h>
#include <limits.h>

#define cpuid(id) __asm__( "cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(id), "b"(0), "c"(0), "d"(0))
#define b(val, base, end) ((val << (__WORDSIZE-end-1)) >> (__WORDSIZE-end+base-1))

int main(int argc, char **argv)
{
unsigned long eax, ebx, ecx, edx;

cpuid(0);
printf("identification: \"%.4s%.4s%.4s\"\n", (char *)&ebx, (char *)&edx, (char *)&ecx);

printf("cpu information:\n");

cpuid(1);
printf(" family %ld model %ld stepping %ld efamily %ld emodel %ld\n",
b(eax, 8, 11), b(eax, 4, 7), b(eax, 0, 3), b(eax, 20, 27), b(eax, 16, 19));
printf(" brand %ld cflush sz %ld*8 nproc %ld apicid %ld\n",
b(ebx, 0, 7), b(ebx, 8, 15), b(ebx, 16, 23), b(ebx, 24, 31));

cpuid(0x80000006);
printf("L1 cache size (per core): %ld KB\n", b(ecx, 16, 31));

return(0);
}

关于caching - 如何找到 x86 处理器的 cpu 缓存大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3818410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com