SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
Linux Scheduler
(Practical View)
1
Liran Ben Haim
liran@discoversdk.com
Rights to Copy
— Attribution – ShareAlike 2.0
— You are free
to copy, distribute, display, and perform the work
to make derivative works
to make commercial use of the work
Under the following conditions
Attribution. You must give the original author credit.
Share Alike. If you alter, transform, or build upon this work, you may
distribute the resulting work only under a license identical to this
one.
For any reuse or distribution, you must make clear to others the
license terms of this work.
Any of these conditions can be waived if you get permission from
the copyright holder.
Your fair use and other rights are in no way affected by the above.
License text: http://creativecommons.org/licenses/by-sa/2.0/legalcode
— This kit contains work by the
following authors:
— © Copyright 2004-2009
Michael Opdenacker /Free
Electrons
michael@free-electrons.com
http://www.free-electrons.com
— © Copyright 2003-2006
Oron Peled
oron@actcom.co.il
http://www.actcom.co.il/~oron
— © Copyright 2004–2008
Codefidence ltd.
info@codefidence.com
http://www.codefidence.com
— © Copyright 2009–2010
Bina ltd.
info@bna.co.il
http://www.bna.co.il
2
Processes and Threads
A process is an instance of a running program.
Multiple instances of the same program can be running.
Program code (“text section”) memory is shared.
Each process has its own data section, address space, open
files and signal handlers.
A thread is a single task in a program.
It belongs to a process and shares the common data
section, address space, open files and pending signals.
It has its own stack, pending signals and state.
It's common to refer to single threaded programs as
processes.
3
The Kernel and Threads
In 2.6 an explicit notion of processes and threads
was introduced to the kernel.
Scheduling is done on a thread by thread basis.
The basic object the kernel works with is a task,
which is analogous to a thread.
4
Thread 1 Thread
1
Thread
2
Thread
3
Thread
4
Process 123 Process 124
File
Descriptors
Memory
Signal
Handlers
File
Descriptors
Memory
Signal
Handlers
Stack
State
Signal
Mask
Stack
State
Signal
Mask
Stack
State
Signal
Mask
Stack
State
Signal
Mask
Stack
State
Signal
Mask
Priority Priority Priority Priority Priority
5
6
Linux Priorities
0
1
2
3
4
98
99
97
...
Non real-time processes
SCHED_OTHER
SCHED_BATCH
SCHED_IDLE
Real time processes
SCHED_FIFO
SCHED_RR
SCHED_DEADLINE (3.14)
19
18
17
16
-19
-20
-18
...
Nice
level
Real Time priority
7
API
— int sched_setscheduler(pid_t pid, int policy,
const struct sched_param *param);
— int setpriority(int which, id_t who, int prio);
— int sched_setparam(pid_t pid, const struct sched_param
*param);
— int sched_setattr(pid_t pid, struct sched_attr *attr, unsigned
int flags);
8
9
Blocking Threads
— A nonblocking infinite loop in a thread scheduled under the
SCHED_FIFO, SCHED_RR, or SCHED_DEADLINE policy will
block all threads with lower priority forever
— Solution: Limiting the CPU usage of real-time and deadline
processes
— /proc/sys/kernel/sched_rt_period_us
— Period that is equivalent to 100% CPU (default:
1000000)
— /proc/sys/kernel/sched_rt_runtime_us
— how much of the "period" time can be used by all real-
time and deadline scheduled processes on the system
(default: 950000)
10
Preemption
— The Linux kernel is a preemptive operating system
— When a task runs in user space mode and gets
interrupted by an interruption, if the interrupt
handler wakes up another task, this task can be
scheduled as soon as we return from the interrupt
handler
11
— However, when the interrupt comes while the task is executing
a system call, this system call has to finish before another task
can be scheduled.
— By default, the Linux kernel does not do kernel preemption.
— This means that the time before which the scheduler will be
called to schedule another task is unbounded
12
Preemption Models
13
CONFIG_PREEMPT_NONE
— Kernel code (interrupts, exceptions, system calls)
never preempted. Default behavior in standard
kernels.
— Best for systems making intense computations, on
which overall throughput is key.
— Best to reduce task switching to maximize CPU and
cache usage (by reducing context switching).
14
CONFIG_PREEMPT_VOLUNTARY
— Kernel code can preempt itself
— Typically for desktop systems, for quicker application
reaction to user input.
— Adds explicit rescheduling points throughout kernel
code.
— Minor impact on throughput.
— Used in: Ubuntu Desktop 15.04, Ubuntu Server 14.04
— Use: cond_resched()
15
CONFIG_PREEMPT
— Most kernel code can be involuntarily preempted at any
time. When a process becomes runnable, no more need
to wait for kernel code (typically a system call) to return
before running the scheduler.
— Exception: kernel critical sections (holding spinlocks). In
a case you hold a spinlock on a uni-processor system,
kernel preemption could run another process, which
would loop forever if it tried to acquire the same
spinlock.
— Typically for desktop or embedded systems with latency
requirements in the milliseconds range.
16
CONFIG_PREEMPT_RT
— The PREEMPT_RT patch adds a new level of preemption, called
CONFIG_PREEMPT_RT_FULL
— This level of preemption replaces all kernel spinlocks by mutexes (or so-called
sleeping spinlocks)
— Instead of providing mutual exclusion by disabling interrupts and preemption, they
are just normal locks: when contention happens, the process is blocked and
another one is selected by the scheduler.
— Works well with threaded interrupts, since threads can block, while usual interrupt
handlers could not.
— Some core, carefully controlled, kernel spinlocks remain as normal spinlocks.
— With CONFIG_PREEMPT_RT_FULL, virtually all kernel code becomes preemptible
— An interrupt can occur at any time, when returning from the interrupt handler, the
woken up process can start immediately.
17
Thank You
Code examples and more
http://www.discoversdk.com/blog
18

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Linux scheduling and input and output
Linux scheduling and input and outputLinux scheduling and input and output
Linux scheduling and input and output
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
The Linux Kernel Scheduler (For Beginners) - SFO17-421
The Linux Kernel Scheduler (For Beginners) - SFO17-421The Linux Kernel Scheduler (For Beginners) - SFO17-421
The Linux Kernel Scheduler (For Beginners) - SFO17-421
 
Debugging linux kernel tools and techniques
Debugging linux kernel tools and  techniquesDebugging linux kernel tools and  techniques
Debugging linux kernel tools and techniques
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
 
Boot process
Boot processBoot process
Boot process
 
Linux Memory
Linux MemoryLinux Memory
Linux Memory
 
Linux Preempt-RT Internals
Linux Preempt-RT InternalsLinux Preempt-RT Internals
Linux Preempt-RT Internals
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 
Linux kernel architecture
Linux kernel architectureLinux kernel architecture
Linux kernel architecture
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking Mechanisms
 
Memory management in Linux kernel
Memory management in Linux kernelMemory management in Linux kernel
Memory management in Linux kernel
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernel
 
Reverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux KernelReverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux Kernel
 
Linux kernel memory allocators
Linux kernel memory allocatorsLinux kernel memory allocators
Linux kernel memory allocators
 

Andere mochten auch

Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
Kernel TLV
 
Docker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутомDocker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутом
Anton Turetsky
 
(07) instalaã§ã£o de programas
(07) instalaã§ã£o de programas(07) instalaã§ã£o de programas
(07) instalaã§ã£o de programas
Anderson Lago
 
Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel Developers
Kernel TLV
 

Andere mochten auch (20)

Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
 
Linux internals v4
Linux internals v4Linux internals v4
Linux internals v4
 
Scheduling In Linux
Scheduling In LinuxScheduling In Linux
Scheduling In Linux
 
React native
React nativeReact native
React native
 
Android internals
Android internalsAndroid internals
Android internals
 
Linux IO
Linux IOLinux IO
Linux IO
 
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecksKernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
Jireh ict
Jireh ictJireh ict
Jireh ict
 
Operating system 11.10.2016 adarsh bang
Operating system 11.10.2016 adarsh bangOperating system 11.10.2016 adarsh bang
Operating system 11.10.2016 adarsh bang
 
Userfaultfd and Post-Copy Migration
Userfaultfd and Post-Copy MigrationUserfaultfd and Post-Copy Migration
Userfaultfd and Post-Copy Migration
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
 
WiFi and the Beast
WiFi and the BeastWiFi and the Beast
WiFi and the Beast
 
Docker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутомDocker & Badoo: 
никогда не останавливайся на достигнутом
Docker & Badoo: 
никогда не останавливайся на достигнутом
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and Drivers
 
grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaX
 
(07) instalaã§ã£o de programas
(07) instalaã§ã£o de programas(07) instalaã§ã£o de programas
(07) instalaã§ã£o de programas
 
Loss Monitor
Loss MonitorLoss Monitor
Loss Monitor
 
Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel Developers
 
Intro to cluster scheduler for Linux containers
Intro to cluster scheduler for Linux containersIntro to cluster scheduler for Linux containers
Intro to cluster scheduler for Linux containers
 

Ähnlich wie Linux scheduler

the NML project
the NML projectthe NML project
the NML project
Lei Yang
 

Ähnlich wie Linux scheduler (20)

Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
Interview questions
Interview questionsInterview questions
Interview questions
 
Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 
the NML project
the NML projectthe NML project
the NML project
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Linux Booting Process
Linux Booting ProcessLinux Booting Process
Linux Booting Process
 
Ch04 system administration
Ch04 system administration Ch04 system administration
Ch04 system administration
 
Ch04
Ch04Ch04
Ch04
 
OffensiveCon2022: Case Studies of Fuzzing with Xen
OffensiveCon2022: Case Studies of Fuzzing with XenOffensiveCon2022: Case Studies of Fuzzing with Xen
OffensiveCon2022: Case Studies of Fuzzing with Xen
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
 
RTDroid_Presentation
RTDroid_PresentationRTDroid_Presentation
RTDroid_Presentation
 
Fuzzing_with_Xen.pdf
Fuzzing_with_Xen.pdfFuzzing_with_Xen.pdf
Fuzzing_with_Xen.pdf
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Tuning systemd for embedded
Tuning systemd for embeddedTuning systemd for embedded
Tuning systemd for embedded
 
Fast boot
Fast bootFast boot
Fast boot
 
cpu-affinity
cpu-affinitycpu-affinity
cpu-affinity
 
Linux clustering solution
Linux clustering solutionLinux clustering solution
Linux clustering solution
 
Walking around linux kernel
Walking around linux kernelWalking around linux kernel
Walking around linux kernel
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compact
 

Kürzlich hochgeladen

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 

Kürzlich hochgeladen (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 

Linux scheduler

  • 1. Linux Scheduler (Practical View) 1 Liran Ben Haim liran@discoversdk.com
  • 2. Rights to Copy — Attribution – ShareAlike 2.0 — You are free to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions Attribution. You must give the original author credit. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. License text: http://creativecommons.org/licenses/by-sa/2.0/legalcode — This kit contains work by the following authors: — © Copyright 2004-2009 Michael Opdenacker /Free Electrons michael@free-electrons.com http://www.free-electrons.com — © Copyright 2003-2006 Oron Peled oron@actcom.co.il http://www.actcom.co.il/~oron — © Copyright 2004–2008 Codefidence ltd. info@codefidence.com http://www.codefidence.com — © Copyright 2009–2010 Bina ltd. info@bna.co.il http://www.bna.co.il 2
  • 3. Processes and Threads A process is an instance of a running program. Multiple instances of the same program can be running. Program code (“text section”) memory is shared. Each process has its own data section, address space, open files and signal handlers. A thread is a single task in a program. It belongs to a process and shares the common data section, address space, open files and pending signals. It has its own stack, pending signals and state. It's common to refer to single threaded programs as processes. 3
  • 4. The Kernel and Threads In 2.6 an explicit notion of processes and threads was introduced to the kernel. Scheduling is done on a thread by thread basis. The basic object the kernel works with is a task, which is analogous to a thread. 4
  • 5. Thread 1 Thread 1 Thread 2 Thread 3 Thread 4 Process 123 Process 124 File Descriptors Memory Signal Handlers File Descriptors Memory Signal Handlers Stack State Signal Mask Stack State Signal Mask Stack State Signal Mask Stack State Signal Mask Stack State Signal Mask Priority Priority Priority Priority Priority 5
  • 6. 6
  • 7. Linux Priorities 0 1 2 3 4 98 99 97 ... Non real-time processes SCHED_OTHER SCHED_BATCH SCHED_IDLE Real time processes SCHED_FIFO SCHED_RR SCHED_DEADLINE (3.14) 19 18 17 16 -19 -20 -18 ... Nice level Real Time priority 7
  • 8. API — int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); — int setpriority(int which, id_t who, int prio); — int sched_setparam(pid_t pid, const struct sched_param *param); — int sched_setattr(pid_t pid, struct sched_attr *attr, unsigned int flags); 8
  • 9. 9
  • 10. Blocking Threads — A nonblocking infinite loop in a thread scheduled under the SCHED_FIFO, SCHED_RR, or SCHED_DEADLINE policy will block all threads with lower priority forever — Solution: Limiting the CPU usage of real-time and deadline processes — /proc/sys/kernel/sched_rt_period_us — Period that is equivalent to 100% CPU (default: 1000000) — /proc/sys/kernel/sched_rt_runtime_us — how much of the "period" time can be used by all real- time and deadline scheduled processes on the system (default: 950000) 10
  • 11. Preemption — The Linux kernel is a preemptive operating system — When a task runs in user space mode and gets interrupted by an interruption, if the interrupt handler wakes up another task, this task can be scheduled as soon as we return from the interrupt handler 11
  • 12. — However, when the interrupt comes while the task is executing a system call, this system call has to finish before another task can be scheduled. — By default, the Linux kernel does not do kernel preemption. — This means that the time before which the scheduler will be called to schedule another task is unbounded 12
  • 14. CONFIG_PREEMPT_NONE — Kernel code (interrupts, exceptions, system calls) never preempted. Default behavior in standard kernels. — Best for systems making intense computations, on which overall throughput is key. — Best to reduce task switching to maximize CPU and cache usage (by reducing context switching). 14
  • 15. CONFIG_PREEMPT_VOLUNTARY — Kernel code can preempt itself — Typically for desktop systems, for quicker application reaction to user input. — Adds explicit rescheduling points throughout kernel code. — Minor impact on throughput. — Used in: Ubuntu Desktop 15.04, Ubuntu Server 14.04 — Use: cond_resched() 15
  • 16. CONFIG_PREEMPT — Most kernel code can be involuntarily preempted at any time. When a process becomes runnable, no more need to wait for kernel code (typically a system call) to return before running the scheduler. — Exception: kernel critical sections (holding spinlocks). In a case you hold a spinlock on a uni-processor system, kernel preemption could run another process, which would loop forever if it tried to acquire the same spinlock. — Typically for desktop or embedded systems with latency requirements in the milliseconds range. 16
  • 17. CONFIG_PREEMPT_RT — The PREEMPT_RT patch adds a new level of preemption, called CONFIG_PREEMPT_RT_FULL — This level of preemption replaces all kernel spinlocks by mutexes (or so-called sleeping spinlocks) — Instead of providing mutual exclusion by disabling interrupts and preemption, they are just normal locks: when contention happens, the process is blocked and another one is selected by the scheduler. — Works well with threaded interrupts, since threads can block, while usual interrupt handlers could not. — Some core, carefully controlled, kernel spinlocks remain as normal spinlocks. — With CONFIG_PREEMPT_RT_FULL, virtually all kernel code becomes preemptible — An interrupt can occur at any time, when returning from the interrupt handler, the woken up process can start immediately. 17
  • 18. Thank You Code examples and more http://www.discoversdk.com/blog 18