Some new features for the preempt/irqsoff tracers
Subsystems (3)
v2 cover Some new features for the preempt/irqsoff tracers
Hello all,
Changes in v2:
- I have tried to improve some of the commit messages by adding additional explanations.
- [PATCH 2/4]: We use burst size checking instead of the confusing modulo game. The example given in the Kconfig file is corrected and extended. I was not able to find a way to use the module parameters as trigger, so I have left the sysfs trigger file as is.
This series is meant to address two issues with the latency tracing.
The first three patches provide a method to trace latencies that always occurs very close to each other and to differentiate between them, in spite of the fact that the latency tracers always work in overwrite mode.
[PATCH 1/4] This implement fs notification for preempt/irqsoff. It makes it possible for userspace to detect when a new latency has been detected.
[PATCH 2/4] This extends the preemptirq_delay_test module so that it can be used to generate a burst of closely occurring latencies.
[PATCH 3/4] This adds a user space program to the tools directory that utilizes the fs notification feature and a randomized algorithm to print out any of the latencies in a burst with approximately equal probability.
The last patch is not directly connected but doesn't apply cleanly on its own:
[PATCH 4/4] This adds the option CONFIG_TRACE_CONSOLE_LATENCY to decide whether we want to trace prints to the console or not.
best regards,
Viktor Rosendahl
Viktor Rosendahl (4):
ftrace: Implement fs notification for preempt/irqsoff tracers
preemptirq_delay_test: Add the burst feature and a sysfs trigger Add the latency-collector to tools
ftrace: Add an option for tracing console latencies
include/linux/irqflags.h | 13 + kernel/printk/printk.c | 5 +- kernel/trace/Kconfig | 27 +- kernel/trace/preemptirq_delay_test.c | 145 +++- kernel/trace/trace.c | 31 +- kernel/trace/trace.h | 5 + kernel/trace/trace_irqsoff.c | 35 + tools/Makefile | 14 +- tools/trace/Makefile | 20 + tools/trace/latency-collector.c | 1190 ++++++++++++++++++++++++++ 10 files changed, 1453 insertions(+), 32 deletions(-) create mode 100644 tools/trace/Makefile create mode 100644 tools/trace/latency-collector.c
Signature · 1 line
2.17.1
PATCH v2 1/4 ftrace: Implement fs notification for preempt/irqsoff tracers
This patch implements the feature that the trace file, e.g. /sys/kernel/debug/tracing/trace will receive notifications through the fsnotify framework when a new trace is available.
This makes it possible to implement a user space program that can, with equal probability, obtain traces of latencies that occur immediately after each other in spite of the fact that the preempt/irqsoff tracers operate in overwrite mode.
--- kernel/trace/Kconfig | 10 ++++++++++ kernel/trace/trace.c | 31 +++++++++++++++++++++++++++++-- kernel/trace/trace.h | 5 +++++ kernel/trace/trace_irqsoff.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-)
kernel/trace/Kconfig +10 -0
@@ -234,6 +234,16 @@ config PREEMPT_TRACER enabled. This option and the irqs-off timing option can be used together or separately.) + config PREEMPTIRQ_FSNOTIFY + bool "Generate fsnotify events for the latency tracers" + default n + depends on (IRQSOFF_TRACER || PREEMPT_TRACER) && FSNOTIFY + help + This option will enable the generation of fsnotify events for the + trace file. This makes it possible for userspace to be notified about + modification of /sys/kernel/debug/tracing/trace through the inotify + interface. + config SCHED_TRACER bool "Scheduling Latency Tracer" select GENERIC_TRACER
kernel/trace/trace.c +29 -2
@@ -44,6 +44,8 @@#include <linux/trace.h> #include <linux/sched/clock.h> #include <linux/sched/rt.h> +#include <linux/fsnotify.h> +#include <linux/workqueue.h> #include "trace.h" #include "trace_output.h"
@@ -8191,6 +8193,32 @@ static __init void create_trace_instances(struct dentry *d_tracer)return ; } +#ifdef CONFIG_PREEMPTIRQ_FSNOTIFY + +static void trace_notify_workfn (struct work_struct *work) +{ + struct trace_array *tr = container_of(work, struct trace_array, + notify_work); +fsnotify (tr->d_trace->d_inode,FS_MODIFY , tr->d_trace->d_inode, +FSNOTIFY_EVENT_INODE ,NULL ,0 ); +} + +static void trace_create_trace_file (struct trace_array *tr, +struct dentry *d_tracer) +{ +/* For notify we need to init the work structure and save the pointer */ +INIT_WORK (&tr->notify_work, trace_notify_workfn); + tr->d_trace =trace_create_file ("trace" ,0644 , d_tracer, tr, + &tracing_fops); +} + +#else /* !CONFIG_PREEMPTIRQ_FSNOTIFY */ + +#define trace_create_trace_file (tr, d_tracer) \ + trace_create_file("trace", 0644, d_tracer, tr, &tracing_fops) + +#endif +static void init_tracer_tracefs (struct trace_array *tr,struct dentry *d_tracer) {
@@ -8209,8 +8237,7 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)trace_create_file ("trace_options" ,0644 , d_tracer, tr, &tracing_iter_fops); -trace_create_file ("trace" ,0644 , d_tracer, - tr, &tracing_fops); +trace_create_trace_file (tr, d_tracer);trace_create_file ("trace_pipe" ,0444 , d_tracer, tr, &tracing_pipe_fops);
kernel/trace/trace.h +5 -0
@@ -17,6 +17,7 @@#include <linux/compiler.h> #include <linux/trace_seq.h> #include <linux/glob.h> +#include <linux/workqueue.h> #ifdef CONFIG_FTRACE_SYSCALLS #include <asm/unistd.h> /* For NR_SYSCALLS */
@@ -302,6 +303,10 @@ struct trace_array {struct dentry *options;struct dentry *percpu_dir;struct dentry *event_dir; +#ifdef CONFIG_PREEMPTIRQ_FSNOTIFY +struct dentry *d_trace; +struct work_struct notify_work; +#endif struct trace_options *topts;struct list_head systems;struct list_head events;
kernel/trace/trace_irqsoff.c +35 -0
@@ -82,6 +82,31 @@ static inline int irqsoff_display_graph(struct trace_array *tr, int set)*/ static __cacheline_aligned_in_smp unsigned long max_sequence; +#ifdef CONFIG_PREEMPTIRQ_FSNOTIFY + +static struct workqueue_struct *notify_wq; + +static __init void trace_file_notify_init (void ) +{ + notify_wq =alloc_workqueue ("irqsoff_notify_wq" , +WQ_UNBOUND |WQ_HIGHPRI ,0 ); +if (!notify_wq) +pr_err ("Unable to allocate irqsoff_notify_wq" ); +} + +static inline void trace_file_notify (struct trace_array *tr) +{ +if (likely (notify_wq)) +queue_work (notify_wq, &tr->notify_work); +} + +#else /* !CONFIG_PREEMPTIRQ_FSNOTIFY */ + +#define trace_file_notify_init () do {} while (0) +#define trace_file_notify (tr) do {} while (0) + +#endif /* !CONFIG_PREEMPTIRQ_FSNOTIFY */ +#ifdef CONFIG_FUNCTION_TRACER /* * Prologue for the preempt and irqs off function tracers.
@@ -323,6 +348,7 @@ check_critical_timing(struct trace_array *tr,u64 T0 ,T1 , delta;unsigned long flags;int pc; +bool notify = false;T0 = data->preempt_timestamp;T1 =ftrace_now (cpu);
@@ -353,6 +379,7 @@ check_critical_timing(struct trace_array *tr,if (likely (!is_tracing_stopped ())) { tr->max_latency = delta;update_max_tr_single (tr, current, cpu); + notify = true; } max_sequence++;
@@ -364,6 +391,13 @@ check_critical_timing(struct trace_array *tr, data->critical_sequence = max_sequence; data->preempt_timestamp =ftrace_now (cpu);__trace_function (tr,CALLER_ADDR0 , parent_ip, flags, pc); + +/* +* We are optimizing for a high threshold, meaning that this will +* happen seldom +*/ +if (unlikely (notify)) +trace_file_notify (tr); }static nokprobe_inline void
@@ -745,6 +779,7 @@ static struct tracer preemptirqsoff_tracer __read_mostly =__init static int init_irqsoff_tracer (void ) { +trace_file_notify_init ();#ifdef CONFIG_IRQSOFF_TRACER register_tracer (&irqsoff_tracer);#endif
Signature · 1 line
2.17.1
Re: [PATCH v2 1/4] ftrace: Implement fs notification for preempt/irqsoff tracers
Quoted 10 lines · Viktor Rosendahl
This patch implements the feature that the trace file, e.g.
/sys/kernel/debug/tracing/tracewill receive notifications through the fsnotify framework when a new trace is available.This makes it possible to implement a user space program that can, with equal probability, obtain traces of latencies that occur immediately after each other in spite of the fact that the preempt/irqsoff tracers operate in overwrite mode.
Signed-off-by Viktor Rosendahl <[email protected]>
I agree with the general idea, but I don't really like how it is done in the patch.
We do have a notification mechanism already in the form of trace_pipe. Can we not improve that in some way to be notified of a new trace data? In theory, the trace_pipe does fit into the description in the documentation: "Reads from this file will block until new data is retrieved"
More comment below:
Quoted 14 lines
--- kernel/trace/Kconfig | 10 ++++++++++ kernel/trace/trace.c | 31 +++++++++++++++++++++++++++++-- kernel/trace/trace.h | 5 +++++ kernel/trace/trace_irqsoff.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-)kernel/trace/Kconfig +0
-0@@ -234,6 +234,16 @@ config PREEMPT_TRACER enabled. This option and the irqs-off timing option can be used together or separately.)
kernel/trace/Kconfig config PREEMPT_TRACER · line 245
Quoted 10 lines
kernel/trace/Kconfig +9
-0+ config PREEMPTIRQ_FSNOTIFY + bool "Generate fsnotify events for the latency tracers" + default n + depends on (IRQSOFF_TRACER || PREEMPT_TRACER) && FSNOTIFY + help + This option will enable the generation of fsnotify events for the + trace file. This makes it possible for userspace to be notified about + modification of /sys/kernel/debug/tracing/trace through the inotify + interface.
Does this have to be a CONFIG option? If prefer if the code automatically does the notification and it is always enabled. I don't see any drawbacks of that.
Quoted 18 lines
kernel/trace/Kconfig +1
-0+ config SCHED_TRACER bool "Scheduling Latency Tracer" select GENERIC_TRACERkernel/trace/trace.c +2
-0@@ -44,6 +44,8 @@#include <linux/trace.h> #include <linux/sched/clock.h> #include <linux/sched/rt.h> +#include <linux/fsnotify.h> +#include <linux/workqueue.h> #include "trace.h" #include "trace_output.h"
kernel/trace/trace.c static __init void create_trace_instances(struct dentry *d_tracer) · line 8198
Quoted 6 lines
kernel/trace/trace.c +3
-0@@ -8191,6 +8193,32 @@ static __init void create_trace_instances(struct dentry *d_tracer)return ; } +#ifdef CONFIG_PREEMPTIRQ_FSNOTIFY + +static void trace_notify_workfn (struct work_struct *work)
[snip]
I prefer if this facility is available to other tracers as well such as the wakeup tracer which is similar in output (check Documentation/trace/ftrace.txt). I believe this should be a generic trace facility, and not tracer specific.
thanks,
- Joel
Steven Rostedt
maintainerRe: [PATCH v2 1/4] ftrace: Implement fs notification for preempt/irqsoff tracers
Quoted 2 lines · On Sat, 4 May 2019 12:47:10 -0400 Joel Fernandes <[email protected]> wrote:
I agree with the general idea, but I don't really like how it is done in the patch.
+1
Quoted 10 lines
We do have a notification mechanism already in the form of
trace_pipe. Can we not improve that in some way to be notified of a new trace data? In theory, thetrace_pipedoes fit into the description in the documentation: "Reads from this file will block until new data is retrieved"More comment below:
Quoted 13 lines
kernel/trace/Kconfig config PREEMPT_TRACER · line 245
Quoted 9 lines
kernel/trace/Kconfig +9
-0+ config PREEMPTIRQ_FSNOTIFY + bool "Generate fsnotify events for the latency tracers" + default n + depends on (IRQSOFF_TRACER || PREEMPT_TRACER) && FSNOTIFY + help + This option will enable the generation of fsnotify events for the + trace file. This makes it possible for userspace to be notified about + modification of /sys/kernel/debug/tracing/trace through the inotify + interface.Does this have to be a CONFIG option? If prefer if the code automatically does the notification and it is always enabled. I don't see any drawbacks of that.
I mentioned that anything it needs to be an option.
Quoted 9 lines
kernel/trace/trace.c static __init void create_trace_instances(struct dentry *d_tracer) · line 8198
Quoted 3 lines
kernel/trace/trace.c +3
-0+#ifdef CONFIG_PREEMPTIRQ_FSNOTIFY + +static void trace_notify_workfn (struct work_struct *work)[snip]
I prefer if this facility is available to other tracers as well such as the wakeup tracer which is similar in output (check
Documentation/trace/ftrace.txt). I believe this should be a generic trace facility, and not tracer specific.
For what it's worth, I agree with everything Joel just stated.
Thanks,
-- Steve
Re: [PATCH v2 1/4] ftrace: Implement fs notification for preempt/irqsoff tracers
In reply to Joel FernandesQuoted 9 lines · Joel Fernandes
Quoted 4 lines · Viktor Rosendahl
immediately after each other in spite of the fact that the preempt/irqsoff tracers operate in overwrite mode.
Signed-off-by Viktor Rosendahl <[email protected]>I agree with the general idea, but I don't really like how it is done in the patch.
Can you explain more precisely what you agree with?
The general idea of being able to trace bursts of latencies?
Or the slightly more specific idea of notifying user space when /sys/kernel/debug/tracing/trace has new data, and let user space do the rest?
Quoted 5 lines
We do have a notification mechanism already in the form of
trace_pipe. Can we not improve that in some way to be notified of a new trace data? In theory, thetrace_pipedoes fit into the description in the documentation: "Reads from this file will block until new data is retrieved"
I am not quite sure what kind of solution you are after here. Could you be more specific?
I think that it would be weird if we used trace_pipe to send a message with the meaning "new data is available in the trace file". To me this would seem like (re)inventing a special purpose alternative to inotify.
Another option would be to allow the user to consume the the latency traces directly from trace_pipe, without using the trace file. This would make sense to me and would indeed be a much better solution. If somebody is able to make such an implementation I am all for it.
However, I do not know how to do this. I believe that it would lead towards a significant rewrite of the latency tracers, probably also how the ftrace buffering works.
My solution is clearly a hack but it has the benefits of requiring quite small and straightforward changes to the kernel and the really ugly things are then done in user space.
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ config PREEMPTIRQ_FSNOTIFYDoes this have to be a CONFIG option? If prefer if the code automatically does the notification and it is always enabled. I don't see any drawbacks of that.
I was just thinking that sending fsnotify events to a sysfs file is a bit off the beaten track and I figured that some people would like to opt out.
It can of course be changed so that it is always enabled when a tracer that requires it is enabled, if there is general agreement on this point.
Quoted 26 lines
Quoted 24 lines
(quoted diff) +1
-0+ config SCHED_TRACER bool "Scheduling Latency Tracer" select GENERIC_TRACERkernel/trace/trace.c +5
-0@@ -44,6 +44,8 @@#include <linux/trace.h> #include <linux/sched/clock.h> #include <linux/sched/rt.h> +#include <linux/fsnotify.h> +#include <linux/workqueue.h> #include "trace.h" #include "trace_output.h" @@ -8191,6 +8193,32 @@ static __init void create_trace_instances(struct dentry *d_tracer)return ; } +#ifdef CONFIG_PREEMPTIRQ_FSNOTIFY + +static void trace_notify_workfn (struct work_struct *work)[snip]
Quoted 5 lines
I prefer if this facility is available to other tracers as well such as the wakeup tracer which is similar in output (check
Documentation/trace/ftrace.txt). I believe this should be a generic trace facility, and not tracer specific.
This should be possible.
If we stick with the fsnotify idea, it's possible to move the functions for it to trace.c and to use it also from other tracers.
It is also possible to do a few small adjustments to the latency-collector and to rename it the trace-collector, so that it can work for the wakeup case also. The random sleep games probably don't make sense for the wakeup case but it's already now a command line option.
Below is a suggestion for how to make the facility more generic and usable by by both preempt/irqsoff and wakeup.
best regards,
Viktor
--- kernel/trace/Kconfig | 11 +++++++ kernel/trace/trace.c | 50 +++++++++++++++++++++++++++++-- kernel/trace/trace.h | 15 ++++++++++ kernel/trace/trace_irqsoff.c | 5 ++++ kernel/trace/trace_sched_wakeup.c | 7 ++++- 5 files changed, 85 insertions(+), 3 deletions(-)
kernel/trace/Kconfig +11 -0
@@ -244,6 +244,17 @@ config SCHED_TRACER This tracer tracks the latency of the highest priority task to be scheduled in, starting from the point it has woken up. +config TRACE_FSNOTIFY + bool "Generate fsnotify events for the trace file" + default n + depends on (IRQSOFF_TRACER || PREEMPT_TRACER || SCHED_TRACER) + depends on FSNOTIFY + help + This option will enable the generation of fsnotify events for the + trace file. This makes it possible for userspace to be notified about + modification of /sys/kernel/debug/tracing/trace through the inotify + interface. + config HWLAT_TRACER bool "Tracer to detect hardware latencies (like SMIs)" select GENERIC_TRACER
kernel/trace/trace.c +48 -2
74 lines not on this page · raw message
kernel/trace/trace.h +15 -0
@@ -17,6 +17,7 @@#include <linux/compiler.h> #include <linux/trace_seq.h> #include <linux/glob.h> +#include <linux/workqueue.h> #ifdef CONFIG_FTRACE_SYSCALLS #include <asm/unistd.h> /* For NR_SYSCALLS */
@@ -302,6 +303,10 @@ struct trace_array {struct dentry *options;struct dentry *percpu_dir;struct dentry *event_dir; +#ifdef CONFIG_TRACE_FSNOTIFY +struct dentry *d_trace; +struct work_struct notify_work; +#endif struct trace_options *topts;struct list_head systems;struct list_head events;
@@ -774,6 +779,16 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,struct trace_pid_list **new_pid_list, const char __user *ubuf, size_t cnt); +#ifdef CONFIG_TRACE_FSNOTIFY + +void trace_file_notify (struct trace_array *tr); + +#else /* !CONFIG_TRACE_FSNOTIFY */ + +#define trace_file_notify (tr) do {} while (0) + +#endif /* !CONFIG_TRACE_FSNOTIFY */ +#ifdef CONFIG_TRACER_MAX_TRACE void update_max_tr (struct trace_array *tr,struct task_struct *tsk,int cpu,void *cond_data);
kernel/trace/trace_irqsoff.c +5 -0
@@ -323,6 +323,7 @@ check_critical_timing(struct trace_array *tr,u64 T0 ,T1 , delta;unsigned long flags;int pc; +bool notify = false;T0 = data->preempt_timestamp;T1 =ftrace_now (cpu);
@@ -353,6 +354,7 @@ check_critical_timing(struct trace_array *tr,if (likely (!is_tracing_stopped ())) { tr->max_latency = delta;update_max_tr_single (tr, current, cpu); + notify = true; } max_sequence++;
@@ -364,6 +366,9 @@ check_critical_timing(struct trace_array *tr, data->critical_sequence = max_sequence; data->preempt_timestamp =ftrace_now (cpu);__trace_function (tr,CALLER_ADDR0 , parent_ip, flags, pc); + +if (unlikely (notify)) +trace_file_notify (tr); }static nokprobe_inline void
kernel/trace/trace_sched_wakeup.c +6 -1
@@ -437,6 +437,8 @@ probe_wakeup_sched_switch(void *ignore, bool preempt,long disabled;int cpu;int pc; +bool report = false; +struct trace_array *tr = wakeup_trace;tracing_record_cmdline (prev);
@@ -481,7 +483,8 @@ probe_wakeup_sched_switch(void *ignore, bool preempt,T1 =ftrace_now (cpu); delta =T1 -T0 ; -if (!report_latency (wakeup_trace, delta)) + report =report_latency (wakeup_trace, delta); +if (!report) goto out_unlock;if (likely (!is_tracing_stopped ())) {
@@ -495,6 +498,8 @@ probe_wakeup_sched_switch(void *ignore, bool preempt,local_irq_restore (flags); out:atomic_dec (&per_cpu_ptr (wakeup_trace->trace_buffer.data, cpu)->disabled); +if (unlikely (report)) +trace_file_notify (tr); }static void __wakeup_reset (struct trace_array *tr)
Signature · 1 line
2.17.1
Steven Rostedt
maintainerRe: [PATCH v2 1/4] ftrace: Implement fs notification for preempt/irqsoff tracers
Quoted 3 lines · On Mon, 6 May 2019 00:39:15 +0200 Viktor Rosendahl…
Can you explain more precisely what you agree with?
The general idea of being able to trace bursts of latencies?
One thing I have an issue with the current approach is the use of the trace file for this.
Quoted 17 lines
Or the slightly more specific idea of notifying user space when
/sys/kernel/debug/tracing/tracehas new data, and let user space do the rest?Quoted 5 lines
We do have a notification mechanism already in the form of
trace_pipe. Can we not improve that in some way to be notified of a new trace data? In theory, thetrace_pipedoes fit into the description in the documentation: "Reads from this file will block until new data is retrieved"I am not quite sure what kind of solution you are after here. Could you be more specific?
I think that it would be weird if we used
trace_pipeto send a message with the meaning "new data is available in the trace file". To me this would seem like (re)inventing a special purpose alternative to inotify.
Quoted 9 lines
Another option would be to allow the user to consume the the latency traces directly from
trace_pipe, without using the trace file. This would make sense to me and would indeed be a much better solution. If somebody is able to make such an implementation I am all for it.However, I do not know how to do this. I believe that it would lead towards a significant rewrite of the latency tracers, probably also how the ftrace buffering works.
Hmm, what about adding a notifier to tracing_max_latency instead? And do it not as a config option, but have it always enabled. It would send a notification when it changes, and that only happens when there's a new max latency. Would that work for you?
-- Steve
Re: [PATCH v2 1/4] ftrace: Implement fs notification for preempt/irqsoff tracers
Quoted 9 lines · Steven Rostedt
Quoted 3 lines · On Mon, 6 May 2019 00:39:15 +0200 Viktor Rosendahl…
Can you explain more precisely what you agree with?
The general idea of being able to trace bursts of latencies?
One thing I have an issue with the current approach is the use of the trace file for this.
You mean that using fsnotify is kind of okayish but that it's confusing for the user because only a subset of tracers would send the fsnotify event when the trace file is updated?
Quoted 5 lines
Hmm, what about adding a notifier to
tracing_max_latencyinstead? And do it not as a config option, but have it always enabled. It would send a notification when it changes, and that only happens when there's a new max latency. Would that work for you?
Yes, it seems to be OK since the tracing_max_latency is updated also with the latest latency that exceeds the threshold when we are using tracing_thresh. I will try to send a new version of the patch series soon, with the modifications that have been discussed so far.
best regards,
Viktor
Re: [PATCH v2 1/4] ftrace: Implement fs notification for preempt/irqsoff tracers
Quoted 1 line · Viktor Rosendahl
On 5/6/19 1:01 AM, Steven Rostedt wrote:
[snip]
Quoted 4 lines
Quoted 4 lines
Hmm, what about adding a notifier to
tracing_max_latencyinstead? And do it not as a config option, but have it always enabled. It would send a notification when it changes, and that only happens when there's a new max latency. Would that work for you?
That sounds like a much better approach. thanks,
- Joel
PATCH v2 2/4 preemptirq_delay_test: Add the burst feature and a sysfs trigger
In reply to Viktor RosendahlThis burst feature enables the user to generate a burst of preempt/irqsoff latencies. This makes it possible to test whether we are able to detect latencies that systematically occur very close to each other.
The maximum burst size is 10. We also create 10 identical test functions, so that we get 10 different backtraces; this is useful when we want to test whether we can detect all the latencies in a burst. Otherwise, there would be no easy way of differentiating between which latency in a burst was captured by the tracer.
In addition, there is a sysfs trigger, so that it's not necessary to reload the module to repeat the test. The trigger will appear as /sys/kernel/preemptirq_delay_test/trigger in sysfs.
--- kernel/trace/Kconfig | 6 +- kernel/trace/preemptirq_delay_test.c | 145 +++++++++++++++++++++++---- 2 files changed, 129 insertions(+), 22 deletions(-)
kernel/trace/Kconfig +3 -3
@@ -750,9 +750,9 @@ config PREEMPTIRQ_DELAY_TEST configurable delay. The module busy waits for the duration of the critical section. - For example, the following invocation forces a one-time irq-disabled - critical section for 500us: - modprobe preemptirq_delay_test test_mode=irq delay=500000 + For example, the following invocation generates a burst of three + irq-disabled critical sections for 500us: + modprobe preemptirq_delay_test test_mode=irq delay=500 burst_size=3 If unsure, say N
kernel/trace/preemptirq_delay_test.c +126 -19
187 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 3/4 Add the latency-collector to tools
In reply to Viktor RosendahlThis is a tool that is intended to work around the fact that the preemptoff, irqsoff, and preemptirqsoff tracers only work in overwrite mode. The idea is to act randomly in such a way that we do not systematically lose any latencies, so that if enough testing is done, all latencies will be captured. If the same burst of latencies is repeated, then sooner or later we will have captured all the latencies.
The reason why it may be desirable to catch all latencies with a long test campaign is that for some organizations, it's necessary to test the kernel in the field and not practical for developers to work iteratively with field testers. Because of cost and project schedules it is not possible to start a new test campaign every time a latency problem has been fixed.
It uses inotify to detect changes to /sys/kernel/debug/tracing/trace. When a latency is detected, it will either sleep or print immediately, depending on a function that act as an unfair coin toss.
If immediate print is chosen, it means that we open /sys/kernel/debug/tracing/trace and thereby cause a blackout period that will hide any subsequent latencies.
If sleep is chosen, it means that we wait before opening /sys/kernel/debug/tracing/trace, by default for 1000 ms, to see if there is another latency during this period. If there is, then we will lose the previous latency. The coin will be tossed again with a different probability, and we will either print the new latency, or possibly a subsequent one.
The probability for the unfair coin toss is chosen so that there is equal probability to obtain any of the latencies in a burst. However, this assumes that we make an assumption of how many latencies there can be. By default the program assumes that there are no more than 2 latencies in a burst, the probability of immediate printout will be:
1/2 and 1
Thus, the probability of getting each of the two latencies will be 1/2.
If we ever find that there is more than one latency in a series, meaning that we reach the probability of 1, then the table will be expanded to:
1/3, 1/2, and 1
Thus, we assume that there are no more than three latencies and each with a probability of 1/3 of being captured. If the probability of 1 is reached in the new table, that is we see more than two closely occurring latencies, then the table will again be extended, and so on.
On my systems, it seems like this scheme works fairly well, as long as the latencies we trace are long enough, 100 us seems to be enough. This userspace program receive the inotify event at the end of a latency, and it has time until the end of the next latency to react, that is to open /sys/kernel/debug/tracing/trace. Thus, if we trace latencies that are >100 us, then we have at least 100 us to react.
Example output:
root@myhost:~# echo 100 > /sys/kernel/debug/tracing/tracing_thresh root@myhost:~# echo preemptirqsoff > /sys/kernel/debug/tracing/current_tracer root@myhost:~# latency-collector -rvv -a 11 & [1] 4915 root@myhost:~# Running with policy rr and priority 99. Using 3 print threads. /sys/kernel/debug/tracing/trace will be printed with random delay Start size of the probability table: 11 Print a message when prob. table table changes size: true Print a warning when an event has been lost: true Sleep time is: 1000 ms Initializing probability table to 11 Thread 4916 runs with policy rr and priority 99 Thread 4917 runs with policy rr and priority 99 Thread 4918 runs with policy rr and priority 99 Thread 4919 runs with policy rr and priority 99
root@myhost:~# modprobe preemptirq_delay_test delay=105 test_mode=alternate burst_size=10 2850.638308 Latency 1 printout skipped due to random delay 2850.638383 Latency 2 printout skipped due to random delay 2850.638497 Latency 3 immediate print
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
BEGIN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
alternat-4922 6d... 0us!: execute_preemptirqtest <-preemptirqtest_2 alternat-4922 6d... 106us : execute_preemptirqtest <-preemptirqtest_2 alternat-4922 6d... 106us : tracer_hardirqs_on <-preemptirqtest_2 alternat-4922 6d... 108us : <stack trace>
=> preemptirqtest_2
=> preemptirq_delay_run
=> kthread
=> ret_from_fork
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
END <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
In the example above, we randomly, happened to get the third latency in a burst burst of 10. If the experiment is repeated enough times, we will get all 10.
Sometimes, there will be lost latencies, so that we get:
3054.078294 Latency 44 printout skipped due to inotify loop ..or:
3211.957685 Latency 112 printout skipped due to print loop
In my experience, these are not frequent enough to be a problem.
Also, we do not really lose any latency in these cases, it's more like we get another latency than what was intended by the design.
--- tools/Makefile | 14 +- tools/trace/Makefile | 20 + tools/trace/latency-collector.c | 1190 +++++++++++++++++++++++++++++++ 3 files changed, 1218 insertions(+), 6 deletions(-) create mode 100644 tools/trace/Makefile create mode 100644 tools/trace/latency-collector.c
tools/Makefile +8 -6
@@ -29,6 +29,7 @@ help: @echo ' spi - spi tools' @echo ' objtool - an ELF object analysis tool' @echo ' tmon - thermal monitoring and tuning tool' + @echo ' trace - misc tracing tools' @echo ' turbostat - Intel CPU idle stats and freq reporting tool' @echo ' usb - USB testing tools' @echo ' virtio - vhost test module'
@@ -62,7 +63,7 @@ acpi: FORCE cpupower: FORCE $(call descend,power/$@) -cgroup firewire hv guest spi usb virtio vm bpf iio gpio objtool leds wmi pci firmware debugging: FORCE +cgroup firewire hv guest spi usb virtio vm bpf iio gpio objtool leds wmi pci firmware debugging trace: FORCE $(call descend,$@) liblockdep: FORCE
@@ -98,7 +99,7 @@ all: acpi cgroup cpupower gpio hv firewire liblockdep \ perf selftests spi turbostat usb \ virtio vm bpf x86_energy_perf_policy \ tmon freefall iio objtool kvm_stat wmi \ - pci debugging + pci debugging trace acpi_install: $(call descend,power/$(@:_install=),install)
@@ -106,7 +107,7 @@ acpi_install: cpupower_install: $(call descend,power/$(@:_install=),install) -cgroup_install firewire_install gpio_install hv_install iio_install perf_install spi_install usb_install virtio_install vm_install bpf_install objtool_install wmi_install pci_install debugging_install: +cgroup_install firewire_install gpio_install hv_install iio_install perf_install spi_install usb_install virtio_install vm_install bpf_install objtool_install wmi_install pci_install debugging_install trace_install: $(call descend,$(@:_install=),install) liblockdep_install:
@@ -132,7 +133,7 @@ install: acpi_install cgroup_install cpupower_install gpio_install \ perf_install selftests_install turbostat_install usb_install \ virtio_install vm_install bpf_install x86_energy_perf_policy_install \ tmon_install freefall_install objtool_install kvm_stat_install \ - wmi_install pci_install debugging_install + wmi_install pci_install debugging_install trace_install acpi_clean: $(call descend,power/acpi,clean)
@@ -140,7 +141,7 @@ acpi_clean: cpupower_clean: $(call descend,power/cpupower,clean) -cgroup_clean hv_clean firewire_clean spi_clean usb_clean virtio_clean vm_clean wmi_clean bpf_clean iio_clean gpio_clean objtool_clean leds_clean pci_clean firmware_clean debugging_clean: +cgroup_clean hv_clean firewire_clean spi_clean usb_clean virtio_clean vm_clean wmi_clean bpf_clean iio_clean gpio_clean objtool_clean leds_clean pci_clean firmware_clean debugging_clean trace_clean: $(call descend,$(@:_clean=),clean) liblockdep_clean:
@@ -178,6 +179,7 @@ clean: acpi_clean cgroup_clean cpupower_clean hv_clean firewire_clean \ perf_clean selftests_clean turbostat_clean spi_clean usb_clean virtio_clean \ vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \ freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean \ - gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean + gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean \ + debugging_clean trace_clean .PHONY: FORCE
tools/trace/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for vm tools +# +TARGETS = latency-collector +CFLAGS = -Wall -Wextra -g -O2 +LDFLAGS = -lpthread + +all: $(TARGETS) + +%: %.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + $(RM) latency-collector + +sbindir ?= /usr/sbin + +install: all + install -d $(DESTDIR)$(sbindir) + install -m 755 -p $(TARGETS) $(DESTDIR)$(sbindir)
tools/trace/latency-collector.c new file +1190 -0
1,190 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 4/4 ftrace: Add an option for tracing console latencies
In reply to Viktor RosendahlThis new option CONFIG_TRACE_CONSOLE_LATENCY will enable the latency tracers to trace the console latencies. Previously this has always been implicitely disabled. I guess this is because they are considered to be well known and unavoidable.
However, for some organizations it may nevertheless be desirable to trace them. Basically, we want to be able to tell that there are latencies in the system under test because someone has incorrectly enabled the serial console.
--- include/linux/irqflags.h | 13 +++++++++++++ kernel/printk/printk.c | 5 +++-- kernel/trace/Kconfig | 11 +++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-)
include/linux/irqflags.h +13 -0
@@ -73,6 +73,19 @@ do { \# define start_critical_timings () do { } while (0)#endif +#ifdef CONFIG_TRACE_CONSOLE_LATENCY + +#define console_stop_critical_timings () do {} while (0) +#define console_start_critical_timings () do {} while (0) + +#else /* !CONFIG_TRACE_CONSOLE_LATENCY */ + +/* don't trace print latency */ +#define console_stop_critical_timings () stop_critical_timings() +#define console_start_critical_timings () start_critical_timings() + +#endif /* !CONFIG_TRACE_CONSOLE_LATENCY */ +/* * Wrap the arch provided IRQ routines to provide appropriate checks. */
kernel/printk/printk.c +3 -2
@@ -2448,9 +2448,10 @@ void console_unlock(void)*/ console_lock_spinning_enable (); -stop_critical_timings ();/* don't trace print latency */ +/* don't trace print latency if it's disabled */ +console_stop_critical_timings ();call_console_drivers (ext_text, ext_len, text, len); -start_critical_timings (); +console_start_critical_timings ();if (console_lock_spinning_disable_and_check ()) {printk_safe_exit_irqrestore (flags);
kernel/trace/Kconfig +11 -0
@@ -244,6 +244,17 @@ config PREEMPT_TRACER modification of /sys/kernel/debug/tracing/trace through the inotify interface. + config TRACE_CONSOLE_LATENCY + bool "Do not turn off latency tracing for the console" + default n + depends on IRQSOFF_TRACER || PREEMPT_TRACER + help + Some of the console drivers will cause long unavoidable + latencies because they are slow and need to print immediately + in a serialized manner. Because of this their latencies are not + traced by default. This option will change behavior so that + they are traced. + config SCHED_TRACER bool "Scheduling Latency Tracer" select GENERIC_TRACER
Signature · 1 line
2.17.1
Steven Rostedt
maintainerRe: [PATCH v2 4/4] ftrace: Add an option for tracing console latencies
Quoted 10 lines · On Wed, 1 May 2019 22:36:50 +0200 Viktor Rosendahl…
This new option
CONFIG_TRACE_CONSOLE_LATENCYwill enable the latency tracers to trace the console latencies. Previously this has always been implicitely disabled. I guess this is because they are considered to be well known and unavoidable.However, for some organizations it may nevertheless be desirable to trace them. Basically, we want to be able to tell that there are latencies in the system under test because someone has incorrectly enabled the serial console.
I'd rather have this be a tracing option than a config one.
Quoted 15 lines
Signed-off-by Viktor Rosendahl <[email protected]>--- include/linux/irqflags.h | 13 +++++++++++++ kernel/printk/printk.c | 5 +++-- kernel/trace/Kconfig | 11 +++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-)include/linux/irqflags.h +0
-0@@ -73,6 +73,19 @@ do { \# define start_critical_timings () do { } while (0)#endif
include/linux/irqflags.h do { \ · line 85
Quoted 10 lines
include/linux/irqflags.h +10
-0+#ifdef CONFIG_TRACE_CONSOLE_LATENCY + +#define console_stop_critical_timings () do {} while (0) +#define console_start_critical_timings () do {} while (0) + +#else /* !CONFIG_TRACE_CONSOLE_LATENCY */ + +/* don't trace print latency */ +#define console_stop_critical_timings () stop_critical_timings() +#define console_start_critical_timings () start_critical_timings()
Instead of this being turned into a nop, don't have a kconfig option but instead have this call into the trace_irqsoff.c code, and depending on what the options are, it should stop it. Of course, this would need to be smart enough to pair it. Perhaps return the result of console_stop_critical_timings() and have that passed to console_start_critical_timings(), and only have start do something if stop did something. This way the option only needs to disable the stop part.
-- Steve
Quoted 48 lines
include/linux/irqflags.h +3
-0+ +#endif /* !CONFIG_TRACE_CONSOLE_LATENCY */ +/* * Wrap the arch provided IRQ routines to provide appropriate checks. */ kernel/printk/printk.c +0
-1@@ -2448,9 +2448,10 @@ void console_unlock(void)*/ console_lock_spinning_enable (); -stop_critical_timings ();/* don't trace print latency */
kernel/printk/printk.c +3
-1+/* don't trace print latency if it's disabled */ +console_stop_critical_timings ();call_console_drivers (ext_text, ext_len, text, len); -start_critical_timings (); +console_start_critical_timings ();if (console_lock_spinning_disable_and_check ()) {printk_safe_exit_irqrestore (flags);kernel/trace/Kconfig +0
-0@@ -244,6 +244,17 @@ config PREEMPT_TRACER modification of /sys/kernel/debug/tracing/trace throughthe inotify interface.
kernel/trace/Kconfig +7
-0+ config TRACE_CONSOLE_LATENCY + bool "Do not turn off latency tracing for the console" + default n + depends on IRQSOFF_TRACER || PREEMPT_TRACER + help + Some of the console drivers will cause long unavoidable + latencies because they are slow and need to printimmediately
kernel/trace/Kconfig +1
-0+ in a serialized manner. Because of this their latenciesare not
kernel/trace/Kconfig +3
-0+ traced by default. This option will change behavior so that + they are traced. + config SCHED_TRACER bool "Scheduling Latency Tracer" select GENERIC_TRACER
Re: [PATCH v2 4/4] ftrace: Add an option for tracing console latencies
Quoted 8 lines
Instead of this being turned into a nop, don't have a kconfig option but instead have this call into the
trace_irqsoff.ccode, and depending on what the options are, it should stop it. Of course, this would need to be smart enough to pair it. Perhaps return the result ofconsole_stop_critical_timings()and have that passed toconsole_start_critical_timings(), and only have start do something if stop did something. This way the option only needs to disable the stop part.
Something like this?
I have only compile tested it so far.
best regards,
Viktor
--- include/linux/irqflags.h | 17 +++++++++++++++++ kernel/printk/printk.c | 6 ++++-- kernel/trace/trace.h | 1 + kernel/trace/trace_irqsoff.c | 13 +++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-)
include/linux/irqflags.h +17 -0
@@ -68,9 +68,26 @@ do { \defined (CONFIG_PREEMPT_TRACER) extern void stop_critical_timings(void);extern void start_critical_timings (void ); +extern bool console_tracing_disabled (void ); + +#define console_stop_critical_timings (FLAG ) \ + do { \ + FLAG = console_tracing_disabled(); \ + if (FLAG) \ + stop_critical_timings(); \ + } while(0) + +#define console_start_critical_timings (FLAG ) \ + do { \ + if (FLAG) \ + start_critical_timings(); \ + } while(0) +#else # define stop_critical_timings () do { } while (0)# define start_critical_timings () do { } while (0) +# define console_stop_critical_timings (FLAG ) do { } while (0) +# define console_start_critical_timings (FLAG ) do { } while (0)#endif /*
kernel/printk/printk.c +4 -2
@@ -2348,6 +2348,7 @@ void console_unlock(void)static char ext_text[CONSOLE_EXT_LOG_MAX ];static char text[LOG_LINE_MAX +PREFIX_MAX ];unsigned long flags; +bool cflag;bool do_cond_resched, retry;if (console_suspended) {
@@ -2448,9 +2449,10 @@ void console_unlock(void)*/ console_lock_spinning_enable (); -stop_critical_timings ();/* don't trace print latency */ +/* don't trace print latency if it's disabled */ +console_stop_critical_timings (cflag);call_console_drivers (ext_text, ext_len, text, len); -start_critical_timings (); +console_start_critical_timings (cflag);if (console_lock_spinning_disable_and_check ()) {printk_safe_exit_irqrestore (flags);
kernel/trace/trace.h +1 -0
@@ -1253,6 +1253,7 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,C (PRINTK_MSGONLY ,"printk-msg-only" ), \C (CONTEXT_INFO ,"context-info" ),/* Print pid/cpu/time */ \C (LATENCY_FMT ,"latency-format" ), \ +C (CONSOLE_LATENCY ,"console-latency" ), \C (RECORD_CMD ,"record-cmd" ), \C (RECORD_TGID ,"record-tgid" ), \ C(OVERWRITE, "overwrite"), \
kernel/trace/trace_irqsoff.c +13 -0
@@ -490,6 +490,19 @@ void stop_critical_timings(void)EXPORT_SYMBOL_GPL (stop_critical_timings);NOKPROBE_SYMBOL (stop_critical_timings); +bool console_tracing_disabled (void ) +{ +struct trace_array *tr = irqsoff_trace; +int pc =preempt_count (); + +if (!preempt_trace (pc) && !irq_trace ()) +return false; + +return !(tr->trace_flags &CONSOLE_LATENCY ); +} + +EXPORT_SYMBOL_GPL (console_tracing_disabled); +#ifdef CONFIG_FUNCTION_TRACER static bool function_enabled;
Signature · 1 line
2.17.1
Steven Rostedt
maintainerRe: [PATCH v2 4/4] ftrace: Add an option for tracing console latencies
Quoted 9 lines · On Thu, 2 May 2019 20:37:31 +0200 Viktor Rosendahl…
Quoted 8 lines
Instead of this being turned into a nop, don't have a kconfig option but instead have this call into the
trace_irqsoff.ccode, and depending on what the options are, it should stop it. Of course, this would need to be smart enough to pair it. Perhaps return the result ofconsole_stop_critical_timings()and have that passed toconsole_start_critical_timings(), and only have start do something if stop did something. This way the option only needs to disable the stop part.
Quoted 4 lines
Something like this?
I have only compile tested it so far.
Yeah, something like that.
-- Steve