A factory server in an enterprise kept running above 95% CPU usage, which raised a CPU alert. The first thing to do is not to log in and start typing commands blindly, but to read the basics from monitoring (Prometheus, Zabbix and similar): is a single core pinned at 100%, or are all cores high? Did usage spike suddenly or climb slowly? That already points to either a traffic surge or a software bug.
Step 1: find the direction
Once on the server, start with the Swiss Army knife of troubleshooting, top, for a first global scan.
Command 1: top
topHow to read the key fields:
- Load average: numbers such as 1.75, 0.85 and 0.45 are the average load over the last 1, 5 and 15 minutes. On a 4-core CPU, a 1-minute load far above 4 means the system is badly overloaded. High load does not always mean high CPU, though: heavy I/O wait also raises it.
- The %Cpu(s) line tells you what kind of problem this is. High us (user) usually means application code or third-party libraries are burning CPU. High sy (system) suggests frequent system calls, excessive context switching or interrupt handling. High wa (iowait) means the CPU is waiting on disk or network I/O — the CPU itself is not busy, but load climbs. id (idle) is idle CPU time, and the goal is to find out who is stealing it.
- Process list (PR, NI, %CPU, TIME+ and so on): press P (uppercase) in top to sort by CPU usage and spot the culprit immediately. Note the PID and command of the heaviest process — is it Java, PHP-FPM, or a database?
In our scenario, top shows a Java process (PID 12345) holding close to 200% CPU — on a multi-core machine a multi-threaded process can consume more than 100%.
Step 2: look inside the process
With a suspect process in hand, examine the threads inside it. A process is made of many threads, and a pinned CPU is usually caused by one or a few threads running flat out.
Command 2: top -Hp <PID>
top -Hp 12345This shows every thread in process 12345; sort by CPU with P again.
Several threads (shown as LWP, light weight process, on Linux) will be consuming CPU. Note the busiest thread ID, for example LWP 12346. This ID is decimal.
What is that thread doing? In a Java application it may be running an inefficient algorithm, spinning in an infinite loop, or driving heavy garbage collection.
Step 3: map the thread to source code
To connect a thread ID with business code, capture that thread's call stack — the thread's execution history, showing exactly which line it is running.
First convert the decimal thread ID (LWP 12346) to hexadecimal, because thread IDs in stack dumps are usually hex.
Command 3: printf "%x\n" <LWP>
printf "%x\n" 12346
# possible output: 303aNext, dump the thread stacks of the whole Java process.
Command 4: jstack <PID>
jstack 12345 > /tmp/jstack_12345.logjstack ships with the JDK and prints the thread stacks of a Java process.
Now search /tmp/jstack_12345.log for the hex thread ID you just produced, 303a (or 0x303a).
Example match:
"http-nio-8080-exec-1" #32 daemon prio=5 os_prio=0 tid=0x00007f4b88101000 nid=0x303a runnable [0x00007f4b817e6000]
java.lang.Thread.State: RUNNABLE
at com.example.app.ExpensiveService.calculateHash(ExpensiveService.java:25)
at com.example.app.ExpensiveService.processRequest(ExpensiveService.java:15)
...There it is. The stack shows thread 0x303a in RUNNABLE state, executing line 25 of com.example.app.ExpensiveService.calculateHash.
Thread states explained
- RUNNABLE: the thread is executing in the JVM or waiting for the OS scheduler to give it CPU time, the state to watch when chasing CPU issues.
- BLOCKED: the thread is blocked waiting for a monitor lock (synchronized). It hurts performance but does not usually burn CPU.
- WAITING / TIMED_WAITING: the thread waits for another thread to act (Object.wait(), Thread.sleep()). Threads in these states consume no CPU.
At this point the 100% CPU problem is pinned to a specific method and line. For other languages such as C/C++ or Python, pstack <PID> or attaching gdb gives similar stack information.
Step 4: dig into JVM and system evidence
Finding the code location is a big win, but sometimes you need deeper context to understand why. For example, is memory pressure causing frequent GC, which in turn burns CPU?
Command 5: jstat -gcutil <PID> <interval> <count>
jstat -gcutil 12345 1000 5This prints GC statistics once per second (1000ms), five times in total.
Key metrics:
- FGC / FGCT: Full GC count and total Full GC time. A sharp rise in FGC with high FGCT means stop-the-world Full GCs are running, burning CPU and pausing the application.
- YGC / YGCT: Young GC count and total Young GC time.
If GC activity is unusually frequent, combine it with jmap -histo:live <PID> (use with care, it triggers a Full GC) or the more thorough jmap -dump:live,file=heap.hprof <PID> to produce a heap dump, then analyze it for leaks with tools such as MAT (Memory Analyzer Tool).
Command 6: vmstat 1
vmstat 1This is a powerful system-level monitor; 1 means print every second. It gives a unified view of processes, memory, paging, block I/O, interrupts and CPU activity.
- r (runnable): processes waiting in the run queue. Persistently higher than the core count means the CPU is saturated.
- cs (context switch): switches per second. Abnormally high switching — hundreds of thousands per second — burns a lot of system CPU.
- us, sy, id and wa mean the same as in top and let you watch trends over time.
Command 7: pidstat 1
pidstat -p 12345 1Part of the sysstat package, it provides finer per-process statistics for CPU, memory, I/O and more.
Step 5: review and optimize
In our scenario jstack pointed at calculateHash. Code review found an MD5 hash function called repeatedly inside a hot loop, over very large amounts of data.
Fixes:
- Algorithm: evaluate a lighter hash such as MurmurHash instead of MD5.
- Caching: cache results for identical inputs instead of recomputing.
- Logic: review call paths to cut call counts or batch the work.
- Asynchrony: if the result is not needed in real time, move it to a message queue to free request threads.
If the problem is frequent GC:
- Analyze the heap dump to find leaked objects such as unclosed connections or oversized static maps.
- Tune heap size (-Xms, -Xmx) and collector settings, for example moving from CMS/G1 to ZGC/Shenandoah to cut pauses.
Summary: build a systematic method
Troubleshooting a pinned CPU is not about memorizing commands; it is about a clear diagnostic flow:
- Global view (top): quickly decide scope and nature — is the CPU busy or is it I/O?
- Inside the process (top -Hp): narrow down to specific threads.
- Stack analysis (jstack / pstack): map threads to source lines for a precise hit.
- Supporting evidence (jstat, vmstat, pidstat): complete the picture at JVM and OS level, including GC and context switching.
- Optimize: fix code, configuration or architecture based on the root cause, and add the monitoring and logging you were missing.
