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.
The top 3
- Memory Hierarchy Latencies: Orders of Magnitude Slower: Accessing data from the fastest L1 cache takes approximately 1 nanosecond (ns), while L2 cache is ~3-5 ns, L3 cache ~10-15 ns, and main memory (RAM) is a stark ~50-100 ns, making a RAM access potentially 50-100 times slower than an L1 hit.
- TLB Misses: Virtual Memory's Costly Page Walk: A Translation Lookaside Buffer (TLB) hit can take just 1-3 CPU cycles, but a TLB miss is significantly more expensive, forcing a page table walk that can cost 100-300 cycles, or even thousands in complex multi-level page table scenarios, severely impacting performance.
- BEAM's Lightweight Processes: Minimizing Cache Pollution: Unlike heavyweight OS threads that often necessitate full TLB invalidation and can pollute CPU caches during context switches due to their large context and distinct memory spaces, BEAM's user-space scheduled, isolated processes minimize this overhead, thereby promoting better cache and TLB locality.
Sources
Open the full topic