|
51 | 51 | + " profiler list # list all supported events\n" |
52 | 52 | + " profiler actions # list all supported actions\n" |
53 | 53 | + " profiler start --event alloc\n" |
| 54 | + + " profiler start --timeout 300s" |
| 55 | + + " profiler start --loop 300s -f /tmp/result-%t.html" |
| 56 | + + " profiler start --duration 300" |
54 | 57 | + " profiler stop --format html # output file format, support flat[=N]|traces[=N]|collapsed|flamegraph|tree|jfr\n" |
55 | 58 | + " profiler stop --file /tmp/result.html\n" |
56 | 59 | + " profiler stop --threads \n" |
|
66 | 69 | //@formatter:on |
67 | 70 | public class ProfilerCommand extends AnnotatedCommand { |
68 | 71 | private static final Logger logger = LoggerFactory.getLogger(ProfilerCommand.class); |
| 72 | + // Track if a file was specified during profiler start |
| 73 | + private static String fileSpecifiedAtStart = null; |
69 | 74 |
|
| 75 | + // TODO start 时,没指定 file, 是否在 stop 时,能生成 html 或者 jfr 不? |
70 | 76 | private String action; |
71 | 77 | private String actionArg; |
72 | 78 |
|
@@ -743,10 +749,36 @@ public void process(final CommandProcess process) { |
743 | 749 | String result = execute(asyncProfiler, this.actionArg); |
744 | 750 | appendExecuteResult(process, result); |
745 | 751 | } else if (ProfilerAction.start.equals(profilerAction)) { |
| 752 | + // Track if file parameter was specified during start |
| 753 | + boolean autoGeneratedFile = false; |
| 754 | + if (this.file != null) { |
| 755 | + fileSpecifiedAtStart = this.file; |
| 756 | + logger.debug("File specified during profiler start: {}", fileSpecifiedAtStart); |
| 757 | + } else if (this.timeout != null) { |
| 758 | + // Auto-generate file if timeout is specified but file is not |
| 759 | + try { |
| 760 | + this.file = outputFile(); |
| 761 | + logger.debug("Auto-generated file for timeout: {}", this.file); |
| 762 | + fileSpecifiedAtStart = this.file; |
| 763 | + autoGeneratedFile = true; |
| 764 | + } catch (IOException e) { |
| 765 | + logger.warn("Failed to auto-generate file for timeout", e); |
| 766 | + } |
| 767 | + } |
| 768 | + |
746 | 769 | if (this.duration == null) { |
747 | 770 | String executeArgs = executeArgs(ProfilerAction.start); |
748 | 771 | String result = execute(asyncProfiler, executeArgs); |
749 | | - appendExecuteResult(process, result); |
| 772 | + ProfilerModel profilerModel = createProfilerModel(result); |
| 773 | + |
| 774 | + // Add information about auto-generated file for timeout |
| 775 | + if (autoGeneratedFile && this.file != null) { |
| 776 | + profilerModel.setOutputFile(this.file); |
| 777 | + profilerModel.setExecuteResult(profilerModel.getExecuteResult() |
| 778 | + + "\nAuto-generated output file will be: " + this.file + "\n"); |
| 779 | + } |
| 780 | + |
| 781 | + process.appendResult(profilerModel); |
750 | 782 | } else { // 设置延时执行 stop |
751 | 783 | final String outputFile = outputFile(); |
752 | 784 | String executeArgs = executeArgs(ProfilerAction.start); |
@@ -833,12 +865,27 @@ public void run() { |
833 | 865 | } |
834 | 866 |
|
835 | 867 | private ProfilerModel processStop(AsyncProfiler asyncProfiler, ProfilerAction profilerAction) throws IOException { |
836 | | - String outputFile = outputFile(); |
| 868 | + String outputFile = null; |
| 869 | + |
| 870 | + // If we're stopping and a file was specified during start, don't generate a new |
| 871 | + // output file |
| 872 | + if (profilerAction == ProfilerAction.stop && fileSpecifiedAtStart != null) { |
| 873 | + outputFile = fileSpecifiedAtStart; |
| 874 | + // Reset the tracking variable after stop |
| 875 | + logger.debug("Using file specified during start: {}", fileSpecifiedAtStart); |
| 876 | + fileSpecifiedAtStart = null; |
| 877 | + } else { |
| 878 | + // Otherwise generate or use the specified output file |
| 879 | + outputFile = outputFile(); |
| 880 | + } |
| 881 | + |
837 | 882 | String executeArgs = executeArgs(profilerAction); |
838 | 883 | String result = execute(asyncProfiler, executeArgs); |
839 | 884 |
|
840 | 885 | ProfilerModel profilerModel = createProfilerModel(result); |
841 | | - profilerModel.setOutputFile(outputFile); |
| 886 | + if (outputFile != null) { |
| 887 | + profilerModel.setOutputFile(outputFile); |
| 888 | + } |
842 | 889 | return profilerModel; |
843 | 890 | } |
844 | 891 |
|
|
0 commit comments