W4118 Operating Systems Instructor: Junfeng Yang.

Slides:



Advertisements
Similar presentations
CPU Scheduling.
Advertisements

Scheduling Criteria CPU utilization – keep the CPU as busy as possible (from 0% to 100%) Throughput – # of processes that complete their execution per.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 17 Scheduling III.
A. Frank - P. Weisberg Operating Systems Advanced CPU Scheduling.
Operating Systems Chapter 6
Chap 5 Process Scheduling. Basic Concepts Maximum CPU utilization obtained with multiprogramming CPU–I/O Burst Cycle – Process execution consists of a.
Chapter 5 CPU Scheduling. CPU Scheduling Topics: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
CPU Scheduling CS 3100 CPU Scheduling1. Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various.
CPU Scheduling Basic Concepts Scheduling Criteria
Operating Systems 1 K. Salah Module 2.1: CPU Scheduling Scheduling Types Scheduling Criteria Scheduling Algorithms Performance Evaluation.
CS 311 – Lecture 23 Outline Kernel – Process subsystem Process scheduling Scheduling algorithms User mode and kernel mode Lecture 231CS Operating.
Scheduling in Batch Systems
Chapter 6: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 6: CPU Scheduling Basic.
1 Thursday, June 15, 2006 Confucius says: He who play in root, eventually kill tree.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Scheduling in Linux COMS W4118 Spring Scheduling Goals O(1) scheduling; 2.4 scheduler iterated through Run queue on each invocation Task queue.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 5: CPU Scheduling.
Job scheduling Queue discipline.
Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Basic Concepts Maximum CPU utilization.
Chapter 6: CPU Scheduling
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of SJF ProcessArrival TimeBurst Time P P P
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-3 CPU Scheduling Department of Computer Science and Software Engineering.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Scheduler What is the job of.
CS212: OPERATING SYSTEM Lecture 3: Process Scheduling 1.
Operating System Examples - Scheduling
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Scheduling. Alternating Sequence of CPU And I/O Bursts.
CPU Scheduling CSCI 444/544 Operating Systems Fall 2008.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
Alternating Sequence of CPU And I/O Bursts. Histogram of CPU-burst Times.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Lecture 7: Scheduling preemptive/non-preemptive scheduler CPU bursts
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
Chapter 5: Process Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Basic Concepts Maximum CPU utilization can be obtained.
1 11/29/2015 Chapter 6: CPU Scheduling l Basic Concepts l Scheduling Criteria l Scheduling Algorithms l Multiple-Processor Scheduling l Real-Time Scheduling.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 5: CPU Scheduling Basic.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 5 CPU Scheduling Slide 1 Chapter 5 CPU Scheduling.
6.1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
1 Uniprocessor Scheduling Chapter 3. 2 Alternating Sequence of CPU And I/O Bursts.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
Basic Concepts Maximum CPU utilization obtained with multiprogramming
1 Lecture 5: CPU Scheduling Operating System Fall 2006.
Process Scheduling II 3/6/13COMS W4118. Spring 2013, Columbia University. Instructor: Dr. Kaustubh Joshi, AT&T Labs. 1 COMS W4118 Prof. Kaustubh R. Joshi.
CPU SCHEDULING.
EEE Embedded Systems Design Process in Operating Systems 서강대학교 전자공학과
Chapter 6: CPU Scheduling
Chapter 6: CPU Scheduling
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
Operating System Concepts
Chapter 5: CPU Scheduling
3: CPU Scheduling Basic Concepts Scheduling Criteria
Chapter5: CPU Scheduling
Chapter 5: CPU Scheduling
Chapter 6: CPU Scheduling
Outline Scheduling algorithms Multi-processor scheduling
Operating System , Fall 2000 EA101 W 9:00-10:00 F 9:00-11:00
Shortest-Job-First (SJR) Scheduling
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
Presentation transcript:

W4118 Operating Systems Instructor: Junfeng Yang

Last Lecture: Scheduling  Basic concepts  Preemptible vs. Nonpreemptible resources  Dispatcher vs. Scheduler  Preemptive vs. Nonpreemptive scheduling  Scheduling Performance Metrics  Different scheduling algorithms  First Come First Served (FCFS)  Shortest Job First (SJF) Shortest Remaining Time First (SRTF)  Round Robin (RR)  Priority  Gantt Chart for analyzing scheduling algorithms 1

Today: Advanced Scheduling  So far have seen basic scheduling algorithms  No one-size-fits-all scheduler  Workloads  Performance requirements  Today  Advanced Scheduling Concepts Multilevel Queue Scheduling Multiprocessor Scheduling Real-time Scheduling  Linux Scheduling 2

Multilevel Queue  Ready queue is partitioned into separate queues. E.g.,  foreground (interactive), background (batch)  Each queue has its own scheduling algorithm. E.g.,  foreground – RR, background – FCFS  Scheduling must be done between the queues. E.g.,  Fixed priority scheduling; (i.e., serve all from foreground then from background). Problem: starvation.  Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR, 20% to background in FCFS Problem: how to split? 3

Multilevel Queue Example 4

Advantages and Disadvantages of Multilevel Queue  Advantage: flexible  Disadvantage  Complex compared to basic algorithms  How to set priorities or time slice for queues?  Process role may change 5

Multilevel Feedback Queue  Process can move between the various queues; aging can be implemented this way  Multilevel-feedback-queue scheduler defined by the following parameters:  number of queues  scheduling algorithms for each queue  method used to determine when to upgrade a process  method used to determine when to demote a process  method used to determine which queue a process will enter when that process needs service 6

Example of Multilevel Feedback Queue  Three queues:  Q 0 – RR with time quantum 8 milliseconds  Q 1 – RR time quantum 16 milliseconds  Q 2 – FCFS  Scheduling  New process goes to Q 0  When scheduled, if doesn’t finish in 8 ms, moved to Q 1  When scheduled again, if doesn’t finish in 16 ms, moved to Q 2 7

Multilevel Feedback Queues 8

Multiprocessor Scheduling Issues  Shared-memory Multiprocessor  How to allocate processes to CPU? CPU0CPU1CPU2CPU3 processes 9

Symmetric Multiprocessor  Architecture  Small number of CPUs  Same access time to main memory  Private cache CPU0CPU1CPU2CPU3 Shared Memory $$$$ 10

SMP: Global Queue of Processes  One ready queue shared across all CPUs  Advantages  Good CPU utilization  Fair to all processes  Disadvantages  Not scalable (contention for global queue lock)  Poor cache locality  Linux 2.4 uses global queue CPU0CPU1CPU2CPU3 11

SMP: Per-CPU Queue of Processes  Static partition of processes to CPUs  Advantages  Easy to implement  Scalable (no contention on ready queue)  Better cache locality  Disadvantages  Load-imbalance (some CPUs have more processes) Unfair to processes and lower CPU utilization CPU0CPU1CPU2CPU3 12

SMP: Hybrid Approach  Use both global and per-CPU queues  Balance jobs across queues  Processor Affinity  Add process to a CPU’s queue if recently run on the CPU Cache state may still present  Linux 2.6 uses a very similar approach CPU0CPU1CPU2CPU3 13

SMP: “Gang” Scheduling  Multiple processes need coordination  Should be scheduled simultaneously  Dispatcher on each CPU does not act independently  Coscheduling (gang scheduling): run a set of processes simultaneously  Global context-switch across all CPUs CPU0CPU1CPU2CPU3 14

Real-Time Scheduling  Real-time processes have timing constraints  Expressed as deadlines or rate requirements  E.g. gaming, video/music player, autopilot …  Hard real-time systems – required to complete a critical task within a guaranteed amount of time  Soft real-time computing – requires that critical processes receive priority over less fortunate ones  Linux supports soft real-time 15

Today: Advanced Scheduling  Advanced Scheduling Concepts  Multilevel Queue Scheduling  Multiprocessor Scheduling  Real-time Scheduling  Linux Scheduling 16

Linux Scheduling Overview  Multilevel Queue Scheduler  Each queue associated with a priority  A process’s priority may be adjusted dynamically  Two classes of processes  Real-time processes: always schedule highest priority processes FCFS (SCHED_FIFO) or RR (SCHED_RR) for processes with same priority  Normal processes: priority with aging RR for processes with same priority (SCHED_NORMAL) Aging is implemented efficiently 17

Priority Partition  Total 140 priorities [0, 140)  Smaller integer = higher priority  Real-time: [0,100)  Normal: [100, 140)  MAX_PRIO and MAX_RT_PRIO  include/linux/sched.h 18

Linux Scheduling Goals  Boost interactivity  Fast response to user despite high load  Achieved by inferring interactive processes and dynamically increasing their priorities  Avoid starvation  Scale well with number of processes  O(1) scheduling overhead  SMP goals  Scale well with number of processors  Load balance: no CPU should be idle if there is work  CPU affinity: no random bouncing of processes  Reference: Documentation/sched-design.txt 19

runqueue data structure  kernel/sched.c  struct prio_array  Array of priority queues  struct runqueue  Two arrays, active and expired 20

task_struct scheduling related fields  static_prio and time_slice: used to compute and store time-slice  prio: dynamic priority  Index to prio_array  rt_priority: real time priority  prio = 99 – rt_priority  include/linux/sched.h 21

Scheduling Algorithm 1. Find highest priority non-empty queue in rq- >active 2. next = first process on that queue 3. Adjust next’s priority 4. Context switch to next 5. When next used up its time slice, insert next to the right queue and call schedule again schedule() in kernel/sched.c 22

Find Highest Priority Non-empty Queue  Use the bitmap field of runqueue  140 queues  5 integers  Time complexity: O(1)  depends on the number of priority levels, not the number of processes  Implementation: only a few compares to find the first that is non-zero  Hardware instruction to find the first 1-bit bsfl on Intel  sched_find_first_bit() in include/asm- i386/bitops.h 23

Adjusting Priority  Goal: dynamically increase priority of interactive process  How to determine interactive?  Sleep ratio  Mostly sleeping: I/O bound  Mostly running: CPU bound  Implementation: sleep_avg in task_struct  Before switching out a process, subtract from sleep_avg how many ticks a task ran schedule()  Before switching in a process, add to sleep_avg how many ticks it was blocked up to MAX_SLEEP_AVG (10 ms) schedule()  recalc_task_prio()  effective_prio() 24

Calculating Time Slices  Higher priority processes also get bigger time- slice  task_timeslice() in sched.c  If (static_priority < 120) time_slice = (140- static_priority) * 20  If (static_priority >= 120) time_slice = (140- static_priority) * 5  How to set static_priority?  Default: 120 (even for realtime processes)  Set use sys_nice() or sys_setpriority() Both call set_user_nice() 25

Example Time Slices Priority:Static PriNicenessQuantum Highest ms High ms Normal ms Low ms Lowest ms 26

On each timer interrupt …  scheduler_tick()  Called on each tick timer_interrupt  do_timer_interrupt  do_timer_interrupt_hook  update_process_times  If realtime and SCHED_FIFO, do nothing  SCHED_FIFO is non-preemptive  If realtime and SCHED_RR and used up time slice, move to end of rq->active[prio]  If SCHED_NORMAL and used up time slice  If not interactive or starving expired queue, move to end of rq- >expired[prio]  Otherwise, move to end of rq->active[prio] Boost interactive  Else // SCHED_NORMAL, and not used up time slice  Break large time slice into pieces TIMESLICE_GRANULARITY

28 Aging: the Traditional Algorithm for(pp = proc; pp < proc+NPROC; pp++) { if (pp->prio != MAX) pp->prio++; if (pp->prio > curproc->prio) reschedule(); } Problem: O(N). Every process is examined on each schedule() call! This code is taken almost verbatim from 6 th Edition Unix, circa 1976.)

Simulate Aging  struct runqueue has active and expired  schedule() only runs processes from active, and puts them on expired when use up time slice  When a queue in active is empty, look for next-highest priority queue  After running all queues on active, swap active and expired  Advantage: O(1)  Processes are touched only when they start or stop running  schedule() in kernel/sched.c 29

Real-Time Scheduling  Linux has soft real-time scheduling  No hard real-time guarantees  All real-time processes are higher priority than any conventional processes  Processes with priorities [0, 99] are real-time  saved in rt_priority in the task_struct  scheduling priority of a real time task is: 99 - rt_priority  Process can be converted to real-time via sched_setscheduler system call 30

Real-Time Policies  First-in, first-out: SCHED_FIFO  Static priority  Process is only preempted for a higher-priority process  No time quanta; it runs until it blocks or yields voluntarily  RR within same priority level  Round-robin: SCHED_RR  As above but with a time quanta  Normal processes have SCHED_OTHER scheduling policy 31

Multiprocessor Scheduling  Each processor has a separate run queue  Each processor only selects processes from its own queue to run  Yes, it’s possible for one processor to be idle while others have jobs waiting in their run queues  Periodically, the queues are rebalanced: if one processor’s run queue is too long, some processes are moved from it to another processor’s queue 32

Locking Runqueues  To rebalance, the kernel sometimes needs to move processes from one runqueue to another  This is actually done by special kernel threads  Naturally, the runqueue must be locked before this happens  The kernel always locks runqueues in order of increasing indexes  Why? Deadlock prevention! 33

Processor Affinity  Each process has a bitmask saying what CPUs it can run on  Normally, of course, all CPUs are listed  Processes can change the mask  The mask is inherited by child processes (and threads), thus tending to keep them on the same CPU  Rebalancing does not override affinity 34

 To keep all CPUs busy, load balancing pulls tasks from busy runqueues to idle runqueues.  If schedule finds that a runqueue has no runnable tasks (other than the idle task), it calls load_balance  load_balance also called via timer  schedule_tick calls rebalance_tick  Every tick when system is idle  Every 100 ms otherwise Load Balancing 35

 load_balance looks for the busiest runqueue (most runnable tasks) and takes a task that is (in order of preference):  inactive (likely to be cache cold)  high priority  load_balance skips tasks that are:  likely to be cache warm (hasn't run for cache_decay_ticks time)  currently running on a CPU  not allowed to run on the current CPU (as indicated by the cpus_allowed bitmask in the task_struct) Load Balancing 36

Optimizations  If next is a kernel thread, borrow the MM mappings from prev  User-level MMs are unused.  Kernel-level MMs are the same for all kernel threads  If prev == next  Don’t context switch 37