public void process()

in core/src/main/java/com/taobao/arthas/core/command/monitor200/ProfilerCommand.java [732:865]


    public void process(final CommandProcess process) {
        try {
            ProfilerAction profilerAction = ProfilerAction.valueOf(action);

            if (ProfilerAction.actions.equals(profilerAction)) {
                process.appendResult(new ProfilerModel(actions()));
                process.end();
                return;
            }

            final AsyncProfiler asyncProfiler = this.profilerInstance();

            if (ProfilerAction.execute.equals(profilerAction)) {
                if (actionArg == null) {
                    process.end(1, "actionArg can not be empty.");
                    return;
                }
                String result = execute(asyncProfiler, this.actionArg);
                appendExecuteResult(process, result);
            } else if (ProfilerAction.start.equals(profilerAction)) {
                // Track if file parameter was specified during start
                boolean autoGeneratedFile = false;
                if (this.file != null) {
                    fileSpecifiedAtStart = this.file;
                    logger.debug("File specified during profiler start: {}", fileSpecifiedAtStart);
                } else if (this.timeout != null) {
                    // Auto-generate file if timeout is specified but file is not
                    try {
                        this.file = outputFile();
                        logger.debug("Auto-generated file for timeout: {}", this.file);
                        fileSpecifiedAtStart = this.file;
                        autoGeneratedFile = true;
                    } catch (IOException e) {
                        logger.warn("Failed to auto-generate file for timeout", e);
                    }
                }

                if (this.duration == null) {
                    String executeArgs = executeArgs(ProfilerAction.start);
                    String result = execute(asyncProfiler, executeArgs);
                    ProfilerModel profilerModel = createProfilerModel(result);

                    // Add information about auto-generated file for timeout
                    if (autoGeneratedFile && this.file != null) {
                        profilerModel.setOutputFile(this.file);
                        profilerModel.setExecuteResult(profilerModel.getExecuteResult()
                                + "\nAuto-generated output file will be: " + this.file + "\n");
                    }

                    process.appendResult(profilerModel);
                } else { // 设置延时执行 stop
                    final String outputFile = outputFile();
                    String executeArgs = executeArgs(ProfilerAction.start);
                    String result = execute(asyncProfiler, executeArgs);
                    ProfilerModel profilerModel = createProfilerModel(result);
                    profilerModel.setOutputFile(outputFile);
                    profilerModel.setDuration(duration);

                    // 延时执行stop
                    ArthasBootstrap.getInstance().getScheduledExecutorService().schedule(new Runnable() {
                        @Override
                        public void run() {
                            // 在异步线程执行,profiler命令已经结束,不能输出到客户端
                            try {
                                logger.info("stopping profiler ...");
                                ProfilerModel model = processStop(asyncProfiler, ProfilerAction.stop);
                                logger.info("profiler output file: " + model.getOutputFile());
                                logger.info("stop profiler successfully.");
                            } catch (Throwable e) {
                                logger.error("stop profiler failure", e);
                            }
                        }
                    }, this.duration, TimeUnit.SECONDS);
                    process.appendResult(profilerModel);
                }

            } else if (ProfilerAction.stop.equals(profilerAction)) {
                ProfilerModel profilerModel = processStop(asyncProfiler, profilerAction);
                process.appendResult(profilerModel);
            } else if (ProfilerAction.dump.equals(profilerAction)) {
                ProfilerModel profilerModel = processStop(asyncProfiler, profilerAction);
                process.appendResult(profilerModel);
            } else if (ProfilerAction.resume.equals(profilerAction)) {
                String executeArgs = executeArgs(ProfilerAction.resume);
                String result = execute(asyncProfiler, executeArgs);
                appendExecuteResult(process, result);
            } else if (ProfilerAction.check.equals(profilerAction)) {
                String executeArgs = executeArgs(ProfilerAction.check);
                String result = execute(asyncProfiler, executeArgs);
                appendExecuteResult(process, result);
            } else if (ProfilerAction.version.equals(profilerAction)) {
                String result = asyncProfiler.execute("version=full");
                appendExecuteResult(process, result);
            } else if (ProfilerAction.status.equals(profilerAction)
                    || ProfilerAction.meminfo.equals(profilerAction)
                    || ProfilerAction.list.equals(profilerAction)) {
                String result = asyncProfiler.execute(profilerAction.toString());
                appendExecuteResult(process, result);
            } else if (ProfilerAction.dumpCollapsed.equals(profilerAction)) {
                if (actionArg == null) {
                    actionArg = "TOTAL";
                }
                actionArg = actionArg.toUpperCase();
                if ("TOTAL".equals(actionArg) || "SAMPLES".equals(actionArg)) {
                    String result = asyncProfiler.dumpCollapsed(Counter.valueOf(actionArg));
                    appendExecuteResult(process, result);
                } else {
                    process.end(1, "ERROR: dumpCollapsed argumment should be TOTAL or SAMPLES. ");
                    return;
                }
            } else if (ProfilerAction.dumpFlat.equals(profilerAction)) {
                int maxMethods = 0;
                if (actionArg != null) {
                    maxMethods = Integer.valueOf(actionArg);
                }
                String result = asyncProfiler.dumpFlat(maxMethods);
                appendExecuteResult(process, result);
            } else if (ProfilerAction.dumpTraces.equals(profilerAction)) {
                int maxTraces = 0;
                if (actionArg != null) {
                    maxTraces = Integer.valueOf(actionArg);
                }
                String result = asyncProfiler.dumpTraces(maxTraces);
                appendExecuteResult(process, result);
            } else if (ProfilerAction.getSamples.equals(profilerAction)) {
                String result = "" + asyncProfiler.getSamples() + "\n";
                appendExecuteResult(process, result);
            }
            process.end();
        } catch (Throwable e) {
            logger.error("AsyncProfiler error", e);
            process.end(1, "AsyncProfiler error: "+e.getMessage());
        }
    }