動態追蹤技術:trace your kernel Functions!

摘自https://zhuanlan.zhihu.com/p/27190018
動態追蹤技術:trace your kernel Functions!
RiboseYim
RiboseYim
Engineer & Writer
28 人贊同了該文章
Ftrace是一個設計用來幫助開發者和設計者監視內核的追蹤器,可用於調試或分析延遲以及性能問題。ftrace令人印象最深刻的是作爲一個function tracer,內核函數調用、耗時等情況一覽無餘。另外,ftrace最常見的用途是事件追蹤,通過內核是成百上千的靜態事件點,看到系統內核的哪些部分在運行。實際上,ftrace更是一個追蹤框架,它具備豐富工具集:延遲跟蹤檢查、何時發生中斷、任務的啓用、禁用及搶佔等。在ftrace的基線版本之上,還有很多第三方提供的開源工具,用於簡化操作或者提供數據可視化等擴展應用。

一、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之前,需要確認調試目錄是否已經掛載,默認目錄:/sys/kernel/debug/ 。

debugfs是Linux內核中一種特殊的文件系統,非常易用、基於RAM,專門設計用於調試。(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
掛載之後會自動創建如下文件:

/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爲例,結果存儲在 trace,該文件類似一張報表,該表將顯示 4 列信息。首先是進程信息,包括進程名和PID ;第二列是CPU;第三列是時間戳;第四列是函數信息,缺省情況下,這裏將顯示內核函數名以及它的上一層調用函數。

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 類似,但輸出爲函數調用圖,更加容易閱讀:

tracer: function_graph

TIME CPU DURATION FUNCTION CALLS

| | | | | | | |

  1. ==========> |
  2.           |  smp_apic_timer_interrupt() {
    
  3. ==========> |
  4.           |  smp_apic_timer_interrupt() {
    
  5.           |  smp_apic_timer_interrupt() {
    
  6. 2.598 us | native_apic_mem_write();
  7. 3.106 us | native_apic_mem_write();
  8. ==========> |
  9.           |  smp_apic_timer_interrupt() {
    
  10. 3.590 us | native_apic_mem_write();
  11. 2.944 us | native_apic_mem_write();
  12. 3.392 us | native_apic_mem_write();
  13. ==========> |
  14.           |  smp_apic_timer_interrupt() {
    
  15. ==========> |
  16.           |  smp_apic_timer_interrupt() {
    
  17. ==========> |
  18.           |  smp_apic_timer_interrupt() {
    

http://og2061b3n.bkt.clouddn.com/DTrace_ftrace_tracers.png

四、體系結構
Ftrace有兩大組成部分,framework和一系列的tracer 。每個tracer完成不同的功能,它們統一由framework管理。 ftrace 的trace信息保存在ring buffer中,由framework負責管理。Framework 利用debugfs建立tracing目錄,並提供了一系列的控制文件。

http://og2061b3n.bkt.clouddn.com/DTrace_ftrace_arch.png
ftrace is a dynamic tracing system. 當你開始“ftracing”一個內核函數的時候,該函數的代碼實際上就已經發生變化了。內核將在程序集中插入一些額外的指令,使得函數調用時可以隨時通知追蹤程序。

WARNNING:使用ftrace追蹤內核將有可能對系統性能產生影響,追蹤的函數越多,開銷越大。
使用者必須提前做好準備工作,生產環境必須謹慎使用。

#cat available_tracers //查看支持的tracers
blk kmemtrace function_graph wakeup_rt wakeup function sysprof sched_switch initcall nop
五、Useful Tools

  1. trace-cmd
    trace-cmd是一個非常有用的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
在很有情況下不能使用函數追蹤,需要依賴 事件追蹤 的支持,例如:

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。

  1. perf-tools
    perf-tools 是性能調試大神Brendan Gregg開發的一個工具包,提供了很多強大的功能,例如:
    iosnoop: 磁盤I/O分析詳細包括延遲
    iolatency: 磁盤I/O分析概要(柱狀圖)
    execsnoop: 追蹤進程exec()
    opensnoop: 追蹤open()系統調用,包含文件名
    killsnoop: 追蹤kill()信號(進程和信號詳細)

代碼下載:brendangregg/perf-tools

./execsnoop //顯示新進程和參數:

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 //測量設備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的前端工具,提供了對trace.dat的可視化分析(Graph View 、List View、Simple and Advance filtering)。

https://static.lwn.net/images/2011/ks-success-zoom-task.png
編輯於 2017-06-01

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章