最近在了解linux网络性能的问题,看到了参考[1]这篇文章。作者碰到了一个性能问题,然后内核社区的大佬指导使用perf找到了性能的瓶颈。很早以前,我记得也用perf在博通的路由器平台找出过一个性能问题,故今天记录一下。
perf的编译
perf的源码在内核tools/perf目录下面。编译方法:
$ ARCH=$(CONFIG_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) \
$(MAKE) -C $(KERNEL_SRC)/tools/perf -f Makefile.perf O=$(ODIR)
perf的使用
1 统计内核一段时间,然后打印统计结果
if CPU is 100%, perf may help us analyze your problem. If it's available, try running below while testing:
# perf record -a -g -- sleep 5
And then run this after testing:
# perf report --no-child
接下来的命令,参考2
2 执行一个命令,并统计
/tmp # perf stat ls
firmware log perf.data
Performance counter stats for 'ls':
5.94 msec task-clock # 0.632 CPUs utilized
2 context-switches # 336.679 /sec
0 cpu-migrations # 0.000 /sec
64 page-faults # 10.774 K/sec
4684633 cycles # 0.789 GHz
955157 instructions # 0.20 insn per cycle
92402 branches # 15.555 M/sec
17412 branch-misses # 18.84% of all branches
0.009404870 seconds time elapsed
0.008618000 seconds user
0.000000000 seconds sys
3 top
$ perf top -e cpu-clock
PerfTop: 701 irqs/sec kernel:77.5% exact: 0.0% lost: 0/0 drop: 0/0 [1300Hz cpu-clock], (all, 2 CPUs)
-------------------------------------------------------------------------------
3.32% perf [.] 0x000b53e8
3.07% [kernel] [k] format_decode
2.70% [kernel] [k] kallsyms_expand_symbol.constprop.0
2.33% [kernel] [k] string
2.21% [kernel] [k] __softirqentry_text_start
2.21% [kernel] [k] vsnprintf
1.97% [kernel] [k] finish_task_switch
1.84% perf [.] 0x000bf894
1.35% [kernel] [k] number
1.23% [kernel] [k] arch_counter_get_cntvct
1.23% [kernel] [k] update_iter
1.11% [kernel] [k] ___slab_alloc.constprop.0
1.11% [kernel] [k] _raw_spin_unlock_bh
0.98% [kernel] [k] memcpy
0.98% [kernel] [k] memset
0.86% [ath11k] [k] $a
0.86% [kernel] [k] _raw_spin_unlock_irqrestore
0.86% [kernel] [k] module_get_kallsym
0.74% [ath11k] [k] ath11k_update_per_peer_tx_stats
0.74% [kernel] [k] get_page_from_freelist
4 bench
/tmp # perf bench mem memcpy
# Running 'mem/memcpy' benchmark:
# function 'default' (Default memcpy() provided by glibc)
# Copying 1MB bytes ...
565.610860 MB/sec
参考
[1] https://patchwork.ozlabs.org/project/netdev/patch/73223229-6bc0-2647-6952-975961811866@gmail.com/
[2] https://www.ubuntupit.com/how-to-install-and-configure-perf-in-linux-distributions/