SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Lazy Context Switching
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

Lazy Context Switching

Eileen David
Last updated: September 11, 2023 1:40 pm
Eileen David 7 Min Read
Share
SHARE

Lazy context switching is a technique used in operating systems and processors like ARM to improve performance and reduce overhead associated with context switches. A context switch refers to the process of storing and restoring the state of a CPU so that execution can be resumed from the same point at a later time. This allows multiple processes to share a single CPU resource. Lazy context switching optimizes this process by avoiding unnecessary context switches and only saving/restoring CPU state when absolutely required.

Contents
What is Context Switching?Problems with Frequent Context SwitchingWhat is Lazy Context Switching?Benefits of Lazy Context SwitchingChallenges in Lazy Context SwitchingLazy Context Switching TechniquesLazy Context Switching in ARMConclusion

What is Context Switching?

In a multitasking operating system, context switching allows the CPU to switch between executing multiple processes or threads. This provides the illusion that multiple processes are executing simultaneously on a single CPU core. However, the CPU can only execute one process at any given instant. The operating system kernel is responsible for scheduling which process gets time on the CPU. When a process’s time slice expires, the kernel performs a context switch to swap that process out and swap another one in.

During a context switch, the kernel has to save the current state of the CPU including values in registers, program counter, and cache contents. This is known as a process’s context. The context is loaded back when that process gets scheduled to run again so it can resume execution from where it left off earlier. The time taken to save and restore contexts introduces CPU overhead that can negatively impact performance.

Problems with Frequent Context Switching

Frequent context switching can create significant overhead, especially in architecture with larger register files like ARM. Every switch requires saving and restoring a large amount of state. Cache performance also suffers due to repeated flushes. Excessive context switching thus results in higher CPU usage and reduced throughput.

Some key problems include:

  • Increased latency – Useful application work is interrupted more often
  • Higher CPU usage – More CPU cycles are spent switching instead of doing useful work
  • Cache thrashing – Cache has to be flushed/invalidated with every switch
  • Loss of locality – Potential loss of hot data in cache
  • Increased kernel mode transitions – Hardware has to switch privilege levels more often

What is Lazy Context Switching?

Lazy context switching aims to reduce the performance impact of excessive context switching in operating systems. The key idea is to avoid unnecessary context saves and restores whenever possible. This keeps more cycles available for application tasks.

With lazy context switching, the kernel attempts to delay context switches and only performs the switch when it cannot be avoided any further. For example, it can skip saving context when scheduling the same process to run again. The context only needs to be saved when actually switching to a different process.

Restoring context can also be deferred until the process actually needs to access the CPU state. For example, if a process resumes execution without using any registers, those registers don’t need to be restored from its saved context immediately.

Benefits of Lazy Context Switching

Lazy context switching provides several benefits:

  • Reduces number of required context switches
  • Saves CPU cycles previously spent on unnecessary context saves/restores
  • Avoids cache thrashing by reducing number of cache flushes
  • Increases cache locality and performance
  • Lowers CPU usage and latency for context switching
  • Allows faster scheduling of same process or thread repeatedly

Together these benefits can significantly boost throughput and responsiveness for latency-sensitive applications.

Challenges in Lazy Context Switching

However, lazy context switching also introduces some challenges including:

  • Complexity of determining when context switching can be deferred/avoided
  • Kernel requires more bookkeeping to track “lazily” saved contexts
  • Process states become more asynchronous requiring extra synchronization
  • Can nullify benefits of eager preemption in real-time systems
  • Overly lazy algorithms risk priority inversion or data corruption

The key is finding an optimal balance between laziness and responsiveness based on workload requirements.

Lazy Context Switching Techniques

Some techniques used for lazy context switching include:

  • Deferring context save – Avoiding save on scheduler reselecting same process
  • Deferring context restore – Delaying restore until registers/state is accessed
  • Partial context save – Skipping some registers or state
  • Just-in-time restoration – Restoring state just before it’s needed
  • Context sharing – Reusing contexts between related processes/threads
  • Speculative context switch – Proactively preparing contexts in expectation of upcoming switches (pipelining)

Lazy Context Switching in ARM

ARM processors are RISC architectures with large register files. This makes context switching particularly expensive. ARM CPU modes also require switching privilege levels during context switches.

To reduce context switching overhead, the ARM Linux kernel employs lazy techniques such as:

  • Avoiding unnecessary CP15 register saves on context switch
  • Deferring restore of lazily-saved registers until first use
  • Pre-restoring high priority process registers speculatively
  • Partial context saving of relevant registers only
  • Using hardware registers to reduce mode switching cost

Together these techniques can reduce context switch times on ARM CPUs by 30% or more. This provides significant throughput and efficiency improvements for multitasking workloads.

Conclusion

Lazy context switching enhances performance by avoiding unnecessary overhead during context switches. Intelligently deferring or skipping context saves and restores allows ARM CPUs to spend more cycles running useful application code. However, care must be taken to balance laziness with responsiveness. Overall, lazy context switching can notably boost efficiency and throughput in ARM-based mobile and embedded systems running complex multitasking workloads.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article ARM Cortex-M4 Interrupt Handling
Next Article Context Switching in RTOS
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Unified vs Separate Memory Address Spaces in ARM Cortex-M

ARM Cortex-M processors can be configured with either a unified…

9 Min Read

ARM Cortex M Registers

The ARM Cortex-M is a group of 32-bit RISC ARM…

6 Min Read

Utilizing Dual Stack Pointers (MSP and PSP) Without an RTOS

ARM Cortex-M processors provide two stack pointers, the main stack…

6 Min Read

Cortex M0+ delay routine without timers

Executing delays and timing operations are common needs in embedded…

10 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account