Purpose
This brief tutorial will show you how to use VTune and its sampling collector to find hotspots in Java code.
While it is not particularly harder to collect data from Java than C or Fortran, one can run into a situation, even with the simplest of programs, where if not set up right you see all relevant activity show up under the process/module
other32. This walkthrough was written to show you how to get to the source view. This is not designed to be a comprehensive VTune tutorial.
Before you Get Started
One requirement at this writing is that the app in question
must be launched by VTune in order to drill down to the function and source view. This can be confusing to existing VTune users because it is not a requirement in C or Fortran. Work is still underway to see if there's a way to view sampling results at the function/source code level for apps that are launched separately from VTune.
This tutorial assumes you already have Java installed, and the Java
bin directory is in your path so that you can simply run the Java compiler (
javac ) and Java interpreter (
java ).
It also assumes you have VTune for Linux version 3.0 installed. VTune 3.0 works better with Java than 2.0.
The Files
Here's the code we will use. I have put it in 2 files. These are simply designed to waste time so we have a decently long run to work with. Put these 2 files in their own directory. In my examples to follow, the directory will me
~/java_vtune.
Note, if you don't use Java that the filenames are significant and must be as specified - case included.
file:
TestApp.java
class TestApp
{
public static void main(String[] args)
{
TestClass tc = new TestClass();
for (int i = 0; i < 10; i++)
{ tc.doSomething();
System.out.println("Iter: " + i + " = " + tc.getVal());
} } }
file:
TestClass.java
class TestClass
{
private double val;
public TestClass()
{ val = 0;
}
public void doSomething()
{
val = Math.abs(Math.random()*10);
for (int i = 1; i < 1000000; i++)
{ val += Math.sqrt(Math.sqrt(Math.abs(Math.log(val*i))));// + i/val;
} }
public double getVal()
{ return val;
}
}
Compiling and Running
To compile, do the following...
-
cd ~/java_vtune
-
javac -g *.java
You should that the compiler created 2
class files,
TestApp.class and
TestClass.class
Let's do a test run.
You should see something like this: (the numbers are different every time)
Iter: 0 = 2265944.406531673
Iter: 1 = 2265939.419801331
Iter: 2 = 2265939.757606619
Iter: 3 = 2265948.8682490564
Iter: 4 = 2265948.82094806
Iter: 5 = 2265944.6017632172
Iter: 6 = 2265948.1442497
Iter: 7 = 2265940.988014595
Iter: 8 = 2265940.8258199953
Iter: 9 = 2265943.5372014153
If this works, you are ready to do the VTune profiling.
Setting Up the Activity
Because activities are easy to set up, I often find it easiest to delete all the activities and results and start over. So, unless your machine has valuable VTune data on it, let's do that.
vtl delete -all (and confirm it)
Now let's create the activity. We'll simply let VTune collect the standard events (instructions retired and clockticks) for 10 seconds. The app will on your machine probably finish long before that anyway. But for other apps that run much longer than you'd like to sample, the
-d option is quite handy.
vtl activity Java_Test -d 10 -c sampling -o java -app java,"TestApp"
NOTE: with VTune, the order of some of the parameters is important. The -d 10, for instance must be before the -c. That's because everything after -c refers to the parameters for the sampling collector.
NOTE: If you see this error...
VTune(TM) Performance Analyzer 3.0 for Linux*
Copyright (C) 2000-2004 Intel Corporation. All rights reserved.
In osi.vt_get_ip_address: failed to get ip address! : No such file or directory
...ignore it. It has to do with VTune not being able to guess your hostname. It has no consequences on data collection and can be safely ignored.
You can now do a
vtl show to see your activity. VTune pre-pends a 'a1__' to the name.
Running the Activity
Simply do...
vtl run
...and wait for the run to complete.
Viewing the Results at the Process Level
If you have the ability to run an 'X' program, the easiest way to view results is to do this....
vtl view -gui
It is normally possible to view the code in a pure command line mode, but at this writing the feature has some issues with Java. The GUI version in any case is much easier to use.
TIP: if you are working over a remote connection, you may prefer to instead connect to the target machine via VNC. The UI tends to be much more responsive via VNC than a remote X server. To start VNC on the server, you have to do /etc/rc.d/init.d/vncserver start. Then launch the VNC viewer on the client.
You will see something like this...
Drilling Down to the Module Level
Now, from this view double click the
Java process to see the module breakdown for this process. Here's part of the screenshot you'll see.
We see that about half the time is spent in the Java library, and the other half is in the Java interpreter (JIT).
Drilling Down to the Function Level
Since our code is handled by the interpreter, now double click on the
java.jit entry. You'll see something very much like this:
This makes sense; the function to compute the
log called by
doSomething() is expensive. The second most common place the processor is when the samples were taken is in our
doSomething() routine itself.
Drilling Down to the Source View
Note: if you are not in the same directory as your source files you will not see the source code in the source view; it will only show the assembler
Now double click on the
doSomething() entry. VTune shows a display like this:
As expected, by far the bulk of the time spent is at the line doing all the math.
--
MattWalsh - 24 Nov 2004