public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [for-next][PATCH 3/5] tracing: Add TRACE_EVENT_FN example
Date: Tue, 10 Feb 2015 12:02:24 -0500	[thread overview]
Message-ID: <20150210170407.558176607@goodmis.org> (raw)
In-Reply-To: 20150210170221.012858236@goodmis.org

[-- Attachment #1: 0003-tracing-Add-TRACE_EVENT_FN-example.patch --]
[-- Type: text/plain, Size: 3908 bytes --]

From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>

If a function should be called before a tracepoint is enabled
and/or after it is disabled, the TRACE_EVENT_FN() serves this
purpose. But it is not well documented. Having it as a sample would
help developers to know how to use it.

Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 samples/trace_events/trace-events-sample.c | 51 ++++++++++++++++++++++++++++++
 samples/trace_events/trace-events-sample.h | 44 ++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)

diff --git a/samples/trace_events/trace-events-sample.c b/samples/trace_events/trace-events-sample.c
index c396a49b5d78..39d4484aef53 100644
--- a/samples/trace_events/trace-events-sample.c
+++ b/samples/trace_events/trace-events-sample.c
@@ -49,6 +49,52 @@ static int simple_thread(void *arg)
 }
 
 static struct task_struct *simple_tsk;
+static struct task_struct *simple_tsk_fn;
+
+static void simple_thread_func_fn(int cnt)
+{
+	set_current_state(TASK_INTERRUPTIBLE);
+	schedule_timeout(HZ);
+
+	/* More silly tracepoints */
+	trace_foo_bar_with_fn("Look at me", cnt);
+}
+
+static int simple_thread_fn(void *arg)
+{
+	int cnt = 0;
+
+	while (!kthread_should_stop())
+		simple_thread_func_fn(cnt++);
+
+	return 0;
+}
+
+static DEFINE_MUTEX(thread_mutex);
+
+void foo_bar_reg(void)
+{
+	pr_info("Starting thread for foo_bar_fn\n");
+	/*
+	 * We shouldn't be able to start a trace when the module is
+	 * unloading (there's other locks to prevent that). But
+	 * for consistency sake, we still take the thread_mutex.
+	 */
+	mutex_lock(&thread_mutex);
+	simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
+	mutex_unlock(&thread_mutex);
+}
+
+void foo_bar_unreg(void)
+{
+	pr_info("Killing thread for foo_bar_fn\n");
+	/* protect against module unloading */
+	mutex_lock(&thread_mutex);
+	if (simple_tsk_fn)
+		kthread_stop(simple_tsk_fn);
+	simple_tsk_fn = NULL;
+	mutex_unlock(&thread_mutex);
+}
 
 static int __init trace_event_init(void)
 {
@@ -62,6 +108,11 @@ static int __init trace_event_init(void)
 static void __exit trace_event_exit(void)
 {
 	kthread_stop(simple_tsk);
+	mutex_lock(&thread_mutex);
+	if (simple_tsk_fn)
+		kthread_stop(simple_tsk_fn);
+	simple_tsk_fn = NULL;
+	mutex_unlock(&thread_mutex);
 }
 
 module_init(trace_event_init);
diff --git a/samples/trace_events/trace-events-sample.h b/samples/trace_events/trace-events-sample.h
index c3232340914d..d0be8411b527 100644
--- a/samples/trace_events/trace-events-sample.h
+++ b/samples/trace_events/trace-events-sample.h
@@ -270,6 +270,50 @@ TRACE_EVENT_CONDITION(foo_bar_with_cond,
 
 	TP_printk("foo %s %d", __get_str(foo), __entry->bar)
 );
+
+void foo_bar_reg(void);
+void foo_bar_unreg(void);
+
+/*
+ * Now in the case that some function needs to be called when the
+ * tracepoint is enabled and/or when it is disabled, the
+ * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
+ * but adds two more parameters at the end:
+ *
+ * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
+ *
+ * reg and unreg are functions with the prototype of:
+ *
+ *    void reg(void)
+ *
+ * The reg function gets called before the tracepoint is enabled, and
+ * the unreg function gets called after the tracepoint is disabled.
+ *
+ * Note, reg and unreg are allowed to be NULL. If you only need to
+ * call a function before enabling, or after disabling, just set one
+ * function and pass in NULL for the other parameter.
+ */
+TRACE_EVENT_FN(foo_bar_with_fn,
+
+	TP_PROTO(const char *foo, int bar),
+
+	TP_ARGS(foo, bar),
+
+	TP_STRUCT__entry(
+		__string(	foo,    foo		)
+		__field(	int,	bar		)
+	),
+
+	TP_fast_assign(
+		__assign_str(foo, foo);
+		__entry->bar	= bar;
+	),
+
+	TP_printk("foo %s %d", __get_str(foo), __entry->bar),
+
+	foo_bar_reg, foo_bar_unreg
+);
+
 #endif
 
 /***** NOTICE! The #if protection ends here. *****/
-- 
2.1.4



  parent reply	other threads:[~2015-02-10 17:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-10 17:02 [for-next][PATCH 0/5] tracing: Some last minute updates for 3.20 Steven Rostedt
2015-02-10 17:02 ` [for-next][PATCH 1/5] tracing: Update the TRACE_EVENT fields available in the sample code Steven Rostedt
2015-02-10 17:02 ` [for-next][PATCH 2/5] tracing: Add TRACE_EVENT_CONDITION sample Steven Rostedt
2015-02-10 17:02 ` Steven Rostedt [this message]
2015-02-10 17:02 ` [for-next][PATCH 4/5] tracing: Add samples of DECLARE_EVENT_CLASS() and DEFINE_EVENT() Steven Rostedt
2015-02-10 17:02 ` [for-next][PATCH 5/5] tracing: Fix unmapping loop in tracing_mark_write 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=20150210170407.558176607@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    /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