動態(tài)追蹤技術(shù)(二):trace your kernel Functions!

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

掛載之后會自動創(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é)果存儲在 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() {
Linux ftrace tracers

四、體系結(jié)構(gòu)

Ftrace有兩大組成部分,framework和一系列的tracer 。每個(gè)tracer完成不同的功能,它們統(tǒng)一由framework管理。 ftrace 的trace信息保存在ring buffer中,由framework負(fù)責(zé)管理。Framework 利用debugfs建立tracing目錄,并提供了一系列的控制文件。

Linux ftrace架構(gòu)示意圖

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)核將有可能對系統(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()信號(進(jìn)程和信號詳細(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的前端工具,提供了對trace.dat的可視化分析(Graph View 、List View、Simple and Advance filtering)。


Linux ftrace KernelShark
Linux ftrace KernelShark Filters

擴(kuò)展閱讀:Linux 操作系統(tǒng)

推薦:電子書《Linux Perf Master》

發(fā)表在GitBook平臺,歡迎訂閱、下載、批評指正:
https://www.gitbook.com/book/riboseyim/linux-perf-master/details

eBook
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容