530 likes | 786 Views
Linux Scheduling. 9662541 張文軒 9665510 許晉榮 9665531 林奕翔. Outline. Introduction Linux 2.6 Scheduling Policy Priority Time Quantum Duration Data Structure Functions Used by the Scheduler Runqueue Balancing in Multiprocessor Systems System call related to scheduling Reference. Outline.
E N D
Linux Scheduling 9662541 張文軒 9665510 許晉榮 9665531 林奕翔
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling
Introduction • What is Scheduling? • when to switch and which process to choose. • Goal of the Linux scheduling: • fast process response time. • good throughput for background jobs. • avoidance of process starvation. • reconciliation of the needs of low- and high-priority processes. • etc… Advanced Operation System - Linux Scheduling
New feature of scheduling in Linux 2.6 • Preemptable kernel bits: • Even as the system enters Kernel Mode, the process can still be interrupted. • New ultra-scalable O(1) schedule: • Every algorithm in the v2.6 scheduling algorithm in constant time, regardless of the number of running processes. • Implement perfect Symmetric Multiprocessing (SMP) scalability: • Each process has its own locking and individual runqueue. Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling
System Call Related to Scheduling Advanced Operation System - Linux Scheduling
Scheduling Policy • Linux scheduling is based on time sharing technique • The scheduling policy is based on ranking processes according to their dynamic priority. • Process priority is dynamically determined by scheduler periodically. Advanced Operation System - Linux Scheduling
Scheduling Policy • Process classification: • Interactive process. • Batch process. • Real-time process. • CPU-bound vs. I/O-bound. • Heuristic algorithm Advanced Operation System - Linux Scheduling
Scheduling Policy • In linux 2.6, processes are preemptable • when higher priority process enters the TASK_RUNNING state, the current one will be preempted. • TIF_NEED_RESCHED • Every Linux process is always scheduled according to one of the following scheduling classes: • SCHED_NORMAL=0 • SCHED_FIFO (real-time process)=1 • SCHED_RR (real-time process)=2 Advanced Operation System - Linux Scheduling
Scheduler v2.4 User mode User mode Kernel mode Scheduler v2.6 Kernel mode Kernel mode Process A Process B User mode User mode Scheduler v2.4 Scheduler v2.6 Process Preempt Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling
Normal Process Priority • Static Priority: • Have the same meanings with nice value in linux 2.4. • Ranging from 100 (highest priority) to 139 (lowest priority) • Determines the base time-slice of a process. • Nice Value: • Used by linux 2.4. • Ranging from -20 to 19. • In linux 2.6, Be replaced by static priority. • static_prio = MAX_RT_PRIO + nice + 20 Advanced Operation System - Linux Scheduling
Normal Process Priority • Dynamic priority • Looked up by the scheduler when selecting the new process to run. • Related to the average sleep time(sleep_avg) of the process. Advanced Operation System - Linux Scheduling
Normal Process Priority • User could change priority by: • nice() • setpriority() Advanced Operation System - Linux Scheduling
Real-time Process Priority • Each real-time process has a real-time priority: • Ranging from 1 (highest priority) to 99 (lowest priority). • A RT process is replaced by another process only when one of the following events occurs: • Be preempted by another process having higher RT priority. • Performs a blocking operation, and it is put to sleep. • The process is stopped or it is killed. • Invoking the sched_yield( ) system call. • It has exhausted its time quantum(SCHED_RR). Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling
Calculate Time-Slice • In linux 2.6 Advanced Operation System - Linux Scheduling
Calculate Time-Slice • According to the code, time-slice will range from 5~800ms. • The higher the static priority, the longer the time-slice. • In linux 2.4, the time-slice range from 10~100ms • So, linux 2.6 has higher elasticity. Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling
Data Structure (runqueue) • The runqueue data structure is the most important data structure of the Linux 2.6 scheduler. • Each CPU in the system has its own runqueue. • The arrays field of the runqueue is an array consisting of two prio_array_t structures. • One is for active process. • The other is for expired process. Advanced Operation System - Linux Scheduling
Data Structure (runqueue) • Active processes • These runnable processes have not yet exhausted their time quantum and are thus allowed to run. • Expired processes • These runnable processes have exhausted their time quantum and are thus forbidden to run until all active processes expire. Advanced Operation System - Linux Scheduling
Active and Expired Array • Includes 140 doubly linked list heads (one list for each possible process priority) Advanced Operation System - Linux Scheduling
Runqueue(v2.6) vs. Task Queue(v2.4) • Linux 2.6 • Each processor has its own runqueue. Advanced Operation System - Linux Scheduling
Runqueue(v2.6) vs. Task Queue(v2.4) • Linux 2.4 • All processor use a global task queue. Advanced Operation System - Linux Scheduling
The Important Fields of the runqueue Structure Advanced Operation System - Linux Scheduling
The Important Fields of the runqueue Structure Advanced Operation System - Linux Scheduling
The Important Fields of the Process Descriptor Advanced Operation System - Linux Scheduling
bit 2 priority 2 bit 0 priority 0 Queue of runnable tasks for priority 2 Bit Map sched_find_first_bit() schedule() Find the Next Process in the Queue • In linux 2.6 • Use Bitmap technique -> O(1) Advanced Operation System - Linux Scheduling
Find the Next Process in the Queue • Through sched_find_first_bit() to find Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling
Functions Used by the Scheduler • The scheduler relies on several functions in order to do its work; the most important are: • scheduler_tick( ): Keeps the time_slice counter of current up-to-date. • try_to_wake_up( ): Awakens a sleeping process. • recalc_task_prio( ): Updates the dynamic priority of a process. • schedule( ): Selects a new process to be executed. Advanced Operation System - Linux Scheduling
Schedule() in linux 2.6 • The schedule( ) function implements the scheduler. • Its objective is to find a process in the runqueue list and then assign the CPU to it. • schedule() preprocessing • Disable preemption and initializing a few local variables. • Determine the length of time that the previous task has been running. Advanced Operation System - Linux Scheduling
Schedule() in linux 2.6 • schedule( ) to make the process switch • If there are runnable tasks in the runqueue but not in the active priority array, then the active and expired priority arrays are swapped. • Exchanging two array pointer takes constant-time O(1). Advanced Operation System - Linux Scheduling
Schedule() in linux 2.6 • Use bitmap to find the next task. • Or load balancing is made. • Or a switch to the idle task is made. Advanced Operation System - Linux Scheduling
Schedule() in linux 2.6 • schedule( ) after a process switch • TIF_NEED_RESCHED flag clear. • Previous task gets its run time deducted from its sleep_avg. Advanced Operation System - Linux Scheduling
1. Initialize some local variables 2. Release the kernel lock Get the lock of run queue 3. Set the status of previous process 4. Calculate goodness of each process. And get the next process to run. All processes use up it time quantum 5. Start a new epoch and reset the time quantum Select one process 6. Perform context switching Select the same process 7. Return Schedule() in linux 2.4 Advanced Operation System - Linux Scheduling
Schedule() in linux 2.4 • goodness(p, this_cpu, prev->active_mm) • weight = -1 • p is the prev and its SCHED_YIELD flag is set • weight = 0 • p is a conventional process • Has exhausted its quantum (p->counter is zero) • 2 <= weight <= 77 • p is a conventional process • Has not exhausted its quantum • weight >= 1000 • p is a real-time process Advanced Operation System - Linux Scheduling
Schedule() in linux 2.4 • Main Problem of the schedule() in linux 2.4 • Have an extra operation to calculate Weight of each process through goodness(). • When all processes in task queue can’t be execute, , calculate time-slice of all processes again. • This operation takes O(n). Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling
Runqueue Balancing in Multiprocessor Systems • A given CPU can execute only the runnable processes that are contained in the corresponding runqueue. • No runnable process ever appears in two or more runqueues. • The kernel periodically checks whether theworkloads of the runqueues are balanced. • If necessary, moves some processfrom one runqueue to another. Advanced Operation System - Linux Scheduling
The Main Function for Runqueue Balancing • rebalance_tick( ) • The rebalance_tick( ) function is invoked by scheduler_tick( ) once every tick. • Determines whether the time has come to invoke the load_balance( ) function • load_balance() • Checks whether a scheduling domain is significantly unbalanced. • Moving some processes from the busiest group to the runqueue of the local CPU. • move_tasks( ) • Moves processes from a source runqueue to the local runqueue. Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling
System call related to scheduling • nice( ) • Change the static priority of a conventional process. • getpriority( ) • Get the maximum static priority of a group of conventional processes. • setpriority( ) • Set the static priority of a group of conventional processes. • sched_getscheduler( ) • Get the scheduling policy of a process. Advanced Operation System - Linux Scheduling
System call related to scheduling • sched_setscheduler( ) • Set the scheduling policy and the real-time priority of a process. • sched_getparam( ) • Get the real-time priority of a process. • sched_setparam( ) • Set the real-time priority of a process. • sched_yield( ) • Relinquish the processor voluntarily without blocking. Advanced Operation System - Linux Scheduling
System call related to scheduling • sched_get_priority_min() • Get the minimum real-time priority value for a policy. • sched_get_priority_max() • Get the maximum real-time priority value for a policy. • sched_rr_get_interval() • Get a process’s timeslice value for the Round Robin policy. • sched_getaffinity() • Get the CPU affinity mask of a process. • sched_setaffinity() • Set the CPU affinity mask of a process. Advanced Operation System - Linux Scheduling
Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling
Reference • Understanding the Linux Kernel, 3rd Edition Daniel P. Bovet, Marco Cesati • Linux 2.4/2.6 核心排程機制剖析 http://loda.zhupiter.com/Linux%202.4-2.6%20KernelSchedulandRealTimeSupport_2.1.htm • Linux 2.6調度系統分析 http://blog.chinaunix.net/u/7270/showart_300343.html • Previous PowerPoint Slices Advanced Operation System - Linux Scheduling