Search in sources :

Example 1 with AsyncProfiler

use of one.profiler.AsyncProfiler in project LinkAgent by shulieTech.

the class ProfilerModule method profiler.

@Command(value = "profiler", description = "执行 async-profiler")
public CommandResponse<ProfilerModel> profiler(final Map<String, String> args) {
    try {
        final String action = getParameter(args, "action");
        ProfilerAction profilerAction = ProfilerAction.valueOf(action);
        if (ProfilerAction.actions.equals(profilerAction)) {
            return CommandResponse.success(new ProfilerModel(actions()));
        }
        final String actionArg = getParameter(args, "actionArg");
        final AsyncProfiler asyncProfiler = this.profilerInstance(action, actionArg);
        if (ProfilerAction.execute.equals(profilerAction)) {
            if (actionArg == null) {
                return CommandResponse.failure("actionArg can not be empty.");
            }
            String result = execute(asyncProfiler, actionArg);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.start.equals(profilerAction)) {
            // jfr录制,必须在start的时候就指定文件路径
            final String file = getFile(args);
            final String format = getParameter(args, "format", "svg");
            String executeArgs = executeArgs(ProfilerAction.start, args);
            String result = execute(asyncProfiler, executeArgs);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            Long duration = getLongParameter(args, "duration");
            if (duration != null) {
                final String outputFile = outputFile(file, format);
                profilerModel.setOutputFile(outputFile);
                profilerModel.setDuration(duration);
                // 延时执行stop
                ExecutorServiceFactory.getFactory().schedule(new Runnable() {

                    @Override
                    public void run() {
                        // 在异步线程执行,profiler命令已经结束,不能输出到客户端
                        try {
                            logger.info("stopping profiler ...");
                            ProfilerModel model = processStop(asyncProfiler, args, file, format, action, actionArg);
                            logger.info("profiler output file: " + model.getOutputFile());
                            logger.info("stop profiler successfully.");
                        } catch (Throwable e) {
                            logger.error("stop profiler failure", e);
                        }
                    }
                }, duration, TimeUnit.SECONDS);
            }
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.stop.equals(profilerAction)) {
            final String file = getFile(args);
            final String format = getParameter(args, "format");
            ProfilerModel profilerModel = processStop(asyncProfiler, args, file, format, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.resume.equals(profilerAction)) {
            String executeArgs = executeArgs(ProfilerAction.resume, args);
            String result = execute(asyncProfiler, executeArgs);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.list.equals(profilerAction)) {
            String result = asyncProfiler.execute("list");
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.version.equals(profilerAction)) {
            String result = asyncProfiler.execute("version");
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.status.equals(profilerAction)) {
            String result = asyncProfiler.execute("status");
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.dumpCollapsed.equals(profilerAction)) {
            String actionArgs = actionArg;
            if (actionArgs == null) {
                actionArgs = "TOTAL";
            }
            actionArgs = actionArgs.toUpperCase();
            if ("TOTAL".equals(actionArg) || "SAMPLES".equals(actionArgs)) {
                String result = asyncProfiler.dumpCollapsed(Counter.valueOf(actionArgs));
                ProfilerModel profilerModel = createProfilerModel(result, action, actionArgs);
                return CommandResponse.success(profilerModel);
            } else {
                return CommandResponse.failure("ERROR: dumpCollapsed argument should be TOTAL or SAMPLES. ");
            }
        } else if (ProfilerAction.dumpFlat.equals(profilerAction)) {
            int maxMethods = 0;
            if (actionArg != null) {
                maxMethods = Integer.valueOf(actionArg);
            }
            String result = asyncProfiler.dumpFlat(maxMethods);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.dumpTraces.equals(profilerAction)) {
            int maxTraces = 0;
            if (actionArg != null) {
                maxTraces = Integer.valueOf(actionArg);
            }
            String result = asyncProfiler.dumpTraces(maxTraces);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.getSamples.equals(profilerAction)) {
            String result = "" + asyncProfiler.getSamples() + "\n";
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        }
        return CommandResponse.failure("ERROR: unsupported action :" + profilerAction);
    } catch (Throwable e) {
        logger.error("AsyncProfiler error", e);
        return CommandResponse.failure(e);
    }
}
Also used : AsyncProfiler(one.profiler.AsyncProfiler) ProfilerModel(com.shulie.instrument.simulator.module.model.profiler.ProfilerModel) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 2 with AsyncProfiler

use of one.profiler.AsyncProfiler in project apm-agent-java by elastic.

the class AsyncProfilerUpgrader method updateAsyncProfilerBinaries.

@Test
void updateAsyncProfilerBinaries() throws Exception {
    GitHub github = GitHub.connectAnonymously();
    GHRepository repository = github.getRepository("jvm-profiling-tools/async-profiler");
    GHRelease release = repository.getReleaseByTagName("v" + TARGET_VERSION);
    PagedIterable<GHAsset> releaseAssets = release.listAssets();
    Path downloadDirPath = Files.createTempDirectory(String.format("AsyncProfiler_%s_", TARGET_VERSION));
    for (GHAsset releaseAsset : releaseAssets) {
        if (releaseAsset.getContentType().equals("application/x-gzip")) {
            downloadAndReplaceBinary(releaseAsset.getBrowserDownloadUrl(), releaseAsset.getName(), downloadDirPath, releaseAsset.getSize());
        }
    }
    // test we are now using the right version
    Path thisOsLib = getBinariesResourceDir().resolve(co.elastic.apm.agent.profiler.asyncprofiler.AsyncProfiler.getLibraryFileName() + ".so");
    AsyncProfiler asyncProfiler = AsyncProfiler.getInstance(thisOsLib.toString());
    assertThat(asyncProfiler.getVersion()).isEqualTo(TARGET_VERSION);
}
Also used : Path(java.nio.file.Path) GHRepository(org.kohsuke.github.GHRepository) GitHub(org.kohsuke.github.GitHub) AsyncProfiler(one.profiler.AsyncProfiler) GHAsset(org.kohsuke.github.GHAsset) GHRelease(org.kohsuke.github.GHRelease) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncProfiler (one.profiler.AsyncProfiler)2 Command (com.shulie.instrument.simulator.api.annotation.Command)1 ProfilerModel (com.shulie.instrument.simulator.module.model.profiler.ProfilerModel)1 Path (java.nio.file.Path)1 Test (org.junit.jupiter.api.Test)1 GHAsset (org.kohsuke.github.GHAsset)1 GHRelease (org.kohsuke.github.GHRelease)1 GHRepository (org.kohsuke.github.GHRepository)1 GitHub (org.kohsuke.github.GitHub)1