• 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

     

  • 相关阅读:
    UVa 10213
    树莓派学习路程No.2 GPIO功能初识 wiringPi安装
    树莓派学习路程No.1 树莓派系统安装与登录 更换软件源 配置wifi
    《软件工程》 的课程总结附加题。
    软件工程《个人总结》
    Java super关键字活用
    软件工程:黄金G点小游戏1.0
    Android之获取数据库路径
    Android自定义折线图
    软件工程:vs单元测试
  • 原文地址:https://www.cnblogs.com/dongxiao-yang/p/6134393.html
Copyright © 2020-2023  润新知