Chapter 4 Process scheduling over 4.1 tasks
Process scheduling for 4.2 Linux
- O (1) Scheduler: The workload on large servers is ideal, but the interaction process is missing.
- Inversion stair deadline scheduling algorithm (RSDL)
- Fully Fair scheduling algorithm (CFS)
4.3 strategy
- The policy determines what program the scheduler is running in.
4.3.1 Process Classification
- I/O consumption type
- Most of the time the process is used to commit I/O requests or wait for I/O requests, often in a running state but running for a short period of time, and eventually blocking when waiting for more requests.
Processor consumption type
- Most of the time is spent on executing code, unless it is preempted, and will usually run continuously.
A scheduling strategy usually seeks to balance the two conflicting goals:
- Fast process scheduling (short response times)
- Maximum system utilization (high throughput)
Linux tends to prioritize I/O consuming processes
4.3.2 Process Priority
- The most basic type of scheduling algorithm is priority-based scheduling:
- High-priority processes run first; processes with the same priority are scheduled on a rotational basis
- The scheduler always chooses the process in which the time slice is not exhausted and the highest priority is run.
- Linux uses two different priority ranges:
- Nice value (from -20--+19): The default value is 0; the larger the number means the lower the priority; You can view the list of system processes by Ps-el and find the priority of the NI tag column
- Real-time priority (from 0--99): higher real-time priority levels mean higher process priorities
- They don't interact with each other
4.3.3 Time Slice
- A time slice represents the time that a process can continue to run before it is preempted.
- The scheduling strategy must determine a default time slice;
- The CFS scheduler for Linux does not divide the time slices directly into the process, but instead divides the processor usage into processes. That is, the timing of the preemption depends on how much processor usage the new executable consumes, and if the consumed usage is smaller than the current process, the new process immediately runs to preempt the current process.
4.3.4 Scheduling policy Activity 4.4 Linux Scheduling algorithm 4.4.1 Scheduler class
- The Linux scheduler is provided in a modular manner (that is, the Scheduler Class) to allow different types of processes to selectively select scheduling algorithms
- Scheduler class allows a variety of dynamically added scheduling algorithms coexist, scheduling belongs to their own category of processes;
- The scheduler code traverses the scheduling class in order of precedence, and the Scheduler class with the highest priority of an executable process wins, choosing the program to be executed below;
Process scheduling in 4.4.2 Unix system
The scheduling algorithm used by UNIX is to allocate absolute time slices, which will trigger a fixed switching frequency, which is not conducive to fairness. The CFS used by Linux completely abandons the time slice, assigning the process a processor to use the weighting, guaranteeing constant fairness and changing the switching frequency.
4.4.3 Fair Dispatch (CFS)
Implementation of 4.5 Linux scheduling
--The implementation of the CFS scheduling algorithm.
Four components:
- 时间记账- 进程选择- 调度器入口- 睡眠和唤醒
4.5.1 Time Accounting
- All schedulers must account for the running time of the process;
- CFS uses scheduler entity structure to track running bookkeeping
- 1. Scheduler Entity Structure
- CFS uses the scheduler entity structure to track process run accounting:
- 2. Virtual Real-time
- CFS uses the Vruntime variable to hold the virtual run time of the process, to indicate how long the process is running, and how long it should run.
- This virtual run time is weighted, regardless of the timer beat.
- The virtual run time is in NS units.
- The associated function is updateCurr (), which calculates the execution time of the current process and puts it into the variable Deltaexec, and then passes the run time toupdatecurr ();
- _UpdateCurr () weights the time based on the total number of currently running processes, and eventually adds the weight value to the Vruntime value of the current running process.
4.5.2 Process Selection
- The core of the CFS algorithm: selecting tasks with minimal vrntime
- How to: Use Red-black tree rbtree (two-fork tree that stores data as nodes)
- Example:
- Select the next task: traverse the binary tree from the root node and continue to the leaf node (i.e. the vrntime smallest process);
- To join a process to a tree: the process becomes executable or the first time the process is created by a fork () call;
- Remove a process from a tree: Occurs when a process is blocked or terminated
**linux, red black tree is called Rbtree, is a self-balancing binary search tree, is a tree node form of data stored, the data will correspond to a key value, the key value can be used to quickly retrieve the data on the node, and the retrieval speed and the entire tree node size into an exponential ratio relationship. **
Pick the next task:
The node key value is the virtual run time of the running process, the process selection algorithm is "the process of running the leftmost leaf node of the rbtree tree species", the function is picknextentity ()
- To join a process to the tree:
- Occurs when the process is awakened or the first time a process is created through a fork call.
- function Enqueueentity (): Update run time and some other statistics, and then call enqueueentity (). function enqueue_entity (): Perform heavy insertion work and actually insert data items into the red-black tree:
Remove a process from the tree
4.5.3 Scheduler Entry
- The main entry point for process scheduling is the function schedule (), which is defined in KERNEL/SCHED.C; this is where the internal and other parts are used to dispatch the process scheduler.
- The most important task of this function is to call picknextState (), examine each scheduling class in turn, and select the highest priority process from the highest priority scheduling class
4.5.4 Sleep and wake up
- Process hibernation must be to wait for some events
- The process marks itself as dormant, removed from the executable red-black tree;
- Put into the wait queue-a list of processes waiting for some time to occur, and the kerneluses wake queue head_t to represent the waiting queue
3. The wake-up operation is performed by the function wake_up ()
- 它会调用函数try_to _wake_up()将进程设置为TASK_RUNNING状态,调用enqueue_task()将进程放入红黑树中- 当然,也存在虚假唤醒进程的状态
4.6 Preemption and Context switching
Resources
Linux kernel design and implementation
20135239 Silam Linux Kernel Analysis The fourth chapter of reading notes