Skip to contentSkip to conversation

Extend scheduling tracepoints

RFC PATCH In review Julien Desfossez 2 participants 4 messages Jan 13, 2017 – Feb 7, 2017 Tracing

lore
Share & tools
lore
#0 lore raw

v3 cover Extend scheduling tracepoints

This patchset proposes a way to extend the existing scheduling tracepoints. The intent is to allow to output relevant priority informations based on the scheduling class of the tasks without breaking the existing tracepoints or having to handle the various cases in the scheduler code.

The proposed approach is to add the TRACE_EVENT_MAP_COND macro which depends on an existing TRACE_EVENT, but only writes data into the trace when a condition is satisfied. This allows to create multiple versions of the same tracepoint without having to call each of them explicitly from the instrumented code.

This approach is then illustrated with the sched_switch tracepoint, where we provide a way to output different fields based on the scheduling class of the next task.

If accepted, this method could be used everywhere the "prio" field is currently exposed to user-space through traces.

Julien Desfossez (2):
tracing: add TRACE_EVENT_MAP_COND
tracing: add policy-based sched_switch events

 include/linux/trace_events.h |  14 +++-
 include/linux/tracepoint.h   |   6 ++
 include/trace/define_trace.h |   6 ++
 include/trace/events/sched.h | 192 +++++++++++++++++++++++++++++++++++++++++++
 include/trace/perf.h         |  24 ++++--
 include/trace/trace_events.h | 130 +++++++++++++++++++++++++----
 kernel/trace/trace_events.c  |  15 +++-
 7 files changed, 360 insertions(+), 27 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

RFC PATCH v3 1/2 tracing: add TRACE_EVENT_MAP_COND

This new macro allows to hook conditional tracepoint probes to pre-existing trace events. This allows to create specialized versions of the same tracepoint without having to explicitly call every possible tracepoints in the instrumented code.

In order to use it, a TRACE_EVENT must already exist, after that, we can connect as many TRACE_EVENT_MAP_COND to this TRACE_EVENT as needed.

Example usage:

TRACE_EVENT(tp_test,

	TP_PROTO(proto),
	TP_ARGS(args),
	TP_STRUCT__entry(), /* can be empty */
	TP_fast_assign(), /* can be empty */
	TP_printk() /* can be empty */

);

TRACE_EVENT_MAP_COND(tp_test, cond_test,

	TP_PROTO(proto),
	TP_ARGS(args),
	TP_CONDITION(cond),
	TP_STRUCT__entry(entry),
	TP_fast_assign(assign),
	TP_printk(print)

);

Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt (Red Hat) <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Daniel Bristot de Oliveira <[email protected]>
---
 include/linux/trace_events.h |  14 ++++-
 include/linux/tracepoint.h   |   6 ++
 include/trace/define_trace.h |   6 ++
 include/trace/perf.h         |  24 ++++++--
 include/trace/trace_events.h | 130 +++++++++++++++++++++++++++++++++++++------
 kernel/trace/trace_events.c  |  15 +++--
 6 files changed, 168 insertions(+), 27 deletions(-)
include/linux/trace_events.h +13 -1
index be00761..1f7e0ec 100644
@@ -217,6 +217,7 @@ enum {
 	TRACE_EVENT_FL_TRACEPOINT_BIT,
 	TRACE_EVENT_FL_KPROBE_BIT,
 	TRACE_EVENT_FL_UPROBE_BIT,
+	TRACE_EVENT_FL_MAP_BIT,
 };
 
 /*
@@ -231,6 +232,7 @@ enum {
  *  TRACEPOINT    - Event is a tracepoint
  *  KPROBE        - Event is a kprobe
  *  UPROBE        - Event is a uprobe
+ *  MAP           - Event maps to a tracepoint as an alias
  */
 enum {
 	TRACE_EVENT_FL_FILTERED		= (1 << TRACE_EVENT_FL_FILTERED_BIT),
@@ -241,10 +243,16 @@ enum {
 	TRACE_EVENT_FL_TRACEPOINT	= (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
 	TRACE_EVENT_FL_KPROBE		= (1 << TRACE_EVENT_FL_KPROBE_BIT),
 	TRACE_EVENT_FL_UPROBE		= (1 << TRACE_EVENT_FL_UPROBE_BIT),
+	TRACE_EVENT_FL_MAP		= (1 << TRACE_EVENT_FL_MAP_BIT),
 };
 
 #define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
 
+struct trace_event_map {
+	struct tracepoint	*tp;
+	char			*name;
+};
+
 struct trace_event_call {
 	struct list_head	list;
 	struct trace_event_class *class;
@@ -252,6 +260,8 @@ struct trace_event_call {
 		char			*name;
 		/* Set TRACE_EVENT_FL_TRACEPOINT flag when using "tp" */
 		struct tracepoint	*tp;
+		/* Set TRACE_EVENT_FL_MAP flag when using "map" instead */
+		struct trace_event_map	*map;
 	};
 	struct trace_event	event;
 	char			*print_fmt;
@@ -282,7 +292,9 @@ struct trace_event_call {
 static inline const char *
 trace_event_name(struct trace_event_call *call)
 {
-	if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
+	if (call->flags & TRACE_EVENT_FL_MAP)
+		return call->map->name;
+	else if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
 		return call->tp ? call->tp->name : NULL;
 	else
 		return call->name;
include/linux/tracepoint.h +6 -0
index f72fcfe..3e5b5a4 100644
@@ -276,6 +276,7 @@ static inline void tracepoint_synchronize_unregister(void)
 
 #define DEFINE_TRACE_FN(name, reg, unreg)
 #define DEFINE_TRACE(name)
+#define DEFINE_TRACE_MAP_COND(name, map, cond)
 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
 #define EXPORT_TRACEPOINT_SYMBOL(name)
 
@@ -469,6 +470,8 @@ static inline void tracepoint_synchronize_unregister(void)
  */
 
 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
+#define DECLARE_EVENT_COND_CLASS(name, proto, args, cond, 	\
+		tstruct, assign, print)
 #define DEFINE_EVENT(template, name, proto, args)		\
 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
@@ -498,4 +501,7 @@ static inline void tracepoint_synchronize_unregister(void)
 
 #define TRACE_EVENT_PERF_PERM(event, expr...)
 
+#define TRACE_EVENT_MAP_COND(name, map, proto, args, cond, 	\
+		struct, assign, print)
+
 #endif /* ifdef TRACE_EVENT (see note above) */
include/trace/define_trace.h +6 -0
index 6e3945f..4e112f2 100644
@@ -45,6 +45,10 @@
 		assign, print, reg, unreg)			\
 	DEFINE_TRACE_FN(name, reg, unreg)
 
+#undef TRACE_EVENT_MAP_COND
+#define TRACE_EVENT_MAP_COND(name, map, proto, args, cond, tstruct,	\
+		assign, print)
+
 #undef DEFINE_EVENT
 #define DEFINE_EVENT(template, name, proto, args) \
 	DEFINE_TRACE(name)
@@ -100,7 +104,9 @@
 #undef TRACE_EVENT_FN
 #undef TRACE_EVENT_FN_COND
 #undef TRACE_EVENT_CONDITION
+#undef TRACE_EVENT_MAP_COND
 #undef DECLARE_EVENT_CLASS
+#undef DECLARE_EVENT_COND_CLASS
 #undef DEFINE_EVENT
 #undef DEFINE_EVENT_FN
 #undef DEFINE_EVENT_PRINT
include/trace/perf.h +19 -5
index 04fe68bb..dbd3d27 100644
@@ -26,8 +26,9 @@
 #undef __perf_task
 #define __perf_task(t)	(__task = (t))
 
-#undef DECLARE_EVENT_CLASS
-#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
+#undef DECLARE_EVENT_COND_CLASS
+#define DECLARE_EVENT_COND_CLASS(call, proto, args, cond, tstruct,	\
+		assign, print)						\
 static notrace void							\
 perf_trace_##call(void *__data, proto)					\
 {									\
@@ -43,6 +44,9 @@
 	int __data_size;						\
 	int rctx;							\
 									\
+	if (!(cond))							\
+		return;							\
+									\
 	__data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
 									\
 	head = this_cpu_ptr(event_call->perf_events);			\
@@ -69,18 +73,28 @@
 				  head, __task);			\
 }
 
+#undef DECLARE_EVENT_CLASS
+#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
+	DECLARE_EVENT_COND_CLASS(call, PARAMS(proto), PARAMS(args),	\
+			1, PARAMS(tstruct), PARAMS(assign),		\
+			PARAMS(print))
+
 /*
  * This part is compiled out, it is only here as a build time check
  * to make sure that if the tracepoint handling changes, the
  * perf probe will fail to compile unless it too is updated.
  */
-#undef DEFINE_EVENT
-#define DEFINE_EVENT(template, call, proto, args)			\
-static inline void perf_test_probe_##call(void)				\
+#undef DEFINE_EVENT_MAP_COND
+#define DEFINE_EVENT_MAP_COND(template, call, map, proto, args, cond)	\
+static inline void perf_test_probe_##map(void)				\
 {									\
 	check_trace_callback_type_##call(perf_trace_##template);	\
 }
 
+#undef DEFINE_EVENT
+#define DEFINE_EVENT(template, call, proto, args)			\
+	DEFINE_EVENT_MAP_COND(template, call, call, PARAMS(proto),	\
+			PARAMS(args), 1)
 
 #undef DEFINE_EVENT_PRINT
 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
include/trace/trace_events.h +113 -17

262 lines not on this page · raw message

kernel/trace/trace_events.c +11 -4
index 9311654..b4ea2bf 100644
@@ -287,26 +287,33 @@ int trace_event_reg(struct trace_event_call *call,
 		    enum trace_reg type, void *data)
 {
 	struct trace_event_file *file = data;
+	struct tracepoint *tp;
 
 	WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
+
+	if (call->flags & TRACE_EVENT_FL_MAP)
+		tp = call->map->tp;
+	else
+		tp = call->tp;
+
 	switch (type) {
 	case TRACE_REG_REGISTER:
-		return tracepoint_probe_register(call->tp,
+		return tracepoint_probe_register(tp,
 						 call->class->probe,
 						 file);
 	case TRACE_REG_UNREGISTER:
-		tracepoint_probe_unregister(call->tp,
+		tracepoint_probe_unregister(tp,
 					    call->class->probe,
 					    file);
 		return 0;
 
 #ifdef CONFIG_PERF_EVENTS
 	case TRACE_REG_PERF_REGISTER:
-		return tracepoint_probe_register(call->tp,
+		return tracepoint_probe_register(tp,
 						 call->class->perf_probe,
 						 call);
 	case TRACE_REG_PERF_UNREGISTER:
-		tracepoint_probe_unregister(call->tp,
+		tracepoint_probe_unregister(tp,
 					    call->class->perf_probe,
 					    call);
 		return 0;
Signature · 1 line
1.9.1
#2 lore raw

RFC PATCH v3 2/2 tracing: add policy-based sched_switch events

In reply to Julien Desfossez

Add 3 new tracepoints: sched_switch_fair, sched_switch_rt and sched_switch_dl.

These conditional tracepoints are emitted based on the scheduling class of the next task. Each of these tracepoint gets rid of the prio field from the original sched_switch and replaces it with fields that are relevant to the policy of the next task:
- for a fair task: the nice value,
- for a rt task: the nice and rt_priority values,
- for a dl task: the runtime, deadline and period values.

The original sched_switch event is left unmodified, so these new events can be enabled at the same time (but they are emitted consecutively so we can see a timestamp offset).

Example output from the 3 new events:
sched_switch_fair: prev_comm=cat prev_pid=2179 prev_state=R+ ==> next_comm=b
next_pid=874 next_policy=SCHED_NORMAL next_nice=0

sched_switch_rt: prev_comm=swapper/10 prev_pid=0 prev_state=R ==> next_comm=b
next_pid=2215 next_policy=SCHED_FIFO next_nice=0 next_rt_priority=100

sched_switch_dl: prev_comm=swapper/10 prev_pid=0 prev_state=R ==> next_comm=b

                 next_pid=2215 next_policy=SCHED_DEADLINE
		 next_dl_runtime=10000000 next_dl_deadline=30000000
		 next_dl_period=30000000
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt (Red Hat) <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Daniel Bristot de Oliveira <[email protected]>
---
 include/trace/events/sched.h | 192 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 192 insertions(+)
include/trace/events/sched.h +192 -0
index 9b90c57..c506ed1 100644
@@ -5,9 +5,39 @@
 #define _TRACE_SCHED_H
 
 #include <linux/sched.h>
+#include <linux/sched/deadline.h>
+#include <linux/sched/rt.h>
 #include <linux/tracepoint.h>
 #include <linux/binfmts.h>
 
+#define SCHEDULING_POLICY				\
+	EM( SCHED_NORMAL,	"SCHED_NORMAL")		\
+	EM( SCHED_FIFO,		"SCHED_FIFO")		\
+	EM( SCHED_RR,		"SCHED_RR")		\
+	EM( SCHED_BATCH,	"SCHED_BATCH")		\
+	EM( SCHED_IDLE,		"SCHED_IDLE")		\
+	EMe(SCHED_DEADLINE,	"SCHED_DEADLINE")
+
+/*
+ * First define the enums in the above macros to be exported to userspace
+ * via TRACE_DEFINE_ENUM().
+ */
+#undef EM
+#undef EMe
+#define EM(a, b)       TRACE_DEFINE_ENUM(a);
+#define EMe(a, b)      TRACE_DEFINE_ENUM(a);
+
+SCHEDULING_POLICY
+
+/*
+ * Now redefine the EM() and EMe() macros to map the enums to the strings
+ * that will be printed in the output.
+ */
+#undef EM
+#undef EMe
+#define EM(a, b)       {a, b},
+#define EMe(a, b)      {a, b}
+
 /*
  * Tracepoint for calling kthread_stop, performed to end a kthread:
  */
@@ -162,6 +192,168 @@ static inline long __trace_sched_switch_state(bool preempt, struct task_struct *
 );
 
 /*
+ * Tracepoint for task switches, performed by the scheduler where the next
+ * task has a fair scheduling policy.
+ */
+TRACE_EVENT_MAP_COND(sched_switch, sched_switch_fair,
+
+	TP_PROTO(bool preempt,
+		 struct task_struct *prev,
+		 struct task_struct *next),
+
+	TP_ARGS(preempt, prev, next),
+
+	TP_CONDITION(!dl_prio(next->prio) && !rt_prio(next->prio)),
+
+	TP_STRUCT__entry(
+		__array(	char,	prev_comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	prev_pid			)
+		__field(	long,	prev_state			)
+		__array(	char,	next_comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	next_pid			)
+		__field(	unsigned int, next_policy		)
+		__field(	int,	next_nice			)
+	),
+
+	TP_fast_assign(
+		memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
+		__entry->prev_pid	= prev->pid;
+		__entry->prev_state	= __trace_sched_switch_state(preempt, prev);
+		memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
+		__entry->next_pid	= next->pid;
+		__entry->next_policy	= next->policy;
+		__entry->next_nice	= task_nice(next);
+	),
+
+	TP_printk("prev_comm=%s prev_pid=%d prev_state=%s%s ==> next_comm=%s "
+			"next_pid=%d next_policy=%s next_nice=%d",
+		__entry->prev_comm, __entry->prev_pid,
+		__entry->prev_state & (TASK_STATE_MAX-1) ?
+		  __print_flags(__entry->prev_state & (TASK_STATE_MAX-1), "|",
+				{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
+				{ 16, "Z" }, { 32, "X" }, { 64, "x" },
+				{ 128, "K" }, { 256, "W" }, { 512, "P" },
+				{ 1024, "N" }) : "R",
+		__entry->prev_state & TASK_STATE_MAX ? "+" : "",
+		__entry->next_comm, __entry->next_pid,
+		__print_symbolic(__entry->next_policy, SCHEDULING_POLICY),
+		__entry->next_nice)
+);
+
+/*
+ * Tracepoint for task switches, performed by the scheduler where the next
+ * task has a rt scheduling policy.
+ */
+TRACE_EVENT_MAP_COND(sched_switch, sched_switch_rt,
+
+	TP_PROTO(bool preempt,
+		 struct task_struct *prev,
+		 struct task_struct *next),
+
+	TP_ARGS(preempt, prev, next),
+
+	TP_CONDITION(rt_prio(next->prio)),
+
+	TP_STRUCT__entry(
+		__array(	char,	prev_comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	prev_pid			)
+		__field(	long,	prev_state			)
+		__array(	char,	next_comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	next_pid			)
+		__field(	unsigned int, next_policy		)
+		__field(	int,	next_nice			)
+		__field(	unsigned int,	next_rt_priority	)
+	),
+
+	TP_fast_assign(
+		memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
+		__entry->prev_pid	= prev->pid;
+		__entry->prev_state	= __trace_sched_switch_state(preempt, prev);
+		memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
+		__entry->next_pid	= next->pid;
+		/*
+		 * With PI, a real RT policy might not be set and the default
+		 * RT policy is SCHED_FIFO.
+		 */
+		__entry->next_policy	= (next->policy == SCHED_RR) ?
+						SCHED_RR : SCHED_FIFO;
+		__entry->next_nice	= task_nice(next);
+		__entry->next_rt_priority = MAX_RT_PRIO - 1 - next->prio;
+	),
+
+	TP_printk("prev_comm=%s prev_pid=%d prev_state=%s%s ==> next_comm=%s "
+			"next_pid=%d next_policy=%s next_nice=%d "
+			"next_rt_priority=%u",
+		__entry->prev_comm, __entry->prev_pid,
+		__entry->prev_state & (TASK_STATE_MAX-1) ?
+		  __print_flags(__entry->prev_state & (TASK_STATE_MAX-1), "|",
+				{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
+				{ 16, "Z" }, { 32, "X" }, { 64, "x" },
+				{ 128, "K" }, { 256, "W" }, { 512, "P" },
+				{ 1024, "N" }) : "R",
+		__entry->prev_state & TASK_STATE_MAX ? "+" : "",
+		__entry->next_comm, __entry->next_pid,
+		__print_symbolic(__entry->next_policy, SCHEDULING_POLICY),
+		__entry->next_nice, __entry->next_rt_priority)
+);
+
+/*
+ * Tracepoint for task switches, performed by the scheduler where the next
+ * task has a deadline scheduling policy.
+ */
+TRACE_EVENT_MAP_COND(sched_switch, sched_switch_dl,
+
+	TP_PROTO(bool preempt,
+		 struct task_struct *prev,
+		 struct task_struct *next),
+
+	TP_ARGS(preempt, prev, next),
+
+	TP_CONDITION(dl_prio(next->prio)),
+
+	TP_STRUCT__entry(
+		__array(	char,	prev_comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	prev_pid			)
+		__field(	long,	prev_state			)
+		__array(	char,	next_comm,	TASK_COMM_LEN	)
+		__field(	pid_t,	next_pid			)
+		__field(	unsigned int, next_policy		)
+		__field( 	u64,	next_dl_runtime			)
+		__field( 	u64,	next_dl_deadline		)
+		__field( 	u64,	next_dl_period			)
+	),
+
+	TP_fast_assign(
+		memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
+		__entry->prev_pid	= prev->pid;
+		__entry->prev_state	= __trace_sched_switch_state(preempt, prev);
+		memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
+		__entry->next_pid	= next->pid;
+		__entry->next_policy	= SCHED_DEADLINE;
+		__entry->next_dl_runtime	= next->dl.dl_runtime;
+		__entry->next_dl_deadline	= next->dl.dl_deadline;
+		__entry->next_dl_period		= next->dl.dl_period;
+	),
+
+	TP_printk("prev_comm=%s prev_pid=%d prev_state=%s%s ==> next_comm=%s "
+			"next_pid=%d next_policy=%s next_dl_runtime=%Lu "
+			"next_dl_deadline=%Lu next_dl_period=%Lu",
+		__entry->prev_comm, __entry->prev_pid,
+		__entry->prev_state & (TASK_STATE_MAX-1) ?
+		  __print_flags(__entry->prev_state & (TASK_STATE_MAX-1), "|",
+				{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
+				{ 16, "Z" }, { 32, "X" }, { 64, "x" },
+				{ 128, "K" }, { 256, "W" }, { 512, "P" },
+				{ 1024, "N" }) : "R",
+		__entry->prev_state & TASK_STATE_MAX ? "+" : "",
+		__entry->next_comm, __entry->next_pid,
+		__print_symbolic(__entry->next_policy, SCHEDULING_POLICY),
+		__entry->next_dl_runtime, __entry->next_dl_deadline,
+		__entry->next_dl_period)
+
+);
+
+/*
  * Tracepoint for a task being migrated:
  */
 TRACE_EVENT(sched_migrate_task,
Signature · 1 line
1.9.1
#3 lore raw

Re: [RFC PATCH v3 2/2] tracing: add policy-based sched_switch events

Quoted 2 lines · Julien Desfossez

Add 3 new tracepoints: sched_switch_fair, sched_switch_rt and sched_switch_dl.

Do they really _have_ to be whole new tracepoints? I really don't like littering the user interface with all that.

Furthermore, it doesn't seem to make sense to enable these individually, either all or none.

Quoted 6 lines

These conditional tracepoints are emitted based on the scheduling class of the next task. Each of these tracepoint gets rid of the prio field from the original sched_switch and replaces it with fields that are relevant to the policy of the next task:
- for a fair task: the nice value,
- for a rt task: the nice and rt_priority values,

Nice is not relevant to RT.

Quoted 5 lines

- for a dl task: the runtime, deadline and period values.

The original sched_switch event is left unmodified, so these new events can be enabled at the same time (but they are emitted consecutively so we can see a timestamp offset).

So ideally there would only be _1_ sched_switch event that I can enable, and it would (per default) not print the old style at all.

Some (global?) compat knob could be used to switch back to the old style output with clear notification that that is going to go away 'soon'.