[Devel] [PATCH RHEL COMMIT] sched: Add primitives to calculate nr running, sleeping, stopped and uninterruptible tasks

Konstantin Khorenko khorenko at virtuozzo.com
Fri Sep 24 14:49:31 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after ark-5.14
------>
commit c03f5556eaf478d254e739ca6d3ac0029004daa3
Author: Kirill Tkhai <ktkhai at virtuozzo.com>
Date:   Fri Sep 24 14:49:30 2021 +0300

    sched: Add primitives to calculate nr running, sleeping, stopped and uninterruptible tasks
    
    Extracted from "Initial patch".
    
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
    
    https://jira.sw.ru/browse/PSBM-133986
    
    unsigned long -> unsigned int
    
    (cherry picked from commit 153ef777fd0e3be36ed2d4cb28de0449f7e14c41)
    Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn at virtuozzo.com>
---
 include/linux/sched/stat.h |  2 ++
 kernel/sched/core.c        | 23 +++++++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/include/linux/sched/stat.h b/include/linux/sched/stat.h
index 0108a38bb64d..044e6dcb2a0d 100644
--- a/include/linux/sched/stat.h
+++ b/include/linux/sched/stat.h
@@ -19,6 +19,8 @@ DECLARE_PER_CPU(unsigned long, process_counts);
 extern int nr_processes(void);
 extern unsigned int nr_running(void);
 extern bool single_task_running(void);
+extern unsigned int nr_sleeping(void);
+extern unsigned int nr_uninterruptible(void);
 extern unsigned int nr_iowait(void);
 extern unsigned int nr_iowait_cpu(int cpu);
 
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index c0c6a90ea32c..ebb6dd99b442 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4727,21 +4727,28 @@ context_switch(struct rq *rq, struct task_struct *prev,
 	return finish_task_switch(prev);
 }
 
+#define DECLARE_NR_ONLINE(varname)			\
+	unsigned int varname(void)			\
+	{						\
+		unsigned int i, sum = 0;		\
+		for_each_online_cpu(i)			\
+			sum += cpu_rq(i)->varname;	\
+		if (unlikely((int)sum < 0))		\
+			return 0;			\
+		return sum;				\
+	}						\
+	EXPORT_SYMBOL(varname);				\
+
 /*
  * nr_running and nr_context_switches:
  *
  * externally visible scheduler statistics: current number of runnable
  * threads, total number of context switches performed since bootup.
  */
-unsigned int nr_running(void)
-{
-	unsigned int i, sum = 0;
 
-	for_each_online_cpu(i)
-		sum += cpu_rq(i)->nr_running;
-
-	return sum;
-}
+DECLARE_NR_ONLINE(nr_running);
+DECLARE_NR_ONLINE(nr_sleeping);
+DECLARE_NR_ONLINE(nr_uninterruptible);
 
 /*
  * Check if only the current task is running on the CPU.


More information about the Devel mailing list