Posts Tagged ‘java’

Runtime profiling and monitoring Java apps with btrace

Wednesday, May 28th, 2008

A the latest Genova JUG meeting last week there was much discussion about performance tuning and optimization, particularly of web applications. My mention of the recently released BTrace profiling tool was met with significant interested and I was asked to provide more details and references…

BTrace (where the ‘B’ stands for bytecode) is an open source tool hosted on java.net, where you can find extensive documentation and samples. It is conceptually similar to the much-talked-about Solaris DTrace platform, but can be used on any Java platform/app.

The main BTrace concept is that of dynamically connecting to a running Java application (or server) to attach short “scripts” which log a range of events and infos such as

  • method calls;
  • execution times;
  • constructor invocation;
  • available memory;

Script execution is tied to several situations

  • reaching a specific line number;
  • invoking a given method;
  • returning from a method;
  • invoking system calls (e.g. exit);
  • recurring timer (period execution at fixed intervals);
  • and many more

In a way, BTrace scripts are very similar to AOP’s aspects, but can be attached to any existing Java code (or better, bytecode) at runtime and without any configuration or modification at development time.

The scripts are simply Java classes which take advantage of methods from the btrace API, and are configured by using simple but powerful annotations. Here you are an hello world script which traces session creation in hibernate:

// import BTrace annotations
import com.sun.btrace.annotations.*;
// import logging methods
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class HelloWorldTrace {

// probe the openSession() method of Hibernate SessionFactory
@OnMethod(
clazz=”org.hibernate.SessionFactory”,
method=”openSession”
)
public static void onOpenSessionTrace() {
// you can only log using the stati println method from BTraceUtils
println(”Hibernate: opening a new session…”);
}
}

Btrace scripts are attached to the running process using the JVM’s instrumentation interface.

If you are using JDK 1.6, you can attach to any running jvm by simply specifying its pid obtained through the new jps command:

jps

18037 Demo.JAR
19032 jps
19025 tomcat

btrace 19025 HelloWorldTrace 

Hibernate: opening a new session...
Hibernate: opening a new session...

The last command is simply an utility batch file which actually compiles and attaches the script.: On the other hand, on previous JDKs it is necessary to enable tracing in a similar way to how you enable remote debugging.

As an example of BTrace power and ease of use, the following script from BTrace samples intercepts all calls to methods in classes which are annotated with @WebService (e.g. all JAX-WS web service classes) and logs theri execution times:

@BTrace public class WebServiceTracker {
// store webservice entry time in this thread local
@TLS private static long startTime;

@OnMethod(
clazz="@javax.jws.WebService",
method="@javax.jws.WebMethod"
)
public static void onWebserviceEntry() {
print("entering webservice ");
println(strcat(strcat(name(probeClass()), "."), probeMethod()));
startTime = timeMillis();
}

@OnMethod(
clazz="@javax.jws.WebService",
method="@javax.jws.WebMethod",
location=@Location(Kind.RETURN)
)
public static void onWebserviceReturn() {
println(strcat("Time taken (msec) ", str(timeMillis() - startTime)));
println("==========================");
}

}

Note the use of BTrace built-in strcat function, which is needed to avoid the overhead of string concatenation. Obviously, the script code must be as lightweight as possible to avoid influencing tracing results with its own execution times, and thus the BTrace API provides efficient implementations of basic logging tasks.

Stay tuned for more tracing examples…

Java IDE day 2008: big success in Genova!

Wednesday, March 12th, 2008

Just a quick post while we recover from the organization effort… I will write a more detailed report as soon as possible. The IDE day was a success, with a participation level that definitely went beyond our best expectations, both in terms of attendance (about 130 people!) and in term of audience interaction (the speakers were overwhelmed with questions!).

Presentation slides will be loaded on the http://www.ideday.org website in the next few days.

In the meantime I publish this picture of the speakers with the organizing group for Genova, which give me the opportunity to say a huge THANK YOU to everyone who made this event possible.

ideday2008

Slides from the JUG Meeting and next Meeting (22/01/2008)

Friday, November 23rd, 2007

The last meeting of the Genova JUG was a very interesting event… you can have a look at the presentations on Model Driven Engineering in Portofino and Nasa World Wind with Java on the JUG Website.

The next meeting will take place on the 22th January 2008 at DIST.  More details will be announced soon…

Struts 1.x with Eclipse WTP How-To

Thursday, November 15th, 2007

Given that Struts syntax is as difficult to remember as the framework is widespread in enterprise applications, I am posting a short How-To that I wrote for the last course  I taught on the topic.

The How-To summarizes the main steps required to define a new Web Application based on Struts 1.3.x using Eclipse Europa with WTP.

You can find it on

http://www.carlobonamico.com/docs/pmwiki.php/Java/Struts13HowTo

Continuous Integration with Hudson

Friday, October 19th, 2007

Here is the presentation that I will deliver tomorrow at the Javaday Torino 2007.

Speaking at Javaday Torino 2007 - 20/10/2007

Wednesday, October 10th, 2007

I will be presenting at Javaday 2007 in Torino in ten days time, talking about Continuous Integration with Hudson. More details (and the talk slides…) will follow…

Introduzione a Spring - slides from the Genova JUG meeting @ DIST - 18/09/2007

Monday, September 24th, 2007

Here you are the slides from my talk (in Italian) about the Spring framework, co-presented with Corrado Alesso at the last Genova Java User Group meeting.

By the way, the next meeting will take place on the 20th November 2007.

JUG Genova @ DIST - 18/09/2007

Tuesday, September 4th, 2007

[Sorry for the italian…]

Vi confermo che il prossimo meeting del Java User Group genovese si terrà

martedì 18 settembre ore 18.30

presso il DIST in Via Opera Pia 13 (zona Albaro)

presenteranno:

  • TBD - speaker annunciati a breve

Per la pizza post-meeting cerchiamo di postare un aggiornamento appena possibile.

Per registrarsi potete usare il nuovo servizio creato dal JUG Padova

Per maggiori info e aggiornamenti:

Do you want to learn Java? or have a friend who does?

Friday, August 3rd, 2007

I am going to teach an introductory-level, hands-on course on Java  (with mentions of JDBC and hibernate)  in Genova at the end of september. If you are interested, feel free to contact me through email (carlo DOT bonamico AT eptamedia DOT com).

Genova JUG website

Thursday, July 26th, 2007

Thanks to the collaboration of several group members, but particularly Massimo and Corrado, the Genova Java User Group website is now online:

http://www.juggenova.net

The slides for the presentation at the latest meeting have been posted, too.