# perf-tools **Repository Path**: home-coder/perf-tools ## Basic Information - **Project Name**: perf-tools - **Description**: 一个开发中的Linux性能测试使用的工具,能够收集ftrace和perf_events中乱七八糟的参数。ftrace和perf都是Linux中的内核跟踪工具,包含在Kernel中。ftrace在多数系统中已经存在,但perf可能需要添加额外的软件包。 这些工具设计的很容易安装(较少的依赖),能提供详细直观的性能参数,并且操作简单。本教程由Brendan Gregg (DTraceToolkit的作者)创建。 这些工具以服务的方式运行在内核中,因此多数工具都有说明手册(man帮助手册),并且工具需要保证内核特性向前兼容或者能够和新的跟踪工具集成。 本教程适用于Linux3.2及更新的版本。Linux2.6.x版本请参考警告页。 - **Primary Language**: Unknown - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2021-05-31 - **Last Updated**: 2021-05-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README perf-tools ========== 一个开发中的Linux性能测试使用的工具,能够收集ftrace和perf_events中乱七八糟的参数。ftrace和perf都是Linux中的内核跟踪工具,包含在Kernel中。ftrace在多数系统中已经存在,但perf可能需要添加额外的软件包。 这些工具设计的很容易安装(较少的依赖),能提供详细直观的性能参数,并且操作简单。本教程由Brendan Gregg (DTraceToolkit的作者)创建。 这些工具以服务的方式运行在内核中,因此多数工具都有说明手册(man帮助手册),并且工具需要保证内核特性向前兼容或者能够和新的跟踪工具集成。 本教程适用于Linux3.2及更新的版本。Linux2.6.x版本请参考警告页。 ## Presentation 这些工具在USENIX LISA 2014 presentation: Linux Performance Analysis: New Tools and Old Secrets中有相关演示 - PPT:http://www.slideshare.net/brendangregg/linux-performance-analysis-new-tools-and-old-secrets - 视频:https://www.usenix.org/conference/lisa14/conference-program/presentation/gregg ## 主题
使用 ftrace: # - [iosnoop](iosnoop): 追踪磁盘IO细节和潜在问题. [Examples](examples/iosnoop_example.txt). - [iolatency](iolatency): 柱状图显示磁盘IO状况. [Examples](examples/iolatency_example.txt). - [execsnoop](execsnoop): 追踪exec()进程的状态和命令行参数. [Examples](examples/execsnoop_example.txt). - [opensnoop](opensnoop): 追踪open()系统调用情况显示文件名. [Examples](examples/opensnoop_example.txt). - [killsnoop](killsnoop): 追踪kill()信号显示进程和信号详细信息. [Examples](examples/killsnoop_example.txt). - fs/[cachestat](fs/cachestat): Linux页缓存的命中/丢失统计. [Examples](examples/cachestat_example.txt). - net/[tcpretrans](net/tcpretrans): 显示TCP的重发情况,包括地址等详细信息. [Examples](examples/tcpretrans_example.txt). - system/[tpoint](system/tpoint): 根据给定的追踪点追踪. [Examples](examples/tpoint_example.txt). - kernel/[funccount](kernel/funccount): 显示内核函数的调用次数,支持通配符匹配. [Examples](examples/funccount_example.txt). - kernel/[functrace](kernel/functrace): 跟踪内核函数的调用,支持通配符匹配. [Examples](examples/functrace_example.txt). - kernel/[funcslower](kernel/funcslower): 跟踪调用慢于设定阈值的内核函数. [Examples](examples/funcslower_example.txt). - kernel/[funcgraph](kernel/funcgraph): 图表方式显示某个内核函数的调用,包括子函数和调用时间. [Examples](examples/funcgraph_example.txt). - kernel/[kprobe](kernel/kprobe): 动态地追踪某个内核函数的调用和返回,及其参数. [Examples](examples/kprobe_example.txt). - user/[uprobe](user/uprobe): 动态地追踪某个用户函数的调用和返回,及其参数. [Examples](examples/uprobe_example.txt). - tools/[reset-ftrace](tools/reset-ftrace): 必要时重置ftrace的状态(正常情况下不会使用该功能). [Examples](examples/reset-ftrace_example.txt). 使用 perf_events: - misc/[perf-stat-hist](misc/perf-stat-hist): 聚合某个追踪点的变量. [Examples](examples/perf-stat-hist_example.txt). - [syscount](syscount): 使用系统调用来统计系统调用. [Examples](examples/syscount_example.txt). - disk/[bitesize](disk/bitesize): 柱状图显示磁盘IO数量. [Examples](examples/bitesize_example.txt). 使用 eBPF: - 未来功能的预览,参考bcc工具的章节 [Tools section](https://github.com/iovisor/bcc/blob/master/README.md#tracing). These use [bcc](https://github.com/iovisor/bcc), a front end for using [eBPF](http://www.brendangregg.com/blog/2015-05-15/ebpf-one-small-step.html). bcc+eBPF will allow some of these tools to be rewritten and improved, and additional tools to be created. - ## 截图 显示新进程及其参数:
# ./execsnoop 
Tracing exec()s. Ctrl-C to end.
   PID   PPID ARGS
 22898  22004 man ls
 22905  22898 preconv -e UTF-8
 22908  22898 pager -s
 22907  22898 nroff -mandoc -rLL=164n -rLT=164n -Tutf8
 22906  22898 tbl
 22911  22910 locale charmap
 22912  22907 groff -mtty-char -Tutf8 -mandoc -rLL=164n -rLT=164n
 22913  22912 troff -mtty-char -mandoc -rLL=164n -rLT=164n -Tutf8
 22914  22912 grotty
从complatex写入队列中的数量来分析块设备IO潜在问题:
# ./iolatency -Q
Tracing block I/O. Output every 1 seconds. Ctrl-C to end.

  >=(ms) .. <(ms)   : I/O      |Distribution                          |
       0 -> 1       : 1913     |######################################|
       1 -> 2       : 438      |#########                             |
       2 -> 4       : 100      |##                                    |
       4 -> 8       : 145      |###                                   |
       8 -> 16      : 43       |#                                     |
      16 -> 32      : 43       |#                                     |
      32 -> 64      : 1        |#                                     |

[...]
跟踪block:block_rq_insert跟踪点,只显示读取时内核栈信息:
# ./tpoint -s block:block_rq_insert 'rwbs ~ "*R*"'
   cksum-11908 [000] d... 7269839.919098: block_rq_insert: 202,1 R 0 () 736560 + 136 [cksum]
   cksum-11908 [000] d... 7269839.919107: 
 => __elv_add_request
 => blk_flush_plug_list
 => blk_finish_plug
 => __do_page_cache_readahead
 => ondemand_readahead
 => page_cache_async_readahead
 => generic_file_read_iter
 => new_sync_read
 => vfs_read
 => SyS_read
 => system_call_fastpath

[...]
统计以“bio_”开始的内核函数的调用,时间间隔为1秒:
# ./funccount -i 1 'bio_*'
Tracing "bio_*"... Ctrl-C to end.

FUNC                              COUNT
bio_attempt_back_merge               26
bio_get_nr_vecs                     361
bio_alloc                           536
bio_alloc_bioset                    536
bio_endio                           536
bio_free                            536
bio_fs_destructor                   536
bio_init                            536
bio_integrity_enabled               536
bio_put                             729
bio_add_page                       1004

[...]
在工具的[examples](examples) 目录中还有许多示例,也可以查阅man手册。 ## Prerequisites 本节力求简洁,更多Linux3.2服务器调试信息的细节请参考man手册。 ### ftrace FTRACE集成在内核中,所以在系统中可能已经存在并且可以使用。FTRACE在2.6.27版本内核中首次集成,需要参数CONFIG_FTRACE和其他FTRACE工具依赖的选项。有些工具(如cunccount)还要依赖CONFIG_FUNCTION_PROFILER选项。 ### perf_events 需要首先安装“perf”命令使用的工具软件,位于linux-tool-common软件包中。安装完成后,perf可能会提示安装一个额外的linux工具(linux-tool-kernel_version)。perf也可以从内核源码中的tools/perf中编译安装。参考[perf_events Prerequisites](http://www.brendangregg.com/perf.html#Prerequisites)要求来获取使用perf_events完整功能的详细信息。 ### debugfs 需要内核选项CONFIG_DEBUG_FS的支持,与FTRACE一样,debugfs可能已经在系统中启用(2.6.10-rc3之后的内核)。debugfs需要被挂载后使用: ``` # mount -t debugfs none /sys/kernel/debug ``` ### awk 工具中的脚本大量使用了awk工具,根据功能目的会选择使用mawk或gawk:mawk用来缓存输出(更快的速度),gawk用来同步输出(更有效的分组输出)。 ## Install 工具本身使一些脚本,可以这样来抓取所有内容: ``` git clone --depth 1 https://github.com/brendangregg/perf-tools ``` 或者使用github源URL获取单个的脚本: ``` wget https://raw.githubusercontent.com/brendangregg/perf-tools/master/iosnoop ``` This preserves tabs (which copy-n-paste can mess up). ## 警告 Ftrace在Linux2.6.27版本内核中首次集成,perf_events则是在2.6.31版本中加入。早前的版本可能存在一些缺陷,这些锁或致命的问题在2.6.32系列内核中被发现。包括CentOS6.x,如果需要分析旧版本的系统内核,此工具需要在容错环境中使用,如实验测试环境。但是在Linux3.2及之后的内核中没有问题。 使用此工具也存在一些限制,具体信息如下。 ## 内部开销 perf_events是在不断衍变的,这个系列主要是为Linux3.2内核所开发,而且也缺乏功能规划,这些可能在将来的内核中加入。在此之前,该工具依然利用欺骗和侵入内核的方式工作。有的工具会将事件发送到用户空间进行处理,这会比在内核空间处理带来更大的开销。每个工具的使用开销在其man手册页中有具体的描述。 警告:在某些极端情况下,使用该工具会使应用运行速度下降5倍。取决于此工具和内核的版本,也可能导致内核的崩溃。阅读程序头部的警告信息并且在使用前进行测试。 如果工具的开销是个问题,那么也可以进行改良。程序可以使用C语言重写,并且调用perf_events_open() 和 mmap()函数获得栈缓存。也可以用C语言实现频率统计,并且直接使用mmap()函数处理,而不必使用awk、Perl、Python等处理。基于ftrace的工具还可以进行一些额外的改进,比如使用快照和预分配缓存。 这些工具被作为内核功能补充被短期维护,所以有很大的可能被重写。旧版本的工具也会保存在仓库中供旧版本的内核使用。 As my main target is a fleet of Linux 3.2 servers that do not have debuginfo, these tools try not to require it. At times, this makes the tool more brittle than it needs to be, as I'm employing workarounds (that may be kernel version and platform specific) instead of using debuginfo information (which can be generic). See the man page for detailed prerequisites for each tool. I've tried to use perf_events ("perf") where possible, since that interface has been developed for multi-user use. For various reasons I've often needed to use ftrace instead. ftrace is surprisingly powerful (thanks Steven Rostedt!), and not all of its features are exposed via perf, or in common usage. This tool collection is in some ways a demonstration of hidden Linux features using ftrace. Since things are changing, it's very possible you may find some tools don't work on your Linux kernel version. Some expertise and assembly will be required to fix them. ## Links 案例学习和总结: - 13 Aug 2014: http://lwn.net/Articles/608497 Ftrace: The hidden light switch Related articles: - 28 Jun 2015: http://www.brendangregg.com/blog/2015-06-28/linux-ftrace-uprobe.html - 31 Dec 2014: http://www.brendangregg.com/blog/2014-12-31/linux-page-cache-hit-ratio.html - 06 Sep 2014: http://www.brendangregg.com/blog/2014-09-06/linux-ftrace-tcp-retransmit-tracing.html - 28 Jul 2014: http://www.brendangregg.com/blog/2014-07-28/execsnoop-for-linux.html - 25 Jul 2014: http://www.brendangregg.com/blog/2014-07-25/opensnoop-for-linux.html - 23 Jul 2014: http://www.brendangregg.com/blog/2014-07-23/linux-iosnoop-latency-heat-maps.html - 16 Jul 2014: http://www.brendangregg.com/blog/2014-07-16/iosnoop-for-linux.html - 10 Jul 2014: http://www.brendangregg.com/blog/2014-07-10/perf-hacktogram.html