Skip to contentSkip to conversation

sched/fair: make CFS bandwidth slice per cpu group

PATCH Superseded Cong Wang 3 participants 9 messages Apr 30, 2018 – May 1, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 sched/fair: make CFS bandwidth slice per cpu group

The value of sched_cfs_bandwidth_slice_us plays an important role in distributing CPU time to each local CPU from the global pool, a smaller slice is more fair to distribute CPU time to each parallel tasks running on different CPUs.

Currently, the sched_cfs_bandwidth_slice_us is a global setting which affects all cgroups. Different groups may want different values based on their own workload, one size doesn't fit all. The global pool filled periodically is per cgroup too, they should have the right to distribute their own quota to each local CPU with their own frequency.

This patch intrdouces cpu.cfs_slice_us which allows each cgroup to specify their own slice length without any global impact. And, the global sysctl sched_cfs_bandwidth_slice_us now becomes the default value of each cpu.cfs_slice_us. However, updating this sysctl does not automatically update existing cgroups using a default value, people will have to update each cgroup accordingly to make a global update.

Cc Paul Turner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 Documentation/scheduler/sched-bwc.txt | 14 +++++++----
 kernel/sched/core.c                   | 45 +++++++++++++++++++++++++++++++++++
 kernel/sched/fair.c                   | 17 ++++++-------
 kernel/sched/sched.h                  |  6 +++++
 4 files changed, 70 insertions(+), 12 deletions(-)
Documentation/scheduler/sched-bwc.txt +10 -4
index f6b1873f68ab..b2d6ff02e5b3 100644
@@ -48,19 +48,25 @@ and return the group to an unconstrained state once more.
 Any updates to a group's bandwidth specification will result in it becoming
 unthrottled if it is in a constrained state.
 
-System wide settings
+Slice
 --------------------
 For efficiency run-time is transferred between the global pool and CPU local
 "silos" in a batch fashion.  This greatly reduces global accounting pressure
 on large systems.  The amount transferred each time such an update is required
 is described as the "slice".
 
-This is tunable via procfs:
-	/proc/sys/kernel/sched_cfs_bandwidth_slice_us (default=5ms)
-
 Larger slice values will reduce transfer overheads, while smaller values allow
 for more fine-grained consumption.
 
+The per-group file cpu.cfs_slice_us controls the slice length within each CPU
+group, different groups could set different values for their own preference.
+Its default value is tunable via procfs:
+
+	/proc/sys/kernel/sched_cfs_bandwidth_slice_us (default=5ms)
+
+Note, updating this file does not automatically update existing groups using
+a default slice.
+
 Statistics
 ----------
 A group's bandwidth statistics are exported via 3 fields in cpu.stat.
kernel/sched/core.c +45 -0

63 lines not on this page · raw message

kernel/sched/fair.c +9 -8
index 54dc31e7ab9b..44b21e70a9b1 100644
@@ -4539,11 +4539,6 @@ static inline u64 default_cfs_period(void)
 	return 100000000ULL;
 }
 
-static inline u64 sched_cfs_bandwidth_slice(void)
-{
-	return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
-}
-
 /*
  * Replenish runtime according to assigned quota and update expiration time.
  * We use sched_clock_cpu directly instead of rq->clock to avoid adding
@@ -4577,6 +4572,11 @@ static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
 	return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
 }
 
+static inline u64 cfs_bandwidth_slice(struct cfs_bandwidth *cfs_b)
+{
+	return cfs_b->slice;
+}
+
 /* returns 0 on failure to allocate runtime */
 static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
 {
@@ -4585,7 +4585,7 @@ static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
 	u64 amount = 0, min_amount, expires;
 
 	/* note: this is a positive sum as runtime_remaining <= 0 */
-	min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
+	min_amount = cfs_bandwidth_slice(cfs_b) - cfs_rq->runtime_remaining;
 
 	raw_spin_lock(&cfs_b->lock);
 	if (cfs_b->quota == RUNTIME_INF)
@@ -5004,7 +5004,7 @@ static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
 		cfs_b->runtime += slack_runtime;
 
 		/* we are under rq->lock, defer unthrottling using a timer */
-		if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
+		if (cfs_b->runtime > cfs_bandwidth_slice(cfs_b) &&
 		    !list_empty(&cfs_b->throttled_cfs_rq))
 			start_cfs_slack_bandwidth(cfs_b);
 	}
@@ -5031,7 +5031,7 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  */
 static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
 {
-	u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
+	u64 runtime = 0, slice = cfs_bandwidth_slice(cfs_b);
 	u64 expires;
 
 	/* confirm we're still not at a refresh boundary */
@@ -5157,6 +5157,7 @@ void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
 	cfs_b->runtime = 0;
 	cfs_b->quota = RUNTIME_INF;
 	cfs_b->period = ns_to_ktime(default_cfs_period());
+	cfs_b->slice = sched_default_cfs_bandwidth_slice();
 
 	INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
 	hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
kernel/sched/sched.h +6 -0
index 15750c222ca2..204377652edb 100644
@@ -331,6 +331,7 @@ struct cfs_bandwidth {
 	raw_spinlock_t		lock;
 	ktime_t			period;
 	u64			quota;
+	u64			slice;
 	u64			runtime;
 	s64			hierarchical_quota;
 	u64			runtime_expires;
@@ -432,6 +433,11 @@ extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
 			struct sched_entity *parent);
 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
 
+static inline u64 sched_default_cfs_bandwidth_slice(void)
+{
+	return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
+}
+
 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
Signature · 1 line
2.13.0
maintainer
#1 lore raw
Quoted 5 lines · Cong Wang

Currently, the sched_cfs_bandwidth_slice_us is a global setting which affects all cgroups. Different groups may want different values based on their own workload, one size doesn't fit all. The global pool filled periodically is per cgroup too, they should have the right to distribute their own quota to each local CPU with their own frequency.

Why.. what happens? This doesn't really tell us anything.

author
#2 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Cong Wang

Currently, the sched_cfs_bandwidth_slice_us is a global setting which affects all cgroups. Different groups may want different values based on their own workload, one size doesn't fit all. The global pool filled periodically is per cgroup too, they should have the right to distribute their own quota to each local CPU with their own frequency.

Why.. what happens? This doesn't really tell us anything.

We saw tasks in a container got throttled for many times even when they don't apparently over-burn the CPU's. I tried to reduce the sched_cfs_bandwidth_slice_us from the default 5ms to 1ms, it solved the problem as no tasks got throttled after this change. This is why I want to change it.

And I don't think 1ms will be good for all containers, so in order to minimize the impact, I would like to keep the slice change within each container. This is why I propose this patch rather just sysctl -w. Do you think otherwise?

BTW, people reported a similar (if not same) issue here before:
https://gist.github.com/bobrik/2030ff040fad360327a5fab7a09c4ff1

Thanks!

maintainer
#3 lore raw
Quoted 8 lines · Cong Wang
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Cong Wang

Currently, the sched_cfs_bandwidth_slice_us is a global setting which affects all cgroups. Different groups may want different values based on their own workload, one size doesn't fit all. The global pool filled periodically is per cgroup too, they should have the right to distribute their own quota to each local CPU with their own frequency.

Quoted 7 lines
Quoted 1 line

Why.. what happens? This doesn't really tell us anything.

We saw tasks in a container got throttled for many times even when they don't apparently over-burn the CPU's. I tried to reduce the sched_cfs_bandwidth_slice_us from the default 5ms to 1ms, it solved the problem as no tasks got throttled after this change. This is why I want to change it.

The 1ms slice distributes time better at the cost of higher overhead, right?

Quoted 4 lines

And I don't think 1ms will be good for all containers, so in order to minimize the impact, I would like to keep the slice change within each container. This is why I propose this patch rather just sysctl -w. Do you think otherwise?

Well, I think I don't quite remember everything and a Changelog that tells me why you want stuff in a little more detail and helps me remember some things is a lot more useful than me having to go dig through the code myself (which I'll invariably postpone because I'm a busy sort of person).

Quoted 2 lines

BTW, people reported a similar (if not same) issue here before:
https://gist.github.com/bobrik/2030ff040fad360327a5fab7a09c4ff1

That's not a report, that's a random person on the interweb posting random crap. A report lands in my inbox.

author
#4 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Cong Wang
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Cong Wang

Currently, the sched_cfs_bandwidth_slice_us is a global setting which affects all cgroups. Different groups may want different values based on their own workload, one size doesn't fit all. The global pool filled periodically is per cgroup too, they should have the right to distribute their own quota to each local CPU with their own frequency.

Why.. what happens? This doesn't really tell us anything.

Quoted 8 lines
Quoted 5 lines

We saw tasks in a container got throttled for many times even when they don't apparently over-burn the CPU's. I tried to reduce the sched_cfs_bandwidth_slice_us from the default 5ms to 1ms, it solved the problem as no tasks got throttled after this change. This is why I want to change it.

The 1ms slice distributes time better at the cost of higher overhead, right?

Right, slightly higher. According to this paper [1]:

"While decreasing this value increases the frequency at which CPUs request for quota from the global pool, we did not notice any measurable impact on performance."

1. https://landley.net/kdocs/ols/2010/ols2010-pages-245-254.pdf

Quoted 11 lines
Quoted 5 lines

And I don't think 1ms will be good for all containers, so in order to minimize the impact, I would like to keep the slice change within each container. This is why I propose this patch rather just sysctl -w. Do you think otherwise?

Well, I think I don't quite remember everything and a Changelog that tells me why you want stuff in a little more detail and helps me remember some things is a lot more useful than me having to go dig through the code myself (which I'll invariably postpone because I'm a busy sort of person).

Sure, you just tell me how I should improve my changelog. I didn't realize I need to state why I need to change sched_cfs_bandwidth_slice_us since it is already a tunable. I will add it in v2.

Quoted 6 lines
Quoted 3 lines

BTW, people reported a similar (if not same) issue here before:
https://gist.github.com/bobrik/2030ff040fad360327a5fab7a09c4ff1

That's not a report, that's a random person on the interweb posting random crap. A report lands in my inbox.

Well, I found it via LKML:
https://marc.info/?l=linux-kernel&m=151270583632566&w=2

(I was too lazy to search for the original LKML link.)

I will update this patch to improve the changelog and address kbuild warnings, if you don't object.

Thanks.

#5 lore raw
In reply to Cong Wang
I love your patch! Perhaps something to improve:

Hi Cong,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tip/sched/core]
[also build test WARNING on v4.17-rc3 next-20180430]
[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/Cong-Wang/sched-fair-make-CFS-bandwidth-slice-per-cpu-group/20180501-064013
reproduce:

        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__

sparse warnings: (new ones prefixed by >>)

kernel/sched/core.c:509:9: sparse: incompatible types in comparison expression (different address spaces) kernel/sched/core.c:1614:17: sparse: incompatible types in comparison expression (different address spaces) kernel/sched/core.c:1802:27: sparse: incompatible types in comparison expression (different address spaces) kernel/sched/core.c:6541:11: sparse: symbol 'max_cfs_quota_period' was not declared. Should it be static? kernel/sched/core.c:6542:11: sparse: symbol 'min_cfs_quota_period' was not declared. Should it be static? kernel/sched/core.c:6622:5: sparse: symbol 'tg_set_cfs_quota' was not declared. Should it be static? kernel/sched/core.c:6635:6: sparse: symbol 'tg_get_cfs_quota' was not declared. Should it be static? kernel/sched/core.c:6648:5: sparse: symbol 'tg_set_cfs_period' was not declared. Should it be static? kernel/sched/core.c:6658:6: sparse: symbol 'tg_get_cfs_period' was not declared. Should it be static?

>> kernel/sched/core.c:6668:5: sparse: symbol 'tg_set_cfs_slice' was not declared. Should it be static?
>> kernel/sched/core.c:6686:6: sparse: symbol 'tg_get_cfs_slice' was not declared. Should it be static?

Please review and possibly fold the followup patch.

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

#6 lore raw

[RFC PATCH] sched/fair: tg_set_cfs_slice() can be static

In reply to Cong Wang
Fixes: 601aedfafd22 ("sched/fair: make CFS bandwidth slice per cpu group")
Fixes 601aedfafd22 ("sched/fair: make CFS bandwidth slice per cpu group")
---
 core.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/core.c +2 -2
index abc48d9..daa1843 100644
@@ -6665,7 +6665,7 @@ long tg_get_cfs_period(struct task_group *tg)
 	return cfs_period_us;
 }
 
-int tg_set_cfs_slice(struct task_group *tg, long cfs_slice_us)
+static int tg_set_cfs_slice(struct task_group *tg, long cfs_slice_us)
 {
 	u64 quota, slice;
 
@@ -6683,7 +6683,7 @@ int tg_set_cfs_slice(struct task_group *tg, long cfs_slice_us)
 	return 0;
 }
 
-long tg_get_cfs_slice(struct task_group *tg)
+static long tg_get_cfs_slice(struct task_group *tg)
 {
 	u64 slice_us;
 
#7 lore raw
In reply to Cong Wang
I love your patch! Yet something to improve:

Hi Cong,

I love your patch! Yet something to improve:

[auto build test ERROR on tip/sched/core]
[also build test ERROR on v4.17-rc3 next-20180430]
[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/Cong-Wang/sched-fair-make-CFS-bandwidth-slice-per-cpu-group/20180501-064013
config: i386-randconfig-a0-201817 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce:

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

All errors (new ones prefixed by >>):

In file included from kernel/sched/idle.c:8:0:
kernel/sched/sched.h: In function 'sched_default_cfs_bandwidth_slice':

>> kernel/sched/sched.h:438:14: error: 'sysctl_sched_cfs_bandwidth_slice' undeclared (first use in this function)
     return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
                 ^

kernel/sched/sched.h:438:14: note: each undeclared identifier is reported only once for each function it appears in

vim +/sysctl_sched_cfs_bandwidth_slice +438 kernel/sched/sched.h

435
436 static inline u64 sched_default_cfs_bandwidth_slice(void)
437 {

 > 438		return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;

439 }
440

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

#8 lore raw
In reply to Cong Wang
I love your patch! Yet something to improve:

Hi Cong,

I love your patch! Yet something to improve:

[auto build test ERROR on tip/sched/core]
[also build test ERROR on v4.17-rc3 next-20180430]
[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/Cong-Wang/sched-fair-make-CFS-bandwidth-slice-per-cpu-group/20180501-064013
config: i386-randconfig-x003-201817 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:

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

All errors (new ones prefixed by >>):

In file included from kernel/sched/debug.c:12:0:
kernel/sched/sched.h: In function 'sched_default_cfs_bandwidth_slice':

>> kernel/sched/sched.h:438:14: error: 'sysctl_sched_cfs_bandwidth_slice' undeclared (first use in this function); did you mean 'sched_default_cfs_bandwidth_slice'?
     return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                 sched_default_cfs_bandwidth_slice

kernel/sched/sched.h:438:14: note: each undeclared identifier is reported only once for each function it appears in

vim +438 kernel/sched/sched.h

435
436 static inline u64 sched_default_cfs_bandwidth_slice(void)
437 {

 > 438		return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;

439 }
440

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