Một context switch (chuyển ngữ cảnh) là việc kernel lưu lại trạng thái CPU của thread đang chạy (các thanh ghi, program counter, stack pointer) và khôi phục trạng thái của một thread khác, để một core đơn có thể phân chia (multiplex) nhiều thread. Nó xảy ra ở mỗi lần scheduler giành quyền, mỗi blocking syscall, hay mỗi interrupt — và nó không hề miễn phí.
Chi phí ẩn ở đâu
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
