Skip to content

RFC mcount and latency tracing utility -v3

Steven Rostedt lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 RFC Latest30 patches 4 repliesIn review
  2. v1 RFC22 patches 77 repliesSuperseded

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/30Add basic support for gcc profiler instrumentationMakefile arch/x86/Kconfig arch/x86/kernel/entry_32.S +9 more+268 -00 replies
2/30Annotate core code that should not be tracedlib/smp_processor_id.c+1 -10 replies
3/30x86_64: notrace annotationsarch/x86/kernel/head64.c arch/x86/kernel/setup64.c arch/x86/kernel/smpboot_64.c+4 -40 replies
4/30add notrace annotations to vsyscall.arch/x86/kernel/vsyscall_64.c arch/x86/vdso/vclock_gettime.c arch/x86/vdso/vgetcpu.c +1 more+14 -100 replies
5/30add notrace annotations for NMI routinesarch/x86/kernel/nmi_32.c arch/x86/kernel/nmi_64.c arch/x86/kernel/traps_32.c+4 -40 replies
6/30mcount based trace in the form of a header file librarylib/tracing/Kconfig lib/tracing/Makefile lib/tracing/tracer.c +2 more+176 -00 replies
7/30tracer add debugfs interfacelib/tracing/tracer.c lib/tracing/tracer.h+87 -10 replies
8/30mcount tracer output filelib/tracing/tracer.c+215 -20 replies
9/30mcount tracer show task comm and pidlib/tracing/tracer.c lib/tracing/tracer.h+8 -10 replies
10/30Add a symbol only trace outputlib/tracing/tracer.c lib/tracing/tracer.h+62 -40 replies
11/30Reset the tracer when startedlib/tracing/tracer.c+14 -00 replies
12/30separate out the percpu date into a percpu structlib/tracing/tracer.c lib/tracing/tracer.h+31 -230 replies
13/30handle accurate time keeping over long delaysarch/x86/kernel/vsyscall_64.c include/asm-x86/vgtod.h include/linux/clocksource.h +1 more+80 -200 replies
14/30ppc clock accumulate fixarch/powerpc/kernel/time.c+2 -10 replies
15/30Fixup merge between xtime_cache and timkkeeping starvation fixkernel/time/timekeeping.c+2 -30 replies
16/30time keeping add cycle_raw for actual incrementationinclude/linux/clocksource.h+2 -10 replies
17/30initialize the clock source to jiffies clock.include/linux/clocksource.h kernel/time/timekeeping.c+10 -22 replies
18/30add get_monotonic_cyclesinclude/linux/clocksource.h kernel/time/timekeeping.c+51 -00 replies
19/30add notrace annotations to timing eventsarch/x86/kernel/apic_32.c arch/x86/kernel/hpet.c arch/x86/kernel/time_32.c +4 more+13 -130 replies
20/30Add timestamps to tracerlib/tracing/tracer.c lib/tracing/tracer.h+17 -00 replies
21/30Sort trace by timestamplib/tracing/tracer.c lib/tracing/tracer.h+1 -40 replies
22/30speed up the output of the tracerlib/tracing/tracer.c+19 -90 replies
23/30Add latency_trace format tor tracerlib/tracing/tracer.c lib/tracing/tracer.h+411 -530 replies
24/30Split out specific tracing functionslib/Makefile lib/tracing/Kconfig lib/tracing/Makefile +4 more+485 -3480 replies
25/30Trace irq disabled critical timingsarch/x86/kernel/process_64.c arch/x86/lib/thunk_64.S include/asm-x86/irqflags_32.h +8 more+680 -210 replies
26/30Add context switch marker to sched.ckernel/sched.c+3 -00 replies
27/30Add tracing of context switcheslib/tracing/Kconfig lib/tracing/Makefile lib/tracing/trace_sched_switch.c +2 more+264 -10 replies
28/30Generic command line storagelib/tracing/Kconfig lib/tracing/trace_function.c lib/tracing/trace_irqsoff.c +3 more+128 -92 replies
29/30make varaible size buffers for traceslib/tracing/trace_function.c lib/tracing/trace_irqsoff.c lib/tracing/trace_sched_switch.c +1 more+36 -70 replies
30/30trace preempt off critical timingsarch/x86/kernel/process_32.c include/linux/irqflags.h include/linux/mcount.h +5 more+92 -130 replies

Reviews trailers per patch

2 of 30 patches have a Reviewed-by or Acked-by.

PatchJohn Stultz
1/30
2/30
3/30
4/30
5/30
6/30
7/30
8/30
9/30
10/30
11/30
12/30
13/30
14/30
15/30
16/30A-b
17/30A-b
18/30
19/30
20/30
21/30
22/30
23/30
24/30
25/30
26/30
27/30
28/30
29/30
30/30

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [RFC PATCH 00/30 v3] mcount and latency tracing utility -v3

[
version 3 of mcount patches:

changes include:

Made mcount be able to register more than one function to call. If only one function is registered, then it is called directly. If more than one is registered, then a loop function is called to call all registered functions.

Add schedule context switch tracing.

Added preemption off tracing.

Removed recording of command line of task at each trace, and placed it into the scheduling trace, or at time of max preemption off.

Renamed irqs off files to preempt prefixes.

Suggested by Jan Kiszka:
cleaned up calls to mcount (following glibc more)

always call mcount_trace_function directly from assembly

Suggested by Sam Ravnborg:

Created CONFIG_HAVE_MCOUNT to be selected by archs instead of each are defining a new config option.

Added time keeping fixes by John Stultz
]

All released version of these patches can be found at:

http://people.redhat.com/srostedt/tracing/

The following patch series brings to vanilla Linux a bit of the RT kernel trace facility. This incorporates the "-pg" profiling option of gcc that will call the "mcount" function for all functions called in the kernel.

Note: I did investigate using -finstrument-functions but that adds a call to both start and end of a function. Using mcount only does the beginning of the function. mcount alone adds ~13% overhead. The -finstrument-functions added ~19%. Also it caused me to do tricks with inline, because it adds the function calls to inline functions as well.

This patch series implements the code for x86 (32 and 64 bit), but other archs can easily be implemented as well (note: ARM and PPC are already implemented in -rt)

Some Background:
----------------

A while back, Ingo Molnar and William Lee Irwin III created a latency tracer to find problem latency areas in the kernel for the RT patch. This tracer became a very integral part of the RT kernel in solving where latency hot spots were. One of the features that the latency tracer added was a function trace. This function tracer would record all functions that were called (implemented by the gcc "-pg" option) and would show what was called when interrupts or preemption was turned off.

This feature is also very helpful in normal debugging. So it's been talked about taking bits and pieces from the RT latency tracer and bring them to LKML. But no one had the time to do it.

Arnaldo Carvalho de Melo took a crack at it. He pulled out the mcount as well as part of the tracing code and made it generic from the point of the tracing code. I'm not sure why this stopped. Probably because Arnaldo is a very busy man, and his efforts had to be utilized elsewhere.

While I still maintain my own Logdev utility:

http://rostedt.homelinux.com/logdev

I came across a need to do the mcount with logdev too. I was successful but found that it became very dependent on a lot of code. One thing that I liked about my logdev utility was that it was very non-intrusive, and has been easy to port from the Linux 2.0 days. I did not want to burden the logdev patch with the intrusiveness of mcount (not really that intrusive, it just needs to add a "notrace" annotation to functions in the kernel that will cause more conflicts in applying patches for me).

Being close to the holidays, I grabbed Arnaldos old patches and started massaging them into something that could be useful for logdev, and what I found out (and talking this over with Arnaldo too) that this can be much more useful for others as well.

The main thing I changed, was that I made the mcount function itself generic, and not the dependency on the tracing code. That is I added

register_mcount_function()
and
clear_mcount_function()

So when ever mcount is enabled and a function is registered that function is called for all functions in the kernel that is not labeled with the "notrace" annotation.

The Simple Tracer:
------------------

To show the power of this I also massaged the tracer code that Arnaldo pulled from the RT patch and made it be a nice example of what can be done with this.

The function that is registered to mcount has the prototype:

void func(unsigned long ip, unsigned long parent_ip);

The ip is the address of the function and parent_ip is the address of the parent function that called it.

The x86_64 version has the assembly call the registered function directly to save having to do a double function call.

To enable mcount, a sysctl is added:

/proc/sys/kernel/mcount_enabled

Once mcount is enabled, when a function is registed, it will be called by all functions. The tracer in this patch series shows how this is done. It adds a directory in the debugfs, called mctracer. With a ctrl file that will allow the user have the tracer register its function. Note, the order of enabling mcount and registering a function is not important, but both must be done to initiate the tracing. That is, you can disable tracing by either disabling mcount or by clearing the registered function.

Only one function may be registered at a time. If another function is registered, it will simply override what ever was there previously.

Here's a simple example of the tracer output:

CPU 2: hackbench:11867 preempt_schedule+0xc/0x84 <-- avc_has_perm_noaudit+0x45d/0x52c CPU 1: hackbench:12052 selinux_file_permission+0x10/0x11c <-- security_file_permission+0x16/0x18 CPU 3: hackbench:12017 update_curr+0xe/0x8b <-- put_prev_task_fair+0x24/0x4c
CPU 2: hackbench:11867 avc_audit+0x16/0x9e3 <-- avc_has_perm+0x51/0x63
CPU 0: hackbench:12019 socket_has_perm+0x16/0x7c <-- selinux_socket_sendmsg+0x27/0x3e CPU 1: hackbench:12052 file_has_perm+0x16/0xbb <-- selinux_file_permission+0x104/0x11c

This is formated like:

CPU <CPU#>: <task-comm>:<task-pid> <function> <-- <parent-function>

Latency Tracer Format:
----------------------

The format used by the RT patch is a bit more complex. It is designed to record a lot of information quickly and dump out a lot too.

There's two versions of the format. Verbose and non-vebose.

verbose:

preemption latency trace v1.1.5 on 2.6.24-rc7-tst
--------------------------------------------------------------------
latency: 89 us, #3/3, CPU#1 | (M:desktop VP:0, KP:0, SP:0 HP:0 #P:4)

    -----------------
    | task: kjournald-600 (uid:0 nice:-5 policy:0 rt_prio:0)
    -----------------

=> started at: _spin_lock_irqsave+0x2a/0x63 <c06310d2>
=> ended at: _spin_unlock_irqrestore+0x32/0x41 <c0631245>

       kjournald   600 1 1 00000000 00000000 [397408f1] 0.003ms (+0.079ms): _spin_lock_irqsave+0x2a/0x63 <c06310d2> (scsi_dispatch_cmd+0x155/0x234 [scsi_mod] <f8867c19>)
       kjournald   600 1 1 00000000 00000001 [39740940] 0.081ms (+0.005ms): _spin_unlock_irqrestore+0x32/0x41 <c0631245> (scsi_dispatch_cmd+0x1be/0x234 [scsi_mod] <f8867c82>)
       kjournald   600 1 1 00000000 00000002 [39740945] 0.087ms (+0.000ms): trace_hardirqs_on_caller+0x74/0x86 <c0508bdc> (_spin_unlock_irqrestore+0x32/0x41 <c0631245>)

non-verbose:

preemption latency trace v1.1.5 on 2.6.24-rc7-tst
--------------------------------------------------------------------
latency: 89 us, #3/3, CPU#2 | (M:desktop VP:0, KP:0, SP:0 HP:0 #P:4)

    -----------------
    | task: kjournald-600 (uid:0 nice:-5 policy:0 rt_prio:0)
    -----------------

=> started at: _spin_lock_irqsave+0x2a/0x63 <c06310d2>
=> ended at: _spin_unlock_irqrestore+0x32/0x41 <c0631245>

                 _------=> CPU#            
                / _-----=> irqs-off        
               | / _----=> need-resched    
               || / _---=> hardirq/softirq 
               ||| / _--=> preempt-depth   
               |||| /                      
               |||||     delay             
   cmd     pid ||||| time  |   caller      
      \   /    |||||   \   |   /           
kjournal-600   1d...    3us+: _spin_lock_irqsave+0x2a/0x63 <c06310d2> (scsi_dispatch_cmd+0x155/0x234 [scsi_mod] <f8867c19>)
kjournal-600   1d...   81us+: _spin_unlock_irqrestore+0x32/0x41 <c0631245> (scsi_dispatch_cmd+0x1be/0x234 [scsi_mod] <f8867c82>)
kjournal-600   1d...   87us : trace_hardirqs_on_caller+0x74/0x86 <c0508bdc> (_spin_unlock_irqrestore+0x32/0x41 <c0631245>)

Debug FS:
---------

Although enabling and disabling mcount is done through the sysctl:

/proc/sys/kernel/mcount_enabled

The rest of the tracing uses debugfs.

/debugfs/tracing

Here's the available files:

fn_trace_ctrl
echo 1 to this enables mcount tracing (if mcount_enabled is set) echo 0 to disable the function trace tracing.

function_trace
Outputs the function trace in latency_trace format.

preempt_fn_trace_ctrl
echo 1 to enable function tracing in critical sections timings echo 0 to disable

preempt_trace
Outputs the critical section latency

preempt_thresh
echo a number (in usecs) into this to record all traces that are greater than threshold.

iter_ctrl
echo "symonly" to not show the instruction pointers in the trace echo "nosymonly" to disable symonly.
echo "verbose" for verbose output from latency format. echo "noverbose" to disable verbose ouput.
cat iter_ctrl to see the current settings.

preempt_max_latency
Holds the current max critical latency.
echo 0 to reset and start tracing.

trace
simple output format of the function trace code.

Overhead:
---------

Note that having mcount compiled in seems to show a little overhead.

Here's 3 runs of hackbench 50 without the patches:
Time: 2.137
Time: 2.283
Time: 2.245

Avg: 2.221

and here's 3 runs with the patches (without tracing on):
Time: 2.738
Time: 2.469
Time: 2.388

Avg: 2.531

So it is a 13% overhead when enabled (according to hackbench).

But full tracing can cause a bit more problems:

# hackbench 50
Time: 113.350

113.350!!!!!

But this is tracing *every* function call!

Future:
-------
The way the mcount hook is done here, other utilities can easily add their own functions. Just care needs to be made not to call anything that is not marked with notrace, or you will crash the box with recursion. But even the simple tracer adds a "disabled" feature so in case it happens to call something that is not marked with notrace, it is a safety net not to kill the box.

I was originally going to use the relay system to record the data, but that had a chance of calling functions not marked with notrace. But, if for example LTTng wanted to use this, it could disable tracing on a CPU when doing the calls, and this will protect from recusion.

SystemTap:
----------
One thing that Arnaldo and I discussed last year was using systemtap to add hooks into the kernel to start and stop tracing. kprobes is too heavy to do on all funtion calls, but it would be perfect to add to non hot paths to start the tracer and stop the tracer.

So when debugging the kernel, instead of recompiling with printks or other markers, you could simply use systemtap to place a trace start and stop locations and trace the problem areas to see what is happening.

Latency Tracing:
----------------

We can also add trace points to record the time the highest priority task needs to wait before running. This too is currently done in the RT patch.

These are just some of the ideas we have with this. And we are sure others could come up with more.

These patches are for the underlining work. We'll see what happens next.