ftrace

作者: 偷油考拉 | 来源:发表于2021-09-09 11:10 被阅读0次

    ftrace.txt - kernel.org
    Ftrace - RTwiki (kernel.org)

    一、挂载文件系统

    kernel 4.1 前,使用debugfs文件系统;后面使用 tracefs文件系统

    配置方式:
    通过 /etc/fstab

    debugfs       /sys/kernel/debug          debugfs defaults        0       0
    
     tracefs       /sys/kernel/tracing       tracefs defaults        0       0
    

    通过mount挂载

    mount -t debugfs nodev /sys/kernel/debug
    
    mount -t tracefs nodev /sys/kernel/tracing
    

    为了便于访问,可以创建一个软链接

    ln -s /sys/kernel/debug /debug
    
     ln -s /sys/kernel/tracing /tracing
    

    范例:
    我的:

    [root@prod-proxy ~]# mount |grep debug
    debugfs on /sys/kernel/debug type debugfs (rw,relatime)
    

    别人的:

    # mount | grep tracefs
    none on /sys/kernel/tracing type tracefs (rw,relatime,seclabel)
    

    二、内核参数配置

    开启如下:
    CONFIG_FUNCTION_TRACER
    CONFIG_FUNCTION_GRAPH_TRACER
    CONFIG_STACK_TRACER
    CONFIG_DYNAMIC_FTRACE

    [root@prod-proxy 3.10.0-693.el7.x86_64]# cat /boot/config-3.10.0-693.el7.x86_64 |grep "CONFIG_FUNCTION_TRACER\|CONFIG_FUNCTION_GRAPH_TRACER\|CONFIG_STACK_TRACER\|CONFIG_DYNAMIC_FTRACE"
    CONFIG_FUNCTION_TRACER=y
    CONFIG_FUNCTION_GRAPH_TRACER=y
    CONFIG_STACK_TRACER=y
    CONFIG_DYNAMIC_FTRACE=y
    CONFIG_DYNAMIC_FTRACE_WITH_REGS=y
    
    [root@prod-proxy ~]# sysctl -a |grep ftrace
    kernel.ftrace_dump_on_oops = 0
    kernel.ftrace_enabled = 1
    

    三、默认配置

    [root@prod-proxy tracing]# cat trace
    # tracer: nop
    #
    # entries-in-buffer/entries-written: 0/0   #P:2
    #
    #                              _-----=> irqs-off
    #                             / _----=> need-resched
    #                            | / _---=> hardirq/softirq
    #                            || / _--=> preempt-depth
    #                            ||| /     delay
    #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
    #              | |       |   ||||       |         |
    
    [root@prod-proxy tracing]# cat tracing_on 
    1
    
    [root@prod-proxy tracing]# cat current_tracer 
    nop
    
    [root@prod-proxy tracing]# cat set_ftrace_filter 
    #### all functions enabled ####
    

    五、FUNCTION TRACE

    1. 开启
    [root@prod-proxy tracing]# echo function > current_tracer
    
    1. 关闭
    [root@prod-proxy tracing]# echo nop > current_tracer
    

    六、ftrace 过滤器

    set_ftrace_filterset_ftrace_notrace 文件,用于配置开启、关闭跟踪特定函数。

    set_ftrace_filter
    配置CONFIG_DYNAMIC_FTRACE=y,开启dynamic ftrace,代码就可以动态修改以禁止调用function profiler (mcount)。这使 tracing 几乎没有性能开销。开启、关闭要跟踪的特定功能会有些副作用。在此文件设置要跟踪的函数名,将仅跟踪这些函数。
    available_filter_functions列出了我们可以使用的函数名。
    该接口也可以使用命令。参见"Filter commands"部分。

    set_ftrace_notrace
    这与set_ftrace_filter过滤器的效果相反,此处添加的任何函数都不会被跟踪。set_ftrace_notrace优先。

    available_filter_functions
    列出了ftrace已经处理,并可以跟踪的函数名。

    stack_trace_filter
    类似于 set_ftrace_filter ,但是它限制了stack tracer会检查哪些函数。

    范例:

     # echo sys_nanosleep hrtimer_interrupt > set_ftrace_filter
     # echo function > current_tracer
     # echo 1 > tracing_on
     # usleep 1
     # echo 0 > tracing_on
     # cat trace
     # echo 'hrtimer_*' >> set_ftrace_filter
     # cat set_ftrace_filter
     # cat set_ftrace_filter
    hrtimer_run_queues
    hrtimer_run_pending
    hrtimer_init
    hrtimer_cancel
    hrtimer_try_to_cancel
    hrtimer_forward
    hrtimer_start
    hrtimer_reprogram
    hrtimer_force_reprogram
    hrtimer_get_next_event
    hrtimer_interrupt
    sys_nanosleep
    hrtimer_nanosleep
    hrtimer_wakeup
    hrtimer_get_remaining
    hrtimer_get_res
    hrtimer_init_sleeper
    

    七、过滤事件 events

    Event Tracing — The Linux Kernel documentation

    available_events
    可以tracing中可以被开启的事件列表。

    set_event
    开启事件。

    我要跟踪 keme 相关的事件:

    [root@prod-proxy tracing]# cat available_events |grep kmem
    kmem:mm_page_alloc_extfrag
    kmem:mm_page_pcpu_drain
    kmem:mm_page_alloc_zone_locked
    kmem:mm_page_alloc
    kmem:mm_page_free_batched
    kmem:mm_page_free
    kmem:kmem_cache_free
    kmem:kfree
    kmem:kmem_cache_alloc_node
    kmem:kmalloc_node
    kmem:kmem_cache_alloc
    kmem:kmalloc
    

    设置 set_event

    [root@prod-proxy tracing]# cat set_event 
    [root@prod-proxy tracing]# echo 'kmem:*' > set_event 
    

    Once you have enabled events, you can run your favourite program (in order to trigger some allocations). When you're done you may disable events (or not) and read them:

    cat /sys/kernel/debug/tracing/trace > kmem.log
    

    八、要在启动时就跟踪事件

    Boot-time tracing — The Linux Kernel documentation
    setting-kernel-parameters - RHEL7
    kernel-parameters

    编辑/etc/default/grub,增加trace_event=kmem:kmalloc,kmem:kmem_cache_alloc,kmem:kfree,kmem:kmem_cache_free

    相关文章

      网友评论

          本文标题:ftrace

          本文链接:https://www.haomeiwen.com/subject/pgtpwltx.html