diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ba5871de41..92227fe0b3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Kotlin now sees `Attachment.getBytes()`, `Device.getArchs()`, `Spring.getActiveProfiles()` and `JsonSerializationUtils.bytesFrom(...)` as nullable, because `@Nullable` now annotates the returned array instead of its elements ([#6558](https://github.com/getsentry/sentry-java/pull/6558)) - Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686)) - Android app start: Reopening the app within a minute of process start is now reported as a warm start timed from the reopened screen, instead of reusing the cold start's timing. In standalone app start mode, this no longer sends the cold start a second time, as a warm start ([#6687](https://github.com/getsentry/sentry-java/pull/6687)) +- 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.65.0 diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java index 19ea1c40659..a24af5bf280 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java @@ -26,7 +26,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; @@ -303,13 +302,6 @@ public void close() { private void putPerformanceCollectionDataInMeasurements( final @Nullable List 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 @NotNull ArrayDeque memoryUsageMeasurements = new ArrayDeque<>(performanceCollectionData.size()); @@ -321,7 +313,9 @@ private void putPerformanceCollectionDataInMeasurements( synchronized (performanceCollectionData) { for (final @NotNull PerformanceCollectionData data : performanceCollectionData) { final long nanoTimestamp = data.getNanoTimestamp(); - final long relativeStartNs = nanoTimestamp + timestampDiff; + // The tick comes from options.getMonotonicTicker(), which on Android is + // SystemClock.elapsedRealtimeNanos(), the clock profileStartNanos was read from. + final long relativeStartNs = data.getTickNanos() - profileStartNanos; if (data.hasCpuUsagePercentage()) { cpuUsageMeasurements.add( diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoContinuousProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoContinuousProfiler.java index 63512b7dd99..8c0cd7c2bee 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoContinuousProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoContinuousProfiler.java @@ -42,7 +42,6 @@ import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; import org.jetbrains.annotations.ApiStatus; @@ -672,14 +671,8 @@ Map stop() { @Nullable List performanceData = null; if (performanceCollector != null && chunkId != null) { performanceData = performanceCollector.stop(chunkId); - final long wallClockNowNanos = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis()); - final long elapsedRealtimeNowNanos = SystemClock.elapsedRealtimeNanos(); addPerformanceDataToMeasurements( - performanceData, - measurements, - wallClockNowNanos, - elapsedRealtimeNowNanos, - profileStartElapsedRealtimeNanos); + performanceData, measurements, profileStartElapsedRealtimeNanos); } performanceCollector = null; chunkId = null; @@ -713,8 +706,6 @@ private void addFrameDataToMeasurements( private static void addPerformanceDataToMeasurements( final @Nullable List performanceData, final @NotNull Map measurements, - final long wallClockNowNanos, - final long elapsedRealtimeNowNanos, final long profileStartElapsedRealtimeNanos) { if (performanceData == null || performanceData.isEmpty()) { return; @@ -730,14 +721,10 @@ private static void addPerformanceDataToMeasurements( // still write to, so we synchronize on it while iterating, as AndroidProfiler does. synchronized (performanceData) { for (final @NotNull PerformanceCollectionData data : performanceData) { - // Convert sample timestamps (reported by CompositePerformanceCollector using - // System.currentTimeMillis), into the SystemClock.elapsedRealtime to report - // elapsed realtime nanos since chunk start final long nanoTimestamp = data.getNanoTimestamp(); - final long nanosSinceSample = wallClockNowNanos - nanoTimestamp; - final long sampleElapsedRealtimeNanos = elapsedRealtimeNowNanos - nanosSinceSample; - final long relativeStartNs = - sampleElapsedRealtimeNanos - profileStartElapsedRealtimeNanos; + // The tick comes from options.getMonotonicTicker(), which on Android is + // SystemClock.elapsedRealtimeNanos(), the clock the chunk start was read from. + final long relativeStartNs = data.getTickNanos() - profileStartElapsedRealtimeNanos; if (data.hasCpuUsagePercentage()) { cpuUsageMeasurements.addLast( new ProfileMeasurementValue( diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt index 5a27a303342..abbdf92f916 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt @@ -196,7 +196,7 @@ class AndroidContinuousProfilerTest { @Test fun `profiler sends chunk with measurements`() { val performanceCollector = mock() - val collectionData = PerformanceCollectionData(10) + val collectionData = PerformanceCollectionData(10, 10) collectionData.usedHeapMemory = 2 collectionData.usedNativeMemory = 3 diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt index 422eb1ca804..5f364503cfb 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt @@ -26,14 +26,14 @@ class AndroidCpuCollectorTest { @Test fun `collect works only after setup`() { - val data = PerformanceCollectionData(10) + val data = PerformanceCollectionData(10, 10) fixture.getSut().collect(data) assertThat(data.hasCpuUsagePercentage()).isFalse() } @Test fun `when collect cpu is collected`() { - val data = PerformanceCollectionData(10) + val data = PerformanceCollectionData(10, 10) val collector = fixture.getSut() collector.setup() collector.collect(data) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt index 4a7a621ede2..8c8b0fe03de 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt @@ -15,7 +15,7 @@ class AndroidMemoryCollectorTest { @Test fun `when collect, both native and heap memory are collected`() { - val data = PerformanceCollectionData(10) + val data = PerformanceCollectionData(10, 10) val usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize() val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory() fixture.collector.collect(data) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt index 8f493343d1e..da00dd87626 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt @@ -17,6 +17,7 @@ import java.io.File import java.util.concurrent.Callable import java.util.concurrent.Future import java.util.concurrent.FutureTask +import java.util.concurrent.TimeUnit import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.assertContentEquals @@ -276,13 +277,13 @@ class AndroidProfilerTest { fun `profiler includes performance measurements when passed on end`() { val profiler = fixture.getSut() val performanceCollectionData = ArrayList() - var singleData = PerformanceCollectionData(10) + var singleData = PerformanceCollectionData(10, 10) singleData.usedHeapMemory = 2 singleData.usedNativeMemory = 3 singleData.cpuUsagePercentage = 1.4 performanceCollectionData.add(singleData) - singleData = PerformanceCollectionData(20) + singleData = PerformanceCollectionData(20, 20) singleData.usedHeapMemory = 3 singleData.usedNativeMemory = 4 performanceCollectionData.add(singleData) @@ -303,6 +304,29 @@ class AndroidProfilerTest { ) } + @Test + fun `performance measurements are placed by their tick, not their wall timestamp`() { + val profiler = fixture.getSut() + val profileStartNanos = profiler.start()!!.startNanos + // Wall timestamps that disagree with the ticks, as after a clock step during the profile. + val samples = + listOf( + PerformanceCollectionData(0, profileStartNanos + 1_000_000).apply { usedHeapMemory = 2 }, + PerformanceCollectionData(TimeUnit.HOURS.toNanos(5), profileStartNanos + 2_000_000).apply { + usedHeapMemory = 3 + }, + ) + + val data = profiler.endAndCollect(false, samples) + + assertContentEquals( + listOf("1000000", "2000000"), + data!!.measurementsMap[ProfileMeasurement.ID_MEMORY_FOOTPRINT]!!.values.map { + it.relativeStartNs + }, + ) + } + @Test fun `profiler stops profiling, clear running flag and scheduled job on close`() { val profiler = fixture.getSut() diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt index b37a6bbdee5..e60d2e8ea6a 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt @@ -472,13 +472,13 @@ class AndroidTransactionProfilerTest { fun `profiler includes performance measurements when passed on transaction finish`() { val profiler = fixture.getSut(context) val performanceCollectionData = ArrayList() - var singleData = PerformanceCollectionData(10) + var singleData = PerformanceCollectionData(10, 10) singleData.usedHeapMemory = 2 singleData.usedNativeMemory = 3 singleData.cpuUsagePercentage = 1.4 performanceCollectionData.add(singleData) - singleData = PerformanceCollectionData(20) + singleData = PerformanceCollectionData(20, 20) singleData.usedHeapMemory = 3 singleData.usedNativeMemory = 4 performanceCollectionData.add(singleData) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ChunkMeasurementCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ChunkMeasurementCollectorTest.kt index 0841f974961..1518b8a5004 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ChunkMeasurementCollectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ChunkMeasurementCollectorTest.kt @@ -4,6 +4,7 @@ import io.sentry.CompositePerformanceCollector import io.sentry.PerformanceCollectionData import io.sentry.android.core.internal.util.SentryFrameMetricsCollector import io.sentry.profilemeasurements.ProfileMeasurement +import io.sentry.test.getProperty import java.util.concurrent.TimeUnit import kotlin.test.Test import kotlin.test.assertEquals @@ -105,6 +106,31 @@ class ChunkMeasurementCollectorTest { ) } + @Test + fun `performance samples are placed by their tick, not their wall timestamp`() { + val performanceCollector: CompositePerformanceCollector = mock() + val collector = PerfettoContinuousProfiler.ChunkMeasurementCollector(mock()) + collector.start(performanceCollector, "chunk") + val chunkStart = collector.getProperty("profileStartElapsedRealtimeNanos") + // Wall timestamps that disagree with the ticks, as after a clock step during the chunk. + whenever(performanceCollector.stop(any())) + .thenReturn( + listOf( + PerformanceCollectionData(0, chunkStart + 1_000_000).apply { usedHeapMemory = 1 }, + PerformanceCollectionData(TimeUnit.HOURS.toNanos(5), chunkStart + 2_000_000).apply { + usedHeapMemory = 2 + }, + ), + ) + + val measurements = collector.stop() + + assertEquals( + listOf("1000000", "2000000"), + measurements[ProfileMeasurement.ID_MEMORY_FOOTPRINT]!!.values.map { it.relativeStartNs }, + ) + } + /** * A frameEndNanos far enough ahead of the collector's own `System.nanoTime()` reading that the * chunk-relative timestamp stays positive regardless of test execution timing. @@ -113,7 +139,7 @@ class ChunkMeasurementCollectorTest { /** A null measurement is left unset, as it would be by a collector that did not report it. */ private fun perfData(nanos: Long, cpu: Double?, heap: Long?, native: Long?) = - PerformanceCollectionData(nanos).apply { + PerformanceCollectionData(nanos, nanos).apply { cpu?.let { cpuUsagePercentage = it } heap?.let { usedHeapMemory = it } native?.let { usedNativeMemory = it } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 5caa7674c90..d07fae04b4e 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2228,9 +2228,10 @@ public final class io/sentry/OutboxSender : io/sentry/IEnvelopeSender { } public final class io/sentry/PerformanceCollectionData { - public fun (J)V + public fun (JJ)V public fun getCpuUsagePercentage ()D public fun getNanoTimestamp ()J + public fun getTickNanos ()J public fun getUsedHeapMemory ()J public fun getUsedNativeMemory ()J public fun hasCpuUsagePercentage ()Z diff --git a/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java b/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java index 89373516e2c..84d87733cc4 100644 --- a/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java +++ b/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java @@ -114,7 +114,9 @@ public void run() { timedOutTransactions.clear(); final @NotNull PerformanceCollectionData tempData = - new PerformanceCollectionData(options.getDateProvider().now().nanoTimestamp()); + new PerformanceCollectionData( + options.getDateProvider().now().nanoTimestamp(), + options.getMonotonicTicker().tickNanos()); // Enrich tempData using collectors for (IPerformanceSnapshotCollector collector : snapshotCollectors) { diff --git a/sentry/src/main/java/io/sentry/PerformanceCollectionData.java b/sentry/src/main/java/io/sentry/PerformanceCollectionData.java index 365cacbeb6f..278ae11e0fa 100644 --- a/sentry/src/main/java/io/sentry/PerformanceCollectionData.java +++ b/sentry/src/main/java/io/sentry/PerformanceCollectionData.java @@ -18,9 +18,17 @@ public final class PerformanceCollectionData { private long usedNativeMemory; private boolean hasUsedNativeMemory; private final long nanoTimestamp; + private final long tickNanos; - public PerformanceCollectionData(final long nanoTimestamp) { + /** + * @param nanoTimestamp when the sample was taken, in epoch nanoseconds + * @param tickNanos when the sample was taken, on {@link SentryOptions#getMonotonicTicker()}. + * Profilers place samples on their timeline with it, because the wall clock can step while a + * profile runs. + */ + public PerformanceCollectionData(final long nanoTimestamp, final long tickNanos) { this.nanoTimestamp = nanoTimestamp; + this.tickNanos = tickNanos; } /** Set the cpu usage percentage. */ @@ -69,4 +77,8 @@ public boolean hasUsedNativeMemory() { public long getNanoTimestamp() { return nanoTimestamp; } + + public long getTickNanos() { + return tickNanos; + } } diff --git a/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt b/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt index 7f2b593b9a5..d47f80b607a 100644 --- a/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt +++ b/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt @@ -13,7 +13,7 @@ class JavaMemoryCollectorTest { @Test fun `when collect, only heap memory is collected`() { - val data = PerformanceCollectionData(10) + val data = PerformanceCollectionData(10, 10) val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory() fixture.collector.collect(data) assertThat(data.hasUsedNativeMemory()).isFalse()