Ftrace是一個(gè)設(shè)計(jì)用來幫助開發(fā)者和設(shè)計(jì)者監(jiān)視內(nèi)核的追蹤器矫膨,可用于調(diào)試或分析延遲以及性能問題响逢。ftrace令人印象最深刻的是作為一個(gè)function tracer躺同,內(nèi)核函數(shù)調(diào)用、耗時(shí)等情況一覽無余士聪。另外,ftrace最常見的用途是事件追蹤猛蔽,通過內(nèi)核是成百上千的靜態(tài)事件點(diǎn)剥悟,看到系統(tǒng)內(nèi)核的哪些部分在運(yùn)行。實(shí)際上曼库,ftrace更是一個(gè)追蹤框架区岗,它具備豐富工具集:延遲跟蹤檢查、何時(shí)發(fā)生中斷毁枯、任務(wù)的啟用慈缔、禁用及搶占等。在ftrace的基線版本之上种玛,還有很多第三方提供的開源工具胀糜,用于簡化操作或者提供數(shù)據(jù)可視化等擴(kuò)展應(yīng)用。
一蒂誉、Introduction
Developer(s): Steven Rostedt(RedHat) and others
Initial release: October 9, 2008;
Operating system: Linux (merged into the Linux kernel mainline in kernel version 2.6.27)
Type: Kernel extension
License: GNU GPL
Website: www.kernel.org/doc/Documentation/trace
二教藻、ABC
在使用ftrace之前,需要確認(rèn)調(diào)試目錄是否已經(jīng)掛載右锨,默認(rèn)目錄:/sys/kernel/debug/ 括堤。
debugfs是Linux內(nèi)核中一種特殊的文件系統(tǒng),非常易用绍移、基于RAM悄窃,專門設(shè)計(jì)用于調(diào)試。(since version 2.6.10-rc3蹂窖,https://en.wikipedia.org/wiki/Debugfs)轧抗。
mount -t debugfs none /sys/kernel/debug
或者指定到自己的目錄
mkdir /debug
mount -t debugfs nodev /debug
掛載之后會(huì)自動(dòng)創(chuàng)建如下文件:
/sys/kernel/debug# ls -lrt
drwxr-xr-x. 2 root root 0 12月 28 17:24 x86
drwxr-xr-x. 3 root root 0 12月 28 17:24 boot_params
drwxr-xr-x. 34 root root 0 12月 28 17:24 bdi
-r--r--r--. 1 root root 0 12月 28 17:24 gpio
drwxr-xr-x. 3 root root 0 12月 28 17:24 usb
drwxr-xr-x. 4 root root 0 12月 28 17:24 xen
drwxr-xr-x. 6 root root 0 12月 28 17:24 tracing
drwxr-xr-x. 2 root root 0 12月 28 17:24 extfrag
drwxr-xr-x. 2 root root 0 12月 28 17:24 dynamic_debug
drwxr-xr-x. 2 root root 0 12月 28 17:24 hid
-rw-r--r--. 1 root root 0 12月 28 17:24 sched_features
drwxr-xr-x. 2 root root 0 12月 28 17:24 mce
drwxr-xr-x. 2 root root 0 12月 28 17:24 kprobes
-r--r--r--. 1 root root 0 12月 28 17:24 vmmemctl
/sys/kernel/debug/tracing# ls -lrt
-rw-r--r--. 1 root root 0 12月 28 17:24 tracing_thresh
-rw-r--r--. 1 root root 0 12月 28 17:24 tracing_on
-rw-r--r--. 1 root root 0 12月 28 17:24 tracing_max_latency
-rw-r--r--. 1 root root 0 12月 28 17:24 tracing_enabled
-rw-r--r--. 1 root root 0 12月 28 17:24 tracing_cpumask
drwxr-xr-x. 2 root root 0 12月 28 17:24 trace_stat
-r--r--r--. 1 root root 0 12月 28 17:24 trace_pipe
-rw-r--r--. 1 root root 0 12月 28 17:24 trace_options
--w--w----. 1 root root 0 12月 28 17:24 trace_marker
-rw-r--r--. 1 root root 0 12月 28 17:24 trace_clock
-rw-r--r--. 1 root root 0 12月 28 17:24 trace
-rw-r--r--. 1 root root 0 12月 28 17:24 sysprof_sample_period
-r--r--r--. 1 root root 0 12月 28 17:24 set_graph_function
-rw-r--r--. 1 root root 0 12月 28 17:24 set_ftrace_pid
-rw-r--r--. 1 root root 0 12月 28 17:24 set_ftrace_notrace
-r--r--r--. 1 root root 0 12月 28 17:24 saved_cmdlines
-r--r--r--. 1 root root 0 12月 28 17:24 README
drwxr-xr-x. 2 root root 0 12月 28 17:24 options
-rw-r--r--. 1 root root 0 12月 28 17:24 function_profile_enabled
-r--r--r--. 1 root root 0 12月 28 17:24 dyn_ftrace_total_info
-rw-r--r--. 1 root root 0 12月 28 17:24 buffer_size_kb
-r--r--r--. 1 root root 0 12月 28 17:24 available_tracers
-r--r--r--. 1 root root 0 12月 28 17:24 available_filter_functions
-rw-r--r--. 1 root root 0 12月 28 17:24 set_event
-r--r--r--. 1 root root 0 12月 28 17:24 printk_formats
drwxr-xr-x. 34 root root 0 12月 28 17:24 per_cpu
drwxr-xr-x. 24 root root 0 12月 28 17:24 events
-r--r--r--. 1 root root 0 12月 28 17:24 available_events
-r--r--r--. 1 root root 0 12月 28 17:24 kprobe_profile
-rw-r--r--. 1 root root 0 12月 28 17:24 kprobe_events
-r--r--r--. 1 root root 0 12月 28 17:24 stack_trace
-rw-r--r--. 1 root root 0 12月 28 17:24 stack_max_size
-rw-r--r--. 1 root root 0 5月 31 11:50 current_tracer
-rwxr-xr-x. 1 root root 0 5月 31 11:57 set_ftrace_filter
三、BASIC
1. Function tracer
以Function tracer為例瞬测,結(jié)果存儲(chǔ)在 trace横媚,該文件類似一張報(bào)表纠炮,該表將顯示 4 列信息。首先是進(jìn)程信息灯蝴,包括進(jìn)程名和PID 恢口;第二列是CPU;第三列是時(shí)間戳穷躁;第四列是函數(shù)信息耕肩,缺省情況下,這里將顯示內(nèi)核函數(shù)名以及它的上一層調(diào)用函數(shù)问潭。
cd /sys/kernel/debug/tracing
echo function > current_tracer
cat trace
# tracer: function
#
# TASK-PID CPU# TIMESTAMP FUNCTION
# | | |
gmond-6684 [004] 13285965.088308: _spin_lock <-hrtimer_interrupt
gmond-6684 [004] 13285965.088308: ktime_get_update_offsets <-hrtimer_interrupt
gmond-6684 [004] 13285965.088309: __run_hrtimer <-hrtimer_interrupt
gmond-6684 [004] 13285965.088309: __remove_hrtimer <-__run_hrtimer
gmond-6684 [004] 13285965.088309: tick_sched_timer <-__run_hrtimer
gmond-6684 [004] 13285965.088309: ktime_get <-tick_sched_timer
gmond-6684 [004] 13285965.088310: tick_do_update_jiffies64 <-tick_sched_timer
gmond-6684 [004] 13285965.088310: update_process_times <-tick_sched_timer
2. Function graph tracer
Function graph tracer 和 function tracer 類似猿诸,但輸出為函數(shù)調(diào)用圖,更加容易閱讀:
# tracer: function_graph
#
# TIME CPU DURATION FUNCTION CALLS
# | | | | | | | |
21) ==========> |
21) | smp_apic_timer_interrupt() {
31) ==========> |
31) | smp_apic_timer_interrupt() {
8) | smp_apic_timer_interrupt() {
11) 2.598 us | native_apic_mem_write();
18) 3.106 us | native_apic_mem_write();
30) ==========> |
30) | smp_apic_timer_interrupt() {
3) 3.590 us | native_apic_mem_write();
22) 2.944 us | native_apic_mem_write();
7) 3.392 us | native_apic_mem_write();
17) ==========> |
17) | smp_apic_timer_interrupt() {
27) ==========> |
27) | smp_apic_timer_interrupt() {
16) ==========> |
16) | smp_apic_timer_interrupt() {
四狡忙、體系結(jié)構(gòu)
Ftrace有兩大組成部分两芳,framework和一系列的tracer 。每個(gè)tracer完成不同的功能去枷,它們統(tǒng)一由framework管理怖辆。 ftrace 的trace信息保存在ring buffer中,由framework負(fù)責(zé)管理删顶。Framework 利用debugfs建立tracing目錄竖螃,并提供了一系列的控制文件。
ftrace is a dynamic tracing system. 當(dāng)你開始“ftracing”一個(gè)內(nèi)核函數(shù)的時(shí)候逗余,該函數(shù)的代碼實(shí)際上就已經(jīng)發(fā)生變化了特咆。內(nèi)核將在程序集中插入一些額外的指令,使得函數(shù)調(diào)用時(shí)可以隨時(shí)通知追蹤程序录粱。
WARNNING:使用ftrace追蹤內(nèi)核將有可能對(duì)系統(tǒng)性能產(chǎn)生影響腻格,追蹤的函數(shù)越多,開銷越大啥繁。
使用者必須提前做好準(zhǔn)備工作菜职,生產(chǎn)環(huán)境必須謹(jǐn)慎使用。
#cat available_tracers //查看支持的tracers
blk kmemtrace function_graph wakeup_rt wakeup function sysprof sched_switch initcall nop
五旗闽、Useful Tools
1. trace-cmd
trace-cmd是一個(gè)非常有用的Ftrace命令行工具酬核。
sudo apt-get install trace-cmd
或者
git clone git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/trace-cmd.git
使用方法:
sudo trace-cmd record --help #help
sudo trace-cmd record -p function -P 123456 #record for PID
sudo trace-cmd record -p function -l do_page_fault #record for function
plugin 'function'
Hit Ctrl^C to stop recording
trace.dat
$ sudo trace-cmd report
chrome-15144 [000] 11446.466121: function: do_page_fault
chrome-15144 [000] 11446.467910: function: do_page_fault
chrome-15144 [000] 11446.469174: function: do_page_fault
chrome-15144 [000] 11446.474225: function: do_page_fault
chrome-15144 [000] 11446.474386: function: do_page_fault
chrome-15144 [000] 11446.478768: function: do_page_fault
CompositorTileW-15154 [001] 11446.480172: function: do_page_fault
chrome-1830 [003] 11446.486696: function: do_page_fault
CompositorTileW-15154 [001] 11446.488983: function: do_page_fault
CompositorTileW-15154 [001] 11446.489034: function: do_page_fault
CompositorTileW-15154 [001] 11446.489045: function: do_page_fault
在很有情況下不能使用函數(shù)追蹤,需要依賴 事件追蹤 的支持适室,例如:
# cat available_events //查看支持的事件類型
power:power_start
power:power_frequency
power:power_end
sched:sched_kthread_stop
sched:sched_kthread_stop_ret
sched:sched_wait_task
sched:sched_wakeup
sched:sched_wakeup_new
sched:sched_switch
sched:sched_migrate_task
sched:sched_process_free
sched:sched_process_exit
sched:sched_process_wait
sched:sched_process_fork
sched:sched_stat_wait
sched:sched_stat_sleep
sched:sched_stat_iowait
sched:sched_stat_runtime
sudo trace-cmd record -e sched:sched_switch
sudo trace-cmd report
輸出如下:
16169.624862: Chrome_ChildIOT:24817 [112] S ==> chrome:15144 [120]
16169.624992: chrome:15144 [120] S ==> swapper/3:0 [120]
16169.625202: swapper/3:0 [120] R ==> Chrome_ChildIOT:24817 [112]
16169.625251: Chrome_ChildIOT:24817 [112] R ==> chrome:1561 [112]
16169.625437: chrome:1561 [112] S ==> chrome:15144 [120]
切換路徑:PID 24817 -> 15144 -> kernel -> 24817 -> 1561 -> 15114嫡意。
2. perf-tools
perf-tools 是性能調(diào)試大神Brendan Gregg開發(fā)的一個(gè)工具包,提供了很多強(qiáng)大的功能捣辆,例如:
iosnoop: 磁盤I/O分析詳細(xì)包括延遲
iolatency: 磁盤I/O分析概要(柱狀圖)
execsnoop: 追蹤進(jìn)程exec()
opensnoop: 追蹤open()系統(tǒng)調(diào)用蔬螟,包含文件名
killsnoop: 追蹤kill()信號(hào)(進(jìn)程和信號(hào)詳細(xì))
代碼下載:https://github.com/brendangregg/perf-tools
# ./execsnoop //顯示新進(jìn)程和參數(shù):
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
# ./iolatency -Q //測量設(shè)備I/O延遲
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 |# |
[...]
六、可視化工具:KernelShark
KernelShark是trace-cmd的前端工具汽畴,提供了對(duì)trace.dat的可視化分析(Graph View 旧巾、List View耸序、Simple and Advance filtering)。
擴(kuò)展閱讀:Linux 操作系統(tǒng)
- 《Linus Torvalds:Just for Fun》
- Linux 常用命令一百條
- Linux 性能診斷:負(fù)載評(píng)估
- Linux 性能診斷:快速檢查單(Netflix版)
- Linux 性能診斷:薦書|《圖解性能優(yōu)化》
- Linux 性能診斷:Web應(yīng)用性能優(yōu)化
- 操作系統(tǒng)原理 | How Linux Works(一):How the Linux Kernel Boots
- 操作系統(tǒng)原理 | How Linux Works(二):User Space & RAM
- 操作系統(tǒng)原理 | How Linux Works(三):Memory
推薦:電子書《Linux Perf Master》
發(fā)表在GitBook平臺(tái)菠齿,歡迎訂閱、下載坐昙、批評(píng)指正:
https://www.gitbook.com/book/riboseyim/linux-perf-master/details