Arthas功能介绍

系列

准备工作

  • 这篇文章的主要目的是对Arthas的使用做一个简单的入门介绍,包含通过官方提供的demo来作-为被测试对象,通过官方提供的arthas包进行调试。

  • 启动Demo

curl -O https://alibaba.github.io/arthas/arthas-demo.jar
java -jar arthas-demo.jar

arthas-demo是一个简单的程序,每隔一秒生成一个随机数,再执行质因数分解,并打印出分解结果。arthas-demo源代码:查看

  • 启动arthas
curl -O https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
  • 选择应用java进程
$ $ java -jar arthas-boot.jar
* [1]: 35542
  [2]: 71560 arthas-demo.jar


[INFO] Try to attach process 71560
[INFO] Attach process 71560 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'
 
wiki: https://alibaba.github.io/arthas
version: 3.0.5.20181127201536
pid: 71560
time: 2018-11-28 19:16:24
 
$

Demo进程是第2个,则输入2,再输入回车/enter。Arthas会attach到目标进程上,并输出日志。


命令帮助

[arthas@37711]$ help
 NAME         DESCRIPTION
 help         Display Arthas Help
 keymap       Display all the available keymap for the specified connection.
 sc           Search all the classes loaded by JVM
 sm           Search the method of classes loaded by JVM
 classloader  Show classloader info
 jad          Decompile class
 getstatic    Show the static field of a class
 monitor      Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.
 stack        Display the stack trace for the specified class and method
 thread       Display thread info, thread stack
 trace        Trace the execution time of specified method invocation.
 watch        Display the input/output parameter, return object, and thrown exception of specified method invocation
 tt           Time Tunnel
 jvm          Display the target JVM information
 perfcounter  Display the perf counter infornation.
 ognl         Execute ognl expression.
 mc           Memory compiler, compiles java files into bytecode and class files in memory.
 redefine     Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)
 dashboard    Overview of target jvms thread, memory, gc, vm, tomcat info.
 dump         Dump class byte array from JVM
 heapdump     Heap dump
 options      View and change various Arthas options
 cls          Clear the screen
 reset        Reset all the enhanced classes
 version      Display Arthas version
 session      Display current session information
 sysprop      Display, and change the system properties.
 sysenv       Display the system env.
 vmoption     Display, and update the vm diagnostic options.
 logger       Print logger info, and update the logger level
 history      Display command history
 cat          Concatenate and print files
 echo         write arguments to the standard output
 pwd          Return working directory name
 mbean        Display the mbean information
 grep         grep command for pipes.
 tee          tee command for pipes.
 profiler     Async Profiler. https://github.com/jvm-profiling-tools/async-profiler
 stop         Stop/Shutdown Arthas server and exit the console.


功能展示

Arthas基本的命令通过help命令就可以看到了,这里我们选择几个比较好玩的命令展示一下。


dashboard - 当前系统的实时数据面板

[arthas@37711]$ dashboard --help
 USAGE:
   dashboard [-h] [-i <value>] [-n <value>]

 SUMMARY:
   Overview of target jvm's thread, memory, gc, vm, tomcat info.

 EXAMPLES:
   dashboard
   dashboard -n 10
   dashboard -i 2000

 WIKI:
   https://alibaba.github.io/arthas/dashboard

 OPTIONS:
 -h, --help                                                          this help
 -i, --interval <value>                                              The interval (in ms) between two executions, default is 5000 ms.
 -n, --number-of-execution <value>                                   The number of times this command will be executed.


thread - 查看当前线程信息,查看线程的堆栈

[arthas@37711]$ thread -h
 USAGE:
   thread [-h] [-b] [-i <value>] [--state <value>] [-n <value>] [id]

 SUMMARY:
   Display thread info, thread stack

 EXAMPLES:
   thread
   thread 51
   thread -n -1
   thread -n 5
   thread -b
   thread -i 2000
   thread --state BLOCKED

 WIKI:
   https://alibaba.github.io/arthas/thread

 OPTIONS:
 -h, --help                                                          this help
 -b, --include-blocking-thread                                       Find the thread who is holding a lock that blocks the most number of threads.
 -i, --sample-interval <value>                                       Specify the sampling interval (in ms) when calculating cpu usage.
     --state <value>                                                 Display the thead filter by the state. NEW, RUNNABLE, TIMED_WAITING, WAITING, BLOCKED, TERMINATED is optional.
 -n, --top-n-threads <value>                                         The number of thread(s) to show, ordered by cpu utilization, -1 to show all.
 <id>                                                                Show thread stack


tt - 记录方法执行信息

[arthas@37711]$ tt -h
 USAGE:
   tt [-d] [--delete-all] [-x <value>] [-h] [-i <value>] [-n <value>] [-l] [-p] [-E] [--replay-interval <value>] [--replay-times <value>] [-s <value>] [-M <value>] [-t] [-w <value>] [class-pattern] [meth
 od-pattern] [condition-express]

 SUMMARY:
   Time Tunnel
   The express may be one of the following expression (evaluated dynamically):
           target : the object
            clazz : the object's class
           method : the constructor or method
           params : the parameters array of method
     params[0..n] : the element of parameters array
        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
            #cost : the execution time in ms of method invocation
 EXAMPLES:
   tt -t *StringUtils isEmpty
   tt -t *StringUtils isEmpty params[0].length==1
   tt -l
   tt -i 1000
   tt -i 1000 -w params[0]
   tt -i 1000 -p
   tt -i 1000 -p --replay-times 3 --replay-interval 3000
   tt --delete-all

 WIKI:
   https://alibaba.github.io/arthas/tt



sc - 查看JVM已加载的类信息

[arthas@37711]$ sc -h
 USAGE:
   sc [-c <value>] [-d] [-x <value>] [-f] [-h] [-E] class-pattern

 SUMMARY:
   Search all the classes loaded by JVM

 EXAMPLES:
   sc -d org.apache.commons.lang.StringUtils
   sc -d org/apache/commons/lang/StringUtils
   sc -d *StringUtils
   sc -d -f org.apache.commons.lang.StringUtils
   sc -E org\\.apache\\.commons\\.lang\\.StringUtils

 WIKI:
   https://alibaba.github.io/arthas/sc

 OPTIONS:
 -c, --classloader <value>                                           The hash code of the special class's classLoader
 -d, --details                                                       Display the details of class
 -x, --expand <value>                                                Expand level of object (0 by default)
 -f, --field                                                         Display all the member variables
 -h, --help                                                          this help
 -E, --regex                                                         Enable regular expression to match (wildcard matching by default)
 <class-pattern>                                                     Class name pattern, use either '.' or '/' as separator


watch - 方法执行数据观测

[arthas@37711]$ watch -h
 USAGE:
   watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express]

 SUMMARY:
   Display the input/output parameter, return object, and thrown exception of specified method invocation
   The express may be one of the following expression (evaluated dynamically):
           target : the object
            clazz : the object's class
           method : the constructor or method
           params : the parameters array of method
     params[0..n] : the element of parameters array
        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
            #cost : the execution time in ms of method invocation
 Examples:
   watch -b org.apache.commons.lang.StringUtils isBlank params
   watch -f org.apache.commons.lang.StringUtils isBlank returnObj
   watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2
   watch -bf *StringUtils isBlank params
   watch *StringUtils isBlank params[0]
   watch *StringUtils isBlank params[0] params[0].length==1
   watch *StringUtils isBlank params '#cost>100'
   watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]

 WIKI:
   https://alibaba.github.io/arthas/watch


trace - 输出方法路径上的节点耗时

  • trace 能方便的帮助你定位和发现因 RT 高而导致的性能问题缺陷,但其每次只能跟踪一级方法的调用链路。
[arthas@37711]$ trace -h
 USAGE:
   trace [-h] [-n <value>] [-p <value>] [-E] [--skipJDKMethod <value>] class-pattern method-pattern [condition-express]

 SUMMARY:
   Trace the execution time of specified method invocation.
   The express may be one of the following expression (evaluated dynamically):
           target : the object
            clazz : the object's class
           method : the constructor or method
           params : the parameters array of method
     params[0..n] : the element of parameters array
        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
            #cost : the execution time in ms of method invocation
 EXAMPLES:
   trace org.apache.commons.lang.StringUtils isBlank
   trace *StringUtils isBlank
   trace *StringUtils isBlank params[0].length==1
   trace *StringUtils isBlank '#cost>100'
   trace -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank
   trace -E com.test.ClassA|org.test.ClassB method1|method2|method3
   trace demo.MathGame run -n 5
   trace demo.MathGame run --skipJDKMethod false

 WIKI:
   https://alibaba.github.io/arthas/trace

 OPTIONS:
 -h, --help                                                          this help
 -n, --limits <value>                                                Threshold of execution times
 -p, --path <value>                                                  path tracing pattern
 -E, --regex                                                         Enable regular expression to match (wildcard matching by default)
     --skipJDKMethod <value>                                         skip jdk method trace, default value true.
 <class-pattern>                                                     Class name pattern, use either '.' or '/' as separator
 <method-pattern>                                                    Method name pattern
 <condition-express>                                                 Conditional expression in ognl style, for example:
                                                                       TRUE  : 1==1
                                                                       TRUE  : true
                                                                       FALSE : false
                                                                       TRUE  : 'params.length>=0'
                                                                       FALSE : 1==2


profiler - 使用async-profiler生成火焰图

[arthas@37711]$ profiler -h
 USAGE:
   profiler [--allkernel] [--alluser] [-e <value>] [-f <value>] [--format <value>] [-h] [-i <value>] [--threads] action [actionArg]

 SUMMARY:
   Async Profiler. https://github.com/jvm-profiling-tools/async-profiler

 EXAMPLES:
   profiler start
   profiler stop
   profiler list                # list all supported events
   profiler actions             # list all supported actions
   profiler start --event alloc
   profiler stop --format svg   # output file format, support svg,html,jfr
   profiler stop --file /tmp/result.html
   profiler stop --threads
   profiler status
   profiler resume              # Start or resume profiling without resetting collected data.
   profiler getSamples          # Get the number of samples collected during the profiling session
   profiler dumpFlat            # Dump flat profile, i.e. the histogram of the hottest methods
   profiler dumpCollapsed       # Dump profile in 'collapsed stacktraces' format
   profiler dumpTraces          # Dump collected stack traces
   profiler execute 'start'                       # Execute an agent-compatible profiling command
   profiler execute 'stop,file=/tmp/result.svg'   # Execute an agent-compatible profiling command

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271