Skip to content

Commit 1c1713c

Browse files
authoredSep 27, 2023
profiler command add action collect (alibaba#2685)
1 parent 82195d3 commit 1c1713c

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed
 

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

+42-7
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ public class ProfilerCommand extends AnnotatedCommand {
219219
*/
220220
private String chunktime;
221221

222+
/**
223+
* run profiler in a loop (continuous profiling)
224+
*/
225+
private String loop;
226+
227+
/**
228+
* automatically stop profiler at TIME (absolute or relative)
229+
*/
230+
private String timeout;
231+
222232
private static String libPath;
223233
private static AsyncProfiler profiler = null;
224234

@@ -458,6 +468,25 @@ public void setChunktime(String chunktime) {
458468
this.chunktime = chunktime;
459469
}
460470

471+
@Option(longName = "loop")
472+
@Description("run profiler in a loop (continuous profiling)")
473+
public void setLoop(String loop) {
474+
this.loop = loop;
475+
if (this.action.equals("collect")) {
476+
this.action = "start";
477+
}
478+
}
479+
480+
@Option(longName = "timeout")
481+
@Description("automatically stop profiler at TIME (absolute or relative)")
482+
public void setTimeout(String timeout) {
483+
this.timeout = timeout;
484+
if (this.action.equals("collect")) {
485+
this.action = "start";
486+
}
487+
}
488+
489+
461490
private AsyncProfiler profilerInstance() {
462491
if (profiler != null) {
463492
return profiler;
@@ -504,7 +533,7 @@ private AsyncProfiler profilerInstance() {
504533
*/
505534
public enum ProfilerAction {
506535
// start, resume, stop, dump, check, status, meminfo, list, collect,
507-
start, resume, stop, dump, check, status, meminfo, list,
536+
start, resume, stop, dump, check, status, meminfo, list, collect,
508537
version,
509538

510539
load,
@@ -613,6 +642,12 @@ private String executeArgs(ProfilerAction action) {
613642
if (this.chunktime!= null) {
614643
sb.append("chunktime=").append(this.chunktime).append(COMMA);
615644
}
645+
if (this.loop != null) {
646+
sb.append("loop=").append(this.loop).append(COMMA);
647+
}
648+
if (this.timeout != null) {
649+
sb.append("timeout=").append(this.timeout).append(COMMA);
650+
}
616651

617652
return sb.toString();
618653
}
@@ -646,12 +681,8 @@ public void process(final CommandProcess process) {
646681
}
647682
String result = execute(asyncProfiler, this.actionArg);
648683
appendExecuteResult(process, result);
649-
} else if (ProfilerAction.start.equals(profilerAction)) {
650-
//jfr录制,必须在start的时候就指定文件路径
651-
if (this.file == null && "jfr".equals(format)) {
652-
this.file = outputFile();
653-
}
654-
String executeArgs = executeArgs(ProfilerAction.start);
684+
} else if (ProfilerAction.collect.equals(profilerAction)) {
685+
String executeArgs = executeArgs(ProfilerAction.collect);
655686
String result = execute(asyncProfiler, executeArgs);
656687
ProfilerModel profilerModel = createProfilerModel(result);
657688

@@ -677,6 +708,10 @@ public void run() {
677708
}, this.duration, TimeUnit.SECONDS);
678709
}
679710
process.appendResult(profilerModel);
711+
} else if (ProfilerAction.start.equals(profilerAction)) {
712+
String executeArgs = executeArgs(ProfilerAction.start);
713+
String result = execute(asyncProfiler, executeArgs);
714+
appendExecuteResult(process, result);
680715
} else if (ProfilerAction.stop.equals(profilerAction)) {
681716
ProfilerModel profilerModel = processStop(asyncProfiler, profilerAction);
682717
process.appendResult(profilerModel);

‎site/docs/doc/profiler.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ profiler stop --include 'java/*' --include 'com/demo/*' --exclude '*Unsafe.park*
226226
227227
## 指定执行时间
228228

229-
比如,希望 profiler 执行 300 秒自动结束,可以用 `-d`/`--duration` 参数指定
229+
比如,希望 profiler 执行 300 秒自动结束,可以用 `-d`/`--duration` 参数为 collect action 指定时间
230230

231231
```bash
232-
profiler start --duration 300
232+
profiler collect --duration 300
233233
```
234234

235235
## 生成 jfr 格式结果
@@ -338,3 +338,14 @@ profiler --ttsp
338338
```bash
339339
profiler start -e cpu --jfrsync profile -f combined.jfr
340340
```
341+
## 周期性保存结果
342+
343+
使用 `--loop TIME` 可以持续运行 profiler 并周期性保存结果。选项格式可以是具体时间 hh:mm:ss 或以秒、分钟、小时或天计算的时间间隔。需要确保指定的输出文件名中包含时间戳,否则每次输出的结果都会覆盖上次保存的结果。以下命令持续执行 profiling 并将每个小时内的记录保存到一个 jfr 文件中。
344+
345+
```bash
346+
profiler start --loop 1h -f /var/log/profile-%t.jfr
347+
```
348+
349+
## `--timeout` 选项
350+
351+
这个选项指定 profiling 自动在多久后停止。该选项和 `--loop` 选项的格式一致,可以是时间点,也可以是一个时间间隔。这两个选项都是用于 `start` action 而不是 `collect` action 的。可参考 [async-profiler Github Discussions](https://github.com/async-profiler/async-profiler/discussions/789) 了解更多信息。

‎site/docs/en/doc/profiler.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ profiler stop --include'java/*' --include 'com/demo/*' --exclude'*Unsafe.park*'
226226
227227
## Specify execution time
228228

229-
For example, if you want the profiler to automatically end after 300 seconds, you can specify it with the `-d`/`--duration` parameter:
229+
For example, if you want the profiler to automatically end after 300 seconds, you can specify it with the `-d`/`--duration` parameter in collect action:
230230

231231
```bash
232-
profiler start --duration 300
232+
profiler collect --duration 300
233233
```
234234

235235
## Generate jfr format result
@@ -338,3 +338,16 @@ For example, command below use "profile" config of JFR:
338338
```bash
339339
profiler start -e cpu --jfrsync profile -f combined.jfr
340340
```
341+
## Run profiler in a loop
342+
343+
Use `--loop TIME` to run profiler in a loop (continuous profiling). The argument is either a clock time (hh:mm:ss) or a loop duration in seconds, minutes, hours, or days. Make sure the filename includes a timestamp pattern, or the output will be overwritten on each iteration. The command below will run profiling endlessly and save records of each hour to a jfr file.
344+
345+
```bash
346+
profiler start --loop 1h -f /var/log/profile-%t.jfr
347+
```
348+
349+
## `--timeout` option
350+
351+
This option specifies the time when profiling will automatically stop. The format is the same as in loop: it is either a wall clock time (12:34:56) or a relative time interval (2h).
352+
353+
Both `--loop` and `--timeout` are used for `start` action but not for `collect` action, for further information refer to [async-profiler Github Discussions](https://github.com/async-profiler/async-profiler/discussions/789).

0 commit comments

Comments
 (0)
Please sign in to comment.