Linux Scheduler. Linux Scheduler



Similar documents
Linux scheduler history. We will be talking about the O(1) scheduler

W4118 Operating Systems. Instructor: Junfeng Yang

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

Process Scheduling II

Real-Time Scheduling 1 / 39

Scheduling policy. ULK3e 7.1. Operating Systems: Scheduling in Linux p. 1

ò Paper reading assigned for next Thursday ò Lab 2 due next Friday ò What is cooperative multitasking? ò What is preemptive multitasking?

Linux O(1) CPU Scheduler. Amit Gud amit (dot) gud (at) veritas (dot) com

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

Scheduling. Scheduling. Scheduling levels. Decision to switch the running process can take place under the following circumstances:

Completely Fair Scheduler and its tuning 1

An Implementation Of Multiprocessor Linux

Chapter 5 Linux Load Balancing Mechanisms

SYSTEM ecos Embedded Configurable Operating System

Scheduling. Yücel Saygın. These slides are based on your text book and on the slides prepared by Andrew S. Tanenbaum

Comp 204: Computer Systems and Their Implementation. Lecture 12: Scheduling Algorithms cont d

Operating Systems. III. Scheduling.

Thomas Fahrig Senior Developer Hypervisor Team. Hypervisor Architecture Terminology Goals Basics Details

Readings for this topic: Silberschatz/Galvin/Gagne Chapter 5

Linux Process Scheduling Policy

CPU Scheduling. Basic Concepts. Basic Concepts (2) Basic Concepts Scheduling Criteria Scheduling Algorithms Batch systems Interactive systems

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

CPU Scheduling Outline

Overview of the Linux Scheduler Framework

Linux Load Balancing

Scheduling 0 : Levels. High level scheduling: Medium level scheduling: Low level scheduling

White Paper Perceived Performance Tuning a system for what really matters

CPU Scheduling. CSC 256/456 - Operating Systems Fall TA: Mohammad Hedayati

Chapter 5 Process Scheduling

Chapter 5: CPU Scheduling. Operating System Concepts 8 th Edition

2. is the number of processes that are completed per time unit. A) CPU utilization B) Response time C) Turnaround time D) Throughput

Process Scheduling CS 241. February 24, Copyright University of Illinois CS 241 Staff

Tasks Schedule Analysis in RTAI/Linux-GPL

Job Scheduling Model

Processor Scheduling. Queues Recall OS maintains various queues

CPU Scheduling. Multitasking operating systems come in two flavours: cooperative multitasking and preemptive multitasking.

A Survey of Parallel Processing in Linux

Process Scheduling in Linux

Konzepte von Betriebssystem-Komponenten. Linux Scheduler. Valderine Kom Kenmegne Proseminar KVBK Linux Scheduler Valderine Kom

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

Linux Process Scheduling. sched.c. schedule() scheduler_tick() hooks. try_to_wake_up() ... CFS CPU 0 CPU 1 CPU 2 CPU 3

Road Map. Scheduling. Types of Scheduling. Scheduling. CPU Scheduling. Job Scheduling. Dickinson College Computer Science 354 Spring 2010.

CPU Scheduling. Core Definitions

The Real-Time Operating System ucos-ii

VIRTUALIZATION AND CPU WAIT TIMES IN A LINUX GUEST ENVIRONMENT

CPU SCHEDULING (CONT D) NESTED SCHEDULING FUNCTIONS

CPU Scheduling. CPU Scheduling

ò Scheduling overview, key trade-offs, etc. ò O(1) scheduler older Linux scheduler ò Today: Completely Fair Scheduler (CFS) new hotness

Objectives. Chapter 5: Process Scheduling. Chapter 5: Process Scheduling. 5.1 Basic Concepts. To introduce CPU scheduling

REAL TIME OPERATING SYSTEMS. Lesson-10:

Operating Systems 4 th Class

Chapter 2: OS Overview

Multilevel Load Balancing in NUMA Computers

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Operatin g Systems: Internals and Design Principle s. Chapter 10 Multiprocessor and Real-Time Scheduling Seventh Edition By William Stallings

Real-time KVM from the ground up

First-class User Level Threads

CPU performance monitoring using the Time-Stamp Counter register

Deciding which process to run. (Deciding which thread to run) Deciding how long the chosen process can run

Effective Computing with SMP Linux

OS OBJECTIVE QUESTIONS

Why Relative Share Does Not Work

Real Time Programming: Concepts

Chapter 6, The Operating System Machine Level

OPERATING SYSTEMS SCHEDULING

Linux Driver Devices. Why, When, Which, How?

Objectives. Chapter 5: CPU Scheduling. CPU Scheduler. Non-preemptive and preemptive. Dispatcher. Alternating Sequence of CPU And I/O Bursts

CS11 Java. Fall Lecture 7

Lecture 3 Theoretical Foundations of RTOS

Threads Scheduling on Linux Operating Systems

4. Fixed-Priority Scheduling

ELEC 377. Operating Systems. Week 1 Class 3

Kernel Synchronization and Interrupt Handling

Understanding Linux on z/vm Steal Time

White Paper. Real-time Capabilities for Linux SGI REACT Real-Time for Linux

Chapter 2 Process, Thread and Scheduling

Announcements. Basic Concepts. Histogram of Typical CPU- Burst Times. Dispatcher. CPU Scheduler. Burst Cycle. Reading

Jorix kernel: real-time scheduling

CS414 SP 2007 Assignment 1

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

Linux process scheduling

Why Computers Are Getting Slower (and what we can do about it) Rik van Riel Sr. Software Engineer, Red Hat

CPU Scheduling 101. The CPU scheduler makes a sequence of moves that determines the interleaving of threads.

Kernel Optimizations for KVM. Rik van Riel Senior Software Engineer, Red Hat June

Module 8. Industrial Embedded and Communication Systems. Version 2 EE IIT, Kharagpur 1

Linux Kernel Networking. Raoul Rivas

Process Scheduling in Linux

Shared Address Space Computing: Programming

Implementing and Using Execution Time Clocks in Ada Hard Real-Time Applications

Lecture Outline Overview of real-time scheduling algorithms Outline relative strengths, weaknesses

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Processes and Non-Preemptive Scheduling. Otto J. Anshus

ICS Principles of Operating Systems

Improvement of Scheduling Granularity for Deadline Scheduler

Transcription:

or or Affinity Basic Interactive es 1 / 40

Reality... or or Affinity Basic Interactive es The Linux scheduler tries to be very efficient To do that, it uses some complex data structures Some of what it does actually contradicts the schemes we ve been discussing... 2 / 40

or or Affinity Basic Interactive es Use large quanta for important processes Modify quanta based on CPU use Bind processes to CPUs Do everything in O(1) time 3 / 40

or or or Affinity Basic Interactive es Have a separate run queue for each processor 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 4 / 40

or Affinity or or Affinity Basic Interactive es Each process has a bitmask saying what CPUs it can run on Normally, of course, all CPUs are listed es 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 5 / 40

Basic or or Affinity Basic Interactive es Find the highest-priority queue with a runnable process Find the first process on that queue Calculate its quantum size Let it run When its time is up, put it on the expired list Repeat 6 / 40

or or Affinity Basic Interactive es 140 separate queues, one for each priority level Actually, that number can be changed at a given site Actually, two sets, active and expired Priorities 0-99 for real-time processes Priorities 100-139 for normal processes; value set via nice() system call 7 / 40

or or Affinity Basic Interactive es There is a bit map indicating which queues have processes that are ready to run Find the first bit that s set: 140 queues 5 integers Only a few compares to find the first that is non-zero Hardware instruction to find the first 1-bit Time depends on the number of priority levels, not the number of processes 8 / 40

or or Affinity Basic Interactive es Calculate Quantum = (140 SP) 20 if SP < 120 (140 SP) 5 if SP 120 where SP is the static priority Higher priority process get longer quanta Basic idea: important processes should run longer Other mechanisms used for quick interactive response 9 / 40

or or Affinity Basic Interactive es Static Pri Niceness Quantum Highest Static Pri 100 20 800 ms High Static Pri 110-10 600 ms Normal 120 0 100 ms Low Static Pri 130 +10 50 ms Lowest Static Pri 139 +20 5 ms 10 / 40

or or Affinity Basic Interactive es Dynamic priority is calculated from static priority and average sleep time When process wakes up, record how long it was sleeping, up to some maximum value When the process is running, decrease that value each timer tick Roughly speaking, the bonus is a number in [0, 10] that measures what percentage of the time the process was sleeping recently; 5 is neutral, 10 helps priority by 5, 0 hurts priority by 5 DP = max(100, min(sp bonus + 5, 139)) 11 / 40

Interactive es or or Affinity Basic Interactive es A process is interactive if bonus 5 S/4 28 Low-priority processes have a hard time becoming interactive A default priority process becomes interactive when its sleep time is greater than 700 ms 12 / 40

or or Affinity Basic Interactive es At every time tick, decrease the quantum of the current running process If the time goes to zero, the process is done If the process is non-interactive, put it aside on the expired list If the process is interactive, put it at the end of the current priority queue If there s nothing else at that priority, it will run again immediately Of course, by running so much is bonus will go down, and so will its priority and its interative status 13 / 40

or or Affinity Basic Interactive es There are two sets of 140 queues, active and expired The system only runs processes from active queues, and puts them on expired queues when they use up their quanta When a priority level of the active queue is empty, the scheduler looks for the next-highest priority queue After running all of the active queues, the active and expired queues are swapped There are pointers to the current arrays; at the end of a cycle, the pointers are switched 14 / 40

or or Affinity Basic Interactive es struct runqueue { struct prioarray *active; struct prioarray *expired; struct prioarray arrays[2]; }; struct prioarray { int nr_active; /* # Runnable */ unsigned long bitmap[5]; struct list_head queue[140]; }; 15 / 40

or or Affinity Basic Interactive es struct prioarray *array = rq->active; if (array->nr_active == 0) { rq->active = rq->expired; rq->expired = array; } 16 / 40

or or Affinity Basic Interactive es Why is it done this way? It avoids the need for traditional aging Why is aging bad? It s O(n) at each clock tick 17 / 40

or or Affinity Basic Interactive es for(pp = proc; pp < proc+nproc; pp++) { if (pp->prio!= MAX) pp->prio++; if (pp->prio > curproc->prio) reschedule(); } Every process is examined, quite frequently (This code is taken almost verbatim from 6th Edition Unix, circa 1976.) 18 / 40

or or Affinity Basic Interactive es es are touched only when they start or stop running That s when we recalculate priorities, bonuses, quanta, and interactive status There are no loops over all processes or even over all runnableprocesses 19 / 40

or or Affinity Basic Interactive es 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 address Why? Deadlock prevention! (It is good for something... 20 / 40

or or Affinity Basic Interactive es Linux has soft real-time scheduling es with priorities [0, 99] are real-time All real-time processes are higher priority than any conventional processes Two real-time scheduling systems, FCFS and round-robin First-come, first-served: process is only preempted for a higher-priority process; no time quanta Round-robin: real-time processes at a given level take turns running for their time quantum 21 / 40

Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers 22 / 40

Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers es need to wait for events Waiting is done by putting the process on a wait queue Wakeups can happen too soon; the process must check its condition and perhaps go back to sleep 23 / 40

Sleeping Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers DECLARE_WAIT_QUEUE(wait, current); /* Sleep on queue q */ 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); 24 / 40

Waking Up a Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers You don t wake a process, you wake a wait queue There may be multiple processes waiting for the event, i.e., several processes trying to read a single disk block The condition may not, in fact, have been satisfied That s why the sleep routine has a loop 25 / 40

Scheduler-Related System Calls Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers nice() Lower a process static priority getpriority()/setpriority() Change priorities of a process group sched getscheduler()/sched setscheduler() Set scheduling policy and parameters. (Many more starting with sched ; use man -k to learn their names.) 26 / 40

Major Kernel Functions Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers scheduler tick() Called each timer tick to update quanta try to wakeup() Attempts to wake a process, put in on a run queue, rebalance loads, etc. recalc task prio() update average sleep time an dynamic priority schedule() Pick the next process to run rebalance tick() Check if load-banlancing is needed 27 / 40

Fair Share Sleeping Waking Up a Scheduler-Related System Calls Major Kernel Functions Fair Share Timers Suppose we wanted to add a fair share scheduler to Linux What should be done? Add a new scheduler type for sched setscheduler() Calculate process priority, interactivity, bonus, etc., based on all processes owned by that user How can that be done efficiently? What sorts of new data structures are needed? 28 / 40

Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Timers 29 / 40

Why Does the Kernel Need Timers? Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Animated applications Screen-savers Time of day for file timestamps Quanta! 30 / 40

Two Basic Functions Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Time of day (especially as a service to applications) Interval timers something should happen n ms from now 31 / 40

Timer Types Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Clock: tracks time and date, even if the computer is off; can interrupt at a certain rate or at a certain time. Use by Linux only at boot time to get time of day Time Stamp Counter: ticks once per CPU clock; provides very accurate interval timing Programmable Interval Timer: generates periodic interrupts. On Linux, the rate, called HZ, is usually 1000 Hz (100 Hz on slow CPUs) A variety of special, less common (and less used) timers 32 / 40

Timer Ticks Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Linux programs a timer to interrupt at a certain rate Each tick, a number of operations are carried out Three most important Keeping track of time Invoking dynamic timer routines Calling scheduler tick() The system uses the best timer available 33 / 40

Jiffies Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Each timer tick, a variable called jiffies is incremented It is thus (roughly) the number of HZ since system boot A 32-bit counter incremented at 1000 Hz wraps around in about 50 days We need 64 bits but there s a problem 34 / 40

Potent and Evil Magic Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls A 64-bit value cannot be accessed atomically on a 32-bit machine A spin-lock is used to synchronize access to jiffies 64; kernel routines call get jiffies 64() But we don t want to have to increment two variables each tick Linker magic is used to make jiffies the low-order 32 bits of jiffies 64 Ugly! 35 / 40

Time of Day Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls The time of day is stored in xtime, which is a struct timespec It s incremented once per tick Again, a spin-lock is used to synchronize access to it The apparent tick rate can be adjusted slightly, via the adjtimex() system call 36 / 40

Kernel Timers Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Two types of timers use by kernel routines Dynamic timer call some routine after a particular interval Delay loops tight spin loops for very short delays User-mode interval timers are similar to kernel dynamic timers 37 / 40

Dynamic Timers Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Specify an interval, a subroutine to call, and a parameter to pass to that subroutine Parameter used to differentiate different instantiations of the same timer if you have 4 Ethernet cards creating dynamic timers, the parameter is typically the address of the per-card data structure Timers can be cancelled; there is (as usual) a delicate synchronization dance on multiprocessors. See the text for details 38 / 40

Delay Functions Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls Spin in a tight loop for a short time microseconds or nanoseconds Nothing else can use that CPU during that time, except via interrupt Used only when the overhead of creating a dynamic timer is too great for a very short delay 39 / 40

System Calls Timers Why Does the Kernel Need Timers? Two Basic Functions Timer Types Timer Ticks Jiffies Potent and Evil Magic Time of Day Kernel Timers Dynamic Timers Delay Functions System Calls time() and gettimeofday() adjtimex() tweaks apparent clock rate (even the best crystals drift; see the Remote Physical Device Fingerprinting paper from my COMS E6184 class setitimer() and alarm() interval timers for applications 40 / 40