Some new features for the latency tracers
v4 cover Some new features for the latency tracers
Hello all,
Changes in v4:
- [PATCH 1/4]:
* Improved support for multiple active trace instances by storing entries that cannot be notified immediately in a linked list instead of a single pointer. With multiple instances this could happen.
* Register trace_maxlat_fsnotify_init() with late_initcall_sync(). Previously it was piggy backed on tracer_init_tracefs() and it got called more than once.
* Added some comments.
- [PATCH 3/4]:
* Some of the code was incorrectly added in the next patch. Improved commit message.
- [PATCH 4/4]:
* Some code from the previous patch was incorrectly included here.
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 work in overwrite mode.
[PATCH 1/4] This implement fs notification for tracing_max_latency. 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 console-latency to the trace options. This makes it possible to enable tracing of console latencies.
best regards,
Viktor
Viktor Rosendahl (4):
ftrace: Implement fs notification for tracing_max_latency
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 | 21 + include/trace/events/power.h | 40 + include/trace/events/sched.h | 40 + kernel/printk/printk.c | 6 +- kernel/sched/core.c | 2 + kernel/sched/idle.c | 2 + kernel/trace/Kconfig | 6 +- kernel/trace/preemptirq_delay_test.c | 145 ++- kernel/trace/trace.c | 168 +++- kernel/trace/trace.h | 13 + kernel/trace/trace_hwlat.c | 4 +- kernel/trace/trace_irqsoff.c | 12 + tools/Makefile | 14 +- tools/trace/Makefile | 20 + tools/trace/latency-collector.c | 1211 ++++++++++++++++++++++++++ 15 files changed, 1671 insertions(+), 33 deletions(-) create mode 100644 tools/trace/Makefile create mode 100644 tools/trace/latency-collector.c
Signature · 1 line
2.17.1
PATCH v4 1/4 ftrace: Implement fs notification for tracing_max_latency
This patch implements the feature that the tracing_max_latency file, e.g. /sys/kernel/debug/tracing/tracing_max_latency will receive notifications through the fsnotify framework when a new latency is available.
One particularly interesting use of this facility is when enabling threshold tracing, through /sys/kernel/debug/tracing/tracing_thresh, together with the preempt/irqsoff tracers. 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.
This facility works with the hwlat, preempt/irqsoff, and wakeup tracers.
This patch also add four new tracepoints, for entering/exiting __schedule() and do_idle(). Those tracepoints are needed to disable and enable the notificaton facility because we cannot wake up the workqueue from these critical sections without risking a deadlock. Similar problems would also arise if we try to schedule a tasklet, raise a softirq, or wake up a kernel thread.
If a notification event would happen in the forbidden sections, we schedule the fsnotify work as soon as we have exited do_idle()/__schedule().
--- include/trace/events/power.h | 40 +++++++++ include/trace/events/sched.h | 40 +++++++++ kernel/sched/core.c | 2 + kernel/sched/idle.c | 2 + kernel/trace/trace.c | 168 ++++++++++++++++++++++++++++++++++- kernel/trace/trace.h | 12 +++ kernel/trace/trace_hwlat.c | 4 +- 7 files changed, 265 insertions(+), 3 deletions(-)
include/trace/events/power.h +40 -0
@@ -40,6 +40,46 @@ DEFINE_EVENT(cpu, cpu_idle,TP_ARGS (state, cpu_id) ); +/* +* Tracepoint for entering do_idle(): +*/ +TRACE_EVENT (do_idle_enter, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + +/* +* Tracepoint for exiting do_idle(): +*/ +TRACE_EVENT (do_idle_exit, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + TRACE_EVENT(powernv_throttle, TP_PROTO(int chip_id, const char *reason, int pmax),
include/trace/events/sched.h +40 -0
@@ -183,6 +183,46 @@ TRACE_EVENT(sched_switch, __entry->next_comm, __entry->next_pid, __entry->next_prio) ); +/* +* Tracepoint for entering __schedule(): +*/ +TRACE_EVENT (sched_schedule_enter, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + +/* +* Tracepoint for exiting __schedule(): +*/ +TRACE_EVENT (sched_schedule_exit, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); +/* * Tracepoint for a task being migrated: */
kernel/sched/core.c +2 -0
@@ -3374,6 +3374,7 @@ static void __sched notrace __schedule(bool preempt)int cpu; cpu =smp_processor_id (); +trace_sched_schedule_enter (cpu); rq =cpu_rq (cpu); prev = rq->curr;
@@ -3448,6 +3449,7 @@ static void __sched notrace __schedule(bool preempt) }balance_callback (rq); +trace_sched_schedule_exit (cpu); }void __noreturn do_task_dead (void )
kernel/sched/idle.c +2 -0
@@ -224,6 +224,7 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {int cpu =smp_processor_id (); +trace_do_idle_enter (cpu);/* * If the arch has a polling bit, we maintain an invariant: *
@@ -287,6 +288,7 @@ static void do_idle(void)if (unlikely (klp_patch_pending (current)))klp_update_patch_state (current); +trace_do_idle_exit (cpu); }bool cpu_in_idle (unsigned long pc)
kernel/trace/trace.c +166 -2
192 lines not on this page · raw message
kernel/trace/trace.h +12 -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 */
@@ -265,6 +266,12 @@ struct trace_array {#endif #if defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )unsigned long max_latency; +#ifdef CONFIG_FSNOTIFY +struct dentry *d_max_latency; +struct work_struct fsnotify_work; +atomic_t notify_pending; +struct llist_node notify_ll; +#endif #endif struct trace_pid_list __rcu *filtered_pids;/*
@@ -786,6 +793,11 @@ void update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu);#endif /* CONFIG_TRACER_MAX_TRACE */ +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) +void trace_maxlat_fsnotify (struct trace_array *tr); +#endif +#ifdef CONFIG_STACKTRACE void __trace_stack (struct trace_array *tr,unsigned long flags,int skip,int pc);
kernel/trace/trace_hwlat.c +3 -1
@@ -254,8 +254,10 @@ static int get_sample(void)trace_hwlat_sample (&s);/* Keep a running maximum ever recorded hardware latency */ -if (sample > tr->max_latency) +if (sample > tr->max_latency) { tr->max_latency = sample; +trace_maxlat_fsnotify (tr); + } } out:
Signature · 1 line
2.17.1
Steven Rostedt
maintainerRe: [PATCH v4 1/4] ftrace: Implement fs notification for tracing_max_latency
[ Added Peter and Rafael ]
Quoted 16 lines · On Fri, 17 May 2019 22:34:27 +0200 Viktor Rosendahl…
This patch implements the feature that the
tracing_max_latencyfile, e.g./sys/kernel/debug/tracing/tracing_max_latencywill receive notifications through the fsnotify framework when a new latency is available.One particularly interesting use of this facility is when enabling threshold tracing, through
/sys/kernel/debug/tracing/tracing_thresh, together with the preempt/irqsoff tracers. 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.This facility works with the hwlat, preempt/irqsoff, and wakeup tracers.
Quoted 6 lines
This patch also add four new tracepoints, for entering/exiting
__schedule()anddo_idle(). Those tracepoints are needed to disable and enable the notificaton facility because we cannot wake up the workqueue from these critical sections without risking a deadlock. Similar problems would also arise if we try to schedule a tasklet, raise a softirq, or wake up a kernel thread.
Note, you need to add the scheduling and power management maintainers when adding trace events to their code.
As these trace events become visible to user space, and you only need a callback to disable fsnotify, it may be better to just have a hard coded call to disable fsnotify (and have it be a nop when not configured). We can use a static branch if you want to keep the overhead down when not enabled, and enable the static branch when you start these latency tracers.
-- Steve
Quoted 411 lines
If a notification event would happen in the forbidden sections, we schedule the fsnotify work as soon as we have exited do_idle()/__schedule().
Signed-off-by Viktor Rosendahl <[email protected]>--- include/trace/events/power.h | 40 +++++++++ include/trace/events/sched.h | 40 +++++++++ kernel/sched/core.c | 2 + kernel/sched/idle.c | 2 + kernel/trace/trace.c | 168 ++++++++++++++++++++++++++++++++++- kernel/trace/trace.h | 12 +++ kernel/trace/trace_hwlat.c | 4 +- 7 files changed, 265 insertions(+), 3 deletions(-)include/trace/events/power.h +40
-0@@ -40,6 +40,46 @@ DEFINE_EVENT(cpu, cpu_idle,TP_ARGS (state, cpu_id) ); +/* +* Tracepoint for entering do_idle(): +*/ +TRACE_EVENT (do_idle_enter, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + +/* +* Tracepoint for exiting do_idle(): +*/ +TRACE_EVENT (do_idle_exit, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + TRACE_EVENT(powernv_throttle, TP_PROTO(int chip_id, const char *reason, int pmax),include/trace/events/sched.h +40
-0@@ -183,6 +183,46 @@ TRACE_EVENT(sched_switch, __entry->next_comm, __entry->next_pid, __entry->next_prio) ); +/* +* Tracepoint for entering __schedule(): +*/ +TRACE_EVENT (sched_schedule_enter, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + +/* +* Tracepoint for exiting __schedule(): +*/ +TRACE_EVENT (sched_schedule_exit, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); +/* * Tracepoint for a task being migrated: */ kernel/sched/core.c +2
-0@@ -3374,6 +3374,7 @@ static void __sched notrace __schedule(bool preempt)int cpu; cpu =smp_processor_id (); +trace_sched_schedule_enter (cpu); rq =cpu_rq (cpu); prev = rq->curr;@@ -3448,6 +3449,7 @@ static void __sched notrace __schedule(bool preempt) }balance_callback (rq); +trace_sched_schedule_exit (cpu); }void __noreturn do_task_dead (void )kernel/sched/idle.c +2
-0@@ -224,6 +224,7 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {int cpu =smp_processor_id (); +trace_do_idle_enter (cpu);/* * If the arch has a polling bit, we maintain an invariant: * @@ -287,6 +288,7 @@ static void do_idle(void)if (unlikely (klp_patch_pending (current)))klp_update_patch_state (current); +trace_do_idle_exit (cpu); }bool cpu_in_idle (unsigned long pc)kernel/trace/trace.c +166
-2@@ -44,6 +44,10 @@#include <linux/trace.h> #include <linux/sched/clock.h> #include <linux/sched/rt.h> +#include <linux/fsnotify.h> +#include <linux/workqueue.h> +#include <trace/events/power.h> +#include <trace/events/sched.h> #include "trace.h" #include "trace_output.h" @@ -1481,6 +1485,166 @@ static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)unsigned long __read_mostly tracing_thresh; +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) + +static const struct file_operations tracing_max_lat_fops; +static struct workqueue_struct *fsnotify_wq; +static DEFINE_PER_CPU(unsigned int, notify_disabled) = 1; +static DEFINE_PER_CPU(struct llist_head, notify_list); + +static void trace_fsnotify_workfn (struct work_struct *work) +{ + struct trace_array *tr = container_of(work, struct trace_array, + fsnotify_work); +fsnotify (tr->d_max_latency->d_inode,FS_MODIFY , + tr->d_max_latency->d_inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); +} + +static void trace_create_maxlat_file (struct trace_array *tr, +struct dentry *d_tracer) +{ +INIT_WORK (&tr->fsnotify_work, trace_fsnotify_workfn); +atomic_set (&tr->notify_pending,0 ); + tr->d_max_latency =trace_create_file ("tracing_max_latency" ,0644 , + d_tracer, &tr->max_latency, + &tracing_max_lat_fops); +} + +/* +* Disable fsnotify while in scheduler and idle code. Trying wake anything up +* from there, such as calling queue_work() is prone to deadlock. +*/ +static inline void notrace trace_disable_fsnotify(void) +{ +int cpu; +unsigned long flags; + +raw_local_irq_save (flags); + cpu =smp_processor_id (); +per_cpu (notify_disabled, cpu) =1 ; +raw_local_irq_restore (flags); +} + +static inline void notrace trace_enable_fsnotify(void) +{ +int cpu; +struct trace_array *tr; +struct llist_head *list; +struct llist_node *node; +unsigned long flags; + +raw_local_irq_save (flags); + cpu =smp_processor_id (); +per_cpu (notify_disabled, cpu) =0 ; +raw_local_irq_restore (flags); + +if (!per_cpu (notify_disabled, cpu)) { + list = &per_cpu (notify_list, cpu); +for (node =llist_del_first (list); node !=NULL ; + node =llist_del_first (list)) { + tr = llist_entry(node, struct trace_array, notify_ll); +atomic_set (&tr->notify_pending,0 ); +queue_work (fsnotify_wq, &tr->fsnotify_work); + } + } +} + +static void notrace probe_sched_enter(void *nihil, int cpu) +{ +trace_disable_fsnotify (); +} + +static void notrace probe_sched_exit(void *nihil, int cpu) +{ +trace_enable_fsnotify (); +} + +__init static int trace_maxlat_fsnotify_init (void ) +{ +int ret; + + ret =register_trace_sched_schedule_enter (probe_sched_enter,NULL ); +if (ret) { +pr_info ("tracing_max_latency: Could not activate tracepoint probe to sched_schedule_enter\n" ); +return ret; + } + + ret =register_trace_sched_schedule_exit (probe_sched_exit,NULL ); +if (ret) { +pr_info ("tracing_max_latency: Could not activate tracepoint probe to sched_schedule_exit\n" ); + goto fail_deprobe_sched_enter; + } + + ret =register_trace_do_idle_enter (probe_sched_enter,NULL ); +if (ret) { +pr_info ("tracing_max_latency: Could not activate tracepoint probe to do_idle_enter\n" ); + goto fail_deprobe_sched_exit; + } + + ret =register_trace_do_idle_exit (probe_sched_exit,NULL ); +if (ret) { +pr_info ("tracing_max_latency: Could not activate tracepoint probe to do_idle_exit\n" ); + goto fail_deprobe_idle_enter; + } + + fsnotify_wq =alloc_workqueue ("tr_max_lat_wq" , +WQ_UNBOUND |WQ_HIGHPRI ,0 ); +if (fsnotify_wq) +return 0 ; + + ret = -ENOMEM ; + +pr_err ("Unable to allocate tr_max_lat_wq\n" ); + +unregister_trace_do_idle_exit (probe_sched_exit,NULL ); + +fail_deprobe_idle_enter: +unregister_trace_do_idle_enter (probe_sched_enter,NULL ); + +fail_deprobe_sched_exit: +unregister_trace_sched_schedule_exit (probe_sched_exit,NULL ); + +fail_deprobe_sched_enter: +unregister_trace_sched_schedule_enter (probe_sched_enter,NULL ); +return ret; +} + +late_initcall_sync (trace_maxlat_fsnotify_init); + +void trace_maxlat_fsnotify (struct trace_array *tr) +{ +int cpu; + +if (!fsnotify_wq) +return ; + + cpu =smp_processor_id (); +if (!per_cpu (notify_disabled, cpu)) +queue_work (fsnotify_wq, &tr->fsnotify_work); +else { +/* +* notify_pending prevents us from adding the same entry to +* more than one notify_list. It will get queued in +* trace_enable_fsnotify() +*/ +if (!atomic_xchg (&tr->notify_pending,1 )) +llist_add (&tr->notify_ll, &per_cpu (notify_list, cpu)); + } +} + +/* +* (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \ +* defined(CONFIG_FSNOTIFY) +*/ +#else + +#define trace_create_maxlat_file (tr, d_tracer) \ + trace_create_file("tracing_max_latency", 0644, d_tracer, \ + &tr->max_latency, &tracing_max_lat_fops) + +#endif +#ifdef CONFIG_TRACER_MAX_TRACE /* * Copy the new maximum trace into the separate maximum-trace @@ -1519,6 +1683,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)/* record this tasks comm */ tracing_record_cmdline (tsk); +trace_maxlat_fsnotify (tr); }/** @@ -8533,8 +8698,7 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)create_trace_options_dir (tr);#if defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER ) -trace_create_file ("tracing_max_latency" ,0644 , d_tracer, - &tr->max_latency, &tracing_max_lat_fops); +trace_create_maxlat_file (tr, d_tracer);#endif if (ftrace_create_function_files (tr, d_tracer))kernel/trace/trace.h +12
-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 */ @@ -265,6 +266,12 @@ struct trace_array {#endif #if defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )unsigned long max_latency; +#ifdef CONFIG_FSNOTIFY +struct dentry *d_max_latency; +struct work_struct fsnotify_work; +atomic_t notify_pending; +struct llist_node notify_ll; +#endif #endif struct trace_pid_list __rcu *filtered_pids;/* @@ -786,6 +793,11 @@ void update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu);#endif /* CONFIG_TRACER_MAX_TRACE */ +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) +void trace_maxlat_fsnotify (struct trace_array *tr); +#endif +#ifdef CONFIG_STACKTRACE void __trace_stack (struct trace_array *tr,unsigned long flags,int skip,int pc);kernel/trace/trace_hwlat.c +3
-1@@ -254,8 +254,10 @@ static int get_sample(void)trace_hwlat_sample (&s);/* Keep a running maximum ever recorded hardware latency */ -if (sample > tr->max_latency) +if (sample > tr->max_latency) { tr->max_latency = sample; +trace_maxlat_fsnotify (tr); + } } out:
Re: [PATCH v4 1/4] ftrace: Implement fs notification for tracing_max_latency
On 5/21/19 6:01 PM, Steven Rostedt wrote:>
Quoted 4 lines
Note, you need to add the scheduling and power management maintainers when adding trace events to their code.
My bad.
Quoted 4 lines
As these trace events become visible to user space, and you only need a callback to disable fsnotify, it may be better to just have a hard coded call to disable fsnotify (and have it be a nop when not configured).
My thinking was that it would not be defensible to clutter the idle and scheduling code with calls to slightly obscure tracing code and that perhaps some users would like to have these tracepoints for perf/ftrace but I don't insist on it.
Below is a patch with hard coded calls.
Quoted 4 lines
We can use a static branch if you want to keep the overhead down when not enabled, and enable the static branch when you start these latency tracers.
I noticed that it's possible to slightly simplify the enable/disble functions, so I guess that this would not be necessary, especially since one would need to handle the case when some CPUs are in the forbidden sections when the tracers are enabled.
I can try to add the static branch if you want it though.
best regards,
Viktor
--- include/linux/ftrace.h | 13 +++++ kernel/sched/core.c | 2 + kernel/sched/idle.c | 2 + kernel/sched/sched.h | 1 + kernel/trace/trace.c | 111 ++++++++++++++++++++++++++++++++++++- kernel/trace/trace.h | 12 ++++ kernel/trace/trace_hwlat.c | 4 +- 7 files changed, 142 insertions(+), 3 deletions(-)
include/linux/ftrace.h +13 -0
@@ -907,4 +907,17 @@ unsigned long arch_syscall_addr(int nr);#endif /* CONFIG_FTRACE_SYSCALLS */ +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) + +void trace_disable_fsnotify (void ); +void trace_enable_fsnotify (void ); + +#else + +#define trace_disable_fsnotify () do { } while (0) +#define trace_enable_fsnotify () do { } while (0) + +#endif +#endif /* _LINUX_FTRACE_H */
kernel/sched/core.c +2 -0
@@ -3374,6 +3374,7 @@ static void __sched notrace __schedule(bool preempt)struct rq *rq;int cpu; +trace_disable_fsnotify (); cpu =smp_processor_id (); rq =cpu_rq (cpu); prev = rq->curr;
@@ -3449,6 +3450,7 @@ static void __sched notrace __schedule(bool preempt) }balance_callback (rq); +trace_enable_fsnotify (); }void __noreturn do_task_dead (void )
kernel/sched/idle.c +2 -0
@@ -225,6 +225,7 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {int cpu =smp_processor_id (); +trace_disable_fsnotify ();/* * If the arch has a polling bit, we maintain an invariant: *
@@ -284,6 +285,7 @@ static void do_idle(void)smp_mb__after_atomic ();sched_ttwu_pending (); +/* schedule_idle() will call trace_enable_fsnotify() */ schedule_idle ();if (unlikely (klp_patch_pending (current)))
kernel/sched/sched.h +1 -0
@@ -46,6 +46,7 @@#include <linux/debugfs.h> #include <linux/delayacct.h> #include <linux/energy_model.h> +#include <linux/ftrace.h> #include <linux/init_task.h> #include <linux/kprobes.h> #include <linux/kthread.h>
kernel/trace/trace.c +109 -2
135 lines not on this page · raw message
kernel/trace/trace.h +12 -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 */
@@ -265,6 +266,12 @@ struct trace_array {#endif #if defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )unsigned long max_latency; +#ifdef CONFIG_FSNOTIFY +struct dentry *d_max_latency; +struct work_struct fsnotify_work; +atomic_t notify_pending; +struct llist_node notify_ll; +#endif #endif struct trace_pid_list __rcu *filtered_pids;/*
@@ -786,6 +793,11 @@ void update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu);#endif /* CONFIG_TRACER_MAX_TRACE */ +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) +void trace_maxlat_fsnotify (struct trace_array *tr); +#endif +#ifdef CONFIG_STACKTRACE void __trace_stack (struct trace_array *tr,unsigned long flags,int skip,int pc);
kernel/trace/trace_hwlat.c +3 -1
@@ -254,8 +254,10 @@ static int get_sample(void)trace_hwlat_sample (&s);/* Keep a running maximum ever recorded hardware latency */ -if (sample > tr->max_latency) +if (sample > tr->max_latency) { tr->max_latency = sample; +trace_maxlat_fsnotify (tr); + } } out:
Signature · 1 line
2.17.1
Steven Rostedt
maintainerRe: [PATCH v4 1/4] ftrace: Implement fs notification for tracing_max_latency
Quoted 2 lines · On Wed, 22 May 2019 02:30:14 +0200 Viktor Rosendahl…
I can try to add the static branch if you want it though.
Yes, it would need a static branch to prevent overhead.
Quoted 41 lines
best regards,
Viktor
--- include/linux/ftrace.h | 13 +++++ kernel/sched/core.c | 2 + kernel/sched/idle.c | 2 + kernel/sched/sched.h | 1 + kernel/trace/trace.c | 111 ++++++++++++++++++++++++++++++++++++- kernel/trace/trace.h | 12 ++++ kernel/trace/trace_hwlat.c | 4 +- 7 files changed, 142 insertions(+), 3 deletions(-)include/linux/ftrace.h +13
-0@@ -907,4 +907,17 @@ unsigned long arch_syscall_addr(int nr);#endif /* CONFIG_FTRACE_SYSCALLS */ +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) + +void trace_disable_fsnotify (void ); +void trace_enable_fsnotify (void ); + +#else + +#define trace_disable_fsnotify () do { } while (0) +#define trace_enable_fsnotify () do { } while (0) + +#endif +#endif /* _LINUX_FTRACE_H */ kernel/sched/core.c +0
-0
kernel/sched/core.c static void __sched notrace __schedule(bool preempt) · line 3377
Quoted 4 lines
kernel/sched/core.c +1
-0@@ -3374,6 +3374,7 @@ static void __sched notrace __schedule(bool preempt)struct rq *rq;int cpu; +trace_disable_fsnotify ();
Also, don't use "trace_*" names, I'm trying to reserve them for tracepoints only.
latency_fsnotify_disable();
perhaps?
kernel/sched/core.c static void __sched notrace __schedule(bool preempt) · line 3453
Quoted 8 lines
kernel/sched/core.c +1
-0cpu =smp_processor_id (); rq =cpu_rq (cpu); prev = rq->curr;@@ -3449,6 +3450,7 @@ static void __sched notrace __schedule(bool preempt) }balance_callback (rq); +trace_enable_fsnotify ();
latency_fsnotify_enable();
Quoted 31 lines
kernel/sched/core.c +0
-0}void __noreturn do_task_dead (void )kernel/sched/sched.h +1
-0@@ -46,6 +46,7 @@#include <linux/debugfs.h> #include <linux/delayacct.h> #include <linux/energy_model.h> +#include <linux/ftrace.h> #include <linux/init_task.h> #include <linux/kprobes.h> #include <linux/kthread.h> kernel/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 ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) · line 1491
Quoted 9 lines
kernel/trace/trace.c +6
-0@@ -1481,6 +1483,111 @@ static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)unsigned long __read_mostly tracing_thresh; +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) + +static const struct file_operations tracing_max_lat_fops; +static struct workqueue_struct *fsnotify_wq; +static DEFINE_PER_CPU(atomic_t, notify_disabled) = ATOMIC_INIT(0);
per cpu atomics is a bit of an overkill. Why the atomic if they are only used per cpu?
Just make them per cpu, and use things like this_cpu_inc/dec();
Quoted 21 lines
kernel/trace/trace.c +21
-0+static DEFINE_PER_CPU(struct llist_head, notify_list); + +static void trace_fsnotify_workfn (struct work_struct *work) +{ + struct trace_array *tr = container_of(work, struct trace_array, + fsnotify_work); +fsnotify (tr->d_max_latency->d_inode,FS_MODIFY , + tr->d_max_latency->d_inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); +} + +static void trace_create_maxlat_file (struct trace_array *tr, +struct dentry *d_tracer) +{ +INIT_WORK (&tr->fsnotify_work, trace_fsnotify_workfn); +atomic_set (&tr->notify_pending,0 ); + tr->d_max_latency =trace_create_file ("tracing_max_latency" ,0644 , + d_tracer, &tr->max_latency, + &tracing_max_lat_fops); +} + +/*
kernel/trace/trace.c static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) · line 1522
Quoted 10 lines
kernel/trace/trace.c +10
-0+* Disable fsnotify while in scheduler and idle code. Trying wake anything up +* from there, such as calling queue_work() is prone to deadlock. +*/ +void trace_disable_fsnotify (void ) +{ +int cpu; + + cpu =smp_processor_id (); +atomic_set (&per_cpu (notify_disabled, cpu),1 ); +}
This should be a static inline function:
static inline void latency_fsnotify_disable(void)
{
this_cpu_inc(latency_trace_notify_disable);
}kernel/trace/trace.c static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) · line 1525
Quoted 3 lines
kernel/trace/trace.c +3
-0+EXPORT_SYMBOL (trace_disable_fsnotify); + +void trace_enable_fsnotify (void )
Also this needs to be split as a static inline and a function call.
static inline void latency_fsnotify_enable(void)
{
this_cpu_dec(latency_trace_notify_disable);
if (static_key_false(&latency_notify_key))
lantency_fsnotify_process();
}Have the static_key enabled by the latency tracers that modify the file.
In this file:
void latency_fsontify_process(void)
{
kernel/trace/trace.c static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) · line 1535
Quoted 10 lines
kernel/trace/trace.c +10
-0+{ +int cpu; +struct trace_array *tr; +struct llist_head *list; +struct llist_node *node; + + cpu =smp_processor_id (); +atomic_set (&per_cpu (notify_disabled, cpu),0 ); + + list = &per_cpu (notify_list, cpu);
list = &this_cpu_read(notify_list);
-- Steve
Quoted 129 lines
kernel/trace/trace.c +57
-2+for (node =llist_del_first (list); node !=NULL ; + node =llist_del_first (list)) { + tr = llist_entry(node, struct trace_array, notify_ll); +atomic_set (&tr->notify_pending,0 ); +queue_work (fsnotify_wq, &tr->fsnotify_work); + } +} +EXPORT_SYMBOL (trace_enable_fsnotify); + +__init static int trace_maxlat_fsnotify_init (void ) +{ + fsnotify_wq =alloc_workqueue ("tr_max_lat_wq" , +WQ_UNBOUND |WQ_HIGHPRI ,0 ); +if (!fsnotify_wq) { +pr_err ("Unable to allocate tr_max_lat_wq\n" ); +return -ENOMEM ; + } +return 0 ; +} + +late_initcall_sync (trace_maxlat_fsnotify_init); + +void trace_maxlat_fsnotify (struct trace_array *tr) +{ +int cpu; + +if (!fsnotify_wq) +return ; + + cpu =smp_processor_id (); +if (!atomic_read (&per_cpu (notify_disabled, cpu))) +queue_work (fsnotify_wq, &tr->fsnotify_work); +else { +/* +* notify_pending prevents us from adding the same entry to +* more than one notify_list. It will get queued in +* trace_enable_fsnotify() +*/ +if (!atomic_xchg (&tr->notify_pending,1 )) +llist_add (&tr->notify_ll, &per_cpu (notify_list, cpu)); + } +} + +/* +* (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \ +* defined(CONFIG_FSNOTIFY) +*/ +#else + +#define trace_create_maxlat_file (tr, d_tracer) \ + trace_create_file("tracing_max_latency", 0644, d_tracer, \ + &tr->max_latency, &tracing_max_lat_fops) + +#endif +#ifdef CONFIG_TRACER_MAX_TRACE /* * Copy the new maximum trace into the separate maximum-trace @@ -1519,6 +1626,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)/* record this tasks comm */ tracing_record_cmdline (tsk); +trace_maxlat_fsnotify (tr); }/** @@ -8533,8 +8641,7 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)create_trace_options_dir (tr);#if defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER ) -trace_create_file ("tracing_max_latency" ,0644 , d_tracer, - &tr->max_latency, &tracing_max_lat_fops); +trace_create_maxlat_file (tr, d_tracer);#endif if (ftrace_create_function_files (tr, d_tracer))kernel/trace/trace.h +12
-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 */ @@ -265,6 +266,12 @@ struct trace_array {#endif #if defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )unsigned long max_latency; +#ifdef CONFIG_FSNOTIFY +struct dentry *d_max_latency; +struct work_struct fsnotify_work; +atomic_t notify_pending; +struct llist_node notify_ll; +#endif #endif struct trace_pid_list __rcu *filtered_pids;/* @@ -786,6 +793,11 @@ void update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu);#endif /* CONFIG_TRACER_MAX_TRACE */ +#if (defined(CONFIG_TRACER_MAX_TRACE ) || defined(CONFIG_HWLAT_TRACER )) && \ + defined(CONFIG_FSNOTIFY ) +void trace_maxlat_fsnotify (struct trace_array *tr); +#endif +#ifdef CONFIG_STACKTRACE void __trace_stack (struct trace_array *tr,unsigned long flags,int skip,int pc);kernel/trace/trace_hwlat.c +3
-1@@ -254,8 +254,10 @@ static int get_sample(void)trace_hwlat_sample (&s);/* Keep a running maximum ever recorded hardware latency */ -if (sample > tr->max_latency) +if (sample > tr->max_latency) { tr->max_latency = sample; +trace_maxlat_fsnotify (tr); + } } out:
Peter Zijlstra
maintainerRe: [PATCH v4 1/4] ftrace: Implement fs notification for tracing_max_latency
In reply to Viktor RosendahlQuoted 33 lines · Viktor Rosendahl
kernel/sched/core.c +2
-0@@ -3374,6 +3374,7 @@ static void __sched notrace __schedule(bool preempt)struct rq *rq;int cpu; +trace_disable_fsnotify (); cpu =smp_processor_id (); rq =cpu_rq (cpu); prev = rq->curr;@@ -3449,6 +3450,7 @@ static void __sched notrace __schedule(bool preempt) }balance_callback (rq); +trace_enable_fsnotify (); }void __noreturn do_task_dead (void )kernel/sched/idle.c +1
-0@@ -225,6 +225,7 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {int cpu =smp_processor_id (); +trace_disable_fsnotify ();/* * If the arch has a polling bit, we maintain an invariant: *
kernel/sched/idle.c static void do_idle(void) · line 291
Quoted 7 lines
kernel/sched/idle.c +1
-0@@ -284,6 +285,7 @@ static void do_idle(void)smp_mb__after_atomic ();sched_ttwu_pending (); +/* schedule_idle() will call trace_enable_fsnotify() */ schedule_idle ();if (unlikely (klp_patch_pending (current)))
I still hate this.. why are we doing this? We already have this stop_critical_timings() nonsense and are now adding more gunk.
kernel/sched/idle.c
Quoted 7 lines
kernel/sched/idle.c +4
-0+static DEFINE_PER_CPU(atomic_t, notify_disabled) = ATOMIC_INIT(0); +atomic_set (&per_cpu (notify_disabled, cpu),1 ); +atomic_set (&per_cpu (notify_disabled, cpu),0 ); +if (!atomic_read (&per_cpu (notify_disabled, cpu)))
That's just wrong on so many levels..
Steven Rostedt
maintainerRe: [PATCH v4 1/4] ftrace: Implement fs notification for tracing_max_latency
Quoted 11 lines · On Wed, 22 May 2019 16:35:45 +0200 Peter Zijlstra <[email protected]> wrote:
kernel/sched/idle.c static void do_idle(void) · line 291
Quoted 8 lines
kernel/sched/idle.c +1
-0@@ -284,6 +285,7 @@ static void do_idle(void)smp_mb__after_atomic ();sched_ttwu_pending (); +/* schedule_idle() will call trace_enable_fsnotify() */ schedule_idle ();if (unlikely (klp_patch_pending (current)))I still hate this.. why are we doing this? We already have this
stop_critical_timings()nonsense and are now adding more gunk.
I was thinking that this can possibly be added in the stop_critical_timings() as that is probably where this is needed anyway.
-- Steve
Re: [PATCH v4 1/4] ftrace: Implement fs notification for tracing_max_latency
In reply to Steven RostedtQuoted 2 lines · Steven Rostedt
[ Added Peter and Rafael ]
Thanks Steve,
Quoted 88 lines
Quoted 85 lines · On Fri, 17 May 2019 22:34:27 +0200 Viktor Rosendahl…
include/trace/events/sched.h +40
-0@@ -183,6 +183,46 @@ TRACE_EVENT(sched_switch, __entry->next_comm, __entry->next_pid, __entry->next_prio) ); +/* +* Tracepoint for entering __schedule(): +*/ +TRACE_EVENT (sched_schedule_enter, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); + +/* +* Tracepoint for exiting __schedule(): +*/ +TRACE_EVENT (sched_schedule_exit, + + TP_PROTO(int cpu), + +TP_ARGS (cpu), + +TP_STRUCT__entry ( +__field (int, cpu) + ), + +TP_fast_assign ( + __entry->cpu = cpu; + ), + +TP_printk ("cpu=%d" , __entry->cpu) +); +/* * Tracepoint for a task being migrated: */ kernel/sched/core.c +2
-0@@ -3374,6 +3374,7 @@ static void __sched notrace __schedule(bool preempt)int cpu; cpu =smp_processor_id (); +trace_sched_schedule_enter (cpu); rq =cpu_rq (cpu); prev = rq->curr;@@ -3448,6 +3449,7 @@ static void __sched notrace __schedule(bool preempt) }balance_callback (rq); +trace_sched_schedule_exit (cpu); }void __noreturn do_task_dead (void )kernel/sched/idle.c +1
-0@@ -224,6 +224,7 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {int cpu =smp_processor_id (); +trace_do_idle_enter (cpu);/* * If the arch has a polling bit, we maintain an invariant: * @@ -287,6 +288,7 @@ static void do_idle(void)
Quoted 6 lines
kernel/sched/idle.c static void do_idle(void) · line 294
Quoted 6 lines
kernel/sched/idle.c +1
-0if (unlikely (klp_patch_pending (current)))klp_update_patch_state (current); +trace_do_idle_exit (cpu); }bool cpu_in_idle (unsigned long pc)
NAK!
PATCH v4 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
@@ -740,9 +740,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 v4 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.
It also works with the wakeup_dl, wakeup_rt, wakeup, and hwlat tracers. However, in that case it is probably not useful to use the random sleep functionality.
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, 300 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 >300 us, then we have at least 300 us to react.
The minimum latency will of course not be 300 us on all systems, it will depend on the hardware, kernel version, workload and configuration.
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 | 1211 +++++++++++++++++++++++++++++++ 3 files changed, 1239 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 +1211 -0
1,211 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 4/4 ftrace: Add an option for tracing console latencies
In reply to Viktor RosendahlThis new trace option "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 | 21 +++++++++++++++++++++ kernel/printk/printk.c | 6 ++++-- kernel/trace/trace.h | 1 + kernel/trace/trace_irqsoff.c | 12 ++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-)
include/linux/irqflags.h +21 -0
@@ -68,9 +68,30 @@ 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 { \ + typecheck(bool, flag); \ + flag = console_tracing_disabled(); \ + if (flag) \ + stop_critical_timings(); \ + } while (0) + +# define console_start_critical_timings (flag) \ + do { \ + typecheck(bool, flag); \ + 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 { typecheck(bool, flag); } while (0) +# define console_start_critical_timings (flag) \ + do { typecheck(bool, flag); } while (0)#endif /*
kernel/printk/printk.c +4 -2
@@ -2358,6 +2358,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) {
@@ -2458,9 +2459,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
@@ -1257,6 +1257,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 +12 -0
@@ -456,6 +456,18 @@ 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 &TRACE_ITER_CONSOLE_LATENCY ); +} +EXPORT_SYMBOL_GPL (console_tracing_disabled); +#ifdef CONFIG_FUNCTION_TRACER static bool function_enabled;
Signature · 1 line
2.17.1