linux trace学习(一)——trace配置及描述

linux trace学习(一)——trace配置及描述

备注:
  1. Kernel版本:4.19.123
  2. 使用工具:Source Insight 4.0
  3. 参考博客:
Linux ftrace框架介绍及运用
Linux ftrace 2.1、ftrace的使用

trace内核配置

trace是内核自带的工具,相比于perf工具,trace只管抓trace数据并没有分析,perf在trace数据分析方面做出了很多成果。
但是我们现在就想看一下底层多调用关系,所以使用trace抓一下数据是非常有必要的,还可以分析一下驱动性能。

因为trace工具是内核自带的,所以我们配置一下内核就可以使用了:

Kernel hacking  --->
    [*] Tracers  --->  
        --- Tracers                                                 
        [*]   Kernel Function Tracer                                
        [*]     Kernel Function Graph Tracer                        
        [*]   Enable trace events for preempt and irq disable/enable
        [*]   Interrupts-off Latency Tracer                         
        [ ]   Preemption-off Latency Tracer                         
        [ ]   Scheduling Latency Tracer                             
        [ ]   Tracer to detect hardware latencies (like SMIs)       
        [*]   Trace syscalls                                        
        -*-   Create a snapshot trace buffer                        
        -*-     Allow snapshot to swap per CPU                      
        Branch Profiling (No branch profiling)  --->          
        [ ]   Trace max stack                                       
        [ ]   Support for tracing block IO actions                  
        [*]   Enable uprobes-based dynamic events                   
        [*]   enable/disable function tracing dynamically           
        [ ]   Kernel function profiler                              
        [ ]   Perform a startup test on ftrace                      
        [ ]   Add tracepoint that benchmarks tracepoints            
        < >   Ring buffer benchmark stress tester                   
        [ ]   Ring buffer startup self test                         
        < >   Preempt / IRQ disable delay thread to test latency tracers
        [ ]   Show eval mappings for trace events                   
        [*]   Trace gpio events              

trace 通过 debugfs 向用户态提供了访问接口,所以还需要将 debugfs 编译进内核。激活对 debugfs 的支持,在 make menuconfig 时到 Kernel hacking 菜单下选中对 debugfs 文件系统的支持:

Kernel hacking  --->
    Compile-time checks and compiler options  --->
            -*- Debug Filesystem

trace文件系统挂载

自动挂载

整个trace对应的操作目录增加到/etc/fstab,在系统启动时挂载:

tracefs       /sys/kernel/tracing       tracefs defaults        0       0

手动挂载

方式一:debugfs挂载

mount -t debugfs none /sys/kernel/debug/

在4.1版本以前的,为本方式,trace目录在以下路径:

/sys/kernel/debug/tracing

方式二:tracefs挂载

mount -t tracefs nodev /sys/kernel/tracing

trace接口使用

/sys/kernel/debug/tracing目录下提供了ftrace的设置和属性接口,对ftrace的配置可以通过echo。
了解每个文件的作用和如何设置对于理解整个ftrace框架很有作用。

下面是arm32+Kernel 4.19.123的/sys/kernel/debug/tracing目录:

README------------------------一个简单的关于Tracepoing的HOWTO,cat读取,echo设置。

通用配置

available_tracers-------------当前编译及内核的跟踪器列表,current_tracer必须是这里面支持的跟踪器。
current_tracer----------------用于设置或者显示当前使用的跟踪器列表。系统启动缺省值为nop,使用echo将跟踪器名字写入即可打开。可以通过写入nop重置跟踪器。
buffer_size_kb----------------用于设置单个CPU所使用的跟踪缓存的大小。跟踪缓存为RingBuffer形式,如果跟踪太多,旧的信息会被新的跟踪信息覆盖掉。需要先将current_trace设置为nop才可以。
buffer_total_size_kb----------显示所有的跟踪缓存大小,不同之处在于buffer_size_kb是单个CPU的,buffer_total_size_kb是所有CPU的和。

free_buffer-------------------此文件用于在一个进程被关闭后,同时释放RingBuffer内存,并将调整大小到最小值。
hwlat_detector/--------------Hardware Latency Detector文件夹
instances/--------------------创建不同的trace buffer实例,可以在不同的trace buffers中分开记录。
tracing_cpumask-------------可以通过此文件设置允许跟踪特定CPU,二进制格式。
per_cpu-----------------------CPU相关的trace信息,包括stats、trace、trace_pipe和trace_pipe_raw。
                  stats:当前CPU的trace统计信息
                  trace:当前CPU的trace文件。
                  trace_pipe:当前CPU的trace_pipe文件。
printk_formats----------------提供给工具读取原始格式trace的文件。
saved_cmdlines---------------存放pid对应的comm名称作为ftrace的cache,这样ftrace中不光能显示pid还能显示comm。
saved_cmdlines_size----------saved_cmdlines的数目
snapshot----------------------是对trace的snapshot。
                                                  echo 0 清空缓存,并释放对应内存。
                                                  echo 1 进行对当前trace进行snapshot,如没有内存则分配。
                                                  echo 2 清空缓存,不释放也不分配内存。
trace--------------------------查看获取到的跟踪信息的接口,echo > trace可以清空当前RingBuffer。
trace_pipe--------------------输出和trace一样的内容,但是此文件输出Trace同时将RingBuffer中的内容删除,这样就避免了RingBuffer的溢出。可以通过cat trace_pipe > trace.txt &保存文件。
trace_clock-------------------显示当前Trace的timestamp所基于的时钟,默认使用local时钟。local:默认时钟;可能无法在不同CPU间同步;global:不同CUP间同步,但是可能比local慢;counter:这是一个跨CPU计数器,需要分析不同CPU间event顺序比较有效。
trace_marker-----------------从用户空间写入标记到trace中,用于用户空间行为和内核时间同步。
trace_marker_raw------------以二进制格式写入到trace中。
trace_options----------------控制Trace打印内容或者操作跟踪器,可以通过trace_options添加很多附加信息。
options----------------------trace选项的一系列文件,和trace_options对应。
trace_stat/-------------------每个CPU的Trace统计信息
tracing_max_latency--------记录Tracer的最大延时。例如,最大中断disable时间。
                                             最大时间被记录到本文件,对应的trace被记录到“trace”文件。
                                             新的trace最有大于现在的max latency胡,才会被记录。
                                             echo xxx > tracing_max_latency // 手工设置一个门限,只有大于门限才会被记录。
tracing_on-------------------用于控制跟踪打开或停止,0停止跟踪,1继续跟踪。
tracing_thresh---------------延时记录Trace的阈值,当延时超过此值时才开始记录Trace。单位是ms,只有非0才起作用。

Events配置

available_events-------------列出系统中所有可用的Trace events,分两个层级,用冒号隔开。
events/-----------------------系统Trace events目录,在每个events下面都有enable、filter和fotmat。enable是开关;format是events的格式,然后根据格式设置 filter。
set_event---------------------将Trace events名称直接写入set_event就可以打开。
set_event_pid----------------指定追踪特定进程的events。

Function配置

vailable_filter_functions------记录了当前可以跟踪的内核函数,不在该文件中列出的函数,无法跟踪其活动。
dyn_ftrace_total_info---------显示available_filter_functins中跟中函数的数目,两者一致。
enabled_functions------------显示有回调附着的函数名称。
function_profile_enabled-----打开此选项,在trace_stat中就会显示function的统计信息。
set_ftrace_filter----------------用于显示指定要跟踪的函数
set_ftrace_notrace-------------用于指定不跟踪的函数,缺省为空。
set_ftrace_pid------------------用于指定要追踪特定进程的函数。

Function graph配置

max_graph_depth-------------函数嵌套的最大深度。
set_graph_function------------设置要清晰显示调用关系的函数,在使用function_graph跟踪器是使用,缺省对所有函数都生成调用关系。
set_graph_notrace-------------不跟踪特定的函数嵌套调用。

Stack trace设置

stack_max_size---------------当使用stack跟踪器时,记录产生过的最大stack size
stack_trace-------------------显示stack的back trace
stack_trace_filter-------------设置stack tracer不检查的函数名称

trace功能描述

TracerDescription说明
functionFunction call tracer to trace all kernel functions.追踪所有的内核函数
function_graphSimilar to the function tracer except that the function tracer probes the functions on their entry whereas the function graph tracer traces on both entry and exit of the functions. It then provides the ability to draw a graph of function calls similar to C code source.和“function tracer”比较类似,但它除了探测函数的入口还探测函数的出口。它可以画出一个图形化的函数调用,类似于c源代码风格。
blkThe block tracer. The tracer used by the blktrace user application.块设备tracer
irqsoffTraces the areas that disable interrupts and saves the trace with the longest max latency. See tracing_max_latency. When a new max is recorded, it replaces the old trace. It is best to view this trace with the latency-format option enabled, which happens automatically when the tracer is selected.追踪最大关闭中断时间。
preemptoffSimilar to irqsoff but traces and records the amount of time for which preemption is disabled.追踪最大关闭抢占时间。
preemptirqsoffSimilar to irqsoff and preemptoff, but traces and records the largest time for which irqs and/or preemption is disabled.追踪 关闭中断 and/or 关闭抢占 的最大时间。
wakeupTraces and records the max latency that it takes for the highest priority task to get scheduled after it has been woken up. Traces all tasks as an average developer would expect.追踪最高优先级普通任务从获得调度到被唤醒的最大延迟时间。
wakeup_rtTraces and records the max latency that it takes for just RT tasks (as the current “wakeup” does). This is useful for those interested in wake up timings of RT tasks.追踪RT类型的任务从获得调度到被唤醒的最大延迟时间。
wakeup_dlTraces and records the max latency that it takes for a SCHED_DEADLINE task to be woken (as the “wakeup” and “wakeup_rt” does).追踪Deadline类型的任务从获得调度到被唤醒的最大延迟时间。
mmiotraceA special tracer that is used to trace binary module. It will trace all the calls that a module makes to the hardware. Everything it writes and reads from the I/O as well.追踪硬件IO
branchThis tracer can be configured when tracing likely/unlikely calls within the kernel. It will trace when a likely and unlikely branch is hit and if it was correct in its prediction of being correct.追踪likely/unlikely的分支预测情况
nopThis is the “trace nothing” tracer. To remove all tracers from tracing simply echo “nop” into current_tracer.空的tracer

trace输出格式

待补充。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值