Search code examples
javajvmti

Profiling JVMs with JVMTI, how to distinguish the different JVMs?


I am writing a profiler with the aid of the JVM TI.

In C++ I have written a simple agent, which writes the information collected to a socket. With Java Swing I have built a simple GUI which reads these data from a socket to visualize it.

However I am facing some usability issues. I would like to provide the functionality to start profiling a Java application on request. There is the Attach API which provides the possibility to inject an agent into a running JVM.

But to start a new Java program and inject the agent is a little bit more complicated. One way would be, to make a call to the command line and start the Java program from the GUI Profiler:

java -agentlib:agent Program

I kind of dislike this idea, because it is somehow hacky but I see no other way, do you?

To summarize I need two ways to start profiling a JVM:

  1. Start a Java applicatiom from the scratch and start profiling it directly
  2. Attach to a running JVM and inject the agent to start profiling it

Further, I would be in need to distinguish the different JVMs which I inspect, but how to do that? There no unique identifier for the different JVMs. The Attach API gives the possibility to list the different JVMs with their name and id, but what to do in the first case? Is it possible to inject the agent with arguments?


Solution

  • I solved the problem by using the local process identification (pid) and the network address to uniquely identfy the JVM.