Lines Matching defs:env

1668 static void update_numa_stats(struct task_numa_env *env, struct numa_stats *ns, int nid, bool find_idle)
1687 if (READ_ONCE(rq->numa_migrate_on) || !cpumask_test_cpu(cpu, env->p->cpus_ptr)) {
1702 ns->node_type = numa_classify(env->imbalance_pct, ns);
1709 static void task_numa_assign(struct task_numa_env *env, struct task_struct *p, long imp)
1711 struct rq *rq = cpu_rq(env->dst_cpu);
1714 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) {
1716 int start = env->dst_cpu;
1719 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start)
1721 if (cpu == env->best_cpu || !idle_cpu(cpu) || !cpumask_test_cpu(cpu, env->p->cpus_ptr)) {
1725 env->dst_cpu = cpu;
1726 rq = cpu_rq(env->dst_cpu);
1741 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) {
1742 rq = cpu_rq(env->best_cpu);
1746 if (env->best_task) {
1747 put_task_struct(env->best_task);
1753 env->best_task = p;
1754 env->best_imp = imp;
1755 env->best_cpu = env->dst_cpu;
1758 static bool load_too_imbalanced(long src_load, long dst_load, struct task_numa_env *env)
1771 src_capacity = env->src_stats.compute_capacity;
1772 dst_capacity = env->dst_stats.compute_capacity;
1776 orig_src_load = env->src_stats.load;
1777 orig_dst_load = env->dst_stats.load;
1798 static bool task_numa_compare(struct task_numa_env *env, long taskimp, long groupimp, bool maymove)
1800 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p);
1801 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1805 int dist = env->dist;
1822 * end try selecting ourselves (current == env->p) as a swap candidate.
1824 if (cur == env->p) {
1830 if (maymove && moveimp >= env->best_imp) {
1838 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) {
1846 if (env->best_task && env->best_task->numa_preferred_nid == env->src_nid &&
1847 cur->numa_preferred_nid != env->src_nid) {
1863 imp = taskimp + task_weight(cur, env->src_nid, dist) - task_weight(cur, env->dst_nid, dist);
1877 imp += group_weight(cur, env->src_nid, dist) - group_weight(cur, env->dst_nid, dist);
1879 imp += task_weight(cur, env->src_nid, dist) - task_weight(cur, env->dst_nid, dist);
1884 if (cur->numa_preferred_nid == env->dst_nid) {
1894 if (cur->numa_preferred_nid == env->src_nid) {
1898 if (maymove && moveimp > imp && moveimp > env->best_imp) {
1908 if (env->best_task && cur->numa_preferred_nid == env->src_nid &&
1909 env->best_task->numa_preferred_nid != env->src_nid) {
1919 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 0x2) {
1926 load = task_h_load(env->p) - task_h_load(cur);
1931 dst_load = env->dst_stats.load + load;
1932 src_load = env->src_stats.load - load;
1934 if (load_too_imbalanced(src_load, dst_load, env)) {
1941 int cpu = env->dst_stats.idle_cpu;
1945 cpu = env->dst_cpu;
1952 if (!idle_cpu(cpu) && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) {
1953 cpu = env->best_cpu;
1956 env->dst_cpu = cpu;
1959 task_numa_assign(env, cur, imp);
1966 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) {
1974 if (!maymove && env->best_task && env->best_task->numa_preferred_nid == env->src_nid) {
1983 static void task_numa_find_cpu(struct task_numa_env *env, long taskimp, long groupimp)
1992 if (env->dst_stats.node_type == node_has_spare) {
2002 src_running = env->src_stats.nr_running - 1;
2003 dst_running = env->dst_stats.nr_running + 1;
2009 if (env->dst_stats.idle_cpu >= 0) {
2010 env->dst_cpu = env->dst_stats.idle_cpu;
2011 task_numa_assign(env, NULL, 0);
2018 * If the improvement from just moving env->p direction is better
2021 load = task_h_load(env->p);
2022 dst_load = env->dst_stats.load + load;
2023 src_load = env->src_stats.load - load;
2024 maymove = !load_too_imbalanced(src_load, dst_load, env);
2027 for_each_cpu(cpu, cpumask_of_node(env->dst_nid))
2030 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) {
2034 env->dst_cpu = cpu;
2035 if (task_numa_compare(env, taskimp, groupimp, maymove)) {
2043 struct task_numa_env env = {
2071 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
2073 env.imbalance_pct = FAIR_ONEHUNDRED + (sd->imbalance_pct - FAIR_ONEHUNDRED) / 0x2;
2088 env.dst_nid = p->numa_preferred_nid;
2089 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
2090 taskweight = task_weight(p, env.src_nid, dist);
2091 groupweight = group_weight(p, env.src_nid, dist);
2092 update_numa_stats(&env, &env.src_stats, env.src_nid, false);
2093 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
2094 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
2095 update_numa_stats(&env, &env.dst_stats, env.dst_nid, true);
2098 task_numa_find_cpu(&env, taskimp, groupimp);
2108 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) {
2111 if (nid == env.src_nid || nid == p->numa_preferred_nid) {
2115 dist = node_distance(env.src_nid, env.dst_nid);
2116 if (sched_numa_topology_type == NUMA_BACKPLANE && dist != env.dist) {
2117 taskweight = task_weight(p, env.src_nid, dist);
2118 groupweight = group_weight(p, env.src_nid, dist);
2128 env.dist = dist;
2129 env.dst_nid = nid;
2130 update_numa_stats(&env, &env.dst_stats, env.dst_nid, true);
2131 task_numa_find_cpu(&env, taskimp, groupimp);
2144 if (env.best_cpu == -1) {
2145 nid = env.src_nid;
2147 nid = cpu_to_node(env.best_cpu);
2156 if (env.best_cpu == -1) {
2157 trace_sched_stick_numa(p, env.src_cpu, NULL, -1);
2161 best_rq = cpu_rq(env.best_cpu);
2162 if (env.best_task == NULL) {
2163 ret = migrate_task_to(p, env.best_cpu);
2166 trace_sched_stick_numa(p, env.src_cpu, NULL, env.best_cpu);
2171 ret = migrate_swap(p, env.best_task, env.best_cpu, env.src_cpu);
2175 trace_sched_stick_numa(p, env.src_cpu, env.best_task, env.best_cpu);
2177 put_task_struct(env.best_task);
7886 static int task_hot(struct task_struct *p, struct lb_env *env)
7890 lockdep_assert_held(&env->src_rq->lock);
7901 if (env->sd->flags & SD_SHARE_CPUCAPACITY) {
7908 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
7920 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
7931 static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
7941 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) {
7945 src_nid = cpu_to_node(env->src_cpu);
7946 dst_nid = cpu_to_node(env->dst_cpu);
7953 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) {
7966 if (env->idle == CPU_IDLE) {
7983 static inline int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
7992 static int can_migrate_task(struct task_struct *p, struct lb_env *env)
7996 lockdep_assert_held(&env->src_rq->lock);
8005 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) {
8014 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) {
8019 env->flags |= LBF_SOME_PINNED;
8029 if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED)) {
8033 /* Prevent to re-select dst_cpu via env's CPUs: */
8034 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus)
8037 env->flags |= LBF_DST_PINNED;
8038 env->new_dst_cpu = cpu;
8047 env->flags &= ~LBF_ALL_PINNED;
8050 if (env->flags & LBF_IGNORE_PREFERRED_CLUSTER_TASKS && !preferred_cluster(cpu_rq(env->dst_cpu)->cluster, p)) {
8055 if (task_running(env->src_rq, p)) {
8066 tsk_cache_hot = migrate_degrades_locality(p, env);
8068 tsk_cache_hot = task_hot(p, env);
8071 if (tsk_cache_hot <= 0 || env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
8073 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
8084 * detach_task() -- detach the task for the migration specified in env
8086 static void detach_task(struct task_struct *p, struct lb_env *env)
8088 lockdep_assert_held(&env->src_rq->lock);
8090 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK);
8092 double_lock_balance(env->src_rq, env->dst_rq);
8093 if (!(env->src_rq->clock_update_flags & RQCF_UPDATED)) {
8094 update_rq_clock(env->src_rq);
8097 set_task_cpu(p, env->dst_cpu);
8099 double_unlock_balance(env->src_rq, env->dst_rq);
8104 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
8109 static struct task_struct *detach_one_task(struct lb_env *env)
8113 lockdep_assert_held(&env->src_rq->lock);
8115 list_for_each_entry_reverse(p, &env->src_rq->cfs_tasks, se.group_node)
8117 if (!can_migrate_task(p, env)) {
8121 detach_task(p, env);
8125 * lb_gained[env->idle] is updated (other is detach_tasks)
8129 schedstat_inc(env->sd->lb_gained[env->idle]);
8143 static int detach_tasks(struct lb_env *env)
8145 struct list_head *tasks = &env->src_rq->cfs_tasks;
8150 int orig_loop = env->loop;
8153 lockdep_assert_held(&env->src_rq->lock);
8155 if (env->imbalance <= 0) {
8160 if (!same_cluster(env->dst_cpu, env->src_cpu)) {
8161 env->flags |= LBF_IGNORE_PREFERRED_CLUSTER_TASKS;
8171 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) {
8177 env->loop++;
8179 if (env->loop > env->loop_max) {
8184 if (env->loop > env->loop_break) {
8185 env->loop_break += sched_nr_migrate_break;
8186 env->flags |= LBF_NEED_BREAK;
8190 if (!can_migrate_task(p, env)) {
8194 switch (env->migration_type) {
8199 * value. Make sure that env->imbalance decreases
8205 if (sched_feat(LB_MIN) && load < 0x10 && !env->sd->nr_balance_failed) {
8215 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) {
8219 env->imbalance -= load;
8224 if (util > env->imbalance) {
8228 env->imbalance -= util;
8232 env->imbalance--;
8237 if (task_fits_capacity(p, capacity_of(env->src_cpu))) {
8241 env->imbalance = 0;
8245 detach_task(p, env);
8246 list_add(&p->se.group_node, &env->tasks);
8256 if (env->idle == CPU_NEWLY_IDLE) {
8265 if (env->imbalance <= 0) {
8275 if (env->flags & LBF_IGNORE_PREFERRED_CLUSTER_TASKS && !detached) {
8276 tasks = &env->src_rq->cfs_tasks;
8277 env->flags &= ~LBF_IGNORE_PREFERRED_CLUSTER_TASKS;
8278 env->loop = orig_loop;
8288 schedstat_add(env->sd->lb_gained[env->idle], detached);
8323 static void attach_tasks(struct lb_env *env)
8325 struct list_head *tasks = &env->tasks;
8329 rq_lock(env->dst_rq, &rf);
8330 update_rq_clock(env->dst_rq);
8336 attach_task(env->dst_rq, p);
8339 rq_unlock(env->dst_rq, &rf);
8932 * @env: The load balancing environment.
8937 static inline void update_sg_lb_stats(struct lb_env *env, struct sched_group *group, struct sg_lb_stats *sgs,
8944 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(group));
8946 for_each_cpu_and(i, sched_group_span(group), env->cpus)
8954 if ((env->flags & LBF_NOHZ_STATS) && update_nohz_stats(rq, false)) {
8955 env->flags |= LBF_NOHZ_AGAIN;
8992 if (env->sd->flags & SD_ASYM_CPUCAPACITY && sgs->group_misfit_task_load < rq->misfit_task_load) {
9008 if (env->sd->flags & SD_ASYM_PACKING && env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running &&
9009 sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu)) {
9017 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
9027 * @env: The load balancing environment.
9038 static bool update_sd_pick_busiest(struct lb_env *env, struct sd_lb_stats *sds, struct sched_group *sg,
9143 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && (sgs->group_type <= group_fully_busy) &&
9508 * @env: The load balancing environment.
9512 static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
9514 struct sched_domain *child = env->sd->child;
9515 struct sched_group *sg = env->sd->groups;
9521 if (env->idle == CPU_NEWLY_IDLE && READ_ONCE(nohz.has_blocked)) {
9522 env->flags |= LBF_NOHZ_STATS;
9530 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg));
9535 if (env->idle != CPU_NEWLY_IDLE || time_after_eq(jiffies, sg->sgc->next_update)) {
9536 update_group_capacity(env->sd, env->dst_cpu);
9540 update_sg_lb_stats(env, sg, sgs, &sg_status);
9546 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
9557 } while (sg != env->sd->groups);
9563 if ((env->flags & LBF_NOHZ_AGAIN) && cpumask_subset(nohz.idle_cpus_mask, sched_domain_span(env->sd))) {
9568 if (env->sd->flags & SD_NUMA) {
9569 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
9572 if (!env->sd->parent) {
9573 struct root_domain *rd = env->dst_rq->rd;
9582 struct root_domain *rd = env->dst_rq->rd;
9608 * @env: load balance environment
9611 static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
9620 env->migration_type = migrate_misfit;
9621 env->imbalance = 1;
9630 env->migration_type = migrate_task;
9631 env->imbalance = busiest->sum_h_nr_running;
9642 env->migration_type = migrate_task;
9643 env->imbalance = 1;
9652 if ((busiest->group_type > group_fully_busy) && !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) {
9661 env->migration_type = migrate_util;
9662 env->imbalance = max(local->group_capacity, local->group_util) - local->group_util;
9671 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) {
9672 env->migration_type = migrate_task;
9673 env->imbalance = 1;
9685 env->migration_type = migrate_task;
9687 env->imbalance = nr_diff >> 1;
9693 env->migration_type = migrate_task;
9694 env->imbalance = max_t(long, 0, (local->idle_cpus - busiest->idle_cpus) >> 1);
9698 if (env->sd->flags & SD_NUMA) {
9699 env->imbalance = adjust_numa_imbalance(env->imbalance, busiest->sum_nr_running);
9723 env->imbalance = 0;
9736 env->migration_type = migrate_load;
9737 env->imbalance = min((busiest->avg_load - sds->avg_load) * busiest->group_capacity,
9771 * @env: The load balancing environment.
9775 static struct sched_group *find_busiest_group(struct lb_env *env)
9786 update_sd_lb_stats(env, &sds);
9789 struct root_domain *rd = env->dst_rq->rd;
9859 if (FAIR_ONEHUNDRED * busiest->avg_load <= env->sd->imbalance_pct * local->avg_load) {
9871 if (env->idle == CPU_NOT_IDLE) {
9903 calculate_imbalance(env, &sds);
9904 return env->imbalance ? sds.busiest : NULL;
9907 env->imbalance = 0;
9914 static struct rq *find_busiest_queue(struct lb_env *env, struct sched_group *group)
9921 for_each_cpu_and(i, sched_group_span(group), env->cpus)
9948 if (rt > env->fbq_type) {
9965 if (env->sd->flags & SD_ASYM_CPUCAPACITY && capacity_of(env->dst_cpu) < capacity && nr_running == 1) {
9969 switch (env->migration_type) {
9976 if (nr_running == 1 && load > env->imbalance && !check_cpu_capacity(rq, env->sd)) {
10048 static inline bool asym_active_balance(struct lb_env *env)
10055 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) &&
10056 sched_asym_prefer(env->dst_cpu, env->src_cpu);
10059 static inline bool voluntary_active_balance(struct lb_env *env)
10061 struct sched_domain *sd = env->sd;
10063 if (asym_active_balance(env)) {
10073 if ((env->idle != CPU_NOT_IDLE) && (env->src_rq->cfs.h_nr_running == 1)) {
10074 if ((check_cpu_capacity(env->src_rq, sd)) &&
10075 (capacity_of(env->src_cpu) * sd->imbalance_pct < capacity_of(env->dst_cpu) * FAIR_ONEHUNDRED)) {
10080 if (env->migration_type == migrate_misfit) {
10087 static int need_active_balance(struct lb_env *env)
10089 struct sched_domain *sd = env->sd;
10091 if (voluntary_active_balance(env)) {
10111 static int should_we_balance(struct lb_env *env)
10113 struct sched_group *sg = env->sd->groups;
10120 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) {
10128 if (env->idle == CPU_NEWLY_IDLE) {
10133 for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus)
10140 return cpu == env->dst_cpu;
10144 return group_balance_cpu_not_isolated(sg) == env->dst_cpu;
10161 struct lb_env env = {
10170 .tasks = LIST_HEAD_INIT(env.tasks),
10178 if (!should_we_balance(&env)) {
10183 group = find_busiest_group(&env);
10189 busiest = find_busiest_queue(&env, group);
10195 BUG_ON(busiest == env.dst_rq);
10197 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
10199 env.src_cpu = busiest->cpu;
10200 env.src_rq = busiest;
10210 env.flags |= LBF_ALL_PINNED;
10211 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
10221 cur_ld_moved = detach_tasks(&env);
10234 attach_tasks(&env);
10240 if (env.flags & LBF_NEED_BREAK) {
10241 env.flags &= ~LBF_NEED_BREAK;
10264 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
10265 /* Prevent to re-select dst_cpu via env's CPUs */
10266 __cpumask_clear_cpu(env.dst_cpu, env.cpus);
10268 env.dst_rq = cpu_rq(env.new_dst_cpu);
10269 env.dst_cpu = env.new_dst_cpu;
10270 env.flags &= ~LBF_DST_PINNED;
10271 env.loop = 0;
10272 env.loop_break = sched_nr_migrate_break;
10287 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) {
10293 if (unlikely(env.flags & LBF_ALL_PINNED)) {
10303 if (!cpumask_subset(cpus, env.dst_grpmask)) {
10304 env.loop = 0;
10305 env.loop_break = sched_nr_migrate_break;
10324 if (need_active_balance(&env)) {
10336 env.flags |= LBF_ALL_PINNED;
10364 if (likely(!active_balance) || voluntary_active_balance(&env)) {
10387 if (sd_parent && !(env.flags & LBF_ALL_PINNED)) {
10414 if (env.idle == CPU_NEWLY_IDLE) {
10419 if ((env.flags & LBF_ALL_PINNED && sd->balance_interval < MAX_PINNED_INTERVAL) ||
10516 struct lb_env env = {
10529 detach_task(push_task, &env);
10546 struct lb_env env = {
10565 p = detach_one_task(&env);
10684 * env->dst_cpu, so we can't know our idle