Linux内核性能分析利器之trace-cmd和kernelshark

trace-cmd是设置读取ftrace的命令行工具,kernelshark既可以记录数据,也可以图形化分析结果。

trace-cmd和kernelshark源码都由kernel.org维护在trace-cmd.git

kernekshark还有自己的帮助网站kernelshark.org

1. trace-cmd编译安装

可以通过git下载trace-cmd相关代码:

git clone https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/trace-cmd.git

trace-cmd编译安装也比较简单

make install--------------------------只安装trace-cmd

make install_gui--------------------安装trace-cmd和kernelshark

2. trace-cmd的使用

2.1 trace-cmd -h

trace-cmd version 2.5.6

usage:
trace-cmd [COMMAND] ...

commands:
record - record a trace into a trace.dat file
start - start tracing without recording into a file
extract - extract a trace from the kernel------------------------------------------------将系统当前的trace保存到trace.dat中。
stop - stop the kernel from recording trace data-------------------------------------start和stop配置使用,用于开始停止录制。
restart - restart the kernel trace data recording
show - show the contents of the kernel tracing buffer------------------------------读取/sys/kernel/debug/tracing/trace
reset - disable all kernel tracing and clear the trace buffers----------------------对ftrace的设置和ring buffer复位
report - read out the trace stored in a trace.dat file
stream - Start tracing and read the output directly----------------------------------实时在shell中显式ftrace结果
profile - Start profiling and read the output directly
hist - show a historgram of the trace.dat information-------------------------------对trace.dat显式统计信息
stat - show the status of the running tracing (ftrace) system---------------------显示当前ftrace的events、ring buffer等情况
split - parse a trace.dat file into smaller file(s)
options - list the plugin options available for trace-cmd report
listen - listen on a network socket for trace clients
list - list the available events, plugins or options-------------------------------------显示当前ftrace支持的events、tracers、options
restore - restore a crashed record
snapshot - take snapshot of running trace
stack - output, enable or disable kernel stack tracing------------------------------echo 1 > /proc/sys/kernel/stack_tracer_enabled打开stack_tracer,然后trace-cmd stack查看
check-events - parse trace event formats

trace-cmd record开始记录,ctrl+c停止记录并保存到trace.dat中。

还可以通过trace-cmd reset对各种设置进行复位,然后trace-cmd start进行录制,trace-cmd stop停止录制,trace-cmd extract将数据保存到trace.dat中。

2.2 trace-cmd record

trace-cmd record用于录制ftrace信息,通过如下选项可以指定只跟踪特定traceevents,或者跟踪特定pid、或者跟踪特定funtion/function_graph函数。

还可以设置cpumask、ringbuffer大小等等。

usage:
trace-cmd record [-v][-e event [-f filter]][-p plugin][-F][-d][-D][-o file] \
[-s usecs][-O option ][-l func][-g func][-n func] \
[-P pid][-N host:port][-t][-r prio][-b size][-B buf][command ...]
[-m max][-C clock]
-e run command with event enabled-----------------------------------------------------指定只抓取某一事件或者某一类型事件
-f filter for previous -e event
-R trigger for previous -e event
-p run command with plugin enabled
-F filter only on the given process
-P trace the given pid like -F for the command-----------------------------------------只跟踪某一个pid
-c also trace the childen of -F
-C set the trace clock
-T do a stacktrace on all events
-l filter function name
-g set graph function
-n do not trace function
-m max size per CPU in kilobytes
-M set CPU mask to trace------------------------------------------------------------------这5个选项对应ftrace的设置set_ftrace_filter、set_graph_function、set_ftrace_notrace、buffer_size_kb、tracing_cpumask。
-v will negate all -e after it (disable those events)
-d disable function tracer when running
-D Full disable of function tracing (for all users)
-o data output file [default trace.dat]-----------------------------------------------------指定输出文件名
-O option to enable (or disable)
-r real time priority to run the capture threads
-s sleep interval between recording (in usecs) [default: 1000]--------------------默认1ms保存一次数据,加大有利于将此操作频率。1000000变成1s写一次数据。
-S used with --profile, to enable only events in command line
-N host:port to connect to (see listen)
-t used with -N, forces use of tcp in live trace
-b change kernel buffersize (in kilobytes per CPU)-----------------------------------改变ring buffer大小
-B create sub buffer and folling events will be enabled here
-k do not reset the buffers after tracing.
-i do not fail if an event is not found
--profile enable tracing options needed for report --profile
--func-stack perform a stack trace for function tracer
(use with caution)

 如下表示只记录sched_switch和sched_wakeup两个时间,每1s写入一次数据。

trace-cmd record -e sched_switch -e sched_wakeup -s 1000000

一个不大不小的坑!正确发送ctrl+c

在使用trace-cmd record记录事件的时候,通过ctrl+c可以停止记录。

但是如果在adb shell中,ctrl+c可能优先退出了shell,而没有正常停止trace-cmd record。

最终在目录下只有trace.dat.cpuX的文件,这些文件是中间文件,kernelshark是无法解析的

解决方法有两种,一是在串口console中ctrl+c,另一种是通过kill发送SIGINT信号kill -2 pid。

3. kernelshark的使用

3.1 kernelshark记录事件

通过菜单栏执行Capture->Record,会弹出Capture对话框。

常用的设置有Events和Plugin,比如这里设置了sched_switch和sched_wakeup两个事件,选择了function_graph插件。

然后开始Run,停止使用Stop。

Close关闭后,可以在Kenelshark中查看细节。

可以看出这里不光显示了sched_switch和sched_wakeup事件,同时还显示了function_graph函数调用。

3.2 kernelshark分析结果

3.2.1 加载trace.dat

通过File->Load data加载trace-cmd record记录的数据,然后开始分析。

默认界面如下:

 

3.2.2 过滤功能

通常获取的数据需要进行一些过滤,才能发现问题。

filter提供了丰富的过滤功能,在File中提供了加载Load filter、保存Save filter、到处Export filter等功能。

具体在Filter中提供三类功能,分别针对事件events、进程tasks、CPU;还有两个菜单决定是否同步显式Filter结果。

task-打开某些task、关闭某些task;event-事件过滤以及高级事件过滤;cpu-开关某些cpu跟踪。

以最常用的场景,只想关注某些进程的内容:

3.4 图表扩展

横轴图表提供了两大类:CPU和进程。

cpu-每个cpu一个plot;task-可以选择不同task,一个task一个plot。

比如在Filter->tasks选定fork_rr_affinity,同时在Plots中选定fork_rr_affinity之后。

增加了4个plots,并且CUP0只显示了fork_rr_affinity相关执行情况,CPU1没有调度,说明affinity设置成功。

3.4 其它功能

还有一些其它有用的功能,比如搜索功能,选定要搜索的Column,文本框中输入字符串即可。

勾选graph follows,可以实现Plots和下面的events同步显示。

 

鼠标向右拖拉可以放大选择区域;向左拖拉则缩小当前选择区域。

向左向右调整显式区域,有助于全局或者细节查看执行情况。

4. 小结

trace-cmd作为ftrace的前端,对ftrace的各种设置进行包装,同时能对结果进行处理,极大地提高了ftrace的使用效率。

kernelshark作为trace-cmd的前端,借助图形化,灵活的filter,缩放功能,能更有效的帮助分析,高效的得到结果。

猜你喜欢

转载自www.linuxidc.com/Linux/2019-04/158309.htm