The alert fires at 14:05: CPU on api-07 above 90 percent for five minutes. You SSH in, run top, and the first line under the header is java at 99.7 percent. The obvious next move is kill, and on a busy afternoon it is tempting. It is also how teams restart the same service three times a week without ever learning which loop, query or garbage collection cycle is burning the core.
Finding the process takes ten seconds. Finding the right process, and the thread and code path inside it, takes about ten minutes and saves the next ten incidents. The steps below go in that order, with the numbers you need to read the tools correctly.
Read the percentage before you trust it
Linux tools report CPU in units that differ by tool and by mode, and half the confusion in a CPU incident comes from mixing them. Three definitions settle most of it.
%CPU in top (Irix mode): the share of one core a process used during the last refresh interval. 100 percent means one core fully busy. On an 8 core machine a process can show up to 800 percent, and a process at 100 percent is using 12.5 percent of the box.
%CPU in ps: total CPU time divided by the time since the process started. A daemon that ran flat out for 10 minutes after boot and has idled for 10 hours shows a small number even if it is spinning right now, and one that just started a busy loop can look worse than it is. ps is a lifetime average, not a live reading.
Steal time (st): on a virtual machine, time your vCPU wanted to run but the hypervisor gave the physical core to someone else. High steal makes everything slower without any process on your side being the cause.
The top header line splits overall CPU into us (user code), sy (kernel on behalf of processes), ni (niced user code), id (idle), wa (waiting on I/O), hi and si (hard and soft interrupts) and st. Look at it before the process list. If us is 12 percent on 8 cores, one process is pinning one core and the machine is fine.
If sy or si dominates, the cost is in the kernel, and the process list may not show it in any useful way. If wa is high, the CPU alert is a disk problem wearing a CPU costume, since I/O wait is idle time with pending I/O.
Every live %CPU number is derived from the same counters in /proc. For one process, fields 14 and 15 of /proc/PID/stat hold user and system time in clock ticks:
%CPU = (delta_utime + delta_stime) / (CLK_TCK x delta_seconds) x 100
utime, stime : fields 14 and 15 of /proc/PID/stat, in clock ticks
CLK_TCK : getconf CLK_TCK, usually 100
delta_seconds: wall clock time between the two samples
One more unit question: what counts as a core. With hyperthreading on, nproc reports logical CPUs, so a 4 core server shows 8, and two busy threads sharing one physical core each read close to 100 percent while doing noticeably less work than two threads on separate cores. Cloud vCPUs are usually hyperthreads too. When a benchmark says the machine is at 50 percent, it may already be near its real limit.
Knowing the formula pays off when the tools disagree. You can sample the file twice yourself and compute a number nobody can argue with.
Find the process with top, pidstat and ps
Start interactive, then confirm with something that prints history you can paste into the incident channel.
- Run
top. PressPto sort by CPU,1to show each core, andcto show full command lines so twelvejavaprocesses become distinguishable. Note the PID at the top and whether a single core is at 100 percentusin the per-core view. - If you prefer
htop, press F6 to choose the sort column and F2 to add columns; its tree view (F5) shows whether the busy PID is a child of a supervisor that respawns it. - Confirm with
pidstat -u 1 5from the sysstat package. It prints per process CPU every second for 5 seconds, split into%usrand%system, which tells you early whether to look at application code or syscalls. - For a snapshot without interactivity, use
ps -eo pid,ppid,user,comm,%cpu,etime --sort=-%cpu | head -15, remembering that%cpuhere is the lifetime average. Theetimecolumn shows how long each process has run, so you can judge how misleading the average is. - Check whether the process is inside a container.
cat /proc/PID/cgroupnames the cgroup; on Docker and Kubernetes nodes it contains the container or pod ID, andsystemd-cgtopshows CPU per cgroup if many small processes add up.
If no process stands out but overall CPU is high, sort by time instead with top and T, or look for many short lived processes. A cron job that forks 300 curl calls a minute never appears at the top of any list, yet it can keep sy at 30 percent. execsnoop from the bcc tools, or perf trace on newer kernels, shows those forks as they happen.
Kernel threads deserve a separate glance. In ps output their names appear in square brackets, such as [kworker/3:1] or [ksoftirqd/0]. A busy ksoftirqd usually means network or block interrupts are arriving faster than they are handled inline, which points at traffic volume or interrupt affinity rather than a bug in your service.
Go below the process to the thread and the code path
A PID at 100 percent tells you which program. To change anything you need the thread and the function.
Run top -H -p PID to list threads of one process, or pidstat -t -p PID 1 for the same view as text. Most multi threaded services show one hot thread and dozens idle. That thread ID (TID) is the key into language specific tools.
For the JVM, convert the TID to hexadecimal with printf '%x\n' TID and search a thread dump for it: jstack PID or jcmd PID Thread.print labels each thread with nid=0x.... If the hot thread is named GC Thread or G1 Conc, the problem is heap pressure, not your loop. For Python, py-spy top --pid PID shows functions by CPU without restarting anything; for Go, the built-in pprof endpoint gives a 30 second CPU profile if the service exposes it.
For native code, or when you do not know the language, perf top -p PID samples the instruction pointer and prints the hottest symbols live. With debug symbols installed, you see function names; without them, at least library names, which already separates "inside libc regex code" from "inside the kernel TCP stack".
Containers add one trap. A container with a CPU limit of 1 core that sits at 100 percent in docker stats or kubectl top pod is at its quota, not necessarily spinning. Check cpu.stat in its cgroup v2 directory: a rising nr_throttled and throttled_usec mean the scheduler is holding it back. Raising the limit may fix latency, while the code is behaving normally.
A few profile shapes recur often enough to recognise on sight. A hot thread whose stack ends in epoll_wait returning immediately, over and over, is an event loop spinning on a socket that is always readable, often a closed connection nobody removed from the poll set. High %system with perf top showing futex or _raw_spin_lock points at lock contention: many threads fighting over one mutex, each burning kernel time to queue.
A stack dominated by memcpy or malloc inside a JSON library usually means one huge payload, so check request sizes before the code. And a Node.js process at exactly 100 percent with every other core idle is a blocked event loop; there is only one JavaScript thread to block.
Profiling does have a cost, and the objection deserves an answer. perf and py-spy sample at around 100 Hz by default and add little overhead, while a full jstack pauses the JVM for a few milliseconds per dump. On a service already at its latency budget, take one dump, not twenty, and take it from the instance you are about to restart anyway.
Worked example and a checklist before you kill anything
Back to api-07, an 8 vCPU VM. The top header shows us at 12.6 percent, sy 0.8, st 0.2: one core busy, no steal, no I/O wait. The java process, PID 3112, shows 99.7 percent.
Sample /proc/3112/stat twice, 3 seconds apart. utime goes from 1,842,310 to 1,842,580, a delta of 270 ticks; stime from 41,200 to 41,230, a delta of 30. With CLK_TCK at 100, the formula gives (270 + 30) / (100 x 3) x 100 = 100 percent. The tools agree, and 90 percent of the time is user code, so look at the application, not syscalls.
top -H -p 3112 shows thread 48213 at 99 percent and 210 other threads near zero. printf '%x\n' 48213 returns bc55. In jstack 3112, the thread with nid=0xbc55 is a request handler stuck in a regular expression match on a 40 KB header, a backtracking pattern that takes exponential time on that input.
Killing the process would have dropped 300 in-flight requests and the next oversized header would have pinned a core again within the hour. The fix is a length limit on the header and a rewrite of the pattern.
Before reaching for kill, run through this list:
- Read the top header: is the load in
us,sy,si,waorst? - Divide the process %CPU by the core count to see the real share of the machine.
- Confirm with
pidstator two/procsamples, notpsalone. - Find the hot thread with
top -Hand note its TID. - Grab one thread dump or profile (
jstack,py-spy dump,perf top) and save it to the ticket. - Check the cgroup for throttling if the process runs in a container.
- Check whether a supervisor will respawn the process with the same state.
- Only then restart, and record the evidence first so the next incident starts from it.
Collecting the evidence costs about two minutes. Skipping it is why the same service gets restarted every Tuesday.
Questions people ask about finding a CPU hog on Linux
Why does top show a single process at 400 percent? top runs in Irix mode by default, where 100 percent equals one core, so a process with four busy threads on a machine with four or more cores can reach 400 percent. Press Shift+I to switch to Solaris mode, which divides by the number of CPUs and caps every process at 100 percent of the whole machine.
Can one process sit at 100 percent CPU while the load average stays low? Yes. One runnable thread adds roughly 1 to the load average, so a single spinning thread on a 16 core server gives a load near 1 plus whatever else runs. Low load with one hot process usually means a single threaded loop, not a saturated machine.
How do I find what used the CPU an hour ago? top and pidstat only show the present. If the sysstat collector is enabled, sar -u shows system wide CPU history, and atop with its logging service keeps per process snapshots you can replay with atop -r on the daily log file.
Is renice a safe way to tame a CPU hog without killing it? renice -n 19 -p PID lowers the process's scheduling weight so other work gets the CPU first, but on an idle machine it will still use 100 percent. To cap it hard, run it in a cgroup with a quota, for example systemd-run --scope -p CPUQuota=50% for a new command.
A process using a full core is not always a fault. A batch encoder, a compiler or a well written worker should use all the CPU it is given, and alerting on that teaches people to ignore alerts. The harder question is what your alert should measure instead: per process CPU, latency of the service the process backs, or throttling in its cgroup?