btrace 笔记

Posted sanmutongzi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了btrace 笔记相关的知识,希望对你有一定的参考价值。

转载请注明原链接地址 http://www.cnblogs.com/dongxiao-yang/p/6134393.html

btrace 是一个可以不用重启线上java业务查问题的神器,记一下自己折腾半天写的几个demo

 

java业务代码

 

package com.youku.data.test.kfktest;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws InterruptedException {
		System.out.println("Hello World!");

		int[] a = new int[] {
				20,
				21,
				21,
				21,
				22,
				19,
				24,
				27,
				20,
				34,
				23,
				20,
				17,
				21,
				20,
				18,
				19,
				20,
				24,
				20,
				23,
				22,
				27
				 };
		
		//int s=0;
		BtraceTest bt = new BtraceTest();
		while(true)
		{
			for(int i:a)
			{
				bt.getret(i);
				Thread.sleep(100);
			}
			
			Thread.sleep(500);
			
		}
		
	
		
		//System.out.println(s);

	}
}

  

package com.youku.data.test.kfktest;

public class BtraceTest {

	private long ret = 0;

	public long getret(int num) {
		ret += num;

		// Thread.sleep(10);

		method1();
		method2();
		method3();
		return ret;

	}

	private void method1() {
		String a = "asdfadsfa" + "sdfasdfdsf";
	}

	private void method2() {
		double a = 1000 / 3.14;
	}

	private void method3() {
		float a = (float) (1000 / 3.14);
	}

}

  

btrace 脚本

/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class TracingScript {
	/* put your code here */
  @TLS private static long startTime = 0;  
     
   @OnMethod(  
      clazz="com.youku.data.test.kfktest.BtraceTest",  
      method="getret"  
   )   
   public static void startExecute(){  
     startTime = timeNanos();  
   } 


    @OnMethod(
        clazz="com.youku.data.test.kfktest.BtraceTest",
        method="getret",
        location=@Location(Kind.RETURN)
    )
public static void traceExecute(@Duration long duration,@ProbeMethodName String pmn){  
    long time = timeNanos() - startTime;  
     println(strcat("duration(nanos): ", str(duration)));
     println(strcat("execute time(nanos): ", str(time)));  
    
     println(strcat("ProbeMethodName(pmn): ", str(pmn)));   
   } 
}

  

package com.sun.btrace.samples;

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


@BTrace class Profiling {
    @Property
    Profiler swingProfiler = BTraceUtils.Profiling.newProfiler();
    
    @OnMethod(clazz="com.youku.data.test.kfktest.BtraceTest", method="/.*/")
    void entry(@ProbeMethodName(fqn=true) String probeMethod) { 
        BTraceUtils.Profiling.recordEntry(swingProfiler, probeMethod);
    }
    
    @OnMethod(clazz="com.youku.data.test.kfktest.BtraceTest", method="/.*/", location=@Location(value=Kind.RETURN))
    void exit(@ProbeMethodName(fqn=true) String probeMethod, @Duration long duration) { 
        BTraceUtils.Profiling.recordExit(swingProfiler, probeMethod, duration);
    }
    
    @OnTimer(5000)
    void timer() {
        BTraceUtils.Profiling.printSnapshot("Swing performance profile", swingProfiler);
    }
}

  

 

脚本启动命令

./bin/btrace <pid>  /Users/dongxiaoyang/Desktop/<btracescript.java>

 

参考资料 

 

用BTrace排查性能问题

Btrace入门到熟练小工完全指南

使用jvisualvm.exe 的Btrace插件介绍/使用教程

BTrace User\'s Guide

 

 

以上是关于btrace 笔记的主要内容,如果未能解决你的问题,请参考以下文章

BTrace

Jvisualvm+Btrace使用

基于Btrace的监控调试

Btrace入门到熟练小工完全指南

利用jvisualvm使用btrace进行线上调试案例

04 使用 BTrace 进行拦截调试