【问题标题】:Using Intel's PMU library to profile the number of cache hits/misses使用 Intel 的 PMU 库来分析缓存命中/未命中的数量
【发布时间】:2014-12-02 08:43:07
【问题描述】:

是否可以使用英特尔的 PMU 库来计算 C 程序中特定 sn-p 代码的缓存命中/未命中次数?计数似乎被系统上运行的其他应用程序污染了。

该库是否支持单独隔离对应于特定代码 sn-p 的缓存统计信息(即,不受系统上运行的其他应用程序的干扰)?

这是我一直在测试的代码的 sn-p

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;

这些是我得到的统计数据(请注意,虽然我不进行任何计算,但缓存命中/未命中计数很高):

===========================================================
Instructions per Clock: 0.410805
L2 cache hits: 2677
L2 cache misses: 2658
L2 cache hit ratio: 0.501781
L3 cache hits: 2151
L3 cache misses: 507
L3 cache hit ratio: 0.809255
Wasted cycles caused by L3 misses: 0.0242752
Bytes read from DRAM: 514048
===========================================================

提前致谢。

【问题讨论】:

    标签: c++ performance caching intel


    【解决方案1】:

    仅仅打印“Printing no calculation at all”实际上就是在进行计算。

    您正在调用 C++ 例程“cout”,这会导致执行相当多的代码。如果你想看这个,编译这个程序:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i;
    
        i = 1;
        cout << "Hello World" << endl;
        i = 2;
    }
    

    使用 gdb,在 cout 上设置断点,然后执行“stepi”命令。您将看到执行“cout”时执行了多少条指令。

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

    您可能想尝试抓取计数器而不进行任何打印。

    【讨论】:

    • 当然。当然。对困惑感到抱歉。我也尝试过不使用“cout”部分,但缓存统计数据仍然不为零。刚刚粘贴了错误的代码迭代。上面已经编辑过了。
    • 您好,您看到的计数可能是由于性能监控代码本身的执行,因为代码和数据不在缓存中。尝试执行两次之前和之后的代码。
    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多