Download - Short refresher on kernel scheduling
SHORT REFRESHER ON SCHEDULING
What is scheduler ?
Overview of Processes and Threads
Programs and Processes
Threads
Scheduling
CPU and I/O-bound Threads
Context Switching
Linux Processes/Threads
Programs and Processes
A program is a combination of instructions and data put together to perform a task when executed.
A process is an instance of a program (what one might call a “running” program).
Processes types
Interactive processes
command shells, text editors, and graphical applications
Batch processes
compilers, database search engines, and scientific
computations
Real-time processes
video and sound applications, robot controllers, and
programs that collect data from physical sensors
Threads
A process can have multiple threads of execution that work together to accomplish its goals.
A kernel must keep track of each thread’s stack
Threads can share address.
Only one thread may be executing on a CPU at any given time
Scheduling
Allow more than one process to exist at any given time.
Each process is allowed to run as if it were the only process on the system.
Processes do not need to be aware of any other processes.
Why ?
Scheduling (cont…)
Threads are scheduled to
run for very short periods.
Usually the scheduler runs in its own thread, which is woken up by a timer interrupt.
Otherwise it is invoked via a system call or another kernel thread that wishes to yield the CPU.
CPU and I/O-bound Threads
Many schedulers do care about whether or not a thread should be considered CPU or I/O bound.
Schedulers tend to give I/O-bound threads priority access to CPUs.
Context Switching
Context switching is the process of switching from one thread of execution to another.
Context switching code :
change current virtual memory mapping -
include/asm-generic/mmu_context.h.
perform CPU context switch -
include/asm-generic/system.h .
Linux Processes/Threads
In Linux, all threads are simply processes that might share certain resources.
Process in Linux is a group of threads that share a thread group ID (TGID)
Linux CPU Scheduler Goals
Efficiency
Interactivity
Fairness and Preventing Starvation
SMP scheduling
SMT Scheduling
Efficiency
Context switching is expensive.
Scheduler’s code is run quite often.
Efficiency suffers for the sake of other goals
Interactivity
Important for desktop environments.
Interactivity is important:
- mouse click response time
Interactivity is less important:
- the Linux kernel compilation time
Fairness and Preventing Starvation
No thread ever starves
Fairness does not imply giving every process the same priority and same CPU time (not all processes are created equal)
What is fairness?
SMP Scheduling
Multiprocessing supports
No task is executing on more than one CPU
What can be reason to prefer one CPU over another?
SMT Scheduling
Simultaneous multithreading (SMT), is a technique for improving the overall efficiency of superscalar CPUs with hardware multithreading.
SMT permits multiple independent threads of execution to better utilize the resources provided by CPU.
Soft Real-Time Scheduling
The Linux scheduler supports soft real-time scheduling. This means that it can effectively schedule tasks that have strict timing requirements.
CPU Scheduler Algorithms Overview:
First Come First Served (FCFS)
Shortest Job First (SJF)
Priority scheduling
Round robin
Earliest Deadline First (EDF)
Multilevel queue
Multilevel feedback queue
Linux kernel scheduler source code
kernel/sched.c
Contains more then 10.000 lines of code.
Main functions: schedule() - is the main scheduler function.
activate_task() - move a task to the runqueue.
deactivate_task() - remove a task from the runqueue.
try_to_wake_up() - wake up a thread.
wake_up_new_task() - wake up a newly created task for the first time.
migrate_task () - Move (not current) task off this CPU, onto destination CPU.
System Calls Related to Scheduling
nice( ) - Change the priority of a conventional process.
getpriority( ) - Get the maximum priority of a group of conventional processes.
setpriority( ) - Set the priority of a group of conventional processes.
sched_getscheduler( ) - Get the scheduling policy of a process.
System Calls Related to Scheduling (cont …)
sched_setscheduler( ) - Set the scheduling policy and priority of a process.
sched_getparam( ) - Get the scheduling priority of a process.
sched_setparam( ) - Set the priority of a process.
sched_yield( ) - Relinquish the processor voluntarily without blocking.
System Calls Related to Scheduling (cont …) (cont …)
sched_get_ priority_min( ) - Get the minimum priority value for a policy.
sched_get_ priority_max( ) - Get the maximum priority value for a policy.
sched_rr_get_interval( ) - Get the time quantum value for the Round Robin policy.
literature
Linux Kernel Development by Robert Love
Understanding the Linux Kernel by Daniel Bovet and Marco Cesati
Linux Device Drivers by Jonathan Corbet, Alessandro Rubiniand Greg Kroach-Hartman
IA-64 Linux Kernel: Design and Implementation by David Mosberger and Stephane Eranian
Understanding the Linux Virtual Memory Manager by Mel Gorman
KLOGGER SCHEDULER
SCHEMATA
Events
EXEC - The exec system call was called.
TRY_TO_WAKEUP - A process was supposed to have been woken up. The TRY prefix emphasize that the process might have already been awake (for example, a running process have been sent a signal).
REMOVE_FROM_RUNQ - A process has became non-runnable (exited, blocked on I/O) and has been removed from the run queue.
ADD_TO_RUNQ - A process has became runnable and was added to the run queue.
Events (cont…)
SCHEDOUT - A process was preempted as part of a context switch.
SCHEDIN - A process was allocated a CPU as part of a context switch.
FORK - The fork system call was called.
EXIT - A process exited.