btrace动态跟踪线上程序

[list]
[*]先贴代码,有时间再补充
[/list]

[list]
[*]获取方法消耗时间和调用者
[/list]
import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import static com.sun.btrace.BTraceUtils.timeMillis;

import static com.sun.btrace.BTraceUtils.*;

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.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import com.sun.btrace.annotations.TLS;
@BTrace
public class TraceHelloWorld {

@TLS
private static long startTime = 0;

@OnMethod(clazz = "my.app.test.HelloWorld", method = "execute")
public static void startMethod(){
startTime = timeMillis();
}

@OnMethod(clazz = "my.app.test.HelloWorld", method = "execute", location = @Location(Kind.RETURN))
public static void endMethod(){
println(strcat("the class method execute time=>", str(timeMillis()-startTime)));
println("-------------------------------------------");
}

@OnMethod(clazz = "my.app.test.HelloWorld", method = "execute", location = @Location(Kind.RETURN))
public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,int sleepTime){
println(strcat("the class name=>", name));
println(strcat("the class method=>", method));
println(strcat("the class method params=>", str(sleepTime)));
println("who call CaseObject.execute :");
jstack();

}
}



[list]
[*]跟踪出现异常的方法
[/list]


import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

/**
* This example demonstrates printing stack trace
* of an exception and thread local variables. This
* trace script prints exception stack trace whenever
* java.lang.Throwable's constructor returns. This way
* you can trace all exceptions that may be caught and
* "eaten" silently by the traced program. Note that the
* assumption is that the exceptions are thrown soon after
* creation [like in "throw new FooException();"] rather
* that be stored and thrown later.
*/
@BTrace public class OnThrow {
// store current exception in a thread local
// variable (@TLS annotation). Note that we can't
// store it in a global variable!
@TLS static Throwable currentException;

// introduce probe into every constructor of java.lang.Throwable
// class and store "this" in the thread local variable.
@OnMethod(
clazz="java.lang.Throwable",
method="<init>"
)
public static void onthrow(@Self Throwable self) {
currentException = self;
}

@OnMethod(
clazz="java.lang.Throwable",
method="<init>"
)
public static void onthrow1(@Self Throwable self, String s) {
currentException = self;
}

@OnMethod(
clazz="java.lang.Throwable",
method="<init>"
)
public static void onthrow1(@Self Throwable self, String s, Throwable cause) {
currentException = self;
}

@OnMethod(
clazz="java.lang.Throwable",
method="<init>"
)
public static void onthrow2(@Self Throwable self, Throwable cause) {
currentException = self;
}

// when any constructor of java.lang.Throwable returns
// print the currentException's stack trace.
@OnMethod(
clazz="java.lang.Throwable",
method="<init>",
location=@Location(Kind.RETURN)
)
public static void onthrowreturn() {
if (currentException != null) {
jstack(currentException);
println("=====================");
currentException = null;
}
}
}


[list]
[*]跟踪内存
[/list]

public class PrintMemory {

/*
* 指定内存区域低于一定的界限的时候才内存使用打印数据<br> 也可以指定时间间隔打印内存使用
*/
@OnLowMemory(pool = "Tenured Gen", threshold = 6000000)
public static void printMem(MemoryUsage mu) {
print("MemoryUsage : ");
println(mu);
print("FreeMem : ");
println(freeMemory());
print("Heap:");
println(heapUsage());
print("Non-Heap:");
println(nonHeapUsage());
}
}



[list]
[*]查看死锁情况
[/list]

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

/**
* This BTrace program demonstrates deadlocks
* built-in function. This example prints
* deadlocks (if any) once every 4 seconds.
*/
@BTrace public class Deadlock {
@OnTimer(4000)
public static void print() {
deadlocks();
}
}




[list]
[*]btrace使用可以看看这个教程[/list]
[url]http://blog.csdn.net/qyongkang/article/details/6090497[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值