Btrace使用详解以及实践

2024-02-21 10:38
文章标签 使用 实践 详解 btrace

本文主要是介绍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的应用会有特殊的场景,需要在替换的方法里面执行一些更加符合我们实际需要的代码。

这个时候就可以开启btarceunsafe模式进行操作,这个模式有两个主要的修改点

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使用详解以及实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/731494

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(