C++ 使用Intel的PMU库分析缓存命中/未命中数

C++ 使用Intel的PMU库分析缓存命中/未命中数,c++,performance,caching,intel,C++,Performance,Caching,Intel,是否可以使用Intel的PMU库计算C程序中特定代码片段的缓存命中/未命中数?计数似乎受到系统上运行的其他应用程序的污染 该库是否支持单独隔离与特定代码片段对应的缓存统计信息,即不受系统上运行的其他应用程序的干扰 这是我一直在测试的代码片段 SystemCounterState before = getSystemCounterState(); SystemCounterState after = getSystemCounterState(); cout << "=======

是否可以使用Intel的PMU库计算C程序中特定代码片段的缓存命中/未命中数?计数似乎受到系统上运行的其他应用程序的污染

该库是否支持单独隔离与特定代码片段对应的缓存统计信息,即不受系统上运行的其他应用程序的干扰

这是我一直在测试的代码片段

SystemCounterState before = getSystemCounterState();

SystemCounterState after = getSystemCounterState();

cout << "===========================================================" << endl;
cout << "Instructions per Clock: " << getIPC(before, after) <<
    "\nL2 cache hits: " << getL2CacheHits(before, after) <<
    "\nL2 cache misses: " << getL2CacheMisses(before, after) <<
    "\nL2 cache hit ratio: " << getL2CacheHitRatio(before, after) <<
    "\nL3 cache hits: " << getL3CacheHits(before, after) <<
    "\nL3 cache misses: " << getL3CacheMisses(before, after) <<
    "\nL3 cache hit ratio: " << getL3CacheHitRatio(before, after) <<
    "\nWasted cycles caused by L3 misses: " << getCyclesLostDueL3CacheMisses(before, after) <<
    "\nBytes read from DRAM: " << getBytesReadFromMC(before, after) << endl;
cout << "===========================================================" << endl;

提前谢谢。

只是打印,根本不计算,实际上是在做计算

你调用C++例程“CUT”,这会导致执行相当多的代码。如果要查看此内容,请编译此程序:

#include <iostream>

using namespace std;

int main()
{
    int i;

    i = 1;
    cout << "Hello World" << endl;
    i = 2;
}
使用gdb,在cout上设置断点,然后执行“stepi”命令。您将看到在执行“cout”时执行了多少条指令

所有这些执行指令的指令都会访问内存,包括指令本身和指令使用的数据,这可能会导致相当多的缓存未命中


您可能想尝试在不进行任何打印的情况下抓取计数器。

当然。当然很抱歉给你带来了困惑。我也尝试过没有“cout”部分,但仍然有非零缓存统计数据。结果只是粘贴了错误的代码迭代。上面已经对其进行了编辑。您好,您看到的计数可能是由于执行了性能监视代码本身,因为代码和数据不在缓存中。尝试执行前后代码两次。
#include <iostream>

using namespace std;

int main()
{
    int i;

    i = 1;
    cout << "Hello World" << endl;
    i = 2;
}