本文主要是介绍Btrace使用详解以及实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Btrace
Btrace是一种java性能观测的工具,其项目地址为https://github.com/btraceio/btrace
根据开发项目组对于此工具的描述为一种安全的动态java跟踪工具
其原理是通过连接指定的正在运行的虚拟机,并根据脚本对于指定的方法进行字节码的替换,导致在执行正常代码的过程中获取并打印需要的信息。
Btrace环境支持
Btace对于运行的环境有个明确的要求,需要运行在JDK环境中,并且依赖系统参数JAVA_HOME,对此不再进行阐述
Btrace基本使用
在此仅以开源项目dble为例,在dble中由于在优化过程中需要观察一个查询各个时间的节点,需要对于每个时间点进行时间的打印和观测在代码中创建了观测类
/** Copyright (C) 2016-2018 ActionTech.* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.*/package com.actiontech.dble.btrace.provider;public class CostTimeProvider {public void beginRequest(long id) {}public void startProcess(long id) {}public void endParse(long id) {}public void endRoute(long id) {}public void resFromBack(long id) {}public void resLastBack(long id) {}public void execLastBack(long id) {}public void startExecuteBackend(long id) {}public void allBackendConnReceive(long id) {}public void beginResponse(long id) {}public void endDelive(long id) {}
}
在代码中每个对应的节点调用上述的方法,在这个类中,所有的实现方法都是空,所以在正常运行过程中不会对于项目中的代码造成影响。
创建观测脚本
/** Copyright (C) 2016-2018 ActionTech.* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.*/package com.actiontech.dble.btrace.script;import com.sun.btrace.BTraceUtils;
import com.sun.btrace.Profiler;
import com.sun.btrace.annotations.*;import java.util.Map;import static com.sun.btrace.BTraceUtils.Profiling;
import static com.sun.btrace.BTraceUtils.timeNanos;@BTrace
public class BTraceCostTime {private static Map<Long, Long> records = BTraceUtils.Collections.newHashMap();@Propertystatic Profiler profiler = BTraceUtils.Profiling.newProfiler();@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "beginRequest")public static void beginRequest(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {BTraceUtils.Collections.put(records, arg, timeNanos());}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "startProcess")public static void startProcess(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->1.startProcess", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "endParse")public static void endParse(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->2.endParse", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "endRoute")public static void endRoute(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->3.endRoute", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "endDelive")public static void endDelive(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->3.05.endDelive", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "resLastBack")public static void resLastBack(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->4L.resLastBack", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "execLastBack")public static void execLastBack(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->5L.execLastBack", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "resFromBack")public static void resFromBack(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->4.resFromBack", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "startExecuteBackend")public static void startExecuteBackend(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->5.startExecuteBackend", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "allBackendConnReceive")public static void allBackendConnReceive(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;Profiling.recordExit(profiler, "request->5.1.allBackendConnReceive", duration);}@OnMethod(clazz = "com.actiontech.dble.btrace.provider.CostTimeProvider",method = "beginResponse")public static void beginResponse(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, long arg) {Long ts = BTraceUtils.Collections.get(records, arg);if (ts == null) {return;}long duration = timeNanos() - ts;BTraceUtils.Collections.remove(records, arg);Profiling.recordExit(profiler, "request->6.response", duration);}@OnTimer(4000)public static void print() {BTraceUtils.Profiling.printSnapshot("profiling:", profiler);}
}
通过观测脚本的代码我们可以看到,事实上通过btrace提供的标签属性指定在脚本BTraceCostTime中的属性替换掉指定clazz里面的指定method方法,在正常代码运行到CostTimeProvider中的对应方法时,会被替换调用到BTraceCostTime的对应方法中,并进行统计通过定时方法print周期性的打印出来
其中调用的方法为
Btrace {java_pid} BTraceCostTime.java
Btrace非安全模式
在一般过程中在btrace的脚本编写时只能使用btrace允许的方法,(https://github.com/btraceio/btrace/wiki/Trace-Scripts)基本上就是BTraceUtils提供的对应方法,非常有限,并且会禁止调用其他对象的方法,但是有的时候我们对于btrace的应用会有特殊的场景,需要在替换的方法里面执行一些更加符合我们实际需要的代码。
这个时候就可以开启btarce的unsafe模式进行操作,这个模式有两个主要的修改点
1 修改btrace脚本,加入unsafe参数
${JAVA_HOME}/bin/java -Dcom.sun.btrace.unsafe=true -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar com.sun.btrace.client.Main $*
2 在脚本中注明unsafe参数
在类声明之前使用btrace提供的标签进行声明@BTrace(unsafe = true)
这篇关于Btrace使用详解以及实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!