介绍
Btrace (Byte Trace)是sun推出的一款java 动态、安全追踪(监控)工具,可以不停机的情况下监控线上情况,并且做到最少的侵入,占用最少的系统资源。
In a way, BTrace scripts are very similar to AOP's aspects, but can be attached to any existing Java code (or better, bytecode) at runtime and without any configuration or modification at development time.
运行环境搭建
Steps to run BTrace
1.下载[btrace|http://kenai.com/projects/btrace/downloads/directory/releases]包 并把btrace的命令放到path中
2. jps命令查出需要监控的jvm pid
3. 编写BTrace程序 (安装包中带有大量的例子)
4. Run btrace tool by the following command line:
btrace <pid> <btrace-script>
BTrace能作的事情
记录以下事件:
* method calls;
* execution times;
* constructor invocation;
* available memory;
触发脚本执行的场景:
Script execution is tied to several situations
* reaching a specific line number;
* invoking a given method;
* returning from a method;
* invoking system calls (e.g. exit);
* recurring timer (period execution at fixed intervals);
BTrace不能作的事情
保证BTrace追踪的是只读操作,所以有一堆限制:
不仅强制要求java脚本需要提供public static方法.而且,脚本里无法实例化对象,数组,不能抛异常或捕捉,不能有循环,内部类等。
特别是不能调用任何的实例、静态方法,除了com.sun.btrace.BTraceUtils类static methods。*所以BTrace的功能全部在BTraceUtils中的方法。*
To guarantee that the tracing actions are "read-only" (i.e., the trace actions don't change the state of the program traced) and bounded (i.e., trace actions terminate in bounded time), a BTrace program is allowed to do only a restricted set of actions. In particular, a BTrace class
* can not create new objects.
* can not create new arrays.
* can not throw exceptions.
* can not catch exceptions.
* can not make arbitrary instance or static method calls - only the public static methods of com.sun.btrace.BTraceUtils class may be called from a BTrace program.
* can not assign to static or instance fields of target program's classes and objects. But, BTrace class can assign to it's own static fields ("trace state" can be mutated).
* can not have instance fields and methods. Only static public void returning methods are allowed for a BTrace class. And all fields have to be static.
* can not have outer, inner, nested or local classes.
* can not have synchronized blocks or synchronized methods.
* can not have loops (for, while, do..while)
* can not extend arbitrary class (super class has to be java.lang.Object)
* can not implement interfaces.
* can not contains assert statements.
* can not use class literals.
例子
监控某个方法的执行时间
- <strong><em>import static com.sun.btrace.BTraceUtils.name;
- import static com.sun.btrace.BTraceUtils.print;
- import static com.sun.btrace.BTraceUtils.println;
- import static com.sun.btrace.BTraceUtils.probeClass;
- import static com.sun.btrace.BTraceUtils.probeMethod;
- 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.TLS;
- /**
- * 监控方法耗时
- *
- */
- @BTrace
- public class PrintTimes {
- /**
- * 开始时间
- */
- @TLS
- private static long startTime = 0;
- /**
- * 方法开始时调用
- */
- @OnMethod(clazz = "/com\\.mike\\../", method = "/.+/")
- public static void startMethod() {
- startTime = timeMillis();
- }
- /**
- * 方法结束时调用<br>
- * Kind.RETURN这个注解很重要
- */
- @SuppressWarnings("deprecation")
- @OnMethod(clazz = "/com\\.mike\\../", method = "/.+/", location = @Location(Kind.RETURN))
- public static void endMethod() {
- print(strcat(strcat(name(probeClass()), "."), probeMethod()));
- print(" [");
- print(strcat("Time taken : ", str(timeMillis() - startTime)));
- println("]");
- }
- }</em></strong>
监控所有HTTP request请求的执行时间
- @TLS
- private static String invoke = "";
- @OnMethod(clazz = "javax.servlet.http.HttpServlet", method = "service", type = "void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)")
- public static void start(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
- invoke = concat(str(threadId(currentThread())), strcat("_", str(timeMillis())));
- print(invoke);
- print(" ");
- print(probeClass);
- print(" ");
- print(probeMethod);
- print(" s ");
- println(timeMillis());
- }
- @OnMethod(clazz = "javax.servlet.http.HttpServlet", method = "service", type = "void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)", location = @Location(Kind.RETURN))
- public static void end(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
- print(invoke);
- print(" ");
- print(probeClass);
- print(" ");
- print(probeMethod);
- print(" e ");
- println(timeMillis());
- invoke = "";
- }
监控所有HTTP request请求的具体java调用栈 call stack以及执行时间
在上面监控http request的基础上,增加你关心类的方法。然后通过ID(Thread ID, 时间戳)生成对应的调用图。
相关推荐
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot生鲜鲜花类及数据处理平台源码+论文+视频.zip
毕设和企业适用springboot企业数据智能分析平台类及汽车管理平台源码+论文+视频
毕设和企业适用springboot社区物业类及企业创新研发平台源码+论文+视频
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Floating Text Example</title> <style> .floating-text { font-size: 24px; position: relative; animation: float 3s ease-in-out infinite; } @keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-20px); } } </style> </head> <body> <div class="floating-text">Hello, I'm floating!</div> <script> document.addEventListener('DOMContentLoaded', function() {
毕设和企业适用springboot社交媒体分析平台类及智慧医疗管理平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及餐饮管理平台源码+论文+视频
毕设和企业适用springboot人工智能客服系统类及用户行为分析平台源码+论文+视频
毕设和企业适用springboot全渠道电商平台类及个性化广告平台源码+论文+视频
毕设和企业适用springboot社交互动平台类及线上图书馆源码+论文+视频
毕设和企业适用springboot企业知识管理平台类及供应链优化平台源码+论文+视频
毕设和企业适用springboot企业健康管理平台类及数据处理平台源码+论文+视频.zip
内容概要:本文档是一份面向初学者的详细指南,重点介绍如何利用Vue.js 2.0快速创建和运行简单的Todo List应用。首先指导安装必需的Node.js、npm/yarn等环境准备,接着通过Vue CLI工具生成新的Vue项目,再详细介绍项目目录和组件的构建方式。最后提供了具体的方法实现添加和删除待办事项,并指导如何使用命令启动应用,查看结果。 适合人群:具备基础Web开发技能的前端开发新手,尤其是对Vue框架感兴趣的学习者。 使用场景及目标:作为初学者入门级的学习资料,本文档的目标是让读者能够在最短时间内掌握Vue.js的基础概念和技术栈的应用方式,以便日后可以独立地构建更加复杂的Vue应用。 其他说明:除了学习如何构建应用程序之外,本文档还涵盖了Vue的基本语法和数据绑定、事件处理机制等重要概念,对于理解Vue框架的工作原理十分有帮助。
毕设和企业适用springboot企业健康管理平台类及智能化系统源码+论文+视频.zip
毕设和企业适用springboot企业健康管理平台类及远程医疗平台源码+论文+视频.zip
毕设和企业适用springboot数据可视化类及数据智能化平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及用户体验优化平台源码+论文+视频.zip
毕设和企业适用springboot人工智能客服系统类及虚拟银行平台源码+论文+视频
毕设和企业适用springboot社交应用平台类及云计算资源管理平台源码+论文+视频
毕设和企业适用springboot企业数据监控平台类及线上图书馆源码+论文+视频