从2.6.31内核开始,linux内核自带了一个性能分析工具perf,能够进行函数级与指令级的热点查找。

perf

Performance analysis tools for Linux.

Performance counters for Linux are a new kernel-based subsystem that provide a framework for all things

performance analysis. It covers hardware level (CPU/PMU, Performance Monitoring Unit) features and

software features (software counters, tracepoints) as well.

Perf是内置于Linux内核源码树中的性能剖析(profiling)工具。

它基于事件采样原理,以性能事件为基础,支持针对处理器相关性能指标与操作系统相关性能指标的性能剖析。

常用于性能瓶颈的查找与热点代码的定位。

CPU周期(cpu-cycles)是默认的性能事件,所谓的CPU周期是指CPU所能识别的最小时间单元,通常为亿分之几秒,

是CPU执行最简单的指令时所需要的时间,例如读取寄存器中的内容,也叫做clock tick。

Perf是一个包含22种子工具的工具集,以下是最常用的5种:

perf-list

perf-stat

perf-top

perf-record

perf-report

perf-list

Perf-list用来查看perf所支持的性能事件,有软件的也有硬件的。

List all symbolic event types.

perf list [hw | sw | cache | tracepoint | event_glob]

(1) 性能事件的分布

hw:Hardware event,9个

sw:Software event,9个

cache:Hardware cache event,26个

tracepoint:Tracepoint event,775个

sw实际上是内核的计数器,与硬件无关。

hw和cache是CPU架构相关的,依赖于具体硬件。

tracepoint是基于内核的ftrace,主线2.6.3x以上的内核版本才支持。

(2) 指定性能事件(以它的属性)

-e <event> : u // userspace

-e <event> : k // kernel

-e <event> : h // hypervisor

-e <event> : G // guest counting (in KVM guests)

-e <event> : H // host counting (not in KVM guests)

(3) 使用例子

显示内核和模块中,消耗最多CPU周期的函数:

# perf top -e cycles:k

显示分配高速缓存最多的函数:

# perf top -e kmem:kmem_cache_alloc

perf-top

对于一个指定的性能事件(默认是CPU周期),显示消耗最多的函数或指令。

System profiling tool.

Generates and displays a performance counter profile in real time.

perf top [-e <EVENT> | --event=EVENT] [<options>]

perf top主要用于实时分析各个函数在某个性能事件上的热度,能够快速的定位热点函数,包括应用程序函数、

模块函数与内核函数,甚至能够定位到热点指令。默认的性能事件为cpu cycles。

(1) 输出格式

# perf top

[plain] view plaincopy
  1. Samples: 1M of event 'cycles', Event count (approx.): 73891391490
  2. 5.44%  perf              [.] 0x0000000000023256
  3. 4.86%  [kernel]          [k] _spin_lock
  4. 2.43%  [kernel]          [k] _spin_lock_bh
  5. 2.29%  [kernel]          [k] _spin_lock_irqsave
  6. 1.77%  [kernel]          [k] __d_lookup
  7. 1.55%  libc-2.12.so      [.] __strcmp_sse42
  8. 1.43%  nginx             [.] ngx_vslprintf
  9. 1.37%  [kernel]          [k] tcp_poll

第一列:符号引发的性能事件的比例,默认指占用的cpu周期比例。

第二列:符号所在的DSO(Dynamic Shared Object),可以是应用程序、内核、动态链接库、模块。

第三列:DSO的类型。[.]表示此符号属于用户态的ELF文件,包括可执行文件与动态链接库)。[k]表述此符号属于内核或模块。

第四列:符号名。有些符号不能解析为函数名,只能用地址表示。

(2) 常用交互命令

h:显示帮助

UP/DOWN/PGUP/PGDN/SPACE:上下和翻页。

a:annotate current symbol,注解当前符号。能够给出汇编语言的注解,给出各条指令的采样率。

d:过滤掉所有不属于此DSO的符号。非常方便查看同一类别的符号。

P:将当前信息保存到perf.hist.N中。

(3) 常用命令行参数

-e <event>:指明要分析的性能事件。

-p <pid>:Profile events on existing Process ID (comma sperated list). 仅分析目标进程及其创建的线程。

-k <path>:Path to vmlinux. Required for annotation functionality. 带符号表的内核映像所在的路径。

-K:不显示属于内核或模块的符号。

-U:不显示属于用户态程序的符号。

-d <n>:界面的刷新周期,默认为2s,因为perf top默认每2s从mmap的内存区域读取一次性能数据。

-G:得到函数的调用关系图。

perf top -G [fractal],路径概率为相对值,加起来为100%,调用顺序为从下往上。

perf top -G graph,路径概率为绝对值,加起来为该函数的热度。

(4) 使用例子

# perf top // 默认配置

# perf top -G // 得到调用关系图

# perf top -e cycles // 指定性能事件

# perf top -p 23015,32476 // 查看这两个进程的cpu cycles使用情况

# perf top -s comm,pid,symbol // 显示调用symbol的进程名和进程号

# perf top --comms nginx,top // 仅显示属于指定进程的符号

# perf top --symbols kfree // 仅显示指定的符号

perf-stat

用于分析指定程序的性能概况。

Run a command and gather performance counter statistics.

perf stat [-e <EVENT> | --event=EVENT] [-a] <command>

perf stat [-e <EVENT> | --event=EVENT] [-a] - <command> [<options>]

(1) 输出格式

# perf stat ls

[plain] view plaincopy
  1. Performance counter stats for 'ls':
  2. 0.653782 task-clock                #    0.691 CPUs utilized
  3. 0 context-switches          #    0.000 K/sec
  4. 0 CPU-migrations            #    0.000 K/sec
  5. 247 page-faults               #    0.378 M/sec
  6. 1,625,426 cycles                    #    2.486 GHz
  7. 1,050,293 stalled-cycles-frontend   #   64.62% frontend cycles idle
  8. 838,781 stalled-cycles-backend    #   51.60% backend  cycles idle
  9. 1,055,735 instructions              #    0.65  insns per cycle
  10. #    0.99  stalled cycles per insn
  11. 210,587 branches                  #  322.106 M/sec
  12. 10,809 branch-misses             #    5.13% of all branches
  13. 0.000945883 seconds time elapsed

输出包括ls的执行时间,以及10个性能事件的统计。

task-clock:任务真正占用的处理器时间,单位为ms。CPUs utilized = task-clock / time elapsed,CPU的占用率。

context-switches:上下文的切换次数。

CPU-migrations:处理器迁移次数。Linux为了维持多个处理器的负载均衡,在特定条件下会将某个任务从一个CPU

迁移到另一个CPU。

page-faults:缺页异常的次数。当应用程序请求的页面尚未建立、请求的页面不在内存中,或者请求的页面虽然在内

存中,但物理地址和虚拟地址的映射关系尚未建立时,都会触发一次缺页异常。另外TLB不命中,页面访问权限不匹配

等情况也会触发缺页异常。

cycles:消耗的处理器周期数。如果把被ls使用的cpu cycles看成是一个处理器的,那么它的主频为2.486GHz。

可以用cycles / task-clock算出。

stalled-cycles-frontend:略过。

stalled-cycles-backend:略过。

instructions:执行了多少条指令。IPC为平均每个cpu cycle执行了多少条指令。

branches:遇到的分支指令数。branch-misses是预测错误的分支指令数。

(2) 常用参数

-p:stat events on existing process id (comma separated list). 仅分析目标进程及其创建的线程。

-a:system-wide collection from all CPUs. 从所有CPU上收集性能数据。

-r:repeat command and print average + stddev (max: 100). 重复执行命令求平均。

-C:Count only on the list of CPUs provided (comma separated list), 从指定CPU上收集性能数据。

-v:be more verbose (show counter open errors, etc), 显示更多性能数据。

-n:null run - don't start any counters,只显示任务的执行时间 。

-x SEP:指定输出列的分隔符。

-o file:指定输出文件,--append指定追加模式。

--pre <cmd>:执行目标程序前先执行的程序。

--post <cmd>:执行目标程序后再执行的程序。

(3) 使用例子

执行10次程序,给出标准偏差与期望的比值:

# perf stat -r 10 ls > /dev/null

显示更详细的信息:

# perf stat -v ls > /dev/null

只显示任务执行时间,不显示性能计数器:

# perf stat -n ls > /dev/null

单独给出每个CPU上的信息:

# perf stat -a -A ls > /dev/null

ls命令执行了多少次系统调用:

# perf stat -e syscalls:sys_enter ls

perf-record

收集采样信息,并将其记录在数据文件中。

随后可以通过其它工具(perf-report)对数据文件进行分析,结果类似于perf-top的。

Run a command and record its profile into perf.data.

This command runs a command and gathers a performance counter profile from it, into perf.data,

without displaying anything. This file can then be inspected later on, using perf report.

(1) 常用参数

-e:Select the PMU event.

-a:System-wide collection from all CPUs.

-p:Record events on existing process ID (comma separated list).

-A:Append to the output file to do incremental profiling.

-f:Overwrite existing data file.

-o:Output file name.

-g:Do call-graph (stack chain/backtrace) recording.

-C:Collect samples only on the list of CPUs provided.

(2) 使用例子

记录nginx进程的性能数据:

# perf record -p `pgrep -d ',' nginx`

记录执行ls时的性能数据:

# perf record ls -g

记录执行ls时的系统调用,可以知道哪些系统调用最频繁:

# perf record -e syscalls:sys_enter ls

perf-report

读取perf record创建的数据文件,并给出热点分析结果。

Read perf.data (created by perf record) and display the profile.

This command displays the performance counter profile information recorded via perf record.

(1) 常用参数

-i:Input file name. (default: perf.data)

(2) 使用例子

# perf report -i perf.data.2

More

除了以上5个常用工具外,还有一些适用于较特殊场景的工具, 比如内核锁、slab分配器、调度器,

也支持自定义探测点。

perf-lock

内核锁的性能分析。

Analyze lock events.

perf lock {record | report | script | info}

需要编译选项的支持:CONFIG_LOCKDEP、CONFIG_LOCK_STAT。

CONFIG_LOCKDEP defines acquired and release events.

CONFIG_LOCK_STAT defines contended and acquired lock events.

(1) 常用选项

-i <file>:输入文件

-k <value>:sorting key,默认为acquired,还可以按contended、wait_total、wait_max和wait_min来排序。

(2) 使用例子

# perf lock record ls // 记录

# perf lock report // 报告

(3) 输出格式

[plain] view plaincopy
  1. Name   acquired  contended total wait (ns)   max wait (ns)   min wait (ns)
  2. &mm->page_table_...        382          0               0               0               0
  3. &mm->page_table_...         72          0               0               0               0
  4. &fs->lock         64          0               0               0               0
  5. dcache_lock         62          0               0               0               0
  6. vfsmount_lock         43          0               0               0               0
  7. &newf->file_lock...         41          0               0               0               0

Name:内核锁的名字。

aquired:该锁被直接获得的次数,因为没有其它内核路径占用该锁,此时不用等待。

contended:该锁等待后获得的次数,此时被其它内核路径占用,需要等待。

total wait:为了获得该锁,总共的等待时间。

max wait:为了获得该锁,最大的等待时间。

min wait:为了获得该锁,最小的等待时间。

最后还有一个Summary:

[plain] view plaincopy
  1. === output for debug===
  2. bad: 10, total: 246
  3. bad rate: 4.065041 %
  4. histogram of events caused bad sequence
  5. acquire: 0
  6. acquired: 0
  7. contended: 0
  8. release: 10

perf-kmem

slab分配器的性能分析。

Tool to trace/measure kernel memory(slab) properties.

perf kmem {record | stat} [<options>]

(1) 常用选项

--i <file>:输入文件

--caller:show per-callsite statistics,显示内核中调用kmalloc和kfree的地方。

--alloc:show per-allocation statistics,显示分配的内存地址。

-l <num>:print n lines only,只显示num行。

-s <key[,key2...]>:sort the output (default: frag,hit,bytes)

(2) 使用例子

# perf kmem record ls // 记录

# perf kmem stat --caller --alloc -l 20 // 报告

(3) 输出格式

[plain] view plaincopy
  1. ------------------------------------------------------------------------------------------------------
  2. Callsite                           | Total_alloc/Per | Total_req/Per   | Hit      | Ping-pong | Frag
  3. ------------------------------------------------------------------------------------------------------
  4. perf_event_mmap+ec                 |    311296/8192  |    155952/4104  |       38 |        0 | 49.902%
  5. proc_reg_open+41                   |        64/64    |        40/40    |        1 |        0 | 37.500%
  6. __kmalloc_node+4d                  |      1024/1024  |       664/664   |        1 |        0 | 35.156%
  7. ext3_readdir+5bd                   |        64/64    |        48/48    |        1 |        0 | 25.000%
  8. load_elf_binary+8ec                |       512/512   |       392/392   |        1 |        0 | 23.438%

Callsite:内核代码中调用kmalloc和kfree的地方。

Total_alloc/Per:总共分配的内存大小,平均每次分配的内存大小。

Total_req/Per:总共请求的内存大小,平均每次请求的内存大小。

Hit:调用的次数。

Ping-pong:kmalloc和kfree不被同一个CPU执行时的次数,这会导致cache效率降低。

Frag:碎片所占的百分比,碎片 = 分配的内存 - 请求的内存,这部分是浪费的。

有使用--alloc选项,还会看到Alloc Ptr,即所分配内存的地址。

最后还有一个Summary:

[plain] view plaincopy
  1. SUMMARY
  2. =======
  3. Total bytes requested: 290544
  4. Total bytes allocated: 447016
  5. Total bytes wasted on internal fragmentation: 156472
  6. Internal fragmentation: 35.003669%
  7. Cross CPU allocations: 2/509

probe-sched

调度模块分析。

trace/measure scheduler properties (latencies)

perf sched {record | latency | map | replay | script}

(1) 使用例子

# perf sched record sleep 10 // perf sched record <command>

# perf report latency --sort max

(2) 输出格式

[plain] view plaincopy
  1. ---------------------------------------------------------------------------------------------------------------
  2. Task                  |   Runtime ms  | Switches | Average delay ms | Maximum delay ms | Maximum delay at     |
  3. ---------------------------------------------------------------------------------------------------------------
  4. events/10:61          |      0.655 ms |       10 | avg:    0.045 ms | max:    0.161 ms | max at: 9804.958730 s
  5. sleep:11156           |      2.263 ms |        4 | avg:    0.052 ms | max:    0.118 ms | max at: 9804.865552 s
  6. edac-poller:1125      |      0.598 ms |       10 | avg:    0.042 ms | max:    0.113 ms | max at: 9804.958698 s
  7. events/2:53           |      0.676 ms |       10 | avg:    0.037 ms | max:    0.102 ms | max at: 9814.751605 s
  8. perf:11155            |      2.109 ms |        1 | avg:    0.068 ms | max:    0.068 ms | max at: 9814.867918 s

TASK:进程名和pid。

Runtime:实际的运行时间。

Switches:进程切换的次数。

Average delay:平均的调度延迟。

Maximum delay:最大的调度延迟。

Maximum delay at:最大调度延迟发生的时刻。

perf-probe

可以自定义探测点。

Define new dynamic tracepoints.

使用例子

(1) Display which lines in schedule() can be probed

# perf probe --line schedule

前面有行号的可以探测,没有行号的就不行了。

(2) Add a probe on schedule() function 12th line.

# perf probe -a schedule:12

在schedule函数的12处增加一个探测点。

系统级性能分析工具 — Perf相关推荐

  1. 系统级性能分析工具perf的介绍与使用

    测试环境:Ubuntu16.04 + Kernel:4.4.0-31 apt-get install linux-source cd /usr/src/tools/perf make &&am ...

  2. linux 解析pdf下载工具,Linux高级系统级性能分析工具-perf.pdf

    Linux高级系统级性能分析工具-perf Linux 的系统级性能剖析工具‐perf (二) 承刚 TAOBAO  Kernel Team chenggang.qin@ 第三章  Perf top ...

  3. linux 系统级性能分析工具 perf 的介绍与使用

    目录 1. 背景知识 1.1 tracepoints 1.2 硬件特性之cache 2. 主要关注点 3. perf的使用 3.0 perf引入的overhead 3.1 perf list 3.2 ...

  4. java perf_系统级性能分析工具perf的介绍与使用

    测试环境:Ubuntu16.04 + Kernel:4.4.0-31 系统级性能优化通常包括两个阶段:性能剖析(performance profiling)和代码优化. 性能剖析的目标是寻找性能瓶颈, ...

  5. 系统级性能分析工具--Systemtap

    SystemTap 是一款诊断Linux系统性能的工具,可以跟踪内核以及用户态程序中的任意函数.syscall.语句甚至指令,可以用来动态地收集调试和性能信息的工具,不需要我们重新编译.重启内核.缺点 ...

  6. 【开发工具】【perf】性能分析工具perf的编译和使用说明

    源码位置: kernel/tools/perf 配置内核以支持perf make xxx_defconfig make menuconfig设置以下配置: CONFIG_HAVE_PERF_EVENT ...

  7. linux 性能分析工具perf

    perf 性能监控分析工具 perf 工具下载 https://cdn.kernel.org/pub/linux/kernel/tools/perf/v5.9.0/perf-5.9.0.tar.gz ...

  8. Linux性能分析工具perf基础使用介绍

    perf是Linux内核内置的性能分析工具.从内核版本2.6.31开始出现该工具,如果没有安装,可以使用以下命令进行安装 yum -y install perf.x86_64 这里我们主要介绍一下如何 ...

  9. 2022最火的Linux性能分析工具--perf

    ►►► 介绍 perf是Linux性能分析中,比较常用的一款工具.它基于时间采集原理,以性能事件为基础,支持针对CPU处理器相关性能指标与操作系统相关性能指标的性能分析.常被用来查找.定位源码级性能问 ...

最新文章

  1. 每日一皮:是金子无论到哪里、哪怕变个形状都会发光..
  2. 如何自学python a byte to python-你是如何自学 Python 的?
  3. (function() {})();和(function(){}())
  4. 第九届蓝桥杯大赛软件赛省赛 C/C++ 大学B组
  5. zoj 1789 The Suspects
  6. blender下载_下载此Blender Hotkey备忘单
  7. php 读取管道,php – 使用proc_open时从STDIN管道读取
  8. html toggle自动隐藏,Javascript / HTML – 切换可见性(当另一个div元素呈现可见时自动导致一个div元素隐藏)...
  9. java实现二分查找
  10. html导航栏的渐变效果,html+css+js实现导航栏滚动渐变效果
  11. 手把手教你修改butterfly主题的样式
  12. Android2018年最新前沿框架和技术
  13. android 背景 投影,Android ImageView投影
  14. 算法题(2)力扣第n位数字
  15. matlab BP神经网络 newff函数官方源码
  16. 在Ubuntu18.04.2LTS上安装电子书软件
  17. 【pyppeteer 截图神器】发起异步请求并截图转换成PDF
  18. 数字IC设计随笔之七(TCL脚本编程入门)
  19. 本证方程的本征值 与 特征方程的特征值 的 区别 辨析
  20. 为数而生,爱数的变与不变

热门文章

  1. Linux中添加pycharm源,linux下python+pycharm安装
  2. 深度学习:dropout和BN的实现
  3. 一时技痒 不用模拟第一印象的构造 通过三个观察得来的规律解决N^2个往返接力问题...
  4. 2018-2019-2 20175204 张湲祯 实验五《网络编程与安全》实验报告
  5. Everything Has Changed(HDU6354+圆交+求周长)
  6. mysql数据库、表、索引、触发器
  7. Java UDP协议传输
  8. 推荐一个很好用的脚本session snapper
  9. 2.监控软件zabbix-客户端安装
  10. 播放生成Android4.0.3中的提示音