Skip to content

Commit

Permalink
fix: remove og pyroscope bucket alignment as it is not needed afte gr… (
Browse files Browse the repository at this point in the history
#112)

* fix: remove og pyroscope bucket alignment as it is not needed afte grafana merge

* fix tests

* fix

* fix: remove og pyroscope bucket alignment as it is not needed afte grafana merge
  • Loading branch information
korniltsev authored Sep 4, 2023
1 parent 7039d94 commit 0654e95
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 114 deletions.
23 changes: 0 additions & 23 deletions agent/src/main/java/io/pyroscope/javaagent/DateUtils.java

This file was deleted.

25 changes: 7 additions & 18 deletions agent/src/main/java/io/pyroscope/javaagent/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,15 @@ public synchronized void stop() {

/**
*
* @param profilingIntervalStartTime - time when profiling has been started
* @param started - time when profiling has been started
* @param ended - time when profiling has ended
* @return Profiling data and dynamic labels as {@link Snapshot}
*/
public synchronized Snapshot dumpProfile(Instant profilingIntervalStartTime) {
return dumpImpl(profilingIntervalStartTime);
public synchronized Snapshot dumpProfile(Instant started, Instant ended) {
return dumpImpl(started, ended);
}

/**
* Stop profiling, dump profiling data, start again
* Deprecated, use start, stop, dumpProfile methods instead
*/
@Deprecated
public synchronized Snapshot dump(Instant profilingIntervalStartTime) {
instance.stop();

Snapshot result = dumpImpl(profilingIntervalStartTime);

start();

return result;
}

private String createJFRCommand() {
StringBuilder sb = new StringBuilder();
Expand All @@ -121,7 +109,7 @@ private String createJFRCommand() {
return sb.toString();
}

private Snapshot dumpImpl(Instant profilingIntervalStartTime) {
private Snapshot dumpImpl(Instant started, Instant ended) {
if (config.gcBeforeDump) {
System.gc();
}
Expand All @@ -134,7 +122,8 @@ private Snapshot dumpImpl(Instant profilingIntervalStartTime) {
return new Snapshot(
format,
eventType,
profilingIntervalStartTime,
started,
ended,
data,
Pyroscope.LabelsWrapper.dump()
);
Expand Down
4 changes: 3 additions & 1 deletion agent/src/main/java/io/pyroscope/javaagent/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public final class Snapshot {
public final Format format;
public final EventType eventType;
public final Instant started;
public final Instant ended;
public final byte[] data;
public final JfrLabels.Snapshot labels;

Snapshot(Format format, final EventType eventType, final Instant started, final byte[] data, JfrLabels.Snapshot labels) {
Snapshot(Format format, final EventType eventType, final Instant started, final Instant ended,final byte[] data, JfrLabels.Snapshot labels) {
this.format = format;
this.eventType = eventType;
this.started = started;
this.ended = ended;
this.data = data;
this.labels = labels;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class Config {
private static final List<EventType> DEFAULT_SAMPLING_EVENT_ORDER = null;
private static final int DEFAULT_JAVA_STACK_DEPTH_MAX = 2048;
private static final String DEFAULT_SERVER_ADDRESS = "http://localhost:4040";
private static final Format DEFAULT_FORMAT = Format.COLLAPSED;
private static final Format DEFAULT_FORMAT = Format.JFR;
// The number of snapshots simultaneously stored in memory is limited by this.
// The number is fairly arbitrary. If an average snapshot is 5KB, it's about 160 KB.
private static final int DEFAULT_PUSH_QUEUE_CAPACITY = 8;
Expand Down Expand Up @@ -188,6 +188,9 @@ public final class Config {
DefaultLogger.PRECONFIG_LOGGER.log(Logger.Level.WARN,
"Setting PYROSCOPE_PROFILER_LOCK to 0 registers every lock event, causing significant overhead and results in large profiles, making it not ideal for production. We recommend a starting value of 10ms, adjusting as needed.");
}
if (format == Format.COLLAPSED) {
DefaultLogger.PRECONFIG_LOGGER.log(Logger.Level.WARN, "COLLAPSED format is deprecated");
}
}

public long profilingIntervalInHertz() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.pyroscope.javaagent.api.Logger;
import io.pyroscope.javaagent.api.ProfilingScheduler;
import io.pyroscope.javaagent.config.Config;
import kotlin.random.Random;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -14,7 +15,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import static io.pyroscope.javaagent.DateUtils.truncate;

public class ContinuousProfilingScheduler implements ProfilingScheduler {
final Config config;
Expand Down Expand Up @@ -47,18 +47,18 @@ public void start(Profiler profiler) {
}
final Runnable dumpProfile = () -> {
Snapshot snapshot;
Instant now;
try {
profiler.stop();
snapshot = profiler.dumpProfile(
alignProfilingIntervalStartTime(this.profilingIntervalStartTime, config.uploadInterval)
);
now = Instant.now();
snapshot = profiler.dumpProfile(this.profilingIntervalStartTime, now);
profiler.start();
} catch (Throwable throwable) {
logger.log(Logger.Level.ERROR, "Error dumping profiler %s", throwable);
stop();
return;
}
profilingIntervalStartTime = Instant.now();
profilingIntervalStartTime = now;
exporter.export(snapshot);
};

Expand All @@ -76,41 +76,26 @@ private void stop() {

/**
* Starts the first profiling interval.
* profilingIntervalStartTime is set to a current time aligned to upload interval
* Duration of the first profiling interval will be smaller than uploadInterval for alignment.
* <a href="https://github.com/pyroscope-io/pyroscope-java/issues/40">...</a>
* profilingIntervalStartTime is set to now
* Duration of the first profiling interval is a random fraction of uploadInterval not smaller than 2000ms.
*
* @return Duration of the first profiling interval
*/
private Duration startFirst(Profiler profiler) {
Instant now = Instant.now();
Instant prevUploadInterval = truncate(now, config.uploadInterval);
Instant nextUploadInterval = prevUploadInterval.plus(config.uploadInterval);
Duration firstProfilingDuration = Duration.between(now, nextUploadInterval);

long uploadIntervalMillis = config.uploadInterval.toMillis();
float randomOffset = Random.Default.nextFloat();
uploadIntervalMillis = (long)((float)uploadIntervalMillis * randomOffset);
if (uploadIntervalMillis < 2000) {
uploadIntervalMillis = 2000;
}
Duration firstProfilingDuration = Duration.ofMillis(uploadIntervalMillis);

profiler.start();
profilingIntervalStartTime = prevUploadInterval;
profilingIntervalStartTime = now;
return firstProfilingDuration;
}

/**
* Aligns profilingIntervalStartTime to the closest aligned upload time either forward or backward
* For example if upload interval is 10s and profilingIntervalStartTime is 00:00.01 it will return 00:00
* and if profilingIntervalStartTime is 00:09.239 it will return 00:10
* <a href="https://github.com/pyroscope-io/pyroscope-java/issues/40">...</a>
*
* @param profilingIntervalStartTime the time to align
* @param uploadInterval
* @return the aligned
*/
public static Instant alignProfilingIntervalStartTime(Instant profilingIntervalStartTime, Duration uploadInterval) {
Instant prev = truncate(profilingIntervalStartTime, uploadInterval);
Instant next = prev.plus(uploadInterval);
Duration d1 = Duration.between(prev, profilingIntervalStartTime);
Duration d2 = Duration.between(profilingIntervalStartTime, next);
if (d1.compareTo(d2) < 0) {
return prev;
} else {
return next;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private void uploadSnapshot(final Snapshot snapshot) throws InterruptedException
final RequestBody requestBody;
if (config.format == Format.JFR) {
byte[] labels = snapshot.labels.toByteArray();
logger.log(Logger.Level.DEBUG, "Upload attempt %d. JFR: %s, labels: %s", tries, snapshot.data.length, labels.length);
logger.log(Logger.Level.DEBUG, "Upload attempt %d. %s %s JFR: %s, labels: %s", tries,
snapshot.started.toString(), snapshot.ended.toString(), snapshot.data.length, labels.length);
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
RequestBody jfrBody = RequestBody.create(snapshot.data);
Expand Down Expand Up @@ -134,7 +135,7 @@ private static void addAuthHeader(Request.Builder request, HttpUrl url, Config c

private HttpUrl urlForSnapshot(final Snapshot snapshot) {
Instant started = snapshot.started;
Instant finished = started.plus(config.uploadInterval);
Instant finished = snapshot.ended;
HttpUrl.Builder builder = HttpUrl.parse(config.serverAddress)
.newBuilder()
.addPathSegment("ingest")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.pyroscope.javaagent.impl;

import static io.pyroscope.javaagent.DateUtils.truncate;

import io.pyroscope.javaagent.EventType;
import io.pyroscope.javaagent.Profiler;
Expand All @@ -9,6 +8,8 @@
import io.pyroscope.javaagent.api.Logger;
import io.pyroscope.javaagent.api.ProfilingScheduler;
import io.pyroscope.javaagent.config.Config;
import kotlin.random.Random;

import io.pyroscope.javaagent.config.Config.Builder;

import java.time.Duration;
Expand Down Expand Up @@ -47,8 +48,8 @@ public SamplingProfilingScheduler(Config config, Exporter exporter, Logger logge
public void start(Profiler profiler) {
final long samplingDurationMillis = config.samplingDuration.toMillis();
final Duration uploadInterval = config.uploadInterval;
final Runnable task = (null != config.samplingEventOrder) ?

final Runnable task = (null != config.samplingEventOrder) ?
() -> {
for (int i = 0; i < config.samplingEventOrder.size(); i++) {
final EventType t = config.samplingEventOrder.get(i);
Expand All @@ -57,7 +58,7 @@ public void start(Profiler profiler) {
profiler.setConfig(tmp);
dumpProfile(profiler, samplingDurationMillis, uploadInterval);
}
} :
} :
() -> dumpProfile(profiler, samplingDurationMillis, uploadInterval);

Duration initialDelay = getInitialDelay();
Expand Down Expand Up @@ -85,7 +86,7 @@ private void dumpProfile(final Profiler profiler, final long samplingDurationMil
}
profiler.stop();

Snapshot snapshot = profiler.dumpProfile(truncate(profilingStartTime, uploadInterval));
Snapshot snapshot = profiler.dumpProfile(profilingStartTime, Instant.now());
exporter.export(snapshot);
}

Expand All @@ -97,11 +98,14 @@ private void stop() {
}

private Duration getInitialDelay() {
Instant now = Instant.now();
Instant prevUploadInterval = truncate(now, config.uploadInterval);
Instant nextUploadInterval = prevUploadInterval.plus(config.uploadInterval);
Duration initialDelay = Duration.between(now, nextUploadInterval);
return initialDelay;
long uploadIntervalMillis = config.uploadInterval.toMillis();
float randomOffset = Random.Default.nextFloat();
uploadIntervalMillis = (long)((float)uploadIntervalMillis * randomOffset);
if (uploadIntervalMillis < 2000) {
uploadIntervalMillis = 2000;
}
Duration firstProfilingDuration = Duration.ofMillis(uploadIntervalMillis);
return firstProfilingDuration;
}

private Config isolate(final EventType type, final Config config) {
Expand Down

This file was deleted.

0 comments on commit 0654e95

Please sign in to comment.