本文目录

  • Ftrace简介
  • Ftrace案例
  • Ftrace结果怎么读?
  • vim进行Ftrace折叠

Ftrace简介

Ftrace是Linux进行代码级实践分析最有效的工具之一,比如我们进行一个系统调用,出来的时间过长,我们想知道时间花哪里去了,利用Ftrace就可以追踪到一级级的时间分布。

Ftrace案例

写一个proc模块,包含一个proc的读和写的入口。test_proc_show()故意调用了一个kill_time()的函数,而kill_time()的函数,又调用了mdelay(2)和kill_moretime()的函数,该函数体内调用mdelay(2)。

kill_time()的函数和kill_moretime()函数前面都加了noinline以避免被编译器inline优化掉。

#include#include#include#include#include#include#include#includestatic unsigned int variable;static struct proc_dir_entry *test_dir, *test_entry;static noinline void kill_moretime(void){mdelay(2);}static noinline void kill_time(void){mdelay(2);kill_moretime();}static int test_proc_show(struct seq_file *seq, void *v){unsigned int *ptr_var = seq->private;kill_time();seq_printf(seq, "%u\n", *ptr_var);return 0;}static ssize_t test_proc_write(struct file *file, const char __user *buffer,size_t count, loff_t *ppos){struct seq_file *seq = file->private_data;unsigned int *ptr_var = seq->private;int err;char *kbuffer;if (!buffer || count > PAGE_SIZE - 1)return -EINVAL;kbuffer = (char *)__get_free_page(GFP_KERNEL);if (!kbuffer)return -ENOMEM;err = -EFAULT;if (copy_from_user(kbuffer, buffer, count))goto out;kbuffer[count] = '\0';*ptr_var = simple_strtoul(kbuffer, NULL, 10);return count;out:free_page((unsigned long)buffer);return err;}static int test_proc_open(struct inode *inode, struct file *file){return single_open(file, test_proc_show, PDE_DATA(inode));}static const struct file_operations test_proc_fops ={.owner = THIS_MODULE,.open = test_proc_open,.read = seq_read,.write = test_proc_write,.llseek = seq_lseek,.release = single_release,};static __init int test_proc_init(void){test_dir = proc_mkdir("test_dir", NULL);if (test_dir) {test_entry = proc_create_data("test_rw",0666, test_dir, &test_proc_fops, &variable);if (test_entry)return 0;}return -ENOMEM;}module_init(test_proc_init);static __exit void test_proc_cleanup(void){remove_proc_entry("test_rw", test_dir);remove_proc_entry("test_dir", NULL);}module_exit(test_proc_cleanup);MODULE_AUTHOR("Barry Song ");MODULE_DESCRIPTION("proc exmaple");MODULE_LICENSE("GPL v2");

模块对应的Makefile如下:

KVERS = $(shell uname -r)# Kernel modulesobj-m += proc.o# Specify flags for the module compilation.#EXTRA_CFLAGS=-g -O0build: kernel_moduleskernel_modules:make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modulesclean:make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

编译并且加载:

$ make

baohua@baohua-perf:~/develop/training/debug/ftrace/proc$

$ sudo insmod proc.ko

[sudo] password for baohua:

之后/proc目录下/proc/test_dir/test_rw文件可被读写。

下面我们用Ftrace来跟踪test_proc_show()这个函数。

我们把启动ftrace的所有命令写到一个脚本function.sh里面:

#!/bin/bashdebugfs=/sys/kernel/debugecho nop > $debugfs/tracing/current_tracerecho 0 > $debugfs/tracing/tracing_onecho $$ > $debugfs/tracing/set_ftrace_pidecho function_graph > $debugfs/tracing/current_tracer#replace test_proc_show by your function nameecho test_proc_show > $debugfs/tracing/set_graph_functionecho 1 > $debugfs/tracing/tracing_onexec "$@"

然后用这个脚本去启动cat /proc/test_dir/test_rw,这样ftrace下面test_proc_show()函数就被trace了。

# ./function.sh cat /proc/test_dir/test_rw

0

读取trace的结果:

# cat /sys/kernel/debug/tracing/trace > 1

接着用vim打开这个文件1,发现这个文件有600多行:

天了撸,长到看不清!!

Ftrace结果怎么读?

Ftrace结果怎么读?答案非常简单:如果是叶子函数,就直接在这个函数的前面显示它占用的时间,如果是非叶子,要等到 }的时候,再显示时间,如下图:

延迟比较大的部分,会有+、#等特殊标号:

'$' - greater than 1 second
 '@' - greater than 100 milisecond
 '*' - greater than 10 milisecond
 '#' - greater than 1000 microsecond
 '!' - greater than 100 microsecond
 '+' - greater than 10 microsecond
 ' ' - less than or equal to 10 microsecond.

vim对Ftrace进行折叠

上面那个Ftrace文件太大了,大到看不清。我们可以用vim来折叠之,不过需要一个vim的特别配置,我把它存放在了我的~目录,名字叫.fungraph-vim:

" Enable folding for ftrace function_graph traces.

"

" To use, :source this file while viewing a function_graph trace, or use vim's

" -S option to load from the command-line together with a trace.  You can then

" use the usual vim fold commands, such as "za", to open and close nested

" functions.  While closed, a fold will show the total time taken for a call,

" as would normally appear on the line with the closing brace.  Folded

" functions will not include finish_task_switch(), so folding should remain

" relatively sane even through a context switch.

"

" Note that this will almost certainly only work well with a

" single-CPU trace (e.g. trace-cmd report --cpu 1).

function! FunctionGraphFoldExpr(lnum)

let line = getline(a:lnum)

if line[-1:] == '{'

if line =~ 'finish_task_switch() {$'

return '>1'

endif

return 'a1'

elseif line[-1:] == '}'

return 's1'

else

return '='

endif

endfunction

function! FunctionGraphFoldText()

let s = split(getline(v:foldstart), '|', 1)

if getline(v:foldend+1) =~ 'finish_task_switch() {$'

let s[2] = ' task switch  '

else

let e = split(getline(v:foldend), '|', 1)

let s[2] = e[2]

endif

return join(s, '|')

endfunction

setlocal foldexpr=FunctionGraphFoldExpr(v:lnum)

setlocal foldtext=FunctionGraphFoldText()

setlocal foldcolumn=12

setlocal foldmethod=expr

之后我们配置vim为这个模板来打开前面那个600多行的文件1:

vim -S ~/.fungraph-vim 1

这样我们看到的样子是:

我们可以把光标移动到第5行,键盘敲打za,则展开为:

继续展开第6行的kill_time(),按za:

我们可以用z、a两个按键,搜索或者展开Ftrace的结果。

最后,https://github.com/brendangregg/perf-tools对Ftrace的功能进行了很好的封装和集成,建议大家用perf-tools来使用Ftrace,则效果更佳更简单。

有空再聊perf-tools。

宋宝华:关于Ftrace的一个完整案例相关推荐

  1. linux pdf 宋宝华,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    原创 宋宝华 Linux阅码场 2018-04-10 前言 网上关于BIO和块设备读写流程的文章何止千万,但是能够让你彻底读懂读明白的文章实在难找,可以说是越读越糊涂! 我曾经跨过山和大海 也穿过人山 ...

  2. 宋宝华:评Linux 5.13内核

    目录 Misc cgroup Landlock安全模块 系统调用的堆栈随机化 printk无锁ringbuffer的进一步优化 BPF可调用内核函数 公共的IO PAGE Fault支持 Linux ...

  3. linux 没有windows.h头文件_宋宝华: Linux内核编程广泛使用的前向声明(Forward Declaration)...

    本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:宋宝华 来源: 微信公众号linux阅码场(id: linuxdev) 前向声明 编程定律 先强调一点:在一切可 ...

  4. 宋宝华:LEP(Linux易用剖析器) 是什么,为什么以及怎么办(2)

    LEP(LINUX EASY PROFILING) 是Linuxer之LEP项目组(Barry Song,Mac Xu,陈松等以及陈莉君教授/西邮Linux 3+1实验室)正在致力于打造的一个开源项目 ...

  5. 宋宝华:论一切都是文件之匿名inode

    01 唯有文件得人心 当一个女生让你替她抓100只萤火虫,她一定不是为了折磨你,而是因为她爱上了你.当你们之间经历了无数的恩恩怨怨和彼此伤害,她再次让你替她抓100只萤火虫,那一定是因为她还爱着你. ...

  6. 宋宝华: 用off-cpu火焰图进行Linux性能分析

    在<宋宝华:火焰图:全局视野的Linux性能剖析>一文中,我们主要看了on-cpu火焰图,理解了系统的CPU的走向的分析.但是,很多时候,单纯地看on-cpu的情况(什么代码在耗费CPU) ...

  7. 宋宝华: 关于DMA ZONE和dma alloc coherent若干误解的彻底澄清

    原创 宋宝华 Linux阅码场 2018-01-22 作者简介 宋宝华,他有10几年的Linux开发经验.他长期在大型企业担任一线工程师和系统架构师,编写大量的Linux代码,并负责在gerrit上r ...

  8. platform设备驱动全透析(转自宋宝华老师)

    platform设备驱动全透析(转自宋宝华老师) 2013-04-12 09:58 384人阅读 评论(0) 收藏 举报 分类: linux kernel(22) 1.1 platform总线.设备与 ...

  9. 让尘土回归尘土,宋宝华_第47集:如何扫除尘土飞扬的手指并保持编码状态

    让尘土回归尘土,宋宝华 我必须承认,在"找工作"的旅途中,我处于非常独特的位置. 不幸的是,鉴于我仍然在"体验"它,所以我不会分享它,并且像任何讲故事的人一样, ...

  10. 宋宝华_2010年11-12月Linux驱动和内核讲座PPT下载

        12月29日,宋宝华老师在线讲座(按键和LCD驱动) cloudquan 2010-12-20 2/146 heyan0208 3 天前 00:37     宋宝华_2010年12月11日_& ...

最新文章

  1. 瞧瞧,这样的代码才叫Pythonic
  2. 回收站功能在 Linux 中的实现
  3. 【错误记录】Groovy 函数拦截调用 invokeMethod 导致栈溢出 ( java.lang.StackOverflowError )
  4. java中小数的乘法_javascript的小数点乘法除法实例
  5. vue 过滤器 格式时间秒数,js 时间日期格式化
  6. maven default aliyun_大家看看大佬对Maven仓库的讲解,有何高明之处?
  7. java 程序命令_命令行运行JAVA程序
  8. linux切换tab,linux-mint – ALT-TAB切换器中的图标从哪里加...
  9. Linux手势控制软件,让 linux 实现触摸板多点触控与手势操作
  10. 【python技能树】python简介
  11. 洛谷 CF894A QAQ
  12. access建立两个字段唯一索引_Mysql不止CRUD,聊聊索引
  13. 小暑调养宝宝身体的五个方法
  14. 电脑ip地址错误,网络无法连接怎么一键解决?
  15. 1.4 Kronecker积
  16. 深夜来一发,拿走不谢
  17. 【算法研究】 AEC 回音消除算法
  18. 在那江南烈日与阵雨中-江南100赛记
  19. chrome修复_使用Google的新Chrome主题修复暗模式
  20. 【T+】单据打印:去掉“畅捷通软件”字样

热门文章

  1. D3D中设备丢失的处理
  2. 手工杀毒辅助软件(PC Hunter) V1.51 免费绿色版
  3. 窗函数(matlab)
  4. C#毕业设计——基于C#+asp.net+SQL server的房地产信息管理系统设计与实现(毕业论文+程序源码)——房地产信息管理系统
  5. Python超市商品管理系统
  6. YYKit 学习笔记之 YYLabel
  7. libxml2的安装及使用
  8. mysql insert语句_MySQL INSERT语句简介
  9. 大学BBS年度十大原创淡黄笑话
  10. 显示驱动版本不符 请重新启动计算机,Win10提示此NVIDIA驱动程序与此Windows版本不兼容怎么解决?...