How to Find a Java Thread Running on Linux with Ps -Axl

How can I get the whole thread name when I use `ps -T -p [pid]`

The thread name length is restricted to 16 characters (including the terminating null byte \0). If the length, including the \0, exceeds 16 bytes, the string is silently truncated before storing it.

See pthread_setname_np and proc.5 -> find /proc/[pid]/task/[tid]/comm.

Related:

How to get the full executable name of a running process in Linux

Why does `ps` list 2 Java threads/ light-weight processes when I have only created two?

If you run jstack to see the stack trace of each thread, you will get a hint as to what these threads are doing:

$ jps -lvm

16321 com.intellij.rt.execution.application.AppMain App -Didea.launcher.port=7532 -Didea.launcher.bin.path=/opt/idea-IU-141.1192.2/bin -Dfile.encoding=UTF-8

$ jstack 16321
2015-06-11 15:15:49
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):

"Attach Listener" #13 daemon prio=9 os_prio=0 tid=0x00007f4268001000 nid=0x4040 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"DestroyJavaVM" #12 prio=5 os_prio=0 tid=0x00007f42cc00a000 nid=0x3fc3 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"pool-1-thread-1" #11 prio=5 os_prio=0 tid=0x00007f42cc13a800 nid=0x3fe0 waiting on condition [0x00007f427c4fe000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at App$Worker.run(App.java:14)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

"Monitor Ctrl-Break" #10 daemon prio=5 os_prio=0 tid=0x00007f42cc137800 nid=0x3fdf runnable [0x00007f429b101000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:404)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
at java.lang.Thread.run(Thread.java:745)

"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007f42cc0d2000 nid=0x3fdd runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007f42cc0cd000 nid=0x3fdc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f42cc0cb000 nid=0x3fdb waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f42cc0c9000 nid=0x3fda waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f42cc0c6000 nid=0x3fd9 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f42cc0c4800 nid=0x3fd8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f42cc08c000 nid=0x3fd0 in Object.wait() [0x00007f42a0208000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000718b06f58> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x0000000718b06f58> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f42cc08a000 nid=0x3fcf in Object.wait() [0x00007f42a0309000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000718b06998> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
- locked <0x0000000718b06998> (a java.lang.ref.Reference$Lock)

"VM Thread" os_prio=0 tid=0x00007f42cc085000 nid=0x3fce runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f42cc01f800 nid=0x3fc4 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f42cc021000 nid=0x3fc5 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f42cc023000 nid=0x3fc6 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f42cc025000 nid=0x3fc7 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007f42cc026800 nid=0x3fc8 runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007f42cc028800 nid=0x3fc9 runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007f42cc02a000 nid=0x3fca runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007f42cc02c000 nid=0x3fcb runnable

"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x00007f42cc02e000 nid=0x3fcc runnable

"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x00007f42cc02f800 nid=0x3fcd runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f42cc0d5000 nid=0x3fde waiting on condition

JNI global references: 15

You can see there is thread for background compilation, GC, the finalizer/reference cleanup, listening for Ctrl-Break and a few other house keeping things.

How to get the number of threads in a Java process

Useful tool for debugging java programs, it gives the number of threads and other relevant info on them:

jconsole <process-id>

Thread handling in a java application

So, I found out that

  1. the number "Threads" excludes every finished thread - so all the threads are running or waiting for something.
  2. That is also the reason why this number is wiggling like that.

After checking my source code again, I found out that some ExecutorService objects are not correctly closed so I correted that and received the following graph (which looks better!)

Sample Image

So when somebody else have similar issues, this is what I did:

  1. Login to the machine where the application is running
  2. Get the correct PID from the process by running
    ps -aux | grep -i 'NAME' (replace NAME with the correct name of the application)
  3. Get the number of running/waiting threads by executing cat /proc/[PID]/status

  4. Create the graph-data with for x in {1..100000}; do echo $(date) - $(find /proc/[PID]/task -maxdepth 1 | wc -l); sleep 1; done >> thread_counter.csv

Check which thread is running on which CPU in linux

Funny that people downvotes but don't tell why. I searched for a while and found that following command gives which thread running on which CPU:

ps -p <PID> -L -o pid,tid,psr

Where < PID > is the pid of the process we are interested in.



Related Topics



Leave a reply



Submit