tags: java, troubleshooting, monitor,arthas


一句话概括:Arthas是Alibaba开源的Java诊断工具,适合在线分析诊断java应用,它算是java监控工具的集大成者,本文将对它进行介绍。

1 引言

上一篇文章《java应用监测(7)-在线动态诊断神器BTrace》我们提到,如果需要不停服务情况下对线上应用进行监测,需要使用动态追踪技术,BTrace是很好动态追踪工具,但是使用起来还是有点复杂(需要写脚本),那么有没有更简单一点的工具,那就是Arthas了,它是阿里开源的Java诊断工具,进一步简化了动态追踪技术,有了它,可以直接查看类加载信息、JVM信息、线程堆栈信息、追踪方法执行情况,反编译类文件等,基本涵盖了前面提到的java命令行工具的功能和BTrace的功能。无需太多额外工作,只需熟悉Arthas提供的命令即可,很方便。其实Arthas底层跟BTrace一样,是基于jvm Agent方式,使用Instrumentation方式修改字节码方式然后执行、输出。本文将对Arthas的使用进行介绍,鉴于官方文档已经写得很详细,建议大家直接阅读官方文档来学习,本文只作抛砖引玉便可。官网地址:https://alibaba.github.io/arthas

2 Arthas安装与运行

2.1 下载Arthas

官方推荐直接下载jar运行

  1. wget https://alibaba.github.io/arthas/arthas-boot.jar

2.2 运行Arthas

把下载好的arthas-boot.jar包放到想要监测的java应用所在服务器,跟Spring Boot应用一样,直接使用java命令运行即可。

  1. java -jar arthas-boot.jar

注意:

  • 第一次运行,下载慢可以使用--repo-mirror aliyun --use-http
  • 启动后,会列出当前的java应用列表(有点像jps -l),输出序号选择想要监测的应用即可。

启动后即进行Arthas的命令行界面,可以使用Arthas提供的命令来实现需要监测的功能。如下图,需要监测的java应用是示例java-monitor-example。

java应用监测(8)-阿里诊断工具arthas - 图1

2.3 退出

如果只是退出当前的连接,可以用quit或者exit命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。

如果想完全退出arthas,可以执行shutdown命令。

3 Arthas使用

Arthas的使用就是需要学会使用它提供的命令功能,主要分为几大类:

  • 基本命令:help、cat、pwd、history,quit等等,跟linux的命令差不多。
  • jvm相关:dashboard、thread、jvm、sysenv等等,主要是对JVM信息的监测,跟之前学习java命令行工具jinfo、jmap、jstack等有异曲同工之妙。
  • class/classloader相关:sc、sm、jad、dump、classloader等等。
  • monitor/watch/trace相关:monitor、watch、trace、stack等等,这些功能基本涵盖了BTrace中所实现的功能,包括定时检测,方法参数,返回值,调用时长等。

下面对常用的几个命令进行说明,详细的命令列表请查阅官方文档。

3.1 概览:dashboard

启动Arthas后,-h查看使用帮助:

  1. $ dashboard -h
  2. USAGE:
  3. dashboard [-b] [-h] [-i <value>] [-n <value>]
  4. SUMMARY:
  5. Overview of target jvm's thread, memory, gc, vm, tomcat info.
  6. EXAMPLES:
  7. dashboard
  8. dashboard -n 10
  9. dashboard -i 2000

相当于概览,在一个界面中显示线程、内存、gc情况,vm情况和tomcat信息。如下图(官方文档的示例图):

java应用监测(8)-阿里诊断工具arthas - 图2

这个概览的信息默认5秒刷新一次,对于内存变化,线程占用情况,GC次数,一目了然。使用ctrl+c退出。

3.2 线程信息:thread

还记得jstack吗,我们需要先找出线程ID,使用它导出线程堆栈,然后使用线程ID查看。在Arthas中就方便多了,像上面dashboard中,已经有ID,直接使用thread id即可。-h查看帮助文档:

  1. $ thread -h
  2. USAGE:
  3. thread [-h] [-b] [-i <value>] [-n <value>] [id]
  4. SUMMARY:
  5. Display thread info, thread stack
  6. EXAMPLES:
  7. thread
  8. thread 51
  9. thread -n -1
  10. thread -n 5
  11. thread -b
  12. thread -i 2000
  13. OPTIONS:
  14. -h, --help this help
  15. -b, --include-blocking-thread Find the thread who is holding a lock that blocks the most number of threads.
  16. -i, --sample-interval <value> Specify the sampling interval (in ms) when calculating cpu usage.
  17. -n, --top-n-threads <value> The number of thread(s) to show, ordered by cpu utilization, -1 to show all.
  18. <id> Show thread stack

如上面所示的EXAMPLES,使用thread命令,可以找出占用CPU最高前N个线程(-n),可以打印指定线程的运行堆栈(id),找出当前阻塞其他线程的线程(-b),由此来分析线程问题很方便。

3.3 JVM信息:jvm

jvm命令很简单,没有参数,它输出的信息包括运行参数,类加载信息, 内存情况,系统信息,线程数信息,文件描述符等。有点像jvisualvm的中概述,但比它更详细。

3.4 反编译:jad

有时需要检测线上运行的应用中,新的代码是否有使用或者是否有更新到,可以把加载的类反编译出来,查看源码是否为最新的,这时jad就很有用。-h打印使用帮助:

  1. $ jad -h
  2. USAGE:
  3. jad [-c <value>] [-h] [-E] [--source-only] class-pattern [method-name]
  4. EXAMPLES:
  5. jad java.lang.String
  6. jad java.lang.String toString
  7. jad --source-only java.lang.String
  8. jad -c 39eb305e org/apache/log4j/Logger
  9. jad -c 39eb305e -E org\\.apache\\.*\\.StringUtils
  10. OPTIONS:
  11. -c, --code <value> The hash code of the special class's classLoader
  12. -h, --help this help
  13. -E, --regex Enable regular expression to match (wildcard matching by default)
  14. --source-only Output source code only
  15. <class-pattern> Class name pattern, use either '.' or '/' as separator
  16. <method-name> method name pattern, decompile a specific method instead of the whole class

如上面所示的EXAMPLES,jad可以反编译类(class-pattern),反编译类的某个方法(method-name),如果有多个classLoader,还可以使用-c来选择显示哪个等。

3.5 方法执行监控:monitor

monitor可以定时输出方法的执行情况进行监控,包括调用次数,成功次数,失败次数,平均时长,失败率等,有点像BTrace中的@Timer,但是更方便。-h查看使用帮助:

  1. $ monitor -h
  2. USAGE:
  3. monitor [-c <value>] [-h] [-n <value>] [-E] class-pattern method-pattern
  4. SUMMARY:
  5. Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.
  6. Examples:
  7. monitor org.apache.commons.lang.StringUtils isBlank
  8. monitor org.apache.commons.lang.StringUtils isBlank -c 5
  9. monitor -E org\.apache\.commons\.lang\.StringUtils isBlank
  10. OPTIONS:
  11. -c, --cycle <value> The monitor interval (in seconds), 60 seconds by default
  12. -h, --help this help
  13. -n, --limits <value> Threshold of execution times
  14. -E, --regex Enable regular expression to match (wildcard matching by default)
  15. <class-pattern> Path and classname of Pattern Matching
  16. <method-pattern> Method of Pattern Matching

如上面所示的EXAMPLES,可以监测方法执行情况,默认是60s输出一次,可以使用-c来修改输出间隔时间。

3.6 方法执行数据监测:watch

类似于BTrace的@OnMethod,若想在线上的应用中把执行方法时的参数,返回值,异常信息,watch命令就非常合适。-h使用帮助:

  1. $ watch -h
  2. USAGE:
  3. watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express]
  4. SUMMARY:
  5. Display the input/output parameter, return object, and thrown exception of specified method invocation
  6. The express may be one of the following expression (evaluated dynamically):
  7. target : the object
  8. clazz : the object's class
  9. method : the constructor or method
  10. params : the parameters array of method
  11. params[0..n] : the element of parameters array
  12. returnObj : the returned object of method
  13. throwExp : the throw exception of method
  14. isReturn : the method ended by return
  15. isThrow : the method ended by throwing exception
  16. #cost : the execution time in ms of method invocation
  17. Examples:
  18. watch -b org.apache.commons.lang.StringUtils isBlank params
  19. watch -f org.apache.commons.lang.StringUtils isBlank returnObj
  20. watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2
  21. watch -bf *StringUtils isBlank params
  22. watch *StringUtils isBlank params[0]
  23. watch *StringUtils isBlank params[0] params[0].length==1
  24. watch *StringUtils isBlank params '#cost>100'
  25. watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]
  26. OPTIONS:
  27. -b, --before Watch before invocation
  28. -e, --exception Watch after throw exception
  29. -x, --expand <value> Expand level of object (1 by default)
  30. -f, --finish Watch after invocation, enable by default
  31. -h, --help this help
  32. -n, --limits <value> Threshold of execution times
  33. -E, --regex Enable regular expression to match (wildcard matching by default)
  34. -M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1024 by default)
  35. -s, --success Watch after successful invocation
  36. <class-pattern> The full qualified class name you want to watch
  37. <method-pattern> The method name you want to watch
  38. <express> the content you want to watch, written by ognl.
  39. Examples:
  40. params
  41. params[0]
  42. 'params[0]+params[1]'
  43. '{params[0], target, returnObj}'
  44. returnObj
  45. throwExp
  46. target
  47. clazz
  48. method

如上面所示的EXAMPLES,监测时机分别是方法执行前(-b),方法执行结束(-f,默认值),方法执行成功(-s)。监测内容包括:参数(params),返回值(returnObj),异常(throwExp)等。

4 总结

Arthas的强大功能,基本可以对java应用进行全面的监测,是线上应用监测的好帮手,本文相对比较简单地对Arthas的安装和使用进行入门讲解,抛砖引玉,希望大家能上Arthas官方网站进一步学习,里面已经对各命令都有详细的讲解和示例,从而让自己的java监测技术更扎实。

参数资料

  • Arthas官网:https://alibaba.github.io/arthas/

相关阅读