BTrace是一个实时监控工具,使用了java agent 和jvm attach技术,可以在product的情况下实时监控线上程序的运行情况。另,有插件可与visualVM一起使用。
不多说了,具体的可见:
http://kenai.com/projects/btrace
下面介绍几个Helloworld示例:
主要使用了btrace命令:btrace [pid] class
pid可由jps命令快速查询
1.监控方法输入参数:
@OnMethod(
clazz="com.btrace.Person",
method="/set.*/"
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args) {
println(pcn);
println(pmn);
printArray(args);
}
执行btract命令
后台输出:
com.btrace.Person
setId
[1, ]……
2.监控方法返回值
@OnMethod(
clazz="com.btrace.Person",
method="/get.*/",
location=@Location(Kind.RETURN)
)
public static void defineclass(@Return String cl) {
println(Strings.strcat("getValue ", cl));
Threads.jstack();
}
执行btract命令
后台输出:
getValue gum
com.btrace.TestThread.main(TestThread.java:23)
3.监控jvm内存使用情况
@OnTimer(4000)
public static void printMem() {
println("Heap:");
println(heapUsage());
println("Non-Heap:");
println(nonHeapUsage());
}
执行btract命令
后台输出:
Heap:
init = 268435456(262144K) used = 26175176(25561K) committed = 251658240(245760K)
max = 492175360(480640K)
Non-Heap:
init = 12746752(12448K) used = 5892104(5754K) committed = 13598720(13280K) max =
100663296(98304K)
4.监控方法执行时间
@TLS private static long startTime;
@OnMethod(clazz="com.btrace.Person",method="setId")
public static void onCall(){
println("enter this method");
startTime=timeMillis();
}
@OnMethod(clazz="com.btrace.Person",method="setId",location=@Location(Kind.RETURN))
public static void onReturn(){
println("method end!");
println(strcat("Time taken ms",str(timeMillis()-startTime)));
}
后台输出:
enter this method
method end!
Time taken ms0
5.监控Thread start
@OnMethod(
clazz="java.lang.Thread",
method="start"
)
public static void func() {
println("about to start a thread!");
}
后台输出:about to start a thread!