Skip to contentSkip to conversation

Consider CPU idle state while choosing a new CPU

RFC In review Sebastian Andrzej Siewior 3 participants 17 messages Jan 30, 2013 – Feb 4, 2013

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

The scheduler does not consider the power states CPUs while selecting an idle one. So it might happen, that it puts load on a cpu which is in a deeper power state than another idle one, which has higher latency and power costs than pushing the load to the core which is not in a deep power saving state.

This patch series implements infrastructure which allows to consider the power state in the selection process of an idle cpu. The decision function is default off and can be enabled via a sysctl.

This is just the basic infrastructure. Further modifications are necessary to hand in proper state information from the various idle implementation. Its has been tested with a static testcase plugged into the cpuidle core.

Sebastian

#1 lore raw

RFC 1/2 cpuidle: trace state of the CPU

This patch adds an interface to cpuidle in order to retrieve the current idle state of a given CPU. Zero means the CPU is not in an idle state. Either because a cpuidle driver is not available or because the CPU is busy executing code. Values greater than zero the CPU indicate that the CPU is in some kind of idle state. The larger the value, the deeper the idle state.

---
 drivers/cpuidle/cpuidle.c |   13 ++++++++++++-
 include/linux/cpuidle.h   |    2 ++
 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +12 -1
index e1f6860..3594e0c 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+static DEFINE_PER_CPU(unsigned int, cpu_state);
 
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -40,13 +41,23 @@ void disable_cpuidle(void)
 	off = 1;
 }
 
+int cpuidle_get_state(int cpu)
+{
+	return per_cpu(cpu_state, cpu);
+}
+
 static int __cpuidle_register_device(struct cpuidle_device *dev);
 
 static inline int cpuidle_enter(struct cpuidle_device *dev,
 				struct cpuidle_driver *drv, int index)
 {
 	struct cpuidle_state *target_state = &drv->states[index];
-	return target_state->enter(dev, drv, index);
+	int ret;
+
+	per_cpu(cpu_state, smp_processor_id()) = index + 1;
+	ret = target_state->enter(dev, drv, index);
+	per_cpu(cpu_state, smp_processor_id()) = 0;
+	return ret;
 }
 
 static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
include/linux/cpuidle.h +2 -0
index 24cd1037..256baeb 100644
@@ -155,6 +155,7 @@ extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
 				struct cpuidle_driver *drv, int index,
 				int (*enter)(struct cpuidle_device *dev,
 					struct cpuidle_driver *drv, int index));
+extern int cpuidle_get_state(int cpu);
 extern int cpuidle_play_dead(void);
 
 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
@@ -186,6 +187,7 @@ static inline int cpuidle_wrap_enter(struct cpuidle_device *dev,
 				int (*enter)(struct cpuidle_device *dev,
 					struct cpuidle_driver *drv, int index))
 { return -ENODEV; }
+static inline int cpuidle_get_state(int cpu) {return 0; }
 static inline int cpuidle_play_dead(void) {return -ENODEV; }
 #endif
 
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [RFC 1/2] cpuidle: trace state of the CPU

Quoted 18 lines · Sebastian Andrzej Siewior

This patch adds an interface to cpuidle in order to retrieve the current idle state of a given CPU. Zero means the CPU is not in an idle state. Either because a cpuidle driver is not available or because the CPU is busy executing code. Values greater than zero the CPU indicate that the CPU is in some kind of idle state. The larger the value, the deeper the idle state.

---
 drivers/cpuidle/cpuidle.c |   13 ++++++++++++-
 include/linux/cpuidle.h   |    2 ++
 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +0 -0
index e1f6860..3594e0c 100644

drivers/cpuidle/cpuidle.c line 26

Quoted 4 lines
drivers/cpuidle/cpuidle.c +1 -0
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+static DEFINE_PER_CPU(unsigned int, cpu_state);

What about making it 'int' as cpuidle_get_state() returns int type?

Quoted 14 lines
drivers/cpuidle/cpuidle.c +5 -0
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -40,13 +41,23 @@ void disable_cpuidle(void)
 	off = 1;
 }
 
+int cpuidle_get_state(int cpu)
+{
+	return per_cpu(cpu_state, cpu);
+}
+
 static int __cpuidle_register_device(struct cpuidle_device *dev);
 

drivers/cpuidle/cpuidle.c void disable_cpuidle(void) · line 59

Quoted 10 lines
drivers/cpuidle/cpuidle.c +5 -1
 static inline int cpuidle_enter(struct cpuidle_device *dev,
 				struct cpuidle_driver *drv, int index)
 {
 	struct cpuidle_state *target_state = &drv->states[index];
-	return target_state->enter(dev, drv, index);
+	int ret;
+
+	per_cpu(cpu_state, smp_processor_id()) = index + 1;
+	ret = target_state->enter(dev, drv, index);
+	per_cpu(cpu_state, smp_processor_id()) = 0;

Maybe we can use local variable 'cpu' for this duplicated smp_processor_id() call as it's never executed on another cpu in between?

Thanks,
Namhyung

Quoted 23 lines
drivers/cpuidle/cpuidle.c +1 -0
+	return ret;
 }
 
 static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
include/linux/cpuidle.h +2 -0
index 24cd1037..256baeb 100644
@@ -155,6 +155,7 @@ extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
 				struct cpuidle_driver *drv, int index,
 				int (*enter)(struct cpuidle_device *dev,
 					struct cpuidle_driver *drv, int index));
+extern int cpuidle_get_state(int cpu);
 extern int cpuidle_play_dead(void);
 
 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
@@ -186,6 +187,7 @@ static inline int cpuidle_wrap_enter(struct cpuidle_device *dev,
 				int (*enter)(struct cpuidle_device *dev,
 					struct cpuidle_driver *drv, int index))
 { return -ENODEV; }
+static inline int cpuidle_get_state(int cpu) {return 0; }
 static inline int cpuidle_play_dead(void) {return -ENODEV; }
 #endif
#3 lore raw

Re: [RFC 1/2] cpuidle: trace state of the CPU

Quoted 11 lines · Namhyung Kim

drivers/cpuidle/cpuidle.c line 26

Quoted 9 lines
drivers/cpuidle/cpuidle.c +1 -0
index e1f6860..3594e0c 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+static DEFINE_PER_CPU(unsigned int, cpu_state);

What about making it 'int' as cpuidle_get_state() returns int type?

index should never get negative because it is an index. In case it does (which should not) we end up with a large value which is considered as a really deep state and the CPU will be ignored as long as possible.

Quoted 1 line
Quoted 1 line
drivers/cpuidle/cpuidle.c +0 -0
@@ -40,13 +41,23 @@ void disable_cpuidle(void)

…

Quoted 6 lines
Quoted 6 lines

static inline int cpuidle_enter(struct cpuidle_device *dev,

drivers/cpuidle/cpuidle.c +1 -1
 				struct cpuidle_driver *drv, int index)
 {
 	struct cpuidle_state *target_state = &drv->states[index];
-	return target_state->enter(dev, drv, index);
+	int ret;
Quoted 8 lines

drivers/cpuidle/cpuidle.c void disable_cpuidle(void) · line 59

Quoted 4 lines
drivers/cpuidle/cpuidle.c +4 -0
+
+	per_cpu(cpu_state, smp_processor_id()) = index + 1;
+	ret = target_state->enter(dev, drv, index);
+	per_cpu(cpu_state, smp_processor_id()) = 0;

Maybe we can use local variable 'cpu' for this duplicated smp_processor_id() call as it's never executed on another cpu in between?

This should be really cheap unless CONFIG_DEBUG_PREEMPT is on but okay, will change.

Quoted 2 lines

Thanks,
Namhyung

Sebastian

#4 lore raw

RFC 2/2 sched/fair: prefer a CPU in the "lowest" idle state

In reply to Sebastian Andrzej Siewior

If a new CPU has to be choosen for a task, then the scheduler first selects the group with the least load. This group is returned if its load is lower compared to the group to which the task is currently assigned. If there are several groups with completely idle CPU(s) (the CPU is in an idle state like C1) then the first group is returned.
This patch extends this decision by considering the idle state of CPU(s) in the group and the first group with a CPU in the lowest idle state wins (C1 is prefered over C2). If there is a CPU which is not in an idle state (C0) but has no tasks assigned then it is consider as a valid target. Should there be no CPU in an idle state at disposal then the loadavg is used as a fallback.

---
 include/linux/sched.h |    1 +
 kernel/sched/core.c   |    6 ++++--
 kernel/sched/fair.c   |   24 ++++++++++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
include/linux/sched.h +1 -0
index d211247..c2f6a44 100644
@@ -934,6 +934,7 @@ struct sched_domain {
 	unsigned int wake_idx;
 	unsigned int forkexec_idx;
 	unsigned int smt_gain;
+	unsigned int prefer_lp;
 	int flags;			/* See SD_* */
 	int level;
 
kernel/sched/core.c +4 -2
index 26058d0..fad16e6 100644
@@ -4971,7 +4971,7 @@ set_table_entry(struct ctl_table *entry,
 static struct ctl_table *
 sd_alloc_ctl_domain_table(struct sched_domain *sd)
 {
-	struct ctl_table *table = sd_alloc_ctl_entry(13);
+	struct ctl_table *table = sd_alloc_ctl_entry(14);
 
 	if (table == NULL)
 		return NULL;
@@ -5001,7 +5001,9 @@ sd_alloc_ctl_domain_table(struct sched_domain *sd)
 		sizeof(int), 0644, proc_dointvec_minmax, false);
 	set_table_entry(&table[11], "name", sd->name,
 		CORENAME_MAX_SIZE, 0444, proc_dostring, false);
-	/* &table[12] is terminator */
+	set_table_entry(&table[12], "prefer_lp", &sd->prefer_lp,
+		sizeof(int), 0644, proc_dointvec_minmax, false);
+	/* &table[13] is terminator */
 
 	return table;
 }
kernel/sched/fair.c +24 -0
index 5eea870..bff9800 100644
@@ -23,6 +23,7 @@
 #include <linux/latencytop.h>
 #include <linux/sched.h>
 #include <linux/cpumask.h>
+#include <linux/cpuidle.h>
 #include <linux/slab.h>
 #include <linux/profile.h>
 #include <linux/interrupt.h>
@@ -3181,8 +3182,10 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		  int this_cpu, int load_idx)
 {
 	struct sched_group *idlest = NULL, *group = sd->groups;
+	struct sched_group *idle_group = NULL;
 	unsigned long min_load = ULONG_MAX, this_load = 0;
 	int imbalance = 100 + (sd->imbalance_pct-100)/2;
+	int least_idle_cpu = INT_MAX;
 
 	do {
 		unsigned long load, avg_load;
@@ -3208,6 +3211,25 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 				load = target_load(i, load_idx);
 
 			avg_load += load;
+			if (!local_group && sd->prefer_lp && least_idle_cpu) {
+				int idle_level;
+
+				idle_level = cpuidle_get_state(i);
+				/*
+				 * Select the CPU which is in the lowest
+				 * possible power state. Take the active
+				 * CPU only if its run queue is empty.
+				 */
+				if (!idle_level) {
+					if (idle_cpu(i)) {
+						least_idle_cpu = idle_level;
+						idle_group = group;
+					}
+				} else if (least_idle_cpu > idle_level) {
+					least_idle_cpu = idle_level;
+					idle_group = group;
+				}
+			}
 		}
 
 		/* Adjust by relative CPU power of the group */
@@ -3221,6 +3243,8 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		}
 	} while (group = group->next, group != sd->groups);
 
+	if (idle_group)
+		return idle_group;
 	if (!idlest || 100*this_load < imbalance*min_load)
 		return NULL;
 	return idlest;
Signature · 1 line
1.7.10.4
#5 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 105 lines · Sebastian Andrzej Siewior

If a new CPU has to be choosen for a task, then the scheduler first selects the group with the least load. This group is returned if its load is lower compared to the group to which the task is currently assigned. If there are several groups with completely idle CPU(s) (the CPU is in an idle state like C1) then the first group is returned.
This patch extends this decision by considering the idle state of CPU(s) in the group and the first group with a CPU in the lowest idle state wins (C1 is prefered over C2). If there is a CPU which is not in an idle state (C0) but has no tasks assigned then it is consider as a valid target. Should there be no CPU in an idle state at disposal then the loadavg is used as a fallback.

---
 include/linux/sched.h |    1 +
 kernel/sched/core.c   |    6 ++++--
 kernel/sched/fair.c   |   24 ++++++++++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
include/linux/sched.h +1 -0
index d211247..c2f6a44 100644
@@ -934,6 +934,7 @@ struct sched_domain {
 	unsigned int wake_idx;
 	unsigned int forkexec_idx;
 	unsigned int smt_gain;
+	unsigned int prefer_lp;
 	int flags;			/* See SD_* */
 	int level;
 
kernel/sched/core.c +4 -2
index 26058d0..fad16e6 100644
@@ -4971,7 +4971,7 @@ set_table_entry(struct ctl_table *entry,
 static struct ctl_table *
 sd_alloc_ctl_domain_table(struct sched_domain *sd)
 {
-	struct ctl_table *table = sd_alloc_ctl_entry(13);
+	struct ctl_table *table = sd_alloc_ctl_entry(14);
 
 	if (table == NULL)
 		return NULL;
@@ -5001,7 +5001,9 @@ sd_alloc_ctl_domain_table(struct sched_domain *sd)
 		sizeof(int), 0644, proc_dointvec_minmax, false);
 	set_table_entry(&table[11], "name", sd->name,
 		CORENAME_MAX_SIZE, 0444, proc_dostring, false);
-	/* &table[12] is terminator */
+	set_table_entry(&table[12], "prefer_lp", &sd->prefer_lp,
+		sizeof(int), 0644, proc_dointvec_minmax, false);
+	/* &table[13] is terminator */
 
 	return table;
 }
kernel/sched/fair.c +22 -0
index 5eea870..bff9800 100644
@@ -23,6 +23,7 @@
 #include <linux/latencytop.h>
 #include <linux/sched.h>
 #include <linux/cpumask.h>
+#include <linux/cpuidle.h>
 #include <linux/slab.h>
 #include <linux/profile.h>
 #include <linux/interrupt.h>
@@ -3181,8 +3182,10 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		  int this_cpu, int load_idx)
 {
 	struct sched_group *idlest = NULL, *group = sd->groups;
+	struct sched_group *idle_group = NULL;
 	unsigned long min_load = ULONG_MAX, this_load = 0;
 	int imbalance = 100 + (sd->imbalance_pct-100)/2;
+	int least_idle_cpu = INT_MAX;
 
 	do {
 		unsigned long load, avg_load;
@@ -3208,6 +3211,25 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 				load = target_load(i, load_idx);
 
 			avg_load += load;
+			if (!local_group && sd->prefer_lp && least_idle_cpu) {
+				int idle_level;
+
+				idle_level = cpuidle_get_state(i);
+				/*
+				 * Select the CPU which is in the lowest
+				 * possible power state. Take the active
+				 * CPU only if its run queue is empty.
+				 */
+				if (!idle_level) {
+					if (idle_cpu(i)) {
+						least_idle_cpu = idle_level;
+						idle_group = group;
+					}
+				} else if (least_idle_cpu > idle_level) {
+					least_idle_cpu = idle_level;
+					idle_group = group;
+				}
+			}
 		}
 
 		/* Adjust by relative CPU power of the group */

kernel/sched/fair.c find_idlest_group(struct sched_domain *sd, struct task_struct *p, · line 3247

Quoted 5 lines
kernel/sched/fair.c +2 -0
@@ -3221,6 +3243,8 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		}
 	} while (group = group->next, group != sd->groups);
 
+	if (idle_group)
+		return idle_group;

Hi, Sebastian

I'm not sure, but just concern about this case:

	group 0		cpu 0			cpu 1
			least idle		4 task

	group 1		cpu 2			cpu 3
			1 task			1 task

The previous logical will pick group 1 and now it will take group 0, and that cause more imbalance, doesn't it?

May be check that state in find_idlest_cpu() will be better?

Regards,
Michael Wang

Quoted 4 lines
kernel/sched/fair.c +0 -0
 	if (!idlest || 100*this_load < imbalance*min_load)
 		return NULL;
 	return idlest;
#6 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Hi Sebastian and Michael,

Quoted 12 lines · Michael Wang
Quoted 11 lines · Sebastian Andrzej Siewior

If a new CPU has to be choosen for a task, then the scheduler first selects the group with the least load. This group is returned if its load is lower compared to the group to which the task is currently assigned. If there are several groups with completely idle CPU(s) (the CPU is in an idle state like C1) then the first group is returned.
This patch extends this decision by considering the idle state of CPU(s) in the group and the first group with a CPU in the lowest idle state wins (C1 is prefered over C2). If there is a CPU which is not in an idle state (C0) but has no tasks assigned then it is consider as a valid target. Should there be no CPU in an idle state at disposal then the loadavg is used as a fallback.

[snip]

Quoted 52 lines
Quoted 38 lines
kernel/sched/fair.c +21 -0
@@ -3181,8 +3182,10 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		  int this_cpu, int load_idx)
 {
 	struct sched_group *idlest = NULL, *group = sd->groups;
+	struct sched_group *idle_group = NULL;
 	unsigned long min_load = ULONG_MAX, this_load = 0;
 	int imbalance = 100 + (sd->imbalance_pct-100)/2;
+	int least_idle_cpu = INT_MAX;
 
 	do {
 		unsigned long load, avg_load;
@@ -3208,6 +3211,25 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 				load = target_load(i, load_idx);
 
 			avg_load += load;
+			if (!local_group && sd->prefer_lp && least_idle_cpu) {
+				int idle_level;
+
+				idle_level = cpuidle_get_state(i);
+				/*
+				 * Select the CPU which is in the lowest
+				 * possible power state. Take the active
+				 * CPU only if its run queue is empty.
+				 */
+				if (!idle_level) {
+					if (idle_cpu(i)) {
+						least_idle_cpu = idle_level;
+						idle_group = group;
+					}
+				} else if (least_idle_cpu > idle_level) {
+					least_idle_cpu = idle_level;
+					idle_group = group;
+				}
+			}
 		}
 
 		/* Adjust by relative CPU power of the group */

kernel/sched/fair.c find_idlest_group(struct sched_domain *sd, struct task_struct *p, · line 3247

Quoted 5 lines
kernel/sched/fair.c +2 -0
@@ -3221,6 +3243,8 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		}
 	} while (group = group->next, group != sd->groups);
 
+	if (idle_group)
+		return idle_group;

I'm not sure, but just concern about this case:

	group 0		cpu 0			cpu 1
			least idle		4 task

	group 1		cpu 2			cpu 3
			1 task			1 task
Quoted 4 lines

The previous logical will pick group 1 and now it will take group 0, and that cause more imbalance, doesn't it?

May be check that state in find_idlest_cpu() will be better?

Right, at least find_idlest_cpu() should also check the idle_level IMHO.

Anyway, I have an idea with this in mind. It's like adding a new "idle load" to each idle cpu rather than special casing the idle cpus like above. IOW an idle cpu will get very small load weight depends on how deep it's slept so that it can be compared to other cpus in a same way but we can find prefered (lowest load) cpu among the idle cpus.

The simple way I can think of is adding idle_level to a rq load in weighted_cpuload():

static unsigned long weighted_cpuload(const int cpu)

{
	return cpu_rq(cpu)->load.weight + cpuidle_get_state(cpu);
}

What do you think?

Thanks,
Namhyung

#7 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 81 lines · Namhyung Kim

Hi Sebastian and Michael,

Quoted 12 lines · Michael Wang
Quoted 11 lines · Sebastian Andrzej Siewior

If a new CPU has to be choosen for a task, then the scheduler first selects the group with the least load. This group is returned if its load is lower compared to the group to which the task is currently assigned. If there are several groups with completely idle CPU(s) (the CPU is in an idle state like C1) then the first group is returned.
This patch extends this decision by considering the idle state of CPU(s) in the group and the first group with a CPU in the lowest idle state wins (C1 is prefered over C2). If there is a CPU which is not in an idle state (C0) but has no tasks assigned then it is consider as a valid target. Should there be no CPU in an idle state at disposal then the loadavg is used as a fallback.

[snip]

Quoted 52 lines
Quoted 38 lines
kernel/sched/fair.c +21 -0
@@ -3181,8 +3182,10 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		  int this_cpu, int load_idx)
 {
 	struct sched_group *idlest = NULL, *group = sd->groups;
+	struct sched_group *idle_group = NULL;
 	unsigned long min_load = ULONG_MAX, this_load = 0;
 	int imbalance = 100 + (sd->imbalance_pct-100)/2;
+	int least_idle_cpu = INT_MAX;
 
 	do {
 		unsigned long load, avg_load;
@@ -3208,6 +3211,25 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 				load = target_load(i, load_idx);
 
 			avg_load += load;
+			if (!local_group && sd->prefer_lp && least_idle_cpu) {
+				int idle_level;
+
+				idle_level = cpuidle_get_state(i);
+				/*
+				 * Select the CPU which is in the lowest
+				 * possible power state. Take the active
+				 * CPU only if its run queue is empty.
+				 */
+				if (!idle_level) {
+					if (idle_cpu(i)) {
+						least_idle_cpu = idle_level;
+						idle_group = group;
+					}
+				} else if (least_idle_cpu > idle_level) {
+					least_idle_cpu = idle_level;
+					idle_group = group;
+				}
+			}
 		}
 
 		/* Adjust by relative CPU power of the group */

kernel/sched/fair.c find_idlest_group(struct sched_domain *sd, struct task_struct *p, · line 3247

Quoted 5 lines
kernel/sched/fair.c +2 -0
@@ -3221,6 +3243,8 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
 		}
 	} while (group = group->next, group != sd->groups);
 
+	if (idle_group)
+		return idle_group;

I'm not sure, but just concern about this case:

	group 0		cpu 0			cpu 1
			least idle		4 task

	group 1		cpu 2			cpu 3
			1 task			1 task
Quoted 4 lines

The previous logical will pick group 1 and now it will take group 0, and that cause more imbalance, doesn't it?

May be check that state in find_idlest_cpu() will be better?

Right, at least find_idlest_cpu() should also check the idle_level IMHO.

Anyway, I have an idea with this in mind. It's like adding a new "idle load" to each idle cpu rather than special casing the idle cpus like above. IOW an idle cpu will get very small load weight depends on how deep it's slept so that it can be compared to other cpus in a same way but we can find prefered (lowest load) cpu among the idle cpus.

Quoted 7 lines

The simple way I can think of is adding idle_level to a rq load in weighted_cpuload():

static unsigned long weighted_cpuload(const int cpu)

{
	return cpu_rq(cpu)->load.weight + cpuidle_get_state(cpu);
}

Hmm... then we don't need changes in find_idlest_cpu(), just compare the load as before, but it works only when the appendix state value is smaller than the lowest load of one task, which is 15 currently, I'm not sure whether we have the promise...

Regards,
Michael Wang

Quoted 11 lines

What do you think?

Thanks,
Namhyung
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#8 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 15 lines · Michael Wang
Quoted 6 lines · Namhyung Kim

Anyway, I have an idea with this in mind. It's like adding a new "idle load" to each idle cpu rather than special casing the idle cpus like above. IOW an idle cpu will get very small load weight depends on how deep it's slept so that it can be compared to other cpus in a same way but we can find prefered (lowest load) cpu among the idle cpus.

Quoted 7 lines

The simple way I can think of is adding idle_level to a rq load in weighted_cpuload():

static unsigned long weighted_cpuload(const int cpu)

{
	return cpu_rq(cpu)->load.weight + cpuidle_get_state(cpu);
}
Quoted 4 lines

Hmm... then we don't need changes in find_idlest_cpu(), just compare the load as before, but it works only when the appendix state value is smaller than the lowest load of one task, which is 15 currently, I'm not sure whether we have the promise...

You said about a nice 19 process, right? But I found that SCHED_IDLE task will have weight of 3. :(

  #define WEIGHT_IDLEPRIO                3

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

Thanks,
Namhyung

#9 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 21 lines · Namhyung Kim
Quoted 15 lines · Michael Wang
Quoted 6 lines · Namhyung Kim

Anyway, I have an idea with this in mind. It's like adding a new "idle load" to each idle cpu rather than special casing the idle cpus like above. IOW an idle cpu will get very small load weight depends on how deep it's slept so that it can be compared to other cpus in a same way but we can find prefered (lowest load) cpu among the idle cpus.

Quoted 7 lines

The simple way I can think of is adding idle_level to a rq load in weighted_cpuload():

static unsigned long weighted_cpuload(const int cpu)

{
	return cpu_rq(cpu)->load.weight + cpuidle_get_state(cpu);
}
Quoted 4 lines

Hmm... then we don't need changes in find_idlest_cpu(), just compare the load as before, but it works only when the appendix state value is smaller than the lowest load of one task, which is 15 currently, I'm not sure whether we have the promise...

Quoted 4 lines

You said about a nice 19 process, right? But I found that SCHED_IDLE task will have weight of 3. :(

  #define WEIGHT_IDLEPRIO                3

I missed that policy :)

Quoted 4 lines

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

And I just got another case we should take care:

	group 0		cpu 0			cpu 1
			power index 8		power index 8


	group 1		cpu 2			cpu 3
			power index 0		load 15

so load of group 0 is 16 and group 1 is 15, but group 0 is better...

Regards,
Michael Wang

Quoted 4 lines

Thanks,
Namhyung

#10 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 6 lines · Michael Wang
Quoted 2 lines · Namhyung Kim

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

And I just got another case we should take care:

Quoted 8 lines
	group 0		cpu 0			cpu 1
			power index 8		power index 8


	group 1		cpu 2			cpu 3
			power index 0		load 15

so load of group 0 is 16 and group 1 is 15, but group 0 is better...

Maybe it's not. The cpus in group 0 are in a lower power state so that there will be a benefit to select cpu 2 from the power' PoV IMHO. Also such a low power state has a longer exit latency so that we should choose cpu2 to get a better performance and it's the basic idea of this patchset I believe.

Thanks,
Namhyung

#11 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 14 lines · Namhyung Kim
Quoted 13 lines · Michael Wang
Quoted 2 lines · Namhyung Kim

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

And I just got another case we should take care:

	group 0		cpu 0			cpu 1
			power index 8		power index 8


	group 1		cpu 2			cpu 3
			power index 0		load 15
Quoted 7 lines
Quoted 1 line

so load of group 0 is 16 and group 1 is 15, but group 0 is better...

Maybe it's not. The cpus in group 0 are in a lower power state so that there will be a benefit to select cpu 2 from the power' PoV IMHO. Also such a low power state has a longer exit latency so that we should choose cpu2 to get a better performance and it's the basic idea of this patchset I believe.

Well, this case is just to notify that, we may face the comparison between load and index, not between index and index, I just doubt there won't be a rule which could take care both, besides, comparison between load and index is strange...

Regards,
Michael Wang

Quoted 9 lines

Thanks,
Namhyung
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#12 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 23 lines · Michael Wang
Quoted 14 lines · Namhyung Kim
Quoted 13 lines · Michael Wang
Quoted 2 lines · Namhyung Kim

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

And I just got another case we should take care:

	group 0		cpu 0			cpu 1
			power index 8		power index 8


	group 1		cpu 2			cpu 3
			power index 0		load 15
Quoted 7 lines
Quoted 1 line

so load of group 0 is 16 and group 1 is 15, but group 0 is better...

Maybe it's not. The cpus in group 0 are in a lower power state so that there will be a benefit to select cpu 2 from the power' PoV IMHO. Also such a low power state has a longer exit latency so that we should choose cpu2 to get a better performance and it's the basic idea of this patchset I believe.

Quoted 4 lines

Well, this case is just to notify that, we may face the comparison between load and index, not between index and index, I just doubt there won't be a rule which could take care both, besides, comparison between load and index is strange...

Oh, I miss the point that you call it 'idle load', hmm...may be it could works, if we could scale the current load number, then we will have more 'space' for 'idle load'.

Regards,
Michael Wang

Quoted 14 lines

Regards,
Michael Wang

Quoted 9 lines

Thanks,
Namhyung
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#13 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 24 lines · Michael Wang
Quoted 23 lines · Michael Wang
Quoted 21 lines · Namhyung Kim
Quoted 6 lines · Michael Wang
Quoted 2 lines · Namhyung Kim

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

And I just got another case we should take care:

Quoted 8 lines
	group 0		cpu 0			cpu 1
			power index 8		power index 8


	group 1		cpu 2			cpu 3
			power index 0		load 15

so load of group 0 is 16 and group 1 is 15, but group 0 is better...

Maybe it's not. The cpus in group 0 are in a lower power state so that there will be a benefit to select cpu 2 from the power' PoV IMHO. Also such a low power state has a longer exit latency so that we should choose cpu2 to get a better performance and it's the basic idea of this patchset I believe.

Quoted 8 lines
Quoted 4 lines

Well, this case is just to notify that, we may face the comparison between load and index, not between index and index, I just doubt there won't be a rule which could take care both, besides, comparison between load and index is strange...

Oh, I miss the point that you call it 'idle load', hmm...may be it could works, if we could scale the current load number, then we will have more 'space' for 'idle load'.

And some tips here:

/*
* Increase resolution of nice-level calculations for 64-bit architectures.
* The extra resolution improves shares distribution and load balancing of
* low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
* hierarchies, especially on larger systems. This is not a user-visible change
* and does not change the user-interface for setting shares/weights.
*
* We increase resolution only if we have enough bits to allow this increased
* resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
* when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
* increased costs.
*/
#if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */

# define SCHED_LOAD_RESOLUTION  10
# define scale_load(w)          ((w) << SCHED_LOAD_RESOLUTION)
# define scale_load_down(w)     ((w) >> SCHED_LOAD_RESOLUTION)

It mentioned some regressions, that's the history but sounds like a lot of testing is needed.

Regards,
Michael Wang

Quoted 25 lines

Regards,
Michael Wang

Quoted 14 lines

Regards,
Michael Wang

Quoted 9 lines

Thanks,
Namhyung
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#14 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Hi Michael,

Quoted 49 lines · Michael Wang
Quoted 24 lines · Michael Wang
Quoted 23 lines · Michael Wang
Quoted 21 lines · Namhyung Kim
Quoted 6 lines · Michael Wang
Quoted 2 lines · Namhyung Kim

But AFAIK the number of states in cpuidle is usually less than 10 so maybe we can change the weight then, but there's no promise...

And I just got another case we should take care:

Quoted 8 lines
	group 0		cpu 0			cpu 1
			power index 8		power index 8


	group 1		cpu 2			cpu 3
			power index 0		load 15

so load of group 0 is 16 and group 1 is 15, but group 0 is better...

Maybe it's not. The cpus in group 0 are in a lower power state so that there will be a benefit to select cpu 2 from the power' PoV IMHO. Also such a low power state has a longer exit latency so that we should choose cpu2 to get a better performance and it's the basic idea of this patchset I believe.

Quoted 8 lines
Quoted 4 lines

Well, this case is just to notify that, we may face the comparison between load and index, not between index and index, I just doubt there won't be a rule which could take care both, besides, comparison between load and index is strange...

Oh, I miss the point that you call it 'idle load', hmm...may be it could works, if we could scale the current load number, then we will have more 'space' for 'idle load'.

And some tips here:

/*
* Increase resolution of nice-level calculations for 64-bit architectures.
* The extra resolution improves shares distribution and load balancing of
* low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
* hierarchies, especially on larger systems. This is not a user-visible change
* and does not change the user-interface for setting shares/weights.
*
* We increase resolution only if we have enough bits to allow this increased
* resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
* when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
* increased costs.
*/
#if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */

Quoted 6 lines
# define SCHED_LOAD_RESOLUTION  10
# define scale_load(w)          ((w) << SCHED_LOAD_RESOLUTION)
# define scale_load_down(w)     ((w) >> SCHED_LOAD_RESOLUTION)

It mentioned some regressions, that's the history but sounds like a lot of testing is needed.

Thanks for the tip. Yes, I was aware of this and bit worried about the mentioned regression. So I just wanted to increate the load weight of a SCHED_IDLE task.

However I don't have a good test environment unfortunately so cannot carry the work on at least for a while.

Thanks for your help anyway,
Namhyung

#15 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

In reply to Michael Wang
Quoted 10 lines · Michael Wang

I'm not sure, but just concern about this case:

	group 0		cpu 0			cpu 1
			least idle		4 task

	group 1		cpu 2			cpu 3
			1 task			1 task

The previous logical will pick group 1 and now it will take group 0, and that cause more imbalance, doesn't it?

That depends on load of CPU 0 + 1 vs CPU 2 + 3. If the four tasks on CPU1 are idle then the previous code should return group 0. If the four tasks are running at 100% each then two of them should be migrated to CPU0 and this point the idle state does not matter :)

Quoted 1 line

May be check that state in find_idlest_cpu() will be better?

You say to move this from find_idlest_group() to find_idlest_cpu()?

Quoted 2 lines

Regards,
Michael Wang

Sebastian

#16 lore raw

Re: [RFC 2/2] sched/fair: prefer a CPU in the "lowest" idle state

Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Michael Wang

I'm not sure, but just concern about this case:

	group 0		cpu 0			cpu 1
			least idle		4 task

	group 1		cpu 2			cpu 3
			1 task			1 task
Quoted 7 lines
Quoted 2 lines

The previous logical will pick group 1 and now it will take group 0, and that cause more imbalance, doesn't it?

That depends on load of CPU 0 + 1 vs CPU 2 + 3. If the four tasks on CPU1 are idle then the previous code should return group 0. If the four tasks are running at 100% each then two of them should be migrated to CPU0 and this point the idle state does not matter :)

Hmm...may be I should make it more clear like this:

Prev find_idlest_group():

cpu 0 is the least idle
cpu 1 has 4 tasks on it's running queue cpu 2 has 1 task(current task) on it's running queue cpu 3 has 1 task on it's running queue

and suppose no changes happen during the search, and this sd only contain 2 groups:

group 0 has cpu 0 and 1
group 1 has cpu 2 and 3

So in the old world, group 0 has load 4096 (if all the task are nice 0, and let's put down the revise), group 1 has load 2048, so find_idlest_group() will return group 1 since it's the idlest.

But now, since we directly using the idle group, that will be group 0, and after applied, group 0 will has 5120 load while group 1 only has 2048, and that's cause more imbalance (than 4096 : 3072).

That's just flash in my mind when I saw the patch, may be not a good case or missed some thing, but since find_idlest_group() is trying to balance the load, if we want to override the rule, we need proof by logical or benchmarks.

Quoted 4 lines
Quoted 2 lines

May be check that state in find_idlest_cpu() will be better?

You say to move this from find_idlest_group() to find_idlest_cpu()?

Yes, since we already make sure the balance by find_idlest_group(), we only need to add some check like below in find_idlest_cpu():

if (load < min_load || (load == min_load && i == this_cpu)) {
	if (power state of 'idlest' < power state of 'i')
		continue;

	min_load = load;
	idlest = i;
}

That will get very limited benefit (only the case when there are multiple different power state idle cpu in the group), but is very easy to be proved by logical, doesn't it?

And Namhyung mentioned some interesting implementation which may need no changes to the code in select, please take a look :)

Regards,
Michael Wang

Quoted 11 lines
Quoted 3 lines

Regards,
Michael Wang

Sebastian
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/