阿里重磅开源在线分析诊断工具Arthas(阿尔萨斯)

github地址:

Arthas

arthas

English version goes here.

Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

  1. 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  2. 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  3. 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  4. 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  5. 是否有一个全局视角来查看系统的运行状况?
  6. 有什么办法可以监控到JVM的实时运行状态?

Arthas采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

快速开始

Linux/Unix/Mac

安装Arthas:

curl -L https://alibaba.github.io/arthas/install.sh | sh

启动Arthas:

./as.sh

Windows

  1. 点击 这里 下载最新的Arthas zip包
  2. 解压缩zip包.
  3. 进入bin目录
  4. 执行以下命令 as.bat $PID

文档

社区正在进行英文版本的翻译工作,如果您有兴趣请在 这里留言。

案例展示

Dashboard

dashboard

Thread

一目了然的了解系统的状态,哪些线程比较占cpu?他们到底在做什么?

  1.  
    $ thread -n 3
  2.  
    "as-command-execute-daemon" Id=29 cpuUsage=75% RUNNABLE
  3.  
    at sun.management.ThreadImpl.dumpThreads0(Native Method)
  4.  
    at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:440)
  5.  
    at com.taobao.arthas.core.command.monitor200.ThreadCommand$1.action(ThreadCommand.java:58)
  6.  
    at com.taobao.arthas.core.command.handler.AbstractCommandHandler.execute(AbstractCommandHandler.java:238)
  7.  
    at com.taobao.arthas.core.command.handler.DefaultCommandHandler.handleCommand(DefaultCommandHandler.java:67)
  8.  
    at com.taobao.arthas.core.server.ArthasServer$4.run(ArthasServer.java:276)
  9.  
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  10.  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  11.  
    at java.lang.Thread.run(Thread.java:745)
  12.  
     
  13.  
    Number of locked synchronizers = 1
  14.  
    - java.util.concurrent.ThreadPoolExecutor$Worker@6cd0b6f8
  15.  
     
  16.  
    "as-session-expire-daemon" Id=25 cpuUsage=24% TIMED_WAITING
  17.  
    at java.lang.Thread.sleep(Native Method)
  18.  
    at com.taobao.arthas.core.server.DefaultSessionManager$2.run(DefaultSessionManager.java:85)
  19.  
     
  20.  
    "Reference Handler" Id=2 cpuUsage=0% WAITING on java.lang.ref.Reference$Lock@69ba0f27
  21.  
    at java.lang.Object.wait(Native Method)
  22.  
    - waiting on java.lang.ref.Reference$Lock@69ba0f27
  23.  
    at java.lang.Object.wait(Object.java:503)
  24.  
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)

jad

对类进行反编译:

$ jad javax.servlet.Servlet

ClassLoader:
+-java.net.URLClassLoader@6108b2d7
  +-sun.misc.Launcher$AppClassLoader@18b4aac2
    +-sun.misc.Launcher$ExtClassLoader@1ddf84b8

Location:
/Users/xxx/work/test/lib/servlet-api.jar

/*
 * Decompiled with CFR 0_122.
 */
package javax.servlet;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public interface Servlet {
    public void init(ServletConfig var1) throws ServletException;

    public ServletConfig getServletConfig();

    public void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    public String getServletInfo();

    public void destroy();
}

sc

查找JVM中已经加载的类

  1.  
    $ sc -d org.springframework.web.context.support.XmlWebApplicationContext
  2.  
    class-info org.springframework.web.context.support.XmlWebApplicationContext
  3.  
    code-source /Users/xxx/work/test/WEB-INF/lib/spring-web-3.2.11.RELEASE.jar
  4.  
    name org.springframework.web.context.support.XmlWebApplicationContext
  5.  
    isInterface false
  6.  
    isAnnotation false
  7.  
    isEnum false
  8.  
    isAnonymousClass false
  9.  
    isArray false
  10.  
    isLocalClass false
  11.  
    isMemberClass false
  12.  
    isPrimitive false
  13.  
    isSynthetic false
  14.  
    simple-name XmlWebApplicationContext
  15.  
    modifier public
  16.  
    annotation
  17.  
    interfaces
  18.  
    super-class +-org.springframework.web.context.support.AbstractRefreshableWebApplicationContext
  19.  
    +-org.springframework.context.support.AbstractRefreshableConfigApplicationContext
  20.  
    +-org.springframework.context.support.AbstractRefreshableApplicationContext
  21.  
    +-org.springframework.context.support.AbstractApplicationContext
  22.  
    +-org.springframework.core.io.DefaultResourceLoader
  23.  
    +-java.lang.Object
  24.  
    class-loader +-org.apache.catalina.loader.ParallelWebappClassLoader
  25.  
    +-java.net.URLClassLoader@6108b2d7
  26.  
    +-sun.misc.Launcher$AppClassLoader@18b4aac2
  27.  
    +-sun.misc.Launcher$ExtClassLoader@1ddf84b8
  28.  
    classLoaderHash 25131501
  29.  
     

stack

查看方法 test.arthas.TestStack#doGet 的调用堆栈:

  1.  
    $ stack test.arthas.TestStack doGet
  2.  
    Press Ctrl+C to abort.
  3.  
    Affect(class-cnt:1 , method-cnt:1) cost in 286 ms.
  4.  
    ts=2018-09-18 10:11:45;thread_name=http-bio-8080-exec-10;id=d9;is_daemon=true;priority=5;TCCL=org.apache.catalina.loader.ParallelWebappClassLoader@25131501
  5.  
    @test.arthas.TestStack.doGet()
  6.  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
  7.  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
  8.  
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  9.  
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  10.  
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  11.  
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  12.  
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  13.  
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  14.  
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  15.  
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
  16.  
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
  17.  
    ...
  18.  
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
  19.  
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
  20.  
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
  21.  
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:451)
  22.  
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1121)
  23.  
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
  24.  
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
  25.  
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  26.  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  27.  
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  28.  
    at java.lang.Thread.run(Thread.java:745)

Trace

观察方法执行的时候那个子调用比较慢:

trace

Watch

观察方法 test.arthas.TestWatch#doGet 执行的入参,仅当方法抛出异常时才输出。

  1.  
    $ watch test.arthas.TestWatch doGet {params[0], throwExp} -e
  2.  
    Press Ctrl+C to abort.
  3.  
    Affect(class-cnt:1 , method-cnt:1) cost in 65 ms.
  4.  
    ts=2018-09-18 10:26:28;result=@ArrayList[
  5.  
    @RequestFacade[org.apache.catalina.connector.RequestFacade@79f922b2],
  6.  
    @NullPointerException[java.lang.NullPointerException],
  7.  
    ]

Classloader

了解当前系统中有多少类加载器,以及每个加载器加载的类数量,帮助您判断是否有类加载器泄露。

  1.  
    $ classloader
  2.  
    name numberOfInstances loadedCountTotal
  3.  
    BootstrapClassLoader 1 3346
  4.  
    com.taobao.arthas.agent.ArthasClassloader 1 1262
  5.  
    java.net.URLClassLoader 2 1033
  6.  
    org.apache.catalina.loader.ParallelWebappClassLoader 1 628
  7.  
    sun.reflect.DelegatingClassLoader 166 166
  8.  
    sun.misc.Launcher$AppClassLoader 1 31
  9.  
    com.alibaba.fastjson.util.ASMClassLoader 6 15
  10.  
    sun.misc.Launcher$ExtClassLoader 1 7
  11.  
    org.jvnet.hk2.internal.DelegatingClassLoader 2 2
  12.  
    sun.reflect.misc.MethodUtil 1 1

Web Console

web console

posted on 2019-11-26 14:04  四海骄阳  阅读(1533)  评论(0编辑  收藏  举报

导航