学习使用greys排查线上问题

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

观察的维度

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

观察的维度

变量名 变量解释
loader 本次调用类所在的ClassLoader
clazz 本次调用类的Class引用
method 本次调用方法反射引用
target 本次调用类的实例
params 本次调用参数列表,这是一个数组,如果方法是无参方法则为空数组
returnObj 本次调用返回的对象。当且仅当isReturn==true成立时候有效,表明方法调用是以正常返回的方式结束。如果当前方法无返回值void,则值为null
throwExp 本次调用抛出的异常。当且仅当isThrow==true成立时有效,表明方法调用是以抛出异常的方式结束。
isBefore 辅助判断标记,当前的通知节点有可能是在方法一开始就通知,此时isBefore==true成立,同时isThrow==false和isReturn==false,因为在方法刚开始时,还无法确定方法调用将会如何结束。
isThrow 辅助判断标记,当前的方法调用以抛异常的形式结束。
isReturn 辅助判断标记,当前的方法调用以正常返回的形式结束。

获取方法入参:

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

参数说明:

  1. -b 表示方法调用之前
  2. -e 表示方法抛出异常之后
  3. -s 表示方法返回之后
  4. -f 方法结束之后(正常返回和异常返回)
  5. params[0]表示第1个入参,params[x]表示第x+1个入参。
  6. -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
 
   
   
   
   
   
   
   
   
   
   

获取方法入参:

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

参数说明:

  1. -b 表示方法调用之前
  2. -e 表示方法抛出异常之后
  3. -s 表示方法返回之后
  4. -f 方法结束之后(正常返回和异常返回)
  5. params[0]表示第1个入参,params[x]表示第x+1个入参。
  6. -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

猜你喜欢

转载自blog.csdn.net/u011490072/article/details/81104050