btrace动态跟踪线上程序

  • 先贴代码,有时间再补充


  • 获取方法消耗时间和调用者

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();

	}
}





  • 跟踪出现异常的方法



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;
        }
    }
}


  • 跟踪内存

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());    
     }    
}   



  • 查看死锁情况

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();        
    }
}
 



  • btrace使用可以看看这个教程
http://blog.csdn.net/qyongkang/article/details/6090497

你可能感兴趣的:(BTrace)