mcount and latency tracing utility -v8
Subsystems (12)
Steven Rostedt
reviewercover mcount and latency tracing utility -v8
[
version 8 of mcount / trace patches:
changes include:
Added read_barrier_depends to mcount and sched function loops incase anyone is crazy enough to port this to alpha.
(Suggested by Peter Ziljstra)
Added smp_rmb for the wakeup_sched_switch to match the smp_wmb in the wakeup_reset. (Pointed out by Peter Ziljstra)
Replaced all the atomic_inc/atomic_read with atomic_inc_returned.
(Suggested by Frank Ch. Eigler)
Found that the tracer ctrl variable wasn't fully protected from multiple updates and fixed it by moving the the tracer_types mutex to protect it (only modified by users reading and writing into the debugfs system).
Fixed a ref count bug in the sched switch disarming of the schedule markers. Markers would stay disarmed when they should be armed.
Changed ref counting in wakeup timings to use atomic_dec_and_test instead of atomic_dec_return (source of sched ref bug).
Hopefully the prologue changes in the two patches by John Stultz show him as author when quilt sends these patches out.
(Noticed by Peter Ziljstra)
Added preempt_disable_notrace and preempt_enable_notrace so that the tracer doesn't constantly trace get_monotonic_cycles that it uses internally!
]
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()
andclear_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.
When one function is registered, it is called directly from the mcount assembly. If more than one function is registered, a "loop" function is called that calls all the registered functions.
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:
available_tracers
Lists the tracers that are compiled into the kernel and can be echoed into current_tracer
current_tracer
Shows the current tracer that is registered. On bootup this is blank. To set a new tracer, simply echo into this file the name found in available_tracers. To unregister all tracers, echo "disable" into this file.
e.g.
echo wakeup > /debugfs/tracing/current_tracer
trace_ctrl
echo 1 to enable the current tracer.
echo 0 to disable it.
Note, the function trace also needs mcount_enabled set, otherwise it will not record.
latency_trace
Outputs the current trace in latency_trace format.
tracing_thresh
echo a number (in usecs) into this to record all traces that are greater than threshold. Only matters for those traces that use a max threshold (preemptoff irqsoff and wakeup)
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.
tracing_max_latency
Holds the current max critical latency. echo 0 to reset and start tracing.
Only holds for those tracers that use a max setting. (preemptoff irqsoff and wakeup)
trace
simple output format of the current trace.
latency_hist/
This is a directory of histograms (when configured) This directory holds histograms for preemption off, irqs off, preemption and/or irqs off, and wakeup timings. (all numbers are in usecs)
Trace tool:
-----------
The trace_cmd.c found at
http://people.redhat.com/srostedt/tracing/trace-cmd.c
This tool will set up the tracer and allow you to run a program and trace it. (must be root)
e.g.
# ./trace-cmd -f echo hi
# cat /debug/tracing/latency_trace | grep echo | head
echo-4240 1d..1 1076us : ret_from_fork+0x6/0x1c (schedule_tail+0x9/0x5e)
echo-4240 1d..1 1077us : schedule_tail+0x1e/0x5e (finish_task_switch+0xb/0x61)
echo-4240 1d..1 1078us : finish_task_switch+0x24/0x61 (_spin_unlock_irq+0x8/0x40)
echo-4240 1...1 1079us : _spin_unlock_irq+0x27/0x40 (sub_preempt_count+0xd/0x104)
echo-4240 1...1 1080us+: sub_preempt_count+0x8d/0x104 (in_lock_functions+0x8/0x2c)
echo-4240 1d... 1082us : error_code+0x72/0x78 (do_page_fault+0xe/0x7c0)
echo-4240 1d... 1083us : do_page_fault+0x35b/0x7c0 (add_preempt_count+0xc/0x10f)
echo-4240 1d..1 1085us : add_preempt_count+0x53/0x10f (in_lock_functions+0x8/0x2c)
echo-4240 1d..1 1086us : do_page_fault+0x38f/0x7c0 (sub_preempt_count+0xd/0x104)
echo-4240 1d..1 1087us : sub_preempt_count+0x8d/0x104 (in_lock_functions+0x8/0x2c)The available switches are:
-s - context switch tracing
-p - preemption off tracing
-i - interrupts off tracing
-b - both interrupts off and preemption off tracing -w - wakeup timings
-e - event tracing
-f - function trace
Only -f (function trace) may be enabled with the other tracers, since most the other tracers also have a function trace version if mcount is enabled (yes -f will enable mcount).
Overhead:
---------
Note that having mcount compiled in seems to show a little overhead.
Here's the new runs: Running "hackbench 50" 10 times each.
Without any of the patches:
Avg: 4.1953 secs
With Event tracer, wakeup timings, context switch compiled in but all turned off (marker overhead only).
Avg: 4.2649 (1.66%)
Same config but event tracer registered but not enabled:
Avg: 4.2717 (1.82%)
Event tracer enabled:
Avg: 4.3249 (3.09%)
Wakeup registed but not enabled:
Avg: 4.2942 (2.36%)
Wakeup enabled:
Avg: 4.3124 (2.79%)
sched_switch registered but not enabled:
Avg: 4.3118 (2.78%)
sched_switch enabled:
Avg: 4.2552 (1.43%) /* seems to be in the noise */
Preempt off timings configured:
preempt not enabled:
Avg: 4.6768 (11.48%)
preempt enabled:
Avg: 9.1784 (118.78%) /* expected */
Irqs off timings configured:
irqsoff not enabled:
Avg: 4.7744 (13.80%)
irqsoff enabled:
Avg: 10.3651 (147.04%)
preempt off and irqs off configured:
everything not enabled:
Avg: 5.0823 (21.14%)
irqsoff enabled:
Avg: 16.0194 (281.84%)
preemptoff enabled:
Avg: 9.8112 (133.86%)
preemptirqsoff enabled:
Avg: 15.9543 (280.29%)
Mcount and function trace configured:
mcount not enabled:
4.8638 (15.93%) /* note - I removed a bit of notrace
which can increase this number */
mcount enabled:
6.2816 (49.74%)
mcount and function tracing enabled:
25.2035 (500.76%) /* this may seem big, but this is down
from 113 secs! (2590%) */Wakeup histogram configured (always on):
Avg: 4.268 (1.73%)
irqs off histogram configured (always on):
Avg: 6.9054 (64.60%)
preempt off histogram configured (always on):
Avg: 5.8953 (40.52%)
irqs and preempt off histograms configured (always on):
Avg: 14.808 (252.85%)
For full output of the above numbers see:
http://people.redhat.com/srostedt/tracing/hack-results.cal
Wakeup timings, event trace and context switch tracing adds virtually no overhead when configured. The interrupt and preemption off, as well as mcount does, and should only be in debugging kernels.
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 plan on looking into converting lockdep to use markers so that lockdep and the preempt and irqs off tracers can be run time enabled without adding much overhead when configured in.
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.
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.
Steven Rostedt
reviewerPATCH 1/23 printk - dont wakeup klogd with interrupts disabled
[ This patch is added to the series since the wakeup timings trace
may lockup without it. ]
I thought that one could place a printk anywhere without worrying. But it seems that it is not wise to place a printk where the runqueue lock is held.
I just spent two hours debugging why some of my code was locking up, to find that the lockup was caused by some debugging printk's that I had in the scheduler. The printk's were only in rare paths so they shouldn't be too much of a problem, but after I hit the printk the system locked up.
Thinking that it was locking up on my code I went looking down the wrong path. I finally found (after examining an NMI dump) that the lockup happened because printk was trying to wakeup the klogd daemon, which caused a deadlock when the try_to_wakeup code tries to grab the runqueue lock.
This patch adds a runqueue_is_locked interface in sched.c for other files to see if the current runqueue lock is held. This is used in printk to determine whether it is safe or not to wakeup the klogd.
And with this patch, my code ran fine ;-)
--- include/linux/sched.h | 2 ++ kernel/printk.c | 14 ++++++++++---- kernel/sched.c | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-)
kernel/printk.c +10 -4
@@ -590,9 +590,11 @@ static int have_callable_console(void)* @fmt: format string * * This is printk(). It can be called from any context. We want it to work. -* Be aware of the fact that if oops_in_progress is not set, we might try to -* wake klogd up which could deadlock on runqueue lock if printk() is called -* from scheduler code. +* +* Note: if printk() is called with the runqueue lock held, it will not wake +* up the klogd. This is to avoid a deadlock from calling printk() in schedule +* with the runqueue lock held and having the wake_up grab the runqueue lock +* as well. * * We try to grab the console_sem. If we succeed, it's easy - we log the output and * call the console drivers. If we fail to get the semaphore we place the output
@@ -1001,7 +1003,11 @@ void release_console_sem(void) console_locked =0 ;up (&console_sem);spin_unlock_irqrestore (&logbuf_lock, flags); -if (wake_klogd) +/* +* If we try to wake up klogd while printing with the runqueue lock +* held, this will deadlock. +*/ +if (wake_klogd && !runqueue_is_locked ())wake_up_klogd (); }EXPORT_SYMBOL (release_console_sem);
include/linux/sched.h +2 -0
@@ -222,6 +222,8 @@ extern void sched_init_smp(void);extern void init_idle (struct task_struct *idle,int cpu);extern void init_idle_bootup_task (struct task_struct *idle); +extern int runqueue_is_locked (void ); +extern cpumask_t nohz_cpu_mask;#if defined(CONFIG_SMP ) && defined(CONFIG_NO_HZ )extern int select_nohz_load_balancer (int cpu);
kernel/sched.c +18 -0
@@ -621,6 +621,24 @@ unsigned long rt_needs_cpu(int cpu)# define const_debug static const#endif +/** +* runqueue_is_locked +* +* Returns true if the current cpu runqueue is locked. +* This interface allows printk to be called with the runqueue lock +* held and know whether or not it is OK to wake up the klogd. +*/ +int runqueue_is_locked (void ) +{ +int cpu =get_cpu (); +struct rq *rq =cpu_rq (cpu); +int ret; + + ret =spin_is_locked (&rq->lock); +put_cpu (); +return ret; +} +/* * Debugging: various feature bits */
Steven Rostedt
reviewerPATCH 2/23 Add basic support for gcc profiler instrumentation
In reply to Steven RostedtIf CONFIG_MCOUNT is selected and /proc/sys/kernel/mcount_enabled is set to a non-zero value the mcount routine will be called everytime we enter a kernel function that is not marked with the "notrace" attribute.
The mcount routine will then call a registered function if a function happens to be registered.
[This code has been highly hacked by Steven Rostedt, so don't
blame Arnaldo for all of this ;-) ]
Update:
It is now possible to register more than one mcount function. If only one mcount function is registered, that will be the function that mcount calls directly. If more than one function is registered, then mcount will call a function that will loop through the functions to call.
--- Makefile | 3 arch/x86/Kconfig | 1 arch/x86/kernel/entry_32.S | 25 +++++++ arch/x86/kernel/entry_64.S | 36 ++++++++++ include/linux/linkage.h | 2 include/linux/mcount.h | 38 +++++++++++ kernel/sysctl.c | 11 +++ lib/Kconfig.debug | 1 lib/Makefile | 2 lib/tracing/Kconfig | 10 ++ lib/tracing/Makefile | 3 lib/tracing/mcount.c | 151 +++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 283 insertions(+)
Makefile +3 -0
@@ -509,6 +509,9 @@ endif include $(srctree)/arch/$(SRCARCH)/Makefile +ifdef CONFIG_MCOUNT +KBUILD_CFLAGS += -pg +endif ifdef CONFIG_FRAME_POINTER KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls else
arch/x86/Kconfig +1 -0
@@ -19,6 +19,7 @@ config X86_64 config X86 bool default y + select HAVE_MCOUNT config GENERIC_TIME bool
arch/x86/kernel/entry_32.S +25 -0
@@ -75,6 +75,31 @@ DF_MASK = 0x00000400 NT_MASK = 0x00004000 VM_MASK = 0x00020000 +#ifdef CONFIG_MCOUNT +.globl mcount +mcount: + /* unlikely(mcount_enabled) */ + cmpl $0, mcount_enabled + jnz trace + ret + +trace: + /* taken from glibc */ + pushl %eax + pushl %ecx + pushl %edx + movl 0xc(%esp), %edx + movl 0x4(%ebp), %eax + + call *mcount_trace_function + + popl %edx + popl %ecx + popl %eax + + ret +#endif + #ifdef CONFIG_PREEMPT #define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF #else
arch/x86/kernel/entry_64.S +36 -0
@@ -53,6 +53,42 @@ .code64 +#ifdef CONFIG_MCOUNT + +ENTRY(mcount) + /* unlikely(mcount_enabled) */ + cmpl $0, mcount_enabled + jnz trace + retq + +trace: + /* taken from glibc */ + subq $0x38, %rsp + movq %rax, (%rsp) + movq %rcx, 8(%rsp) + movq %rdx, 16(%rsp) + movq %rsi, 24(%rsp) + movq %rdi, 32(%rsp) + movq %r8, 40(%rsp) + movq %r9, 48(%rsp) + + movq 0x38(%rsp), %rsi + movq 8(%rbp), %rdi + + call *mcount_trace_function + + movq 48(%rsp), %r9 + movq 40(%rsp), %r8 + movq 32(%rsp), %rdi + movq 24(%rsp), %rsi + movq 16(%rsp), %rdx + movq 8(%rsp), %rcx + movq (%rsp), %rax + addq $0x38, %rsp + + retq +#endif + #ifndef CONFIG_PREEMPT #define retint_kernel retint_restore_args #endif
include/linux/linkage.h +2 -0
@@ -3,6 +3,8 @@#include <asm/linkage.h> +#define notrace __attribute__((no_instrument_function)) +#ifdef __cplusplus#define CPP_ASMLINKAGE extern "C"#else
include/linux/mcount.h new file +38 -0
@@ -0,0 +1,38 @@ +#ifndef _LINUX_MCOUNT_H +#define _LINUX_MCOUNT_H + +#ifdef CONFIG_MCOUNT +extern int mcount_enabled; + +#include <linux/linkage.h> + +#define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) +#define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1)) +#define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2)) + +typedef void (*mcount_func_t )(unsigned long ip,unsigned long parent_ip); + +struct mcount_ops { +mcount_func_t func; +struct mcount_ops *next; +}; + +/* +* The mcount_ops must be a static and should also +* be read_mostly. These functions do modify read_mostly variables +* so use them sparely. Never free an mcount_op or modify the +* next pointer after it has been registered. Even after unregistering +* it, the next pointer may still be used internally. +*/ +int register_mcount_function (struct mcount_ops *ops); +int unregister_mcount_function (struct mcount_ops *ops); +void clear_mcount_function (void ); + +extern void mcount (void ); + +#else /* !CONFIG_MCOUNT */ +# define register_mcount_function (ops) do { } while (0) +# define unregister_mcount_function (ops) do { } while (0) +# define clear_mcount_function (ops) do { } while (0) +#endif /* CONFIG_MCOUNT */ +#endif /* _LINUX_MCOUNT_H */
kernel/sysctl.c +11 -0
@@ -46,6 +46,7 @@#include <linux/nfs_fs.h> #include <linux/acpi.h> #include <linux/reboot.h> +#include <linux/mcount.h> #include <asm/uaccess.h> #include <asm/processor.h>
@@ -514,6 +515,16 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = &proc_dointvec, }, +#ifdef CONFIG_MCOUNT + { + .ctl_name = CTL_UNNUMBERED, + .procname = "mcount_enabled", + .data = &mcount_enabled, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif #ifdef CONFIG_KMOD { .ctl_name = KERN_MODPROBE,
lib/Kconfig.debug +1 -0
@@ -562,5 +562,6 @@ config LATENCYTOP Enable this option if you want to use the LatencyTOP tool to find out which userspace is blocking on what kernel operations. +source lib/tracing/Kconfig source "samples/Kconfig"
lib/Makefile +2 -0
@@ -67,6 +67,8 @@ obj-$(CONFIG_AUDIT_GENERIC) += audit.o obj-$(CONFIG_SWIOTLB) += swiotlb.o obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o +obj-$(CONFIG_MCOUNT) += tracing/ + lib-$(CONFIG_GENERIC_BUG) += bug.o hostprogs-y := gen_crc32table
lib/tracing/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ + +# Archs that enable MCOUNT should select HAVE_MCOUNT +config HAVE_MCOUNT + bool + +# MCOUNT itself is useless, or will just be added overhead. +# It needs something to register a function with it. +config MCOUNT + bool + select FRAME_POINTER
lib/tracing/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_MCOUNT) += libmcount.o + +libmcount-y := mcount.o
lib/tracing/mcount.c new file +151 -0
151 lines not on this page · raw message
Steven Rostedt
reviewerPATCH 3/23 Annotate core code that should not be traced
In reply to Steven RostedtMark with "notrace" functions in core code that should not be traced. The "notrace" attribute will prevent gcc from adding a call to mcount on the annotated funtions.
--- lib/smp_processor_id.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/smp_processor_id.c +1 -1
@@ -7,7 +7,7 @@#include <linux/kallsyms.h> #include <linux/sched.h> -unsigned int debug_smp_processor_id (void ) +notrace unsigned int debug_smp_processor_id(void) {unsigned long preempt_count =preempt_count ();int this_cpu =raw_smp_processor_id ();
Steven Rostedt
reviewerPATCH 4/23 x86_64: notrace annotations
In reply to Steven RostedtAdd "notrace" annotation to x86_64 specific files.
--- arch/x86/kernel/head64.c | 2 +- arch/x86/kernel/setup64.c | 4 ++-- arch/x86/kernel/smpboot_64.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/head64.c +1 -1
@@ -46,7 +46,7 @@ static void __init copy_bootdata(char *r } } -void __init x86_64_start_kernel (char * real_mode_data) +notrace void __init x86_64_start_kernel(char *real_mode_data) {int i;
arch/x86/kernel/setup64.c +2 -2
@@ -114,7 +114,7 @@ void __init setup_per_cpu_areas(void) } } -void pda_init (int cpu) +notrace void pda_init(int cpu) {struct x8664_pda *pda =cpu_pda (cpu);
@@ -197,7 +197,7 @@ DEFINE_PER_CPU(struct orig_ist, orig_ist* 'CPU state barrier', nothing should get across. * A lot of state is already set up in PDA init. */ -void __cpuinit cpu_init (void ) +notrace void __cpuinit cpu_init(void) {int cpu =stack_smp_processor_id ();struct tss_struct *t = &per_cpu (init_tss, cpu);
arch/x86/kernel/smpboot_64.c +1 -1
@@ -317,7 +317,7 @@ static inline void set_cpu_sibling_map(i/* * Setup code on secondary processor (after comming out of the trampoline) */ -void __cpuinit start_secondary (void ) +notrace __cpuinit void start_secondary(void) {/* * Dont put anything before smp_callin(), SMP
Steven Rostedt
reviewerPATCH 5/23 add notrace annotations to vsyscall.
In reply to Steven RostedtAdd the notrace annotations to some of the vsyscall functions.
--- arch/x86/kernel/vsyscall_64.c | 3 ++- arch/x86/vdso/vclock_gettime.c | 15 ++++++++------- arch/x86/vdso/vgetcpu.c | 3 ++- include/asm-x86/vsyscall.h | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-)
arch/x86/kernel/vsyscall_64.c +2 -1
@@ -42,7 +42,8 @@#include <asm/topology.h> #include <asm/vgtod.h> -#define __vsyscall (nr) __attribute__ ((unused,__section__(".vsyscall_" #nr))) +#define __vsyscall (nr) \ + __attribute__ ((unused, __section__(".vsyscall_" #nr))) notrace#define __syscall_clobber "r11","rcx","memory"#define __pa_vsymbol (x) \ ({unsigned long v; \
arch/x86/vdso/vclock_gettime.c +8 -7
@@ -24,7 +24,7 @@#define gtod vdso_vsyscall_gtod_data -static long vdso_fallback_gettime (long clock,struct timespec *ts) +notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) {long ret; asm("syscall" : "=a" (ret) :
@@ -32,7 +32,7 @@ static long vdso_fallback_gettime(long creturn ret; } -static inline long vgetns (void ) +notrace static inline long vgetns(void) {long v;cycles_t (*vread)(void);
@@ -41,7 +41,7 @@ static inline long vgetns(void)return (v * gtod->clock.mult) >> gtod->clock.shift; } -static noinline int do_realtime(struct timespec *ts) +notrace static noinline int do_realtime(struct timespec *ts) {unsigned long seq, ns; do {
@@ -55,7 +55,8 @@ static noinline int do_realtime(struct t }/* Copy of the version in kernel/time.c which we cannot directly access */ -static void vset_normalized_timespec (struct timespec *ts,long sec,long nsec) +notrace static void +vset_normalized_timespec (struct timespec *ts,long sec,long nsec) {while (nsec >=NSEC_PER_SEC ) { nsec -=NSEC_PER_SEC ;
@@ -69,7 +70,7 @@ static void vset_normalized_timespec(str ts->tv_nsec = nsec; } -static noinline int do_monotonic(struct timespec *ts) +notrace static noinline int do_monotonic(struct timespec *ts) {unsigned long seq, ns, secs; do {
@@ -83,7 +84,7 @@ static noinline int do_monotonic(structreturn 0 ; } -int __vdso_clock_gettime (clockid_t clock,struct timespec *ts) +notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) {if (likely (gtod->sysctl_enabled && gtod->clock.vread))switch (clock) {
@@ -97,7 +98,7 @@ int __vdso_clock_gettime(clockid_t clockint clock_gettime (clockid_t ,struct timespec *) __attribute__((weak,alias ("__vdso_clock_gettime" ))); -int __vdso_gettimeofday (struct timeval *tv,struct timezone *tz) +notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) {long ret;if (likely (gtod->sysctl_enabled && gtod->clock.vread)) {
arch/x86/vdso/vgetcpu.c +2 -1
@@ -13,7 +13,8 @@#include <asm/vgtod.h> #include "vextern.h" -long __vdso_getcpu (unsigned *cpu,unsigned *node,struct getcpu_cache *unused) +notrace long +__vdso_getcpu (unsigned *cpu,unsigned *node,struct getcpu_cache *unused) {unsigned int dummy, p;
include/asm-x86/vsyscall.h +2 -1
@@ -24,7 +24,8 @@ enum vsyscall_num { ((unused, __section__ (".vsyscall_gtod_data"),aligned(16)))#define __section_vsyscall_clock __attribute__ \ ((unused, __section__ (".vsyscall_clock"),aligned(16))) -#define __vsyscall_fn __attribute__ ((unused,__section__(".vsyscall_fn"))) +#define __vsyscall_fn \ + __attribute__ ((unused, __section__(".vsyscall_fn"))) notrace#define VGETCPU_RDTSCP 1#define VGETCPU_LSL 2
Steven Rostedt
reviewerPATCH 6/23 handle accurate time keeping over long delays
In reply to Steven RostedtFrom: John Stultz <[email protected]>
Handle accurate time even if there's a long delay between accumulated clock cycles.
--- arch/powerpc/kernel/time.c | 3 +- arch/x86/kernel/vsyscall_64.c | 5 ++- include/asm-x86/vgtod.h | 2 - include/linux/clocksource.h | 58 ++++++++++++++++++++++++++++++++++++++++-- kernel/time/timekeeping.c | 36 +++++++++++++------------- 5 files changed, 82 insertions(+), 22 deletions(-)
arch/x86/kernel/vsyscall_64.c +4 -1
@@ -86,6 +86,7 @@ void update_vsyscall(struct timespec *wa vsyscall_gtod_data.clock.mask = clock->mask; vsyscall_gtod_data.clock.mult = clock->mult; vsyscall_gtod_data.clock.shift = clock->shift; + vsyscall_gtod_data.clock.cycle_accumulated = clock->cycle_accumulated; vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec; vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec; vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic;
@@ -121,7 +122,7 @@ static __always_inline long time_syscall static __always_inline void do_vgettimeofday(struct timeval * tv) { -cycle_t now, base, mask, cycle_delta; +cycle_t now, base, accumulated, mask, cycle_delta;unsigned seq;unsigned long mult, shift, nsec;cycle_t (*vread)(void);
@@ -135,6 +136,7 @@ static __always_inline void do_vgettimeo } now =vread (); base = __vsyscall_gtod_data.clock.cycle_last; + accumulated = __vsyscall_gtod_data.clock.cycle_accumulated; mask = __vsyscall_gtod_data.clock.mask; mult = __vsyscall_gtod_data.clock.mult; shift = __vsyscall_gtod_data.clock.shift;
@@ -145,6 +147,7 @@ static __always_inline void do_vgettimeo/* calculate interval: */ cycle_delta = (now - base) & mask; + cycle_delta += accumulated;/* convert to nsecs: */ nsec += (cycle_delta * mult) >> shift;
include/asm-x86/vgtod.h +1 -1
@@ -15,7 +15,7 @@ struct vsyscall_gtod_data {struct timezone sys_tz;struct {/* extract of a clocksource struct */ cycle_t (*vread)(void ); -cycle_t cycle_last; +cycle_t cycle_last, cycle_accumulated;cycle_t mask;u32 mult;u32 shift;
include/linux/clocksource.h +56 -2
95 lines not on this page · raw message
kernel/time/timekeeping.c +19 -17
@@ -66,16 +66,10 @@ static struct clocksource *clock; /* poi*/ static inline s64 __get_nsec_offset (void ) { -cycle_t cycle_now, cycle_delta; +cycle_t cycle_delta;s64 ns_offset; -/* read clocksource: */ - cycle_now =clocksource_read (clock); - -/* calculate the delta since the last update_wall_time: */ - cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; - -/* convert to nanoseconds: */ + cycle_delta =clocksource_get_cycles (clock,clocksource_read (clock)); ns_offset =cyc2ns (clock, cycle_delta);return ns_offset;
@@ -195,7 +189,7 @@ static void change_clocksource(void) clock = new; clock->cycle_last = now; - + clock->cycle_accumulated =0 ; clock->error =0 ; clock->xtime_nsec =0 ;clocksource_calculate_interval (clock,NTP_INTERVAL_LENGTH );
@@ -205,9 +199,15 @@ static void change_clocksource(void)printk (KERN_INFO "Time: %s clocksource has been installed.\n" , clock->name); } + +void timekeeping_accumulate (void ) +{ +clocksource_accumulate (clock,clocksource_read (clock)); +}#else static inline void change_clocksource (void ) { }static inline s64 __get_nsec_offset (void ) {return 0 ; } +void timekeeping_accumulate (void ) { }#endif /**
@@ -302,6 +302,7 @@ static int timekeeping_resume(struct systimespec_add_ns (&xtime, timekeeping_suspend_nsecs);/* re-base the last cycle value */ clock->cycle_last =clocksource_read (clock); + clock->cycle_accumulated =0 ; clock->error =0 ; timekeeping_suspended =0 ;write_sequnlock_irqrestore (&xtime_lock, flags);
@@ -448,27 +449,28 @@ static void clocksource_adjust(s64 offse*/ void update_wall_time (void ) { -cycle_t offset; +cycle_t cycle_now;/* Make sure we're fully resumed: */ if (unlikely (timekeeping_suspended))return ;#ifdef CONFIG_GENERIC_TIME - offset = (clocksource_read (clock) - clock->cycle_last) & clock->mask; + cycle_now =clocksource_read (clock);#else - offset = clock->cycle_interval; + cycle_now = clock->cycle_last + clock->cycle_interval;#endif +clocksource_accumulate (clock, cycle_now); + clock->xtime_nsec += (s64 )xtime.tv_nsec << clock->shift;/* normally this loop will run just once, however in the * case of lost or late ticks, it will accumulate correctly. */ -while (offset >= clock->cycle_interval) { +while (clock->cycle_accumulated >= clock->cycle_interval) {/* accumulate one interval */ clock->xtime_nsec += clock->xtime_interval; - clock->cycle_last += clock->cycle_interval; - offset -= clock->cycle_interval; + clock->cycle_accumulated -= clock->cycle_interval;if (clock->xtime_nsec >= (u64 )NSEC_PER_SEC << clock->shift) { clock->xtime_nsec -= (u64 )NSEC_PER_SEC << clock->shift;
@@ -482,13 +484,13 @@ void update_wall_time(void) }/* correct the clock when NTP error is too big */ -clocksource_adjust (offset); +clocksource_adjust (clock->cycle_accumulated);/* store full nanoseconds into xtime */ xtime.tv_nsec = (s64 )clock->xtime_nsec >> clock->shift; clock->xtime_nsec -= (s64 )xtime.tv_nsec << clock->shift; -update_xtime_cache (cyc2ns (clock, offset)); +update_xtime_cache (cyc2ns (clock, clock->cycle_accumulated));/* check to see if there is a new clocksource to use */ change_clocksource ();
arch/powerpc/kernel/time.c +2 -1
@@ -773,7 +773,8 @@ void update_vsyscall(struct timespec *wa stamp_xsec = (u64 ) xtime.tv_nsec *XSEC_PER_SEC ;do_div (stamp_xsec,1000000000 ); stamp_xsec += (u64 ) xtime.tv_sec *XSEC_PER_SEC ; -update_gtod (clock->cycle_last, stamp_xsec, t2x); +update_gtod (clock->cycle_last-clock->cycle_accumulated, + stamp_xsec, t2x); }void update_vsyscall_tz (void )
Re: [PATCH 06/23 -v8] handle accurate time keeping over long delays
Quoted 5 lines · * Steven Rostedt ([email protected]) wrote:
From: John Stultz <[email protected]>
Handle accurate time even if there's a long delay between accumulated clock cycles.
About this one.. we talked a lot about the importance of timekeeping at the first Montreal Tracing Summit this week. Actually, someone mentioned a very interesting point : in order to be able to synchronize traces taken from the machine with traces taken on external hardware (i.e. memory bus tracer on Freescale), taking the "real" counter value rather that using the "cumulated cycles" approach (which creates a virtual counted instead) would be better.
So I would recommend using an algorithm that would return a clock value which is the same as the underlying hardware counter.
Mathieu
Quoted 286 lines
Signed-off-by John Stultz <[email protected]>Signed-off-by Steven Rostedt <[email protected]>--- arch/powerpc/kernel/time.c | 3 +- arch/x86/kernel/vsyscall_64.c | 5 ++- include/asm-x86/vgtod.h | 2 - include/linux/clocksource.h | 58 ++++++++++++++++++++++++++++++++++++++++-- kernel/time/timekeeping.c | 36 +++++++++++++------------- 5 files changed, 82 insertions(+), 22 deletions(-)Index:
linux-mcount.git/arch/x86/kernel/vsyscall_64.c
===================================================================arch/x86/kernel/vsyscall_64.c +4
-1@@ -86,6 +86,7 @@ void update_vsyscall(struct timespec *wa vsyscall_gtod_data.clock.mask = clock->mask; vsyscall_gtod_data.clock.mult = clock->mult; vsyscall_gtod_data.clock.shift = clock->shift; + vsyscall_gtod_data.clock.cycle_accumulated = clock->cycle_accumulated; vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec; vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec; vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic;@@ -121,7 +122,7 @@ static __always_inline long time_syscall static __always_inline void do_vgettimeofday(struct timeval * tv) { -cycle_t now, base, mask, cycle_delta; +cycle_t now, base, accumulated, mask, cycle_delta;unsigned seq;unsigned long mult, shift, nsec;cycle_t (*vread)(void);@@ -135,6 +136,7 @@ static __always_inline void do_vgettimeo } now =vread (); base = __vsyscall_gtod_data.clock.cycle_last; + accumulated = __vsyscall_gtod_data.clock.cycle_accumulated; mask = __vsyscall_gtod_data.clock.mask; mult = __vsyscall_gtod_data.clock.mult; shift = __vsyscall_gtod_data.clock.shift;@@ -145,6 +147,7 @@ static __always_inline void do_vgettimeo/* calculate interval: */ cycle_delta = (now - base) & mask; + cycle_delta += accumulated;/* convert to nsecs: */ nsec += (cycle_delta * mult) >> shift;Index:
linux-mcount.git/include/asm-x86/vgtod.h
===================================================================include/asm-x86/vgtod.h +1
-1@@ -15,7 +15,7 @@ struct vsyscall_gtod_data {struct timezone sys_tz;struct {/* extract of a clocksource struct */ cycle_t (*vread)(void ); -cycle_t cycle_last; +cycle_t cycle_last, cycle_accumulated;cycle_t mask;u32 mult;u32 shift;Index:
linux-mcount.git/include/linux/clocksource.h
===================================================================include/linux/clocksource.h +56
-2@@ -50,8 +50,12 @@ struct clocksource;* @flags: flags describing special properties * @vread: vsyscall based read * @resume: resume function for the clocksource, if necessary +* @cycle_last: Used internally by timekeeping core, please ignore. +* @cycle_accumulated: Used internally by timekeeping core, please ignore. * @cycle_interval: Used internally by timekeeping core, please ignore. * @xtime_interval: Used internally by timekeeping core, please ignore. +* @xtime_nsec: Used internally by timekeeping core, please ignore. +* @error: Used internally by timekeeping core, please ignore. */ struct clocksource {/* @@ -82,7 +86,10 @@ struct clocksource {* Keep it in a different cache line to dirty no * more than one cache line. */ -cycle_t cycle_last____cacheline_aligned_in_smp ; +struct { +cycle_t cycle_last, cycle_accumulated; + }____cacheline_aligned_in_smp ; +u64 xtime_nsec;s64 error;@@ -168,11 +175,44 @@ static inline cycle_t clocksource_read(s }/** +* clocksource_get_cycles: - Access the clocksource's accumulated cycle value +* @cs: pointer to clocksource being read +* @now: current cycle value +* +* Uses the clocksource to return the current cycle_t value. +* NOTE!!!: This is different from clocksource_read, because it +* returns the accumulated cycle value! Must hold xtime lock! +*/ +static inline cycle_t +clocksource_get_cycles (struct clocksource *cs,cycle_t now) +{ +cycle_t offset = (now - cs->cycle_last) & cs->mask; + offset += cs->cycle_accumulated; +return offset; +} + +/** +* clocksource_accumulate: - Accumulates clocksource cycles +* @cs: pointer to clocksource being read +* @now: current cycle value +* +* Used to avoids clocksource hardware overflow by periodically +* accumulating the current cycle delta. Must hold xtime write lock! +*/ +static inline void clocksource_accumulate (struct clocksource *cs,cycle_t now) +{ +cycle_t offset = (now - cs->cycle_last) & cs->mask; + cs->cycle_last = now; + cs->cycle_accumulated += offset; +} + +/** * cyc2ns - converts clocksource cycles to nanoseconds * @cs: Pointer to clocksource * @cycles: Cycles * * Uses the clocksource and ntp ajdustment to convert cycle_ts to nanoseconds. +* Must hold xtime lock! * * XXX - This could use some mult_lxl_ll() asm optimization */ @@ -184,13 +224,27 @@ static inline s64 cyc2ns(struct clocksou }/** +* ns2cyc - converts nanoseconds to clocksource cycles +* @cs: Pointer to clocksource +* @nsecs: Nanoseconds +*/ +static inline cycle_t ns2cyc (struct clocksource *cs,u64 nsecs) +{ +cycle_t ret = nsecs << cs->shift; + +do_div (ret, cs->mult +1 ); + +return ret; +} + +/** * clocksource_calculate_interval - Calculates a clocksource interval struct * * @c: Pointer to clocksource. * @length_nsec: Desired interval length in nanoseconds. * * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment -* pair and interval request. +* pair and interval request. Must hold xtime_lock! * * Unless you're the timekeeping code, you should not be using this! */ Index:
linux-mcount.git/kernel/time/timekeeping.c
===================================================================kernel/time/timekeeping.c +19
-17@@ -66,16 +66,10 @@ static struct clocksource *clock; /* poi*/ static inline s64 __get_nsec_offset (void ) { -cycle_t cycle_now, cycle_delta; +cycle_t cycle_delta;s64 ns_offset; -/* read clocksource: */ - cycle_now =clocksource_read (clock); - -/* calculate the delta since the last update_wall_time: */ - cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; - -/* convert to nanoseconds: */ + cycle_delta =clocksource_get_cycles (clock,clocksource_read (clock)); ns_offset =cyc2ns (clock, cycle_delta);return ns_offset;@@ -195,7 +189,7 @@ static void change_clocksource(void) clock = new; clock->cycle_last = now; - + clock->cycle_accumulated =0 ; clock->error =0 ; clock->xtime_nsec =0 ;clocksource_calculate_interval (clock,NTP_INTERVAL_LENGTH );@@ -205,9 +199,15 @@ static void change_clocksource(void)printk (KERN_INFO "Time: %s clocksource has been installed.\n" , clock->name); } + +void timekeeping_accumulate (void ) +{ +clocksource_accumulate (clock,clocksource_read (clock)); +}#else static inline void change_clocksource (void ) { }static inline s64 __get_nsec_offset (void ) {return 0 ; } +void timekeeping_accumulate (void ) { }#endif /** @@ -302,6 +302,7 @@ static int timekeeping_resume(struct systimespec_add_ns (&xtime, timekeeping_suspend_nsecs);/* re-base the last cycle value */ clock->cycle_last =clocksource_read (clock); + clock->cycle_accumulated =0 ; clock->error =0 ; timekeeping_suspended =0 ;write_sequnlock_irqrestore (&xtime_lock, flags);@@ -448,27 +449,28 @@ static void clocksource_adjust(s64 offse*/ void update_wall_time (void ) { -cycle_t offset; +cycle_t cycle_now;/* Make sure we're fully resumed: */ if (unlikely (timekeeping_suspended))return ;#ifdef CONFIG_GENERIC_TIME - offset = (clocksource_read (clock) - clock->cycle_last) & clock->mask; + cycle_now =clocksource_read (clock);#else - offset = clock->cycle_interval; + cycle_now = clock->cycle_last + clock->cycle_interval;#endif +clocksource_accumulate (clock, cycle_now); + clock->xtime_nsec += (s64 )xtime.tv_nsec << clock->shift;/* normally this loop will run just once, however in the * case of lost or late ticks, it will accumulate correctly. */ -while (offset >= clock->cycle_interval) { +while (clock->cycle_accumulated >= clock->cycle_interval) {/* accumulate one interval */ clock->xtime_nsec += clock->xtime_interval; - clock->cycle_last += clock->cycle_interval; - offset -= clock->cycle_interval; + clock->cycle_accumulated -= clock->cycle_interval;if (clock->xtime_nsec >= (u64 )NSEC_PER_SEC << clock->shift) { clock->xtime_nsec -= (u64 )NSEC_PER_SEC << clock->shift;@@ -482,13 +484,13 @@ void update_wall_time(void) }/* correct the clock when NTP error is too big */ -clocksource_adjust (offset); +clocksource_adjust (clock->cycle_accumulated);/* store full nanoseconds into xtime */ xtime.tv_nsec = (s64 )clock->xtime_nsec >> clock->shift; clock->xtime_nsec -= (s64 )xtime.tv_nsec << clock->shift; -update_xtime_cache (cyc2ns (clock, offset)); +update_xtime_cache (cyc2ns (clock, clock->cycle_accumulated));/* check to see if there is a new clocksource to use */ change_clocksource ();Index:
linux-mcount.git/arch/powerpc/kernel/time.c
===================================================================arch/powerpc/kernel/time.c +2
-1@@ -773,7 +773,8 @@ void update_vsyscall(struct timespec *wa stamp_xsec = (u64 ) xtime.tv_nsec *XSEC_PER_SEC ;do_div (stamp_xsec,1000000000 ); stamp_xsec += (u64 ) xtime.tv_sec *XSEC_PER_SEC ; -update_gtod (clock->cycle_last, stamp_xsec, t2x); +update_gtod (clock->cycle_last-clock->cycle_accumulated, + stamp_xsec, t2x); }void update_vsyscall_tz (void )Signature · 1 line
--
Signature · 3 lines
Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Re: [PATCH 06/23 -v8] handle accurate time keeping over long delays
Quoted 15 lines · Mathieu Desnoyers
Quoted 5 lines · * Steven Rostedt ([email protected]) wrote:
From: John Stultz <[email protected]>
Handle accurate time even if there's a long delay between accumulated clock cycles.
About this one.. we talked a lot about the importance of timekeeping at the first Montreal Tracing Summit this week. Actually, someone mentioned a very interesting point : in order to be able to synchronize traces taken from the machine with traces taken on external hardware (i.e. memory bus tracer on Freescale), taking the "real" counter value rather that using the "cumulated cycles" approach (which creates a virtual counted instead) would be better.
Quoted 2 lines
So I would recommend using an algorithm that would return a clock value which is the same as the underlying hardware counter.
Hmm. It is an interesting issue. Clearly having the raw cycle value match up so hardware analysis could be mapped to software timestamps would be useful(although obscure) feature. However with the variety of clocksources, dealing properly with the clocksource wrap issue (ACPI PM for instance wraps about every 5 seconds) also has to be addressed.
I think you were mentioning an idea that required some work on the read side to handle the wraps, basically managing the high order bits by hand. This sounds like it would be an additional feature that could be added on to the infrastructure being provided in the get_monotonic_cycles() patch. No?
However, all of the above is a separate issue then what this (the timekeeping over long delay) patch addresses, as it is not really directly related to the get_monotonic_cycles() patch, but instead allows for correct timekeeping, making update_wall_time() to function properly if it was deferred for longer then the clocksource's wrap time.
thanks
-john
Re: [PATCH 06/23 -v8] handle accurate time keeping over long delays
Quoted 26 lines · * John Stultz ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 5 lines · * Steven Rostedt ([email protected]) wrote:
From: John Stultz <[email protected]>
Handle accurate time even if there's a long delay between accumulated clock cycles.
About this one.. we talked a lot about the importance of timekeeping at the first Montreal Tracing Summit this week. Actually, someone mentioned a very interesting point : in order to be able to synchronize traces taken from the machine with traces taken on external hardware (i.e. memory bus tracer on Freescale), taking the "real" counter value rather that using the "cumulated cycles" approach (which creates a virtual counted instead) would be better.
Quoted 2 lines
So I would recommend using an algorithm that would return a clock value which is the same as the underlying hardware counter.
Hmm. It is an interesting issue. Clearly having the raw cycle value match up so hardware analysis could be mapped to software timestamps would be useful(although obscure) feature. However with the variety of clocksources, dealing properly with the clocksource wrap issue (ACPI PM for instance wraps about every 5 seconds) also has to be addressed.
Quoted 6 lines
I think you were mentioning an idea that required some work on the read side to handle the wraps, basically managing the high order bits by hand. This sounds like it would be an additional feature that could be added on to the infrastructure being provided in the
get_monotonic_cycles()patch. No?
Yup, exactly.
Quoted 7 lines
However, all of the above is a separate issue then what this (the timekeeping over long delay) patch addresses, as it is not really directly related to the
get_monotonic_cycles()patch, but instead allows for correct timekeeping, makingupdate_wall_time()to function properly if it was deferred for longer then the clocksource's wrap time.
I agree, that could apply on top of the monotonic cycles patch. It's just a different way to see it : dealing with wrapping TSC bits, returning the LSBs given by the hardware, rather than simply accumulating time. This is what the patch I sent earlier (which I use in LTTng) does. I currently expects 32 LSBs to be given by the hardware, but it would be trivial to extend it to support any given number of hardware LSBs.
Mathieu
Quoted 4 lines
thanks
-john
Signature · 3 lines
Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Steven Rostedt
reviewerRe: [PATCH 06/23 -v8] handle accurate time keeping over long delays
Quoted 37 lines · Mathieu Desnoyers
Quoted 20 lines
Quoted 11 lines
Quoted 2 lines
accumulated clock cycles.
About this one.. we talked a lot about the importance of timekeeping at the first Montreal Tracing Summit this week. Actually, someone mentioned a very interesting point : in order to be able to synchronize traces taken from the machine with traces taken on external hardware (i.e. memory bus tracer on Freescale), taking the "real" counter value rather that using the "cumulated cycles" approach (which creates a virtual counted instead) would be better.
Quoted 2 lines
So I would recommend using an algorithm that would return a clock value which is the same as the underlying hardware counter.
Hmm. It is an interesting issue. Clearly having the raw cycle value match up so hardware analysis could be mapped to software timestamps would be useful(although obscure) feature. However with the variety of clocksources, dealing properly with the clocksource wrap issue (ACPI PM for instance wraps about every 5 seconds) also has to be addressed.
Quoted 6 lines
I think you were mentioning an idea that required some work on the read side to handle the wraps, basically managing the high order bits by hand. This sounds like it would be an additional feature that could be added on to the infrastructure being provided in the
get_monotonic_cycles()patch. No?Yup, exactly.
Quoted 7 lines
However, all of the above is a separate issue then what this (the timekeeping over long delay) patch addresses, as it is not really directly related to the
get_monotonic_cycles()patch, but instead allows for correct timekeeping, makingupdate_wall_time()to function properly if it was deferred for longer then the clocksource's wrap time.
Quoted 8 lines
I agree, that could apply on top of the monotonic cycles patch. It's just a different way to see it : dealing with wrapping TSC bits, returning the LSBs given by the hardware, rather than simply accumulating time. This is what the patch I sent earlier (which I use in LTTng) does. I currently expects 32 LSBs to be given by the hardware, but it would be trivial to extend it to support any given number of hardware LSBs.
So you are saying that you can trivally make it work with a clock that is, say 24 bits? And this same code can work if we boot up with a clock with 32 bits or more?
-- Steve
Re: [PATCH 06/23 -v8] handle accurate time keeping over long delays
Quoted 51 lines · * Steven Rostedt ([email protected]) wrote:
Quoted 37 lines · Mathieu Desnoyers
Quoted 20 lines
Quoted 11 lines
Quoted 2 lines
accumulated clock cycles.
About this one.. we talked a lot about the importance of timekeeping at the first Montreal Tracing Summit this week. Actually, someone mentioned a very interesting point : in order to be able to synchronize traces taken from the machine with traces taken on external hardware (i.e. memory bus tracer on Freescale), taking the "real" counter value rather that using the "cumulated cycles" approach (which creates a virtual counted instead) would be better.
Quoted 2 lines
So I would recommend using an algorithm that would return a clock value which is the same as the underlying hardware counter.
Hmm. It is an interesting issue. Clearly having the raw cycle value match up so hardware analysis could be mapped to software timestamps would be useful(although obscure) feature. However with the variety of clocksources, dealing properly with the clocksource wrap issue (ACPI PM for instance wraps about every 5 seconds) also has to be addressed.
Quoted 6 lines
I think you were mentioning an idea that required some work on the read side to handle the wraps, basically managing the high order bits by hand. This sounds like it would be an additional feature that could be added on to the infrastructure being provided in the
get_monotonic_cycles()patch. No?Yup, exactly.
Quoted 7 lines
However, all of the above is a separate issue then what this (the timekeeping over long delay) patch addresses, as it is not really directly related to the
get_monotonic_cycles()patch, but instead allows for correct timekeeping, makingupdate_wall_time()to function properly if it was deferred for longer then the clocksource's wrap time.Quoted 8 lines
I agree, that could apply on top of the monotonic cycles patch. It's just a different way to see it : dealing with wrapping TSC bits, returning the LSBs given by the hardware, rather than simply accumulating time. This is what the patch I sent earlier (which I use in LTTng) does. I currently expects 32 LSBs to be given by the hardware, but it would be trivial to extend it to support any given number of hardware LSBs.
Quoted 6 lines
So you are saying that you can trivally make it work with a clock that is, say 24 bits? And this same code can work if we boot up with a clock with 32 bits or more?
-- Steve
Yes, with this updated version. It supports HW clocks with various number of bits. I limit myself to the 32 LSBs of the clock even if the clock provides more bits for performance reasons. This module is aimed at 32 bits architectures because such tricks are not necessary on 64 bits architectures given they provide atomic 64 bits updates.
(it's only compile-tested)
LTTng timestamp
LTTng synthetic TSC code for timestamping. Extracts 64 bits tsc from a [0..32] bits counter, kept up to date by periodical timer interrupt. Lockless.
Quoted 3 lines
do you actually use the RCU internals? or do you just reimplement an RCU algorithm?
Nope, I don't use RCU internals in this code. Preempt disable seemed like the best way to handle this utterly short code path and I wanted the write side to be fast enough to be called periodically. What I do is:
- Disable preemption at the read-side :
it makes sure the pointer I get will point to a data structure that will never change while I am in the preempt disabled code. (see *)
- I use per-cpu data to allow the read-side to be as fast as possible (only need to disable preemption, does not race against other CPUs and won't generate cache line bouncing). It also allows dealing with unsynchronized TSCs if needed.
- Periodical write side : it's called from an IPI running on each CPU.
(*) We expect the read-side (preempt off region) to last shorter than the interval between IPI updates so we can guarantee the data structure it uses won't be modified underneath it. Since the IPI update is launched each seconds or so (depends on the frequency of the counter we are trying to extend), it's more than ok.
Changelog:
- Support [0..32] bits -> 64 bits.
I volountarily limit the code to use at most 32 bits of the hardware clock for performance considerations. If this is a problem it could be changed. Also, the algorithm is aimed at a 32 bits architecture. The code becomes muuuch simpler on a 64 bits arch, since we can do the updates atomically.
--- init/Kconfig | 2 ltt/Kconfig | 17 ++++ ltt/Makefile | 1 ltt/ltt-timestamp.c | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 230 insertions(+)
ltt/ltt-timestamp.c new file +210 -0
@@ -0,0 +1,210 @@ +/* +* (C) Copyright 2006,2007 - +* Mathieu Desnoyers ([email protected]) +* +* notes : ltt-timestamp timer-based clock cannot be used for early tracing in +* the boot process, as it depends on timer interrupts. +* +* The timer is only on one CPU to support hotplug. +* We have the choice between schedule_delayed_work_on and an IPI to get each +* CPU to write the heartbeat. IPI has been chosen because it is considered +* faster than passing through the timer to get the work scheduled on all the +* CPUs. +*/ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/delay.h> +#include <linux/timer.h> +#include <linux/workqueue.h> +#include <linux/cpu.h> +#include <linux/timex.h> +#include <linux/bitops.h> +#include <linux/ltt.h> +#include <linux/smp.h> +#include <linux/sched.h> /* FIX for m68k local_irq_enable in on_each_cpu */ + +/* +* Number of hardware clock bits. The higher order bits are expected to be 0. +* If the hardware clock source has more than 32 bits, the bits higher than the +* 32nd will be truncated by a cast to a 32 bits unsigned. Range : 1 - 32. +* (too few bits would be unrealistic though, since we depend on the timer to +* detect the overflows). +*/ +#define HW_BITS 32 + +#define HW_BITMASK ((1ULL << HW_BITS) - 1) +#define HW_LSB (hw) ((hw) & HW_BITMASK) +#define SW_MSB (sw) ((sw) & ~HW_BITMASK) + +/* Expected maximum interrupt latency in ms : 15ms, *2 for security */ +#define EXPECTED_INTERRUPT_LATENCY 30 + +atomic_t lttng_generic_clock; +EXPORT_SYMBOL (lttng_generic_clock); + +static struct timer_list stsc_timer; +static unsigned int precalc_expire; + +static struct synthetic_tsc_struct { +union { +u64 val; +struct { +#ifdef __BIG_ENDIAN +u32 msb; +u32 lsb; +#else +u32 lsb; +u32 msb; +#endif + } sel; + } tsc[2 ]; +unsigned int index;/* Index of the current synth. tsc. */ +}____cacheline_aligned synthetic_tsc[NR_CPUS ]; + +/* Called from IPI : either in interrupt or process context */ +static void ltt_update_synthetic_tsc (void ) +{ +struct synthetic_tsc_struct *cpu_synth; +u32 tsc; + +preempt_disable (); + cpu_synth = &synthetic_tsc[smp_processor_id ()]; + tsc =ltt_get_timestamp32 ();/* Hardware clocksource read */ + +if (tsc <HW_LSB (cpu_synth->tsc[cpu_synth->index].sel.lsb)) { +unsigned int new_index =1 - cpu_synth->index;/* 0 <-> 1 */ +/* +* Overflow +* Non atomic update of the non current synthetic TSC, followed +* by an atomic index change. There is no write concurrency, +* so the index read/write does not need to be atomic. +*/ + cpu_synth->tsc[new_index].val = + (SW_MSB (cpu_synth->tsc[cpu_synth->index].val) + | (u64 )tsc) + (1ULL <<HW_BITS ); + cpu_synth->index = new_index;/* atomic change of index */ + }else { +/* +* No overflow : We know that the only bits changed are +* contained in the 32 LSBs, which can be written to atomically. +*/ + cpu_synth->tsc[cpu_synth->index].sel.lsb = +SW_MSB (cpu_synth->tsc[cpu_synth->index].sel.lsb) | tsc; + } +preempt_enable (); +} + +/* Called from buffer switch : in _any_ context (even NMI) */ +u64 ltt_read_synthetic_tsc (void ) +{ +struct synthetic_tsc_struct *cpu_synth; +u64 ret; +unsigned int index; +u32 tsc; + +preempt_disable (); + cpu_synth = &synthetic_tsc[smp_processor_id ()]; + index = cpu_synth->index;/* atomic read */ + tsc =ltt_get_timestamp32 ();/* Hardware clocksource read */ + +/* Overflow detection */ +if (unlikely (tsc <HW_LSB (cpu_synth->tsc[index].sel.lsb))) + ret = (SW_MSB (cpu_synth->tsc[index].val) | (u64 )tsc) + + (1ULL <<HW_BITS ); +else + ret =SW_MSB (cpu_synth->tsc[index].val) | (u64 )tsc; +preempt_enable (); +return ret; +} +EXPORT_SYMBOL_GPL (ltt_read_synthetic_tsc); + +static void synthetic_tsc_ipi (void *info) +{ +ltt_update_synthetic_tsc (); +} + +/* We need to be in process context to do an IPI */ +static void synthetic_tsc_work (struct work_struct *work) +{ +on_each_cpu (synthetic_tsc_ipi,NULL ,1 ,1 ); +} +static DECLARE_WORK(stsc_work, synthetic_tsc_work); + +/* +* stsc_timer : - Timer function synchronizing synthetic TSC. +* @data: unused +* +* Guarantees at least 1 execution before low word of TSC wraps. +*/ +static void stsc_timer_fct (unsigned long data) +{ +PREPARE_WORK (&stsc_work, synthetic_tsc_work); +schedule_work (&stsc_work); + +mod_timer (&stsc_timer, jiffies + precalc_expire); +} + +/* +* precalc_stsc_interval: - Precalculates the interval between the clock +* wraparounds. +*/ +static int __init precalc_stsc_interval (void ) +{ + precalc_expire = + (HW_BITMASK / ((ltt_frequency () /HZ *ltt_freq_scale ()) <<1 ) + -1 - (EXPECTED_INTERRUPT_LATENCY *HZ /1000 )) >>1 ; +WARN_ON (precalc_expire ==0 ); +printk (KERN_DEBUG "Synthetic TSC timer will fire each %u jiffies.\n" , + precalc_expire); +return 0 ; +} + +/* +* hotcpu_callback - CPU hotplug callback +* @nb: notifier block +* @action: hotplug action to take +* @hcpu: CPU number +* +* Sets the new CPU's current synthetic TSC to the same value as the +* currently running CPU. +* +* Returns the success/failure of the operation. (NOTIFY_OK, NOTIFY_BAD) +*/ +static int __cpuinit hotcpu_callback (struct notifier_block *nb, +unsigned long action, +void *hcpu) +{ +unsigned int hotcpu = (unsigned long )hcpu; +struct synthetic_tsc_struct *cpu_synth; +u64 local_count; + +switch (action) { +case CPU_UP_PREPARE : + cpu_synth = &synthetic_tsc[hotcpu]; + local_count =ltt_read_synthetic_tsc (); + cpu_synth->tsc[0 ].val = local_count; + cpu_synth->index =0 ; +smp_wmb ();/* Writing in data of CPU about to come up */ +break ; +case CPU_ONLINE : +/* As we are preemptible, make sure it runs on the right cpu */ +smp_call_function_single (hotcpu, synthetic_tsc_ipi,NULL ,1 ,0 ); +break ; + } +return NOTIFY_OK ; +} + +/* Called from one CPU, before any tracing starts, to init each structure */ +static int __init ltt_init_synthetic_tsc (void ) +{ +hotcpu_notifier (hotcpu_callback,3 ); +precalc_stsc_interval (); +init_timer (&stsc_timer); + stsc_timer.function = stsc_timer_fct; + stsc_timer.expires = jiffies + precalc_expire; +add_timer (&stsc_timer); +return 0 ; +} + +__initcall (ltt_init_synthetic_tsc);
ltt/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +menu "Linux Trace Toolkit" + +config LTT_TIMESTAMP + bool "LTTng fine-grained timestamping" + default y + help + Allow fine-grained timestamps to be taken from tracing applications. + +config HAVE_LTT_CLOCK + def_bool n + +config HAVE_LTT_SYNTHETIC_TSC + bool + default y if (!HAVE_LTT_CLOCK) + default n if HAVE_LTT_CLOCK + +endmenu
ltt/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_HAVE_LTT_SYNTHETIC_TSC) += ltt-timestamp.o
init/Kconfig +2 -0
@@ -691,6 +691,8 @@ config MARKERS Place an empty function call at each marker site. Can be dynamically changed for a probe function. +source "ltt/Kconfig" + source "arch/Kconfig" config DISABLE_IMMEDIATE
Signature · 3 lines
Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Steven Rostedt
reviewerPATCH 7/23 initialize the clock source to jiffies clock.
In reply to Steven RostedtThe latency tracer can call clocksource_read very early in bootup and before the clock source variable has been initialized. This results in a crash at boot up (even before earlyprintk is initialized). Since the clock->read variable points to NULL.
This patch simply initializes the clock to use clocksource_jiffies, so that any early user of clocksource_read will not crash.
--- include/linux/clocksource.h | 3 +++ kernel/time/timekeeping.c | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
include/linux/clocksource.h +3 -0
@@ -273,6 +273,9 @@ extern struct clocksource* clocksource_gextern void clocksource_change_rating (struct clocksource *cs,int rating);extern void clocksource_resume (void ); +/* used to initialize clock */ +extern struct clocksource clocksource_jiffies; +#ifdef CONFIG_GENERIC_TIME_VSYSCALL extern void update_vsyscall (struct timespec *ts,struct clocksource *c);extern void update_vsyscall_tz (void );
kernel/time/timekeeping.c +7 -2
@@ -53,8 +53,13 @@ static inline void update_xtime_cache(u6timespec_add_ns (&xtime_cache, nsec); } -static struct clocksource *clock;/* pointer to current clocksource */ - +/* +* pointer to current clocksource +* Just in case we use clocksource_read before we initialize +* the actual clock source. Instead of calling a NULL read pointer +* we return jiffies. +*/ +static struct clocksource *clock = &clocksource_jiffies;#ifdef CONFIG_GENERIC_TIME /**
Steven Rostedt
reviewerPATCH 8/23 add get_monotonic_cycles
In reply to Steven RostedtFrom: John Stultz <[email protected]>
The latency tracer needs a way to get an accurate time without grabbing any locks. Locks themselves might call the latency tracer and cause at best a slow down.
This patch adds get_monotonic_cycles that returns cycles from a reliable clock source in a monotonic fashion.
--- include/linux/clocksource.h | 54 +++++++++++++++++++++++++++++++++++++------- kernel/time/timekeeping.c | 26 +++++++++++++++++++-- 2 files changed, 70 insertions(+), 10 deletions(-)
include/linux/clocksource.h +46 -8
@@ -88,8 +88,16 @@ struct clocksource {*/ struct {cycle_t cycle_last, cycle_accumulated; - }____cacheline_aligned_in_smp ; +/* base structure provides lock-free read +* access to a virtualized 64bit counter +* Uses RCU-like update. +*/ +struct { +cycle_t cycle_base_last, cycle_base; + } base[2 ]; +int base_num; + }____cacheline_aligned_in_smp ;u64 xtime_nsec;s64 error;
@@ -175,19 +183,30 @@ static inline cycle_t clocksource_read(s }/** -* clocksource_get_cycles: - Access the clocksource's accumulated cycle value +* clocksource_get_basecycles: - get the clocksource's accumulated cycle value * @cs: pointer to clocksource being read * @now: current cycle value * * Uses the clocksource to return the current cycle_t value. * NOTE!!!: This is different from clocksource_read, because it -* returns the accumulated cycle value! Must hold xtime lock! +* returns a 64bit wide accumulated value. */ static inline cycle_t -clocksource_get_cycles (struct clocksource *cs,cycle_t now) +clocksource_get_basecycles (struct clocksource *cs) { -cycle_t offset = (now - cs->cycle_last) & cs->mask; - offset += cs->cycle_accumulated; +int num; +cycle_t now, offset; + +preempt_disable (); + num = cs->base_num; +/* base_num is shared, and some archs are wacky */ +smp_read_barrier_depends (); + now =clocksource_read (cs); + offset = (now - cs->base[num].cycle_base_last); + offset &= cs->mask; + offset += cs->base[num].cycle_base; +preempt_enable (); +return offset; }
@@ -197,11 +216,27 @@ clocksource_get_cycles(struct clocksourc* @now: current cycle value * * Used to avoids clocksource hardware overflow by periodically -* accumulating the current cycle delta. Must hold xtime write lock! +* accumulating the current cycle delta. Uses RCU-like update, but +* ***still requires the xtime_lock is held for writing!*** */ static inline void clocksource_accumulate (struct clocksource *cs,cycle_t now) { -cycle_t offset = (now - cs->cycle_last) & cs->mask; +/* +* First update the monotonic base portion. +* The dual array update method allows for lock-free reading. +* 'num' is always 1 or 0. +*/ +int num =1 - cs->base_num; +cycle_t offset = (now - cs->base[1 -num].cycle_base_last); + offset &= cs->mask; + cs->base[num].cycle_base = cs->base[1 -num].cycle_base + offset; + cs->base[num].cycle_base_last = now; +/* make sure this array is visible to the world first */ +smp_wmb (); + cs->base_num = num; + +/* Now update the cycle_accumulated portion */ + offset = (now - cs->cycle_last) & cs->mask; cs->cycle_last = now; cs->cycle_accumulated += offset; }
@@ -272,6 +307,9 @@ extern int clocksource_register(struct cextern struct clocksource *clocksource_get_next (void );extern void clocksource_change_rating (struct clocksource *cs,int rating);extern void clocksource_resume (void ); +extern cycle_t get_monotonic_cycles (void ); +extern unsigned long cycles_to_usecs (cycle_t cycles); +extern cycle_t usecs_to_cycles (unsigned long usecs);/* used to initialize clock */ extern struct clocksource clocksource_jiffies;
kernel/time/timekeeping.c +24 -2
@@ -71,10 +71,12 @@ static struct clocksource *clock = &cloc*/ static inline s64 __get_nsec_offset (void ) { -cycle_t cycle_delta; +cycle_t now, cycle_delta;s64 ns_offset; - cycle_delta =clocksource_get_cycles (clock,clocksource_read (clock)); + now =clocksource_read (clock); + cycle_delta = (now - clock->cycle_last) & clock->mask; + cycle_delta += clock->cycle_accumulated; ns_offset =cyc2ns (clock, cycle_delta);return ns_offset;
@@ -103,6 +105,26 @@ static inline void __get_realtime_clock_timespec_add_ns (ts, nsecs); } +cycle_t notrace get_monotonic_cycles(void) +{ +return clocksource_get_basecycles (clock); +} + +unsigned long notrace cycles_to_usecs (cycle_t cycles) +{ +u64 ret =cyc2ns (clock, cycles); + + ret +=NSEC_PER_USEC /2 ;/* For rounding in do_div() */ +do_div (ret,NSEC_PER_USEC ); + +return ret; +} + +cycle_t notrace usecs_to_cycles(unsigned long usecs) +{ +return ns2cyc (clock, (u64 )usecs *1000 ); +} +/** * getnstimeofday - Returns the time of day in a timespec * @ts: pointer to the timespec to be set
Steven Rostedt
reviewerPATCH 9/23 add notrace annotations to timing events
In reply to Steven RostedtThis patch adds notrace annotations to timer functions that will be used by tracing. This helps speed things up and also keeps the ugliness of printing these functions down.
--- arch/x86/kernel/apic_32.c | 2 +- arch/x86/kernel/hpet.c | 2 +- arch/x86/kernel/time_32.c | 2 +- arch/x86/kernel/tsc_32.c | 2 +- arch/x86/kernel/tsc_64.c | 4 ++-- arch/x86/lib/delay_32.c | 6 +++--- drivers/clocksource/acpi_pm.c | 8 ++++---- 7 files changed, 13 insertions(+), 13 deletions(-)
arch/x86/kernel/apic_32.c +1 -1
@@ -577,7 +577,7 @@ static void local_apic_timer_interrupt(v* interrupt as well. Thus we cannot inline the local irq ... ] */ -void fastcall smp_apic_timer_interrupt(struct pt_regs *regs) +notrace fastcall void smp_apic_timer_interrupt(struct pt_regs *regs) {struct pt_regs *old_regs =set_irq_regs (regs);
arch/x86/kernel/hpet.c +1 -1
@@ -295,7 +295,7 @@ static int hpet_legacy_next_event(unsign/* * Clock source related code */ -static cycle_t read_hpet (void ) +static notrace cycle_t read_hpet(void) {return (cycle_t )hpet_readl (HPET_COUNTER ); }
arch/x86/kernel/time_32.c +1 -1
@@ -122,7 +122,7 @@ static int set_rtc_mmss(unsigned long noint timer_ack; -unsigned long profile_pc (struct pt_regs *regs) +notrace unsigned long profile_pc (struct pt_regs *regs) {unsigned long pc =instruction_pointer (regs);
arch/x86/kernel/tsc_32.c +1 -1
@@ -269,7 +269,7 @@ core_initcall(cpufreq_tsc);static unsigned long current_tsc_khz =0 ; -static cycle_t read_tsc (void ) +static notrace cycle_t read_tsc(void) {cycle_t ret;
arch/x86/kernel/tsc_64.c +2 -2
@@ -248,13 +248,13 @@ __setup("notsc", notsc_setup);/* clock source code: */ -static cycle_t read_tsc (void ) +static notrace cycle_t read_tsc(void) {cycle_t ret = (cycle_t )get_cycles_sync ();return ret; } -static cycle_t __vsyscall_fn vread_tsc(void) +static notrace cycle_t __vsyscall_fn vread_tsc(void) {cycle_t ret = (cycle_t )get_cycles_sync ();return ret;
arch/x86/lib/delay_32.c +3 -3
@@ -24,7 +24,7 @@#endif /* simple loop based delay: */ -static void delay_loop (unsigned long loops) +static notrace void delay_loop(unsigned long loops) {int d0;
@@ -39,7 +39,7 @@ static void delay_loop(unsigned long loo }/* TSC based delay: */ -static void delay_tsc (unsigned long loops) +static notrace void delay_tsc(unsigned long loops) {unsigned long bclock, now;
@@ -72,7 +72,7 @@ int read_current_timer(unsigned long *tireturn -1 ; } -void __delay (unsigned long loops) +notrace void __delay(unsigned long loops) {delay_fn (loops); }
drivers/clocksource/acpi_pm.c +4 -4
@@ -30,13 +30,13 @@*/ u32 pmtmr_ioport__read_mostly ; -static inline u32 read_pmtmr (void ) +static inline notrace u32 read_pmtmr(void) {/* mask the output to 24 bits */ return inl (pmtmr_ioport) &ACPI_PM_MASK ; } -u32 acpi_pm_read_verified (void ) +notrace u32 acpi_pm_read_verified(void) {u32 v1 =0 , v2 =0 , v3 =0 ;
@@ -56,12 +56,12 @@ u32 acpi_pm_read_verified(void)return v2; } -static cycle_t acpi_pm_read_slow (void ) +static notrace cycle_t acpi_pm_read_slow(void) {return (cycle_t )acpi_pm_read_verified (); } -static cycle_t acpi_pm_read (void ) +static notrace cycle_t acpi_pm_read(void) {return (cycle_t )read_pmtmr (); }
Steven Rostedt
reviewerPATCH 10/23 mcount tracer add preempt_enable/disable notrace macros
In reply to Steven RostedtThe tracer may need to call preempt_enable and disable functions for time keeping and such. The trace gets ugly when we see these functions show up for all traces. To make the output cleaner this patch adds preempt_enable_notrace and preempt_disable_notrace to be used by tracer (and debugging) functions.
--- include/linux/clocksource.h | 5 +++-- include/linux/preempt.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-)
include/linux/clocksource.h +3 -2
@@ -197,7 +197,8 @@ clocksource_get_basecycles(struct clocksint num;cycle_t now, offset; -preempt_disable (); +/* This code is used for tracing. */ +preempt_disable_notrace (); num = cs->base_num;/* base_num is shared, and some archs are wacky */ smp_read_barrier_depends ();
@@ -205,7 +206,7 @@ clocksource_get_basecycles(struct clocks offset = (now - cs->base[num].cycle_base_last); offset &= cs->mask; offset += cs->base[num].cycle_base; -preempt_enable (); +preempt_enable_notrace ();return offset; }
include/linux/preempt.h +32 -0
@@ -52,6 +52,34 @@ do { \preempt_check_resched (); \ } while (0) +/* For debugging and tracer internals only! */ +#define add_preempt_count_notrace (val) \ + do { preempt_count() += (val); } while (0) +#define sub_preempt_count_notrace (val) \ + do { preempt_count() -= (val); } while (0) +#define inc_preempt_count_notrace () add_preempt_count_notrace(1) +#define dec_preempt_count_notrace () sub_preempt_count_notrace(1) + +#define preempt_disable_notrace () \ +do { \ + inc_preempt_count_notrace(); \ + barrier(); \ +} while (0) + +#define preempt_enable_no_resched_notrace () \ +do { \ + barrier(); \ + dec_preempt_count_notrace(); \ +} while (0) + +/* preempt_check_resched is OK to trace */ +#define preempt_enable_notrace () \ +do { \ + preempt_enable_no_resched_notrace(); \ + barrier(); \ + preempt_check_resched(); \ +} while (0) +#else #define preempt_disable () do { } while (0)
@@ -59,6 +87,10 @@ do { \#define preempt_enable () do { } while (0)#define preempt_check_resched () do { } while (0) +#define preempt_disable_notrace () do { } while (0) +#define preempt_enable_no_resched_notrace () do { } while (0) +#define preempt_enable_notrace () do { } while (0) +#endif #ifdef CONFIG_PREEMPT_NOTIFIERS
Steven Rostedt
reviewerPATCH 11/23 mcount based trace in the form of a header file library
In reply to Steven RostedtThis is a simple trace that uses the mcount infrastructure. It is designed to be fast and small, and easy to use. It is useful to record things that happen over a very short period of time, and not to analyze the system in general.
An interface is added to the debugfs
/debugfs/tracing/
This patch adds the following files:
available_tracers
list of available tracers. Currently only "function" is available.
current_tracer
The trace that is currently active. Empty on start up. To switch to a tracer simply echo one of the tracers that are listed in available_tracers:
echo function > /debugfs/tracing/current_tracer
To disable the tracer:
echo disable > /debugfs/tracing/current_tracer
trace_ctrl
echoing "1" into this file starts the mcount function tracing
(if sysctl kernel.mcount_enabled=1)
echoing "0" turns it off.
latency_trace
This file is readonly and holds the result of the trace.
trace
This file outputs a easier to read version of the trace.
iter_ctrl
Controls the way the output of traces look.
So far there's two controls:
echoing in "symonly" will only show the kallsyms variables
without the addresses (if kallsyms was configured)
echoing in "verbose" will change the output to show
a lot more data, but not very easy to understand by humans.
echoing in "nosymonly" turns off symonly. echoing in "noverbose" turns off verbose.
The output of the function_trace file is as follows
"echo noverbose > /debugfs/tracing/iter_ctrl"
preemption latency trace v1.1.5 on 2.6.24-rc7-tst
--------------------------------------------------------------------
latency: 0 us, #419428/4361791, CPU#1 | (M:desktop VP:0, KP:0, SP:0 HP:0 #P:4)
-----------------
| task: -0 (uid:0 nice:0 policy:0 rt_prio:0)
-----------------
_------=> CPU#
/ _-----=> irqs-off
| / _----=> need-resched
|| / _---=> hardirq/softirq
||| / _--=> preempt-depth
|||| /
||||| delay
cmd pid ||||| time | caller
\ / ||||| \ | /
swapper-0 0d.h. 1595128us+: set_normalized_timespec+0x8/0x2d <c043841d> (ktime_get_ts+0x4a/0x4e <c04499d4>)
swapper-0 0d.h. 1595131us+: _spin_lock+0x8/0x18 <c0630690> (hrtimer_interrupt+0x6e/0x1b0 <c0449c56>)Or with verbose turned on:
"echo verbose > /debugfs/tracing/iter_ctrl"
preemption latency trace v1.1.5 on 2.6.24-rc7-tst
--------------------------------------------------------------------
latency: 0 us, #419428/4361791, CPU#1 | (M:desktop VP:0, KP:0, SP:0 HP:0 #P:4)
-----------------
| task: -0 (uid:0 nice:0 policy:0 rt_prio:0)
-----------------
swapper 0 0 9 00000000 00000000 [f3675f41] 1595.128ms (+0.003ms): set_normalized_timespec+0x8/0x2d <c043841d> (ktime_get_ts+0x4a/0x4e <c04499d4>)
swapper 0 0 9 00000000 00000001 [f3675f45] 1595.131ms (+0.003ms): _spin_lock+0x8/0x18 <c0630690> (hrtimer_interrupt+0x6e/0x1b0 <c0449c56>)
swapper 0 0 9 00000000 00000002 [f3675f48] 1595.135ms (+0.003ms): _spin_lock+0x8/0x18 <c0630690> (hrtimer_interrupt+0x6e/0x1b0 <c0449c56>)The "trace" file is not affected by the verbose mode, but is by the symonly.
echo "nosymonly" > /debugfs/tracing/iter_ctrl
tracer:
[ 81.479967] CPU 0: bash:3154 register_mcount_function+0x5f/0x66 <ffffffff80337a4d> <-- _spin_unlock_irqrestore+0xe/0x5a <ffffffff8048cc8f> [ 81.479967] CPU 0: bash:3154 _spin_unlock_irqrestore+0x3e/0x5a <ffffffff8048ccbf> <-- sub_preempt_count+0xc/0x7a <ffffffff80233d7b> [ 81.479968] CPU 0: bash:3154 sub_preempt_count+0x30/0x7a <ffffffff80233d9f> <-- in_lock_functions+0x9/0x24 <ffffffff8025a75d> [ 81.479968] CPU 0: bash:3154 vfs_write+0x11d/0x155 <ffffffff8029a043> <-- dnotify_parent+0x12/0x78 <ffffffff802d54fb> [ 81.479968] CPU 0: bash:3154 dnotify_parent+0x2d/0x78 <ffffffff802d5516> <-- _spin_lock+0xe/0x70 <ffffffff8048c910> [ 81.479969] CPU 0: bash:3154 _spin_lock+0x1b/0x70 <ffffffff8048c91d> <-- add_preempt_count+0xe/0x77 <ffffffff80233df7> [ 81.479969] CPU 0: bash:3154 add_preempt_count+0x3e/0x77 <ffffffff80233e27> <-- in_lock_functions+0x9/0x24 <ffffffff8025a75d>
echo "symonly" > /debugfs/tracing/iter_ctrl
tracer:
[ 81.479913] CPU 0: bash:3154 register_mcount_function+0x5f/0x66 <-- _spin_unlock_irqrestore+0xe/0x5a [ 81.479913] CPU 0: bash:3154 _spin_unlock_irqrestore+0x3e/0x5a <-- sub_preempt_count+0xc/0x7a [ 81.479913] CPU 0: bash:3154 sub_preempt_count+0x30/0x7a <-- in_lock_functions+0x9/0x24 [ 81.479914] CPU 0: bash:3154 vfs_write+0x11d/0x155 <-- dnotify_parent+0x12/0x78 [ 81.479914] CPU 0: bash:3154 dnotify_parent+0x2d/0x78 <-- _spin_lock+0xe/0x70 [ 81.479914] CPU 0: bash:3154 _spin_lock+0x1b/0x70 <-- add_preempt_count+0xe/0x77 [ 81.479914] CPU 0: bash:3154 add_preempt_count+0x3e/0x77 <-- in_lock_functions+0x9/0x24
--- lib/Makefile | 1 lib/tracing/Kconfig | 15 lib/tracing/Makefile | 3 lib/tracing/trace_function.c | 72 ++ lib/tracing/tracer.c | 1165 +++++++++++++++++++++++++++++++++++++++++++ lib/tracing/tracer.h | 96 +++ 6 files changed, 1352 insertions(+)
lib/tracing/Kconfig +15 -0
@@ -8,3 +8,18 @@ config HAVE_MCOUNT config MCOUNT bool select FRAME_POINTER + +config TRACING + bool + select DEBUG_FS + +config FUNCTION_TRACER + bool "Profiler instrumentation based tracer" + depends on DEBUG_KERNEL && HAVE_MCOUNT + select MCOUNT + select TRACING + help + Use profiler instrumentation, adding -pg to CFLAGS. This will + insert a call to an architecture specific __mcount routine, + that the debugging mechanism using this facility will hook by + providing a set of inline routines.
lib/tracing/Makefile +3 -0
@@ -1,3 +1,6 @@ obj-$(CONFIG_MCOUNT) += libmcount.o +obj-$(CONFIG_TRACING) += tracer.o +obj-$(CONFIG_FUNCTION_TRACER) += trace_function.o + libmcount-y := mcount.o
lib/tracing/tracer.c new file +1165 -0
1,165 lines not on this page · raw message
lib/tracing/tracer.h new file +96 -0
96 lines not on this page · raw message
lib/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-$(CONFIG_SWIOTLB) += swiotlb.o obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o obj-$(CONFIG_MCOUNT) += tracing/ +obj-$(CONFIG_TRACING) += tracing/ lib-$(CONFIG_GENERIC_BUG) += bug.o
lib/tracing/trace_function.c new file +72 -0
72 lines not on this page · raw message
Steven Rostedt
reviewerPATCH 12/23 Add context switch marker to sched.c
In reply to Steven RostedtAdd marker into context_switch to record the prev and next tasks.
--- kernel/sched.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/sched.c +2 -0
@@ -2198,6 +2198,8 @@ context_switch(struct rq *rq, struct tasstruct mm_struct *mm, *oldmm;prepare_task_switch (rq, prev, next); +trace_mark (kernel_sched_schedule, +"prev %p next %p" , prev, next); mm = next->mm; oldmm = prev->active_mm;/*
Steven Rostedt
reviewerPATCH 13/23 Make the task State char-string visible to all
In reply to Steven RostedtThe tracer wants to be able to convert the state number into a user visible character. This patch pulls that conversion string out the scheduler into the header. This way if it were to ever change, other parts of the kernel will know.
--- include/linux/sched.h | 2 ++ kernel/sched.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/sched.h +2 -0
@@ -2055,6 +2055,8 @@ static inline void migration_init(void) }#endif +#define TASK_STATE_TO_CHAR_STR "RSDTtZX" +#endif /* __KERNEL__ */ #endif
kernel/sched.c +1 -1
@@ -5149,7 +5149,7 @@ out_unlock:return retval; } -static const char stat_nam[] ="RSDTtZX" ; +static const char stat_nam[] =TASK_STATE_TO_CHAR_STR ;void sched_show_task (struct task_struct *p) {
Steven Rostedt
reviewerPATCH 14/23 Add tracing of context switches
In reply to Steven RostedtThis patch adds context switch tracing, of the format of:
_------=> CPU#
/ _-----=> irqs-off
| / _----=> need-resched
|| / _---=> hardirq/softirq
||| / _--=> preempt-depth
|||| /
||||| delay
cmd pid ||||| time | caller
\ / ||||| \ | /
swapper-0 1d..3 137us+: 0:140:R --> 2912:120
sshd-2912 1d..3 216us+: 2912:120:S --> 0:140
swapper-0 1d..3 261us+: 0:140:R --> 2912:120
bash-2920 0d..3 267us+: 2920:120:S --> 0:140
sshd-2912 1d..3 330us!: 2912:120:S --> 0:140
swapper-0 1d..3 2389us+: 0:140:R --> 2847:120
yum-upda-2847 1d..3 2411us!: 2847:120:S --> 0:140
swapper-0 0d..3 11089us+: 0:140:R --> 3139:120
gdm-bina-3139 0d..3 11113us!: 3139:120:S --> 0:140
swapper-0 1d..3 102328us+: 0:140:R --> 2847:120
yum-upda-2847 1d..3 102348us!: 2847:120:S --> 0:140 "sched_switch" is added to /debugfs/tracing/available_tracers
--- lib/tracing/Kconfig | 9 ++ lib/tracing/Makefile | 1 lib/tracing/trace_sched_switch.c | 166 +++++++++++++++++++++++++++++++++++++++ lib/tracing/tracer.c | 43 ++++++++++ lib/tracing/tracer.h | 23 +++++ 5 files changed, 241 insertions(+), 1 deletion(-)
lib/tracing/Kconfig +9 -0
@@ -23,3 +23,12 @@ config FUNCTION_TRACER insert a call to an architecture specific __mcount routine, that the debugging mechanism using this facility will hook by providing a set of inline routines. + +config CONTEXT_SWITCH_TRACER + bool "Trace process context switches" + depends on DEBUG_KERNEL + select TRACING + help + This tracer hooks into the context switch and records + all switching of tasks. +
lib/tracing/Makefile +1 -0
@@ -1,6 +1,7 @@ obj-$(CONFIG_MCOUNT) += libmcount.o obj-$(CONFIG_TRACING) += tracer.o +obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_FUNCTION_TRACER) += trace_function.o libmcount-y := mcount.o
lib/tracing/trace_sched_switch.c new file +166 -0
166 lines not on this page · raw message
lib/tracing/tracer.c +43 -0
85 lines not on this page · raw message
lib/tracing/tracer.h +22 -1
@@ -10,6 +10,14 @@ struct tracing_function {unsigned long parent_ip; }; +struct tracing_sched_switch { +unsigned int prev_pid; +unsigned char prev_prio; +unsigned char prev_state; +unsigned int next_pid; +unsigned char next_prio; +}; +struct tracing_entry {char type;char cpu;/* who will want to trace more than 256 CPUS? */
@@ -18,7 +26,10 @@ struct tracing_entry {int pid;cycle_t t;char comm[TASK_COMM_LEN ]; -struct tracing_function fn; +union { +struct tracing_function fn; +struct tracing_sched_switch ctx; + }; };struct tracing_trace_cpu {
@@ -82,11 +93,21 @@ void tracing_function_trace(struct traciunsigned long ip, unsigned long parent_ip, unsigned long flags); +void tracing_sched_switch_trace (struct tracing_trace *tr, +struct tracing_trace_cpu *data, +struct task_struct *prev, +struct task_struct *next, +unsigned long flags); +void tracing_start_function_trace (void );void tracing_stop_function_trace (void );int register_trace (struct trace_types_struct *type);void unregister_trace (struct trace_types_struct *type); +void tracing_start_sched_switch (void ); +void tracing_stop_sched_switch (void ); + +extern int tracing_sched_switch_enabled; static inline notrace cycle_t now(void) {
Re: [PATCH 14/23 -v8] Add tracing of context switches
Quoted 7 lines · Steven Rostedt
Index: linux-mcount.git/lib/tracing/Kconfig
===================================================================lib/tracing/Kconfig +0
-0@@ -23,3 +23,12 @@ config FUNCTION_TRACER insert a call to an architecture specific __mcount routine, that the debugging mechanism using this facility will hook by
lib/tracing/Kconfig config FUNCTION_TRACER · line 34
Quoted 10 lines
lib/tracing/Kconfig +9
-0providing a set of inline routines. + +config CONTEXT_SWITCH_TRACER + bool "Trace process context switches" + depends on DEBUG_KERNEL + select TRACING + help + This tracer hooks into the context switch and records + all switching of tasks. +
This needs a "select MARKERS" or it doesn't compile.
My config has:
CONFIG_TRACE_IRQFLAGS_SUPPORT=yCONFIG_TRACE_IRQFLAGS=yCONFIG_TRACER_MAX_TRACE=yCONFIG_FUNCTION_TRACER=y
# CONFIG_EVENT_TRACER is not set
# CONFIG_WAKEUP_TRACER is not setCONFIG_CONTEXT_SWITCH_TRACER=y
-- Tim
=============================
Tim Bird
Architecture Group Chair, CE Linux Forum Senior Staff Engineer, Sony Corporation of America
=============================
Steven Rostedt
reviewerPATCH 15/23 Generic command line storage
In reply to Steven RostedtSaving the comm of tasks for each trace is very expensive. This patch includes in the context switch hook, a way to store the last 100 command lines of tasks. This table is examined when a trace is to be printed.
Note: The comm may be destroyed if other traces are performed. Later (TBD) patches may simply store this information in the trace itself.
--- lib/tracing/Kconfig | 1 lib/tracing/trace_function.c | 2 lib/tracing/trace_sched_switch.c | 5 + lib/tracing/tracer.c | 108 ++++++++++++++++++++++++++++++++++++--- lib/tracing/tracer.h | 3 - 5 files changed, 111 insertions(+), 8 deletions(-)
lib/tracing/Kconfig +1 -0
@@ -18,6 +18,7 @@ config FUNCTION_TRACER depends on DEBUG_KERNEL && HAVE_MCOUNT select MCOUNT select TRACING + select CONTEXT_SWITCH_TRACER help Use profiler instrumentation, adding -pg to CFLAGS. This will insert a call to an architecture specific __mcount routine,
lib/tracing/trace_function.c +2 -0
@@ -29,10 +29,12 @@ static notrace void start_function_trace {function_reset (tr);tracing_start_function_trace (); +tracing_start_sched_switch (); } static notrace void stop_function_trace(struct tracing_trace *tr) { +tracing_stop_sched_switch ();tracing_stop_function_trace (); }
lib/tracing/trace_sched_switch.c +5 -0
@@ -33,6 +33,11 @@ static notrace void sched_switch_callbacva_list ap;int cpu; +if (!atomic_read (&sched_ref)) +return ; + +tracing_record_cmdline (current); +if (!trace_enabled)return ;
lib/tracing/tracer.c +102 -6
183 lines not on this page · raw message
lib/tracing/tracer.h +1 -2
@@ -25,7 +25,6 @@ struct tracing_entry {char preempt_count;/* assumes PREEMPT_MASK is 8 bits or less */ int pid;cycle_t t; -char comm[TASK_COMM_LEN ];union {struct tracing_function fn;struct tracing_sched_switch ctx;
@@ -98,7 +97,7 @@ void tracing_sched_switch_trace(struct tstruct task_struct *prev, struct task_struct *next, unsigned long flags); - +void tracing_record_cmdline (struct task_struct *tsk);void tracing_start_function_trace (void );void tracing_stop_function_trace (void );
Steven Rostedt
reviewerPATCH 16/23 trace generic call to schedule switch
In reply to Steven RostedtThis patch adds hooks into the schedule switch tracing to allow other latency traces to hook into the schedule switches.
--- lib/tracing/trace_sched_switch.c | 135 +++++++++++++++++++++++++++++++++------ lib/tracing/tracer.h | 14 ++++ 2 files changed, 131 insertions(+), 18 deletions(-)
lib/tracing/tracer.h +14 -0
@@ -113,4 +113,18 @@ static inline notrace cycle_t now(void) return get_monotonic_cycles(); } +#ifdef CONFIG_CONTEXT_SWITCH_TRACER +typedef void (*tracer_switch_func_t )(void *private, +struct task_struct *prev, +struct task_struct *next); +struct tracer_switch_ops { +tracer_switch_func_t func; +void *private; +struct tracer_switch_ops *next; +}; + +extern int register_tracer_switch (struct tracer_switch_ops *ops); +extern int unregister_tracer_switch (struct tracer_switch_ops *ops); +#endif /* CONFIG_CONTEXT_SWITCH_TRACER */ +#endif /* _LINUX_MCOUNT_TRACER_H */
lib/tracing/trace_sched_switch.c +117 -18
157 lines not on this page · raw message
Steven Rostedt
reviewerPATCH 17/23 Add marker in try_to_wake_up
In reply to Steven RostedtAdd markers into the wakeup code, to allow the tracer to record wakeup timings.
--- kernel/sched.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/sched.c +8 -0
@@ -1885,6 +1885,10 @@ static int try_to_wake_up(struct task_st out_activate:#endif /* CONFIG_SMP */ +trace_mark (kernel_sched_wakeup, +"p %p rq->curr %p" , + p, rq->curr); +schedstat_inc (p, se.nr_wakeups);if (sync)schedstat_inc (p, se.nr_wakeups_sync);
@@ -2026,6 +2030,10 @@ void fastcall wake_up_new_task(struct ta p->sched_class->task_new (rq, p);inc_nr_running (p, rq); } +trace_mark (kernel_sched_wakeup_new, +"p %p rq->curr %p" , + p, rq->curr); +check_preempt_curr (rq, p);#ifdef CONFIG_SMP if (p->sched_class->task_wake_up)
Steven Rostedt
reviewerPATCH 18/23 mcount tracer for wakeup latency timings.
In reply to Steven RostedtThis patch adds hooks to trace the wake up latency of the highest priority waking task.
"wakeup" is added to /debugfs/tracing/available_tracers
Also added to /debugfs/tracing
tracing_max_latency
holds the current max latency for the wakeup
wakeup_thresh
if set to other than zero, a log will be recorded for every wakeup that takes longer than the number entered in here (usecs for all counters) (deletes previous trace)
Examples:
(with mcount_enabled = 0)
============
preemption latency trace v1.1.5 on 2.6.24-rc8
--------------------------------------------------------------------
latency: 26 us, #2/2, CPU#1 | (M:rt VP:0, KP:0, SP:0 HP:0 #P:2)
-----------------
| task: migration/0-3 (uid:0 nice:-5 policy:1 rt_prio:99)
-----------------
_------=> CPU#
/ _-----=> irqs-off
| / _----=> need-resched
|| / _---=> hardirq/softirq
||| / _--=> preempt-depth
|||| /
||||| delay
cmd pid ||||| time | caller
\ / ||||| \ | /
quilt-8551 0d..3 0us+: wake_up_process+0x15/0x17 <ffffffff80233e80> (sched_exec+0xc9/0x100 <ffffffff80235343>)
quilt-8551 0d..4 26us : sched_switch_callback+0x73/0x81 <ffffffff80338d2f> (schedule+0x483/0x6d5 <ffffffff8048b3ee>)vim:ft=help
============
(with mcount_enabled = 1)
============
preemption latency trace v1.1.5 on 2.6.24-rc8
--------------------------------------------------------------------
latency: 36 us, #45/45, CPU#0 | (M:rt VP:0, KP:0, SP:0 HP:0 #P:2)
-----------------
| task: migration/1-5 (uid:0 nice:-5 policy:1 rt_prio:99)
-----------------
_------=> CPU#
/ _-----=> irqs-off
| / _----=> need-resched
|| / _---=> hardirq/softirq
||| / _--=> preempt-depth
|||| /
||||| delay
cmd pid ||||| time | caller
\ / ||||| \ | /
bash-10653 1d..3 0us : wake_up_process+0x15/0x17 <ffffffff80233e80> (sched_exec+0xc9/0x100 <ffffffff80235343>)
bash-10653 1d..3 1us : try_to_wake_up+0x271/0x2e7 <ffffffff80233dcf> (sub_preempt_count+0xc/0x7a <ffffffff8023309e>)
bash-10653 1d..2 2us : try_to_wake_up+0x296/0x2e7 <ffffffff80233df4> (update_rq_clock+0x9/0x20 <ffffffff802303f3>)
bash-10653 1d..2 2us : update_rq_clock+0x1e/0x20 <ffffffff80230408> (__update_rq_clock+0xc/0x90 <ffffffff80230366>)
bash-10653 1d..2 3us : __update_rq_clock+0x1b/0x90 <ffffffff80230375> (sched_clock+0x9/0x29 <ffffffff80214529>)
bash-10653 1d..2 4us : try_to_wake_up+0x2a6/0x2e7 <ffffffff80233e04> (activate_task+0xc/0x3f <ffffffff8022ffca>)
bash-10653 1d..2 4us : activate_task+0x2d/0x3f <ffffffff8022ffeb> (enqueue_task+0xe/0x66 <ffffffff8022ff66>)
bash-10653 1d..2 5us : enqueue_task+0x5b/0x66 <ffffffff8022ffb3> (enqueue_task_rt+0x9/0x3c <ffffffff80233351>)
bash-10653 1d..2 6us : try_to_wake_up+0x2ba/0x2e7 <ffffffff80233e18> (check_preempt_wakeup+0x12/0x99 <ffffffff80234f84>)
[...]
bash-10653 1d..5 33us : tracing_record_cmdline+0xcf/0xd4 <ffffffff80338aad> (_spin_unlock+0x9/0x33 <ffffffff8048d3ec>)
bash-10653 1d..5 34us : _spin_unlock+0x19/0x33 <ffffffff8048d3fc> (sub_preempt_count+0xc/0x7a <ffffffff8023309e>)
bash-10653 1d..4 35us : wakeup_sched_switch+0x65/0x2ff <ffffffff80339f66> (_spin_lock_irqsave+0xc/0xa9 <ffffffff8048d08b>)
bash-10653 1d..4 35us : _spin_lock_irqsave+0x19/0xa9 <ffffffff8048d098> (add_preempt_count+0xe/0x77 <ffffffff8023311a>)
bash-10653 1d..4 36us : sched_switch_callback+0x73/0x81 <ffffffff80338d2f> (schedule+0x483/0x6d5 <ffffffff8048b3ee>)vim:ft=help
============
The [...] was added here to not waste your email box space.
--- lib/tracing/Kconfig | 14 + lib/tracing/Makefile | 1 lib/tracing/trace_wakeup.c | 377 +++++++++++++++++++++++++++++++++++++++++++++ lib/tracing/tracer.c | 127 +++++++++++++++ lib/tracing/tracer.h | 5 5 files changed, 523 insertions(+), 1 deletion(-)
lib/tracing/Kconfig +14 -0
@@ -9,6 +9,9 @@ config MCOUNT bool select FRAME_POINTER +config TRACER_MAX_TRACE + bool + config TRACING bool select DEBUG_FS
@@ -25,6 +28,17 @@ config FUNCTION_TRACER that the debugging mechanism using this facility will hook by providing a set of inline routines. +config WAKEUP_TRACER + bool "Trace wakeup latencies" + depends on DEBUG_KERNEL + select TRACING + select CONTEXT_SWITCH_TRACER + select TRACER_MAX_TRACE + help + This tracer adds hooks into scheduling to time the latency + of the highest priority task tasks to be scheduled in + after it has worken up. + config CONTEXT_SWITCH_TRACER bool "Trace process context switches" depends on DEBUG_KERNEL
lib/tracing/Makefile +1 -0
@@ -3,5 +3,6 @@ obj-$(CONFIG_MCOUNT) += libmcount.o obj-$(CONFIG_TRACING) += tracer.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_FUNCTION_TRACER) += trace_function.o +obj-$(CONFIG_WAKEUP_TRACER) += trace_wakeup.o libmcount-y := mcount.o
lib/tracing/trace_wakeup.c new file +377 -0
377 lines not on this page · raw message
lib/tracing/tracer.c +126 -1
183 lines not on this page · raw message
lib/tracing/tracer.h +5 -0
@@ -69,6 +69,7 @@ struct trace_types_struct {void (*stop)(struct tracing_iterator *iter);void (*ctrl_update)(struct tracing_trace *tr);struct trace_types_struct *next; +int print_max; };struct tracing_iterator {
@@ -107,6 +108,10 @@ void tracing_start_sched_switch(void);void tracing_stop_sched_switch (void );extern int tracing_sched_switch_enabled; +extern unsigned long tracing_max_latency; +extern unsigned long tracing_thresh; + +void update_max_tr (struct tracing_trace *tr,struct task_struct *tsk,int cpu); static inline notrace cycle_t now(void) {
Steven Rostedt
reviewerPATCH 19/23 Trace irq disabled critical timings
In reply to Steven RostedtThis patch adds latency tracing for critical timings (how long interrupts are disabled for).
"irqsoff" is added to /debugfs/tracing/available_tracers
Note:
tracing_max_latency
also holds the max latency for irqsoff (in usecs).
(default to large number so one must start latency tracing)
tracing_thresh
threshold (in usecs) to always print out if irqs off is detected to be longer than stated here. If irq_thresh is non-zero, then max_irq_latency is ignored.
Here's an example of a trace with mcount_enabled = 0
=======
preemption latency trace v1.1.5 on 2.6.24-rc7
--------------------------------------------------------------------
latency: 100 us, #3/3, CPU#1 | (M:rt VP:0, KP:0, SP:0 HP:0 #P:2)
-----------------
| task: swapper-0 (uid:0 nice:0 policy:0 rt_prio:0)
----------------- => started at: _spin_lock_irqsave+0x2a/0xb7
=> ended at: _spin_unlock_irqrestore+0x32/0x5f
_------=> CPU#
/ _-----=> irqs-off
| / _----=> need-resched
|| / _---=> hardirq/softirq
||| / _--=> preempt-depth
|||| /
||||| delay
cmd pid ||||| time | caller
\ / ||||| \ | /
swapper-0 1d.s3 0us+: _spin_lock_irqsave+0x2a/0xb7 (e1000_update_stats+0x47/0x64c [e1000])
swapper-0 1d.s3 100us : _spin_unlock_irqrestore+0x32/0x5f (e1000_update_stats+0x641/0x64c [e1000])
swapper-0 1d.s3 100us : trace_hardirqs_on_caller+0x75/0x89 (_spin_unlock_irqrestore+0x32/0x5f)vim:ft=help
=======
And this is a trace with mcount_enabled == 1
=======
preemption latency trace v1.1.5 on 2.6.24-rc7
--------------------------------------------------------------------
latency: 102 us, #12/12, CPU#1 | (M:rt VP:0, KP:0, SP:0 HP:0 #P:2)
-----------------
| task: swapper-0 (uid:0 nice:0 policy:0 rt_prio:0)
----------------- => started at: _spin_lock_irqsave+0x2a/0xb7
=> ended at: _spin_unlock_irqrestore+0x32/0x5f
_------=> CPU#
/ _-----=> irqs-off
| / _----=> need-resched
|| / _---=> hardirq/softirq
||| / _--=> preempt-depth
|||| /
||||| delay
cmd pid ||||| time | caller
\ / ||||| \ | /
swapper-0 1dNs3 0us+: _spin_lock_irqsave+0x2a/0xb7 (e1000_update_stats+0x47/0x64c [e1000])
swapper-0 1dNs3 46us : e1000_read_phy_reg+0x16/0x225 [e1000] (e1000_update_stats+0x5e2/0x64c [e1000])
swapper-0 1dNs3 46us : e1000_swfw_sync_acquire+0x10/0x99 [e1000] (e1000_read_phy_reg+0x49/0x225 [e1000])
swapper-0 1dNs3 46us : e1000_get_hw_eeprom_semaphore+0x12/0xa6 [e1000] (e1000_swfw_sync_acquire+0x36/0x99 [e1000])
swapper-0 1dNs3 47us : __const_udelay+0x9/0x47 (e1000_read_phy_reg+0x116/0x225 [e1000])
swapper-0 1dNs3 47us+: __delay+0x9/0x50 (__const_udelay+0x45/0x47)
swapper-0 1dNs3 97us : preempt_schedule+0xc/0x84 (__delay+0x4e/0x50)
swapper-0 1dNs3 98us : e1000_swfw_sync_release+0xc/0x55 [e1000] (e1000_read_phy_reg+0x211/0x225 [e1000])
swapper-0 1dNs3 99us+: e1000_put_hw_eeprom_semaphore+0x9/0x35 [e1000] (e1000_swfw_sync_release+0x50/0x55 [e1000])
swapper-0 1dNs3 101us : _spin_unlock_irqrestore+0xe/0x5f (e1000_update_stats+0x641/0x64c [e1000])
swapper-0 1dNs3 102us : _spin_unlock_irqrestore+0x32/0x5f (e1000_update_stats+0x641/0x64c [e1000])
swapper-0 1dNs3 102us : trace_hardirqs_on_caller+0x75/0x89 (_spin_unlock_irqrestore+0x32/0x5f)vim:ft=help
=======
--- arch/x86/kernel/process_64.c | 3 arch/x86/lib/thunk_64.S | 18 + include/asm-x86/irqflags_32.h | 4 include/asm-x86/irqflags_64.h | 4 include/linux/irqflags.h | 37 ++- include/linux/mcount.h | 31 ++- kernel/fork.c | 2 kernel/lockdep.c | 16 + lib/tracing/Kconfig | 18 + lib/tracing/Makefile | 1 lib/tracing/trace_irqsoff.c | 416 ++++++++++++++++++++++++++++++++++++++++++ lib/tracing/tracer.c | 59 ++++- lib/tracing/tracer.h | 2 13 files changed, 576 insertions(+), 35 deletions(-)
arch/x86/kernel/process_64.c +3 -0
@@ -233,7 +233,10 @@ void cpu_idle (void)*/ local_irq_disable ();enter_idle (); +/* Don't trace irqs off for idle */ +stop_critical_timings ();idle (); +start_critical_timings ();/* In many cases the interrupt that ended idle has already called exit_idle. But some idle loops can be woken up without interrupt. */
arch/x86/lib/thunk_64.S +16 -2
@@ -47,8 +47,22 @@ thunk __up_wakeup,__up #ifdef CONFIG_TRACE_IRQFLAGS - thunk trace_hardirqs_on_thunk,trace_hardirqs_on - thunk trace_hardirqs_off_thunk,trace_hardirqs_off + /* put return address in rdi (arg1) */ + .macro thunk_ra name,func + .globl \name +\name: + CFI_STARTPROC + SAVE_ARGS + /* SAVE_ARGS pushs 9 elements */ + /* the next element would be the rip */ + movq 9*8(%rsp), %rdi + call \func + jmp restore + CFI_ENDPROC + .endm + + thunk_ra trace_hardirqs_on_thunk,trace_hardirqs_on_caller + thunk_ra trace_hardirqs_off_thunk,trace_hardirqs_off_caller #endif #ifdef CONFIG_DEBUG_LOCK_ALLOC
include/asm-x86/irqflags_32.h +2 -2
@@ -139,9 +139,9 @@ static inline int raw_irqs_disabled(voidstatic inline void trace_hardirqs_fixup_flags (unsigned long flags) {if (raw_irqs_disabled_flags (flags)) -trace_hardirqs_off (); +__trace_hardirqs_off ();else -trace_hardirqs_on (); +__trace_hardirqs_on (); }static inline void trace_hardirqs_fixup (void )
include/asm-x86/irqflags_64.h +2 -2
@@ -120,9 +120,9 @@ static inline int raw_irqs_disabled(voidstatic inline void trace_hardirqs_fixup_flags (unsigned long flags) {if (raw_irqs_disabled_flags (flags)) -trace_hardirqs_off (); +__trace_hardirqs_off ();else -trace_hardirqs_on (); +__trace_hardirqs_on (); }static inline void trace_hardirqs_fixup (void )
include/linux/irqflags.h +29 -8
@@ -12,10 +12,21 @@#define _LINUX_TRACE_IRQFLAGS_H#ifdef CONFIG_TRACE_IRQFLAGS -extern void trace_hardirqs_on (void ); -extern void trace_hardirqs_off (void ); +# include <linux/mcount.h> +extern void trace_hardirqs_on_caller (unsigned long ip); +extern void trace_hardirqs_off_caller (unsigned long ip);extern void trace_softirqs_on (unsigned long ip);extern void trace_softirqs_off (unsigned long ip); +extern void trace_hardirqs_on (void ); +extern void trace_hardirqs_off (void ); + static inline void notrace __trace_hardirqs_on(void) + { +trace_hardirqs_on_caller (CALLER_ADDR0 ); + } + static inline void notrace __trace_hardirqs_off(void) + { +trace_hardirqs_off_caller (CALLER_ADDR0 ); + }# define trace_hardirq_context (p) ((p)->hardirq_context)# define trace_softirq_context (p) ((p)->softirq_context)# define trace_hardirqs_enabled (p) ((p)->hardirqs_enabled)
@@ -28,6 +39,8 @@#else # define trace_hardirqs_on () do { } while (0)# define trace_hardirqs_off () do { } while (0) +# define __trace_hardirqs_on () do { } while (0) +# define __trace_hardirqs_off () do { } while (0)# define trace_softirqs_on (ip) do { } while (0)# define trace_softirqs_off (ip) do { } while (0)# define trace_hardirq_context (p) 0
@@ -41,24 +54,32 @@# define INIT_TRACE_IRQFLAGS #endif +#ifdef CONFIG_CRITICAL_IRQSOFF_TIMING +extern void stop_critical_timings (void ); +extern void start_critical_timings (void ); +#else +# define stop_critical_timings () do { } while (0) +# define start_critical_timings () do { } while (0) +#endif +#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT #include <asm/irqflags.h> #define local_irq_enable () \ - do { trace_hardirqs_on(); raw_local_irq_enable(); } while (0) + do { __trace_hardirqs_on(); raw_local_irq_enable(); } while (0)#define local_irq_disable () \ - do { raw_local_irq_disable(); trace_hardirqs_off(); } while (0) + do { raw_local_irq_disable(); __trace_hardirqs_off(); } while (0)#define local_irq_save (flags) \ - do { raw_local_irq_save(flags); trace_hardirqs_off(); } while (0) + do { raw_local_irq_save(flags); __trace_hardirqs_off(); } while (0)#define local_irq_restore (flags) \ do { \ if (raw_irqs_disabled_flags(flags)) { \ raw_local_irq_restore(flags); \ - trace_hardirqs_off(); \ + __trace_hardirqs_off(); \ } else { \ - trace_hardirqs_on(); \ + __trace_hardirqs_on(); \ raw_local_irq_restore(flags); \ } \ } while (0)
@@ -76,7 +97,7 @@#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT #define safe_halt () \ do { \ - trace_hardirqs_on(); \ + __trace_hardirqs_on(); \ raw_safe_halt(); \ } while (0)
include/linux/mcount.h +27 -4
@@ -6,10 +6,6 @@ extern int mcount_enabled;#include <linux/linkage.h> -#define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) -#define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1)) -#define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2)) -typedef void (*mcount_func_t )(unsigned long ip,unsigned long parent_ip);struct mcount_ops {
@@ -35,4 +31,31 @@ extern void mcount(void);# define unregister_mcount_function (ops) do { } while (0)# define clear_mcount_function (ops) do { } while (0)#endif /* CONFIG_MCOUNT */ + + +#ifdef CONFIG_FRAME_POINTER +/* TODO: need to fix this for ARM */ +# define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) +# define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1)) +# define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2)) +# define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3)) +# define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4)) +# define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5)) +#else +# define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) +# define CALLER_ADDR1 0UL +# define CALLER_ADDR2 0UL +# define CALLER_ADDR3 0UL +# define CALLER_ADDR4 0UL +# define CALLER_ADDR5 0UL +#endif + +#ifdef CONFIG_CRITICAL_IRQSOFF_TIMING + extern void notrace time_hardirqs_on(unsigned long a0, unsigned long a1); + extern void notrace time_hardirqs_off(unsigned long a0, unsigned long a1); +#else +# define time_hardirqs_on (a0, a1) do { } while (0) +# define time_hardirqs_off (a0, a1) do { } while (0) +#endif +#endif /* _LINUX_MCOUNT_H */
kernel/fork.c +1 -1
@@ -1036,7 +1036,7 @@ static struct task_struct *copy_process(rt_mutex_init_task (p); -#ifdef CONFIG_TRACE_IRQFLAGS +#if defined(CONFIG_TRACE_IRQFLAGS ) && defined(CONFIG_LOCKDEP )DEBUG_LOCKS_WARN_ON (!p->hardirqs_enabled);DEBUG_LOCKS_WARN_ON (!p->softirqs_enabled);#endif
kernel/lockdep.c +14 -2
@@ -39,6 +39,7 @@#include <linux/irqflags.h> #include <linux/utsname.h> #include <linux/hash.h> +#include <linux/mcount.h> #include <asm/sections.h>
@@ -2009,7 +2010,7 @@ void early_boot_irqs_on(void)/* * Hardirqs will be enabled: */ -void trace_hardirqs_on (void ) +void notrace trace_hardirqs_on_caller(unsigned long a0) {struct task_struct *curr = current;unsigned long ip;
@@ -2050,14 +2051,19 @@ void trace_hardirqs_on(void) curr->hardirq_enable_ip = ip; curr->hardirq_enable_event = ++curr->irq_events;debug_atomic_inc (&hardirqs_on_events); +time_hardirqs_on (CALLER_ADDR0 , a0); } +EXPORT_SYMBOL (trace_hardirqs_on_caller); +void notrace trace_hardirqs_on(void) { +trace_hardirqs_on_caller (CALLER_ADDR0 ); +}EXPORT_SYMBOL (trace_hardirqs_on);/* * Hardirqs were disabled: */ -void trace_hardirqs_off (void ) +void notrace trace_hardirqs_off_caller(unsigned long a0) {struct task_struct *curr = current;
@@ -2075,9 +2081,15 @@ void trace_hardirqs_off(void) curr->hardirq_disable_ip = _RET_IP_; curr->hardirq_disable_event = ++curr->irq_events;debug_atomic_inc (&hardirqs_off_events); +time_hardirqs_off (CALLER_ADDR0 , a0); }else debug_atomic_inc (&redundant_hardirqs_off); } +EXPORT_SYMBOL (trace_hardirqs_off_caller); + +void notrace trace_hardirqs_off(void) { +trace_hardirqs_off_caller (CALLER_ADDR0 ); +}EXPORT_SYMBOL (trace_hardirqs_off);
lib/tracing/Kconfig +18 -0
@@ -28,6 +28,24 @@ config FUNCTION_TRACER that the debugging mechanism using this facility will hook by providing a set of inline routines. +config CRITICAL_IRQSOFF_TIMING + bool "Interrupts-off critical section latency timing" + default n + depends on TRACE_IRQFLAGS_SUPPORT + depends on GENERIC_TIME + select TRACE_IRQFLAGS + select TRACING + select TRACER_MAX_TRACE + help + This option measures the time spent in irqs-off critical + sections, with microsecond accuracy. + + The default measurement method is a maximum search, which is + disabled by default and can be runtime (re-)started + via: + + echo 0 > /debugfs/tracing/tracing_max_latency + config WAKEUP_TRACER bool "Trace wakeup latencies" depends on DEBUG_KERNEL
lib/tracing/Makefile +1 -0
@@ -3,6 +3,7 @@ obj-$(CONFIG_MCOUNT) += libmcount.o obj-$(CONFIG_TRACING) += tracer.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_FUNCTION_TRACER) += trace_function.o +obj-$(CONFIG_CRITICAL_IRQSOFF_TIMING) += trace_irqsoff.o obj-$(CONFIG_WAKEUP_TRACER) += trace_wakeup.o libmcount-y := mcount.o
lib/tracing/trace_irqsoff.c new file +416 -0
416 lines not on this page · raw message
lib/tracing/tracer.c +45 -14
87 lines not on this page · raw message
lib/tracing/tracer.h +2 -0
@@ -112,6 +112,8 @@ extern unsigned long tracing_max_latencyextern unsigned long tracing_thresh;void update_max_tr (struct tracing_trace *tr,struct task_struct *tsk,int cpu); +void update_max_tr_single (struct tracing_trace *tr, +struct task_struct *tsk,int cpu); static inline notrace cycle_t now(void) {
Steven Rostedt
reviewerPATCH 20/23 trace preempt off critical timings
In reply to Steven RostedtAdd preempt off timings. A lot of kernel core code is taken from the RT patch latency trace that was written by Ingo Molnar.
This adds "preemptoff" and "preemptirqsoff" to /debugfs/tracing/available_tracers
Now instead of just tracing irqs off, preemption off can be selected to be recorded.
When this is selected, it shares the same files as irqs off timings. One can either trace preemption off, irqs off, or one or the other off.
By echoing "preemptoff" into /debugfs/tracing/current_tracer, recording of preempt off only is performed. "irqsoff" will only record the time irqs are disabled, but "preemptirqsoff" will take the total time irqs or preemption are disabled. Runtime switching of these options is now supported by simpling echoing in the appropriate trace name into /debugfs/tracing/current_tracer.
--- arch/x86/kernel/process_32.c | 3 include/linux/irqflags.h | 3 include/linux/mcount.h | 8 + include/linux/preempt.h | 2 kernel/sched.c | 24 +++++ lib/tracing/Kconfig | 25 +++++ lib/tracing/Makefile | 1 lib/tracing/trace_irqsoff.c | 183 +++++++++++++++++++++++++++++++------------ 8 files changed, 196 insertions(+), 53 deletions(-)
lib/tracing/Kconfig +25 -0
@@ -46,6 +46,31 @@ config CRITICAL_IRQSOFF_TIMING echo 0 > /debugfs/tracing/tracing_max_latency + (Note that kernel size and overhead increases with this option + enabled. This option and the preempt-off timing option can be + used together or separately.) + +config CRITICAL_PREEMPT_TIMING + bool "Preemption-off critical section latency timing" + default n + depends on GENERIC_TIME + depends on PREEMPT + select TRACING + select TRACER_MAX_TRACE + help + This option measures the time spent in preemption off critical + sections, with microsecond accuracy. + + The default measurement method is a maximum search, which is + disabled by default and can be runtime (re-)started + via: + + echo 0 > /debugfs/tracing/tracing_max_latency + + (Note that kernel size and overhead increases with this option + enabled. This option and the irqs-off timing option can be + used together or separately.) + config WAKEUP_TRACER bool "Trace wakeup latencies" depends on DEBUG_KERNEL
lib/tracing/Makefile +1 -0
@@ -4,6 +4,7 @@ obj-$(CONFIG_TRACING) += tracer.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_FUNCTION_TRACER) += trace_function.o obj-$(CONFIG_CRITICAL_IRQSOFF_TIMING) += trace_irqsoff.o +obj-$(CONFIG_CRITICAL_PREEMPT_TIMING) += trace_irqsoff.o obj-$(CONFIG_WAKEUP_TRACER) += trace_wakeup.o libmcount-y := mcount.o
lib/tracing/trace_irqsoff.c +133 -50
304 lines not on this page · raw message
include/linux/preempt.h +1 -1
@@ -10,7 +10,7 @@#include <linux/linkage.h> #include <linux/list.h> -#ifdef CONFIG_DEBUG_PREEMPT +#if defined(CONFIG_DEBUG_PREEMPT ) || defined(CONFIG_CRITICAL_PREEMPT_TIMING ) extern void fastcall add_preempt_count(int val); extern void fastcall sub_preempt_count(int val);#else
kernel/sched.c +23 -1
@@ -66,6 +66,7 @@#include <linux/unistd.h> #include <linux/pagemap.h> #include <linux/hrtimer.h> +#include <linux/mcount.h> #include <asm/tlb.h> #include <asm/irq_regs.h>
@@ -3780,26 +3781,44 @@ void scheduler_tick(void)#endif } -#if defined(CONFIG_PREEMPT ) && defined(CONFIG_DEBUG_PREEMPT ) +#if defined(CONFIG_PREEMPT ) && (defined(CONFIG_DEBUG_PREEMPT ) || \ + defined(CONFIG_CRITICAL_PREEMPT_TIMING )) + +static inline unsigned long get_parent_ip (unsigned long addr) +{ +if (in_lock_functions (addr)) { + addr =CALLER_ADDR2 ; +if (in_lock_functions (addr)) + addr =CALLER_ADDR3 ; + } +return addr; +} void fastcall add_preempt_count(int val) { +#ifdef CONFIG_DEBUG_PREEMPT /* * Underflow? */ if (DEBUG_LOCKS_WARN_ON ((preempt_count () <0 )))return ; +#endif preempt_count () += val; +#ifdef CONFIG_DEBUG_PREEMPT /* * Spinlock count overflowing soon? */ DEBUG_LOCKS_WARN_ON ((preempt_count () &PREEMPT_MASK ) >=PREEMPT_MASK -10 ); +#endif +if (preempt_count () == val) +trace_preempt_off (CALLER_ADDR0 ,get_parent_ip (CALLER_ADDR1 )); }EXPORT_SYMBOL (add_preempt_count); void fastcall sub_preempt_count(int val) { +#ifdef CONFIG_DEBUG_PREEMPT /* * Underflow? */
@@ -3811,7 +3830,10 @@ void fastcall sub_preempt_count(int val)if (DEBUG_LOCKS_WARN_ON ((val <PREEMPT_MASK ) && !(preempt_count () &PREEMPT_MASK )))return ; +#endif +if (preempt_count () == val) +trace_preempt_on (CALLER_ADDR0 ,get_parent_ip (CALLER_ADDR1 ));preempt_count () -= val; }EXPORT_SYMBOL (sub_preempt_count);
arch/x86/kernel/process_32.c +3 -0
@@ -195,7 +195,10 @@ void cpu_idle(void)play_dead ();__get_cpu_var (irq_stat).idle_timestamp = jiffies; +/* Don't trace irqs off for idle */ +stop_critical_timings ();idle (); +start_critical_timings (); }tick_nohz_restart_sched_tick ();preempt_enable_no_resched ();
include/linux/mcount.h +8 -0
@@ -58,4 +58,12 @@ extern void mcount(void);# define time_hardirqs_off (a0, a1) do { } while (0)#endif +#ifdef CONFIG_CRITICAL_PREEMPT_TIMING + extern void notrace trace_preempt_on(unsigned long a0, unsigned long a1); + extern void notrace trace_preempt_off(unsigned long a0, unsigned long a1); +#else +# define trace_preempt_on (a0, a1) do { } while (0) +# define trace_preempt_off (a0, a1) do { } while (0) +#endif +#endif /* _LINUX_MCOUNT_H */
include/linux/irqflags.h +2 -1
@@ -54,7 +54,8 @@# define INIT_TRACE_IRQFLAGS #endif -#ifdef CONFIG_CRITICAL_IRQSOFF_TIMING +#if defined(CONFIG_CRITICAL_IRQSOFF_TIMING ) || \ + defined(CONFIG_CRITICAL_PREEMPT_TIMING )extern void stop_critical_timings (void );extern void start_critical_timings (void );#else
Steven Rostedt
reviewerPATCH 21/23 Add markers to various events
In reply to Steven RostedtThis patch adds markers to various events in the kernel. (interrupts, task activation and hrtimers)
--- arch/x86/kernel/apic_32.c | 2 ++ arch/x86/kernel/irq_32.c | 1 + arch/x86/kernel/irq_64.c | 2 ++ arch/x86/kernel/traps_32.c | 2 ++ arch/x86/kernel/traps_64.c | 2 ++ arch/x86/mm/fault_32.c | 3 +++ arch/x86/mm/fault_64.c | 3 +++ kernel/hrtimer.c | 7 +++++++ kernel/sched.c | 11 +++++++++++ 9 files changed, 33 insertions(+)
arch/x86/kernel/apic_32.c +2 -0
@@ -581,6 +581,8 @@ notrace fastcall void smp_apic_timer_int {struct pt_regs *old_regs =set_irq_regs (regs); +trace_mark (arch_apic_timer,"ip %lx" , regs->eip); +/* * NOTE! We'd better ACK the irq immediately, * because timer handling can be slow.
arch/x86/kernel/irq_32.c +1 -0
@@ -85,6 +85,7 @@ fastcall unsigned int do_IRQ(struct pt_r old_regs =set_irq_regs (regs);irq_enter (); +trace_mark (arch_do_irq,"ip %lx irq %d" , regs->eip, irq);#ifdef CONFIG_DEBUG_STACKOVERFLOW /* Debugging check for stack overflow: is there less than 1KB free? */ {
arch/x86/kernel/irq_64.c +2 -0
@@ -149,6 +149,8 @@ asmlinkage unsigned int do_IRQ(struct ptirq_enter (); irq =__get_cpu_var (vector_irq)[vector]; +trace_mark (arch_do_irq,"ip %lx irq %d" , regs->rip, irq); +#ifdef CONFIG_DEBUG_STACKOVERFLOW stack_overflow_check (regs);#endif
arch/x86/kernel/traps_32.c +2 -0
@@ -769,6 +769,8 @@ fastcall __kprobes void do_nmi(struct ptnmi_enter (); +trace_mark (arch_do_nmi,"ip %lx flags %lx" , regs->eip, regs->eflags); + cpu =smp_processor_id (); ++nmi_count (cpu);
arch/x86/kernel/traps_64.c +2 -0
@@ -782,6 +782,8 @@ asmlinkage __kprobes void default_do_nmi cpu =smp_processor_id (); +trace_mark (arch_do_nmi,"ip %lx flags %lx" , regs->rip, regs->eflags); +/* Only the BSP gets external NMIs from the system. */ if (!cpu) reason =get_nmi_reason ();
arch/x86/mm/fault_32.c +3 -0
@@ -311,6 +311,9 @@ fastcall void __kprobes do_page_fault(st/* get the address */ address =read_cr2 (); +trace_mark (arch_do_page_fault,"ip %lx err %lx addr %lx" , + regs->eip, error_code, address); + tsk = current; si_code =SEGV_MAPERR ;
arch/x86/mm/fault_64.c +3 -0
@@ -316,6 +316,9 @@ asmlinkage void __kprobes do_page_fault(/* get the address */ address =read_cr2 (); +trace_mark (arch_do_page_fault,"ip %lx err %lx addr %lx" , + regs->rip, error_code, address); + info.si_code =SEGV_MAPERR ;
kernel/hrtimer.c +7 -0
@@ -709,6 +709,8 @@ static void enqueue_hrtimer(struct hrtimstruct hrtimer *entry;int leftmost =1 ; +trace_mark (kernel_hrtimer_enqueue, +"expires %p timer %p" , &timer->expires, timer);/* * Find the right place in the rbtree: */
@@ -1130,6 +1132,7 @@ void hrtimer_interrupt(struct clock_even retry: now =ktime_get (); +trace_mark (kernel_hrtimer_interrupt,"now %p" , &now); expires_next.tv64 =KTIME_MAX ;
@@ -1168,6 +1171,10 @@ void hrtimer_interrupt(struct clock_evencontinue ; } +trace_mark (kernel_hrtimer_interrupt_expire, +"expires %p timer %p" , + &timer->expires, timer); +__run_hrtimer (timer); }spin_unlock (&cpu_base->lock);
kernel/sched.c +11 -0
@@ -90,6 +90,11 @@ unsigned long long __attribute__((weak))#define PRIO_TO_NICE (prio) ((prio) - MAX_RT_PRIO - 20)#define TASK_NICE (p) PRIO_TO_NICE((p)->static_prio) +#define __PRIO (prio) \ + ((prio) <= 99 ? 199 - (prio) : (prio) - 120) + +#define PRIO (p) __PRIO((p)->prio) +/* * 'User priority' is the nice value converted to something we * can work with better when scaling various scheduler parameters,
@@ -1372,6 +1377,9 @@ static void activate_task(struct rq *rq,if (p->state ==TASK_UNINTERRUPTIBLE ) rq->nr_uninterruptible--; +trace_mark (kernel_sched_activate_task, +"pid %d prio %d nr_running %ld" , + p->pid,PRIO (p), rq->nr_running);enqueue_task (rq, p, wakeup);inc_nr_running (p, rq); }
@@ -1385,6 +1393,9 @@ static void deactivate_task(struct rq *r rq->nr_uninterruptible++;dequeue_task (rq, p, sleep); +trace_mark (kernel_sched_deactivate_task, +"pid %d prio %d nr_running %ld" , + p->pid,PRIO (p), rq->nr_running);dec_nr_running (p, rq); }
Re: [PATCH 21/23 -v8] Add markers to various events
Quoted 3 lines · * Steven Rostedt ([email protected]) wrote:
This patch adds markers to various events in the kernel. (interrupts, task activation and hrtimers)
Hi Steven,
I would propose the following standard for IRQ handler markers:
trace_mark(kernel_irq_entry, "irq_id %u kernel_mode %u", irq,
(regs)?(!user_mode(regs)):(1));
...trace_mark(kernel_irq_exit, MARK_NOARGS);
So we can know the elaspsed time in irq handlers and whether they are nested on user of kernel code.
The same for traps :
trace_mark(kernel_arch_trap_entry, "trap_id %d ip #p%ld", trapnr,
instruction_pointer(regs));
Where we know the trap number and the instruction pointer that caused the trap. Here again, we should put a :
trace_mark(kernel_arch_trap_exit, MARK_NOARGS);
At the end of the trap handlers.
It makes automatic analysis _much_ easier than trying to gather each and every handler instrumentation which would have a different name...
Mathieu
Quoted 175 lines
Signed-off-by Steven Rostedt <[email protected]>--- arch/x86/kernel/apic_32.c | 2 ++ arch/x86/kernel/irq_32.c | 1 + arch/x86/kernel/irq_64.c | 2 ++ arch/x86/kernel/traps_32.c | 2 ++ arch/x86/kernel/traps_64.c | 2 ++ arch/x86/mm/fault_32.c | 3 +++ arch/x86/mm/fault_64.c | 3 +++ kernel/hrtimer.c | 7 +++++++ kernel/sched.c | 11 +++++++++++ 9 files changed, 33 insertions(+)Index:
linux-mcount.git/arch/x86/kernel/apic_32.c
===================================================================arch/x86/kernel/apic_32.c +2
-0@@ -581,6 +581,8 @@ notrace fastcall void smp_apic_timer_int {struct pt_regs *old_regs =set_irq_regs (regs); +trace_mark (arch_apic_timer,"ip %lx" , regs->eip); +/* * NOTE! We'd better ACK the irq immediately, * because timer handling can be slow. Index:
linux-mcount.git/arch/x86/kernel/irq_32.c
===================================================================arch/x86/kernel/irq_32.c +1
-0@@ -85,6 +85,7 @@ fastcall unsigned int do_IRQ(struct pt_r old_regs =set_irq_regs (regs);irq_enter (); +trace_mark (arch_do_irq,"ip %lx irq %d" , regs->eip, irq);#ifdef CONFIG_DEBUG_STACKOVERFLOW /* Debugging check for stack overflow: is there less than 1KB free? */ {Index:
linux-mcount.git/arch/x86/kernel/irq_64.c
===================================================================arch/x86/kernel/irq_64.c +2
-0@@ -149,6 +149,8 @@ asmlinkage unsigned int do_IRQ(struct ptirq_enter (); irq =__get_cpu_var (vector_irq)[vector]; +trace_mark (arch_do_irq,"ip %lx irq %d" , regs->rip, irq); +#ifdef CONFIG_DEBUG_STACKOVERFLOW stack_overflow_check (regs);#endif Index:
linux-mcount.git/arch/x86/kernel/traps_32.c
===================================================================arch/x86/kernel/traps_32.c +2
-0@@ -769,6 +769,8 @@ fastcall __kprobes void do_nmi(struct ptnmi_enter (); +trace_mark (arch_do_nmi,"ip %lx flags %lx" , regs->eip, regs->eflags); + cpu =smp_processor_id (); ++nmi_count (cpu);Index:
linux-mcount.git/arch/x86/kernel/traps_64.c
===================================================================arch/x86/kernel/traps_64.c +2
-0@@ -782,6 +782,8 @@ asmlinkage __kprobes void default_do_nmi cpu =smp_processor_id (); +trace_mark (arch_do_nmi,"ip %lx flags %lx" , regs->rip, regs->eflags); +/* Only the BSP gets external NMIs from the system. */ if (!cpu) reason =get_nmi_reason ();Index:
linux-mcount.git/arch/x86/mm/fault_32.c
===================================================================arch/x86/mm/fault_32.c +3
-0@@ -311,6 +311,9 @@ fastcall void __kprobes do_page_fault(st/* get the address */ address =read_cr2 (); +trace_mark (arch_do_page_fault,"ip %lx err %lx addr %lx" , + regs->eip, error_code, address); + tsk = current; si_code =SEGV_MAPERR ;Index:
linux-mcount.git/arch/x86/mm/fault_64.c
===================================================================arch/x86/mm/fault_64.c +3
-0@@ -316,6 +316,9 @@ asmlinkage void __kprobes do_page_fault(/* get the address */ address =read_cr2 (); +trace_mark (arch_do_page_fault,"ip %lx err %lx addr %lx" , + regs->rip, error_code, address); + info.si_code =SEGV_MAPERR ;Index:
linux-mcount.git/kernel/hrtimer.c
===================================================================kernel/hrtimer.c +7
-0@@ -709,6 +709,8 @@ static void enqueue_hrtimer(struct hrtimstruct hrtimer *entry;int leftmost =1 ; +trace_mark (kernel_hrtimer_enqueue, +"expires %p timer %p" , &timer->expires, timer);/* * Find the right place in the rbtree: */ @@ -1130,6 +1132,7 @@ void hrtimer_interrupt(struct clock_even retry: now =ktime_get (); +trace_mark (kernel_hrtimer_interrupt,"now %p" , &now); expires_next.tv64 =KTIME_MAX ;@@ -1168,6 +1171,10 @@ void hrtimer_interrupt(struct clock_evencontinue ; } +trace_mark (kernel_hrtimer_interrupt_expire, +"expires %p timer %p" , + &timer->expires, timer); +__run_hrtimer (timer); }spin_unlock (&cpu_base->lock);Index:
linux-mcount.git/kernel/sched.c
===================================================================kernel/sched.c +11
-0@@ -90,6 +90,11 @@ unsigned long long __attribute__((weak))#define PRIO_TO_NICE (prio) ((prio) - MAX_RT_PRIO - 20)#define TASK_NICE (p) PRIO_TO_NICE((p)->static_prio) +#define __PRIO (prio) \ + ((prio) <= 99 ? 199 - (prio) : (prio) - 120) + +#define PRIO (p) __PRIO((p)->prio) +/* * 'User priority' is the nice value converted to something we * can work with better when scaling various scheduler parameters, @@ -1372,6 +1377,9 @@ static void activate_task(struct rq *rq,if (p->state ==TASK_UNINTERRUPTIBLE ) rq->nr_uninterruptible--; +trace_mark (kernel_sched_activate_task, +"pid %d prio %d nr_running %ld" , + p->pid,PRIO (p), rq->nr_running);enqueue_task (rq, p, wakeup);inc_nr_running (p, rq); }@@ -1385,6 +1393,9 @@ static void deactivate_task(struct rq *r rq->nr_uninterruptible++;dequeue_task (rq, p, sleep); +trace_mark (kernel_sched_deactivate_task, +"pid %d prio %d nr_running %ld" , + p->pid,PRIO (p), rq->nr_running);dec_nr_running (p, rq); }Signature · 1 line
--
Signature · 3 lines
Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Steven Rostedt
reviewerRe: [PATCH 21/23 -v8] Add markers to various events
5B
Quoted 11 lines · Mathieu Desnoyers
Quoted 3 lines · * Steven Rostedt ([email protected]) wrote:
This patch adds markers to various events in the kernel. (interrupts, task activation and hrtimers)
Hi Steven,
I would propose the following standard for IRQ handler markers:
trace_mark(kernel_irq_entry, "
irq_id%ukernel_mode%u", irq,
(regs)?(!user_mode(regs)):(1));
Are you saying that two markers with the same name is ok? That would be great if that is true.
Quoted 2 lines
....
trace_mark(kernel_irq_exit, MARK_NOARGS);
My patches don't use this (yet) so I'm leaving out adding markers that are not used. I'm not disagreeing with adding these, it's just that a patch series should only add markers that are actually used.
Quoted 12 lines
So we can know the elaspsed time in irq handlers and whether they are nested on user of kernel code.
The same for traps :
trace_mark(kernel_arch_trap_entry, "
trap_id%d ip #p%ld", trapnr,
instruction_pointer(regs));Where we know the trap number and the instruction pointer that caused the trap. Here again, we should put a :
Quoted 7 lines
trace_mark(kernel_arch_trap_exit, MARK_NOARGS);At the end of the trap handlers.
It makes automatic analysis _much_ easier than trying to gather each and every handler instrumentation which would have a different name...
-- Steve
Re: [PATCH 21/23 -v8] Add markers to various events
Quoted 11 lines · * Steven Rostedt ([email protected]) wrote:
5B
Quoted 7 lines · Mathieu Desnoyers
Quoted 3 lines · * Steven Rostedt ([email protected]) wrote:
This patch adds markers to various events in the kernel. (interrupts, task activation and hrtimers)
Hi Steven,
Quoted 8 lines
Quoted 4 lines
I would propose the following standard for IRQ handler markers:
trace_mark(kernel_irq_entry, "
irq_id%ukernel_mode%u", irq,
(regs)?(!user_mode(regs)):(1));Are you saying that two markers with the same name is ok? That would be great if that is true.
Yep. Just make sure that they have the exact same format string, otherwise the marker infrastructure will refuse to connect probes to them (and will printk an error message saying this). You can even put a marker in an inline function or unrolled loop (many markers will be generated and this is ok).
Quoted 7 lines
Quoted 2 lines
....
trace_mark(kernel_irq_exit, MARK_NOARGS);My patches don't use this (yet) so I'm leaving out adding markers that are not used. I'm not disagreeing with adding these, it's just that a patch series should only add markers that are actually used.
Ok, I agree with your approach. About using the markers, I wonder if it would be best for me to continue to concentrate my effort in getting the low-level stuff into the kernel first or to switch my focus on posting the higher-level stuff : the tracer itself, which is mostly a stand-alone kernel module.
Mathieu
Quoted 22 lines
Quoted 12 lines
So we can know the elaspsed time in irq handlers and whether they are nested on user of kernel code.
The same for traps :
trace_mark(kernel_arch_trap_entry, "
trap_id%d ip #p%ld", trapnr,
instruction_pointer(regs));Where we know the trap number and the instruction pointer that caused the trap. Here again, we should put a :
Quoted 7 lines
trace_mark(kernel_arch_trap_exit, MARK_NOARGS);At the end of the trap handlers.
It makes automatic analysis _much_ easier than trying to gather each and every handler instrumentation which would have a different name...
-- Steve
Signature · 3 lines
Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Steven Rostedt
reviewerPATCH 22/23 Add event tracer.
In reply to Steven RostedtThis patch adds a event trace that hooks into various events in the kernel. Although it can be used separately, it is mainly to help other traces (wakeup and preempt off) with seeing various events in the traces without having to enable the heavy mcount hooks.
--- lib/tracing/Kconfig | 12 + lib/tracing/Makefile | 1 lib/tracing/trace_events.c | 478 ++++++++++++++++++++++++++++++++++++++++++++ lib/tracing/trace_irqsoff.c | 6 lib/tracing/trace_wakeup.c | 55 ++++- lib/tracing/tracer.c | 159 ++++++++++---- lib/tracing/tracer.h | 64 +++++ 7 files changed, 724 insertions(+), 51 deletions(-)
lib/tracing/trace_events.c new file +478 -0
@@ -0,0 +1,478 @@ +/* +* trace task events +* +* Copyright (C) 2007 Steven Rostedt <[email protected]> +* +* Based on code from the latency_tracer, that is: +* +* Copyright (C) 2004-2006 Ingo Molnar +* Copyright (C) 2004 William Lee Irwin III +*/ +#include <linux/module.h> +#include <linux/fs.h> +#include <linux/debugfs.h> +#include <linux/kallsyms.h> +#include <linux/uaccess.h> +#include <linux/mcount.h> + +#include "tracer.h" + +static struct tracing_trace *tracer_trace__read_mostly ; +static int trace_enabled__read_mostly ; + +static void notrace event_reset(struct tracing_trace *tr) +{ +struct tracing_trace_cpu *data; +int cpu; + +for_each_possible_cpu (cpu) { + data = tr->data[cpu]; +tracing_reset (data); + } + + tr->time_start =now (); +} + +static void notrace event_trace_sched_switch(void *private, +struct task_struct *prev, +struct task_struct *next) +{ +struct tracing_trace **ptr = private; +struct tracing_trace *tr = *ptr; +struct tracing_trace_cpu *data; +unsigned long flags; +long disabled; +int cpu; + +if (!trace_enabled || !tr) +return ; + +local_irq_save (flags); + cpu =raw_smp_processor_id (); + data = tr->data[cpu]; + + disabled =atomic_inc_return (&data->disabled); +if (unlikely (disabled !=1 )) + goto out; + +tracing_sched_switch_trace (tr, data, prev, next, flags); + + out: +atomic_dec (&data->disabled); +local_irq_restore (flags); +} + +static struct tracer_switch_ops switch_ops__read_mostly = { + .func = event_trace_sched_switch, + .private = &tracer_trace, +}; + +notrace int trace_event_enabled(void) +{ +return trace_enabled && tracer_trace; +} + +/* Taken from sched.c */ +#define __PRIO (prio) \ + ((prio) <= 99 ? 199 - (prio) : (prio) - 120) + +#define PRIO (p) __PRIO((p)->prio) + +notrace void trace_event_wakeup(unsigned long ip, +struct task_struct *p, +struct task_struct *curr) +{ +struct tracing_trace *tr = tracer_trace; +struct tracing_trace_cpu *data; +unsigned long flags; +long disabled; +int cpu; + +if (!trace_enabled || !tr) +return ; + +local_irq_save (flags); + cpu =raw_smp_processor_id (); + data = tr->data[cpu]; + + disabled =atomic_inc_return (&data->disabled); +if (unlikely (disabled !=1 )) + goto out; + +/* record process's command line */ +tracing_record_cmdline (p); +tracing_record_cmdline (curr); +tracing_trace_pid (tr, data, flags, ip, p->pid,PRIO (p),PRIO (curr)); + + out: +atomic_dec (&data->disabled); +local_irq_restore (flags); +} + +struct event_probes { +const char *name; +const char *fmt; +void (*func)(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap); +int active; +int armed; +}; + +#define getarg (arg, ap) arg = va_arg(ap, typeof(arg)) + +static notrace void event_trace_apic_timer(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +unsigned long parent_ip; + +getarg (parent_ip, ap); + +tracing_trace_special (tr, data, flags, ip, parent_ip,0 ,0 ); +} + +static notrace void event_trace_do_irq(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +unsigned long parent_ip; +int irq; + +getarg (parent_ip, ap); +getarg (irq, ap); + +tracing_trace_special (tr, data, flags, ip, parent_ip, irq,0 ); +} + +static notrace void event_trace_do_page_fault(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +unsigned long parent_ip; +unsigned long err; +unsigned long addr; + +getarg (parent_ip, ap); +getarg (err, ap); +getarg (addr, ap); + +tracing_trace_special (tr, data, flags, ip, parent_ip, err, addr); +} + +static notrace void event_trace_hrtimer_timer(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +ktime_t *expires; +unsigned long timer; + +getarg (expires, ap); +getarg (timer, ap); + +tracing_trace_timer (tr, data, flags, ip,ktime_to_ns (*expires), timer); +} + +static notrace void event_trace_hrtimer_time(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +ktime_t *now; + +getarg (now, ap); + +tracing_trace_timer (tr, data, flags, ip,ktime_to_ns (*now),0 ); +} + +static notrace void event_trace_sched3(const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +int pid; +int prio; +int running; + +getarg (pid, ap); +getarg (prio, ap); +getarg (running, ap); + +tracing_trace_pid (tr, data, flags, ip, pid, prio, running); +} + +#ifndef CONFIG_WAKEUP_TRACER +static notrace event_trace_sched_wakeup (const struct event_probes *probe, +struct tracing_trace *tr, +struct tracing_trace_cpu *data, +unsigned long flags, +unsigned long ip, +va_list ap) +{ +struct task_struct *p; +struct task_struct *curr; + +getarg (p, ap); +getarg (curr, ap); + +tracing_record_cmdline (p); +tracing_record_cmdline (curr); +tracing_trace_pid (tr, data, flags, ip, p->pid,PRIO (p),PRIO (curr)); +} +#endif + +static struct event_probes event_probes[]__read_mostly = { + { + .name ="arch_apic_timer" , + .fmt ="ip %lx" , + .func = event_trace_apic_timer, + }, + { + .name ="arch_do_irq" , + .fmt ="ip %lx irq %d" , + .func = event_trace_do_irq, + }, + { + .name ="arch_do_page_fault" , + .fmt ="ip %lx err %lx addr %lx" , + .func = event_trace_do_page_fault, + }, + { + .name ="kernel_hrtimer_enqueue" , + .fmt ="expires %p timer %p" , + .func = event_trace_hrtimer_timer, + }, + { + .name ="kernel_hrtimer_interrupt" , + .fmt ="now %p" , + .func = event_trace_hrtimer_time, + }, + { + .name ="kernel_hrtimer_interrupt_expire" , + .fmt ="expires %p timer %p" , + .func = event_trace_hrtimer_timer, + }, + { + .name ="kernel_sched_activate_task" , + .fmt ="pid %d prio %d nr_running %ld" , + .func = event_trace_sched3, + }, + { + .name ="kernel_sched_deactivate_task" , + .fmt ="pid %d prio %d nr_running %ld" , + .func = event_trace_sched3, + }, +#ifndef CONFIG_WAKEUP_TRACER + { + .name ="kernel_sched_wakeup" , + .fmt ="p %p rq->curr %p" , + .func = event_trace_sched_wakeup, + }, +#endif + { + .name =NULL , + } +}; + +static notrace void event_trace_callback(const struct marker *mdata, +void *private_data, +const char *format, ...) +{ +struct event_probes *probe = mdata->private; +struct tracing_trace *tr = tracer_trace; +struct tracing_trace_cpu *data; +unsigned long flags; +unsigned long ip; +long disabled; +int cpu; +va_list ap; + +if (!trace_enabled || !tracer_trace) +return ; + +local_irq_save (flags); + cpu =raw_smp_processor_id (); + data = tr->data[cpu]; + + disabled =atomic_inc_return (&data->disabled); +if (unlikely (disabled !=1 )) + goto out; + + ip =CALLER_ADDR0 ; + +va_start (ap, format); + probe->func (probe, tr, data, flags, ip, ap); +va_end (ap); + + out: +atomic_dec (&data->disabled); +local_irq_restore (flags); +} + +static notrace void event_trace_arm(void) +{ +struct event_probes *probe; +int ret; + +for (probe = event_probes; probe->name; probe++) { +if (!probe->active || probe->armed) +continue ; + + ret =marker_arm (probe->name); +if (ret) +pr_info ("event trace: Couldn't arm probe %s\n" , + probe->name); +else + probe->armed =1 ; + } + +tracing_start_wakeup (); +} + +static notrace void event_trace_disarm(void) +{ +struct event_probes *probe; + +tracing_stop_wakeup (); + +for (probe = event_probes; probe->name; probe++) { +if (!probe->active || !probe->armed) +continue ; + +marker_disarm (probe->name); + probe->armed =0 ; + } +} + +static notrace void start_event_trace(struct tracing_trace *tr) +{ +event_trace_arm (); + +event_reset (tr); + trace_enabled =1 ; + +register_tracer_switch (&switch_ops); +tracing_start_sched_switch (); +tracing_start_function_trace (); +} + +static notrace void stop_event_trace(struct tracing_trace *tr) +{ +tracing_stop_function_trace (); + trace_enabled =0 ; +unregister_tracer_switch (&switch_ops); +tracing_stop_sched_switch (); +event_trace_disarm (); +} + +static notrace void event_trace_init(struct tracing_trace *tr) +{ + tracer_trace = tr; + +if (tr->ctrl) +start_event_trace (tr); +} + +static notrace void event_trace_reset(struct tracing_trace *tr) +{ +if (tr->ctrl) +stop_event_trace (tr); +} + +static void event_trace_ctrl_update (struct tracing_trace *tr) +{ +if (tr->ctrl) +start_event_trace (tr); +else +stop_event_trace (tr); +} + +static void notrace event_trace_open(struct tracing_iterator *iter) +{ +/* stop the trace while dumping */ +if (iter->tr->ctrl) +stop_event_trace (iter->tr); +} + +static void notrace event_trace_close(struct tracing_iterator *iter) +{ +if (iter->tr->ctrl) +start_event_trace (iter->tr); +} + +static struct trace_types_struct event_trace__read_mostly = +{ + .name ="events" , + .init = event_trace_init, + .reset = event_trace_reset, + .open = event_trace_open, + .close = event_trace_close, + .ctrl_update = event_trace_ctrl_update, +}; + +notrace void trace_event_register(struct tracing_trace *tr) +{ + tracer_trace = tr; +event_trace_arm (); +} + +notrace void trace_event_unregister(struct tracing_trace *tr) +{ +event_trace_disarm (); +} + +notrace void trace_start_events(void) +{ + trace_enabled =1 ; +} + +notrace void trace_stop_events(void) +{ + trace_enabled =0 ; +} + +__init static int init_event_trace (void ) +{ +struct event_probes *probe; +int ret; + + ret =register_trace (&event_trace); +if (ret) +return ret; + +for (probe = event_probes; probe->name; probe++) { + + ret =marker_probe_register (probe->name, + probe->fmt, + event_trace_callback, + probe); +if (ret) +pr_info ("event trace: Couldn't add marker" +" probe to %s\n" , probe->name); +else + probe->active =1 ; + } + +return 0 ; +} + +device_initcall (init_event_trace);
lib/tracing/Kconfig +12 -0
@@ -28,6 +28,18 @@ config FUNCTION_TRACER that the debugging mechanism using this facility will hook by providing a set of inline routines. +config EVENT_TRACER + bool "trace kernel events" + depends on DEBUG_KERNEL + select TRACING + select CONTEXT_SWITCH_TRACER + select MARKERS + help + This option activates the event tracer of the latency_tracer. + It activates markers through out the kernel for tracing. + This option has a fairly low overhead when enabled. + + config CRITICAL_IRQSOFF_TIMING bool "Interrupts-off critical section latency timing" default n
lib/tracing/Makefile +1 -0
@@ -6,5 +6,6 @@ obj-$(CONFIG_FUNCTION_TRACER) += trace_f obj-$(CONFIG_CRITICAL_IRQSOFF_TIMING) += trace_irqsoff.o obj-$(CONFIG_CRITICAL_PREEMPT_TIMING) += trace_irqsoff.o obj-$(CONFIG_WAKEUP_TRACER) += trace_wakeup.o +obj-$(CONFIG_EVENT_TRACER) += trace_events.o libmcount-y := mcount.o
lib/tracing/trace_wakeup.c +46 -9
117 lines not on this page · raw message
lib/tracing/tracer.c +117 -42
201 lines not on this page · raw message
lib/tracing/tracer.h +64 -0
92 lines not on this page · raw message
lib/tracing/trace_irqsoff.c +6 -0
@@ -217,6 +217,7 @@ start_critical_timing(unsigned long ip,local_save_flags (flags);tracing_function_trace (tr, data, ip, parent_ip, flags); +trace_start_events ();__get_cpu_var (tracing_cpu) =1 ;
@@ -249,6 +250,7 @@ stop_critical_timing(unsigned long ip, uatomic_inc (&data->disabled);local_save_flags (flags); +trace_stop_events ();tracing_function_trace (tr, data, ip, parent_ip, flags);check_critical_timing (tr, data, parent_ip ? : ip, cpu); data->critical_start =0 ;
@@ -372,12 +374,16 @@ static void __irqsoff_trace_init(struct/* make sure that the tracer is visibel */ smp_wmb (); +trace_event_register (tr); +if (tr->ctrl)start_irqsoff_trace (tr); }static void irqsoff_trace_reset (struct tracing_trace *tr) { +trace_event_unregister (tr); +if (tr->ctrl)stop_irqsoff_trace (tr); }
Steven Rostedt
reviewerPATCH 23/23 Critical latency timings histogram
In reply to Steven RostedtThis patch adds hooks into the latency tracer to give us histograms of interrupts off, preemption off and wakeup timings.
This code was based off of work done by Yi Yang <[email protected]>
But heavily modified to work with the new tracer, and some clean ups by Steven Rostedt <[email protected]>
This adds the following to /debugfs/tracing
latency_hist/ - root dir for historgrams.
Under latency_hist there is (depending on what's configured):
interrupt_off_latency/ - latency histograms of interrupts off.
preempt_interrupts_off_latency/ - latency histograms of
preemption and/or interrupts off.
preempt_off_latency/ - latency histograms of preemption off.
wakeup_latency/ - latency histograms of wakeup timings.
Under each of the above is a file labeled:
CPU# for each possible CPU were # is the CPU number.
reset - writing into this file will reset the histogram
back to zeros and start again.
--- lib/tracing/Kconfig | 20 + lib/tracing/Makefile | 4 lib/tracing/trace_irqsoff.c | 19 + lib/tracing/trace_wakeup.c | 21 + lib/tracing/tracer_hist.c | 514 ++++++++++++++++++++++++++++++++++++++++++++ lib/tracing/tracer_hist.h | 39 +++ 6 files changed, 613 insertions(+), 4 deletions(-)
lib/tracing/Kconfig +20 -0
@@ -102,3 +102,23 @@ config CONTEXT_SWITCH_TRACER This tracer hooks into the context switch and records all switching of tasks. +config INTERRUPT_OFF_HIST + bool "Interrupts off critical timings histogram" + depends on CRITICAL_IRQSOFF_TIMING + help + This option uses the infrastructure of the critical + irqs off timings to create a histogram of latencies. + +config PREEMPT_OFF_HIST + bool "Preempt off critical timings histogram" + depends on CRITICAL_PREEMPT_TIMING + help + This option uses the infrastructure of the critical + preemption off timings to create a histogram of latencies. + +config WAKEUP_LATENCY_HIST + bool "Interrupts off critical timings histogram" + depends on WAKEUP_TRACER + help + This option uses the infrastructure of the wakeup tracer + to create a histogram of latencies.
lib/tracing/Makefile +4 -0
@@ -8,4 +8,8 @@ obj-$(CONFIG_CRITICAL_PREEMPT_TIMING) += obj-$(CONFIG_WAKEUP_TRACER) += trace_wakeup.o obj-$(CONFIG_EVENT_TRACER) += trace_events.o +obj-$(CONFIG_INTERRUPT_OFF_HIST) += tracer_hist.o +obj-$(CONFIG_PREEMPT_OFF_HIST) += tracer_hist.o +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += tracer_hist.o + libmcount-y := mcount.o
lib/tracing/trace_irqsoff.c +19 -0
@@ -16,6 +16,7 @@#include <linux/mcount.h> #include "tracer.h" +#include "tracer_hist.h" static struct tracing_trace *tracer_trace__read_mostly ;static __cacheline_aligned_in_smp DEFINE_MUTEX (max_mutex );
@@ -262,10 +263,14 @@ void notrace start_critical_timings(void {if (preempt_trace () ||irq_trace ())start_critical_timing (CALLER_ADDR0 ,0 ); + +tracing_hist_preempt_start (); } void notrace stop_critical_timings(void) { +tracing_hist_preempt_stop (TRACE_STOP ); +if (preempt_trace () ||irq_trace ())stop_critical_timing (CALLER_ADDR0 ,0 ); }
@@ -274,6 +279,8 @@ void notrace stop_critical_timings(void)#ifdef CONFIG_LOCKDEP void notrace time_hardirqs_on(unsigned long a0, unsigned long a1) { +tracing_hist_preempt_stop (1 ); +if (!preempt_trace () &&irq_trace ())stop_critical_timing (a0, a1); }
@@ -282,6 +289,8 @@ void notrace time_hardirqs_off(unsigned {if (!preempt_trace () &&irq_trace ())start_critical_timing (a0, a1); + +tracing_hist_preempt_start (); }#else /* !CONFIG_LOCKDEP */
@@ -315,6 +324,8 @@ inline void print_irqtrace_events(struct*/ void notrace trace_hardirqs_on(void) { +tracing_hist_preempt_stop (1 ); +if (!preempt_trace () &&irq_trace ())stop_critical_timing (CALLER_ADDR0 ,0 ); }
@@ -324,11 +335,15 @@ void notrace trace_hardirqs_off(void) {if (!preempt_trace () &&irq_trace ())start_critical_timing (CALLER_ADDR0 ,0 ); + +tracing_hist_preempt_start (); }EXPORT_SYMBOL (trace_hardirqs_off); void notrace trace_hardirqs_on_caller(unsigned long caller_addr) { +tracing_hist_preempt_stop (1 ); +if (!preempt_trace () &&irq_trace ())stop_critical_timing (CALLER_ADDR0 , caller_addr); }
@@ -338,6 +353,8 @@ void notrace trace_hardirqs_off_caller(u {if (!preempt_trace () &&irq_trace ())start_critical_timing (CALLER_ADDR0 , caller_addr); + +tracing_hist_preempt_start (); }EXPORT_SYMBOL (trace_hardirqs_off_caller);
@@ -347,12 +364,14 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller)#ifdef CONFIG_CRITICAL_PREEMPT_TIMING void notrace trace_preempt_on(unsigned long a0, unsigned long a1) { +tracing_hist_preempt_stop (0 );stop_critical_timing (a0, a1); } void notrace trace_preempt_off(unsigned long a0, unsigned long a1) {start_critical_timing (a0, a1); +tracing_hist_preempt_start (); }#endif /* CONFIG_CRITICAL_PREEMPT_TIMING */
lib/tracing/trace_wakeup.c +17 -4
@@ -16,6 +16,7 @@#include <linux/mcount.h> #include "tracer.h" +#include "tracer_hist.h" static struct tracing_trace *tracer_trace__read_mostly ;static int trace_enabled__read_mostly ;
@@ -55,7 +56,9 @@ static void notrace wakeup_sched_switch(unsigned long flags;int cpu; -if (unlikely (!trace_enabled)) +tracing_hist_wakeup_stop (next); + +if (!trace_enabled)return ;/*
@@ -226,7 +229,8 @@ static notrace void wake_up_callback(construct task_struct *p;va_list ap; -if (likely (!trace_enabled) && !trace_event_enabled ()) +if (likely (!trace_enabled) && + !trace_event_enabled () && !tracing_wakeup_hist)return ;va_start (ap, format);
@@ -238,6 +242,7 @@ static notrace void wake_up_callback(conva_end (ap);trace_event_wakeup (CALLER_ADDR0 , p, curr); +tracing_hist_wakeup_start (p, curr);if (trace_enabled)wakeup_check_start (tr, p, curr);
@@ -292,7 +297,8 @@ static notrace void start_wakeup_trace(sif (ret)return ; -register_tracer_switch (&switch_ops); +if (!tracing_wakeup_hist) +register_tracer_switch (&switch_ops);tracing_start_sched_switch ();trace_start_events ();
@@ -317,7 +323,8 @@ static notrace void stop_wakeup_trace(st trace_enabled =0 ;tracing_stop_wakeup ();tracing_stop_sched_switch (); -unregister_tracer_switch (&switch_ops); +if (!tracing_wakeup_hist) +unregister_tracer_switch (&switch_ops); } static notrace void wakeup_trace_init(struct tracing_trace *tr)
@@ -403,6 +410,12 @@ __init static int init_wakeup_trace(void goto fail_deprobe; } +if (tracing_wakeup_hist) { +register_tracer_switch (&switch_ops); +tracing_start_wakeup (); +tracing_start_sched_switch (); + } +return 0 ; fail_deprobe:marker_probe_unregister ("kernel_sched_wakeup" );
lib/tracing/tracer_hist.c new file +514 -0
514 lines not on this page · raw message
lib/tracing/tracer_hist.h new file +39 -0
@@ -0,0 +1,39 @@ +/* +* lib/tracing/tracer_hist.h +* +* Add support for histograms of preemption-off latency and +* interrupt-off latency and wakeup latency, it depends on +* Real-Time Preemption Support. +* +* Copyright (C) 2005 MontaVista Software, Inc. +* Yi Yang <[email protected]> +* +* Converted to work with the new latency tracer. +* Copyright (C) 2008 Red Hat, Inc. +* Steven Rostedt <[email protected]> +* +*/ +#ifndef _LIB_TRACING_TRACER_HIST_H_ +#define _LIB_TRACING_TRACER_HIST_H_ + +#if defined(CONFIG_INTERRUPT_OFF_HIST ) || defined(CONFIG_PREEMPT_OFF_HIST ) +# define TRACE_STOP 2 +void tracing_hist_preempt_start (void ); +void tracing_hist_preempt_stop (int irqs_on); +#else +# define tracing_hist_preempt_start () do { } while (0) +# define tracing_hist_preempt_stop (irqs_off) do { } while (0) +#endif + +#ifdef CONFIG_WAKEUP_LATENCY_HIST +void tracing_hist_wakeup_start (struct task_struct *p, +struct task_struct *curr); +void tracing_hist_wakeup_stop (struct task_struct *next); +extern int tracing_wakeup_hist; +#else +# define tracing_hist_wakeup_start (p, curr) do { } while (0) +# define tracing_hist_wakeup_stop (next) do { } while (0) +# define tracing_wakeup_hist 0 +#endif + +#endif /* ifndef _LIB_TRACING_TRACER_HIST_H_ */
Tim Bird
AppliedQuoted 2 lines · Steven Rostedt
[
version 8 of mcount / trace patches:
I applied these to 2.6.24, making a few fixups to do so. If you haven't already done this, I can send them to you.
Here is the list of stuff I found.
mcount-add-basic-support-for-gcc-profiler-instrum.patch:
fix for changes in patch context in lib/Kconfig.debug
mcount-trace-latency-trace-preempt-off.patch:
fix for missing #include hrtimer.h in kernel/sched.c
tracer-add-event-markers.patch:Changelog:
Had to adjust patch for change in kernel/hrtimer.c:hrtimer_interrupt() I'm not sure if I did this right.
Thanks for the patch set. I plan to give it a test today.
-- Tim
=============================
Tim Bird
Architecture Group Chair, CE Linux Forum Senior Staff Engineer, Sony Corporation of America
=============================