编辑
2025-03-03
工作知识
0
请注意,本文编写于 73 天前,最后修改于 73 天前,其中某些信息可能已经过时。

目录

一:基本环境
1.1 安装包
1.2 内核工程
1.3 火焰图
1.4 编译
二:基本使用
2.1 用户使用perf的权限
2.2 去掉内核指针打印保护
2.3 查看版本
2.4 实时分析
2.5 支持的事件
2.5.1 关于事件类型的解释
2.6 统计信息
2.7 收集perf数据
2.8 分析perf数据
2.9 火焰图
2.10 简单分析

Perf是Linux内核自带的调试工具,在分析Linux系统的性能的时候,需要用到这个工具。下面从编译和基本使用的角度来了解perf

一:基本环境

1.1 安装包

apt install binutils elfutils libasm1 zlib1g

1.2 内核工程

git clone http://10.3.4.182/arm-embedded/stable-4.4.194-rk3399-linux.git (这里只要是你板子上匹配的内核即可)

1.3 火焰图

git clone https://github.com/brendangregg/FlameGraph.git

1.4 编译

cd tools/perf/ && ARCH=arm64 WERROR=0 NO_LIBPERL=1 NO_LIBPYTHON=1 make -f Makefile.perf perf

perf建议在arm上编译,交叉编译容易出问题

二:基本使用

2.1 用户使用perf的权限

echo -1 > /proc/sys/kernel/perf_event_paranoid

2.2 去掉内核指针打印保护

echo 0 > /proc/sys/kernel/kptr_restrict

2.3 查看版本

kylin@kylin:~$ ./perf -v perf version 4.4.194.g9ca7d7

2.4 实时分析

./perf top -d 2 -p 528

2.5 支持的事件

./perf list

2.5.1 关于事件类型的解释

Hardware [Cache] Events: These instrument low-level processor activity based on CPU performance counters. For example, CPU cycles, instructions retired, memory stall cycles, level 2 cache misses, etc. Some will be listed as Hardware Cache Events. Software Events: These are low level events based on kernel counters. For example, CPU migrations, minor faults, major faults, etc. Tracepoint Events: This are kernel-level events based on the ftrace framework. These tracepoints are placed in interesting and logical locations of the kernel, so that higher-level behavior can be easily traced. For example, system calls, TCP events, file system I/O, disk I/O, etc. These are grouped into libraries of tracepoints; eg, "sock:" for socket events, "sched:" for CPU scheduler events. Dynamic Tracing: Software can be dynamically instrumented, creating events in any location. For kernel software, this uses the kprobes framework. For user-level software, uprobes. Timed Profiling: Snapshots can be collected at an arbitrary frequency, using perf record -FHz. This is commonly used for CPU usage profiling, and works by creating custom timed interrupt events.

image.png

2.6 统计信息

./perf stat -p 528

2.7 收集perf数据

./perf record -g -p 528 -o perf.data

2.8 分析perf数据

./perf report -i perf.data

2.9 火焰图

./perf script -i perf.data | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > flame.html

image.png

2.10 简单分析

perf record -a -g -p 1 perf report -g --tui