JVM诊断工具-Greys

简介: Greys为一款“事后工具” ,即服务已经上线了,无法再通过打印日志等方式进行埋点分析,此时可以借助此工具,来跟踪代码执行耗时、堆栈运行情况等。使用Greys,我们无需编写 脚步,它是命令交互式的,直接输入命令指定监控的类、方法。

 工具概述

     Greys为一款“事后工具” ,即服务已经上线了,无法再通过打印日志等方式进行埋点分析,此时可以借助此工具,来跟踪代码执行耗时、堆栈运行情况等。使用Greys,我们无需编写 脚步,它是命令交互式的,直接输入命令指定监控的类、方法。


 工作原理

    (1)基于动态字节码修改技术(Hotswap)来实现运行时 Java 程序的跟踪和替换。

    (2)利用了Java SE 6 新特性Instrumentation。


  使用场景

    (1)分析哪些方法慢,查询具体的故障点

    (2)查看方法的参数、返回值

    (3)查看对象属性等

       # 程序安装

1、在线安装方式(推荐)


[root@testserver ~]# curl -sLk http://ompc.oss.aliyuncs.com/greys/install.sh|bash
Tips:
  安装成功后,若出现“greys not found, please check your network” ,则需进行本地安装


2、本地安装方式

       (1)下载最新版本的Greys


[root@testserver ~]# wget http://ompc.oss.aliyuncs.com/greys/release/greys-stable-bin.zip


(2)解压zip文件后,执行以下命令,即可完成本地安装


[root@testserver ~]# cd greys
    [root@testserver ~]# sh ./install-local.sh


3、 操作指南


[root@testserver greys]# ./greys.sh 944————>“944 为 jvm的进程号”

如下,执行此命令后,显示greys相关信息:


[root@testserver greys]# ./greys.sh 944
                                                            _                    
      ____  ____ _____ _   _  ___ _____ _____ ____  _____ _| |_ ___  ____  _   _ 
    / _  |/ ___) ___ | | | |/___|_____|____ |  _ \(____ (_   _) _ \|    \| | | |
    ( (_| | |   | ____| |_| |___ |     / ___ | | | / ___ | | || |_| | | | | |_| |
    \___ |_|   |_____)\__  (___/      \_____|_| |_\_____|  \__)___/|_|_|_|\__  |
    (_____|           (____/                                              (____/ 
                                              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                                              |v|e|r|s|i|o|n|:|1|.|7|.|6|.|6|
                                              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    ga?>
    ga?>


4、常用命令参考


ga?>help
+----------+----------------------------------------------------------------------------------+
|       sc | Search all the classes loaded by JVM                                             |
+----------+----------------------------------------------------------------------------------+
|       sm | Search the method of classes loaded by JVM                                       |
+----------+----------------------------------------------------------------------------------+
|  monitor | Monitor the execution of specified Class and its method                          |
+----------+----------------------------------------------------------------------------------+
|    watch | Display the details of specified class and method                                |
+----------+----------------------------------------------------------------------------------+
|       tt | Time Tunnel                                                                      |
+----------+----------------------------------------------------------------------------------+
|    stack | Display the stack trace of specified class and method                            |
+----------+----------------------------------------------------------------------------------+
|   ptrace | Display the detailed thread path stack of specified class and method             |
+----------+----------------------------------------------------------------------------------+
|       js | Enhanced JavaScript                                                              |
+----------+----------------------------------------------------------------------------------+
|    trace | Display the detailed thread stack of specified class and method                  |
+----------+----------------------------------------------------------------------------------+
|  session | Display current session information                                              |
+----------+----------------------------------------------------------------------------------+
|     quit | Quit Greys console                                                               |
+----------+----------------------------------------------------------------------------------+
|  version | Display Greys version                                                            |
+----------+----------------------------------------------------------------------------------+
|      jvm | Display the target JVM information                                               |
+----------+----------------------------------------------------------------------------------+
|    reset | Reset all the enhanced classes                                                   |
+----------+----------------------------------------------------------------------------------+
|      asm | Display class bytecode by asm format                                             |
+----------+----------------------------------------------------------------------------------+
| shutdown | Shut down Greys server and exit the console                                      |
+----------+----------------------------------------------------------------------------------+
|     help | Display Greys Help                                                               |
+----------+----------------------------------------------------------------------------------+
|      top | Display The Threads Of Top CPU TIME                                              |
+----------+----------------------------------------------------------------------------------+


5、具体参考示例

      针对help命令行所显示的操作中,若进行相关操作,可通过执行“help arg”命令查询使用方式,例如:help stack/trace/ptrace/tt/monitor


ga?>help stack
+---------+----------------------------------------------------------------------------------+
|   USAGE | -[En:] class-pattern method-pattern condition-express                            |
|         | Display the stack trace of specified class and method                            |
+---------+----------------------------------------------------------------------------------+
| OPTIONS |              [E] | Enable regular expression to match (wildcard matching by def  |
|         |                  | ault)                                                         |
|         | -----------------+-------------------------------------------------------------- |
|         |             [n:] | Threshold of execution times                                  |
|         | -----------------+-------------------------------------------------------------- |
|         |    class-pattern | Path and classname of Pattern Matching                        |
|         | -----------------+-------------------------------------------------------------- |
|         |   method-pattern | Method of Pattern Matching                                    |
|         | -----------------+-------------------------------------------------------------- |
|         |  condition-expre | Conditional expression by OGNL                                |
|         |               ss |                                                               |
|         |                  | FOR EXAMPLE                                                   |
|         |                  |      TRUE : 1==1                                              |
|         |                  |      TRUE : true                                              |
|         |                  |     FALSE : false                                             |
|         |                  |      TRUE : params.length>=0                                  |
|         |                  |     FALSE : 1==2                                              |
|         |                  |                                                               |
|         |                  | THE STRUCTURE                                                 |
|         |                  |           target : the object                                 |
|         |                  |            clazz : the object's class                         |
|         |                  |           method : the constructor or method                  |
|         |                  |     params[0..n] : the parameters of method                   |
|         |                  |        returnObj : the returned object of method              |
|         |                  |         throwExp : the throw exception of method              |
|         |                  |         isReturn : the method ended by return                 |
|         |                  |          isThrow : the method ended by throwing exception     |
+---------+----------------------------------------------------------------------------------+
| EXAMPLE | stack -E org\.apache\.commons\.lang\.StringUtils isBlank                         |
|         | stack org.apache.commons.lang.StringUtils isBlank                                |
|         | stack *StringUtils isBlank                                                       |
|         | stack *StringUtils isBlank 'params[0].length==1'                                 |
|         | stack *StringUtils isBlank '#cost>100'                                           |
+---------+----------------------------------------------------------------------------------+

      以Trace命令为例,使用 trace命令 跟踪指定类、方法的执行时间、参数、返回值情况,如下场景:(此处以官网给的参考为主)

      场景:跟踪CookieDecoder类中 requestDecode()方法耗时超过500ms 的方法执行情况:


ga?>trace  -n 2  com.netease.urs.CookieDecoder  requestDecode    '#cost>500'
*************************************************************************************************************
 *   -n 2 :代表只打印2次就退出(防止刷屏,影响性能)                                                           
 *   com.netease.urs.CookieDecoder :监听的类名                                                               *
 *   requestDecode :监听的方法名                                                                              *
 *   ‘#cost>500’ : 打印条件为耗时超过 500ms                                                                  *
 *************************************************************************************************************    执行后,会显示:
ga?>trace  -n 2  com.netease.urs.CookieDecoder  requestDecode    '#cost>10'
Press Ctrl+D to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 262 ms.

代表动态修改了一个类,对两个方法(例如方法重载)进行监控,修改花费262毫秒。            如果出现满足条件的情况,则我们会看到打印结果:


`---+Tracing for : thread_name="http-nio-8003-exec-8" thread_id=0x7a;is_daemon=true;priority=5;
`---+[5283,5283ms]com.netease.urs.CookieDecoder:requestDecode()
    +---[1,0ms]java.lang.System:nanoTime()
    +---[2,1ms]org.apache.http.client.methods.HttpPost:<init>(@39)
    +---[2,0ms]java.lang.StringBuffer:<init>(@41)
    +---[2,0ms]java.lang.StringBuffer:append(@42)
    +---[2,0ms]java.net.URLEncoder:encode(@43)
    +---[2,0ms]java.lang.StringBuffer:append(@43)
    +---[2,0ms]java.lang.StringBuffer:append(@44)
    +---[2,0ms]java.lang.StringBuffer:append(@45)
    +---[2,0ms]java.lang.StringBuffer:append(@46)
    +---[2,0ms]java.lang.StringBuffer:append(@47)
    +---[2,0ms]java.lang.StringBuffer:append(@48)
    +---[2,0ms]java.lang.Integer:<init>(@49)
    +---[2,0ms]java.lang.Integer:<init>(@49)
    +---[2,0ms]java.lang.reflect.Method:invoke(@49)
    +---[2,0ms]java.lang.StringBuffer:append(@49)
    +---[2,0ms]java.lang.StringBuffer:toString(@50)
    +---[2,0ms]org.apache.http.entity.StringEntity:<init>(@50)
    +---[2,0ms]org.apache.http.entity.StringEntity:setContentType(@51)
    +---[2,0ms]org.apache.http.client.methods.HttpPost:setEntity(@52)
    +---[2,0ms]org.apache.http.client.methods.HttpPost:getParams(@53)
    +---[2,0ms]org.apache.http.params.HttpParams:setIntParameter(@55)
    +---[2,0ms]org.apache.http.params.HttpParams:setIntParameter(@58)
    +---[5282,5280ms]com.netease.urs.http.CustomHttpComponent:execute(@60)
    +---[5283,0ms]org.apache.http.HttpResponse:getEntity(@61)
    +---[5283,0ms]org.apache.http.util.EntityUtils:toString(@62)
    +---[5283,0ms]com.netease.urs.util.LogUtil:debug(@63)
    +---[5283,0ms]org.apache.http.client.methods.HttpPost:releaseConnection(@71)
    +---[5283,0ms]java.lang.System:nanoTime(@64)
    `---[5283,0ms]com.netease.urs.CookieDecoder:$btrace$com$netease$fa$trace$UrsInterfaceCalls$2$requestDecode(@64)

可以看到,主要耗时在

       +---[5282,5280ms]com.netease.urs.http.CustomHttpComponent:execute(@60)

       退出前可以使用 reset 恢复增强类(即被动态修改的代码)。最后,使用shutdown 关闭greys 并退出即可。

相关文章
|
7月前
|
Arthas 监控 Java
(十一)JVM成神路之性能调优篇:GC调优、Arthas工具详解及各场景下线上最佳配置推荐
“在当前的互联网开发模式下,系统访问量日涨、并发暴增、线上瓶颈等各种性能问题纷涌而至,性能优化成为了现时代开发过程中炙手可热的名词,无论是在开发、面试过程中,性能优化都是一个常谈常新的话题”。
628 3
|
3月前
|
Arthas Prometheus 监控
监控堆外使用JVM工具
监控堆外使用JVM工具
65 7
|
4月前
|
JavaScript 前端开发 Java
jvm的jshell,学生的工具
本文介绍了JVM的jshell工具,它为Java平台添加了REPL(读取-评估-打印循环)功能,使得学习、探索编码和原型代码变得更加便捷,但作者认为其在实际开发中较为鸡肋。
59 1
jvm的jshell,学生的工具
|
4月前
|
Arthas 监控 数据可视化
JVM进阶调优系列(7)JVM调优监控必备命令、工具集合|实用干货
本文介绍了JVM调优监控命令及其应用,包括JDK自带工具如jps、jinfo、jstat、jstack、jmap、jhat等,以及第三方工具如Arthas、GCeasy、MAT、GCViewer等。通过这些工具,可以有效监控和优化JVM性能,解决内存泄漏、线程死锁等问题,提高系统稳定性。文章还提供了详细的命令示例和应用场景,帮助读者更好地理解和使用这些工具。
|
4月前
|
存储 监控 算法
JVM调优深度剖析:内存模型、垃圾收集、工具与实战
【10月更文挑战第9天】在Java开发领域,Java虚拟机(JVM)的性能调优是构建高性能、高并发系统不可或缺的一部分。作为一名资深架构师,深入理解JVM的内存模型、垃圾收集机制、调优工具及其实现原理,对于提升系统的整体性能和稳定性至关重要。本文将深入探讨这些内容,并提供针对单机几十万并发系统的JVM调优策略和Java代码示例。
85 2
|
4月前
|
小程序 Oracle Java
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
这篇文章是关于JVM基础知识的介绍,包括JVM的跨平台和跨语言特性、Class文件格式的详细解析,以及如何使用javap和jclasslib工具来分析Class文件。
78 0
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
|
6月前
|
缓存 监控 算法
吃透 JVM 诊断方法与工具使用
【8月更文挑战第4天】深入了解并掌握JVM诊断需把握几大要点:1) 熟悉JVM内存模型,如堆、栈及方法区;2) 掌握垃圾回收机制与算法;3) 运用工具如`jps`(查看Java进程)、`jstat`(监控运行状态)、`jmap`(生成堆快照)、`jhat`(分析堆快照)、`jstack`(检查线程栈); 4) 利用专业工具如Eclipse Memory Analyzer分析堆转储文件查找内存泄漏; 5) 动态监控与调整JVM参数; 6) 结合日志分析性能瓶颈。通过实战案例加深理解,有效应对JVM性能问题。
|
6月前
|
Arthas Prometheus 监控
使用JDK自带工具调优JVM的常用命令
使用JDK自带工具调优JVM的常用命令
|
6月前
|
监控 Java Android开发
吃透 JVM 诊断方法与工具使用
【8月更文挑战第3天】要精通JVM诊断,需掌握关键监控指标如内存(堆/非堆)、CPU使用及线程状态;熟悉工具如`jstat`(监控状态)、`jmap`(堆转储)、`jstack`(线程堆栈);并能利用Eclipse Memory Analyzer (MAT)分析堆转储找内存泄漏;同时理解GC日志以优化垃圾回收行为;通过实践案例加深理解。
|
3月前
|
缓存 Prometheus 监控
Elasticsearch集群JVM调优设置合适的堆内存大小
Elasticsearch集群JVM调优设置合适的堆内存大小
635 1