Why does Perf and Papi give different values for L3 cache references and misses?

前端 未结 1 968
一向
一向 2021-02-02 15:36

I am working on a project where we have to implement an algorithm that is proven in theory to be cache friendly. In simple terms, if N is the input and B

相关标签:
1条回答
  • 2021-02-02 16:27

    You can go through the source files of both perf and PAPI to find out to which performance counter they actually map these events, but it turns out they are the same (assuming Intel Core i here): Event 2E with umask 4F for references and 41 for misses. In the the Intel 64 and IA-32 Architectures Developer's Manual these events are described as:

    2EH 4FH LONGEST_LAT_CACHE.REFERENCE This event counts requests originating from the core that reference a cache line in the last level cache.

    2EH 41H LONGEST_LAT_CACHE.MISS This event counts each cache miss condition for references to the last level cache.

    That seems to be ok. So the problem is somewhere else.

    Here are my reproduced numbers, only that I increased the array length by a factor of 100. (I noticed large fluctuations in timing results otherwise and with length of 1,000,000 the array almost fits into your L3 cache still). main1 here is your first code example without PAPI and main2 your second one with PAPI.

    $ perf stat -e cache-references,cache-misses ./main1 
    
     Performance counter stats for './main1':
    
            27.148.932      cache-references                                            
            22.233.713      cache-misses              #   81,895 % of all cache refs 
    
           0,885166681 seconds time elapsed
    
    $ ./main2 
    L3 accesses: 7084911
    L3 misses: 2750883
    L3 miss/access ratio: 0.388273
    

    These obviously don't match. Let's see where we actually count the LLC references. Here are the first few lines of perf report after perf record -e cache-references ./main1:

      31,22%  main1    [kernel]          [k] 0xffffffff813fdd87                                                                                                                                   ▒
      16,79%  main1    main1             [.] main                                                                                                                                                 ▒
       6,22%  main1    [kernel]          [k] 0xffffffff8182dd24                                                                                                                                   ▒
       5,72%  main1    [kernel]          [k] 0xffffffff811b541d                                                                                                                                   ▒
       3,11%  main1    [kernel]          [k] 0xffffffff811947e9                                                                                                                                   ▒
       1,53%  main1    [kernel]          [k] 0xffffffff811b5454                                                                                                                                   ▒
       1,28%  main1    [kernel]          [k] 0xffffffff811b638a                                              
       1,24%  main1    [kernel]          [k] 0xffffffff811b6381                                                                                                                                   ▒
       1,20%  main1    [kernel]          [k] 0xffffffff811b5417                                                                                                                                   ▒
       1,20%  main1    [kernel]          [k] 0xffffffff811947c9                                                                                                                                   ▒
       1,07%  main1    [kernel]          [k] 0xffffffff811947ab                                                                                                                                   ▒
       0,96%  main1    [kernel]          [k] 0xffffffff81194799                                                                                                                                   ▒
       0,87%  main1    [kernel]          [k] 0xffffffff811947dc   
    

    So what you can see here is that actually only 16.79% of the cache references actually happen in user space, the rest are due to the kernel.

    And here lies the problem. Comparing this to the PAPI result is unfair, because PAPI by default only counts user space events. Perf however by default collects user and kernel space events.

    For perf we can easily reduce to user space collection only:

    $ perf stat -e cache-references:u,cache-misses:u ./main1 
    
     Performance counter stats for './main1':
    
             7.170.190      cache-references:u                                          
             2.764.248      cache-misses:u            #   38,552 % of all cache refs    
    
           0,658690600 seconds time elapsed
    

    These seem to match pretty well.

    Edit:

    Lets look a bit closer at what the kernel does, this time with debug symbols and cache misses instead of references:

      59,64%  main1    [kernel]       [k] clear_page_c_e
      23,25%  main1    main1          [.] main
       2,71%  main1    [kernel]       [k] compaction_alloc
       2,70%  main1    [kernel]       [k] pageblock_pfn_to_page
       2,38%  main1    [kernel]       [k] get_pfnblock_flags_mask
       1,57%  main1    [kernel]       [k] _raw_spin_lock
       1,23%  main1    [kernel]       [k] clear_huge_page
       1,00%  main1    [kernel]       [k] get_page_from_freelist
       0,89%  main1    [kernel]       [k] free_pages_prepare
    

    As we can see most cache misses actually happen in clear_page_c_e. This is called when a new page is accessed by our program. As explained in the comments new pages are zeroed by the kernel before allowing access, therefore the cache miss already happens here.

    This messes with your analysis, because a good part of the cache misses you expect happen in kernel space. However you can not guarantee under which exact circumstances the kernel actually accesses memory, so that might be deviations from the behavior expected by your code.

    To avoid this build an additional loop around your array-filling one. Only the first iteration of the inner loop incurs the kernel overhead. As soon as every page in the array was accessed, there should be no contribution left. Here is my result for 100 repetition of the outer loop:

    $ perf stat -e cache-references:u,cache-references:k,cache-misses:u,cache-misses:k ./main1
    
     Performance counter stats for './main1':
    
         1.327.599.357      cache-references:u                                          
            23.678.135      cache-references:k                                          
         1.242.836.730      cache-misses:u            #   93,615 % of all cache refs    
            22.572.764      cache-misses:k            #   95,332 % of all cache refs    
    
          38,286354681 seconds time elapsed
    

    The array length was 100,000,000 with 100 iterations and therefore you would have expected 1,250,000,000 cache misses by your analysis. This is pretty close now. The deviation is mostly from the first loop which is loaded to the cache by the kernel during page clearing.

    With PAPI a few extra warm-up loops can be inserted before the counter starts, and so the result fits the expectation even better:

    $ ./main2 
    L3 accesses: 1318699729
    L3 misses: 1250684880
    L3 miss/access ratio: 0.948423
    
    0 讨论(0)
提交回复
热议问题