profiler command support duration option. close #1197

This commit is contained in:
hengyunabc
2020-05-27 00:04:41 +08:00
parent abeaddea56
commit 1dd7f6c9bd
@@ -10,11 +10,13 @@ import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.taobao.arthas.common.OSUtils;
import com.taobao.arthas.core.command.Constants;
import com.taobao.arthas.core.server.ArthasBootstrap;
import com.taobao.arthas.core.shell.cli.CliToken;
import com.taobao.arthas.core.shell.cli.Completion;
import com.taobao.arthas.core.shell.cli.CompletionUtils;
@@ -91,6 +93,11 @@ public class ProfilerCommand extends AnnotatedCommand {
*/
private boolean alluser;
/**
* run profiling for <duration> seconds
*/
private Long duration;
private static String libPath;
private static AsyncProfiler profiler = null;
@@ -180,6 +187,12 @@ public class ProfilerCommand extends AnnotatedCommand {
this.alluser = alluser;
}
@Option(shortName = "d", longName = "duration")
@Description("run profiling for <duration> seconds")
public void setDuration(long duration) {
this.duration = duration;
}
private AsyncProfiler profilerInstance() {
if (profiler != null) {
return profiler;
@@ -256,7 +269,7 @@ public class ProfilerCommand extends AnnotatedCommand {
}
@Override
public void process(CommandProcess process) {
public void process(final CommandProcess process) {
int status = 0;
try {
ProfilerAction profilerAction = ProfilerAction.valueOf(action);
@@ -266,7 +279,7 @@ public class ProfilerCommand extends AnnotatedCommand {
return;
}
AsyncProfiler asyncProfiler = this.profilerInstance();
final AsyncProfiler asyncProfiler = this.profilerInstance();
if (ProfilerAction.execute.equals(profilerAction)) {
if (actionArg == null) {
@@ -280,13 +293,28 @@ public class ProfilerCommand extends AnnotatedCommand {
String executeArgs = executeArgs(ProfilerAction.start);
String result = execute(asyncProfiler, executeArgs);
process.write(result);
} else if (ProfilerAction.stop.equals(profilerAction)) {
if (this.file == null) {
this.file = new File("arthas-output",
new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + "." + this.format)
.getAbsolutePath();
if (this.duration != null) {
final String outputFile = outputFile();
final String stopExecuteArgs = executeArgs(ProfilerAction.stop);
process.write(String.format("profiler will silent stop after %d seconds.\n", this.duration.longValue()));
process.write("profiler output file will be: " + new File(outputFile).getAbsolutePath() + "\n");
ArthasBootstrap.getInstance().getScheduledExecutorService().schedule(new Runnable() {
@Override
public void run() {
try {
logger.info("profiler output file: " + new File(outputFile).getAbsolutePath() + "\n");
String result = execute(asyncProfiler, stopExecuteArgs);
logger.info("profiler stop result: " + result);
} catch (Throwable e) {
logger.error("", e);
}
}
}, this.duration, TimeUnit.SECONDS);
}
process.write("profiler output file: " + new File(this.file).getAbsolutePath() + "\n");
} else if (ProfilerAction.stop.equals(profilerAction)) {
String outputFile = outputFile();
process.write("profiler output file: " + new File(outputFile).getAbsolutePath() + "\n");
String executeArgs = executeArgs(ProfilerAction.stop);
String result = execute(asyncProfiler, executeArgs);
process.write(result);
@@ -342,6 +370,14 @@ public class ProfilerCommand extends AnnotatedCommand {
}
}
private String outputFile() {
if (this.file == null) {
this.file = new File("arthas-output",
new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + "." + this.format).getAbsolutePath();
}
return file;
}
private List<String> events() {
List<String> result = new ArrayList<String>();