#linux ftrace 简介 [linuxcon2020 ftrace]: https://events.static.linuxfound.org/slides/2010/linuxcon_japan/linuxcon_jp2010_rostedt.pdf "ftrace" # ftrace背景 ftrace 是 Function Trace的意思, 最开始主要用于记录内核函数的运行轨迹 随着功能的逐渐增加,演变成一个用于trace的framework. ## static tracepoints static tracepoints within the kernel (event tracing) scheduling interrupts file systems ... ## dynamic kernel funciton tracing trace all functions within the kernel pick and choose what function to trace (less functions, less overhead) call graph stack usage ## Latency Tracer how long interrupts are disabled how long preemption is disabled how long interrupts and/or preemption is disable ## Wakeup latency how long it takes a process to run after it is woken ftrace的帮助文档在 Documentation/trace ftrace的代码在 kernel/trace ftrace的作者在LinuxCon有一篇关于ftrace的slides ![Ftrace Linux Kernel Tracing][linuxcon2020 ftrace] # ftrace的配置和使用 ftrace通过debugfs文件系统向用户空间提供访问接口,因此要确保挂载上debugfs /sys/kernel/debug/tracing目录下提供了各种跟踪器(tracer)和event事件 ## 通用配置 ### trace The static contents of the buffer, 保存trace到的信息 `echo > trace` 可以清空buffer ### trace_pipe 和trace类似,可查看trace到的内容 但是输出trace的同时,会将输出的信息从buffer中删除 ### available_tracers 列出支持的tracer (跟踪器) ---- | TRACER | description | | - | - | | function | 跟踪内核函数执行情况 | | function_graph | 可以显示函数调用关系图 | | wakeup | 跟踪进程唤醒信息 | | wakeup_rt | | | wakeup_dl | | | nop | 不跟踪任何信息,将nop写入current_tracer | | | 可以清空之前收集到的tracer信息 | | mmiotrace | | | blk | | ---- ### current_tracer 设置和显示当前正在使用的tracer, 默认为nop, 表示不做任何跟踪操作 ### trace_clock 当前trace的timestamp的时钟,默认使用local时钟 | trace_clock | description | | - | - | | local | Per cpu clock but may not be synced across CPUs | | | 可能无法在不同cpu间同步 | | global | Synced across CPUs but slows tracing down. | | | 支持在不同cpu间同步 | | counter | Not a clock, but just an increment | | | 跨CPU计数器,对于分析不同cpu间的event顺序比较有效 | | uptime | Jiffy counter from time of boot | | perf | Same clock that perf events use | | x86-tsc | TSC cycle counter | ### tracing_on 控制tracer打开或停止 > echo 0 > tracing_on : quick way to disable tracing > echo 1 > tracing_on : quick way to re-enable tracing