Been playing with btrace this weekend. Very cool utility for adding performance monitoring to methods using weaving and aspects. It's pretty easy to use. You write a script which is a Java class which specifies the classes and methods you want watched. There are restrictions on what you can do in the methods called when a method is called or returns but it's flexible enough.
I'm attaching a script that times all Agent class made from a client. It prints them out by Agent class every 20 seconds. The script needs to be compiled with the btracec utility like this:
./../btrace-bin/bin/btracec -cp ../lib/objectgrid.jar AgentTimer.java
This creates an AgentTimer.class file which is the compiled form of the script
Now, run the client JVM but add this to the command line:
-javaagent:/Users/bnewport/Development/btrace-bin/build/btrace-agent.jar=script=AgentTimer.class
When you run the client, an AgentTimer.class.btrace file is created in the same directory as the AgentTimer.class file. This is where all the output goes.
The output looks like this:
Count
redis.agent.list.LTrim 3
redis.agent.Set 3
redis.agent.Incr 3
redis.agent.list.Push 6
redis.agent.set.SetCard 14
redis.agent.set.SetMembers 17
redis.agent.list.LRange 18
Min
redis.agent.list.LRange 4743
redis.agent.set.SetCard 6221
redis.agent.list.LTrim 6562
redis.agent.set.SetMembers 6582
redis.agent.list.Push 7549
redis.agent.Incr 9445
redis.agent.Set 11536
Max
redis.agent.list.LTrim 10037
redis.agent.list.Push 12034
redis.agent.set.SetCard 16798
redis.agent.Set 25215
redis.agent.set.SetMembers 28472
redis.agent.Incr 33212
It times each CallMapAgent call and plots all the results in a histogram. Very handy. I also print out the duration of each call.
I'll do more playing around with this. I was unable to get it to attach scripts dynamically to a running JVM. It only works for me when I use the agent approach to specify a script.