public void run()

in apm-agent-plugins/apm-profiling-plugin/src/main/java/co/elastic/apm/agent/profiler/SamplingProfiler.java [341:403]


    public void run() {

        boolean enabled = config.isProfilingEnabled() && tracer.isRunning();
        boolean hasBeenDisabled = previouslyEnabled && !enabled;
        previouslyEnabled = enabled;

        if (!enabled) {
            if (jfrParser != null) {
                jfrParser = null;
            }
            if (!scheduler.isShutdown()) {
                scheduler.schedule(this, config.getProfilingInterval().getMillis(), TimeUnit.MILLISECONDS);
            }

            if (hasBeenDisabled) {
                // only clear when going from enabled -> disabled state
                try {
                    clear();
                } catch (Throwable throwable) {
                    logger.error("Error while trying to clear profiler constructs", throwable);
                }
            }

            return;
        }


        // lazily create temporary files
        try {
            createFilesIfRequired();
        } catch (IOException e) {
            logger.error("unable to initialize profiling files", e);
            return;
        }

        TimeDuration profilingDuration = config.getProfilingDuration();
        boolean postProcessingEnabled = config.isPostProcessingEnabled();

        setProfilingSessionOngoing(postProcessingEnabled);

        if (postProcessingEnabled) {
            logger.debug("Start full profiling session (async-profiler and agent processing)");
        } else {
            logger.debug("Start async-profiler profiling session");
        }
        try {
            profile(profilingDuration);
        } catch (Throwable t) {
            setProfilingSessionOngoing(false);
            logger.error("Stopping profiler", t);
            return;
        }
        logger.debug("End profiling session");

        boolean interrupted = Thread.currentThread().isInterrupted();
        boolean continueProfilingSession = config.isNonStopProfiling() && !interrupted && config.isProfilingEnabled() && postProcessingEnabled;
        setProfilingSessionOngoing(continueProfilingSession);

        if (!interrupted && !scheduler.isShutdown()) {
            long delay = config.getProfilingInterval().getMillis() - profilingDuration.getMillis();
            scheduler.schedule(this, delay, TimeUnit.MILLISECONDS);
        }
    }