[Linux kernel] Real-time scheduling class ⑥ (source code analysis of real-time scheduling class core functions | Insert process into execution queue | Select the process with the highest priority from the execution queue)


In this blog, start to analyze the function source code pointed to by each function pointer in the struct sched_class rt_sched_classstructure variable;

rt_sched_classThe structure is defined in the linux-5.6.18\kernel\sched\rt.csource , and the core real-time scheduling are also defined in the source code;





1. The enqueue_task_rt function (insert process into the execution queue)



enqueue_task_rtThe function is used to update the "scheduling information" and insert the "real-time scheduling entity" sched_rt_entity into the end of the " execution queue" (red-black tree) (the far right of the red-black tree);

The core code of this function is to call the enqueue_pushable_taskfunction to insert the current "real-time scheduling entity" into the corresponding priority list ;

/*
 * Adding/removing a task to/from a priority array:
 */
static void
enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
{
    
    
	struct sched_rt_entity *rt_se = &p->rt;

	if (flags & ENQUEUE_WAKEUP)
		rt_se->timeout = 0;

	enqueue_rt_entity(rt_se, flags);

	if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
		enqueue_pushable_task(rq, p);
}

Source path: linux-5.6.18\kernel\sched\rt.c#1367





2. pick_next_task_rt function (select the process with the highest priority from the execution queue)



enqueue_task_rtThe function is used to select the "real-time process" with the "highest priority" in the "execution queue" (red-black tree) for execution;

The core code of the function is to call the _pick_next_task_rtfunction ;

static struct task_struct *pick_next_task_rt(struct rq *rq)
{
    
    
	struct task_struct *p;

	if (!sched_rt_runnable(rq))
		return NULL;

	p = _pick_next_task_rt(rq);
	set_next_task_rt(rq, p, true);
	return p;
}

Source code path: linux-5.6.18\kernel\sched\rt.c#1616


In the _pick_next_task_rtfunction , the function is called again pick_next_rt_entity;

static struct task_struct *_pick_next_task_rt(struct rq *rq)
{
    
    
	struct sched_rt_entity *rt_se;
	struct rt_rq *rt_rq  = &rq->rt;

	do {
    
    
		rt_se = pick_next_rt_entity(rq, rt_rq);
		BUG_ON(!rt_se);
		rt_rq = group_rt_rq(rt_se);
	} while (rt_rq);

	return rt_task_of(rt_se);
}

Source code path: linux-5.6.18\kernel\sched\rt.c#1602


pick_next_rt_entityThe function source code is as follows:

idx = sched_find_first_bit(array->bitmap);The role is to find available entities;

queue = array->queue + idx;The function is to find the corresponding linked list from the "linked list group";

return nextReturn the found live running entity;

static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
						   struct rt_rq *rt_rq)
{
    
    
	struct rt_prio_array *array = &rt_rq->active;
	struct sched_rt_entity *next = NULL;
	struct list_head *queue;
	int idx;

	idx = sched_find_first_bit(array->bitmap);
	BUG_ON(idx >= MAX_RT_PRIO);

	queue = array->queue + idx;
	next = list_entry(queue->next, struct sched_rt_entity, run_list);

	return next;
}

Source code path: linux-5.6.18\kernel\sched\rt.c#1585

Guess you like

Origin blog.csdn.net/han1202012/article/details/123892361