Linux Kernel Development Chapter 4 Process Scheduling Kim
















- Slides: 16

Linux Kernel Development Chapter 4. Process Scheduling Kim, Byung-Chul Ch. 4. Processing Scheduling

Contents u Overview u Preemption and Context Switching u Policy n n n I/O-Bound Versus Processor. Bound Processes Policy Priority Timeslice Process Preemption The Scheduling Policy in Action n n n u Real-Time u Scheduler-Related System Calls n u The Linux Scheduling Algorithm n n n Runqueues The Priority Arrays Recalculating Timeslices Schedule() Calculating Priority and Timeslice Sleeping and Waking up The Load Balancer 2 User Preemption Kernel Preemption n Scheduling Policy and Priority. Related System Calls Process Affinity System Calls Yielding Processor Time u Scheduler Finale Ch. 4. Processing Scheduling

Overview u. Process Scheduler by deciding whch process to run next n n Best utilizing the system Giving the impression that multiple processes are executing simultaneously u. Multi-tasking Operating System n n Cooperative multi-tasking Preemptive multi-tasking u. This Chapter n n The fundamentals of scheduler design How they apply to the new O(1) scheduler 3 Ch. 4. Processing Scheduling

Policy u. The Type of Processes n n I/O-Bound Processes: spend much of their time submitting and waiting on I/O requests Processor-Bound Processes: spend much of their time executing code u. Process Priority n n n Process with a higher priority run before those with a lower priority. Dynamic priority-based scheduling Priority Range implemented by Linux n Nice value n Real-time priority 4 Ch. 4. Processing Scheduling

u. Timeslice n n n The numeric value that represents how long a task can run until it is preempted. A process does not have to use all its timeslice at once. When a process’s timeslice runs out, the process is not eligible to run until all other processes have exhausted their timeslices. u. Process Preemption n n When a process with the higher priority than the priority of the currently executing process enters the TASK_RUNNING state. When a process’s timeslice reaches zero. 5 Ch. 4. Processing Scheduling

u. The Scheduling Policy in Action Text-Editor Video-Encoder I/O-bound process More interactive High priority Larger timeslice Processor-bound process Less interactive Low priority Fewer timeslice 6 Ch. 4. Processing Scheduling

The Linux Scheduling Algorithm u. The new scheduler was designed to accomplish specific goals from the 2. 5 linux kernel: n n n Implement fully O(1) scheduling. Implement perfect SMP scalability. Implement improved SMP affinity. Provide good interactive performance. Provide fairness. Optimize for the common case of only one or two runnable processes, yet scale well to multiple processors, each with many processes. 7 Ch. 4. Processing Scheduling

u. Runqueues n n The list of runnable processes on a given processor. n Refer struct runqueue on the page 45 for the detailed elements. Before a runqueue can be manipulated, it must be locked. u. The Priority Arrays n n The Linux scheduler maintains two priority arrays n The active and the expired array. Priority arrays are the data structures that provide O(1) scheduling. 8 Ch. 4. Processing Scheduling

schedule() schedule_find_first_set() Bit 0 priority 0 Bit 7 priority 7 List of all runnable tasks, by priority 140 -bit priority array Bit 139 priority 139 Run the first process in the list 9 List of runnable tasks for priority 7 Ch. 4. Processing Scheduling

u. Calculating Priority & Timeslice n n Dynamic Priority for Priority n To get whether a process is interactive, Linux keeps a running tab on how much time a process is spent sleeping versus how much time the process spends in a runnable state. Static Priority for Timeslice n When a process is first created, the new child and the parent split the parent’s remaining timeslice. n After a task’s timeslice is exhausted, it is recalculated based on the task’s static priority. 10 Ch. 4. Processing Scheduling

u. Sleeping and Waking Up n n The sleeping task marks itself as sleeping, puts itself on a wait queue 1), removes itself from the runqueue, and calls schedule() to select a new process to execute. The waking task is set as runnable, removed from the wait queue, and added back to the runqueue. 1) wait queue: a simple list of process waiting for an event to occur 11 Ch. 4. Processing Scheduling

u. The Load Balancer Process 1 Process 2 Process 3 load_balance() Process 4 Process 5 Pull process from one runqueue To another to relieve imbalance Processor 1’s Runqueue Total process: 5 Processor 2’s Runqueue Total process: 1 12 Ch. 4. Processing Scheduling

Preemption & Context Switching u Context Switching n n Calls switch_mm() to switch the virtual memory mapping from the previous process’s to that of the new process. Calls switch_to() to switch the processor state from the previous process’s to the current’s. u Preemption n n User Preemption n When returning to user-space from a system call n When returning to user-space from an interrupt handler Kernel Preemption n When an interrupt handler exists, before returning to kernel-space n When kernel code becomes preemptible again n If a task in the kernel explicitly calls schedule() n If a task in the kernel blocks 13 Ch. 4. Processing Scheduling

Run-Time u. Real-time Scheduling Policy 1) n n SCHED_FIFO: implements a simple first-in, first-out scheduling algorithm without timeslices. SCHED_RR: identical to SCHED_FIFO except that each process can run only until it exhausts a predetermined timeslice. u. Real-Time Behavior n n Soft Real-Time Hard Real-Time: not supported by linux 1) Real-time scheduling policy implements static priorities. The kernel does not calculate dynamic priority values. 14 Ch. 4. Processing Scheduling

Schedule-Related System Calls u. Scheduling Policy and Priority-Related System Calls u. Processor Affinity System Calls u. Yielding Processor Time 15 Ch. 4. Processing Scheduling

Scheduler Finale u. The process scheduler is an important part of the kernel, but controlling the demands of process scheduling are nontrivial. n n A large number of runnable processes scalability concerns tradeoffs between latency and throughput the demands of various workloads u. This Chapter n Theory behind process scheduling, and the specific implementation, algorithms, and interfaces used by the current Linux kernel. 16 Ch. 4. Processing Scheduling