[Linux kernel] CFS scheduler ⑥ ( CFS scheduler ready queue cfs_rq | Linux kernel scheduling entity sched_entity | "red-black tree" data structure rb_root_cached )





1. CFS scheduler ready queue cfs_rq



The main responsibility of the scheduler is to perform "scheduling management" on the "process" . The process is placed in the "scheduling queue" during scheduling.

The dispatch queue of the CFS scheduler is struct cfs_rq;

Through the "CFS scheduler ready queue" cfs_rq , you can track the "ready queue" information, manage the "ready state" scheduling entity , and maintain a "red-black tree" data structure sorted according to the virtual clock;


The struct cfs_rqstructure is defined in the header file of the Linux kernel source code ;linux-5.6.18\kernel\sched\sched.h


/* CFS-related fields in a runqueue */
struct cfs_rq {
    
    
	struct load_weight	load;
	unsigned long		runnable_weight;
	unsigned int		nr_running;
	unsigned int		h_nr_running;      /* SCHED_{NORMAL,BATCH,IDLE} */
	unsigned int		idle_h_nr_running; /* SCHED_IDLE */

	u64			exec_clock;
	u64			min_vruntime;

	struct rb_root_cached	tasks_timeline;

	/*
	 * 'curr' points to currently running entity on this cfs_rq.
	 * It is set to NULL otherwise (i.e when none are currently running).
	 */
	struct sched_entity	*curr;
	struct sched_entity	*next;
	struct sched_entity	*last;
	struct sched_entity	*skip;
}

insert image description here





Second, the Linux kernel scheduling entity sched_entity



sched_entityA structure is an entity that can be scheduled by the Linux kernel;

This "scheduling entity" can be inserted into a red- black tree (execution queue) node;

	struct sched_entity	*curr;
	struct sched_entity	*next;
	struct sched_entity	*last;
	struct sched_entity	*skip;




Three, "red-black tree" data structure rb_root_cached



Defined in "CFS Scheduler Ready Queue" cfs_rq

struct rb_root_cached	tasks_timeline;

The field is the "red-black tree" data structure sorted according to the "virtual clock" ,

tasks_timelinepointer to a struct of rb_root_cachedtype ,

rb_rootmember is the root node of this "red-black tree" data structure ;

rb_leftmostThe member points to the leftmost "scheduling entity" of this " red- black tree" data structure , which is the scheduling entity with the smallest "virtual time";


The "red-black tree" data structure is as follows: defined in the source code corresponding to the Linux kernel source code linux-5.6.18\include\linux\rbtree.hpath ;

/*
 * Leftmost-cached rbtrees.
 *
 * We do not cache the rightmost node based on footprint
 * size vs number of potential users that could benefit
 * from O(1) rb_last(). Just not worth it, users that want
 * this feature can always implement the logic explicitly.
 * Furthermore, users that want to cache both pointers may
 * find it a bit asymmetric, but that's ok.
 */
struct rb_root_cached {
    
    
	struct rb_root rb_root;
	struct rb_node *rb_leftmost;
};

insert image description here

Guess you like

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