⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sched_fair.c

📁 Kernel code of linux kernel
💻 C
📖 第 1 页 / 共 3 页
字号:
	if (!(this_sd->flags & SD_WAKE_AFFINE) || !sched_feat(AFFINE_WAKEUPS))		return 0;	/*	 * If sync wakeup then subtract the (maximum possible)	 * effect of the currently running task from the load	 * of the current CPU:	 */	if (sync) {		tg = task_group(current);		weight = current->se.load.weight;		tl += effective_load(tg, this_cpu, -weight, -weight);		load += effective_load(tg, prev_cpu, 0, -weight);	}	tg = task_group(p);	weight = p->se.load.weight;	balanced = 100*(tl + effective_load(tg, this_cpu, weight, weight)) <=		imbalance*(load + effective_load(tg, prev_cpu, 0, weight));	/*	 * If the currently running task will sleep within	 * a reasonable amount of time then attract this newly	 * woken task:	 */	if (sync && balanced) {		if (curr->se.avg_overlap < sysctl_sched_migration_cost &&		    p->se.avg_overlap < sysctl_sched_migration_cost)			return 1;	}	schedstat_inc(p, se.nr_wakeups_affine_attempts);	tl_per_task = cpu_avg_load_per_task(this_cpu);	if ((tl <= load && tl + target_load(prev_cpu, idx) <= tl_per_task) ||			balanced) {		/*		 * This domain has SD_WAKE_AFFINE and		 * p is cache cold in this domain, and		 * there is no bad imbalance.		 */		schedstat_inc(this_sd, ttwu_move_affine);		schedstat_inc(p, se.nr_wakeups_affine);		return 1;	}	return 0;}static int select_task_rq_fair(struct task_struct *p, int sync){	struct sched_domain *sd, *this_sd = NULL;	int prev_cpu, this_cpu, new_cpu;	unsigned long load, this_load;	struct rq *rq, *this_rq;	unsigned int imbalance;	int idx;	prev_cpu	= task_cpu(p);	rq		= task_rq(p);	this_cpu	= smp_processor_id();	this_rq		= cpu_rq(this_cpu);	new_cpu		= prev_cpu;	/*	 * 'this_sd' is the first domain that both	 * this_cpu and prev_cpu are present in:	 */	for_each_domain(this_cpu, sd) {		if (cpu_isset(prev_cpu, sd->span)) {			this_sd = sd;			break;		}	}	if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))		goto out;	/*	 * Check for affine wakeup and passive balancing possibilities.	 */	if (!this_sd)		goto out;	idx = this_sd->wake_idx;	imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;	load = source_load(prev_cpu, idx);	this_load = target_load(this_cpu, idx);	if (wake_affine(rq, this_sd, this_rq, p, prev_cpu, this_cpu, sync, idx,				     load, this_load, imbalance))		return this_cpu;	if (prev_cpu == this_cpu)		goto out;	/*	 * Start passive balancing when half the imbalance_pct	 * limit is reached.	 */	if (this_sd->flags & SD_WAKE_BALANCE) {		if (imbalance*this_load <= 100*load) {			schedstat_inc(this_sd, ttwu_move_balance);			schedstat_inc(p, se.nr_wakeups_passive);			return this_cpu;		}	}out:	return wake_idle(new_cpu, p);}#endif /* CONFIG_SMP */static unsigned long wakeup_gran(struct sched_entity *se){	unsigned long gran = sysctl_sched_wakeup_granularity;	/*	 * More easily preempt - nice tasks, while not making it harder for	 * + nice tasks.	 */	if (sched_feat(ASYM_GRAN))		gran = calc_delta_asym(sysctl_sched_wakeup_granularity, se);	else		gran = calc_delta_fair(sysctl_sched_wakeup_granularity, se);	return gran;}/* * Should 'se' preempt 'curr'. * *             |s1 *        |s2 *   |s3 *         g *      |<--->|c * *  w(c, s1) = -1 *  w(c, s2) =  0 *  w(c, s3) =  1 * */static intwakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se){	s64 gran, vdiff = curr->vruntime - se->vruntime;	if (vdiff < 0)		return -1;	gran = wakeup_gran(curr);	if (vdiff > gran)		return 1;	return 0;}/* return depth at which a sched entity is present in the hierarchy */static inline int depth_se(struct sched_entity *se){	int depth = 0;	for_each_sched_entity(se)		depth++;	return depth;}/* * Preempt the current task with a newly woken task if needed: */static void check_preempt_wakeup(struct rq *rq, struct task_struct *p){	struct task_struct *curr = rq->curr;	struct cfs_rq *cfs_rq = task_cfs_rq(curr);	struct sched_entity *se = &curr->se, *pse = &p->se;	int se_depth, pse_depth;	if (unlikely(rt_prio(p->prio))) {		update_rq_clock(rq);		update_curr(cfs_rq);		resched_task(curr);		return;	}	if (unlikely(se == pse))		return;	cfs_rq_of(pse)->next = pse;	/*	 * Batch tasks do not preempt (their preemption is driven by	 * the tick):	 */	if (unlikely(p->policy == SCHED_BATCH))		return;	if (!sched_feat(WAKEUP_PREEMPT))		return;	/*	 * preemption test can be made between sibling entities who are in the	 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of	 * both tasks until we find their ancestors who are siblings of common	 * parent.	 */	/* First walk up until both entities are at same depth */	se_depth = depth_se(se);	pse_depth = depth_se(pse);	while (se_depth > pse_depth) {		se_depth--;		se = parent_entity(se);	}	while (pse_depth > se_depth) {		pse_depth--;		pse = parent_entity(pse);	}	while (!is_same_group(se, pse)) {		se = parent_entity(se);		pse = parent_entity(pse);	}	if (wakeup_preempt_entity(se, pse) == 1)		resched_task(curr);}static struct task_struct *pick_next_task_fair(struct rq *rq){	struct task_struct *p;	struct cfs_rq *cfs_rq = &rq->cfs;	struct sched_entity *se;	if (unlikely(!cfs_rq->nr_running))		return NULL;	do {		se = pick_next_entity(cfs_rq);		cfs_rq = group_cfs_rq(se);	} while (cfs_rq);	p = task_of(se);	hrtick_start_fair(rq, p);	return p;}/* * Account for a descheduled task: */static void put_prev_task_fair(struct rq *rq, struct task_struct *prev){	struct sched_entity *se = &prev->se;	struct cfs_rq *cfs_rq;	for_each_sched_entity(se) {		cfs_rq = cfs_rq_of(se);		put_prev_entity(cfs_rq, se);	}}#ifdef CONFIG_SMP/************************************************** * Fair scheduling class load-balancing methods: *//* * Load-balancing iterator. Note: while the runqueue stays locked * during the whole iteration, the current task might be * dequeued so the iterator has to be dequeue-safe. Here we * achieve that by always pre-iterating before returning * the current task: */static struct task_struct *__load_balance_iterator(struct cfs_rq *cfs_rq, struct list_head *next){	struct task_struct *p = NULL;	struct sched_entity *se;	if (next == &cfs_rq->tasks)		return NULL;	/* Skip over entities that are not tasks */	do {		se = list_entry(next, struct sched_entity, group_node);		next = next->next;	} while (next != &cfs_rq->tasks && !entity_is_task(se));	if (next == &cfs_rq->tasks)		return NULL;	cfs_rq->balance_iterator = next;	if (entity_is_task(se))		p = task_of(se);	return p;}static struct task_struct *load_balance_start_fair(void *arg){	struct cfs_rq *cfs_rq = arg;	return __load_balance_iterator(cfs_rq, cfs_rq->tasks.next);}static struct task_struct *load_balance_next_fair(void *arg){	struct cfs_rq *cfs_rq = arg;	return __load_balance_iterator(cfs_rq, cfs_rq->balance_iterator);}static unsigned long__load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,		unsigned long max_load_move, struct sched_domain *sd,		enum cpu_idle_type idle, int *all_pinned, int *this_best_prio,		struct cfs_rq *cfs_rq){	struct rq_iterator cfs_rq_iterator;	cfs_rq_iterator.start = load_balance_start_fair;	cfs_rq_iterator.next = load_balance_next_fair;	cfs_rq_iterator.arg = cfs_rq;	return balance_tasks(this_rq, this_cpu, busiest,			max_load_move, sd, idle, all_pinned,			this_best_prio, &cfs_rq_iterator);}#ifdef CONFIG_FAIR_GROUP_SCHEDstatic unsigned longload_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,		  unsigned long max_load_move,		  struct sched_domain *sd, enum cpu_idle_type idle,		  int *all_pinned, int *this_best_prio){	long rem_load_move = max_load_move;	int busiest_cpu = cpu_of(busiest);	struct task_group *tg;	rcu_read_lock();	update_h_load(busiest_cpu);	list_for_each_entry(tg, &task_groups, list) {		struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];		unsigned long busiest_h_load = busiest_cfs_rq->h_load;		unsigned long busiest_weight = busiest_cfs_rq->load.weight;		u64 rem_load, moved_load;		/*		 * empty group		 */		if (!busiest_cfs_rq->task_weight)			continue;		rem_load = (u64)rem_load_move * busiest_weight;		rem_load = div_u64(rem_load, busiest_h_load + 1);		moved_load = __load_balance_fair(this_rq, this_cpu, busiest,				rem_load, sd, idle, all_pinned, this_best_prio,				tg->cfs_rq[busiest_cpu]);		if (!moved_load)			continue;		moved_load *= busiest_h_load;		moved_load = div_u64(moved_load, busiest_weight + 1);		rem_load_move -= moved_load;		if (rem_load_move < 0)			break;	}	rcu_read_unlock();	return max_load_move - rem_load_move;}#elsestatic unsigned longload_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,		  unsigned long max_load_move,		  struct sched_domain *sd, enum cpu_idle_type idle,		  int *all_pinned, int *this_best_prio){	return __load_balance_fair(this_rq, this_cpu, busiest,			max_load_move, sd, idle, all_pinned,			this_best_prio, &busiest->cfs);}#endifstatic intmove_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,		   struct sched_domain *sd, enum cpu_idle_type idle){	struct cfs_rq *busy_cfs_rq;	struct rq_iterator cfs_rq_iterator;	cfs_rq_iterator.start = load_balance_start_fair;	cfs_rq_iterator.next = load_balance_next_fair;	for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {		/*		 * pass busy_cfs_rq argument into		 * load_balance_[start|next]_fair iterators		 */		cfs_rq_iterator.arg = busy_cfs_rq;		if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,				       &cfs_rq_iterator))		    return 1;	}	return 0;}#endif /* CONFIG_SMP *//* * scheduler tick hitting a task of our scheduling class: */static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued){	struct cfs_rq *cfs_rq;	struct sched_entity *se = &curr->se;	for_each_sched_entity(se) {		cfs_rq = cfs_rq_of(se);		entity_tick(cfs_rq, se, queued);	}}#define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)/* * Share the fairness runtime between parent and child, thus the * total amount of pressure for CPU stays equal - new tasks * get a chance to run but frequent forkers are not allowed to * monopolize the CPU. Note: the parent runqueue is locked, * the child is not running yet. */static void task_new_fair(struct rq *rq, struct task_struct *p){	struct cfs_rq *cfs_rq = task_cfs_rq(p);	struct sched_entity *se = &p->se, *curr = cfs_rq->curr;	int this_cpu = smp_processor_id();	sched_info_queued(p);	update_curr(cfs_rq);	place_entity(cfs_rq, se, 1);	/* 'curr' will be NULL if the child belongs to a different group */	if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&			curr && curr->vruntime < se->vruntime) {		/*		 * Upon rescheduling, sched_class::put_prev_task() will place		 * 'current' within the tree based on its new key value.		 */		swap(curr->vruntime, se->vruntime);	}	enqueue_task_fair(rq, p, 0);	resched_task(rq->curr);}/* * Priority of the task has changed. Check to see if we preempt * the current task. */static void prio_changed_fair(struct rq *rq, struct task_struct *p,			      int oldprio, int running){	/*	 * Reschedule if we are currently running on this runqueue and	 * our priority decreased, or if we are not currently running on	 * this runqueue and our priority is higher than the current's	 */	if (running) {		if (p->prio > oldprio)			resched_task(rq->curr);	} else		check_preempt_curr(rq, p);}/* * We switched to the sched_fair class. */static void switched_to_fair(struct rq *rq, struct task_struct *p,			     int running){	/*	 * We were most likely switched from sched_rt, so	 * kick off the schedule if running, otherwise just see	 * if we can still preempt the current task.	 */	if (running)		resched_task(rq->curr);	else		check_preempt_curr(rq, p);}/* Account for a task changing its policy or group. * * This routine is mostly called to set cfs_rq->curr field when a task * migrates between groups/classes. */static void set_curr_task_fair(struct rq *rq){	struct sched_entity *se = &rq->curr->se;	for_each_sched_entity(se)		set_next_entity(cfs_rq_of(se), se);}#ifdef CONFIG_FAIR_GROUP_SCHEDstatic void moved_group_fair(struct task_struct *p){	struct cfs_rq *cfs_rq = task_cfs_rq(p);	update_curr(cfs_rq);	place_entity(cfs_rq, &p->se, 1);}#endif/* * All the scheduling class methods: */static const struct sched_class fair_sched_class = {	.next			= &idle_sched_class,	.enqueue_task		= enqueue_task_fair,	.dequeue_task		= dequeue_task_fair,	.yield_task		= yield_task_fair,#ifdef CONFIG_SMP	.select_task_rq		= select_task_rq_fair,#endif /* CONFIG_SMP */	.check_preempt_curr	= check_preempt_wakeup,	.pick_next_task		= pick_next_task_fair,	.put_prev_task		= put_prev_task_fair,#ifdef CONFIG_SMP	.load_balance		= load_balance_fair,	.move_one_task		= move_one_task_fair,#endif	.set_curr_task          = set_curr_task_fair,	.task_tick		= task_tick_fair,	.task_new		= task_new_fair,	.prio_changed		= prio_changed_fair,	.switched_to		= switched_to_fair,#ifdef CONFIG_FAIR_GROUP_SCHED	.moved_group		= moved_group_fair,#endif};#ifdef CONFIG_SCHED_DEBUGstatic void print_cfs_stats(struct seq_file *m, int cpu){	struct cfs_rq *cfs_rq;	rcu_read_lock();	for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)		print_cfs_rq(m, cpu, cfs_rq);	rcu_read_unlock();}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -