下载arthas
https://arthas.aliyun.com/
#下载包 curl -O https://arthas.aliyun.com/arthas-boot.jar挂载目录
#添加ksb-recycle挂载本地/opt/bin目录 vim docker-compose-app.yml重启ksb-recycle服务
#重启ksb-recycle服务 docker-compose -f docker-compose-app.yml restart ksb-recycle #进入ksb-recycle容器内 docker-compose -f docker-compose-app.yml exec ksb-recycle /bin/bash查看到本地的arthas包和脚本已经挂载到容器内
show-busy-java-threads脚本定位Java进程中高CPU占用线程并打印其堆栈信息的工具
# 1. 基础用法:显示所有Java进程中Top 5高CPU线程的堆栈 ./show-busy-java-threads # 2. 指定Java PID:只排查PID为12345的Java进程 ./show-busy-java-threads -p 12345 # 3. 显示Top 10高CPU线程,持续监控(每2秒刷新,共5次) ./show-busy-java-threads -c 10 2 5 # 4. 进程卡死时,强制抓取堆栈(加-F参数) ./show-busy-java-threads -p 12345 -F # 5. 保存结果到日志文件,方便后续分析 ./show-busy-java-threads -a /tmp/java_cpu_high.logtcp-connection-state-counter脚本统计TCP连接各状态的数量并排序输出
./tcp-connection-state-counter ESTABLISHED 128 # 已建立的活跃连接(数量最多) TIME_WAIT 45 # 等待关闭的连接 CLOSE_WAIT 12 # 被动关闭后未释放的连接 LISTEN 8 # 监听中的端口 SYN_RECV 3 # 收到SYN包等待建立连接 FIN_WAIT1 1 # 主动关闭等待对方确认启动arthas
#进入ksb-recycle容器内 docker-compose -f docker-compose-app.yml exec ksb-recycle /bin/bash #启动arthas-boot.jar,输入1诊断PID为1的java进程 java -jar /opt/bin/arthas-boot.jar常用命令
profiler | 命令作用 |
profiler start | 启动profiler,默认情况下,生成cpu的火焰图 |
profiler list | 显示所有支持的事件 |
profiler getSamples | 获取已采集的sample的数量 |
profiler status | 查看profiler的状态,运行的时间 |
profiler stop | 停止profiler,生成火焰图的结果,指定输出目录和输出格式:svg或html |
profiler stop --file /pathname --format html | 停止时指定输出火焰图的路径和文件格式 |
profiler start --event cpu --interval 10 | 启动CPU采样 --event 指定事件--interval 代表10 毫秒采样一次 数值越小,采样越密集、火焰图越精准,会稍微增加服务器 CPU 开销 数值越大,开销越小,适合长时间采样(比如采样 5 分钟以上可以设为 20) |
profiler start --event lock --interval 10 | --event lock指定采样事件为锁竞争,这是生成锁火焰图的关键参数 |
profiler stop --file /opt/arthas-output/cpu-profiler.html --format html | 停止采样生成CPU火焰图 |
profiler stop --file /opt/arthas-output/profiler-lock-10-100-13-23.html --format html | 停止采样生成HTML格式锁火焰图 |
生成后到宿主机挂载目录执行sz命令下载查看
第二种方式生成
登录Dozzle终端生成查看(更新了Dozzle镜像)
http://192.168.1.220:9999/
java -jar /opt/bin/arthas-boot.jar profiler startprofiler stop --file /opt/arthas-output/cpu-profiler.html --format html | 停止采样生成CPU火焰图 |
profiler stop --file /opt/arthas-output/profiler-lock-10-100-13-23.html --format html | 停止采样生成HTML格式锁火焰图 |
使用命令生成后打开链接下载所生成的
http://192.168.1.219:8188/
火焰图查看
火焰图是基于 perf 结果产生的图片用来展示CPU的调用栈。
纵轴表示调用栈每一层都是一个函数。调用栈越深,火焰就越高,顶部就是正在执行的函数,下方都是它的父函数。
横轴表示抽样数,如果一个函数在横轴占据的宽度越宽,就表示它被抽到的次数多,即执行的时间长。注意横轴不代表时间
,而是所有的调用栈合并后,按字母顺序排列的。
火焰图就是看顶层的哪个函数占据的宽度最大。只要有"平顶"(plateaus),就表示该函数可能存在性能问题。
颜色没有特殊含义,因为火焰图表示的是 CPU 的繁忙程度,所以一般选择暖色调
dashboard命令
NAME 线程名称
STATE 线程状态
1、RUNNABLE:正在运行(CPU高的线程通常是这个状态)
2、TIMED_WAITING:超时等待
3、WAITING:无限等待
%CPU 线程占用的 CPU 百分比(核心指标)
TIME 线程累计运行时间能看出线程的 “总工作量”
DAEMON 是否是守护线程(true表示是,JVM 退出时会自动关闭这类线程)
heap 堆内存Eden 是新对象区、Survivor 是存活对象区、old 是老年代ps_eden_space ps_survivor_space ps_old_gen
nonheap 存放类信息、常量池核心看 GC 次数和耗时这部分是 JVM 垃圾回收的统计,GC 频繁会导致性能下降
关键指标
线程区域:找 %CPU高的线程,看它的STAT是不是 RUNNABLE(如果是,说明这个线程在 疯狂干活,可能是性能瓶颈)
内存区域:看heap的 usage(超过 80% 警惕内存泄漏)、ps_old_gen 的使用率(持续涨要查老年代对象)
GC 区域:看 gc.ps_marksweep.count(Full GC 次数多不多)、gc.ps_scavenge.time(Minor GC 耗时久不久)
更多命令:https://arthas.aliyun.com/doc/commands.html