Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:pzskc383
kernel
linux-2.6.33-sched-ilb-logic.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File linux-2.6.33-sched-ilb-logic.patch of Package kernel
From: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> Subject: [PATCH] sched: (v7) Change nohz ilb logic from pull to push model Patch-mainline: 2.6.35? In the new push model, all idle CPUs indeed go into nohz mode. There is still the concept of idle load balancer. Busy CPU kicks the nohz balancer when any of the nohz CPUs need idle load balancing. The kickee CPU does the idle load balancing on behalf of all idle CPUs instead of the normal idle balance. This addresses two problems with the current nohz ilb logic * the balancer will continue to have periodic ticks and wakeup frequently, even though it may not have any rebalancing to do on behalf of any of the idle CPUs. * On x86 and CPUs that have APIC timer stoppage on idle CPUs, this periodic wakeup can result in an additional interrupt on a CPU doing the timer broadcast. Signed-off-by: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com> --- kernel/sched.c | 241 +++++++++++++++++++++++++++++++++----------------------- 1 files changed, 141 insertions(+), 100 deletions(-) diff --git a/kernel/sched.c b/kernel/sched.c index 4508fe7..986cf14 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -535,7 +535,7 @@ struct rq { #define CPU_LOAD_IDX_MAX 5 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; #ifdef CONFIG_NO_HZ - unsigned char in_nohz_recently; + unsigned char nohz_balance_kick; #endif /* capture load from *all* tasks on this cpu: */ struct load_weight load; @@ -628,6 +628,8 @@ struct rq { #endif }; +static void nohz_idle_balance(int this_cpu, struct rq *this_rq); + static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); static inline @@ -4456,6 +4458,12 @@ static void idle_balance(int this_cpu, struct rq *this_rq) if (this_rq->avg_idle < sysctl_sched_migration_cost) return; + if (this_rq->nohz_balance_kick) { + raw_spin_unlock_irq(&this_rq->lock); + nohz_idle_balance(this_cpu, this_rq); + raw_spin_lock_irq(&this_rq->lock); + return; + } for_each_domain(this_cpu, sd) { unsigned long interval; @@ -4536,10 +4544,24 @@ static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) } #ifdef CONFIG_NO_HZ + +/* + * idle load balancing details + * - One of the idle CPUs nominates itself as idle load_balancer, while + * entering idle. + * - This idle load balancer CPU will also go into tickless mode when + * it is idle, just like all other idle CPUs + * - When one of the busy CPUs notice that there may be an idle rebalancing + * needed, they will kick the idle load balancer, which then does idle + * load balancing for all the idle CPUs. + */ static struct { atomic_t load_balancer; - cpumask_var_t cpu_mask; + atomic_t first_pick_cpu; + atomic_t second_pick_cpu; + cpumask_var_t idle_cpus_mask; cpumask_var_t ilb_grp_nohz_mask; + unsigned long next_balance; /* in jiffy units */ } nohz ____cacheline_aligned = { .load_balancer = ATOMIC_INIT(-1), }; @@ -4596,7 +4618,7 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) */ static inline int is_semi_idle_group(struct sched_group *ilb_group) { - cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask, + cpumask_and(nohz.ilb_grp_nohz_mask, nohz.idle_cpus_mask, sched_group_cpus(ilb_group)); /* @@ -4639,7 +4661,7 @@ static int find_new_ilb(int cpu) * Optimize for the case when we have no idle CPUs or only one * idle CPU. Don't walk the sched_domain hierarchy in such cases */ - if (cpumask_weight(nohz.cpu_mask) < 2) + if (cpumask_weight(nohz.idle_cpus_mask) < 2) goto out_done; for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) { @@ -4655,42 +4677,58 @@ static int find_new_ilb(int cpu) } out_done: - return cpumask_first(nohz.cpu_mask); + return nr_cpu_ids; } #else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */ static inline int find_new_ilb(int call_cpu) { - return cpumask_first(nohz.cpu_mask); + return nr_cpu_ids; } #endif /* + * Kick a CPU to do the nohz balancing, when needed. We pick the + * nohz_load_balancer CPU (if there is one) or fallback to any idle + * CPU (if there is one). + */ +static void nohz_balancer_kick(int cpu) +{ + int ilb_cpu; + + nohz.next_balance++; + + ilb_cpu = get_nohz_load_balancer(); + if (ilb_cpu < 0) { + ilb_cpu = cpumask_first(nohz.idle_cpus_mask); + if (ilb_cpu >= nr_cpu_ids) + return; + } + + if (!cpu_rq(ilb_cpu)->nohz_balance_kick) { + cpu_rq(ilb_cpu)->nohz_balance_kick = 1; + resched_cpu(ilb_cpu); + } + return; +} + +/* * This routine will try to nominate the ilb (idle load balancing) * owner among the cpus whose ticks are stopped. ilb owner will do the idle - * load balancing on behalf of all those cpus. If all the cpus in the system - * go into this tickless mode, then there will be no ilb owner (as there is - * no need for one) and all the cpus will sleep till the next wakeup event - * arrives... + * load balancing on behalf of all those cpus. * - * For the ilb owner, tick is not stopped. And this tick will be used - * for idle load balancing. ilb owner will still be part of - * nohz.cpu_mask.. + * When the ilb owner becomes busy, we will not have new ilb owner until some + * idle CPU wakes up and goes back to idle or some busy CPU tries to kick + * idle load balancing by kicking one of the idle CPUs. * - * While stopping the tick, this cpu will become the ilb owner if there - * is no other owner. And will be the owner till that cpu becomes busy - * or if all cpus in the system stop their ticks at which point - * there is no need for ilb owner. - * - * When the ilb owner becomes busy, it nominates another owner, during the - * next busy scheduler_tick() + * Ticks are stopped for the ilb owner as well, with busy CPU kicking this + * ilb owner CPU in future (when there is a need for idle load balancing on + * behalf of all idle CPUs). */ int select_nohz_load_balancer(int stop_tick) { int cpu = smp_processor_id(); if (stop_tick) { - cpu_rq(cpu)->in_nohz_recently = 1; - if (!cpu_active(cpu)) { if (atomic_read(&nohz.load_balancer) != cpu) return 0; @@ -4705,25 +4743,18 @@ int select_nohz_load_balancer(int stop_tick) return 0; } - cpumask_set_cpu(cpu, nohz.cpu_mask); - - /* time for ilb owner also to sleep */ - if (cpumask_weight(nohz.cpu_mask) == num_active_cpus()) { - if (atomic_read(&nohz.load_balancer) == cpu) - atomic_set(&nohz.load_balancer, -1); - return 0; - } + this_rq()->idle_at_tick = 1; + cpumask_set_cpu(cpu, nohz.idle_cpus_mask); + atomic_cmpxchg(&nohz.first_pick_cpu, cpu, nr_cpu_ids); + atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids); if (atomic_read(&nohz.load_balancer) == -1) { - /* make me the ilb owner */ - if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) - return 1; - } else if (atomic_read(&nohz.load_balancer) == cpu) { int new_ilb; - if (!(sched_smt_power_savings || - sched_mc_power_savings)) - return 1; + /* make me the ilb owner */ + if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) != -1) + return 0; + /* * Check to see if there is a more power-efficient * ilb. @@ -4734,13 +4765,14 @@ int select_nohz_load_balancer(int stop_tick) resched_cpu(new_ilb); return 0; } - return 1; + return 0; } } else { - if (!cpumask_test_cpu(cpu, nohz.cpu_mask)) + if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) return 0; - cpumask_clear_cpu(cpu, nohz.cpu_mask); + this_rq()->idle_at_tick = 0; + cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); if (atomic_read(&nohz.load_balancer) == cpu) if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) @@ -4828,35 +4860,23 @@ out: rq->next_balance = next_balance; } +#ifdef CONFIG_NO_HZ /* - * run_rebalance_domains is triggered when needed from the scheduler tick. - * In CONFIG_NO_HZ case, the idle load balance owner will do the + * In CONFIG_NO_HZ case, the idle balance kickee will do the * rebalancing for all the cpus for whom scheduler ticks are stopped. */ -static void run_rebalance_domains(struct softirq_action *h) +static void nohz_idle_balance(int this_cpu, struct rq *this_rq) { - int this_cpu = smp_processor_id(); - struct rq *this_rq = cpu_rq(this_cpu); - enum cpu_idle_type idle = this_rq->idle_at_tick ? - CPU_IDLE : CPU_NOT_IDLE; - - rebalance_domains(this_cpu, idle); - -#ifdef CONFIG_NO_HZ /* - * If this cpu is the owner for idle load balancing, then do the + * If this cpu has a pending nohz_balance_kick, then do the * balancing on behalf of the other idle cpus whose ticks are * stopped. */ - if (this_rq->idle_at_tick && - atomic_read(&nohz.load_balancer) == this_cpu) { + if (this_rq->nohz_balance_kick) { struct rq *rq; int balance_cpu; - for_each_cpu(balance_cpu, nohz.cpu_mask) { - if (balance_cpu == this_cpu) - continue; - + for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { /* * If this cpu gets work to do, stop the load balancing * work being done for other cpus. Next load @@ -4871,8 +4891,26 @@ static void run_rebalance_domains(struct softirq_action *h) if (time_after(this_rq->next_balance, rq->next_balance)) this_rq->next_balance = rq->next_balance; } + nohz.next_balance = this_rq->next_balance; + this_rq->nohz_balance_kick = 0; } +} +#else +static void nohz_idle_balance(int this_cpu, struct rq *this_rq) { } #endif + +/* + * run_rebalance_domains is triggered when needed from the scheduler tick. + * Also triggered for nohz idle balancing (with nohz_balancing_kick set). + */ +static void run_rebalance_domains(struct softirq_action *h) +{ + int this_cpu = smp_processor_id(); + struct rq *this_rq = cpu_rq(this_cpu); + enum cpu_idle_type idle = this_rq->idle_at_tick ? + CPU_IDLE : CPU_NOT_IDLE; + + rebalance_domains(this_cpu, idle); } static inline int on_null_domain(int cpu) @@ -4881,58 +4919,56 @@ static inline int on_null_domain(int cpu) } /* - * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. - * - * In case of CONFIG_NO_HZ, this is the place where we nominate a new - * idle load balancing owner or decide to stop the periodic load balancing, - * if the whole system is idle. + * Current heuristic for kicking the idle load balancer + * - first_pick_cpu is the one of the busy CPUs. It will kick + * idle load balancer when it has more than one process active. This + * eliminates the need for idle load balancing altogether when we have + * only one running process in the system (common case). + * - If there are more than one busy CPU, idle load balancer may have + * to run for active_load_balance to happen (i.e., two busy CPUs are + * SMT or core siblings and can run better if they move to different + * physical CPUs). So, second_pick_cpu is the second of the busy CPUs + * which will kick idle load balancer as soon as it has any load. */ -static inline void trigger_load_balance(struct rq *rq, int cpu) +static inline int nohz_kick_needed(struct rq *rq, int cpu) { -#ifdef CONFIG_NO_HZ - /* - * If we were in the nohz mode recently and busy at the current - * scheduler tick, then check if we need to nominate new idle - * load balancer. - */ - if (rq->in_nohz_recently && !rq->idle_at_tick) { - rq->in_nohz_recently = 0; + unsigned long now = jiffies; + int ret; - if (atomic_read(&nohz.load_balancer) == cpu) { - cpumask_clear_cpu(cpu, nohz.cpu_mask); - atomic_set(&nohz.load_balancer, -1); - } + if (time_before(now, nohz.next_balance)) + return 0; - if (atomic_read(&nohz.load_balancer) == -1) { - int ilb = find_new_ilb(cpu); + if (!rq->nr_running) + return 0; - if (ilb < nr_cpu_ids) - resched_cpu(ilb); + ret = atomic_cmpxchg(&nohz.first_pick_cpu, nr_cpu_ids, cpu); + if (ret == nr_cpu_ids || ret == cpu) { + atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids); + if (rq->nr_running > 1) + return 1; + } else { + ret = atomic_cmpxchg(&nohz.second_pick_cpu, nr_cpu_ids, cpu); + if (ret == nr_cpu_ids || ret == cpu) { + if (rq->nr_running) + return 1; } } + return 0; +} - /* - * If this cpu is idle and doing idle load balancing for all the - * cpus with ticks stopped, is it time for that to stop? - */ - if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && - cpumask_weight(nohz.cpu_mask) == num_online_cpus()) { - resched_cpu(cpu); - return; - } - - /* - * If this cpu is idle and the idle load balancing is done by - * someone else, then no need raise the SCHED_SOFTIRQ - */ - if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && - cpumask_test_cpu(cpu, nohz.cpu_mask)) - return; -#endif +/* + * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. + */ +static inline void trigger_load_balance(struct rq *rq, int cpu) +{ /* Don't need to rebalance while attached to NULL domain */ if (time_after_eq(jiffies, rq->next_balance) && likely(!on_null_domain(cpu))) raise_softirq(SCHED_SOFTIRQ); +#ifdef CONFIG_NO_HZ + else if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu))) + nohz_balancer_kick(cpu); +#endif } #else /* CONFIG_SMP */ @@ -5301,7 +5337,6 @@ void scheduler_tick(void) perf_event_task_tick(curr, cpu); #ifdef CONFIG_SMP - rq->idle_at_tick = idle_cpu(cpu); trigger_load_balance(rq, cpu); #endif } @@ -9627,6 +9662,10 @@ void __init sched_init(void) rq->push_cpu = 0; rq->cpu = i; rq->online = 0; +#ifdef CONFIG_NO_HZ + rq->idle_at_tick = 0; + rq->nohz_balance_kick = 0; +#endif rq->migration_thread = NULL; rq->idle_stamp = 0; rq->avg_idle = 2*sysctl_sched_migration_cost; @@ -9676,8 +9715,10 @@ void __init sched_init(void) zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT); #ifdef CONFIG_SMP #ifdef CONFIG_NO_HZ - zalloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT); + zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT); + atomic_set(&nohz.first_pick_cpu, nr_cpu_ids); + atomic_set(&nohz.second_pick_cpu, nr_cpu_ids); #endif /* May be allocated at isolcpus cmdline parse time */ if (cpu_isolated_map == NULL) -- 1.6.0.6
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor