Linux CPU Scheduling Explained: Context Switches, Run Queues & Monitoring
This article outlines how the Linux CPU scheduler manages threads and interrupts, explains context switches, run queue length, CPU utilization metrics, typical performance thresholds, and introduces monitoring tools like vmstat and mpstat, followed by real‑world troubleshooting examples.
CPU Scheduler Resources
CPU scheduler is responsible for scheduling two kinds of resources: threads and interrupts. Priority order high to low: 1) Interrupts – devices notify the kernel when they have completed (e.g., network card finished a packet or disk completed an I/O request). 2) Kernel processes. 3) User processes.
1. Context Switches
Most processors can run only one process at a time; on multi‑core systems Linux treats each core as an independent processor. A kernel can run 50–50,000 processes simultaneously. If there is only one CPU, the kernel must balance these processes. Each thread receives a time slice; when the slice expires or a higher‑priority thread preempts it, the thread is placed back into the CPU queue. Switching a thread is a context switch. Each context switch consumes resources to save CPU registers and enqueue the thread. More context switches increase the kernel’s scheduling overhead.
2. Run Queue
Each CPU has its own run queue. Threads are either in a sleeping state (blocked waiting for I/O) or running. A longer run queue means longer wait time for a thread. Load describes the run queue: load = number of running threads + threads in the run queue. For example, on a 2‑CPU system with 2 threads executing and 4 threads in the run queue, load = 2 + 4 = 6.
3. CPU Utilization
CPU utilization consists of:
User time : time spent in user space.
System time : time spent in kernel space.
Wait I/O : time spent waiting for I/O (blocked).
Idle : idle time.
4. CPU Performance Monitoring
Typical acceptable values:
Run queues per processor ≤ 3
User time 56%–70%
System time 30%–35%
Idle time 0%–5%
Context switches correlate with CPU utilization.
5. Monitoring Tools
Common tools: vmstat, mpstat.
6. Performance Troubleshooting Examples
Example 1
High interrupt count, low context switches indicate a single process accessing hardware. User time consumes 85% and few context switches suggest a single application causing the load.
Example 2
Context switches exceed interrupts, indicating excessive time spent switching. High wait I/O shows many threads waiting for I/O, suggesting many threads need to be switched out.
Example 3
CPU0 and CPU1 are handling CPU‑intensive processes, CPU2 is idle, CPU3 handles kernel and other system functions. Commands like ps -psr show which processes run on which cores, and ps -eo pid,ni,pri,pcpu,psr,comm provides detailed process info.
7. Summary
1) Each core’s run queue should be ≤ 3. 2) CPU utilization: roughly 70% user space, 30% kernel space. 3) Excessive kernel‑space usage often indicates heavy load and the kernel spending too much resource on priority scheduling. 4) CPU‑bound processes are penalized while I/O‑bound processes are rewarded.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
