From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
"Joel Fernandes (Google)" <joel@joelfernandes.org>,
"Viktor Rosendahl (BMW)" <viktor.rosendahl@gmail.com>
Subject: [for-next][PATCH 16/33] ftrace: Implement fs notification for tracing_max_latency
Date: Thu, 14 Nov 2019 13:17:50 -0500 [thread overview]
Message-ID: <20191114181825.715158579@goodmis.org> (raw)
In-Reply-To: 20191114181734.067922168@goodmis.org
From: "Viktor Rosendahl (BMW)" <viktor.rosendahl@gmail.com>
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.
The tracers may call the latency_fsnotify() from places such as
__schedule() or do_idle(); this makes it impossible to call
queue_work() directly without risking a deadlock. The same would
happen with a softirq, kernel thread or tasklet. For this reason we
use the irq_work mechanism to call queue_work().
This patch creates a new workqueue. The reason for doing this is that
I wanted to use the WQ_UNBOUND and WQ_HIGHPRI flags. My thinking was
that WQ_UNBOUND might help with the latency in some important cases.
If we use:
queue_work(system_highpri_wq, &tr->fsnotify_work);
then the work will (almost) always execute on the same CPU but if we are
unlucky that CPU could be too busy while there could be another CPU in
the system that would be able to process the work soon enough.
queue_work_on() could be used to queue the work on another CPU but it
seems difficult to select the right CPU.
Link: http://lkml.kernel.org/r/20191008220824.7911-2-viktor.rosendahl@gmail.com
Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Signed-off-by: Viktor Rosendahl (BMW) <viktor.rosendahl@gmail.com>
[ Added max() to have one compare for max latency ]
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
kernel/trace/trace.c | 75 +++++++++++++++++++++++++++++++++++++-
kernel/trace/trace.h | 18 +++++++++
kernel/trace/trace_hwlat.c | 11 ++++--
3 files changed, 98 insertions(+), 6 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 5ea8c7c0f2d7..f093a433cb42 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -45,6 +45,9 @@
#include <linux/trace.h>
#include <linux/sched/clock.h>
#include <linux/sched/rt.h>
+#include <linux/fsnotify.h>
+#include <linux/irq_work.h>
+#include <linux/workqueue.h>
#include "trace.h"
#include "trace_output.h"
@@ -1497,6 +1500,74 @@ static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
}
unsigned long __read_mostly tracing_thresh;
+static const struct file_operations tracing_max_lat_fops;
+
+#if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \
+ defined(CONFIG_FSNOTIFY)
+
+static struct workqueue_struct *fsnotify_wq;
+
+static void latency_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 latency_fsnotify_workfn_irq(struct irq_work *iwork)
+{
+ struct trace_array *tr = container_of(iwork, struct trace_array,
+ fsnotify_irqwork);
+ queue_work(fsnotify_wq, &tr->fsnotify_work);
+}
+
+static void trace_create_maxlat_file(struct trace_array *tr,
+ struct dentry *d_tracer)
+{
+ INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
+ init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
+ tr->d_max_latency = trace_create_file("tracing_max_latency", 0644,
+ d_tracer, &tr->max_latency,
+ &tracing_max_lat_fops);
+}
+
+__init static int latency_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(latency_fsnotify_init);
+
+void latency_fsnotify(struct trace_array *tr)
+{
+ if (!fsnotify_wq)
+ return;
+ /*
+ * We cannot call queue_work(&tr->fsnotify_work) from here because it's
+ * possible that we are called from __schedule() or do_idle(), which
+ * could cause a deadlock.
+ */
+ irq_work_queue(&tr->fsnotify_irqwork);
+}
+
+/*
+ * (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
/*
@@ -1536,6 +1607,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
/* record this tasks comm */
tracing_record_cmdline(tsk);
+ latency_fsnotify(tr);
}
/**
@@ -8594,8 +8666,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))
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 8b590f10bc72..718eb998c13e 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -16,6 +16,8 @@
#include <linux/trace_events.h>
#include <linux/compiler.h>
#include <linux/glob.h>
+#include <linux/irq_work.h>
+#include <linux/workqueue.h>
#ifdef CONFIG_FTRACE_SYSCALLS
#include <asm/unistd.h> /* For NR_SYSCALLS */
@@ -264,6 +266,11 @@ 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;
+ struct irq_work fsnotify_irqwork;
+#endif
#endif
struct trace_pid_list __rcu *filtered_pids;
/*
@@ -786,6 +793,17 @@ 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 latency_fsnotify(struct trace_array *tr);
+
+#else
+
+static void latency_fsnotify(struct trace_array *tr) { }
+
+#endif
+
#ifdef CONFIG_STACKTRACE
void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
int pc);
diff --git a/kernel/trace/trace_hwlat.c b/kernel/trace/trace_hwlat.c
index 862f4b0139fc..63526670605a 100644
--- a/kernel/trace/trace_hwlat.c
+++ b/kernel/trace/trace_hwlat.c
@@ -237,6 +237,7 @@ static int get_sample(void)
/* If we exceed the threshold value, we have found a hardware latency */
if (sample > thresh || outer_sample > thresh) {
struct hwlat_sample s;
+ u64 latency;
ret = 1;
@@ -253,11 +254,13 @@ static int get_sample(void)
s.nmi_count = nmi_count;
trace_hwlat_sample(&s);
+ latency = max(sample, outer_sample);
+
/* Keep a running maximum ever recorded hardware latency */
- if (sample > tr->max_latency)
- tr->max_latency = sample;
- if (outer_sample > tr->max_latency)
- tr->max_latency = outer_sample;
+ if (latency > tr->max_latency) {
+ tr->max_latency = latency;
+ latency_fsnotify(tr);
+ }
}
out:
--
2.23.0
next prev parent reply other threads:[~2019-11-14 18:20 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-14 18:17 [for-next][PATCH 00/33] tracing: Updates for 5.5 Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 01/33] ftrace: Introduce PERMANENT ftrace_ops flag Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 02/33] selftests/livepatch: Make dynamic debug setup and restore generic Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 03/33] selftests/livepatch: Test interaction with ftrace_enabled Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 04/33] ftrace: Separate out the copying of a ftrace_hash from __ftrace_hash_move() Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 05/33] ftrace: Separate out functionality from ftrace_location_range() Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 06/33] ftrace: Add register_ftrace_direct() Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 07/33] ftrace: Add ftrace_find_direct_func() Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 08/33] ftrace: Add sample module that uses register_ftrace_direct() Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 09/33] ftrace/selftest: Add tests to test register_ftrace_direct() Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 10/33] ftrace: Add another example of register_ftrace_direct() use case Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 11/33] ftrace/selftests: Update the direct call selftests to test two direct calls Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 12/33] ftrace/x86: Add register_ftrace_direct() for custom trampolines Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 13/33] ftrace/x86: Add a counter to test function_graph with direct Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 14/33] ftrace/x86: Tell objtool to ignore nondeterministic ftrace stack layout Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 15/33] ftrace: Add information on number of page groups allocated Steven Rostedt
2019-11-14 18:17 ` Steven Rostedt [this message]
2019-11-14 18:17 ` [for-next][PATCH 17/33] preemptirq_delay_test: Add the burst feature and a sysfs trigger Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 18/33] tracing: Use CONFIG_PREEMPTION Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 19/33] tracing: Make internal ftrace events static Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 20/33] tracing: Declare newly exported APIs in include/linux/trace.h Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 21/33] tracing: Verify if trace array exists before destroying it Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 22/33] tracing: Adding NULL checks for trace_array descriptor pointer Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 23/33] fgraph: Fix function type mismatches of ftrace_graph_return using ftrace_stub Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 24/33] tracing/selftests: Turn off timeout setting Steven Rostedt
2019-11-14 18:17 ` [for-next][PATCH 25/33] lib/sort: Move swap, cmp and cmp_r function types for wider use Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 26/33] lib/bsearch: Use generic type for comparator function Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 27/33] tracing: " Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 28/33] tracing/hwlat: Fix a few trivial nits Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 29/33] tracing: use kvcalloc for tgid_map array allocation Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 30/33] tracing/kprobe: Check whether the non-suffixed symbol is notrace Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 31/33] seq_buf: Add printing formatted hex dumps Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 32/33] tracing: Use seq_buf_hex_dump() to dump buffers Steven Rostedt
2019-11-14 18:18 ` [for-next][PATCH 33/33] tracing: Remove stray tab in TRACE_EVAL_MAP_FILEs help text Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191114181825.715158579@goodmis.org \
--to=rostedt@goodmis.org \
--cc=akpm@linux-foundation.org \
--cc=joel@joelfernandes.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=viktor.rosendahl@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox