profiler¶
使用async-profiler生成火焰图
profiler 命令支持生成应用热点的火焰图。本质上是通过不断的采样,然后把收集到的采样结果生成火焰图。
profiler 命令基本运行结构是 profiler action [actionArg]
获取已采集的sample的数量¶
$ profiler getSamples
23
查看profiler状态¶
$ profiler status
[cpu] profiling is running for 4 seconds可以查看当前profiler在采样哪种event和采样时间。
停止profiler¶
生成svg格式结果¶
$ profiler stop
profiler output file: /tmp/demo/arthas-output/20191125-135546.svg
OK
默认情况下,生成的结果保存到应用的工作目录下的arthas-output目录。可以通过 --file参数来指定输出结果路径。比如:
$ profiler stop --file /tmp/output.svg
profiler output file: /tmp/output.svg
OK生成html格式结果¶
默认情况下,结果文件是svg格式,如果想生成html格式,可以用--format参数指定:
$ profiler stop --format html
profiler output file: /tmp/test/arthas-output/20191125-143329.html
OK或者在--file参数里用文件名指名格式。比如--file /tmp/result.html 。
通过浏览器查看arthas-output下面的profiler结果¶
默认情况下,arthas使用3658端口,则可以打开: http://localhost:3658/arthas-output/ 查看到arthas-output目录下面的profiler结果:

点击可以查看具体的结果:

如果是chrome浏览器,可能需要多次刷新。
profiler支持的events¶
在不同的平台,不同的OS下面,支持的events各有不同。比如在macos下面:
$ profiler list
Basic events:
cpu
alloc
lock
wall
itimer在linux下面
$ profiler list
Basic events:
cpu
alloc
lock
wall
itimer
Perf events:
page-faults
context-switches
cycles
instructions
cache-references
cache-misses
branches
branch-misses
bus-cycles
L1-dcache-load-misses
LLC-load-misses
dTLB-load-misses
mem:breakpoint
trace:tracepoint如果遇到OS本身的权限/配置问题,然后缺少部分event,可以参考async-profiler本身文档:async-profiler
可以用--event参数指定要采样的事件,比如对alloc事件进入采样:
$ profiler start --event alloc恢复采样¶
$ profiler resume
Started [cpu] profilingstart和resume的区别是:start是新开始采样,resume会保留上次stop时的数据。
通过执行profiler getSamples可以查看samples的数量来验证。
使用execute来执行复杂的命令¶
比如开始采样:
profiler execute 'start'停止采样,并保存到指定文件里:
profiler execute 'stop,file=/tmp/result.svg'具体的格式参考: arguments.cpp#L34
查看所有支持的action¶
$ profiler actions
Supported Actions: [resume, dumpCollapsed, getSamples, start, list, execute, version, stop, load, dumpFlat, actions, dumpTraces, status]查看版本¶
$ profiler version
Async-profiler 1.6 built on Sep 9 2019
Copyright 2019 Andrei Pangin