Running Java observability tools on "events"

Java serviceability, observability tools such as jstack, jmap, jstat are run asynchronously. i.e., these tools can be "attached" to running Java program at any time (without requiring any VM event). But sometimes, you may want to run these tools on specific "event(s)". For example, you can use -XX:OnError flag to run any of these tools at the time of fatal error exit. For example, you can create heapdump on exit using jmap -F -dump command. What about other events such as gc begin/end, class load/unload, thread start/stop etc?

Mustang (Java SE 6) supports many built-in probes for DTrace. These may be used to dynamically trace JVM and your Java programs. DTrace supports system action that can be used to run arbitrary program from D-script. If you use system action along with JVM built-in probes, then you can run any of the Java serviceability tools such as jstack, jmap based on specific "event(s)". For example, you can write a D-script that triggers java heapdump after 100'th GC event occurs.

Sample D-script: test.d

int cnt;

BEGIN {
    cnt = 0; /\* initialize count \*/
}

hotspot1274:::gc_begin  
{
   cnt++;
}

hotspot1274:::gc-begin 
 / cnt == 100 / 
{
    /\* create java heap dump \*/
    system("jmap -dump:format=b,file=heap.bin 1274"); 
}



where 1274 is pid of your java process. Heap dump is created in "heap.bin" file after 100'th GC event. More complex "events" are possible with different DTrace probe and predicate combinations. Note that the system action is a destructive DTrace action -- this means you have to run DTrace with -w option to enable it. I ran the above script with the following command line:

/usr/sbin/dtrace -s test.d -w 

More examples of running Java Serviceability tools on "events":

  • run "jstack -l" command on every contended monitor entry/exit probes or on some specific Java method entry/exit.
  • run "jmap -dump" when 100'th full GC event occurs (full/partial GC is inferred by first argument to gc-begin probe)
  • run "jmap -histo" on GC related probe points (after every 2000'th GC event)
  • run "jmap -permstat" on classloader probe points (say at every class unload event)
Comments:

I don't know a whole lot about using dtrace, but I assume you can parameterise the script with 1274 both in the system call and the probe match line?

Posted by Ken Horn on January 12, 2006 at 12:08 PM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder