Greys是一个JVM进程执行过程中的异常诊断工具。 在不中断程序执行(不重启服务)的情况下轻松完成JVM相关问题排查工作。
安装和启动
下载greys脚本:
curl -sLk http://ompc.oss.aliyuncs.com/greys/install.sh|sh
启动脚本:
./greys.sh pid
如何获取pid:
ps -ef|grep java
然后从输出中找到你需要的Java进程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
观察的维度
获取方法入参:
watch -b +全类名 +方法名 +观察的维度 -x 1
watch -b com.room.*.DiscussServiceImpl gene params[0] -x 1
trace com.room.*.DiscussServiceImpl gene
示例输出:
ga?>watch -f com.room.*.DiscussServiceImpl gene params[0] -x 1
Press Ctrl+D to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 90 ms.
@SearchDto[
place=@String[北京],
roomTypes=@Integer[0],
page=@Integer[1],
pageSize=@Integer[13],
authorId=null,
city=@Integer[1],
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
获取方法的返回值
watch -f +全类名 +方法名 +观察的维度 -x 1
watch -f com.room.*.DiscussServiceImpl gene returnObj -x 1
示例输出:
得到一个方法执行的返回值
ga?>watch -f com.room.*.DiscussServiceImpl gene returnObj -x 1
Press Ctrl+D to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 92 ms.
@String[ 1 =1 And title like '%北京%' And city = 1 order by update_time desc limit 0 , 13]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
参数说明:
- -b 表示方法调用之前
- -e 表示方法抛出异常之后
- -s 表示方法返回之后
- -f 方法结束之后(正常返回和异常返回)
- params[0]表示第1个入参,params[x]表示第x+1个入参。
- -x 1表示展开第一个层级(和调用toString()打印对象类似),比如展开对象的属性,如果属性里面还有对象,就需要展开2级层级(-x 2)
搜索class-pattern/method-pattern所渲染的方法调用路径,渲染和统计整个调用链路上的所有性能开销和追踪调用链路。
trace +类名+方法名
trace com.room.*.DiscussServiceImpl gene
示例输出:
ga?>trace com.room.*.DiscussServiceImpl gene
Press Ctrl+D to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 350 ms.
`---+Tracing for : thread_name="http-nio-80-exec-10" thread_id=0x21;is_daemon=true;priority=5;
`---+[2,2ms]com.room.service.serviceImpl.DiscussServiceImpl:gene()
+---[0,0ms]com.taobao.profile.Profiler:Start()
+---[1,0ms]java.lang.StringBuilder:<init>(@30)
+---[1,0ms]java.lang.StringBuilder:append(@31)
+---[1,0ms]com.room.entity.SearchDto:getPlace(@32)
+---[1,0ms]org.apache.commons.lang.StringUtils:isNotEmpty(@32)
+---[1,0ms]java.lang.StringBuilder:append(@33)
+---[1,0ms]com.room.entity.SearchDto:getPlace(@33)
+---[1,0ms]java.lang.StringBuilder:append(@33)
+---[1,0ms]java.lang.StringBuilder:append(@33)
+---[1,0ms]com.room.entity.SearchDto:getRoomTypes(@35)
+---[1,0ms]com.room.entity.SearchDto:getAuthorId(@38)
+---[1,0ms]org.apache.commons.lang.StringUtils:isNotEmpty(@38)
+---[1,0ms]com.room.entity.SearchDto:getCity(@41)
+---[1,0ms]java.lang.StringBuilder:<init>(@42)
+---[1,0ms]com.room.entity.SearchDto:getCity(@42)
+---[1,0ms]java.lang.StringBuilder:append(@42)
+---[1,0ms]java.lang.StringBuilder:toString(@42)
+---[1,0ms]java.lang.StringBuilder:append(@42)
+---[1,0ms]java.lang.StringBuilder:append(@44)
+---[1,0ms]com.room.entity.SearchDto:getPage(@45)
+---[1,0ms]com.room.entity.SearchDto:getPageSize(@45)
+---[1,0ms]java.lang.StringBuilder:append(@46)
+---[1,0ms]java.lang.StringBuilder:append(@46)
+---[1,0ms]com.room.entity.SearchDto:getPageSize(@46)
+---[1,0ms]java.lang.StringBuilder:append(@46)
+---[1,0ms]java.lang.StringBuilder:toString(@47)
`---[1,0ms]com.taobao.profile.Profiler:End(@47)
[1,0ms]的含义,1所代表的含义是:当前节点的整体耗时;0的含义是:当前节点在当前步骤的耗时;两者之间用逗号分割,单位为毫秒。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
查询一个类是否被加载到JVM中,以及该Class信息,比如是由哪个类加载器加载的
ga?>sc -d com.room.*.UserController
+----------------------------------------------------+----------------------------------------------------------------------------------+
| class-info | com.room.controller.UserController |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| code-source | /opt/soft/tomcat/webapps/ROOT/WEB-INF/classes/ |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| name | com.room.controller.UserController |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isInterface | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isAnnotation | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isEnum | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isAnonymousClass | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isArray | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isLocalClass | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isMemberClass | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isPrimitive | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| isSynthetic | false |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| simple-name | UserController |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| modifier | public |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| annotation | org.springframework.web.bind.annotation.RequestMapping,org.springframework.stere |
| | otype.Controller |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| interfaces | |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| super-class | java.lang.Object |
+----------------------------------------------------+----------------------------------------------------------------------------------+
| class-loader | WebappClassLoader |
| | context: ROOT |
| | delegate: false |
| | ----------> Parent Classloader: |
| | java.net.URLClassLoader@27d6c5e0 |
| | |
| | `-java.net.URLClassLoader@27d6c5e0 |
| | `-sun.misc.Launcher$AppClassLoader@18b4aac2 |
| | `-sun.misc.Launcher$ExtClassLoader@97e1986 |
+----------------------------------------------------+----------------------------------------------------------------------------------+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48