性能统计与性能记录

Perf Stat vs Perf Record

我对 perf recordperf stat 在计算页面错误、缓存未命中和 perf list 中的任何其他事件时的区别感到困惑。我在 "Question 1" 的答案下面有 2 个问题也可能有助于回答 "Question 2" 但我明确地把它们写出来以防它没有。

问题一: 据我了解,perf stat 获得 "summary" 计数,但当与 -I 选项一起使用时,会在指定的毫秒间隔内获得计数。使用此选项,它是对间隔内的计数求和还是获取间隔内的平均值,或者完全是其他东西?我假设它被总结了。 perf wiki 表示它是聚合的,但我想这可能意味着两者之一。

问题二: 为什么 perf stat -e <event1> -I 1000 sleep 5 给出的计数与我对以下命令 perf record -e <event1> -F 1000 sleep 5 每秒的计数求和相同?

例如,如果我使用 "page-faults" 作为 event1 的事件,我会得到以下输出,我在每个命令下列出了下面的输出。 (我假设期间字段是 perf record 的 perf.data 文件中事件的计数)

性能统计

    perf stat -e page-faults -I 1000 sleep 5
    #           time             counts unit events
         1.000252928                 54      page-faults                                                 
         2.000498389      <not counted>      page-faults                                                 
         3.000569957      <not counted>      page-faults                                                 
         4.000659987      <not counted>      page-faults                                                 
         5.000837864                  2      page-faults

性能记录

    perf record -e page-faults -F 1000 sleep 5
    [ perf record: Woken up 1 times to write data ]
    [ perf record: Captured and wrote 0.016 MB perf.data (6 samples) ]
    perf script -F period
             1
             1
             1
             5
            38
           164

我预计,如果我对 perf stat 的计数求和,我会得到与 perf record 的总和相同的结果。如果我将 -c 选项与 perf record 一起使用并给出参数 1,我会得到一个接近的匹配。这是否只是巧合,因为页面错误的数量相对较少?

到目前为止我使用的参考资料:

提前感谢您提供的所有见解。

首先,您使用 sleeppage-faults 的测试用例不是理想的测试用例。在休眠期间不应该有页面错误事件,你真的不能期待任何有趣的事情。为了更容易推理,我建议使用 ref-cycles(硬件)事件和繁忙的工作负载,例如 awk 'BEGIN { while(1){} }'.

Question 1: It is my understanding that perf stat gets a "summary" of counts but when used with the -I option gets the counts at the specified millisecond interval. With this option does it sum up the counts over the interval or get the average over the interval, or something else entirely? I assume it is summed up.

是的。这些值只是总结出来的。您可以通过测试确认:

$ perf stat -e ref-cycles -I 1000 timeout 10s awk 'BEGIN { while(1){} }'
#           time             counts unit events
 1.000105072      2,563,666,664      ref-cycles                                                  
 2.000267991      2,577,462,550      ref-cycles                                                  
 3.000415395      2,577,211,936      ref-cycles                                                  
 4.000543311      2,577,240,458      ref-cycles                                                  
 5.000702131      2,577,525,002      ref-cycles                                                  
 6.000857663      2,577,156,088      ref-cycles                                                  

[ ... snip ... ]
[ Note that it may not be as nicely consistent on all systems due dynamic frequency scaling ]

$ perf stat -e ref-cycles -I 3000 timeout 10s awk 'BEGIN { while(1){} }' 
#           time             counts unit events
 3.000107921      7,736,108,718      ref-cycles                                                  
 6.000265186      7,732,065,900      ref-cycles                                                  
 9.000372029      7,728,302,192      ref-cycles     

Question 2: Why doesn't perf stat -e <event1> -I 1000 sleep 5 give about the same counts as if I summed up the counts over each second for the following command perf record -e <event1> -F 1000 sleep 5?

perf stat -I毫秒,而perf record -FHZ(1/s),所以perf stat -I 1000对应的命令是perf record -F 1。事实上,我们的 event/workload 更稳定,这看起来更好:

$ perf stat -e ref-cycles -I 1000 timeout 10s awk 'BEGIN { while(1){} }'
#           time             counts unit events
 1.000089518      2,578,694,534      ref-cycles                                                  
 2.000203872      2,579,866,250      ref-cycles                                                  
 3.000294300      2,579,857,852      ref-cycles                                                  
 4.000390273      2,579,964,842      ref-cycles                                                  
 5.000488375      2,577,955,536      ref-cycles                                                  
 6.000587028      2,577,176,316      ref-cycles                                                  
 7.000688250      2,577,334,786      ref-cycles                                                  
 8.000785388      2,577,581,500      ref-cycles                                                  
 9.000876466      2,577,511,326      ref-cycles                                                  
10.000977965      2,577,344,692      ref-cycles                                                  
10.001195845            466,674      ref-cycles    

$ perf record -e ref-cycles -F 1 timeout 10s awk 'BEGIN { while(1){} }'
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.008 MB perf.data (17 samples) ]

$ perf script -F time,period        
3369070.273722:          1 
3369070.273755:          1 
3369070.273911:       3757 
3369070.273916:    3015133 
3369070.274486:          1 
3369070.274556:          1 
3369070.274657:       1778 
3369070.274662:    2196921 
3369070.275523: 47192985748 
3369072.663696: 2578692405 
3369073.663547: 2579122382 
3369074.663609: 2580015300 
3369075.664085: 2579873741 
3369076.664433: 2578638211 
3369077.664379: 2578378119 
3369078.664175: 2578166440 
3369079.663896: 2579238122 

所以你看,最终 perf record -F 的结果也是稳定的。不幸的是 perf record 的文档很差。您可以通过查看底层系统调用 man perf_event_open:

的文档来了解设置 -c-F 的含义

sample_period, sample_freq A "sampling" event is one that generates an overflow notification every N events, where N is given by sample_period. A sampling event has sample_period > 0. When an overflow occurs, requested data is recorded in the mmap buffer. The sample_type field controls what data is recorded on each overflow.

sample_freq can be used if you wish to use frequency rather than period. In this case, you set the freq flag. The kernel will adjust the sampling period to try and achieve the desired rate. The rate of adjustment is a timer tick.

因此,perf stat 使用内部计时器每 -i 毫秒读取一次计数器的值,perf record 设置事件溢出计数器每 -c 事件。这意味着它会在每个 N 事件(例如每个 N page-faultcycles 事件)中采样。使用 -F,它会尝试调节此溢出值以达到所需的频率。它尝试不同的值并相应地调整它 up/down。这最终适用于具有稳定速率的计数器,但对于动态事件会得到不稳定的结果。