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 并退出即可。