Skip to contentSkip to conversation

distinct load average per-cpuset

PATCH In review Andrea Righi 3 participants 7 messages Oct 20, 2012 – Jul 18, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover distinct load average per-cpuset

Overview
~~~~~~~~
The cpusets subsystem allows to assign a different set of CPUs to a cgroup. A typical use case is to split large systems in small CPU/memory partitions and isolate certain users/applications in these subsets of the system.

Sometimes, to have a quick overview of the state of each partition, we may be interested to get the load average of the CPUs assigned to a particular cpuset, rather than the global load average of the system.

Proposed solution
~~~~~~~~~~~~~~~~~
The proposal is to add a new file in the cpuset subsystem to report the load average of the CPUs assinged to a particular cpuset cgroup.

Example:

 # echo 0-1 > /sys/fs/cgroup/cpuset/foo/cpuset.cpus
 # echo 2-3 > /sys/fs/cgroup/cpuset/bar/cpuset.cpus
 # echo $$ > /sys/fs/cgroup/cpuset/foo/tasks
 # for i in `seq 4`; do yes > /dev/null & done

... after ~5mins ...

 # cat /proc/loadavg /sys/fs/cgroup/cpuset/{foo,bar}/cpuset.loadavg
 3.99 2.66 1.24 6/377 2855
 3.98 2.64 1.20
 0.01 0.02 0.04

In this case we can easily find that the cpuset "foo" is the most busy in the system.

ChangeLog v1->v2:
- convert rq->nr_uninterruptible to a percpu variable
- fix nr_uninterruptible accounting in the wakeup/sleep paths
- use DEFINE_PER_CPU() instead of NR_CPUS arrays
- in patch 2/3 add a comment to explain the validity of evaluating the cpuset load average as the sum of the individual per-cpu load averages

[ Thanks to Peter Z. for the review and suggestions of v1 ]

TODO:
- report nr_running and nr_threads in cpuset.loadavg, producing the same output as /proc/loadavg; in this way we could do nice things like, for example, "mount --bind cpuset.loadavg /proc/loadavg" in a new mount namespace for a specific user that we want to isolate into a specific cpuset cgroup, etc...

[PATCH v2 1/3] sched: introduce distinct per-cpu load average [PATCH v2 2/3] cpusets: add load avgerage interface [PATCH v2 3/3] cpusets: add documentation of the loadavg file

 Documentation/cgroups/cpusets.txt |    1 +
 include/linux/sched.h             |    6 ++
 kernel/cpuset.c                   |  108 +++++++++++++++++++++++++++++++++++
 kernel/sched/core.c               |  112 +++++++++++++++++++++++++++++++------
 kernel/sched/debug.c              |    3 +-
 kernel/sched/sched.h              |    8 +--
 6 files changed, 214 insertions(+), 24 deletions(-)
#1 lore raw

PATCH v2 1/3 sched: introduce distinct per-cpu load average

Account load average, nr_running and nr_uninterruptible tasks per-cpu.

The new task_struct attribute on_cpu_uninterruptible is added to properly keep track of the cpu at deactivate time, when the task is set to the uninterruptible sleep state.

Moreover, rq->nr_uninterruptible is converted to a percpu variable to maintain a coherent nr_uninterruptible counter for each CPU (rather than having a single global counter defined as the sum over all CPUs). This adds less performance overhead than introducing atomic operations in the wakeup/sleep path.

This feature is required by the cpusets cgroup subsystem to report the load average per-cpuset.

---
 include/linux/sched.h |    6 +++
 kernel/sched/core.c   |  112 ++++++++++++++++++++++++++++++++++++++++++-------
 kernel/sched/debug.c  |    3 +-
 kernel/sched/sched.h  |    8 +---
 4 files changed, 105 insertions(+), 24 deletions(-)
include/linux/sched.h +6 -0
index 0dd42a0..e5dfe2a 100644
@@ -80,6 +80,8 @@ struct blk_plug;
  */
 extern unsigned long avenrun[];		/* Load averages */
 extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
+extern void get_cpu_avenrun(unsigned long *loads, int cpu,
+				unsigned long offset, int shift);
 
 #define FSHIFT		11		/* nr of bits of precision */
 #define FIXED_1		(1<<FSHIFT)	/* 1.0 as fixed-point */
@@ -98,7 +100,9 @@ extern int nr_threads;
 DECLARE_PER_CPU(unsigned long, process_counts);
 extern int nr_processes(void);
 extern unsigned long nr_running(void);
+extern unsigned long nr_running_cpu(int cpu);
 extern unsigned long nr_uninterruptible(void);
+extern unsigned long nr_uninterruptible_cpu(int cpu);
 extern unsigned long nr_iowait(void);
 extern unsigned long nr_iowait_cpu(int cpu);
 extern unsigned long this_cpu_load(void);
@@ -1197,6 +1201,8 @@ struct task_struct {
 #ifdef CONFIG_SMP
 	struct llist_node wake_entry;
 	int on_cpu;
+	/* Used to keep track of nr_uninterruptible tasks per-cpu */
+	int on_cpu_uninterruptible;
 #endif
 	int on_rq;
 
kernel/sched/core.c +96 -16

206 lines not on this page · raw message

kernel/sched/debug.c +2 -1
index 6f79596..ac6c73f 100644
@@ -276,7 +276,8 @@ do {									\
 		   rq->load.weight);
 	P(nr_switches);
 	P(nr_load_updates);
-	P(nr_uninterruptible);
+	SEQ_printf(m, "  .%-30s: %lu\n", "nr_uninterruptible",
+		   nr_uninterruptible_cpu(cpu));
 	PN(next_balance);
 	P(curr->pid);
 	PN(clock);
kernel/sched/sched.h +1 -7
index 7a7db09..8a0d303 100644
@@ -383,13 +383,7 @@ struct rq {
 	struct list_head leaf_rt_rq_list;
 #endif
 
-	/*
-	 * This is part of a global counter where only the total sum
-	 * over all CPUs matters. A task can increase this counter on
-	 * one CPU and if it got migrated afterwards it may decrease
-	 * it on another CPU. Always updated under the runqueue lock:
-	 */
-	unsigned long nr_uninterruptible;
+	unsigned long __percpu *nr_uninterruptible;
 
 	struct task_struct *curr, *idle, *stop;
 	unsigned long next_balance;
Signature · 1 line
1.7.10.4
maintainer
#2 lore raw

Re: [PATCH v2 1/3] sched: introduce distinct per-cpu load average

kernel/sched/sched.h struct rq { · line 387

Quoted 6 lines · Andrea Righi
kernel/sched/sched.h +1 -0
@@ -383,13 +383,7 @@ struct rq {
        struct list_head leaf_rt_rq_list;
 #endif
 
 
+       unsigned long __percpu *nr_uninterruptible; 

This is O(nr_cpus^2) memory..

kernel/sched/core.c unsigned long nr_uninterruptible(void) · line 1957

Quoted 13 lines
kernel/sched/core.c +11 -0
+unsigned long nr_uninterruptible_cpu(int cpu)
+{
+       struct rq *this = cpu_rq(cpu);
+       unsigned long val = 0;
+       int i;
+
+       for_each_online_cpu(i)
+               val += per_cpu(*this->nr_uninterruptible, i);
+
+       return val;
+}

I suspect you've got an accounting leak here on hot-plug.

kernel/sched/core.c unsigned long nr_running(void) · line 1938

Quoted 11 lines

unsigned long nr_uninterruptible(void)

 {
        unsigned long i, sum = 0;
 
        for_each_possible_cpu(i)
kernel/sched/core.c +1 -1
-               sum += cpu_rq(i)->nr_uninterruptible;
+               sum += nr_uninterruptible_cpu(i);
 
        /*
         * Since we read the counters lockless, it might be slightly

And this makes O(n^2) runtime!

#3 lore raw

Re: [PATCH v2 1/3] sched: introduce distinct per-cpu load average

Quoted 10 lines · Peter Zijlstra

kernel/sched/sched.h struct rq { · line 387

Quoted 6 lines · Andrea Righi
kernel/sched/sched.h +1 -0
@@ -383,13 +383,7 @@ struct rq {
        struct list_head leaf_rt_rq_list;
 #endif
 
 
+       unsigned long __percpu *nr_uninterruptible; 

This is O(nr_cpus^2) memory..

Correct, this doesn't add too much overhead to the wakeup/sleep path, but it's bad both in terms of memory and performance overhead in the other parts of the code for large SMP systems.

Quoted 15 lines
Quoted 4 lines
kernel/sched/core.c +1 -0
+unsigned long nr_uninterruptible_cpu(int cpu)

kernel/sched/core.c unsigned long nr_uninterruptible(void) · line 1957

Quoted 10 lines
kernel/sched/core.c +10 -0
+{
+       struct rq *this = cpu_rq(cpu);
+       unsigned long val = 0;
+       int i;
+
+       for_each_online_cpu(i)
+               val += per_cpu(*this->nr_uninterruptible, i);
+
+       return val;
+}

I suspect you've got an accounting leak here on hot-plug.

And I think you're right about the accounting leak with cpu hotplug. I'll do more tests with this part, until I come up with a better idea in general for the nr_uninterruptible accounting.

Thanks!
-Andrea

Quoted 14 lines

kernel/sched/core.c unsigned long nr_running(void) · line 1938

Quoted 11 lines

unsigned long nr_uninterruptible(void)

 {
        unsigned long i, sum = 0;
 
        for_each_possible_cpu(i)
kernel/sched/core.c +1 -1
-               sum += cpu_rq(i)->nr_uninterruptible;
+               sum += nr_uninterruptible_cpu(i);
 
        /*
         * Since we read the counters lockless, it might be slightly

And this makes O(n^2) runtime!

#4 lore raw

PATCH v2 2/3 cpusets: add load avgerage interface

In reply to Andrea Righi

Add the new file loadavg to report the load average of the cpus assigned to the cpuset cgroup.

The load average is reported using the typical three values as they appear in /proc/loadavg, averaged over 1, 5 and 15 minutes.

Example:

  # cat /sys/fs/cgroup/cpuset/foo/cpuset.loadavg
  3.98 2.64 1.20
---
 kernel/cpuset.c |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)
kernel/cpuset.c +108 -0
index f33c715..1bb10d1 100644
@@ -1465,6 +1465,7 @@ typedef enum {
 	FILE_MEMORY_PRESSURE,
 	FILE_SPREAD_PAGE,
 	FILE_SPREAD_SLAB,
+	FILE_LOADAVG,
 } cpuset_filetype_t;
 
 static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
@@ -1686,6 +1687,107 @@ static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
 	return 0;
 }
 
+/*
+ * XXX: move all of this to a better place and unify the different
+ * re-definition of these macros.
+ */
+#define LOAD_INT(x) ((x) >> FSHIFT)
+#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
+
+static void cpuset_show_loadavg(struct seq_file *m, const struct cpuset *cs)
+{
+	unsigned long avnrun[3] = {};
+	int cpu;
+
+	/*
+	 * The global load average is an exponentially decaying average of:
+	 *
+	 *   x(t) = nr_running(t) + nr_uninterruptible(t)
+	 *
+	 * The global load average of the system is evaluated as:
+	 *
+	 *   load(t) = load(t - 1) * exp_k + x(t) * (1 - exp_k)
+	 *
+	 * So, the load average of a cpuset with N CPUS can be evaluated as:
+	 *
+	 *   load_cs(t) = load_cs(t - 1) * exp_k + x_cs(t) * (1 - exp_k),
+	 *   x_cs(t) = \sum{i = 1}^{N} x_i(t)
+	 *
+	 * This is equivalent to the sum of all the partial load averages of
+	 * each CPU assigned to the cpuset:
+	 *
+	 *   load_cs(t) = \sum{i = 1}^{N} load_i(t)
+	 *
+	 * Proof:
+	 *
+	 *   load_1(t) = load_1(t - 1) * exp_k + x_1(t) * (1 - exp_k)
+	 *   load_2(t) = load_2(t - 1) * exp_k + x_2(t) * (1 - exp_k)
+	 *   ...
+	 *   load_N(t) = load_N(t - 1) * exp_k + x_N(t) * (1 - exp_k)
+	 *
+	 *   ===>
+	 *
+	 *   load_1(t) = x_1(1) * (1 - exp_k) * exp_k^{t - 1} +
+	 *               x_1(2) * (1 - exp_k) * exp_k^{t - 2} +
+	 *               ... +
+	 *               x_1(t)(1 - exp_k)
+	 *   load_2(t) = x_2(1) * (1 - exp_k) * exp_k^{t - 1} +
+	 *               x_2(2) * (1 - exp_k) * exp_k^{t - 2} +
+	 *               ... +
+	 *               x_2(t)(1 - exp_k)
+	 *   ...
+	 *   load_N(t) = x_N(1) * (1 - exp_k) * exp_k^{t - 1} +
+	 *               x_N(2) * (1 - exp_k) * exp_k^{t - 2} +
+	 *               ... +
+	 *               x_N(t)(1 - exp_k)
+	 *
+	 *   ===>
+	 *
+	 *   load_1(t) + load_2(t) + ... + load_N(t) =
+	 *       \sum_{i = 1}^{N} x_i(1) * (1 - exp_k) * exp_k^{t - 1} +
+	 *       \sum_{i = 1}^{N} x_i(2) * (1 - exp_k) * exp_k^{t - 2} +
+	 *       ... +
+	 *       \sum_{i = 1}^{N} x_i(t) * (1 - exp_k) = load_cs(t)
+	 */
+	for_each_cpu(cpu, cs->cpus_allowed) {
+		unsigned long cpu_avnrun[3];
+		int i;
+
+		get_cpu_avenrun(cpu_avnrun, cpu, FIXED_1/200, 0);
+
+		for (i = 0; i < ARRAY_SIZE(cpu_avnrun); i++)
+			avnrun[i] += cpu_avnrun[i];
+	}
+	/*
+	 * TODO: also report nr_running/nr_threads and last_pid, producing the
+	 * same output as /proc/loadavg.
+	 *
+	 * For nr_running we can just sum the nr_running_cpu() of the cores
+	 * assigned to this cs; what should we report in nr_threads? maybe
+	 * cgroup_task_count()? and what about last_pid?
+	 */
+	seq_printf(m, "%lu.%02lu %lu.%02lu %lu.%02lu\n",
+		   LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
+		   LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
+		   LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]));
+}
+
+static int cpuset_read_seq_string(struct cgroup *cont, struct cftype *cft,
+			       struct seq_file *m)
+{
+	struct cpuset *cs = cgroup_cs(cont);
+	cpuset_filetype_t type = cft->private;
+
+	switch (type) {
+	case FILE_LOADAVG:
+		cpuset_show_loadavg(m, cs);
+		break;
+	default:
+		BUG();
+	}
+
+	return 0;
+}
 
 /*
  * for the common functions, 'private' gives the type of file
@@ -1780,6 +1882,12 @@ static struct cftype files[] = {
 		.private = FILE_MEMORY_PRESSURE_ENABLED,
 	},
 
+	{
+		.name = "loadavg",
+		.read_seq_string = cpuset_read_seq_string,
+		.private = FILE_LOADAVG,
+	},
+
 	{ }	/* terminate */
 };
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v2 3/3 cpusets: add documentation of the loadavg file

In reply to Andrea Righi
---
 Documentation/cgroups/cpusets.txt |    1 +
 1 file changed, 1 insertion(+)
Documentation/cgroups/cpusets.txt +1 -0
index cefd3d8..d5ddc36 100644
@@ -179,6 +179,7 @@ files describing that cpuset:
  - cpuset.memory_spread_slab flag: if set, spread slab cache evenly on allowed nodes
  - cpuset.sched_load_balance flag: if set, load balance within CPUs on that cpuset
  - cpuset.sched_relax_domain_level: the searching range when migrating tasks
+ - cpuset.loadavg: the load average of the CPUs in that cpuset
 
 In addition, only the root cpuset has the following file:
  - cpuset.memory_pressure_enabled flag: compute memory_pressure?
Signature · 1 line
1.7.10.4
#6 lore raw
In reply to Andrea Righi
Quoted 6 lines · Andrea Righi

Overview
~~~~~~~~
The cpusets subsystem allows to assign a different set of CPUs to a cgroup. A typical use case is to split large systems in small CPU/memory partitions and isolate certain users/applications in these subsets of the system.

Quoted 8 lines

Sometimes, to have a quick overview of the state of each partition, we may be interested to get the load average of the CPUs assigned to a particular cpuset, rather than the global load average of the system.

Proposed solution
~~~~~~~~~~~~~~~~~
The proposal is to add a new file in the cpuset subsystem to report the load average of the CPUs assinged to a particular cpuset cgroup.

Hi,

What is the situation with this patch?

--
Ozan Çağlayan
Research Assistant
Galatasaray University - Computer Engineering Dept.
http://www.ozancaglayan.com