Skip to content

High CPU in a Load Test? Find the Cause with AI, top -H and Thread Dumps

The load test is at 60% of the target and the app server CPU is already at 95%. Response times climb, and the question in the call is always the same: what is burning the CPU? For a Java application the answer is hidden in two outputs that are tedious to read by hand: per-thread CPU and a thread dump. That reading is where an AI tool helps most.

This post shows what to capture, how to match a hot thread to its stack, and how to ask Claude or ChatGPT to do the grouping without letting it guess.

CPU split in top: us is user code, sy is kernel, wa is waiting on disk, st is time stolen by the hypervisor, each pointing to a different next stepRead the CPU split before anything elseusyour code,GC, JITlook at threadssykernel worksyscalls, network,context switcheswawaiting on disknot really CPU,check I/Oststolen bythe hypervisorVM is shortof real CPU
High "us" is the case this article is about.

Step 1: Check what kind of CPU it is

Run top and look at the CPU line, not only the total. us is user space: your application code, garbage collection and the JIT compiler. sy is the kernel. wa is time waiting on disk, and st is time the hypervisor gave to another VM.

If st is high, the VM itself is short of CPU, and no code change will fix it. If wa is high, the problem is I/O. Only when us dominates does it make sense to look inside the process. Paste the top header to the AI if you are not sure, but this first call is quick to make yourself.

Step 2: Find the hot threads

Take the process ID of the Java process, then list its threads by CPU:

top -H -p <PID> -b -n 1 | head -30

# or, per thread every 5 seconds (needs the sysstat package)
pidstat -u -t -p <PID> 5 3

The PID column in top -H now shows thread IDs. Note the top five or so and their CPU %. On recent JDKs the JVM gives its threads OS-level names (cut to 15 characters), so the command column of top -H or pidstat -t often tells you if the hot ones are GC Thread or C2 CompilerThread rather than your request threads. Busy GC threads point to a memory problem, which is tomorrow's topic.

Step 3: Take thread dumps at the same time

for i in 1 2 3; do
  top -H -p <PID> -b -n 1 | head -20 > top_$i.txt
  jstack <PID> > jstack_$i.txt     # or: jcmd <PID> Thread.print
  sleep 10
done

Run it as the same user as the Java process. Three rounds matter. A single dump shows where threads were at one instant. A thread that is RUNNABLE in the same method in all three is doing real, repeated work.

Capture plan: take a top -H snapshot and a jstack dump together, three times, about 10 seconds apartCapture at the same moment, three timestop -H snapshot+ jstack0 s10 stop -H snapshot+ jstack10 s10 stop -H snapshot+ jstack20 sA thread that is hot in all three dumps is your suspect
One dump is a photo. Three dumps show what keeps running.

Step 4: Match thread IDs to stacks

top prints thread IDs in decimal. The dump prints the same ID in hex as nid=0x.... Convert with:

printf '%x\n' 23817
# 5d09  -> search jstack_1.txt for nid=0x5d09
Mapping: top -H shows the thread ID using CPU, printf converts it to hex, and the same value appears as nid in the jstack thread dump with the code it runsFrom a hot thread to a line of codetop -H -p PIDTID 23817 97% CPUto hexprintf %x5d09searchjstack PIDnid=0x5d09 RUNNABLEat ...PriceService.calcThe AI reads the stacks and groupsthe hot threads by the code they run
The thread ID from top is the nid in the dump, in hex.

You can do this by hand for one thread. For twenty threads across three dumps, it is exactly the boring matching work to hand to an AI.

Step 5: Ask the AI to group the evidence

Mask host names, user data and any secrets that show up in thread names or stack frames. Then paste the three top files and the three dumps (or the relevant threads if the dumps are huge):

Java app on Linux, CPU at 95% (mostly "us") during a load test.
Below are 3 top -H snapshots and 3 jstack dumps taken 10 s apart.
1. Convert the top thread IDs to hex and find each one as nid
   in the dumps.
2. For each hot thread, show its name, state and the top 8
   frames of its stack in each dump.
3. Group hot threads by the application method they are in.
4. Say which code paths are RUNNABLE in all three dumps.
Only use what is in the files. If a thread ID is not in a dump,
say so. Do not guess at fixes yet.

[top_1.txt] [jstack_1.txt] ...

Asking for the evidence first, and fixes only later, keeps the answer honest. Once you have the grouped stacks, ask a second question: "Why could this method use so much CPU under load?" Typical answers include regex or JSON parsing in a loop, logging at debug level, a busy retry loop, or an inefficient collection lookup. Treat each as a hypothesis, then confirm it with a profiler or a test where you change that code path only.

Verify the AI's answer

  • Spot-check two hex conversions yourself.
  • Open the dump and confirm the stack it quoted is really there.
  • Check the CPU % it reports against your top files.
  • If it says "GC threads", look at GC logs before you change any code.
What you seeLikely areaNext step
Request threads RUNNABLE in your codeApplication logicProfile that method
GC threads hotHeap pressureRead the GC log
C2 CompilerThread hot early in the testJIT warm-upAdd a ramp-up period, recheck later
High sy, many threadsContext switching, syscallsCheck thread pool sizes
High stVM CPU shortageTalk to the infra team

Frequently asked questions

Can I do this on Windows?
Thread dumps work the same way with jstack or jcmd. For per-thread CPU, tools like Process Explorer show thread IDs that you can convert to hex in the same way.

The dump is 5 MB. Will the AI read all of it?
It may hit context limits. Send only the threads whose IDs appear in your top list, plus a count of threads per state.

Do APM tools make this unnecessary?
Dynatrace, AppDynamics and similar tools show hot methods directly when the agent is installed. This method works on any server where you have a shell, with no agent.

Tomorrow in the AI in Performance Testing series: JVM problems with AI, from GC logs and heap patterns to OutOfMemoryError and GC settings.

Page views