short refresher on kernel scheduling

26
SHORT REFRESHER ON SCHEDULING What is scheduler ?

Upload: others

Post on 03-May-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Short refresher on kernel scheduling

SHORT REFRESHER ON SCHEDULING

What is scheduler ?

Page 2: Short refresher on kernel scheduling

Overview of Processes and Threads

Programs and Processes

Threads

Scheduling

CPU and I/O-bound Threads

Context Switching

Linux Processes/Threads

Page 3: Short refresher on kernel scheduling

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).

Page 4: Short refresher on kernel scheduling

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

Page 5: Short refresher on kernel scheduling

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

Page 6: Short refresher on kernel scheduling

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 ?

Page 7: Short refresher on kernel scheduling

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.

Page 8: Short refresher on kernel scheduling

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.

Page 9: Short refresher on kernel scheduling

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 .

Page 10: Short refresher on kernel scheduling

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)

Page 11: Short refresher on kernel scheduling

Linux CPU Scheduler Goals

Efficiency

Interactivity

Fairness and Preventing Starvation

SMP scheduling

SMT Scheduling

Page 12: Short refresher on kernel scheduling

Efficiency

Context switching is expensive.

Scheduler’s code is run quite often.

Efficiency suffers for the sake of other goals

Page 13: Short refresher on kernel scheduling

Interactivity

Important for desktop environments.

Interactivity is important:

- mouse click response time

Interactivity is less important:

- the Linux kernel compilation time

Page 14: Short refresher on kernel scheduling

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?

Page 15: Short refresher on kernel scheduling

SMP Scheduling

Multiprocessing supports

No task is executing on more than one CPU

What can be reason to prefer one CPU over another?

Page 16: Short refresher on kernel scheduling

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.

Page 17: Short refresher on kernel scheduling

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.

Page 18: Short refresher on kernel scheduling

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

Page 19: Short refresher on kernel scheduling

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.

Page 20: Short refresher on kernel scheduling

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.

Page 21: Short refresher on kernel scheduling

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.

Page 22: Short refresher on kernel scheduling

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.

Page 23: Short refresher on kernel scheduling

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

Page 24: Short refresher on kernel scheduling

KLOGGER SCHEDULER

SCHEMATA

Page 25: Short refresher on kernel scheduling

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.

Page 26: Short refresher on kernel scheduling

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.