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.

例子
監控某個方法的執行時間

import static com.sun.btrace.BTraceUtils.name;
import static com.sun.btrace.BTraceUtils.print;
import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.probeClass;
import static com.sun.btrace.BTraceUtils.probeMethod;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import static com.sun.btrace.BTraceUtils.timeMillis;

import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.TLS;

/**
 * 監控方法耗時
 * 
  */
@BTrace
public class PrintTimes {

    /**
     * 開始時間
     */
    @TLS
    private static long startTime = 0;

    /**
     * 方法開始時調用
     */
    @OnMethod(clazz = "/com\\.mike\\../", method = "/.+/")
    public static void startMethod() {
        startTime = timeMillis();
    }

    /**
     * 方法結束時調用<br>
     * Kind.RETURN這個註解很重要
     */
    @SuppressWarnings("deprecation")
    @OnMethod(clazz = "/com\\.mike\\../", method = "/.+/", location = @Location(Kind.RETURN))
    public static void endMethod() {

        print(strcat(strcat(name(probeClass()), "."), probeMethod()));
        print("  [");
        print(strcat("Time taken : ", str(timeMillis() - startTime)));
        println("]");
    }
}

 

 

 

監控所有HTTP request請求的執行時間

 

    @TLS
    private static String invoke = "";

    @OnMethod(clazz = "javax.servlet.http.HttpServlet", method = "service", type = "void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)")
    public static void start(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
        invoke = concat(str(threadId(currentThread())), strcat("_", str(timeMillis())));
        print(invoke);
        print(" ");
        print(probeClass);
        print(" ");
        print(probeMethod);
        print(" s ");
        println(timeMillis());
    }

    @OnMethod(clazz = "javax.servlet.http.HttpServlet", method = "service", type = "void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)", location = @Location(Kind.RETURN))
    public static void end(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
        print(invoke);
        print(" ");
        print(probeClass);
        print(" ");
        print(probeMethod);
        print(" e ");
        println(timeMillis());
        invoke = "";
    }

 

 

 監控所有HTTP request請求的具體java調用棧 call stack以及執行時間

在上面監控http request的基礎上,增加你關心類的方法。然後通過ID(Thread ID, 時間戳)生成對應的調用圖。


 

發佈了15 篇原創文章 · 獲贊 0 · 訪問量 1482
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章