Jaganathan Sadasivan
Jaganathan Sadasivan

Reputation: 41

Socket information from Linux-Java thread dump

While analyzing a performance issue, I took continuous thread dumps for every 5 secs, and analyzed using samurai thread dump analyzer. Noticed that, many threads are in runnable state with below stack dump continuously on all the occurrences. But I couldn't find to which host they are communicating. I tried using the commands ss -t -a, watch ss -tp and netstat -A inet -p, but couldn't relate their results with the thread. Any idea? Thanks in advance.

Thread dump 2/5 "TP-Processor125"   prio=5 tid=0x25756 nid=0x649c RUNNABLE (JNI Native Code) - stats: cpu=828 blk=-1 wait=-1      java.lang.Thread.State: RUNNABLE      
     at java.net.SocketInputStream.socketRead0(Native Method)      
     at java.net.SocketInputStream.read(SocketInputStream.java:152)      
     at java.net.SocketInputStream.read(SocketInputStream.java:122)      
     at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)      
     at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)      
     at java.io.BufferedInputStream.read(BufferedInputStream.java:334)      
     at org.apache.jk.common.ChannelSocket.read(ChannelSocket.java:628)
     at org.apache.jk.common.ChannelSocket.receive(ChannelSocket.java:566)
     at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:693)
     at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
     at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
     at java.lang.Thread.run(Thread.java:745)      Locked synchronizers: count = 0

Upvotes: 4

Views: 2299

Answers (3)

Mariano Paniga
Mariano Paniga

Reputation: 618

From comments and replies to similar questions Find thread id that open socket for net , How to view thread id of a process which has opened a socket connection? , it seems there is no tracking, from the operating system, of the thread that is using a specific opened socket. So it appears to be very difficult to obtain the information you requested.

In the https://stackoverflow.com/a/50273809/1996150 reply there is a suggested (complex) method that requires to log the system calls with strace from the start of the process, and finally debug with gdb the process to find the thread.

Upvotes: 0

MojoJojo
MojoJojo

Reputation: 4242

tid is the Java level thread id and nid is the native thread id. What nid actually refers to on different OS'es, is somewhat confusing. What I would recommend is connecting to your application using a tool such as Visual VM and noting down the PID of the application. Once you have the PID, try the command below:

sudo netstat -nlpt

This should give you something like below (with remote/local address and PID and program name):

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1144/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      661/cupsd       
tcp6       0      0 ::1:631                 :::*                    LISTEN      661/cupsd

That should tell you which connections are open from your Java program.

Upvotes: 2

Harald
Harald

Reputation: 5133

The nid is actually the process/thread ID of the underlying OS (at least for Linux), albeit in hex notation. Convert to decimal pid and use

lsof -p pid |grep -Ei 'tcp|socket'

to learn more about the socket connections used. It may turn out though, that all sockets are opened by the main thread, in which case the sub-threads only inherit several (many) so it may be difficult to see which thread it connected where.

If lsof does not show the thread of the main process (seems it indeed does not) you may have to resolve to looking into /proc/<pid>/fd.

Upvotes: 3

Related Questions