Song Baohua: A complete case of ftrace

Source: Internet
Author: User
Ftrace Introduction

Ftrace is one of the most effective tools for Linux to do code-level practice analysis, such as we make a system call, out of time, we want to know where the time spent, using ftrace can be traced to the first level of time distribution. Ftrace Case

Write a proc module that contains a proc read and write entry. Test_proc_show () intentionally invokes a kill_time () function, while the Kill_time () function calls the Mdelay (2) and kill_moretime () functions, which call Mdelay (2).

The Kill_time () function and the Kill_moretime () function are preceded by noinline to avoid being optimized by the compiler inline.

#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/ version.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/delay.h> #
Include <linux/uaccess.h> static 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_p
rintf (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) {...} static int Test_proc_open (struct inode *inode, struct file *file) {return Single_open (file, Test_proc_show, Pde_data (Inod
e)); 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 <baohua@kernel.org>");
Module_description ("proc exmaple"); Module_license ("GPL v2");
The module corresponds to the makefile as follows:

Kvers = $ (Shell uname-r)

# Kernel Modules

obj-m + = PROC.O

# Specify flags for the module compilation.

#EXTRA_CFLAGS =-g-o0

build:kernel_modules
kernel_modules:
 make-c/lib/modules/$ (kvers)/build m=$ ( CurDir) Modules Clean
:
 make-c/lib/modules/$ (kvers)/build m=$ (CurDir) Clean
Compile and load:

$ make

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

$ sudo insmod Proc.ko

[sudo] password for Baohua:

The/PROC/TEST_DIR/TEST_RW file can be read/write after the/proc directory.

Below we use Ftrace to track the function of Test_proc_show ().

We wrote all the commands that started ftrace into a script function.sh:

#!/bin/bash
debugfs=/sys/kernel/debug
echo nop > $debugfs/tracing/current_tracer
echo 0 > $debugfs /tracing/tracing_on
echo $$ > $debugfs/tracing/set_ftrace_pid
echo function_graph > $debugfs/tracing/ Current_tracer
#replace test_proc_show by your function name
echo test_proc_show > $debugfs/tracing/set_ Graph_function
echo 1 > $debugfs/tracing/tracing_on
exec "$@"
Then use this script to start the CAT/PROC/TEST_DIR/TEST_RW so that the Test_proc_show () function under Ftrace is trace.

#./function.sh CAT/PROC/TEST_DIR/TEST_RW

0

Read the result of trace:

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

Then using VIM to open this file 1, found that this file has more than 600 lines:




Days, long to see not clear.


Ftrace How to read the results.

Ftrace How to read the results. The answer is very simple: if it is a leaf function, directly in front of this function to show the time it takes, if it is not the leaves, to wait until}, then show time, the following figure:


The larger part of the delay, there will be +, #等特殊标号:

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


vim folds the Ftrace

The Ftrace file above is too big to see clearly. We can use Vim to fold, but need a special configuration of vim, I put it in my ~ directory, named. 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 ' 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'll show the total time taken for a call,

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

"Functions would not include finish_task_switch (), so folding should remain

"Relatively sane even through a context switch.

"

"Note, this would almost certainly only work OK with a

"Single-cpu Trace (e.g. trace-cmd,--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

After that, we configure VIM to open the previous 600-line file 1 for this template:

Vim-s ~/.fungraph-vim 1

So the way we see it is:


We can move the cursor to line 5th, the keyboard beats za, then expand to:


Continue to expand the 6th line of Kill_time (), press za:


We can use Z, a two buttons to search for or expand ftrace results.



Finally, the Ftrace function of the https://github.com/brendangregg/perf-tools is well encapsulated and integrated, it is recommended to use the Perf-tools to Ftrace, the effect is better and simpler.


Talk about Perf-tools again when you are free.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.