Investigating High CPU Usage on a Linux System
System DesignModel answerLast updated
Walk through the steps to identify and verify the cause of elevated CPU consumption on a Linux machine, from system-wide metrics to specific code paths.
You're operating a Linux system that's responding more slowly than usual, and initial observations show elevated CPU consumption. Walk through your investigation methodology to pinpoint what's consuming CPU resources and confirm whether it's the actual bottleneck affecting performance.
What to Cover
Explain your diagnostic approach from initial system inspection through identifying the specific process, thread, or function responsible. Describe how you'd verify that CPU is genuinely the limiting factor and not another resource constraint.
Important Considerations
- High CPU utilization by itself doesn't prove a problem exists—relate resource consumption back to observable degradation in response time or request throughput
- Consider whether load is distributed evenly or concentrated on specific cores
- Determine if processes are actively computing, blocked on I/O, restricted by resource limits, or competing for shared resources
- Account for containerized workloads if relevant
Hints
Hint 1
Start with system-wide visibility before drilling down to individual processes
Hint 2
Distinguish between time spent in application code versus kernel operations versus idle wait states
Hint 3
Use profiling tools to identify hot code paths once you've isolated the problematic process
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Diagnostic Approach
1. Establish Baseline and Confirm User Impact
Before investigating metrics, confirm what actually degraded. Compare current latency (p50, p95, p99) or throughput against historical baselines. Ask: when did this start, what changed recently (deployment, traffic pattern, dependency), and which user-facing metric regressed? This prevents chasing resource usage that's normal for the current workload.
2. System-Wide Resource Survey
Start with broad visibility using top or htop to see:
- Overall CPU utilization and per-core breakdown (is one core saturated while others idle, or all busy?)
- Load average (1/5/15 minute) relative to core count—load > cores suggests queuing
- Memory and swap usage (page faults can masquerade as CPU issues)
- I/O wait percentage (high
%wameans processes blocked on disk)
Check vmstat 1 or sar for trends: user vs system time split, context switch rate (high rate suggests thrashing), interrupts.
3. Identify the Process
Use top sorted by CPU or ps aux --sort=-%cpu to find which process consumes the most CPU time. Note:
- Is it a single process or many workers?
- User time vs system time ratio—high system suggests syscalls, kernel work, or driver issues
- State: running (R), sleeping (S), or uninterruptible sleep (D, indicates I/O wait)
For containerized environments, use docker stats or kubectl top pods first, then nsenter or docker exec to inspect inside the container.
4. Thread-Level Breakdown
Once you've identified the process, check if CPU load is distributed across threads or concentrated in one:
top -H -p <PID>
This shows individual threads. Single hot thread suggests a serial bottleneck; many busy threads suggest parallel work or contention (lock spinning).
5. Distinguish CPU Type
Check /proc/<PID>/stat or tools like pidstat -u -p <PID> 1:
- High user CPU: the application code is computing heavily
- High system CPU: frequent syscalls (file operations, network, context switches)
- High iowait: the process is actually blocked waiting for I/O despite appearing CPU-intensive
6. Check Resource Limits and Throttling
Processes may be throttled by cgroup limits in containerized environments:
cat /sys/fs/cgroup/cpu/docker/<container_id>/cpu.cfs_quota_us
cat /sys/fs/cgroup/cpu/docker/<container_id>/cpu.cfs_period_us
cat /sys/fs/cgroup/cpu/docker/<container_id>/cpu.stat
Look for nr_throttled and throttled_time—throttling causes artificial slowness even with available system CPU.
7. Profile Hot Code Paths
To find which functions consume CPU, use sampling profilers:
- perf:
perf record -p <PID> -g -- sleep 30thenperf reportshows flamegraphs of where time is spent - strace:
strace -c -p <PID>counts syscalls if system time is high - Language-specific profilers:
py-spyfor Python,async-profilerfor Java,pproffor Go
This reveals whether CPU is spent in application logic (tight loops, inefficient algorithms), library code, or system calls.
8. Verify the Bottleneck
Before concluding CPU is the constraint, check:
- Is the process actually running or blocked? Check state in
psandperf sched record - Is there lock contention? High futex syscalls or low instructions-per-cycle suggest spinning on locks
- Is memory the real bottleneck? Page faults or high minor/major fault counts in
/proc/<PID>/stat - Is downstream latency causing backpressure? Database or API slowness can make workers appear CPU-busy while they're actually waiting
9. Controlled Experiment
Once you have a hypothesis (inefficient query loop, missing index, quadratic algorithm), test it:
- Apply the proposed fix in a staging environment
- Measure before/after for the specific user metric that regressed
- Confirm CPU utilization drops proportionally if CPU was truly the bottleneck
Common Mistakes
- Assuming high CPU utilization means a problem—100% utilization is expected under heavy load if throughput meets targets
- Optimizing CPU without confirming it's the bottleneck—the real constraint might be database queries, network latency, or lock contention
- Looking only at averages—p99 latency may spike even when average CPU is low if scheduling delays occur
- Ignoring recent changes—correlate performance regression timing with deployments, config changes, or traffic pattern shifts
Example Sequence
- User reports: API p95 latency rose from 100ms to 800ms starting 2 hours ago
htop: all 8 cores at 90%, load average 12top: Python process using 700% CPU (7 cores)top -H -p <PID>: 16 worker threads all busyperf record+perf report: 80% time injson.loads()within request handler- Hypothesis: recent change increased JSON payload size
- Verification: roll back deployment, latency returns to 100ms, CPU drops to 40%
This confirms JSON parsing was CPU-bound and blocking request throughput.