Course contentsShow
← Linux
Lesson 1677 of 1,81914. Observability and Performance TroubleshootingFree lesson

Measuring Context-Switch-Induced Cache Pollution

Quantify how frequently switching between tasks degrades L1/L2 cache performance.

In this lesson, you will learn how to quantify the performance degradation caused by context switching when incoming tasks overwrite the L1 and L2 cache lines of their predecessors.

The Cost of Cache Displacement

When a CPU context-switches from Task A to Task B, it isn't just swapping register states; it is effectively "polluting" the L1 and L2 caches. Task A’s hot working set—the data it was actively processing—is evicted to make room for Task B’s instructions and data. When Task A resumes, it suffers a burst of cache misses as it reloads its state from L3 or main memory.

To measure this, we don't look at total context switches, but at the correlation between voluntary/involuntary switches and cache-miss rates.

Using Hardware Performance Counters (PMUs)

We use perf to observe the relationship between context switches (cs) and cache misses (cache-misses). If we isolate a specific core, we can measure how the "miss rate per instruction" spikes immediately following an involuntary context switch.

Run this command to observe the delta on a running process:

perf stat -e cs,cache-references,cache-misses -p <PID> sleep 5

If your cache-misses / cache-references ratio is significantly higher than the baseline for that application during periods of high context switching, you have confirmed cache pollution.

Detecting the Impact via "perf stat"

A more surgical approach involves monitoring the CPU's hardware event counters for cache misses specifically triggered by the scheduler's migration. You can monitor the L1-dcache-load-misses during heavy task thrashing:

perf stat -e L1-dcache-load-misses,context-switches -a -- sleep 10

If you observe that L1-dcache-load-misses scales non-linearly with context-switches, you are witnessing the "Cache-Warmup Penalty." In high-performance scenarios, this is often mitigated by CPU pinning (affinity) or increasing the timeslice duration in the CFS (Completely Fair Scheduler) to allow tasks to complete their work before being evicted, thereby keeping the cache "warm" for longer intervals.

Key Takeaway: Context-switch-induced cache pollution is quantified by observing the spike in L1/L2 cache misses that occurs immediately following involuntary task preemption, revealing the hidden cost of task migration beyond mere register state saving.