简介 async-profiler是一款采集分析java性能的工具,翻译一下github上的项目介绍: ❝async-profiler是一款没有Safepoint bias problem的低开销java ❞ 使用方法 首先下载async-profiler,github主页(https://github.com/jvm-profiling-tools/async-profiler)上有已经编译好的文件, github主页 一次网关性能压测实例 笔者有一次在压测全链路异步网关时,RPS在2000左右,始终上不去,cpu消耗比较高,于是使用async-profiler进行cpu剖析,生成的火焰图如下 ? 原理介绍 看到这里相信你应该会用async-profiler来进行cpu剖析了,如果感兴趣可以了解下async-profiler实现的原理,这块有一篇文章介绍的很详细,可以参考 ❝《JVM CPU Profiler 而且阿里开源的Arthas中的cpu分析也是使用的async-profiler。所以你不试试吗?
项目标题与描述Async-profiler 是一个针对Java的低开销采样性能分析器,它克服了传统分析器的“安全点偏差”(Safepoint bias)问题。 导航到async-profiler源码根目录。运行 make 命令。构建完成后,启动器 asprof 将位于 build/bin/asprof 目录下。 /* * Copyright The async-profiler authors * SPDX-License-Identifier: Apache-2.0 */#include "cpuEngine.h /* * Copyright The async-profiler authors * SPDX-License-Identifier: Apache-2.0 */#include "allocTracer.h /* * Copyright The async-profiler authors * SPDX-License-Identifier: Apache-2.0 */#include "callTraceStorage.h
另一方面,async-profiler 使用非官方的 AsyncGetCallTrace API。 Async-Profiler Async-profiler 是最常用的分析器之一,这不仅仅是因为它被嵌入到了许多其他工具中,如 IntelliJ Ultimate Profiler 和 AppIication 以下是 async-profiler 的发展简史,感兴趣的可以了解一下。 要了解关于 async-profiler 的更多信息,可以查看 async-profiler 自述文件、async-profiler 维基以及 Krzysztof Ślusarski 提供的 async-profiler 它的工作原理与 async-profiler 类似,主要区别是它直接使用内部的 JVM API。
@toc二、命令列表2.4 profiler(使用async-profiler对应用采样,生成火焰图)使用场景:profiler 命令支持生成应用热点的火焰图。 提示使用async-profiler在新窗口打开生成火焰图火焰图的含义:火焰图是基于 perf 结果产生的SVG 图片,用来展示 CPU 的调用栈。y 轴表示调用栈,每一层都是一个函数。 profiler 命令基本运行结构是: profiler action [actionArg]profiler 命令的格式基本与上游项目 async-profiler在新窗口打开 保持一致,详细的使用方式可参考上游项目的 event-descriptor/ mem:breakpoint trace:tracepoint kprobe:func uprobe:path如果遇到 OS 本身的权限/配置问题,然后缺少部分 event,可以参考 async-profiler tt(方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测)26.Arthas watch (方法执行数据观测)27.Arthas profiler(使用async-profiler
这篇文章介绍 async-profiler 相关内容。 使用 async-profiler 可以做下面几个方面的分析。 其他信息可以看官方文档:https://github.com/jvm-profiling-tools/async-profiler async-profiler 安装 下载 async-profiler Resolving deltas: 100% (1288/1288), done. ➜ develop cd async-profiler ➜ async-profiler git:(master) git:(master) async-profiler 使用 运行项目里的 profiler.sh 可以看到 async-profiler 的使用帮助文档。
另一方面,Async-profiler 使用非官方的 AsyncGetCallTrace API。 您可以从其项目的 GitHub 页面下载 async-profiler 。它在 Windows 上不受支持,由特定于平台的二进制文件组成。 您可以通过使用嵌入它的许多工具或直接将其用作本机 Java 代理来使用 async-profiler。 async-profiler 的开发始于 2016 年;它是目前使用 AsyncGetCallTrace 的主要开源分析器。 async-profiler 的问题在于它基于非官方的内部 API。 它的工作方式与 async-profiler 相当,主要区别在于它直接使用内部 JVM API。
async-profiler 这里我们介绍一个更简单易用的工具 async-profiler。 async-profiler 不使用侵入性的技术,例如字节码检测工具或者探针检测等,这也说明 async-profiler 的内存分配分析像 CPU 性能分析一样,不会产生太大的性能开销,同时也不用写出庞大的堆栈文件再去进行进一步处理 Linux / x64 / x86 / ARM / AArch64 macOS / x64 async-profiler 可以跟踪以下类型的事件: CPU 周期 硬件和软件性能计数器,如缓存未命中、分支未命中 Java 堆中的内存分配 锁尝试,包括 Java 对象监视器和 ReentrantLocks 使用方式 下载地址: https://github.com/jvm-profiling-tools/async-profiler async-profiler应该由特权用户从主机运行 - 它将自动切换到正确的pid/装载命名空间,并更改用户凭据以匹配目标进程。
首先跟大多数监控系统一样,数据采集+前端绘图,这个图也是根据某些数据绘制而成的,绘图工具本篇文章采用FlameGraph,而负责收集这些数据的工具,这里采用async-profiler,这个工具会在程序运行期间向 jvm发送信号采集其运行期数据(简单来说就是通过该工具可以找出程序中占用CPU资源时间最长的代码块,这里async-profiler的实现使用了jvmti,然后生成相应的数据格式文件,而FlameGraph 从GitHub上clone git clone https://github.com/jvm-profiling-tools/async-profiler 这里使用mac演示 可以新建一个工程,编写Target.java
https://github.com/alibaba/arthas async-profiler Java 应用性能分析工具,开源、火焰图、跨平台。 https://github.com/jvm-profiling-tools/async-profiler 关于第三方工具的使用,后面会有文章对每一个工具进行详细的演示和说明。
故而找到一个async-profiler分析工具作为替代品。 当时正在测试随机数性能的,所以就用了一个动态QPS模型的Case,学习了async-profiler的使用。 async-profiler 这个工具安装和使用教程,可以网上搜一下,建议去Github仓库看看Wiki,这里我就不多说。 Case code 下面是Case的代码,用了动态QPS模型。
jvisualvm 第三方:MAT(eclipse插件)、GCHisto、GCeasy(在线GC日志分析工具,https://gceasy.io/) 开源:大名鼎鼎的Arthas、bistoury(去哪网开源)、Async-profiler [img4.png] 但我用另一个 jvm-profiling-tools/async-profiler 来采集,就能看到性能瓶颈: [img5.png] 虽然 Async-profiler 也是基于采样做 于是依据 Async-profiler 给出的火焰图进行优化,Qps 从 58k 涨到 81k,Cpu 反而从72%下降到了41% [img6.png] 数据 数据包括: 监控数据,如APM、metric
在Java中,我们用Junit做单元测试,用JMH做性能基准测试(benchmark),用async-profiler剖析cpu性能,用jstack、jmap、arthas等来排查问题。 pprof pprof是golang自带的可以用来做cpu、内存、锁分析的工具,非常类似java的async-profiler。
使用async-profiler,该项目将perf的堆栈追踪和JDK提供的AsyncGetCallTrace结合了起来,同样能够获得mixed-mode火焰图。 /flamegraph.pl --color=io --countname=ms < out.offcpu01.txt > out.offcpu01.svg async-profiler async-profiler async-profiler的使用 下载并解压好async-profiler安装包。 完整的火焰图,解决perf和bcc profile不能识别Java符号和Java stack traces的问题,目前有以下两种方式: perf-map-agent 加上 perf或bcc profile async-profiler (内部会使用到perf) 如果只是对Java进程做on-CPU分析,async-profiler更加方便好用。
该方式的典型开源实现有Async-Profiler和Honest-Profiler,Async-Profiler实现质量较高,感兴趣的话建议大家阅读参考源码。 有趣的是,IntelliJ IDEA内置的Java Profiler,其实就是Async-Profiler的包装。 jattach是Async-Profiler提供的一个Driver,使用方式比较直观: Usage: jattach <pid> <cmd> [args ...] 如下所示的Main函数描述了一次Attach的整体流程: // async-profiler/src/jattach/jattach.c int main(int argc, char** argv) ,check_socket源码如下: // async-profiler/src/jattach/jattach.c // Check if remote JVM has already opened
选择程序进行火焰图的分析 直接使用Async Profiler更简单 async-profiler git地址[1] 安装 从git上直接下载。 解压下可用。 简单使用 执行命令。 . wall:告诉async-profiler在给定的时间内对所有线程平均采样,而不管线程状态如何: 运行、休眠或阻塞。例如,在分析应用程序启动时间时,这可能会有所帮助。。 来源:juejin.cn/post/6936063402640932878 [1] https://github.com/jvm-profiling-tools/async-profiler [2] https
于是做了一些分析,看看 Java 程序启动慢到底慢在哪里,如何去优化,目前的效果是大部分大型应用启动时间可以缩短 30%~50% 主要有下面这些内容 修改 async-profiler 源码,只抓取启动阶段 在生成热点调用火焰图的时候,我们通过 arthas 的几个简单的命令就可以生成,它底层用的是 async-profiler 这个开源项目,它的作者 apangin 做过一系列关于 jvm profiling async-profiler 底层原理简介 async-profiler 是一个非常强大的工具,使用 jvmti 技术来实现。 async-profiler 除了可以生成热点调用的火焰图,它还提供了 Wall-clock profiling 的功能,这个功能其实就是固定时间采样所有的线程(不管线程当前是 Running、Sleeping 就算你找到了 main 线程,在函数耗时算占比的时候也不太方便,我们关心的其实只是 main 线程(也就是加载 jar 包,执行 spring 初始化的线程),于是我做了一些简单的修改,让 async-profiler
Java 的性能分析工具链非常的健全,比如通过 async-profiler 这个工具我可以清楚的知道某个特定函数占用的时间,占程序总时间的百分比是多少。 上面是一个通过 async-profiler 打出的火焰图,通过这个开发者可以清楚的看到哪个函数占用的时间多,进而有针对性地优化。 还是说回 Python,完善的性能分析工具链,有助于工程师发现问题。
tt(方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测)26.Arthas watch (方法执行数据观测)27.Arthas profiler(使用async-profiler
在后台kubectl-flame使用 async-profiler 来为 Java 应用程序生成火焰图,通过共享/tmp文件夹与目标 JVM 进行交互,Golang 则支持基于 ebpf 分析,Python
tt(方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测)26.Arthas watch (方法执行数据观测)27.Arthas profiler(使用async-profiler