btrace

下载

https://github.com/btraceio/btrace

例子1

HelloWorld


import java.util.Random;

public class HelloWorld {
    public static void main(String[] args) throws Exception {
        //CaseObject object = new CaseObject();  
        while (true) {
            Random random = new Random();
            execute(random.nextInt(4000));
            //object.execute(random.nextInt(4000));
        }
    }

    public static Integer execute(int sleepTime) {
        try {
            Thread.sleep(sleepTime);
        } catch (Exception e) {
        }
        System.out.println("sleep time is=>" + sleepTime);
        return 0;
    }
}

TraceHelloWorld

  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 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 = "HelloWorld", method = "execute")  
      public static void startMethod(){  
          startTime = timeMillis();  
      }  

      @OnMethod(clazz = "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)));  

      }  
  }  

jps -> btrace

D:\>jps
31728 Jps
30724 HelloWorld

D:\>btrace 30724 TraceHelloWorld.java
the class method execute time=>1299
-------------------------------------------
the class method execute time=>677
-------------------------------------------
the class method execute time=>1726
-------------------------------------------
the class method execute time=>2939
-------------------------------------------
the class method execute time=>2497
-------------------------------------------
the class method execute time=>3685

例子2

BTrace Samples
1.跟踪内存信息,用@OnTimer 这个annotation没几秒钟打印一次内存堆栈信息:

import com.sun.btrace.annotations.BTrace;  
import com.sun.btrace.annotations.OnTimer;  
import static com.sun.btrace.BTraceUtils.*;  
@BTrace  
public class TraceMemory {  
    //heapUsage()/nonHeapUsage() – 打印堆/非堆内存信息,包括init、used、commit、max   
    @OnTimer(4000)  
    public static void printM(){  
        //打印内存信息  
         println("heap:");  
         println(heapUsage());  
         println("no-heap:");  
         println(nonHeapUsage());  
     }  
 }  

2.打印下系统信息:

import static com.sun.btrace.BTraceUtils.*;  
import com.sun.btrace.annotations.BTrace;  
@BTrace  
public class TraceJInfo {  
     static{  
        println("<a href="http://lib.csdn.net/base/java" class='replace_word' title="Java 知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java </a>vm properties:===>");  
        printVmArguments();  
        println("System properties:===>");  
        printProperties();  
         println("OS properties:===>");  
         printEnv();  
         exit();  
     }  
 }  
 ```

3.正则表达式,这个匹配的是com.crm. components包下的所有的以Delegate结尾的类的所有的方法
匹配包如:/packae//.package2//..*.*/ 类似

import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import 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 TraceAllDelegate {

 @TLS  
 private static long startTime = 0;  

 @OnMethod(clazz = "/com//.crm//.components//..*Delegate.*/",   
                                                 method = "/.*/")  
 public static void startMethod(){  
     startTime = BTraceUtils.timeMillis();  
 }  

 @OnMethod(clazz = "/com//.crm//.components//..*Delegate.*/",   
                             method = "/.*/", location = @Location(Kind.RETURN))  
 public static void endMethod(){  
     println(strcat("time taken=>",str(BTraceUtils.timeMillis()-startTime)));  
     println("--------------------------------------");  
 }  

 @OnMethod(clazz = "/com//.crm//.components//..*Delegate.*/",   
                                             method = "/.*/", location = @Location(Kind.RETURN))  
 public static void print(@ProbeClassName String pcn,@ProbeMethodName String pmn) {  
     println(pcn);  
     println(pmn);  
 }  

}

##注解##

Method Annotations
1. @com.sun.btrace.annotations.OnMethod
定位目标类,方法,位置,且可定位多个。target class(es), target method(s) and “location(s)” within the method(s)
查找clazz中的类和方法。类可以是全路径定义或者正则。正则为//中间的。 /Java
.awt
..+/
可以用annotation来确定定位范围。@javax.jws.WebService所有被WebService annotation的类。
用+表示继承。+java.lang.Runnable可以定位所有继承自runnable接口的类。
2. @com.sun.btrace.annotations.OnTimer
监控需要周期运行的方法。参数 N milliseconds。
3. @com.sun.btrace.annotations.OnError
当被监控的btrace的任何方法抛异常的时候被触发。
4. @com.sun.btrace.annotations.OnExit
当BTrace调用方法exit(int)推出监控session时被触发。
5. @com.sun.btrace.annotations.OnEvent
当BTrace客户端从外部发出事件请求时被触发。可以用string表示事件名
6. @com.sun.btrace.annotations.OnLowMemory
监控内存超过临界点的方法。
7. @com.sun.btrace.annotations.OnProbe
用来避免吧需要监控的类或方法写死在脚本里。可以用xml文件配置来定义需要监控的方法等。运行的时候需要把这个xml文件拷到JVM的运行路径下,或者修改btracer.bat文件的probeDescPath来指定xml文件的位置。
Argument Annotations
8. @com.sun.btrace.annotations.Self
标记一个参数拥有this或(self)的值。
9. @com.sun.btrace.annotations.Return
标记一个参数持有方法return的值。
10. @com.sun.btrace.annotations.ProbeClassName (since 1.1)
标记一个参数持有被监控类的类名
11. @com.sun.btrace.annotations.ProbeMethodName (since 1.1)
标记一个参数持有被监控方法的方法名。
1.2版本开始可以用Boolean参数fqn表是是否需要全路径名。
12. @com.sun.btrace.annotations.TargetInstance (since 1.1)
标记一个参数持有调用实例的值。
13. @com.sun.btrace.annotations.TargetMethodOrField (since 1.1)
标记一个参数持有调用方法名。
1.2版本开始可以用Boolean参数fqn表是是否需要全路径名。
Unannotated arguments
Unannotated arguments是用来做特征匹配的,因此必须按照被监控方法的定义顺序出现。但他们可以和标记参数交叉出现。它们的精确意义取决于使用它们的location:
Kind.ENTRY, Kind.RETURN- the probed method arguments
Kind.THROW - the thrown exception
Kind.ARRAY_SET, Kind.ARRAY_GET - the array index
Kind.CATCH - the caught exception
Kind.FIELD_SET - the field value
Kind.LINE - the line number
Kind.NEW - the class name
Kind.ERROR - the thrown exception
Field Annotations
14. @com.sun.btrace.annotations.Export
使用这个关联一个BTrace成员变量来指定这个成员变量和jvmstat的counter相关联。这样就可以暴露出外部jvmstat clients的监控次数。
15. @com.sun.btrace.annotations.Property
用来标记一个指定的成员变量作为MBean属性。如果一个BTrace类至少定义了一个用@Property注解的静态成员变量,那么MBean就会被创建并且注册到MBean服务器上。
16. @com.sun.btrace.annotations.TLS
定义一个BTrace的静态成员变量为线程局部变量(thread local)。因此为了正常工作这个变量必须是immutable或cloneable的。这个可以让我们用来监控是不是同一个线程会进入到多个被监控action。
Class Annotations
17. @com.sun.btrace.annotations.DTrace
监控DTrace的
18. @com.sun.btrace.annotations.DTraceRef
监控DTrace的
19. @com.sun.btrace.annotations.BTrace
必选annotation。用以指定java类。

文档地址为http://kenai.com/projects/btrace/pages/UserGuide

 annotation的API补充:  
 1. AnyType  
     用来匹配任何引用类型。AnyType[]。用来对方法进行特征匹配。  
 2. Location  
     定义了需要监控的位置。可以选择不同的位置进行监控。配合Kind和Where来使用。  
   Kind,枚举类型  

ARRAY_GET array element load
ARRAY_SET array element store
CALL method call
CATCH exception catch
CHECKCAST checkcast
ENTRY method entry
ERROR “return” because of no-catch
FIELD_GET getting a field value
FIELD_SET setting a field value
INSTANCEOF instanceof check
LINE source line number
NEW new object created
NEWARRAY new array created
RETURN return from method
SYNC_ENTRY entry into a synchronized block
SYNC_EXIT exit from a synchronized block
THROW throwing an exception

   Where  

AFTER after the location of interest
BEFORE before the location of interest
“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值