利用btrace在线监控java程序状态
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
利用btrace在線監(jiān)控java程序狀態(tài) ?btrace介紹
????? 下載地址: https://kenai.com/projects/btrace/downloads/directory/releases/
????? 選擇版本進行下載,這里下載的是 release-1.2.4 / btrace-bin.zip
????? 這兩天在調(diào)試程序時,發(fā)現(xiàn)一個比較好用的工具-btrace,能夠線上監(jiān)控程序狀態(tài),獲取運行時數(shù)據(jù)信息,如方法返回值,參數(shù),調(diào)用次數(shù),全局變量,調(diào)用堆棧等。
btrace命令行使用
????? 位于bin目錄下面主要有6個腳本,3個windows的,另外3個是linux的,分別是btrace、btracec、btracer。具體功能如下:
????? 1、btrace
功能: 用于運行BTrace跟蹤程序。
命令格式:
???????? btrace [-I <include-path>] [-p <port>] [-cp <classpath>] <pid> <btrace-script> [<args>]
示例:
????????? btrace -cp build/? 1200 AllCalls1.java
參數(shù)含義:
??????? include-path指定頭文件的路徑,用于腳本預(yù)處理功能,可選;
??????? port指定BTrace agent的服務(wù)端監(jiān)聽端口號,用來監(jiān)聽clients,默認為2020,可選;
??????? classpath用來指定類加載路徑,默認為當(dāng)前路徑,可選;?
????????pid表示進程號,可通過jps命令獲取;
??????? btrace-script即為BTrace腳本;btrace腳本如果以.java結(jié)尾,會先編譯再提交執(zhí)行。可使用btracec命令對腳本進行預(yù)編譯。
?????? args是BTrace腳本可選參數(shù),在腳本中可通過"$"和"$length"獲取參數(shù)信息。
???? 2. btracec
功能: 用于預(yù)編譯BTrace腳本,用于在編譯時期驗證腳本正確性。
??????? btracec [-I <include-path>] [-cp <classpath>] [-d <directory>] <one-or-more-BTrace-.java-files>
參數(shù)意義同btrace命令一致,directory表示編譯結(jié)果輸出目錄。
3. btracer?
功能: btracer命令同時啟動應(yīng)用程序和BTrace腳本,即在應(yīng)用程序啟動過程中使用BTrace腳本。而btrace命令針對已運行程序執(zhí)行BTrace腳本。
命令格式:
?????? btracer <pre-compiled-btrace.class> <application-main-class> <application-args>
參數(shù)說明:
?????? pre-compiled-btrace.class表示經(jīng)過btracec編譯后的BTrace腳本。
?????? application-main-class表示應(yīng)用程序代碼;?
?????? application-args表示應(yīng)用程序參數(shù)。?
?????? 該命令的等價寫法為:?
????????????? java -javaagent:btrace-agent.jar=script=<pre-compiled-btrace-script1>[,<pre-compiled-btrace-script1>]*??????? <MainClass> <AppArguments>
?
btrace腳本限制
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.
?
?
jvisualvm 插件
BTrace提供了jvisualvm插件,強烈推薦在jvisualvm中編寫和測試BTrace腳本,啟動、關(guān)閉、發(fā)送事件、增加classpath都非常方便。
btrace實例
package baby.btrace;public class CaseObject{private static int sleepTotalTime=0; private int sleepTotalTime2=0; public boolean execute(int sleepTime) throws Exception{System.out.println("sleep: "+sleepTime);sleepTotalTime+=sleepTime;sleepTotalTime2=sleepTotalTime+1;sleep(sleepTime);if(sleepTime%2==0)return true;else return false;}public void sleep(int sleepTime) throws Exception { Thread.sleep(sleepTime); }}
?
1、獲取返回值,參數(shù)等信息
/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;@BTrace
public class TracingScript {/* put your code here */
/*指明要查看的方法,類*/@OnMethod(clazz="baby.btrace.CaseObject",method="execute",location=@Location(Kind.RETURN))
/*主要兩個參數(shù)是對象自己的引用 和 返回值,其它參數(shù)都是方法調(diào)用時傳入的參數(shù)*/public static void traceExecute(@Self baby.btrace.CaseObject object,int sleepTime, @Return boolean result){println("調(diào)用堆棧!!");println(strcat("返回結(jié)果是:",str(result)));jstack();println(strcat("時間是:",str(sleepTime)));}}
2、獲取對象屬性值
/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;@BTrace
public class TracingScript {/* put your code here */
/*指明要查看的方法,類*/@OnMethod(clazz="baby.btrace.CaseObject",method="execute",location=@Location(Kind.RETURN))
/*主要兩個參數(shù)是對象自己的引用 和 返回值,其它參數(shù)都是方法調(diào)用時傳入的參數(shù)*/public static void traceExecute(@Self baby.btrace.CaseObject object,int sleepTime, @Return boolean result){println("調(diào)用堆棧!!");println(strcat("返回結(jié)果是:",str(result)));jstack();println(strcat("時間是:",str(sleepTime)));}}
3、獲取方法執(zhí)行時長
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;@BTrace
public class TracingScript3 {@TLS private static long startTime = 0;@OnMethod(clazz="baby.btrace.CaseObject",method="execute") public static void startExecute(){startTime = timeNanos();} @OnMethod(clazz="baby.btrace.CaseObject",method="execute",location=@Location(Kind.RETURN)) public static void endExecute(@Duration long duration){long time = timeNanos() - startTime;println(strcat("execute time(nanos): ", str(time)));println(strcat("duration(nanos): ", str(duration)));}
}
4、正則匹配和獲取方法執(zhí)行次數(shù)
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;@BTrace
public class TracingScript4 {private static long count; @OnMethod(clazz="/.*/",method="execute",location=@Location(value=Kind.CALL, clazz="/.*/", method="sleep"))public static void traceExecute(@ProbeClassName String pcm, @ProbeMethodName String pmn,@TargetInstance Object instance, @TargetMethodOrField String method){println("====== ");println(strcat("ProbeClassName: ",pcm));println(strcat("ProbeMethodName: ",pmn));println(strcat("TargetInstance: ",str(classOf(instance))));println(strcat("TargetMethodOrField : ",str(method)));count++;}@OnEventpublic static void getCount(){println(strcat("count: ", str(count)));}
}
5、正則和事件交互
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;@BTrace
public class TracingScript5 {private static long count; @OnMethod(clazz="/.*/",method="execute",location=@Location(value=Kind.CALL, clazz="/.*/", method="sleep"))public static void traceExecute(@ProbeClassName String pcm, @ProbeMethodName String pmn,@TargetInstance Object instance, @TargetMethodOrField String method){println("====== ");println(strcat("ProbeClassName: ",pcm));println(strcat("ProbeMethodName: ",pmn));println(strcat("TargetInstance: ",str(classOf(instance))));println(strcat("TargetMethodOrField : ",str(method)));count++;}@OnEventpublic static void getCount(){println(strcat("count: ", str(count)));}@OnEvent("A")public static void getCountA(){println("==AAAA==== ");println(strcat("count: ", str(count)));}@OnEvent("B")public static void getCountB(){println("==BBB==== ");println(strcat("count: ", str(count)));}
}
?
鏈接:
http://agapple.iteye.com/blog/1005918
http://agapple.iteye.com/blog/962119
轉(zhuǎn)載于:https://my.oschina.net/xiaominmin/blog/1599642
總結(jié)
以上是生活随笔為你收集整理的利用btrace在线监控java程序状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flume NG 简介及配置实战
- 下一篇: 机器学习如何选择模型 机器学习与数据挖