2

How does jps get information about all the local java processes? Does it connect to some local server process to fetch the information?

How do jinfo, jstat, jmap, and jstack get information about a local java process? Do they connect to some local server process(es) to fetch the information?

Is jstatd only used for providing remote access to local java processes, but not for providing local access to local java processes?

I am running Ubuntu. My question comes from https://stackoverflow.com/a/55669949/156458.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

13

jps finds running Java processes by scanning through /tmp/hsperfdata_<username> directory. Each HotSpot-based Java process creates a file in this directory with the name equal to the process ID.

The file /tmp/hsperfdata_<username>/<pid> contains various counters exported by the JVM. These counters can be read by an external process. This is exactly how jstat works. I described jvmstat performance counters in the JavaMagazine article.

So, jstat can always read counters of a local Java process, but in order to be able to monitor a remote machine, jstatd needs to be running.

jmap, jstack and jinfo use Dynamic Attach mechanism. These utilities connect to the target JVM via UNIX-domain socket and send the corresponding command to the JVM. The command is executed by the remote JVM itself. Find more about Dynamic Attach in this answer and in this presentation.

nyg
  • 2,380
  • 3
  • 25
  • 40
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Do utilities always connect to target JVM via Unix-domain socket? @apangin – MS90 Apr 13 '19 at 22:51
  • @MS90 In JDK 8 and earlier `jmap` and `jstack` had forced mode `-F`. In this mode they used Serviceability Agent which works quite differently. The linked answer and the presentation has more details. `jinfo` also used Serviceability Agent before JDK 9. Since JDK 9 these utilities always connect to a socket on Linux and macOS, or run remote thread on Windows. – apangin Apr 13 '19 at 23:06
  • Thank you for your kind and knowledge-shareable answer, @apangin. Keep up good work. – MS90 Apr 13 '19 at 23:17