Skip to contentSkip to conversation

sched: Enabled schedstat when schedstat tracepoints are enabled

PATCH In review Steven Rostedt 5 participants 9 messages Apr 12, 2017 – Apr 13, 2017

Subsystems (2)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH 1/1 sched: Enabled schedstat when schedstat tracepoints are enabled

From: Steven Rostedt (VMware) <[email protected]>

During my tests, I see this in my dmesg:

"Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enabled or kernel.sched_schedstats=1"

And found the commit:

cb2517653fc ("sched/debug: Make schedstats a runtime tunable that is disabled by default")

Which states:

"For tracepoints, there is a simple warning as it's not safe to activate
schedstats in the context when it's known the tracepoint may be wanted but is unavailable."

I'm assuming that Mel did not know about the TRACE_EVENT_FN() and DEFINE_EVENT_FN() that allow for callbacks for tracepoints as they are enabled and disabled. I do not see any reason for not enabling schedstat when one of its tracepoints are enabled.

The state of schedstat is saved when the first tracepoint is enabled, and that state is put back when the tracepoints are disabled.

---

include/linux/sched.h +8 -0
index d67eee8..691ab93f 100644
@@ -1456,6 +1456,14 @@ static inline int test_tsk_need_resched(struct task_struct *tsk)
 	return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
 }
 
+#if defined(CONFIG_TRACING) && defined(CONFIG_SCHEDSTATS)
+int schedstat_tracepoint_reg(void);
+void schedstat_tracepoint_unreg(void);
+#else
+static inline int schedstat_tracepoint_reg(void) { return 0; }
+static inline void schedstat_tracepoint_unreg(void) { }
+#endif
+
 /*
  * cond_resched() and cond_resched_lock(): latency reduction via
  * explicit rescheduling in places that are safe. The return
include/trace/events/sched.h +15 -10
index 9e3ef6c..ab8fa69 100644
@@ -346,32 +346,36 @@ DECLARE_EVENT_CLASS(sched_stat_template,
  * Tracepoint for accounting wait time (time the task is runnable
  * but not actually running due to scheduler contention).
  */
-DEFINE_EVENT(sched_stat_template, sched_stat_wait,
+DEFINE_EVENT_FN(sched_stat_template, sched_stat_wait,
 	     TP_PROTO(struct task_struct *tsk, u64 delay),
-	     TP_ARGS(tsk, delay));
+	     TP_ARGS(tsk, delay), schedstat_tracepoint_reg,
+	     schedstat_tracepoint_unreg);
 
 /*
  * Tracepoint for accounting sleep time (time the task is not runnable,
  * including iowait, see below).
  */
-DEFINE_EVENT(sched_stat_template, sched_stat_sleep,
+DEFINE_EVENT_FN(sched_stat_template, sched_stat_sleep,
 	     TP_PROTO(struct task_struct *tsk, u64 delay),
-	     TP_ARGS(tsk, delay));
+	     TP_ARGS(tsk, delay), schedstat_tracepoint_reg,
+	     schedstat_tracepoint_unreg);
 
 /*
  * Tracepoint for accounting iowait time (time the task is not runnable
  * due to waiting on IO to complete).
  */
-DEFINE_EVENT(sched_stat_template, sched_stat_iowait,
+DEFINE_EVENT_FN(sched_stat_template, sched_stat_iowait,
 	     TP_PROTO(struct task_struct *tsk, u64 delay),
-	     TP_ARGS(tsk, delay));
+	     TP_ARGS(tsk, delay), schedstat_tracepoint_reg,
+	     schedstat_tracepoint_unreg);
 
 /*
  * Tracepoint for accounting blocked time (time the task is in uninterruptible).
  */
-DEFINE_EVENT(sched_stat_template, sched_stat_blocked,
+DEFINE_EVENT_FN(sched_stat_template, sched_stat_blocked,
 	     TP_PROTO(struct task_struct *tsk, u64 delay),
-	     TP_ARGS(tsk, delay));
+	     TP_ARGS(tsk, delay), schedstat_tracepoint_reg,
+	     schedstat_tracepoint_unreg);
 
 /*
  * Tracepoint for accounting runtime (time the task is executing
@@ -403,9 +407,10 @@ DECLARE_EVENT_CLASS(sched_stat_runtime,
 			(unsigned long long)__entry->vruntime)
 );
 
-DEFINE_EVENT(sched_stat_runtime, sched_stat_runtime,
+DEFINE_EVENT_FN(sched_stat_runtime, sched_stat_runtime,
 	     TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime),
-	     TP_ARGS(tsk, runtime, vruntime));
+	     TP_ARGS(tsk, runtime, vruntime), schedstat_tracepoint_reg,
+	     schedstat_tracepoint_unreg);
 
 /*
  * Tracepoint for showing priority inheritance modifying a tasks
kernel/sched/core.c +28 -0
index 3b31fc0..95e9ce9 100644
@@ -2266,6 +2266,8 @@ int sysctl_numa_balancing(struct ctl_table *table, int write,
 
 DEFINE_STATIC_KEY_FALSE(sched_schedstats);
 static bool __initdata __sched_schedstats = false;
+static int schedstat_tracepoint_ref;
+static bool schedstat_save_state;
 
 static void set_schedstats(bool enabled)
 {
@@ -2275,6 +2277,32 @@ static void set_schedstats(bool enabled)
 		static_branch_disable(&sched_schedstats);
 }
 
+/*
+ * schedstat_tracepoint_reg() and unreg() are called by the tracepoint
+ * regfunc/unregfunc functions. They are protected by the tracepoint mutex.
+ *     See kernel/tracepoint.c:tracepoint_add_func().
+ *
+ * The modifications to schedstat_tracepoint_ref and schedstat_save_state
+ * are only done under that mutex, and do not need further protection.
+ */
+int schedstat_tracepoint_reg(void)
+{
+	if (!schedstat_tracepoint_ref) {
+		schedstat_save_state = schedstat_enabled();
+		if (!schedstat_save_state)
+			set_schedstats(true);
+	}
+	schedstat_tracepoint_ref++;
+}
+
+void schedstat_tracepoint_unreg(void)
+{
+	schedstat_tracepoint_ref--;
+	if (schedstat_tracepoint_ref || schedstat_save_state)
+		return;
+	set_schedstats(false);
+}
+
 void force_schedstat_enabled(void)
 {
 	if (!schedstat_enabled()) {
kernel/sched/fair.c +0 -22
index dea1389..d6665f6 100644
@@ -3526,27 +3526,6 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
 
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
 
-static inline void check_schedstat_required(void)
-{
-#ifdef CONFIG_SCHEDSTATS
-	if (schedstat_enabled())
-		return;
-
-	/* Force schedstat enabled if a dependent tracepoint is active */
-	if (trace_sched_stat_wait_enabled()    ||
-			trace_sched_stat_sleep_enabled()   ||
-			trace_sched_stat_iowait_enabled()  ||
-			trace_sched_stat_blocked_enabled() ||
-			trace_sched_stat_runtime_enabled())  {
-		printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
-			     "stat_blocked and stat_runtime require the "
-			     "kernel parameter schedstats=enabled or "
-			     "kernel.sched_schedstats=1\n");
-	}
-#endif
-}
-
-
 /*
  * MIGRATION
  *
@@ -3617,7 +3596,6 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	if (flags & ENQUEUE_WAKEUP)
 		place_entity(cfs_rq, se, 0);
 
-	check_schedstat_required();
 	update_stats_enqueue(cfs_rq, se, flags);
 	check_spread(cfs_rq, se);
 	if (!curr)
#1 lore raw
url: https://github.com/0day-ci/linux/commits/Steven-Rostedt/sched-Enabled-schedstat-when-schedstat-tracepoints-are-enabled/20170413-082900 config: i386-randconfig-x006-201715 (attached as .config)…

Hi Steven,

[auto build test ERROR on tip/sched/core]
[also build test ERROR on v4.11-rc6 next-20170412]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Steven-Rostedt/sched-Enabled-schedstat-when-schedstat-tracepoints-are-enabled/20170413-082900
config: i386-randconfig-x006-201715 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

>> kernel/sched/core.c:2289:5: error: redefinition of 'schedstat_tracepoint_reg'
    int schedstat_tracepoint_reg(void)
        ^~~~~~~~~~~~~~~~~~~~~~~~
   In file included from kernel/sched/core.c:8:0:
   include/linux/sched.h:1463:19: note: previous definition of 'schedstat_tracepoint_reg' was here
    static inline int schedstat_tracepoint_reg(void) { return 0; }
                      ^~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/sched/core.c:2299:6: error: redefinition of 'schedstat_tracepoint_unreg'
    void schedstat_tracepoint_unreg(void)
         ^~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from kernel/sched/core.c:8:0:
   include/linux/sched.h:1464:20: note: previous definition of 'schedstat_tracepoint_unreg' was here
    static inline void schedstat_tracepoint_unreg(void) { }
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/sched/core.c: In function 'schedstat_tracepoint_reg':
   kernel/sched/core.c:2297:1: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^

vim +/schedstat_tracepoint_reg +2289 kernel/sched/core.c

2283 * regfunc/unregfunc functions. They are protected by the tracepoint mutex.
2284 * See kernel/tracepoint.c:tracepoint_add_func().
2285 *
2286 * The modifications to schedstat_tracepoint_ref and schedstat_save_state
2287 * are only done under that mutex, and do not need further protection.
2288 */

> 2289	int schedstat_tracepoint_reg(void)
  2290	{
  2291		if (!schedstat_tracepoint_ref) {
  2292			schedstat_save_state = schedstat_enabled();
  2293			if (!schedstat_save_state)
  2294				set_schedstats(true);
  2295		}
  2296		schedstat_tracepoint_ref++;
  2297	}
  2298	
> 2299	void schedstat_tracepoint_unreg(void)

2300 {
2301 schedstat_tracepoint_ref--;
2302 if (schedstat_tracepoint_ref || schedstat_save_state)

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

maintainer
#2 lore raw
In reply to Steven Rostedt
Quoted 19 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

During my tests, I see this in my dmesg:

"Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enabled or kernel.sched_schedstats=1"

And found the commit:

cb2517653fc ("sched/debug: Make schedstats a runtime tunable that is disabled by default")

Which states:

"For tracepoints, there is a simple warning as it's not safe to activate
schedstats in the context when it's known the tracepoint may be wanted but is unavailable."

Quoted 7 lines

I'm assuming that Mel did not know about the TRACE_EVENT_FN() and DEFINE_EVENT_FN() that allow for callbacks for tracepoints as they are enabled and disabled. I do not see any reason for not enabling schedstat when one of its tracepoints are enabled.

The state of schedstat is saved when the first tracepoint is enabled, and that state is put back when the tracepoints are disabled.

There is one additional complication with all this.

Dynamically enabling the sched_stats like this doesn't guarantee correct information. So you've now taken away the informational print and silently generate bollocks numbers.

maintainer
#3 lore raw
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

During my tests, I see this in my dmesg:

"Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enabled or kernel.sched_schedstats=1"

And found the commit:

cb2517653fc ("sched/debug: Make schedstats a runtime tunable that is disabled by default")

Which states:

"For tracepoints, there is a simple warning as it's not safe to activate
schedstats in the context when it's known the tracepoint may be wanted but is unavailable."

I'm assuming that Mel did not know about the TRACE_EVENT_FN() and DEFINE_EVENT_FN() that allow for callbacks for tracepoints as they are enabled and disabled. I do not see any reason for not enabling schedstat when one of its tracepoints are enabled.

Quoted 8 lines
Quoted 2 lines

The state of schedstat is saved when the first tracepoint is enabled, and that state is put back when the tracepoints are disabled.

There is one additional complication with all this.

Dynamically enabling the sched_stats like this doesn't guarantee correct information. So you've now taken away the informational print and silently generate bollocks numbers.

Josh has a patch like:

http://lkml.kernel.org/r/00e8805cc094657d5ccb20bb88e0d2ce1cbb92e1.1466184592.git.jpoimboe@redhat.com

That tries to keep the few stats required for the tracepoint active at all times. But I never managed to get performance overhead to 0.

author maintainer
#4 lore raw
Quoted 35 lines · On Thu, 13 Apr 2017 11:01:24 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

During my tests, I see this in my dmesg:

"Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enabled or kernel.sched_schedstats=1"

And found the commit:

cb2517653fc ("sched/debug: Make schedstats a runtime tunable that is disabled by default")

Which states:

"For tracepoints, there is a simple warning as it's not safe to activate
schedstats in the context when it's known the tracepoint may be wanted but is unavailable."

I'm assuming that Mel did not know about the TRACE_EVENT_FN() and DEFINE_EVENT_FN() that allow for callbacks for tracepoints as they are enabled and disabled. I do not see any reason for not enabling schedstat when one of its tracepoints are enabled.

Quoted 8 lines
Quoted 2 lines

The state of schedstat is saved when the first tracepoint is enabled, and that state is put back when the tracepoints are disabled.

There is one additional complication with all this.

Dynamically enabling the sched_stats like this doesn't guarantee correct information. So you've now taken away the informational print and silently generate bollocks numbers.

Quoted 6 lines

Josh has a patch like:

http://lkml.kernel.org/r/00e8805cc094657d5ccb20bb88e0d2ce1cbb92e1.1466184592.git.jpoimboe@redhat.com

That tries to keep the few stats required for the tracepoint active at all times. But I never managed to get performance overhead to 0.

Interesting. I may take a look at that patch too. What benchmark did you use to measure performance?

Thanks!

-- Steve

reviewer Acked-by on v1 1/1
#5 lore raw
In reply to Steven Rostedt
Quoted 24 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

During my tests, I see this in my dmesg:

"Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enabled or kernel.sched_schedstats=1"

And found the commit:

cb2517653fc ("sched/debug: Make schedstats a runtime tunable that is disabled by default")

Which states:

"For tracepoints, there is a simple warning as it's not safe to activate
schedstats in the context when it's known the tracepoint may be wanted but is unavailable."

I'm assuming that Mel did not know about the TRACE_EVENT_FN() and DEFINE_EVENT_FN() that allow for callbacks for tracepoints as they are enabled and disabled. I do not see any reason for not enabling schedstat when one of its tracepoints are enabled.

Quoted 4 lines

The state of schedstat is saved when the first tracepoint is enabled, and that state is put back when the tracepoints are disabled.

You're right, I wasn't aware. Patch looks ok. When it triggers, there will be a short interval of garbage numbers but that's the same as what happens currently. It's not deliberate action any more that would flag to the user that there is a hazard but I think it'll be manageable.

Thanks Steven.

Signature · 2 lines
Mel Gorman
SUSE Labs
author maintainer
#6 lore raw
Quoted 28 lines · On Thu, 13 Apr 2017 11:01:19 +0100 Mel Gorman <[email protected]> wrote:
Quoted 27 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

During my tests, I see this in my dmesg:

"Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enabled or kernel.sched_schedstats=1"

And found the commit:

cb2517653fc ("sched/debug: Make schedstats a runtime tunable that is disabled by default")

Which states:

"For tracepoints, there is a simple warning as it's not safe to activate
schedstats in the context when it's known the tracepoint may be wanted but is unavailable."

I'm assuming that Mel did not know about the TRACE_EVENT_FN() and DEFINE_EVENT_FN() that allow for callbacks for tracepoints as they are enabled and disabled. I do not see any reason for not enabling schedstat when one of its tracepoints are enabled.

The state of schedstat is saved when the first tracepoint is enabled, and that state is put back when the tracepoints are disabled.

Quoted 6 lines
Quoted 1 line

You're right, I wasn't aware. Patch looks ok. When it triggers, there will be a short interval of garbage numbers but that's the same as what happens currently. It's not deliberate action any more that would flag to the user that there is a hazard but I think it'll be manageable.

I wonder if there's a way to figure out when the measurements are no longer garbage, and have a tracepoint or something to notify the user applications that they are good to go? Or perhaps even suppress the tracepoints (TRACE_EVENT_FN_COND) until they are stable.

Quoted 2 lines

Thanks!

-- Steve

#7 lore raw
In reply to Steven Rostedt

include/linux/sched.h static inline int test_tsk_need_resched(struct task_struct *tsk)

Quoted 12 lines
include/linux/sched.h +9 -0
+#if defined(CONFIG_TRACING) && defined(CONFIG_SCHEDSTATS)
+int schedstat_tracepoint_reg(void);
+void schedstat_tracepoint_unreg(void);
+#else
+static inline int schedstat_tracepoint_reg(void) { return 0; }
+static inline void schedstat_tracepoint_unreg(void) { }
+#endif
+
 
 
 
+#ifdef CONFIG_TRACING

Shouldn't this be also

Quoted 1 line

+#if defined(CONFIG_TRACING) && defined(CONFIG_SCHEDSTATS) ????

If CONFIG_TRACING is defined but CONFIG_SCHEDSTATS is not then build should complain about duplicate schedstat_tracepoint_reg(). No?

Quoted 29 lines
kernel/sched/core.c +29 -0
+static int schedstat_tracepoint_ref;
+static bool schedstat_save_state;
+/*
+ * schedstat_tracepoint_reg() and unreg() are called by the tracepoint
+ * regfunc/unregfunc functions. They are protected by the tracepoint mutex.
+ *     See kernel/tracepoint.c:tracepoint_add_func().
+ *
+ * The modifications to schedstat_tracepoint_ref and schedstat_save_state
+ * are only done under that mutex, and do not need further protection.
+ */
+int schedstat_tracepoint_reg(void)
+{
+	if (!schedstat_tracepoint_ref) {
+		schedstat_save_state = schedstat_enabled();
+		if (!schedstat_save_state)
+			set_schedstats(true);
+	}
+	schedstat_tracepoint_ref++;
+	return 0;
+}
+
+void schedstat_tracepoint_unreg(void)
+{
+	schedstat_tracepoint_ref--;
+	if (schedstat_tracepoint_ref || schedstat_save_state)
+		return;
+	set_schedstats(false);
+}
+#endif
author maintainer
#8 lore raw
Quoted 11 lines · On Thu, 13 Apr 2017 04:05:48 -0700 Srikar Dronamraju…
Quoted 9 lines
include/linux/sched.h +8 -0
+#if defined(CONFIG_TRACING) && defined(CONFIG_SCHEDSTATS)
+int schedstat_tracepoint_reg(void);
+void schedstat_tracepoint_unreg(void);
+#else
+static inline int schedstat_tracepoint_reg(void) { return 0; }
+static inline void schedstat_tracepoint_unreg(void) { }
+#endif
+
Quoted 4 lines
Quoted 1 line

+#ifdef CONFIG_TRACING

Shouldn't this be also

Quoted 1 line

+#if defined(CONFIG_TRACING) && defined(CONFIG_SCHEDSTATS) ????

This code is within a #ifdef CONFIG_SCHEDSTATS block. That would be redundant putting it here too.

-- Steve

Quoted 33 lines

If CONFIG_TRACING is defined but CONFIG_SCHEDSTATS is not then build should complain about duplicate schedstat_tracepoint_reg(). No?

Quoted 29 lines
kernel/sched/core.c +29 -0
+static int schedstat_tracepoint_ref;
+static bool schedstat_save_state;
+/*
+ * schedstat_tracepoint_reg() and unreg() are called by the tracepoint
+ * regfunc/unregfunc functions. They are protected by the tracepoint mutex.
+ *     See kernel/tracepoint.c:tracepoint_add_func().
+ *
+ * The modifications to schedstat_tracepoint_ref and schedstat_save_state
+ * are only done under that mutex, and do not need further protection.
+ */
+int schedstat_tracepoint_reg(void)
+{
+	if (!schedstat_tracepoint_ref) {
+		schedstat_save_state = schedstat_enabled();
+		if (!schedstat_save_state)
+			set_schedstats(true);
+	}
+	schedstat_tracepoint_ref++;
+	return 0;
+}
+
+void schedstat_tracepoint_unreg(void)
+{
+	schedstat_tracepoint_ref--;
+	if (schedstat_tracepoint_ref || schedstat_save_state)
+		return;
+	set_schedstats(false);
+}
+#endif