370 likes | 576 Views
Process Scheduling. Fred Kuhns. Policy versus Mechanism. Policies set rules for determining when to switch and which process/thread to run Implementation consists of the data structures and algorithms used to implement policy
E N D
Process Scheduling Fred Kuhns
Policy versus Mechanism • Policies set rules for determining when to switch and which process/thread to run • Implementation consists of the data structures and algorithms used to implement policy • Policy and Implementation influenced by platform - for example, context switch cost • Policy must balance needs of different application types: • Interactive, Batch and Real-Time CS523 – Operating Systems
Consider Three Basic Policies • First-In First-Out (FIFO) • runs to completion • Round Robin (RR) • runs for a specified time quantum • Time-Sharing (TS) • Multilevel feedback queues CS523 – Operating Systems
Platform Issues • Interrupt latency • Clock resolution • Cost of a context switch • saving processor state and loading new • instruction and data cache misses/flushes • memory map cache (TLB) • FPU state CS523 – Operating Systems
Typical Scheduler Goals • Interactive • shells, GUI. • Spend most of their time waiting for I/O. • Minimize perceived delay (50-150msec) • Batch • compiles, computations. • Optimize throughput. • Real-time • Require predictable behavior. • May require guarantees on throughput, latency or delay. CS523 – Operating Systems
Some RT Characteristics • Determinism - one measure: delay between interrupt assertion and executing the ISR • Responsiveness - time to service interrupt • User Control • Reliability • Fail-soft operation CS523 – Operating Systems
Clock Interrupts • How system keeps track of time • implemented using a hardware clock interrupt • interrupt at periodic intervals (clock tick) • typically 10msec, see tick frequency defined by HZ in param.h • High priority, second to power-failure interrupt (NMI). CS523 – Operating Systems
Interrupt Latency • time to recognize interrupt and execute first instruction of ISR - Determinism • hardware, for example will CPU finish current instruction. • kernel time to dispatch interrupt to correct ISR • plus time to run ISR • affected by interrupt nesting • plus worst-case interrupt disable time CS523 – Operating Systems
Clock Interrupt Handler • Update CPU usage for current process • scheduler functions - priority computation, time-slice expiration (major/minor ticks) • quota policing • update time-of-day and other clocks • callout queue processing • process alarms • run system processes CS523 – Operating Systems
Callout queue • Queue of functions to be processed at a particular tick • system context, base interrupt priority (sw int) • Example: • packet retransmission, system management functions, device monitoring and polling • for real-time systems insertion time is important • Typical to optimize lookup time not insertion time • time-to-fire, • absolute time, • timing wheel (fixed size circular array) CS523 – Operating Systems
Alarms • BSD: alarm(), setitimer() • SVR4: alarm(), hrtsys() • Real-time - actual elapsed time • sends SIGALRM • profiling - execution time • sends SIGPROF • virtual time - user mode time • sends SIGVTALRM CS523 – Operating Systems
BSD Scheduler • Policy - Multilevel Feedback queues • Policy Goal - good response time for interactive tasks while guaranteeing batch job progress • Priority based, dynamically adjusted • Preemptive time-slicing (time quantum) CS523 – Operating Systems
4.4 BSD Process Scheduling • Scheduling priority range, hi to lo: 0 - 127 • 0-49 kernel mode • 50-127 user mode • 32 run queues (prio/4 = run queue) • priority adjusted based on resource usage. • time quantum set to 0.1 second for over 15 years! • Sleep priorities CS523 – Operating Systems
Scheduling Related Attributes Predefined Process Priorities PROC structure PSWP 0 while swapping process PVM 4 wait for memory PINOD 8 wait for file control info PRIBIO 16 wait on disk I/O PVFS 20 wait for kernel-level FS lock PZERO 22 baseline priority PSOCK 24 wait on socket PWAIT 32 wait for child to exit PLOCK 36 wait for user-level FS lock PPAUSE 40 wait for signal to arrive PUSER 50 base user priority p_priority (kernel pri) p_usrpri (user pri) p_estcpu p_slptime ... CS523 – Operating Systems
Calculation of Priority • Proc structure: • p_estcpu - estimate of cpu utilization • p_nice - user-settable (-20 to 19, default = 0) • priority (p_usrpri) recalculated every 4 clock ticks • p_estcpu incremented each clock tick process is running • every second CPU usage is decayed • sleeping processes are ignored CS523 – Operating Systems
BSD Formulas • Priority calculation (PUSER = 50) • p_usrpri = PUSER + (p_estcpu/4) + 2 p_nice • Decay calculation - schedcpu ()each second • p_estcpu = (2·load)/(2·load+1)·p_estcpu+p_nice • ex, load=1: p_estcpu = 0.66T4 + … + 0.13T0 • Processes sleeping for > 1 second • p_slptime set to 0 then incremented by 1 ea. second • p_estcpu = p_estcpu·((2·load)/(2·load+1))p_slptime CS523 – Operating Systems
Context switch on 4.4 BSD • Synchronous vs. asynchronous • Interprocess: voluntary vs. involuntary • voluntary - process blocks because it requires an unavailable resource. sleep () • involuntary - time slice expires or higher priority process runnable. mi_switch () CS523 – Operating Systems
BSD - Voluntary Context Switch • Invoke sleep() with a wait channel (resource address) and (kernel) priority • sleeping process organized as an array of queues. Wait channel is hashed to find bin. • sleep() raises priority level splhigh (block interrupts). Set kernel priority (p_priority) • wakeup(): remove process(es) from queue splhigh and recalculate user priority (p_usrpri). CS523 – Operating Systems
BSD - Involuntary context switch • Results from an asynchronous event • kernel schedules an AST, need_resched (), and sets global flag want_resched • current proc checks this flag before returning to user mode. If set, then call mi_switch () • Note: BSD does not preempt process in kernel mode. CS523 – Operating Systems
BSD Interprocess Context Switch • Change user and kernel context • All user mode state located in • kernel-mode HW state - stored in PCB (u area) • user-mode HW state - kernel stack (u area) • proc structure (not swapped) • kernel changes the current process pointers and loads new state. CS523 – Operating Systems
Selecting a process to run • cpu_switch (), called by mi_switch () • block interruptsandcheck whichqs for nonempty run queue. If non, unblock interrupts and loop. • Remove first process in queue. If queue is not empty then reset bit in whichqs • clear curproc and want_resched • set new process (context switch) and unblock interrupts CS523 – Operating Systems
Limitations of the BSD Model • Limited scalability • No resource guarantees • non-deterministic, unbounded delays (priority inversion) • limited application control over priorities CS523 – Operating Systems
Scheduler Implementations • SVR4 • Solaris • Mach • Digital UNIX • Other RT CS523 – Operating Systems
SVR4 Scheduler • Redesigned from traditional approach • Separate policy from implementation • Define scheduling classes • define framework with well defined interfaces • Attempt to bound dispatch latencies • Enhance application control CS523 – Operating Systems
SVR4 Scheduling Classes • Scheduler represents an abstract base class • defines interface • performs class independent processing • Derived classes are specific implementations of a scheduling policy • priority computation, range of priorities, whether quantums are used or if priorities can vary dynamically. CS523 – Operating Systems
SVR4 - Issues Addressed • Dispatch Latencies - time between a process becoming runnable and when it executes. • Does not include interrupt processing. Includes nonpreemptive kernel processing and context switch time. • Kernel preemption points - PREMPT checks kprunrun at well defined points in the kernel • runrun also used as in traditional implementations • Response time - total time from event to application response. CS523 – Operating Systems
SVR4 - Class-Independent • Responsible for • context switching, run queue management and preemption. • Highest (global) priority always runs • priority range: 0 - 160, each with own queue • Default allocations: • real-time 100-159, • system 60-99, • timesharing 0-59 CS523 – Operating Systems
SVR4 - Scheduler Interface CL_TICK CL_FORK, CL_FORKRET CL_ENTERCLASS, CL_EXITCLASS CL_SLEEP, CL_WAKEUP CL_PREEMPT CL_YIELD CL_SETRUN ... CS523 – Operating Systems
SVR4 Class Implementations • Time-Sharing: event driven scheduling. • priority changed dynamically, round-robin within priority, time-slice and priority static parameter table. • System - fixed priority (FIFO). System tasks. • Real-time: fixed priority and time quantum. CS523 – Operating Systems
Solaris Overview • Multithreaded, Symmetric Multi-Processing • Preemptive kernel - protected data structures • Interrupts handled using threads • MP Support - per cpu dispatch queues, one global kernel preempt queue. • System Threads • Priority Inheritance • Turnstiles rather than wait queues CS523 – Operating Systems
Hidden Scheduling • Hidden Scheduling - kernel performs work asynchronously on behalf of threads, without considering priority of requester • Examples: STREAMS process and callout queue • Solaris addresses by using system threads • run at kernel priority which is lower than the RT class. • Callout processing is also a problem. Solaris uses two different callout queues: real-time and non-real-time (callout thread). CS523 – Operating Systems
Priority Inversion • Low priority thread holds a resource required by a higher priority thread. • Partial solution - Priority Inheritance. • High priority thread “lends” its priority to the lower priority thread. • Must be transitive • kernel keeps a synchronization chain CS523 – Operating Systems
Priority Inheritance in Solaris • Each thread has a global and inherited priority. • Object must keep a reference to the owner • If requester priority > owner, then owner priority raised to that of the requesters • This works with Mutexes, owner always known • Not used for semaphores or condition variables • For reader/write, • owner of record inherits priority, only partial solution CS523 – Operating Systems
MACH • Inherited base scheduling priority which is combined with a CPU usage factor • CPU usage factor decayed 5/8 each second inactive • threads set own priority after waking up. • Clock handler charges current thread. • Every 2 seconds, system thread scans run queue and recomputes priorities (addresses starvation) • Fixed quantums, preemptive scheduling • handoff scheduling - used by IPC CS523 – Operating Systems
MACH MP Support • No cross processor interrupts • processor sets • thread runs on any of the processors from the assigned set. • Processor allocation can be handled by a user-level server • Gang scheduling • dedicate one processor per thread. • Minimize barrier synchronization delay CS523 – Operating Systems
Digital UNIX • Time-sharing and real-time classes: SCHED_OTHER, SCHED_FIFO, SCHED_RR, • Highest priority process is always run • Priority range: 0-60 (0-29 TS, 20-31 SYS, 32-63 RT) • nonpreemptive kernel • no control for priority inversion CS523 – Operating Systems
Digital UNIX - MP • Per processor dispatch queues • Scheduler will move threads between queues to balance load • soft affinity CS523 – Operating Systems