namhyung / uftrace

Function graph tracer for C/C++/Rust/Python
https://uftrace.github.io/slide/
GNU General Public License v2.0
3.06k stars 474 forks source link

How to capture traces for only a short time #325

Open atwufei opened 6 years ago

atwufei commented 6 years ago

Firstly, this tool looks great.

I want to use uftrace to trace MySQL. As I only care about how a specific SQL is executed in MySQL, I want to start tracing only after initialization of MySQL, and finish tracing after that SQL, is it possible to capture traces for only a short time instead of the application's whole lifecycle?

elfring commented 6 years ago

…, is it possible to capture traces for only a short time instead of the application's whole lifecycle?

I imagine that this is generally possible. But such an action would depend on your willingness to invest further software development efforts.

The tool “uftrace” is constructed in the way so far that tracing is performed for a specific command exeution.

Another approach is to query only the desired data from a previous recording. Would you dare to convert the available data collection into another database?

namhyung commented 6 years ago

@atwufei you can use trace-on/-off trigger (using -T option) to enable/disable tracing during execution. The trigger works on a specific function specified on the command line. Also uftrace can be started tracing-disabled using the --disable option.

For example, if the init fuctions is mysql_init() and some other function you know is mysql_foo, following will record trace between the two functions:

$ uftrace --disable -T mysql_init@trace-on  -T mysql_foo@trace-off  mysql <options>...

Currently trigger actions are processed at function entry.

atwufei commented 6 years ago

@namhyung thank you very much. Is it possible to trace the only function like: -T func_entry@trace-on -T func_exit@trace-off

namhyung commented 6 years ago

Do you mean single function? uftrace also provides function filters so if you want to trace only specific functions, please try -F option:

$ uftrace -F mysql_foo  mysql <options>...
honggyukim commented 6 years ago

Hi @atwufei,

If you want to trace only a specific single function, you can use -F option as @namhyung said. But it shows the target function and its children functions as well.

Please see the below example,

$ uftrace tests/t-abc
# DURATION     TID     FUNCTION
   1.326 us [ 24505] | __monstartup();
   0.926 us [ 24505] | __cxa_atexit();
            [ 24505] | main() {
            [ 24505] |   a() {
            [ 24505] |     b() {
            [ 24505] |       c() {
   0.640 us [ 24505] |         getpid();
   1.534 us [ 24505] |       } /* c */
   1.944 us [ 24505] |     } /* b */
   2.211 us [ 24505] |   } /* a */
   2.717 us [ 24505] | } /* main */

In this case, if you use -F b to trace b function, the output looks like below:

$ uftrace -F b tests/t-abc
# DURATION     TID     FUNCTION
            [ 24518] | b() {
            [ 24518] |   c() {
   0.640 us [ 24518] |     getpid();
   2.324 us [ 24518] |   } /* c */
   3.145 us [ 24518] | } /* b */

If you want to trace only b function without its children, you can run it as follows:

$ uftrace -F b -D 1 tests/t-abc
# DURATION     TID     FUNCTION
   1.283 us [ 24534] | b();

Here is another way:

$ uftrace -F b@depth=1 tests/t-abc
# DURATION     TID     FUNCTION
   1.206 us [ 24562] | b();

Please refer to the man page for -D option and @depth trigger.

namhyung commented 6 years ago

@atwufei you might be interested in the signal trigger #340.

atwufei commented 6 years ago

Cool, thank you very much.