linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: peterz@infradead.org
Cc: mingo@redhat.com, gregkh@linuxfoundation.org, jslaby@suse.com,
	linux-kernel@vger.kernel.org, laoar.shao@gmail.com
Subject: [PATCH v3] scheduler: enhancement to show_state_filter and SysRq
Date: Wed,  9 Aug 2017 18:31:28 +0800	[thread overview]
Message-ID: <1502274688-2508-1-git-send-email-laoar.shao@gmail.com> (raw)

Sometimes we want to get tasks in TASK_RUNNING sepcifically,
instead of dump all tasks.

For example, when the loadavg are high, we want to dump
tasks in TASK_RUNNING and TASK_UNINTERRUPTIBLE, which contribute
to system load. But mostly there're lots of tasks in Sleep state,
which occupies almost all of the kernel log buffer, even overflows
it, that causes the useful messages get lost. Although we can
enlarge the kernel log buffer, but that's not a good idea.

But with the current facility we can't dump tasks in TASK_RUNNING
state specifically because TASK_RUNNING is 0, that will dump all
tasks if we call show_state_filter(TASK_RUNNING).

So I made this change to make the show_state_filter more flexible,
and then we can dump the tasks in TASK_RUNNING specifically.

The reason I just modified the existing SysRq key 'W' other than
introducing a new key to dump the tasks in running state is that
dumping both blocked tasks and running tasks is more helpful than
dump blocked tasks only. That's an improvement.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 drivers/tty/sysrq.c         | 15 ++++++++-------
 include/linux/sched.h       |  1 +
 include/linux/sched/debug.h |  6 ++++--
 kernel/sched/core.c         |  8 +++++---
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index 3ffc1ce..d1433ed 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -289,14 +289,15 @@ static void sysrq_handle_showstate(int key)
 	.enable_mask	= SYSRQ_ENABLE_DUMP,
 };
 
-static void sysrq_handle_showstate_blocked(int key)
+/* tasks in blocked and running state contribute to loadavg */
+static void sysrq_handle_showstate_load(int key)
 {
-	show_state_filter(TASK_UNINTERRUPTIBLE);
+	show_state_filter(TASK_UNINTERRUPTIBLE << 1 | (TASK_RUNNING | 0x1));
 }
-static struct sysrq_key_op sysrq_showstate_blocked_op = {
-	.handler	= sysrq_handle_showstate_blocked,
-	.help_msg	= "show-blocked-tasks(w)",
-	.action_msg	= "Show Blocked State",
+static struct sysrq_key_op sysrq_showstate_load_op = {
+	.handler	= sysrq_handle_showstate_load,
+	.help_msg	= "show-blocked/running-tasks(w)",
+	.action_msg	= "Show Blocked/Running State",
 	.enable_mask	= SYSRQ_ENABLE_DUMP,
 };
 
@@ -477,7 +478,7 @@ static void sysrq_handle_unrt(int key)
 	&sysrq_mountro_op,		/* u */
 	/* v: May be registered for frame buffer console restore */
 	NULL,				/* v */
-	&sysrq_showstate_blocked_op,	/* w */
+	&sysrq_showstate_load_op,	/* w */
 	/* x: May be registered on mips for TLB dump */
 	/* x: May be registered on ppc/powerpc for xmon */
 	/* x: May be registered on sparc64 for global PMU dump */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 8337e2d..318f149 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -82,6 +82,7 @@
 #define TASK_NOLOAD			1024
 #define TASK_NEW			2048
 #define TASK_STATE_MAX			4096
+#define TASK_ALL_BITS			((TASK_STATE_MAX << 1) - 1)
 
 #define TASK_STATE_TO_CHAR_STR		"RSDTtXZxKWPNn"
 
diff --git a/include/linux/sched/debug.h b/include/linux/sched/debug.h
index e0eaee5..c844689 100644
--- a/include/linux/sched/debug.h
+++ b/include/linux/sched/debug.h
@@ -1,6 +1,8 @@
 #ifndef _LINUX_SCHED_DEBUG_H
 #define _LINUX_SCHED_DEBUG_H
 
+#include <linux/sched.h>
+
 /*
  * Various scheduler/task debugging interfaces:
  */
@@ -10,13 +12,13 @@
 extern void dump_cpu_task(int cpu);
 
 /*
- * Only dump TASK_* tasks. (0 for all tasks)
+ * Only dump TASK_* tasks. (TASK_ALL_BITS for all tasks)
  */
 extern void show_state_filter(unsigned long state_filter);
 
 static inline void show_state(void)
 {
-	show_state_filter(0);
+	show_state_filter(TASK_ALL_BITS);
 }
 
 struct pt_regs;
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 0869b20..873e579 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5161,19 +5161,21 @@ void show_state_filter(unsigned long state_filter)
 		 */
 		touch_nmi_watchdog();
 		touch_all_softlockup_watchdogs();
-		if (!state_filter || (p->state & state_filter))
+		/* in case we want to set TASK_RUNNING specifically */
+		if ((p->state != TASK_RUNNING ? p->state << 1 : 1) &
+		    state_filter)
 			sched_show_task(p);
 	}
 
 #ifdef CONFIG_SCHED_DEBUG
-	if (!state_filter)
+	if (state_filter == TASK_ALL_BITS)
 		sysrq_sched_debug_show();
 #endif
 	rcu_read_unlock();
 	/*
 	 * Only show locks if all tasks are dumped:
 	 */
-	if (!state_filter)
+	if (state_filter == TASK_ALL_BITS)
 		debug_show_all_locks();
 }
 
-- 
1.8.3.1

             reply	other threads:[~2017-08-09  2:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-09 10:31 Yafang Shao [this message]
2017-08-09  7:43 ` [PATCH v3] scheduler: enhancement to show_state_filter and SysRq Peter Zijlstra
2017-08-09  8:01   ` Yafang Shao
2017-08-09  9:09     ` Peter Zijlstra
2017-08-09  9:26       ` Yafang Shao
2017-08-09 16:42         ` Peter Zijlstra
2017-08-10  2:44           ` Yafang Shao

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=1502274688-2508-1-git-send-email-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.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;
as well as URLs for NNTP newsgroup(s).