linux系统调用测试工具,Linux下的内核测试工具——perf使用简介

博客介绍了如何使用perf工具进行性能分析,包括通过采样率(-f)、事件触发(-c)、指定进程(-p)和线程(-t)等参数进行性能数据收集。perfreport则用于展示统计信息,如按调用关系(-g)显示,以及通过-k参数获取内核相关信息。此外,文章提到了tracepoint在关注特定内核行为和系统调用统计上的应用,举例展示了如何使用perf记录和报告sys_enter tracepoint。
摘要由CSDN通过智能技术生成

结果解释:

The column 'Overhead' indicates the percentage of the overall samples collected in the corresponding function. The second column reports the process from which the samples were collected. In per-thread/per-process mode, this is always the name of the monitored command. But in cpu-wide mode, the command can vary. The third column shows the name of the ELF image where the samples came from. If a program is dynamically linked, then this may show the name of a shared library. When the samples come from the kernel, then the pseudo ELF image name [kernel.kallsyms] is used. The fourth column indicates the privilege level at which the sample was taken, i.e. when the program was running when it was interrupted:

[.] : user level

[k]: kernel level

[g]: guest kernel level (virtualization)

[u]: guest os user space

[H]: hypervisor

The final column shows the symbol name.

代码是非常复杂难说的,t1 程序中的 foo1() 也是一个潜在的调优对象,为什么要调用 100 次那个无聊的 longa() 函数呢?但我们在上图中无法发现 foo1 和 foo2,更无法了解他们的区别了。

我曾发现自己写的一个程序居然有近一半的时间花费在 string 类的几个方法上,string 是 C++ 标准,我绝不可能写出比 STL 更好的代码了。因此我只有找到自己程序中过多使用 string 的地方。因此我很需要按照调用关系进行显示的统计信息。

使用 perf 的 -g 选项便可以得到需要的信息:

perf record -g -e cpu-clock ./test1

perf report

5d2a7ed4b70279cf04bb56d6779da5ad.png

当然,这里符号表没有定位的问题有依然没有解决!

perf record的其他参数:

-f:强制覆盖产生的.data数据

-c:事件每发生count次采样一次

-p:指定进程

-t:指定线程

4.2 perf report的相关参数:

-k:指定未经压缩的内核镜像文件,从而获得内核相关信息

--report:cpu按照cpu列出负载

5.使用tracepoint

当 perf 根据 tick 时间点进行采样后,人们便能够得到内核代码中的 hot spot。那什么时候需要使用 tracepoint 来采样呢?

我想人们使用 tracepoint 的基本需求是对内核的运行时行为的关心,如前所述,有些内核开发人员需要专注于特定的子系统,比如内存管理模块。这便需要统计相关内核函数的运行情况。另外,内核行为对应用程序性能的影响也是不容忽视的:

以之前的遗憾为例,假如时光倒流,我想我要做的是统计该应用程序运行期间究竟发生了多少次系统调用。在哪里发生的?

下面我用 ls 命令来演示 sys_enter 这个 tracepoint 的使用:

[root@ovispoly /]# perf stat -e raw_syscalls:sys_enter ls

bin dbg etc  lib  media opt root

selinux sys usr

boot dev home lost+found mnt proc sbin srv

tmp var

Performance counter stats for 'ls':

101 raw_syscalls:sys_enter

0.003434730 seconds time elapsed

[root@ovispoly /]# perf record -e raw_syscalls:sys_enter ls

[root@ovispoly /]# perf report

Failed to open .lib/ld-2.12.so, continuing without symbols

# Samples: 70

#

# Overhead Command Shared Object Symbol

# ........ ............... ............... ......

#

97.14% ls ld-2.12.so [.] 0x0000000001629d

2.86% ls [vdso] [.] 0x00000000421424

#

# (For a higher level overview, try: perf report --sort comm,dso)

#

这个报告详细说明了在 ls 运行期间发生了多少次系统调用 ( 上例中有 101 次 ),多数系统调用都发生在哪些地方 (97% 都发生在 ld-2.12.so 中 )。

有了这个报告,或许我能够发现更多可以调优的地方。比如函数 foo() 中发生了过多的系统调用,那么我就可以思考是否有办法减少其中有些不必要的系统调用。

您可能会说 strace 也可以做同样事情啊,的确,统计系统调用这件事完全可以用 strace 完成,但 perf 还可以干些别的,您所需要的就是修改 -e 选项后的字符串。

罗列 tracepoint 实在是不太地道,本文当然不会这么做。但学习每一个 tracepoint 是有意义的,类似背单词之于学习英语一样,是一项缓慢痛苦却不得不做的事情。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值