Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Session Replay: Stop dropping or freezing video when the device clock changes mid-recording ([#6542](https://github.com/getsentry/sentry-java/pull/6542))
- Breadcrumbs and errors stay aligned with the video. In a full-session recording, the video since the last segment sent before the change (up to 5s) is dropped. When recording only for errors, what was buffered before the change is kept, and the replay of a later error shows the change as a gap or an overlap.
- A forward change longer than the session duration (1h) ends a full-session recording, as reaching that duration does. When recording only for errors, such a change drops what was buffered before it.
- Android: Keep CPU and memory samples aligned with the profile when the device clock changes during profiling ([#6591](https://github.com/getsentry/sentry-java/pull/6591))

## 8.67.0

Expand Down
5 changes: 3 additions & 2 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android
}

public final class io/sentry/android/core/AndroidContinuousProfiler : io/sentry/IContinuousProfiler, io/sentry/transport/RateLimiter$IRateLimitObserver {
public fun <init> (Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/ILogger;Ljava/lang/String;ILio/sentry/util/LazyEvaluator$Evaluator;)V
public fun <init> (Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/ILogger;Ljava/lang/String;ILio/sentry/util/LazyEvaluator$Evaluator;Lio/sentry/time/EpochClock;Lio/sentry/time/MonotonicTicker;)V
public fun close (Z)V
public fun getChunkId ()Lio/sentry/protocol/SentryId;
public fun getProfileRecordingState (Lio/sentry/protocol/SentryId;Lio/sentry/SentryDate;Lio/sentry/SentryDate;)Lio/sentry/profiling/ProfileRecordingState;
Expand Down Expand Up @@ -120,6 +120,7 @@ public final class io/sentry/android/core/AndroidMetricsBatchProcessorFactory :

public final class io/sentry/android/core/AndroidProfiler {
public fun <init> (Ljava/lang/String;ILio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/util/LazyEvaluator$Evaluator;Lio/sentry/ILogger;)V
public fun <init> (Ljava/lang/String;ILio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/util/LazyEvaluator$Evaluator;Lio/sentry/ILogger;Lio/sentry/time/EpochClock;Lio/sentry/time/MonotonicTicker;)V
public fun close ()V
public fun endAndCollect (ZLjava/util/List;)Lio/sentry/android/core/AndroidProfiler$ProfileEndData;
public fun start ()Lio/sentry/android/core/AndroidProfiler$ProfileStartData;
Expand Down Expand Up @@ -394,7 +395,7 @@ public final class io/sentry/android/core/NetworkBreadcrumbsIntegration : io/sen
}

public final class io/sentry/android/core/PerfettoContinuousProfiler : io/sentry/IContinuousProfiler, io/sentry/transport/RateLimiter$IRateLimitObserver {
public fun <init> (Lio/sentry/ILogger;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/util/LazyEvaluator$Evaluator;Ljava/util/function/Supplier;)V
public fun <init> (Lio/sentry/ILogger;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/util/LazyEvaluator$Evaluator;Ljava/util/function/Supplier;Lio/sentry/time/EpochClock;Lio/sentry/time/MonotonicTicker;)V
public fun close (Z)V
public fun getChunkId ()Lio/sentry/protocol/SentryId;
public fun getProfileRecordingState (Lio/sentry/protocol/SentryId;Lio/sentry/SentryDate;Lio/sentry/SentryDate;)Lio/sentry/profiling/ProfileRecordingState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.profiling.ProfileRecordingState;
import io.sentry.protocol.SentryId;
import io.sentry.time.EpochClock;
import io.sentry.time.MonotonicTicker;
import io.sentry.transport.RateLimiter;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.LazyEvaluator;
Expand Down Expand Up @@ -53,6 +55,8 @@ public final class AndroidContinuousProfiler
private final @Nullable String profilingTracesDirPath;
private final int profilingTracesHz;
private final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier;
private final @NotNull EpochClock epochClock;
private final @NotNull MonotonicTicker ticker;
private final @NotNull BuildInfoProvider buildInfoProvider;
private boolean isInitialized = false;
private final @NotNull SentryFrameMetricsCollector frameMetricsCollector;
Expand Down Expand Up @@ -80,13 +84,17 @@ public AndroidContinuousProfiler(
final @NotNull ILogger logger,
final @Nullable String profilingTracesDirPath,
final int profilingTracesHz,
final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier) {
final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier,
final @NotNull EpochClock epochClock,
final @NotNull MonotonicTicker ticker) {
this.logger = logger;
this.frameMetricsCollector = frameMetricsCollector;
this.buildInfoProvider = buildInfoProvider;
this.profilingTracesDirPath = profilingTracesDirPath;
this.profilingTracesHz = profilingTracesHz;
this.executorServiceSupplier = executorServiceSupplier;
this.epochClock = epochClock;
this.ticker = ticker;
}

private void init() {
Expand Down Expand Up @@ -115,7 +123,9 @@ private void init() {
(int) SECONDS.toMicros(1) / profilingTracesHz,
frameMetricsCollector,
null,
logger);
logger,
epochClock,
ticker);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ private static void setupProfiler(
() -> options.getExecutorService(),
() ->
new PerfettoProfiler(
appContext, options.getLogger(), options.getExecutorService())));
appContext, options.getLogger(), options.getExecutorService()),
options.getEpochClock(),
options.getMonotonicTicker()));
// Report adoption of the Perfetto continuous profiling backend (API 35+).
addIntegrationToSdkVersion("PerfettoContinuousProfiling");
} else if (options.isEnableLegacyProfiling()) {
Expand All @@ -400,7 +402,9 @@ private static void setupProfiler(
options.getLogger(),
options.getProfilingTracesDirPath(),
options.getProfilingTracesHz(),
() -> options.getExecutorService()));
() -> options.getExecutorService(),
options.getEpochClock(),
options.getMonotonicTicker()));
} else {
options
.getLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.os.Debug;
import android.os.Process;
import android.os.SystemClock;
import io.sentry.DateUtils;
import io.sentry.ILogger;
import io.sentry.ISentryExecutorService;
Expand All @@ -12,9 +11,14 @@
import io.sentry.SentryLevel;
import io.sentry.SentryNanotimeDate;
import io.sentry.SentryUUID;
import io.sentry.android.core.internal.time.AndroidMonotonicTicker;
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.profilemeasurements.ProfileMeasurement;
import io.sentry.profilemeasurements.ProfileMeasurementValue;
import io.sentry.time.AnchoredClock;
import io.sentry.time.EpochClock;
import io.sentry.time.MonotonicTicker;
import io.sentry.time.SystemEpochClock;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.Objects;
Expand All @@ -26,7 +30,6 @@
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -80,6 +83,11 @@ public ProfileEndData(

private static final int PROFILING_TIMEOUT_MILLIS = 30_000;
private long profileStartNanos = 0;
// CPU and memory samples get their timestamps from it rather than from the wall clock when they
// were taken, so a clock change during the profile cannot move them.
private @Nullable AnchoredClock profileClock = null;
private final @NotNull EpochClock epochClock;
private final @NotNull MonotonicTicker ticker;
private final @NotNull File traceFilesDir;
private final int intervalUs;
private @Nullable Future<?> scheduledFinish = null;
Expand All @@ -99,13 +107,37 @@ public ProfileEndData(
private volatile boolean isRunning = false;
final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock();

/** Kept for sentry-react-native, which calls this constructor. */
public AndroidProfiler(
final @NotNull String tracesFilesDirPath,
final int intervalUs,
final @NotNull SentryFrameMetricsCollector frameMetricsCollector,
final @Nullable LazyEvaluator.Evaluator<ISentryExecutorService>
timeoutExecutorServiceSupplier,
final @NotNull ILogger logger) {
this(
tracesFilesDirPath,
intervalUs,
frameMetricsCollector,
timeoutExecutorServiceSupplier,
logger,
SystemEpochClock.getInstance(),
AndroidMonotonicTicker.getInstance());
}

/**
* @param ticker must be the {@link io.sentry.SentryOptions#getMonotonicTicker()} that stamps the
* {@link PerformanceCollectionData} passed to {@link #endAndCollect}.
*/
public AndroidProfiler(
final @NotNull String tracesFilesDirPath,
final int intervalUs,
final @NotNull SentryFrameMetricsCollector frameMetricsCollector,
final @Nullable LazyEvaluator.Evaluator<ISentryExecutorService>
timeoutExecutorServiceSupplier,
final @NotNull ILogger logger,
final @NotNull EpochClock epochClock,
final @NotNull MonotonicTicker ticker) {
this.traceFilesDir =
new File(Objects.requireNonNull(tracesFilesDirPath, "TracesFilesDirPath is required"));
this.intervalUs = intervalUs;
Expand All @@ -114,6 +146,8 @@ public AndroidProfiler(
this.timeoutExecutorServiceSupplier = timeoutExecutorServiceSupplier;
this.frameMetricsCollector =
Objects.requireNonNull(frameMetricsCollector, "SentryFrameMetricsCollector is required");
this.epochClock = epochClock;
this.ticker = ticker;
}

@SuppressLint("NewApi")
Expand Down Expand Up @@ -153,15 +187,12 @@ public void onFrameMetricCollected(
final boolean isSlow,
final boolean isFrozen,
final float refreshRate) {
// profileStartNanos is calculated through SystemClock.elapsedRealtimeNanos(),
// but frameEndNanos uses System.nanotime(), so we convert it to get the timestamp
// relative to profileStartNanos
// profileStartNanos is read from the ticker, but frameEndNanos uses
// System.nanotime(), so we convert it to get the timestamp relative to
// profileStartNanos
final long timestampNanos = new SentryNanotimeDate().nanoTimestamp();
final long frameTimestampRelativeNanos =
frameEndNanos
- System.nanoTime()
+ SystemClock.elapsedRealtimeNanos()
- profileStartNanos;
frameEndNanos - System.nanoTime() + ticker.tickNanos() - profileStartNanos;

// We don't allow negative relative timestamps.
// So we add a check, even if this should never happen.
Expand Down Expand Up @@ -201,8 +232,11 @@ public void onFrameMetricCollected(
e);
}

profileStartNanos = SystemClock.elapsedRealtimeNanos();
final @NotNull Date profileStartTimestamp = DateUtils.getCurrentDateTime();
profileStartNanos = ticker.tickNanos();
final @NotNull AnchoredClock clock = AnchoredClock.create(epochClock, ticker);
profileClock = clock;
final @NotNull Date profileStartTimestamp =
DateUtils.getDateTime(clock.startTime().epochMillis());
long profileStartCpuMillis = Process.getElapsedCpuTime();

// We don't make any check on the file existence or writeable state, because we don't want to
Expand Down Expand Up @@ -247,7 +281,7 @@ public void onFrameMetricCollected(
}
frameMetricsCollector.stopCollection(frameMetricsCollectorId);

long transactionEndNanos = SystemClock.elapsedRealtimeNanos();
long transactionEndNanos = ticker.tickNanos();
long transactionEndCpuMillis = Process.getElapsedCpuTime();

if (traceFile == null) {
Expand Down Expand Up @@ -303,14 +337,8 @@ public void close() {
private void putPerformanceCollectionDataInMeasurements(
final @Nullable List<PerformanceCollectionData> performanceCollectionData) {

// This difference is required, since the PerformanceCollectionData timestamps are expressed in
// terms of System.currentTimeMillis() and measurements timestamps require the nanoseconds since
// the beginning, expressed with SystemClock.elapsedRealtimeNanos()
long timestampDiff =
SystemClock.elapsedRealtimeNanos()
- profileStartNanos
- TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
if (performanceCollectionData != null) {
final @Nullable AnchoredClock clock = profileClock;
if (performanceCollectionData != null && clock != null) {
final @NotNull ArrayDeque<ProfileMeasurementValue> memoryUsageMeasurements =
new ArrayDeque<>(performanceCollectionData.size());
final @NotNull ArrayDeque<ProfileMeasurementValue> nativeMemoryUsageMeasurements =
Expand All @@ -320,8 +348,8 @@ private void putPerformanceCollectionDataInMeasurements(

synchronized (performanceCollectionData) {
for (final @NotNull PerformanceCollectionData data : performanceCollectionData) {
final long nanoTimestamp = data.getNanoTimestamp();
final long relativeStartNs = nanoTimestamp + timestampDiff;
final long relativeStartNs = data.getTickNanos() - profileStartNanos;
final long nanoTimestamp = clock.at(data.getTickNanos()).epochNanos();

if (data.hasCpuUsagePercentage()) {
cpuUsageMeasurements.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.sentry.SentryOptions;
import io.sentry.android.core.internal.util.CpuInfoUtils;
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.time.EpochClock;
import io.sentry.time.MonotonicTicker;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.Objects;
Expand All @@ -36,6 +38,8 @@ final class AndroidTransactionProfiler implements ITransactionProfiler {
private final boolean isProfilingEnabled;
private final int profilingTracesHz;
private final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier;
private final @NotNull EpochClock epochClock;
private final @NotNull MonotonicTicker ticker;
private final @NotNull BuildInfoProvider buildInfoProvider;
private boolean isInitialized = false;
private final @NotNull AtomicBoolean isRunning = new AtomicBoolean(false);
Expand Down Expand Up @@ -66,7 +70,9 @@ public AndroidTransactionProfiler(
sentryAndroidOptions.getProfilingTracesDirPath(),
sentryAndroidOptions.isProfilingEnabled(),
sentryAndroidOptions.getProfilingTracesHz(),
() -> sentryAndroidOptions.getExecutorService());
() -> sentryAndroidOptions.getExecutorService(),
sentryAndroidOptions.getEpochClock(),
sentryAndroidOptions.getMonotonicTicker());
}

public AndroidTransactionProfiler(
Expand All @@ -77,7 +83,9 @@ public AndroidTransactionProfiler(
final @Nullable String profilingTracesDirPath,
final boolean isProfilingEnabled,
final int profilingTracesHz,
final @NotNull ISentryExecutorService executorService) {
final @NotNull ISentryExecutorService executorService,
final @NotNull EpochClock epochClock,
final @NotNull MonotonicTicker ticker) {
this(
context,
buildInfoProvider,
Expand All @@ -86,7 +94,9 @@ public AndroidTransactionProfiler(
profilingTracesDirPath,
isProfilingEnabled,
profilingTracesHz,
() -> executorService);
() -> executorService,
epochClock,
ticker);
}

public AndroidTransactionProfiler(
Expand All @@ -97,7 +107,9 @@ public AndroidTransactionProfiler(
final @Nullable String profilingTracesDirPath,
final boolean isProfilingEnabled,
final int profilingTracesHz,
final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier) {
final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier,
final @NotNull EpochClock epochClock,
final @NotNull MonotonicTicker ticker) {
this.context =
Objects.requireNonNull(
ContextUtils.getApplicationContext(context), "The application context is required");
Expand All @@ -112,6 +124,8 @@ public AndroidTransactionProfiler(
this.executorServiceSupplier =
Objects.requireNonNull(
executorServiceSupplier, "A supplier for ISentryExecutorService is required.");
this.epochClock = epochClock;
this.ticker = ticker;
this.profileStartTimestamp = DateUtils.getCurrentDateTime();
}

Expand Down Expand Up @@ -146,7 +160,9 @@ private void init() {
(int) SECONDS.toMicros(1) / profilingTracesHz,
frameMetricsCollector,
executorServiceSupplier,
logger);
logger,
epochClock,
ticker);
}

@Override
Expand Down
Loading
Loading