ftrace(一)

Ftrace相对于其他的工具,比如Dtrace/systemTap来说,是一种轻量级的trace工具,利用静态代码插装技术来实现,实现更加可靠,以一种友好 的可视化方式来进行输出,支持ASCII。相对于其他的工具,它更加受内核开发者推崇,目前已经合入的内核主线。它的实现也依赖很多其他内核特性,比如 tracepoint,debugfs,kprobe等。

(1)debugfs

debugfs编译:

CONFIG_DEBUG_FS=y

Ftrace使用debugfs来操作tracer和更新trace信息,所以内核必须配置使能debugfs,上面kernel 配置项会把debugfs的支持编译进内核中,那么光编译还不行,还要在系统起来之后挂载debugfs。

debugfs挂载:

配置etc/fstable文件:

debugfs       /sys/kernel/debug          debugfs defaults        0       0

或者使用命令:

mount -t debugfs nodev /sys/kernel/debug

(2)ftrace

make menuconfig后选择如下:

  Kernel hacking ---> Tracers

当以上选项被选上之后可以进一步选择需要编译到内核的Tracer:

--- Tracers                                          
[ ]   Kernel Function Tracer                         
[ ]   Interrupts-off Latency Tracer                  
[ ]   Preemption-off Latency Tracer                  
[ ]   Scheduling Latency Tracer                      
[ ]   Trace process context switches and events (NEW)
[ ]   Trace syscalls                                 
[ ]   Create a snapshot trace buffer                 
      Branch Profiling (No branch profiling)  --->   
[ ]   Trace max stack                                
[ ]   Support for tracing block IO actions           
[ ]   Enable uprobes-based dynamic events            
[ ]   Memory mapped IO tracing                       
[ ]   Add tracepoint that benchmarks tracepoints  

当我们配置了上面的Tracer后,重新编译内核并运行,会在 debugfs 下创建一个 tracing 目录:/sys/kernel/debug/tracing。

下面是我的内核编译后生成的tracing目录:

/sys/kernel/debug/tracing # ls
README                      saved_cmdlines_size
available_events            set_event
available_filter_functions  set_ftrace_filter
available_tracers           set_ftrace_notrace
buffer_size_kb              set_ftrace_pid
buffer_total_size_kb        set_graph_function
current_tracer              set_graph_notrace
dyn_ftrace_total_info       snapshot
enabled_functions           trace
events                      trace_clock
free_buffer                 trace_marker
instances                   trace_options
max_graph_depth             trace_pipe
options                     tracing_cpumask
per_cpu                     tracing_max_latency
printk_formats              tracing_on
saved_cmdlines              tracing_thresh

可以通过cat README获取mini HOWTO使用说明。下面也简单介绍一下关键的节点介绍:

  • current_tracer
    用于设置和显示当前的tracer,系统默认为nop

  • available_tracers
    用于显示当前编译到内核中的tracer,可以配置到current_tracer来使用

  • tracing_on
    用于使能和关闭tracing的开关:
    echo 0 > tracing_on : 关闭tracing
    echo 1 > tracing_on : 使能tracing

  • trace
    trace信息输出接口,从trace ring buffer中获取

  • trace_pipe
    也是trace信息输出接口,是一种流式输出格式,和上面的区别是读取的信息会被丢掉,不会每次读取重复输出。

  • trace_options
    这个文件节点是用来设置trace特性的,比如输出格式或者trace方式,其中的选项是以使能和禁止方式可选的。
    如果想要禁止某个属性,只需要在属性名前加”no”前缀,并且echo到trace_options中即可设置成功。
    下面列了几个比较常用的options的说明:

    markers - When set, the trace_marker is writable (only by root).
        When disabled, the trace_marker will error with EINVAL
        on write.
    function-trace - The latency tracers will enable function tracing
        if this option is enabled (default it is). When
        it is disabled, the latency tracers do not trace
        functions. This keeps the overhead of the tracer down
        when performing latency tests.
    Note: Some tracers have their own options. They only appear
       when the tracer is active.
  • options
    和上面的功能一样,但是是一种目录的形式来体现的,包含的属性都会在此目录中生成一个文件节点,使能和禁止的方式,是直接echo 1或者echo 0到对应属性文件中即可,设置效果和上面的一样。

  • buffer_size_kb
    查看和设置per cpu buffer大小

  • buffer_total_size_kb
    查看所有的 all cpu buffers大小

  • tracing_cpumask
    设置指定的trace CPU,是以HEX输出的一种格式,一个bit代表一个CPU。

  • set_ftrace_filter
    写函数名到此文件,将只会trace对应function的功能。

  • set_ftrace_notrace
    和上面的相反,写函数名进去会屏蔽特定函数的trace功能。

  • available_filter_functions
    显示可以设置给set_ftrace_filter或者set_ftrace_notrace的函数名

  • trace_marker
    这是一个非常常用的一个节点功能,目的就是可以写一个标记到trace buff中,这样可以通过查找对应的标记来确定发生的事件开始和结束时间。

  • set_ftrace_pid
    写入pids来只追踪特定pids的trace信息。

  • tracing_max_latency
    最大延迟(us),一些tracers会记录最大延迟信息,当然这个信息会在”trace”文件的输出中也会体现出来。
    需要注意的是,这个文件只会记录最大值,所以每次我们在开始一次trace时都要echo 0进去清空之间的记录。

  • tracing_thresh
    一些延迟的tracers类型会记录延迟时间信息 ,这个可设置时间阈值,时间大于此值才会被记录(us)

  • trace_clock
    用于event记录的clock类型,一个event的记录都会带有timestamp,那么此时间是取自那种clock就是在该文件中配置的。

          local:   Per cpu clock but may not be synced across CPUs
          global:   Synced across CPUs but slows tracing down.
          counter:   Not a clock, but just an increment
          uptime:   Jiffy counter from time of boot
          perf:   Same clock that perf events use
    
  • triggers
    当function执行时会触发一个命令。

    Format: <function>:<trigger>[:count](count代表function命中多少次才触发该命令)
    
    trigger: traceon, traceoff
          enable_event:<system>:<event>
          disable_event:<system>:<event>
          stacktrace
          snapshot
          dump
          cpudump
  • set_graph_function
    Trace the nested calls of a function (function_graph)

  • set_graph_notrace
    Do not trace the nested calls of a function (function_graph)

  • events
    是一个目录,它包含了event trace所要配置的相关信息,包括各个子系统,以及enable/trigger等等的配置选项。
    以上就是tracing相关的各个目录的介绍,可能不够全面,因为随着内核的不断更新,会不停的有新的trace特性加入进来。

猜你喜欢

转载自blog.csdn.net/rikeyone/article/details/80107821