Skip to content

Commit 0493d95

Browse files
authored
profiler command support jfrsync option, check action (alibaba#2683)
1 parent 19c3e0e commit 0493d95

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

core/src/main/java/com/taobao/arthas/core/command/monitor200/ProfilerCommand.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public class ProfilerCommand extends AnnotatedCommand {
9292
*/
9393
private String lock;
9494

95+
/**
96+
* start Java Flight Recording with the given config along with the profiler
97+
*/
98+
private String jfrsync;
99+
95100
/**
96101
* output file name for dumping
97102
*/
@@ -318,6 +323,12 @@ public void setLock(String lock) {
318323
this.lock = lock;
319324
}
320325

326+
@Option(longName = "jfrsync")
327+
@Description("start Java Flight Recording with the given config along with the profiler")
328+
public void setJfrsync(String jfrsync) {
329+
this.jfrsync = jfrsync;
330+
}
331+
321332
@Option(shortName = "t", longName = "threads", flag = true)
322333
@Description("profile different threads separately")
323334
public void setThreads(boolean threads) {
@@ -493,7 +504,7 @@ private AsyncProfiler profilerInstance() {
493504
*/
494505
public enum ProfilerAction {
495506
// start, resume, stop, dump, check, status, meminfo, list, collect,
496-
start, resume, stop, dump, status, meminfo, list,
507+
start, resume, stop, dump, check, status, meminfo, list,
497508
version,
498509

499510
load,
@@ -523,6 +534,10 @@ private String executeArgs(ProfilerAction action) {
523534
if (this.lock!= null) {
524535
sb.append("lock=").append(this.lock).append(COMMA);
525536
}
537+
if (this.jfrsync != null) {
538+
this.format = "jfr";
539+
sb.append("jfrsync=").append(this.jfrsync).append(COMMA);
540+
}
526541
if (this.file != null) {
527542
sb.append("file=").append(this.file).append(COMMA);
528543
}
@@ -672,6 +687,10 @@ public void run() {
672687
String executeArgs = executeArgs(ProfilerAction.resume);
673688
String result = execute(asyncProfiler, executeArgs);
674689
appendExecuteResult(process, result);
690+
} else if (ProfilerAction.check.equals(profilerAction)) {
691+
String executeArgs = executeArgs(ProfilerAction.check);
692+
String result = execute(asyncProfiler, executeArgs);
693+
appendExecuteResult(process, result);
675694
} else if (ProfilerAction.version.equals(profilerAction)) {
676695
String result = asyncProfiler.execute("version=full");
677696
appendExecuteResult(process, result);

site/docs/doc/profiler.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Perf events:
138138

139139
如果遇到 OS 本身的权限/配置问题,然后缺少部分 event,可以参考 [async-profiler 的文档](https://github.com/jvm-profiling-tools/async-profiler)
140140

141+
可以使用 `check` action 测试某个 event 是否可用,此 action 的参数格式与 start 一致。
142+
141143
可以用`--event`参数指定要采样的事件,比如 `alloc` 表示分析内存分配情况:
142144

143145
```bash
@@ -178,13 +180,13 @@ profiler execute 'start,framebuf=5000000'
178180
profiler execute 'stop,file=/tmp/result.html'
179181
```
180182

181-
具体的格式参考: [arguments.cpp](https://github.com/jvm-profiling-tools/async-profiler/blob/v2.5/src/arguments.cpp#L50)
183+
具体的格式参考: [arguments.cpp](https://github.com/async-profiler/async-profiler/blob/v2.9/src/arguments.cpp#L52)
182184

183185
## 查看所有支持的 action
184186

185187
```bash
186188
$ profiler actions
187-
Supported Actions: [resume, dumpCollapsed, getSamples, start, list, version, execute, meminfo, stop, load, dumpFlat, dump, actions, dumpTraces, status]
189+
Supported Actions: [resume, dumpCollapsed, getSamples, start, list, version, execute, meminfo, stop, load, dumpFlat, dump, actions, dumpTraces, status, check]
188190
```
189191

190192
## 查看版本
@@ -324,3 +326,15 @@ profiler --cstack fp
324326
profiler start --begin SafepointSynchronize::begin --end RuntimeService::record_safepoint_synchronized
325327
profiler --ttsp
326328
```
329+
330+
## 使用 profiler 记录的 event 生成 JFR 文件
331+
332+
`--jfrsync CONFIG` 选项可以指定配置启动 Java Flight Recording,输出的 jfr 文件会包含所有常规的 JFR event,但采样的来源是由 profiler 提供的。
333+
334+
`CONFIG` 选项可以是 `profile`,表示使用在 `$JAVA_HOME/lib/jfr` 目录下预置的“profile”配置,也可以是自定义的 JFR 配置文件(.jfc),此选项的值采用与 [JFR.start 命令的 settings 选项](https://docs.oracle.com/en/java/javase/17/docs/specs/man/jcmd.html) 相同的格式。
335+
336+
比如,以下命令使用“profile”配置启动 JFR:
337+
338+
```bash
339+
profiler start -e cpu --jfrsync profile -f combined.jfr
340+
```

site/docs/en/doc/profiler.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Perf events:
138138

139139
If you encounter the permissions/configuration issues of the OS itself and then missing some events, you can refer to the [async-profiler](https://github.com/jvm-profiling-tools/async-profiler) documentation.
140140

141+
You can use `check` action to check if a profiling event is available, this action receives the same format options with `start`.
142+
141143
You can use the `--event` parameter to specify the event to sample, for example, `alloc` event means heap memory allocation profiling:
142144

143145
```bash
@@ -178,13 +180,13 @@ Stop sampling and save to the specified file:
178180
profiler execute 'stop,file=/tmp/result.html'
179181
```
180182

181-
Specific format reference: [arguments.cpp](https://github.com/jvm-profiling-tools/async-profiler/blob/v2.5/src/arguments.cpp#L50)
183+
Specific format reference: [arguments.cpp](https://github.com/async-profiler/async-profiler/blob/v2.9/src/arguments.cpp#L52)
182184

183185
## View all supported actions
184186

185187
```bash
186188
$ profiler actions
187-
Supported Actions: [resume, dumpCollapsed, getSamples, start, list, version, execute, meminfo, stop, load, dumpFlat, dump, actions, dumpTraces, status]
189+
Supported Actions: [resume, dumpCollapsed, getSamples, start, list, version, execute, meminfo, stop, load, dumpFlat, dump, actions, dumpTraces, status, check]
188190
```
189191

190192
## View version
@@ -324,3 +326,15 @@ The `--ttsp` option is an alias for `--begin SafepointSynchronize::begin --end R
324326
profiler start --begin SafepointSynchronize::begin --end RuntimeService::record_safepoint_synchronized
325327
profiler --ttsp
326328
```
329+
330+
## Use events from profiler for Java Flight Recording
331+
332+
Use `--jfrsync CONFIG` to start Java Flight Recording with the given configuration synchronously with the profiler. The output .jfr file will include all regular JFR events, except that execution samples will be obtained from async-profiler. This option implies -o jfr.
333+
334+
`CONFIG` can be `profile`, means using the predefined JFR config "profile" in `$JAVA_HOME/lib/jfr/`, or full path of a JFR configuration file (.jfc), this value has the same format with [settings option of JFR.start](https://docs.oracle.com/en/java/javase/17/docs/specs/man/jcmd.html).
335+
336+
For example, command below use "profile" config of JFR:
337+
338+
```bash
339+
profiler start -e cpu --jfrsync profile -f combined.jfr
340+
```

0 commit comments

Comments
 (0)