linux下的 c 和 c++ 开发工具及linux内核开发工具 2

续前节:linux下的 c 和 c++ 开发工具及linux内核开发工具
继续工具的介绍:

  1. ftrace   ftrace 除了 trace-cmd 这个命令行前端工具之外,还有一个图形化显示前端工具叫 kernelshark,非常好用,有时间片显示和过滤功能。kernelshark 和 trace-cmd 都可以使用apt安装。使用方法是先用trace-cmd产生trace.dat文件,然后用kernelshark打开。一般情况下,trace-cmd 需要在板子上运行,因此需要交叉编译,它依赖libtraceevent 和 libtracefs这两个库,各自的下载地址为:
    https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git
    https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git
    git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/trace-cmd.git
    链接 trace-cmd 的时候最好使用 libtraceevent 和 libtracefs 的静态库。


    ftrace 的配置一般在调试文件系统:/sys/kernel/debug/tracing 或 /sys/kernel/tracing。该文件夹中的buffer_size_kb限定了对单个cpu进行trace的最大存储buffer,而buffer_total_size_kb则一般是buffer_size_kb * cpu个数
    trace-cmd真的好用,下面是使用方法:

    1. 常用命令:
     record    - record a trace into a trace.dat file
     set      - set a ftrace configuration parameter
     start       - start tracing without recording into a file
     extract     - extract a trace from the kernel
     stop     - stop the kernel from recording trace data
     restart     - restart the kernel trace data recording
     show     - show the contents of the kernel tracing buffer
     reset     - disable all kernel tracing and clear the trace buffers
     clear     - clear the trace buffers
     report     - read out the trace stored in a trace.dat file
    

    在每一个抓取动作之前,必须先清掉trace buffers,因为ftrace使用的是ringbuffer,因此有可能还需要设置该buffer的大小,不然可能存在覆盖的情况。record下的-m参数就是这个作用(如果要永久修改该参数,直接echo 8192 到 buffer_size_kb)。清掉buffer的动作命令为trace-cmd clear

    record 命令是一个复合命令,一般用来跟踪进程或者可执行程序的, 它会在当前目录下生成trace.dat文件,使用如下:
    trace-cmd record -m 8192 -p function_graph ls ,但这种方式会记录所有层级的函数调用图,哪怕是一个小程序,都非常多的调用,以至于打印都打印不完。
    trace-cmd record -m 8192 -p function_graph --max-graph-depth 3 ls,限定调用层级,可以先层级1,再层级2,层级太深很难分析。
    trace-cmd record -m 8192 -e ext4 ls,这个是抓取ext4的所有event。
    查看报告:
    trace-cmd report。它会读取当前目录下的trace.dat文件进行报告。因此他是record命令的附庸。


    除了上面的 record 复合命令外,还可以使用独立命令,独立命令必须成套出现,比如start后必须调用stop来停止trace,不然会一直trace。注意,reset并不会停止trace,这个命令没什么用,最好别用,以免造成自己误解。当然,start之前调用clear是必须的。start 不会创建trace.dat文件,所以需要自己去把trace导出来。


    有关ftrace的资料:

    1. https://blogs.vmware.com/opensource/2019/11/12/ftrace-linux-kernel/
    2. https://alex.dzyoba.com/blog/ftrace/
    3. https://andreasch.com/2017/12/06/ftrace/
    4. https://jvns.ca/blog/2017/03/19/getting-started-with-ftrace/
    5. https://www.brendangregg.com/blog/2014-08-30/ftrace-the-hidden-light-switch.html
    6. https://lwn.net/Articles/365835/
    7. https://lwn.net/Articles/366796/
    8. https://lwn.net/Articles/370423/
    9. https://lwn.net/Articles/410200/
    10. https://www.kernel.org/doc/Documentation/trace/ftrace.txt
    11. https://jvns.ca/blog/2017/03/19/getting-started-with-ftrace/
posted @ 2021-11-18 17:48  微信公众号--共鸣圈  阅读(398)  评论(0编辑  收藏  举报