BTrace入门及使用实例

介绍
Btrace (Byte Trace)是sun推出的一款java 动态、安全追踪(监控)工具,可以不停机的情况下监控线上情况,并且做到最少的侵入,占用最少的系统资源。 
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.

 

运行环境搭建
Steps to run BTrace
1.下载[btrace|http://kenai.com/projects/btrace/downloads/directory/releases]包 并把btrace的命令放到path中
2. jps命令查出需要监控的jvm pid
3. 编写BTrace程序 (安装包中带有大量的例子)
4. Run btrace tool by the following command line:
       btrace <pid> <btrace-script>


BTrace能作的事情

记录以下事件:
    * 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);

 

BTrace不能作的事情
保证BTrace追踪的是只读操作,所以有一堆限制:
不仅强制要求java脚本需要提供public static方法.而且,脚本里无法实例化对象,数组,不能抛异常或捕捉,不能有循环,内部类等。
特别是不能调用任何的实例、静态方法,除了com.sun.btrace.BTraceUtils类static methods。*所以BTrace的功能全部在BTraceUtils中的方法。*


To guarantee that the tracing actions are "read-only" (i.e., the trace actions don't change the state of the program traced) and bounded (i.e., trace actions terminate in bounded time), a BTrace program is allowed to do only a restricted set of actions. In particular, a BTrace class

    * can not create new objects.
    * can not create new arrays.
    * can not throw exceptions.
    * can not catch exceptions.
    * can not make arbitrary instance or static method calls - only the public static methods of com.sun.btrace.BTraceUtils class may be called from a BTrace program.
    * can not assign to static or instance fields of target program's classes and objects. But, BTrace class can assign to it's own static fields ("trace state" can be mutated).
    * can not have instance fields and methods. Only static public void returning methods are allowed for a BTrace class. And all fields have to be static.
    * can not have outer, inner, nested or local classes.
    * can not have synchronized blocks or synchronized methods.
    * can not have loops (for, while, do..while)
    * can not extend arbitrary class (super class has to be java.lang.Object)
    * can not implement interfaces.
    * can not contains assert statements.
    * can not use class literals.

例子
监控某个方法的执行时间

 

Java代码   收藏代码
  1. <strong><em>import static com.sun.btrace.BTraceUtils.name;  
  2. import static com.sun.btrace.BTraceUtils.print;  
  3. import static com.sun.btrace.BTraceUtils.println;  
  4. import static com.sun.btrace.BTraceUtils.probeClass;  
  5. import static com.sun.btrace.BTraceUtils.probeMethod;  
  6. import static com.sun.btrace.BTraceUtils.str;  
  7. import static com.sun.btrace.BTraceUtils.strcat;  
  8. import static com.sun.btrace.BTraceUtils.timeMillis;  
  9.   
  10. import com.sun.btrace.annotations.BTrace;  
  11. import com.sun.btrace.annotations.Kind;  
  12. import com.sun.btrace.annotations.Location;  
  13. import com.sun.btrace.annotations.OnMethod;  
  14. import com.sun.btrace.annotations.TLS;  
  15.   
  16. /** 
  17.  * 监控方法耗时 
  18.  *  
  19.   */  
  20. @BTrace  
  21. public class PrintTimes {  
  22.   
  23.     /** 
  24.      * 开始时间 
  25.      */  
  26.     @TLS  
  27.     private static long startTime = 0;  
  28.   
  29.     /** 
  30.      * 方法开始时调用 
  31.      */  
  32.     @OnMethod(clazz = "/com\\.mike\\../", method = "/.+/")  
  33.     public static void startMethod() {  
  34.         startTime = timeMillis();  
  35.     }  
  36.   
  37.     /** 
  38.      * 方法结束时调用<br> 
  39.      * Kind.RETURN这个注解很重要 
  40.      */  
  41.     @SuppressWarnings("deprecation")  
  42.     @OnMethod(clazz = "/com\\.mike\\../", method = "/.+/", location = @Location(Kind.RETURN))  
  43.     public static void endMethod() {  
  44.   
  45.         print(strcat(strcat(name(probeClass()), "."), probeMethod()));  
  46.         print("  [");  
  47.         print(strcat("Time taken : ", str(timeMillis() - startTime)));  
  48.         println("]");  
  49.     }  
  50. }</em></strong>  

 

 

 

 

监控所有HTTP request请求的执行时间

 

Java代码   收藏代码
  1. @TLS  
  2. private static String invoke = "";  
  3.   
  4. @OnMethod(clazz = "javax.servlet.http.HttpServlet", method = "service", type = "void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)")  
  5. public static void start(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {  
  6.     invoke = concat(str(threadId(currentThread())), strcat("_", str(timeMillis())));  
  7.     print(invoke);  
  8.     print(" ");  
  9.     print(probeClass);  
  10.     print(" ");  
  11.     print(probeMethod);  
  12.     print(" s ");  
  13.     println(timeMillis());  
  14. }  
  15.   
  16. @OnMethod(clazz = "javax.servlet.http.HttpServlet", method = "service", type = "void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)", location = @Location(Kind.RETURN))  
  17. public static void end(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {  
  18.     print(invoke);  
  19.     print(" ");  
  20.     print(probeClass);  
  21.     print(" ");  
  22.     print(probeMethod);  
  23.     print(" e ");  
  24.     println(timeMillis());  
  25.     invoke = "";  
  26. }  

 

 

  监控所有HTTP request请求的具体java调用栈 call stack以及执行时间

 

在上面监控http request的基础上,增加你关心类的方法。然后通过ID(Thread ID, 时间戳)生成对应的调用图。



已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐
  • —软件人才免语言低担保 赴美带薪读研!—



你可能感兴趣的:(BTrace)