Kernel Context Switch: The Heavyweight Cost
A full kernel context switch, necessary for traditional OS threads, involves saving and restoring extensive CPU state, changing privilege levels, and can take hundreds of nanoseconds to microseconds, significantly more than user-space operations.
The top 3
- Kernel Context Switch: Microseconds of CPU Time: A typical kernel context switch can directly consume 1 to 5 microseconds of CPU time, or thousands of CPU cycles, just for saving and restoring process state like registers and page tables, making it a notably heavyweight operation.
- Beyond Direct Cost: Cache & TLB Thrashing: The indirect costs of kernel context switches, often more significant than the direct CPU time, stem from the invalidation or eviction of CPU cache lines, TLB entries, and flushing of the instruction pipeline, leading to costly cache misses and degraded performance upon process resumption.
- Kernel Switches: 10x Slower Than User-Space: Kernel context switches, typically taking 1-5 microseconds, are significantly slower—often by an order of magnitude or more—compared to lightweight user-space context switches, such as those for Goroutines (around 170 nanoseconds), highlighting the performance benefits of user-level threading models.
Sources
Open the full topic