diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index 5db38fadc3..8e39c9f533 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -746,6 +746,7 @@ public final class io/sentry/android/core/anr/AnrProfile { } public final class io/sentry/android/core/anr/AnrProfileManager : java/lang/AutoCloseable { + public static final field MAX_NUM_STACKTRACES I public fun (Lio/sentry/SentryOptions;)V public fun (Lio/sentry/SentryOptions;Ljava/io/File;)V public fun add (Lio/sentry/android/core/anr/AnrStackTrace;)V @@ -904,6 +905,15 @@ public abstract interface class io/sentry/android/core/hang/ThreadPoster { public abstract fun remove (Ljava/lang/Runnable;)V } +public final class io/sentry/android/core/hang/ThreadSampleCollector : io/sentry/android/core/hang/HangDataCollector { + public fun (Lio/sentry/SentryOptions;Lio/sentry/time/EpochClock;Lio/sentry/time/MonotonicTicker;Ljava/lang/Thread;)V + public fun close ()V + public fun onRecordingCanceled ()V + public fun onRecordingStarted (Lio/sentry/android/core/hang/HangInterval;)V + public fun onRecordingStopped (Lio/sentry/android/core/hang/HangInterval;Lio/sentry/android/core/hang/HangRecord$Builder;)V + public fun onSample (Lio/sentry/android/core/hang/HangInterval;)V +} + public abstract class io/sentry/android/core/performance/ActivityLifecycleCallbacksAdapter : android/app/Application$ActivityLifecycleCallbacks { public fun ()V public fun onActivityCreated (Landroid/app/Activity;Landroid/os/Bundle;)V diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java index dd3cffc3ff..ffa5749fc4 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java @@ -1,11 +1,10 @@ package io.sentry.android.core.anr; import static io.sentry.SentryLevel.ERROR; -import static io.sentry.android.core.anr.AnrProfilingIntegration.POLLING_INTERVAL_MS; -import static io.sentry.android.core.anr.AnrProfilingIntegration.THRESHOLD_ANR_MS; import io.sentry.ILogger; import io.sentry.SentryOptions; +import io.sentry.android.core.hang.HangRecorder; import io.sentry.cache.tape.ObjectQueue; import io.sentry.cache.tape.QueueFile; import io.sentry.util.Objects; @@ -22,8 +21,12 @@ @ApiStatus.Internal public final class AnrProfileManager implements AutoCloseable { - private static final int MAX_NUM_STACKTRACES = - (int) ((THRESHOLD_ANR_MS / POLLING_INTERVAL_MS) * 2); + /** + * One sample per {@link HangRecorder#DEFAULT_POLLING_INTERVAL_MS} tick for {@link + * HangRecorder#DEFAULT_MAX_RECORDING_TIME_MS}. The ThreadSampleCollector stops at this size, + * because a full file drops its oldest entry. + */ + public static final int MAX_NUM_STACKTRACES = 120; @NotNull private final ObjectQueue queue; diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/hang/ThreadSampleCollector.java b/sentry-android-core/src/main/java/io/sentry/android/core/hang/ThreadSampleCollector.java new file mode 100644 index 0000000000..75ef4a7cf6 --- /dev/null +++ b/sentry-android-core/src/main/java/io/sentry/android/core/hang/ThreadSampleCollector.java @@ -0,0 +1,131 @@ +package io.sentry.android.core.hang; + +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import io.sentry.SentryLevel; +import io.sentry.SentryOptions; +import io.sentry.android.core.anr.AnrProfileManager; +import io.sentry.android.core.anr.AnrProfileRotationHelper; +import io.sentry.android.core.anr.AnrStackTrace; +import io.sentry.time.EpochClock; +import io.sentry.time.MonotonicTicker; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Samples the stack of a thread on each tick of a hang interval. The samples stay in memory until + * the interval is {@link #DISK_THRESHOLD_MS} old, then they are written to the ANR profile file, so + * the fatal ANR path can still read them if the process is killed. + * + *

Not thread-safe, {@link HangRecorder} calls it only on its recorder thread. + */ +@ApiStatus.Internal +public final class ThreadSampleCollector implements HangDataCollector { + + static final long DISK_THRESHOLD_MS = 700; + + private final @NotNull SentryOptions options; + private final @NotNull EpochClock epochClock; + private final @NotNull MonotonicTicker ticker; + private final @NotNull Thread thread; + private final @NotNull List samples = new ArrayList<>(); + private @Nullable AnrProfileManager profileManager; + private boolean onDisk; + + public ThreadSampleCollector( + final @NotNull SentryOptions options, + final @NotNull EpochClock epochClock, + final @NotNull MonotonicTicker ticker, + final @NotNull Thread thread) { + this.options = options; + this.epochClock = epochClock; + this.ticker = ticker; + this.thread = thread; + } + + @Override + public void onRecordingStarted(final @NotNull HangInterval interval) { + reset(); + if (profileManager == null) { + final @Nullable String cacheDirPath = options.getCacheDirPath(); + if (cacheDirPath != null) { + final @NotNull File file = + AnrProfileRotationHelper.getFileForRecording(new File(cacheDirPath)); + profileManager = new AnrProfileManager(options, file); + } + } + } + + @Override + public void onSample(final @NotNull HangInterval interval) { + if (samples.size() >= AnrProfileManager.MAX_NUM_STACKTRACES) { + return; + } + + final long epochMs = NANOSECONDS.toMillis(epochClock.now().epochNanos()); + final @NotNull AnrStackTrace sample = + new AnrStackTrace(epochMs, thread.getStackTrace(), thread.getState()); + samples.add(sample); + + final long nowMs = NANOSECONDS.toMillis(ticker.tickNanos()); + // Most hangs end before this, so they cause no disk IO. + if (nowMs - interval.getStartTimeMs() < DISK_THRESHOLD_MS) { + return; + } + + final @Nullable AnrProfileManager manager = profileManager; + if (manager == null) { + return; + } + + try { + if (!onDisk) { + onDisk = true; + startDiskRecording(manager); + } else { + manager.add(sample); + } + } catch (IOException e) { + options.getLogger().log(SentryLevel.WARNING, "Failed to write the thread samples", e); + } + } + + private void startDiskRecording(final @NotNull AnrProfileManager manager) throws IOException { + manager.clear(); + for (final @NotNull AnrStackTrace buffered : samples) { + manager.add(buffered); + } + } + + @Override + public void onRecordingStopped( + final @NotNull HangInterval interval, final @NotNull HangRecord.Builder out) { + out.setSamples(new ArrayList<>(samples)); + } + + /** Discards the samples in memory only, the fatal path keeps the file. */ + @Override + public void onRecordingCanceled() { + reset(); + } + + @Override + public void close() throws IOException { + reset(); + final @Nullable AnrProfileManager manager = profileManager; + profileManager = null; + if (manager != null) { + manager.close(); + } + } + + private void reset() { + samples.clear(); + onDisk = false; + } +} diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/hang/HangRecorderTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/hang/HangRecorderTest.kt index c43f309408..12835000b2 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/hang/HangRecorderTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/hang/HangRecorderTest.kt @@ -10,6 +10,7 @@ import io.sentry.android.core.anr.AnrStackTrace import io.sentry.android.core.hang.HangRecorder.DEFAULT_MAX_RECORDING_TIME_MS import io.sentry.android.core.hang.HangRecorder.DEFAULT_POLLING_INTERVAL_MS import io.sentry.test.DeferredExecutorService +import io.sentry.test.ImmediateExecutorService import io.sentry.test.NonOverridableNoOpSentryExecutorService import io.sentry.time.EpochClock import io.sentry.time.JavaMonotonicTicker @@ -19,6 +20,7 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit.HOURS import java.util.concurrent.TimeUnit.MILLISECONDS +import java.util.concurrent.locks.LockSupport import kotlin.test.BeforeTest import kotlin.test.Test import org.mockito.kotlin.any @@ -668,20 +670,31 @@ class HangRecorderTest { } @Test - fun `recorder on the default executor posts the probe of a thread signal`() { - val posted = CountDownLatch(1) + fun `recorder on the default executor records the stack of the watched thread`() { + val parked = Thread { while (!Thread.currentThread().isInterrupted) LockSupport.park() } + parked.isDaemon = true + parked.start() + var posts = 0 val poster = object : ThreadPoster { - override fun post(runnable: Runnable) = posted.countDown() + // The probe runs only at the 10th tick, so the thread looks blocked for about 600 ms. + override fun post(runnable: Runnable) { + if (++posts == 10) { + runnable.run() + } + } override fun remove(runnable: Runnable) = Unit } + val ticker = JavaMonotonicTicker.getInstance() + val recorded = CountDownLatch(1) + var record: HangRecord? = null appStateProvider.foreground() val created = HangRecorder( - options, + SentryOptions().apply { executorService = ImmediateExecutorService() }, appStateProvider, - JavaMonotonicTicker.getInstance(), + ticker, epochClock, DEFAULT_POLLING_INTERVAL_MS, DEFAULT_MAX_RECORDING_TIME_MS, @@ -690,23 +703,30 @@ class HangRecorderTest { listOf( ThreadHangSignal( metronome, - JavaMonotonicTicker.getInstance(), + ticker, { false }, poster, HangRecorder.DEFAULT_SUSPICIOUS_THRESHOLD_MS, ), ) }, - emptyList(), + listOf(ThreadSampleCollector(SentryOptions(), epochClock, ticker, parked)), ) - created.subscribe(listener) + created.subscribe { + record = it + recorded.countDown() + } try { - assertThat(posted.await(10, TimeUnit.SECONDS)).isTrue() + assertThat(recorded.await(10, TimeUnit.SECONDS)).isTrue() } finally { created.close() + parked.interrupt() + parked.join() } + assertThat(record!!.samples).isNotEmpty() + assertThat(record!!.samples.first().stack.map { it.methodName }).contains("park") } private class FakeAppStateProvider : AppStateProvider { diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/hang/ThreadSampleCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/hang/ThreadSampleCollectorTest.kt new file mode 100644 index 0000000000..859747d024 --- /dev/null +++ b/sentry-android-core/src/test/java/io/sentry/android/core/hang/ThreadSampleCollectorTest.kt @@ -0,0 +1,221 @@ +package io.sentry.android.core.hang + +import com.google.common.truth.Truth.assertThat +import io.sentry.SentryOptions +import io.sentry.android.core.anr.AnrProfileManager +import io.sentry.android.core.anr.AnrProfileRotationHelper +import io.sentry.android.core.anr.AnrStackTrace +import io.sentry.time.EpochClock +import io.sentry.time.TestMonotonicTicker +import io.sentry.time.Timestamp +import java.io.File +import java.nio.file.Files +import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeUnit.HOURS +import java.util.concurrent.TimeUnit.MILLISECONDS +import java.util.concurrent.locks.LockSupport +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test + +class ThreadSampleCollectorTest { + + private lateinit var cacheDir: File + private lateinit var ticker: TestMonotonicTicker + private var unixTimeMs = 1_700_000_000_000L + private lateinit var epochClock: EpochClock + private lateinit var options: SentryOptions + private lateinit var collector: ThreadSampleCollector + private lateinit var interval: HangInterval + + @BeforeTest + fun setUp() { + cacheDir = Files.createTempDirectory("thread-sample-collector").toFile() + // Rotation moves an existing file once per process, do it before any file is seeded. + AnrProfileRotationHelper.getFileForRecording(cacheDir) + ticker = TestMonotonicTicker(HOURS.toNanos(30)) + epochClock = EpochClock { Timestamp.ofEpochNanos(MILLISECONDS.toNanos(unixTimeMs)) } + options = SentryOptions() + options.cacheDirPath = cacheDir.absolutePath + collector = ThreadSampleCollector(options, epochClock, ticker, Thread.currentThread()) + } + + @AfterTest + fun tearDown() { + collector.close() + cacheDir.deleteRecursively() + } + + private fun nowMs(): Long = TimeUnit.NANOSECONDS.toMillis(ticker.tickNanos()) + + private fun startInterval() { + interval = HangInterval(nowMs()) + collector.onRecordingStarted(interval) + } + + private fun advance(ms: Long) { + ticker.advance(ms, MILLISECONDS) + unixTimeMs += ms + } + + private fun sampleEvery66Ms(count: Int) { + repeat(count) { + collector.onSample(interval) + advance(66) + } + } + + private fun file() = File(cacheDir, "anr_profile") + + private fun readFile(): List { + // The collector keeps the queue open, so release it before reading from a second manager. + collector.close() + AnrProfileManager(options, file()).use { + return it.load().stacks + } + } + + private fun seedFile(timestampMs: Long) { + AnrProfileManager(options, file()).use { it.add(AnrStackTrace(timestampMs, emptyArray())) } + } + + @Test + fun `samples stay in memory before 700ms`() { + startInterval() + sampleEvery66Ms(10) // 0..594 ms + + val builder = HangRecord.Builder(660, unixTimeMs) + collector.onRecordingStopped(interval, builder) + assertThat(builder.build().samples).hasSize(10) + assertThat(readFile()).isEmpty() + } + + @Test + fun `onRecordingStarted opens the profile file before the first sample`() { + startInterval() + + assertThat(file().exists()).isTrue() + } + + @Test + fun `at 700ms file is cleared then buffer and new samples are written`() { + seedFile(1) + startInterval() + sampleEvery66Ms(11) // 0..660 ms + assertThat(AnrProfileManager(options, file()).use { it.load().stacks }.map { it.timestampMs }) + .containsExactly(1L) + + collector.onSample(interval) // 726 ms: first one past the threshold + advance(66) + collector.onSample(interval) // 792 ms + + val timestamps = readFile().map { it.timestampMs } + assertThat(timestamps).hasSize(13) + assertThat(timestamps).doesNotContain(1L) + assertThat(timestamps).isInOrder() + assertThat(timestamps.first()).isEqualTo(1_700_000_000_000L) + } + + @Test + fun `stops sampling when the file is full but keeps samples`() { + startInterval() + sampleEvery66Ms(150) + + val builder = HangRecord.Builder(0, unixTimeMs) + collector.onRecordingStopped(interval, builder) + assertThat(builder.build().samples).hasSize(120) + val timestamps = readFile().map { it.timestampMs } + assertThat(timestamps).hasSize(120) + assertThat(timestamps.first()).isEqualTo(1_700_000_000_000L) + } + + @Test + fun `onRecordingStopped puts samples and thread state into record`() { + val parked = Thread { while (!Thread.currentThread().isInterrupted) LockSupport.park() } + parked.isDaemon = true + parked.start() + try { + val deadlineNanos = System.nanoTime() + TimeUnit.SECONDS.toNanos(10) + while (parked.state != Thread.State.WAITING && System.nanoTime() < deadlineNanos) { + Thread.yield() + } + assertThat(parked.state).isEqualTo(Thread.State.WAITING) + collector = ThreadSampleCollector(options, epochClock, ticker, parked) + startInterval() + collector.onSample(interval) + + val builder = HangRecord.Builder(66, unixTimeMs) + collector.onRecordingStopped(interval, builder) + val sample = builder.build().samples.single() + assertThat(sample.state).isEqualTo(Thread.State.WAITING) + assertThat(sample.timestampMs).isEqualTo(unixTimeMs) + assertThat(sample.stack).isNotEmpty() + } finally { + parked.interrupt() + parked.join() + } + } + + @Test + fun `onRecordingCanceled drops the samples in memory but keeps the file`() { + startInterval() + sampleEvery66Ms(12) + + collector.onRecordingCanceled() + + val builder = HangRecord.Builder(0, unixTimeMs) + collector.onRecordingStopped(interval, builder) + assertThat(builder.build().samples).isEmpty() + assertThat(readFile()).hasSize(12) + } + + @Test + fun `interval after onRecordingCanceled clears the file once`() { + startInterval() + sampleEvery66Ms(12) + collector.onRecordingCanceled() + startInterval() + sampleEvery66Ms(12) + + val timestamps = readFile().map { it.timestampMs } + assertThat(timestamps).hasSize(12) + assertThat(timestamps.first()).isAtLeast(1_700_000_000_000L + 12 * 66) + } + + @Test + fun `second interval clears the file once`() { + startInterval() + sampleEvery66Ms(12) + startInterval() + sampleEvery66Ms(12) + + val timestamps = readFile().map { it.timestampMs } + assertThat(timestamps).hasSize(12) + assertThat(timestamps.first()).isAtLeast(1_700_000_000_000L + 12 * 66) + } + + @Test + fun `close drops the samples in memory`() { + startInterval() + sampleEvery66Ms(5) + + collector.close() + + val builder = HangRecord.Builder(0, unixTimeMs) + collector.onRecordingStopped(interval, builder) + assertThat(builder.build().samples).isEmpty() + } + + @Test + fun `keeps samples in memory when cacheDirPath is null at onRecordingStarted`() { + options.cacheDirPath = null + startInterval() + options.cacheDirPath = cacheDir.absolutePath + sampleEvery66Ms(15) + + val builder = HangRecord.Builder(0, unixTimeMs) + collector.onRecordingStopped(interval, builder) + assertThat(builder.build().samples).hasSize(15) + assertThat(file().exists()).isFalse() + } +}