14 February 2012

Remote Java Profiling using Visual Vm

This blog entry details how to profile a JVM running on a remote box using Visual Vm. This enables you to monitor the performances of all the Java processes running on the remote JVM.

What is Visual VM?
Visual VM is an open-source profiling tool that comes shipped with the JDK nowadays (JDK7+). It can be used to profile JDK1.5+. Using the tool is very self-explanatory if you have used other profilers, check out the vsiual vm getting started / introduction documentation if you haven't used a java profiling tool before. 

Setting up your localhost
Download and install VM on your machine, important GOTCHA ensure you start visual VM with the same version of the JDK that the remote host is using. To specify the JDK start with the following argument:

--jdkhome JDK_HOME visualvm (where JDK_HOME is the path to your JDK install directory)

Ok that's the easy part now we need to get Jstatd running on the remote host to be able to hook into the JVM.

Adding a remote host in Visual VM

Setting up JStatd
Jstatd is an RMI service that provides an interface to hook into/ monitor JVM's. WWe will create a basic set up with all security switched off for the quickest of set ups. If you want to enable security read the Java docs which explain how to set up policies.

Jstatd is provided with your jdk, to use it we need to create a policy file which will contain the security policy details. Create a file called jstatd.all.policy and enter the following syntax:

Save the file in JDK_HOME/bin. Now start Jstatd from the command promptly using the following command:

jstatd -J-Djava.security.policy=jstatd.all.policy.

 If you receive the error message detailed below then it means the Jstatd RMI server was unable to find your policy file ensure the  java.security.policy jvm argument matches your filename and that the file name lives in the correct JDK bin directory e.g. C:\PROGRA~1\Java\jdk1.6.0_29\bin.

Once working a Jstatd connection should have automatically appeared on your remote host that you set up in visual vm. When a process is started on the remote vm this should also appear under the remote host with a PID. You can now profile each PID, however to get the full profiling capabilities you will have to set up a JMX connection.

Creating a JMX Connection for more profile information
JMX (Java Management Extensions) has been around since JDK1.5 and is also part of your JDK/JRE.It allows remote monitoring of the JVM using Managed Beans which you connect to via RMI and other network protocols such as http/soap. Most Application Servers support this and you can even run a standalone Java application with JMX.

Again we are going for a basic set up with security switched off. You also need to specify a port number, in my example I have used 6643 but you can choose any port just ensure there isn't another service already using this. To use jmx start your remote process with the following java arguments:

-Dcom.sun.management.jmxremote.port=6643 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

Once the process has started go back to Visual Vm and add a JMX connection on the remote host, the connection string should be hostname:port number e.g. If you receieve the error messsage 'Cannot connect to hostname:port using service:jmx:rmi:///jndi/rmi://hostname:port/jmxrmi'. This means that the JMX did not start correctly on your remote process, ensure the port number and hostname is correct. You can always run JConsole on the remote machine to check if JMX has started. To start it simply run JDK_HOME/bin/jconsole.

CPU Sampling on a Remote JMX Connection in Visual VM

1 comment: