Perf -- Linux下的系统性能调优工具介绍

Linux perf:系统性能监控和分析工具 Linuxperf是 Linux 2.6+ 后内置于内核源码树中的性能剖析(profiling)工具,它基于事件采样,以性能事件为基础,针对 CPU 相关性能指标与操作系统相关性能指标进行性能剖析,可用于性能瓶颈查找与热点代码的定位。包含在 Linux 内核tools/perf下,通过命令perf实现一组子命令:stat , record , report ,[…] 阅读详情

Perf简介

Perf是Linux kernel自带的系统性能优化工具。虽然它的版本还只是0.0.2,Perf已经显现出它强大的实力,足以与目前Linux流行的OProfile相媲美了。

Perf的优势在于与Linux Kernel的紧密结合,它可以最先应用到加入Kernel的new feature。而像OProfile, GProf等通常会“慢一拍”。Perf的基本原理跟OProfile等类似,也是在CPU的PMU registers中Get/Set performance counters来获得诸如instructions executed, cache-missed suffered, branches mispredicted等信息。Linux kernel对这些registers进行了一系列抽象,所以你可以按进程,按CPU或者按counter group等不同类别来查看Sample信息。

使用Perf

Perf的使用流程和OProfile很像。所以如果你会用OProfile的话,用Perf就很简单。这里只是简单翻译一下在[1]中的Perf examples中举的例子。有更多发现的话以后会继续更新。

$ perf record -f -- git gc   Counting objects: 1283571, done.
Compressing objects: 100% (206724/206724), done.
Writing objects: 100% (1283571/1283571), done.
Total 1283571 (delta 1070675), reused 1281443 (delta 1068566)
[ perf record: Captured and wrote 31.054 MB perf.data (~1356768 samples) ]
 
$ perf report --sort comm,dso,symbol | head -10
# Samples: 1355726
#
# Overhead          Command                            Shared Object  Symbol
# ........  ...............  .......................................  ......
#
    31.53%              git  /usr/bin/git                             [.] 0x0000000009804f
    13.41%        git-prune  /usr/bin/git-prune                       [.] 0x000000000ad06d
    10.05%              git  /lib/tls/i686/cmov/libc-2.8.90.so        [.] _nl_make_l10nflist
     5.36%        git-prune  /usr/lib/libz.so.1.2.3.3                 [.] 0x00000000009d51
     4.48%              git  /lib/tls/i686/cmov/libc-2.8.90.so        [.] memcpy

perf record相当于opcontrol –-start, 而perf report相当于opreport.

Perf用例

查看所有可用的counters用'perf list’:

titan:~> perf list
 [...]
 kmem:kmalloc                             [Tracepoint event]
 kmem:kmem_cache_alloc                    [Tracepoint event]
 kmem:kmalloc_node                        [Tracepoint event]
 kmem:kmem_cache_alloc_node               [Tracepoint event]
 kmem:kfree                               [Tracepoint event]
 kmem:kmem_cache_free                     [Tracepoint event]
 kmem:mm_page_free_direct                 [Tracepoint event]
 kmem:mm_pagevec_free                     [Tracepoint event]
 kmem:mm_page_alloc                       [Tracepoint event]
 kmem:mm_page_alloc_zone_locked           [Tracepoint event]
 kmem:mm_page_pcpu_drain                  [Tracepoint event]
 kmem:mm_page_alloc_extfrag               [Tracepoint event]
你可以用以上counter的任意组合来跑你的测试程序。比如,用以下命令来看跑
hackbench时page alloc/free的次数。
titan:~> perf stat -e kmem:mm_page_pcpu_drain -e kmem:mm_page_alloc 
-e kmem:mm_pagevec_free -e kmem:mm_page_free_direct ./hackbench 10
Time: 0.575
Performance counter stats for './hackbench 10':
         13857  kmem:mm_page_pcpu_drain 
         27576  kmem:mm_page_alloc      
          6025  kmem:mm_pagevec_free    
         20934  kmem:mm_page_free_direct
   0.613972165  seconds time elapsed
Perf可以帮你统计N次结果的数值波动情况:
titan:~> perf stat --repeat 5 -e kmem:mm_page_pcpu_drain -e 
  kmem:mm_page_alloc -e kmem:mm_pagevec_free -e 
  kmem:mm_page_free_direct ./hackbench 10
Time: 0.627
Time: 0.644
Time: 0.564
Time: 0.559
Time: 0.626
Performance counter stats for './hackbench 10' (5 runs):
         12920  kmem:mm_page_pcpu_drain    ( +-   3.359% )
         25035  kmem:mm_page_alloc         ( +-   3.783% )
          6104  kmem:mm_pagevec_free       ( +-   0.934% )
         18376  kmem:mm_page_free_direct   ( +-   4.941% )
   0.643954516  seconds time elapsed   ( +-   2.363% )
有了以上的统计数据,你可以开始sample某一个你关心的tracepoint(比如page
allocations):
titan:~/git> perf record -f -e kmem:mm_page_alloc -c 1 ./git gc
Counting objects: 1148, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (450/450), done.
Writing objects: 100% (1148/1148), done.
Total 1148 (delta 690), reused 1148 (delta 690)
[ perf record: Captured and wrote 0.267 MB perf.data (~11679 samples) ]
查看哪个function引起了page allocations:
titan:~/git> perf report
# Samples: 10646
#
# Overhead          Command               Shared Object
# ........  ...............  ..........................
#
   23.57%       git-repack  /lib64/libc-2.5.so        
   21.81%              git  /lib64/libc-2.5.so        
   14.59%              git  ./git                     
   11.79%       git-repack  ./git                     
    7.12%              git  /lib64/ld-2.5.so          
    3.16%       git-repack  /lib64/libpthread-2.5.so  
    2.09%       git-repack  /bin/bash                 
    1.97%               rm  /lib64/libc-2.5.so        
    1.39%               mv  /lib64/ld-2.5.so          
    1.37%               mv  /lib64/libc-2.5.so        
    1.12%       git-repack  /lib64/ld-2.5.so          
    0.95%               rm  /lib64/ld-2.5.so          
    0.90%  git-update-serv  /lib64/libc-2.5.so        
    0.73%  git-update-serv  /lib64/ld-2.5.so          
    0.68%             perf  /lib64/libpthread-2.5.so  
    0.64%       git-repack  /usr/lib64/libz.so.1.2.3  

更进一步的查看:

titan:~/git> perf report --sort comm,dso,symbol

# Samples: 10646
#
# Overhead          Command               Shared Object  Symbol
# ........  ...............  ..........................  ......
#
    9.35%       git-repack  ./git                       [.] insert_obj_hash
    9.12%              git  ./git                       [.] insert_obj_hash
    7.31%              git  /lib64/libc-2.5.so          [.] memcpy
    6.34%       git-repack  /lib64/libc-2.5.so          [.] _int_malloc
    6.24%       git-repack  /lib64/libc-2.5.so          [.] memcpy
    5.82%       git-repack  /lib64/libc-2.5.so          [.] __GI___fork
    5.47%              git  /lib64/libc-2.5.so          [.] _int_malloc
    2.99%              git  /lib64/libc-2.5.so          [.] memset

 
同时,call-graph(函数调用图)也可以被记录下来,并且能告诉你每个函数所占用的百分比。
titan:~/git> perf record -f -g -e kmem:mm_page_alloc -c 1 ./git gc
Counting objects: 1148, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (450/450), done.
Writing objects: 100% (1148/1148), done.
Total 1148 (delta 690), reused 1148 (delta 690)
[ perf record: Captured and wrote 0.963 MB perf.data (~42069 samples) ]
titan:~/git> perf report -g
# Samples: 10686
#
# Overhead          Command               Shared Object
# ........  ...............  ..........................
#
   23.25%       git-repack  /lib64/libc-2.5.so        
               |          
               |--50.00%-- _int_free
               |          
               |--37.50%-- __GI___fork
               |          make_child
               |          
               |--12.50%-- ptmalloc_unlock_all2
               |          make_child
               |          
                --6.25%-- __GI_strcpy
   21.61%              git  /lib64/libc-2.5.so        
               |          
               |--30.00%-- __GI_read
               |          |          
               |           --83.33%-- git_config_from_file
               |                     git_config
               |                     |          
  [...]
用以下命令可以查看整个系统10秒内的page allocation次数:

titan:~/git> perf stat -a -e kmem:mm_page_pcpu_drain -e kmem:mm_page_alloc -e kmem:mm_pagevec_free -e kmem:mm_page_free_direct sleep 10

Performance counter stats for 'sleep 10':
        171585  kmem:mm_page_pcpu_drain 
        322114  kmem:mm_page_alloc      
         73623  kmem:mm_pagevec_free    
        254115  kmem:mm_page_free_direct
  10.000591410  seconds time elapsed

用以下命令查看每隔1秒,系统page allocation的波动状况:

titan:~/git> perf stat --repeat 10 -a -e kmem:mm_page_pcpu_drain -e 
  kmem:mm_page_alloc -e kmem:mm_pagevec_free -e 
  kmem:mm_page_free_direct sleep 1
Performance counter stats for 'sleep 1' (10 runs):
         17254  kmem:mm_page_pcpu_drain    ( +-   3.709% )
         34394  kmem:mm_page_alloc         ( +-   4.617% )
          7509  kmem:mm_pagevec_free       ( +-   4.820% )
         25653  kmem:mm_page_free_direct   ( +-   3.672% )
   1.058135029  seconds time elapsed   ( +-   3.089% )

通过反汇编往往能找出是哪行代码生成的指令会引起问题。

titan:~/git> perf annotate __GI___fork
------------------------------------------------
 Percent |      Source code & Disassembly of libc-2.5.so
------------------------------------------------  :  :  :      Disassembly of section .plt:  :      Disassembly of section .text:  :  :      00000031a2e95560 <__fork>:
[...]
    0.00 :        31a2e95602:   b8 38 00 00 00          mov    $0x38,%eax
    0.00 :        31a2e95607:   0f 05                   syscall 
   83.42 :        31a2e95609:   48 3d 00 f0 ff ff       cmp    $0xfffffffffffff000,%rax
    0.00 :        31a2e9560f:   0f 87 4d 01 00 00       ja     31a2e95762 <__fork+0x202>
    0.00 :        31a2e95615:   85 c0                   test  %eax,%eax
以上结果显示__GI__forks的83.42%的时间来源于0x38的系统调用。

值得优化某个特定的函数吗?

你也许想知道是否值得去优化你程序中的某个特定函数。一个很好的例子是git mailing list中讨论的关于SHA1 哈希算法优化的问题,我们可以用perf来预判优化的结果。具体参见Linus的回信[2].

"perf report --sort comm,dso,symbol" profiling shows the following for 
'git fsck --full' on the kernel repo, using the Mozilla SHA1:
   47.69%               git  /home/torvalds/git/git     [.] moz_SHA1_Update
   22.98%               git  /lib64/libz.so.1.2.3       [.] inflate_fast
    7.32%               git  /lib64/libc-2.10.1.so      [.] __GI_memcpy
    4.66%               git  /lib64/libz.so.1.2.3       [.] inflate
    3.76%               git  /lib64/libz.so.1.2.3       [.] adler32
    2.86%               git  /lib64/libz.so.1.2.3       [.] inflate_table
    2.41%               git  /home/torvalds/git/git     [.] lookup_object
    1.31%               git  /lib64/libc-2.10.1.so      [.] _int_malloc
    0.84%               git  /home/torvalds/git/git     [.] patch_delta
    0.78%               git  [kernel]                   [k] hpet_next_event

很明显,SHA1加密算法的性能在这里很关键。

如何测量latency

如果你在build kernel时enabled了

 CONFIG_PERF_COUNTER=y 
 CONFIG_EVENT_TRACING=y

那你可以加-tip参数来使用几个新的performance counter来测量scheduler的lantencies。

perf stat -e sched:sched_stat_wait -e task-clock ./hackbench 20

以上命令能够得出等待CPU用了多少时间。你可以重复10次这样的操作:

aldebaran:/home/mingo> perf stat --repeat 10 -e /
             sched:sched_stat_wait:r -e task-clock ./hackbench 20
Time: 0.251
Time: 0.214
Time: 0.254
Time: 0.278
Time: 0.245
Time: 0.308
Time: 0.242
Time: 0.222
Time: 0.268
Time: 0.244
Performance counter stats for './hackbench 20' (10 runs):
         59826  sched:sched_stat_wait    #      0.026 M/sec   ( +-   5.540% )
   2280.099643  task-clock-msecs         #      7.525 CPUs    ( +-   1.620% )
   0.303013390  seconds time elapsed   ( +-   3.189% )
读取scheduling的events counter
# perf list 2>&1 | grep sched:
 sched:sched_kthread_stop                   [Tracepoint event]
 sched:sched_kthread_stop_ret               [Tracepoint event]
 sched:sched_wait_task                      [Tracepoint event]
 sched:sched_wakeup                         [Tracepoint event]
 sched:sched_wakeup_new                     [Tracepoint event]
 sched:sched_switch                         [Tracepoint event]
 sched:sched_migrate_task                   [Tracepoint event]
 sched:sched_process_free                   [Tracepoint event]
 sched:sched_process_exit                   [Tracepoint event]
 sched:sched_process_wait                   [Tracepoint event]
 sched:sched_process_fork                   [Tracepoint event]
 sched:sched_signal_send                    [Tracepoint event]
 sched:sched_stat_wait                      [Tracepoint event]
 sched:sched_stat_sleep                     [Tracepoint event]
 sched:sched_stat_iowait                    [Tracepoint event]

对于latency analysis而言,stat_wait/sleep/iowait是值得注意的event。如果你想看所有delays和它们的mix/max/avg,你可以:

 perf record -e sched:sched_stat_wait:r -f -R -c 1 ./hackbench 20
 perf trace

Reference:

1. https://perf.wiki.kernel.org/index.php/Main_Page

2. http://thread.gmane.org/gmane.comp.version-control.git/124745

深入浅析Linux Perf 性能分析工具及火焰图 本文首先介绍了 Linux Perf Event 子系统的整体架构,接着为读者展示了 Perf 的两种工作模式及各种类别的 Perf Events,之后详细介绍了 Perf Tool 的多种子工具集,最后为读者展示了 Bredan Gregg 先生引入的各种火焰图及其绘制原理。可用于性能瓶颈的查找与热点代码的定位。CPU 火焰图反映了一段时间内用户程序在 CPU 上运行的热点,其绘制原理是对 Perf 采集到的 samples 进行解析,对函数调用栈进行归纳合并,以柱状图的形式呈现给系统管理员。 阅读详情

相关推荐

Linux性能分析工具Perf简单教程

Perf是基于Linux内核perf_events接口实现的性能分析工具。我们可以用它来分析一个程序的性能,找出影响程序性能的瓶颈,并在此基础上对程序性能进行改进。

volunteer01的专栏 4349

实时系统实时性测试的方法

rt-linux实时性测试方法

high_sky_fly的博客 2604

Hackbench 使用介绍

Linux 的调度性能测试

m0_37749564的博客 3004

Perf -- Linux下的系统性能调优工具,第 2 部分

Perf -- Linux下的系统性能调优工具,第 2 部分 特殊用法以及内核调优示例 本文转自:https://www.ibm.com/developerworks/cn/linux/l-cn-perf2/ 本文内容简介 之前介绍了 perf 最常见的一些用法,关注于 Linux 系统上应用程序的调优。现在让我们把目光转移到内核以及其他 perf 命令上面来。 在内核方面,人们的兴趣五...

姜的理想国 1119

Linux性能测试工具

Linux系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工具来判断究竟是哪一部分(内存、CPU、硬盘……)出了问题。在Linux系统中,所有的运行参数保存在虚拟目录/proc中,换句话说,我们使用的性能监控工具取到的数据值实际上是源自于这个目录,当涉及到系统高估时,我们可以修改/proc目录中的相关参数了,当然有些是不能乱改的。下面让我们了解一下这些常用的性能监控工具。   工具     功能描述 uptime  —-   系统平均负载率  dmesg  —- 硬件/系统信息  top  —–  进程进行状态 iostat  ——–  CPU和磁盘平均使

win10安装openvpn客户端

进入C:\Program Files\TAP-Windows\bin目录,然后右键用管理员权限执行addtap.bat文件,此文件执行会创建一个网卡。链接:https://pan.baidu.com/s/1KAifTiP7qp1i5ZkfdnfwkA。4.2 进入安装目录的config下新建一个文件夹,将证书文件全部挪进去(不移动.ovpn文件)3.4 将拿到的ssl认证文件放入安装目录的config目录下。然后修改ovpn文件 “./”代表当前路径,第二个链接重复。3.1 一直下一步到这里打上勾。

qq_36200932的博客 3万+

Perf -- Linux下的系统性能调优工具

Perf -- Linux下的系统性能调优工具 http://www.ibm.com/developerworks/cn/linux/l-cn-perf1/?jumpfrom=wenwenweekly

liangkwok的专栏 961

稳定性性能系列之五——Native Crash深度分析:工具实战

深入理解Native Crash的机制与分析方法,掌握addr2line、ndk-stack、gdb等核心工具的实战使用,学会从Tombstone文件中快速定位问题根因

Cheson的专栏 1545

Perf -- Linux下的系统性能调优工具,第 1 部分

Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 本文转自:https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/index.html Perf 简介 Perf 是用来进行软件性能分析的工具。 通过它,应用程序可以利用 PMU,tracepoint 和内核中的特殊计数器来进行性能统计。它不但可...

姜的理想国 1165

FPGA实现Gardner环定时同步:Verilog代码与Testbench实战解析

本文深入解析了在FPGA上实现Gardner环定时同步的完整过程,详细阐述了其非数据辅助的定时误差检测原理。通过Verilog代码实战,展示了内插滤波器、定时误差检测器、环路滤波器和数控振荡器等核心模块的设计,并提供了完整的Testbench验证方法,帮助开发者解决数字通信系统中的时钟同步难题。

gpt4scribbler的博客 576

CST实战:5分钟搞定圆极化天线轴比分析(含远场设置技巧)

本文提供了一份CST Studio Suite圆极化天线轴比分析的实战指南。文章详细讲解了轴比的概念与工程标准,并重点演示了如何正确设置远场监视器以获取Axial Ratio数据,包括关键步骤与后处理技巧。最后,通过植入式天线在生物组织环境中的案例,分析了复杂环境对轴比性能的影响及优化思路。

nokia的博客 997

Perf -- Linux下的系统性能调优工具(转载)

https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/index.html Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 刘 明 2011 年 5 月 26 日发布 WeiboGoogle+用电子邮件发送本页面 9 Perf 简介 Perf 是用来进行软件性能分析的工具。...

710

Verilog十大基本功2(testbench的设计 文件读取和写入操作 源代码)

需求说明:Verilog设计基础 内容       :testbench的设计 读取文件 写入文件 来自       :时间的诗 十大基本功之 testbench 1. 激励的产生 对于 testbench 而言,端口应当和被测试的 module 一一对应。 端口分为 input,output 和 inout 类型产生激励信号的时候, input  对

时间的诗 5万+

Linux下的系统性能调优工具——Perf

参考以下链接文章整理: Perf -- Linux下的系统性能调优工具,第 1 部分 Perf -- Linux下的系统性能调优工具,第 2 部分 感谢原作者,让我知道了性能调优也是如此有趣,Linux下还有如此强大之Linux系统调优工具。 Perf——Linux下的系统性能调优工具 1. 背景知识 1.1 与性能调优相关的硬件特性 硬件特性之cache 内存读写是很快的

梦断代码的梦中 1万+

huggingface使用git上传model或者Dataset到HF,当然也可以上传代码,以及大文件,很哇塞!!!

huggingface使用git上传model或者Dataset到HF,当然也可以上传代码,以及大文件,很哇塞!!! 第一次使用时,需要使用设置系统全局的huggingface token,这。点击huggingface首页右上角的头像 → settings → access tokens,创建一个write权限的token,名字可以随便取。

拥抱具身智能,昇腾Ascend C 3105

三菱FX系列PLC模拟量输入AD模块的使用方法和相关编程设置详解

三菱FX系列PLC模拟量输入AD模块的使用方法和相关编程设置详解

Robot_PLC_HMI_Automation 1万+

Perf -- Linux下的系统性能调优工具(引用博客)

   Perf 宏观介绍:     刘 明 软件工程师, 上海交通大学通信与电子工程系   Perf -- Linux下的系统性能调优工具1              https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/   Perf -- Linux下的系统性能调优工具2               https://www.i...

飘过的春风 957

lammps 案例in文件汇总

进阶案例: 聚乙烯和石墨烯界面pull out模拟代码 反应力场reax/c设置方法及案例解析 石墨烯热导率模拟代码(NEMD方法) 圆锥形SiC纳米磨削单晶硅源代码 高熵合金FeNiCrCoCu纳米压痕模拟代码 fix indent命令实现纳米摩擦及摩擦力计算 Fe原子辐照轰击多层石墨烯模拟代码 铝基石墨烯复合物压缩变形in文件源代码 石墨烯自动卷曲到Fe纳米线模拟源代码 铝/石墨烯复合物结晶过程lammps模

lammps_jiayou的博客 9446

Perf -- Linux下的系统性能调优工具(一)

Perf -- Linux下的系统性能调优工具(一) 简介: Perf Event是一款随 Linux 内核代码一同发布和维护的性能诊断工具,由内核社区维护和发展。Perf不仅可以用于应用程序的性能统计分析,也可以应用于内核代码的性能统计和分析。得益于其优秀的体系结构设计,越来越多的新功能被加入 Perf,使其已经成为一个多功能的性能统计工具集。在第一部分,将介绍 Perf 在应用程序开发

tebeg521的专栏 589

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

上一篇: 谁动了我的cpu——oprofile使用
下一篇: gluster 删除数据的扩展属性
peter_cloud
博客等级 码龄15年 24粉丝 30原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值