This series is called Naïve Unix, but also includes the details of various Unix-like systems, this series of articles collated from my years of study notes, focusing on the implementation of UNIX, Unix-like ideas and sentiment.
This article is relatively short, just analyze a Linux CFS scheduler in one detail.
Is it just the process of removing the lower left corner of the red and black tree while the process is looking for the next process to be run? In fact, the simplest implementation is, but given the optimization, it's not that simple. Consider the utilization of CPU cache, that is, if a process a preemption process B, in the context of a process, pick next is to select Process B, or choose the lower left corner of the red and black tree process? In addition, if process A has just been awakened, attempting to preempt process B, but not successful, is the next option to select the lower left corner of the red-black tree or process a? Yes, these are the questions.
Let's take a look at the implementation of pick Next:
static struct sched_entity *pick_next_entity (struct Cfs_rq *cfs_rq) { struct sched_entity *se = __pick_next_entity ( CFS_RQ); struct sched_entity *left = se; if (Cfs_rq->next && wakeup_preempt_entity (Cfs_rq->next, left) < 1) SE = cfs_rq->next; / * * Prefer last buddy, try to return the CPU to a preempted task. * /if (cfs_rq->last && wakeup_preempt_entity (Cfs_rq->last, left) < 1) SE = cfs_rq->last; Clear_buddies (CFS_RQ, se); return SE;}
Obviously, to the red and black tree The bottom left of the process and the other two processes, namely next and last, the next is to seize the failure of the process, and last is to seize the success of the process of being preempted, which of the three processes is the optimal next process? The decision conditions for Linux CFS implementations are:
1. As much as possible to meet the need for the processes that have just been awakened to preempt other processes;
2. Minimize the impact of the cache refresh that this preemption brings.
How does the Linux CFS implementation work? Linux holds two variables, that is, the process of preemption and the preemption of a successful preemption, and in the tradeoff, the order of preference is:
The process that was preempted after preemption succeeded > preempted the failed process > The bottom left corner of the red and black tree
In the end can choose the first two processes, it is wakeup_preempt_entity function to decide, code too much useless, look at the following diagram:
The details of this CFS were not implemented when the Linux 2.6.23 originally implemented CFS. So from the simplest of times, you can gradually understand the ins and outs of the details, to see Changelog is good. It must be said that reading history makes people wise.
Plain UNIX-linux cfs a comment