context switch(문맥 전환)는 kernel이 현재 실행 중인 thread의 CPU 상태(register, program counter, stack pointer)를 저장하고 다른 thread의 상태를 복원해, 단일 core가 여러 thread를 다중화할 수 있게 하는 것이다. scheduler의 preemption마다, blocking syscall마다, 또는 interrupt마다 일어나며 — 결코 공짜가 아니다.
비용은 어디에 숨어 있는가
DIRECT cost (visible, ~1–5 µs):
save/restore registers · run scheduler logic · switch kernel stacks
if PROCESS switch: also swap page tables (CR3) → FLUSHES the TLB
INDIRECT cost (often much larger):
cold CPU CACHES → the new thread's data isn't in L1/L2/L3 → cache misses to RAM
cold TLB → address translations must be re-walked
lost pipeline / branch-predictor state
비용은 수 마이크로초다. 비용 — 전환 이후의 차가운 캐시와 TLB — 이 종종 더 큰 타격이다: 새로 들어온 thread는 주 메모리로부터 캐시를 다시 데우는 동안 한동안 느리게 돈다(miss당 수십에서 수백 cycle).
