Which Java thread eats CPU the most? Or at all?

Or how to pair system process with a Java thread that spawned it. Yesterday I did a JVM tuning workshop at Codepot. I didn’t got to show this and promised I’ll outline this. While CodePot has nice feature that enables you to reach out to all attendees (in MarkDown!), it’s not good for two reasons:

  1. not everybody was listed as a participant
  2. to write about this is better here (length)

So, while yet again finding myself wanting MarkDown here, I write.


1. ps -mo pid,lwp,stime,time,c -C java
2. -m – shows threads process spawned
3. -o – tells ps to use our own format
4. pid,lwp,… defines our format. Here shows PID, LightWeightProcess (threads), start time (launch time), CPU occupation time (user + system) and CPU usage %.
5. -C java – for java executable.

Thanks to jps we can narrow it down to one PID:

➜ ~ jps
14721 jar
14775 Jps
14569 Main
➜ ~ ps -mo lwp,stime,time,c -p 14721

Thus getting rid of PID – result?

LWP STIME     TIME  C
- 07:47 00:01:17 22
14721 07:47 00:00:00  0
14722 07:47 00:00:00  0
14723 07:47 00:00:01  0
14724 07:47 00:00:00  0
14725 07:47 00:00:00  0
14726 07:47 00:00:00  0
14727 07:47 00:00:05  1
14728 07:47 00:00:04  1
14729 07:47 00:00:03  0
14730 07:47 00:00:00  0
14731 07:47 00:00:00  0
14732 07:47 00:00:00  0
14733 07:47 00:00:09  2
14734 07:47 00:00:00  0
14735 07:47 00:00:51 14
14744 07:47 00:00:00  0
14747 07:47 00:00:00  0
14748 07:47 00:00:00  0
14749 07:47 00:00:00  0
14750 07:47 00:00:00  0

So we know that 22 percent of CPU are being eaten by our app and out of these 14 are taken by thread 14735.
➜ ~ printf '%x\n' 14735
398f

And which thread it is?
➜ ~ jstack 14721 | grep 0x398f
"AWT-EventQueue-0" #12 prio=6 os_prio=0 tid=0x00007f6a44261800 nid=0x398f runnable [0x00007f6a0fbf4000]

Java's threadNative Id (NID) is equivalent to GNU/Linux' LightWeightProcess id (LWP), you just need to convert between hex and dec.

All in one

It's good to know the steps, it's better to have it as one command.

I'll add the script here later, now, gotta go. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s