380 likes | 565 Views
CSC 660: Advanced OS. Scheduling. Topics. Basic Concepts Scheduling Policy The O(1) Scheduler Runqueues Priority Arrays Calculating Priorities and Timeslices. Scheduler Interrupts. Sleeping and Waking. The schedule() function Multiprocessor Scheduling Soft Realtime Scheduling.
E N D
CSC 660: Advanced OS Scheduling CSC 660: Advanced Operating Systems
Topics • Basic Concepts • Scheduling Policy • The O(1) Scheduler • Runqueues • Priority Arrays • Calculating Priorities and Timeslices. • Scheduler Interrupts. • Sleeping and Waking. • The schedule() function • Multiprocessor Scheduling • Soft Realtime Scheduling CSC 660: Advanced Operating Systems
Basic Concepts Scheduler Selects a process to run and allocates CPU to it. Provides semblence of multitasking on single CPU. Scheduler is invoked when: Process blocks on an I/O operation. A hardware interrupt occurs. Process time slice expires. Kernel thread yields to scheduler. CSC 660: Advanced Operating Systems
Types of Processes CPU Bound Spend most time on computations. Example: computer algebra systems. I/O Bound Spend most time on I/O. Example: word processor. Mixed Alternate CPU and I/O activity. Example: web browser. CSC 660: Advanced Operating Systems
Alternating CPU and I/O Bursts CSC 660: Advanced Operating Systems
Scheduling Policy Scheduler executes policy, determining 1. When threads can execute. 2. How long threads can execute. 3. Where threads can execute. CSC 660: Advanced Operating Systems
Scheduling Policy Goals • Efficiency • Maximize amount of work accomplished. • Interactivity • Respond as quickly as possible to user. • Fairness • Don’t allow any process to starve. CSC 660: Advanced Operating Systems
Which goal is most important? Depends on the target audience: Desktop: interactivity But kernel shouldn’t spend all its time in context switch. Server: efficiency But should offer interactivity in order to serve multiple users. CSC 660: Advanced Operating Systems
Pre-2.6 Scheduler O(n) algorithm at every process switch: 1. Scanned list of runnable processes. 2. Computed priority of each task. 3. Selected best task to run. CSC 660: Advanced Operating Systems
The O(1) Scheduler Replacement for O(n) 2.4 scheduler. All algorithms run in constant time. New data structures: runqueues and priority arrays. Performs work in small pieces. Additional new features Improved SMP scalability, including NUMA. Better processor affinity. SMT scheduling. CSC 660: Advanced Operating Systems
Runqueues List of runnable processes on a processor. Each runnable process is a member of precisely one runqueue. Runqueue data: Lock to prevent concurrency problems. Pointers to current and idle tasks. Priority arrays which contain actual tasks. Statistics CSC 660: Advanced Operating Systems
Runqueues struct runqueue { spinlock_t lock; unsigned long nr_running; unsigned long long nr_switches; unsigned long expired_timestamp, nr_uninterruptible; unsigned long long timestamp_last_tick; task_t *curr, *idle; struct mm_struct *prev_mm; prio_array_t *active, *expired, arrays[2]; int best_expired_prio; atomic_t nr_iowait; } CSC 660: Advanced Operating Systems
Priority Arrays Each runqueue contains 2 priority arrays Active array Expired array Basis for O(1) performance: Scheduler always runs highest priority task. Round robin for multiple equal priority tasks. Priority array finds highest task O(1) operation. Using two arrays allows transitions between epochs by switching active and expired pointers. CSC 660: Advanced Operating Systems
Priority Arrays struct prio_array { /* # of runnable tasks in array */ unsigned int nr_active; /* bitmap: pri lvls contain tasks */ unsigned long bitmap[BITMAP_SIZE]; /* 1 list_head per priority (140) */ struct list_head queue[MAX_PRIO]; }; CSC 660: Advanced Operating Systems
Finding Highest Priority Task • Find first bit set in bitmap. sched_find_first_bit() • Read corresponding queue[n] If one process, give CPU to that one. If multiple processes, round-robin schedule all processes in queue for that priority. idx = sched_find_first_bit(array->bitmap); queue = array->queue + idx; next = list_entry(queue->next, task_t, run_list); CSC 660: Advanced Operating Systems
What if no runnable task exists? System runs the swapper task (PID 0). Each CPU has its own swapper process. CSC 660: Advanced Operating Systems
Running out of Timeslice • Remove task from active priority array. • Calculate new priority and timeslice. • Add task to expired priority array. • Swap arrays when active array is empty. array = rq->active; if (unlikely(!array->nr_active)) { rq->active = rq->expired; rq->expired = array; ... } CSC 660: Advanced Operating Systems
Static and Dynamic Priorities Initial priority value called the nice value. Set via the nice() system call. Static priority is nice value + 120. Stored in current->static_prio. Ranges from 100 (highest) to 139 (lowest). Scheduling based on dynamic priority. Bonuses and penalties according to interactivity. Stored in current->prio. Calculated by effective_prio() function. CSC 660: Advanced Operating Systems
Dynamic Priority Policy Increase priority of interactive processes. Favor I/O-bound over CPU-bound. Need heuristic for determining interactivity. Use time spent sleeping vs. runnable time. Sleep average Stored in current->sleep_avg. Incremented when task becomes runnable. Decremented for each timer tick task runs. Scaled to produce priority bonus ranging 0..10. CSC 660: Advanced Operating Systems
Calculating Priority /* Scale sleep_avg to range 0..MAX_BONUS */ #define CURRENT_BONUS(p) \ (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \ MAX_SLEEP_AVG) static int effective_prio(task_t *p) { int bonus, prio; bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; prio = p->static_prio - bonus; return prio; } CSC 660: Advanced Operating Systems
Time Slices Time slice duration critical to performance. Too short: high overhead from context switches. Too long: loss of apparent multitasking. Interactive processes and time slices Interactive processes have high priority. Pre-empt CPU bound tasks on kbd/ptr interrupts. Long time slices slow start of new tasks. CSC 660: Advanced Operating Systems
Calculating Timeslice Initial Timeslice On fork(), parent + child divide remaining time evenly. Stored in current->time_slice. Recalculating Timeslices Time Slice = (140 – static priority) x 20 if static < 120 = (140 – static priority) x 5 if static >= 120 CSC 660: Advanced Operating Systems
Scheduler Interrupts • Scheduler interrupt: scheduler_tick() • Invoked every 1ms by a timer interrupt. • Decrements task’s time slice. • If a higher priority task exists, • Higher priority task is given CPU. • Current task remains in TASK_RUNNING state. • If time slice expired, • Moved to expired priority array. • If highly interactive, may be re-inserted into active priority array. CSC 660: Advanced Operating Systems
Sleeping and Waking Sleeping tasks are not in runqueues. Require no CPU time until awakened. Why sleep? Waiting for I/O. Waiting for other hardware events. Waiting for a kernel semaphore. CSC 660: Advanced Operating Systems
Sleeping DECLARE_WAITQUEUE(wait, current); /* q is a wait queue, wait is a q entry */ add_wait_queue(q, &wait); while (!condition) { set_current_state(TASK_INTERRUPTIBLE); if (signal_pending(current)) /* Handle signal */ schedule() } set_current_state(TASK_RUNNING); remove_wait_queue(q, &wait); CSC 660: Advanced Operating Systems
Waking wake_up() wakes up tasks on event Exclusive: only wakes up one task on waitqueue Non-exclusive: wakes all tasks on waitqueue add_wait_queue TASK_RUNNING TASK_INTERRUPTIBLE Signal wake_up CSC 660: Advanced Operating Systems
Multiprocessor Architectures Classic Memory shared by all CPUs. Hyperthreading Single CPU executing multiple on-chip threads. NUMA CPUs + RAM grouped in local nodes. Reduces contention for accessing RAM. Fast to access local RAM. Slower to access remote RAM. CSC 660: Advanced Operating Systems
Multiprocessor Scheduling Each CPU has own runqueue. Scheduler selects tasks from local runqueue. CPU cache more likely to still be hot. Periodic checks to balance load across CPUs. Called by rebalance_tick(). Loops over all scheduling domains. Calls load_balance() if balance interval expired. CSC 660: Advanced Operating Systems
load_balance() • Acquires this_rq->lock spin lock. • Finds busiest CPU with > 1 process. • If no busiest or current CPU is busiest, terminates. • Obtains spin lock on busiest CPU. • Pull tasks from busiest CPU to local runqueue. • Releases locks. CSC 660: Advanced Operating Systems
move_tasks() Searches for runnable tasks in expired runqueue. Then scans active runqueue. Call pull_task() to move task if all true: Task not currently being executed. Local CPU is in cpus_allowed bitmask. At least one of the following is true: Local CPU is idle. Multiple attempts to move processes have failed. Process is not cache hot. CSC 660: Advanced Operating Systems
Realtime Scheduling Hard Real-time Guaranteed response within defined period. Used for embedded systems: car engines. Ex: RealTime Application Interface (RTAI) Soft Real-time Best effort to meet scheduling constraints. Used for multimedia applications. Currently provided by Linux. Improved by Realtime Preemption Patch. CSC 660: Advanced Operating Systems
Soft Realtime Scheduling Scheduling Priorities RT have higher priorities than any non-RT tasks. RT priorities are static, ranging 1-99, not dynamic. If RT tasks are runnable, no other tasks can run. Scheduling Policies SCHED_NORMAL (non-realtime) SCHED_FIFO SCHED_RR CSC 660: Advanced Operating Systems
Realtime Policies SCHED_FIFO First-in First-out real-time Scheduling Process uses CPU until: It blocks or yields the CPU voluntarily. A higher priority real-time process pre-empts it. SCHED_RR Round Robin real-time scheduling. Process runs for time slice, then waits for other equal priority real-time processes in runqueue. CSC 660: Advanced Operating Systems
Realtime Process Replacement Realtime processes replaced only when: Pre-empted by a high-priority RT process. Process performs a blocking operation. Process is stopped or killed by a signal. Process invokes sched_yield() system call. SCHED_RR process has exhausted its time slice. CSC 660: Advanced Operating Systems
Realtime System Calls Scheduler Policy sched_setscheduler() sched_getscheduler() Priority sched_getparam() sched_setparam() CSC 660: Advanced Operating Systems
Yielding the Processor sched_yield() system call Moves regular task to expired priority array. RT tasks moved to end of priority list. Kernel tasks can yield the CPU too. Call yield() function. CSC 660: Advanced Operating Systems
References • Josh Aas, “Understanding the Linux 2.6.8.1 Scheduler,” http://josh.trancesoftware.com/linux/, 2005. • Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3rd edition, O’Reilly, 2005. • Corbet, “Realtime preemption and read-copy-update,” Linux Weekly News, http://lwn.net/Articles/129511/, March 29, 2005. • Robert Love, Linux Kernel Development, 2nd edition, Prentice-Hall, 2005. • Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, 2005. • RTAI, http://www.rtai.org/, 2006. • Peter Salzman et. al., Linux Kernel Module Programming Guide, version 2.6.1, 2005. • Avi Silberchatz et. al., Operating System Concepts, 7th edition, 2004. • Andrew S. Tanenbaum, Modern Operating Systems, 3rd edition, Prentice-Hall, 2005. CSC 660: Advanced Operating Systems