C# 获取 CPU 缓存未命中性能计数器

V.B*_*.B. 5 .net c# cpu-cache

我知道 CPU 会计算所有 L1/2/3 缓存未命中,并且原则上可以访问此信息。例如,有一个来自英特尔的性能查看器。我只是在 C# 中找不到示例。可以从 .NET 访问这些数据吗?

Evk*_*Evk 5

您可以使用Intel Performance Counter Monitor来做到这一点(至少在 Windows 上)。除了与之捆绑在一起的其他工具之外,它还包含 PCM-Service - 添加 PCM Windows 性能计数器的 Windows 服务。下载、编译并安装此服务后,您可以访问 L2 缓存未命中(例如),就像这样简单:

var pc = new PerformanceCounter("PCM Core Counters", "L2 Cache Misses", "total_"); // instead of total_ you can use number of core
var value = pc.RawValue; // or pc.NextValue() and so on.
Run Code Online (Sandbox Code Playgroud)

当然,Intel PCM 添加了更多有趣的计数器,而不仅仅是缓存未命中,所有这些都可以从 .NET 访问。