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
9 changes: 9 additions & 0 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ public final class io/sentry/android/core/hang/HangRecorder : io/sentry/android/
public static final field DEFAULT_EXECUTOR_FACTORY Lio/sentry/android/core/hang/HangRecorder$ExecutorFactory;
public static final field DEFAULT_MAX_RECORDING_TIME_MS J
public static final field DEFAULT_POLLING_INTERVAL_MS J
public static final field DEFAULT_SUSPICIOUS_THRESHOLD_MS J
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/android/core/AppStateProvider;Lio/sentry/time/MonotonicTicker;Lio/sentry/time/EpochClock;JJLio/sentry/android/core/hang/HangRecorder$ExecutorFactory;Lio/sentry/android/core/hang/HangRecorder$SignalFactory;Ljava/util/List;)V
public fun addTickListener (Lio/sentry/android/core/hang/Metronome$TickListener;)V
public fun close ()V
Expand Down Expand Up @@ -890,6 +891,14 @@ public abstract interface class io/sentry/android/core/hang/Metronome$TickListen
public abstract fun onMetronomeTick (J)V
}

public final class io/sentry/android/core/hang/ThreadHangSignal : io/sentry/android/core/hang/HangSignal, io/sentry/android/core/hang/Metronome$TickListener {
public fun <init> (Lio/sentry/android/core/hang/Metronome;Lio/sentry/time/MonotonicTicker;Lio/sentry/IDebugManager;Lio/sentry/android/core/hang/ThreadPoster;J)V
public fun close ()V
public fun onMetronomeTick (J)V
public fun start (Lio/sentry/android/core/hang/HangSignalListener;)V
public fun stop ()V
}

public abstract interface class io/sentry/android/core/hang/ThreadPoster {
public abstract fun post (Ljava/lang/Runnable;)V
public abstract fun remove (Ljava/lang/Runnable;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class HangRecorder implements Metronome, Closeable, AppState.AppSta

public static final long DEFAULT_POLLING_INTERVAL_MS = 66;
public static final long DEFAULT_MAX_RECORDING_TIME_MS = 8_000;
public static final long DEFAULT_SUSPICIOUS_THRESHOLD_MS = 300;

/**
* Creates the executor of the recorder thread. It must run one task at a time on one thread, in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.sentry.android.core.hang;

import io.sentry.IDebugManager;
import io.sentry.time.MonotonicTicker;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Reports {@link HangSignal.State#SUSPICIOUS} when a probe posted to the watched thread did not run
* for more than the suspicious threshold, and {@link HangSignal.State#IDLE} when it ran. The
* watched thread is the thread that the {@link ThreadPoster} posts to.
*
* <p>Only the probe runs on the watched thread. Everything else runs on the thread that calls the
* ticks of the {@link Metronome}.
*/
@ApiStatus.Internal
public final class ThreadHangSignal implements HangSignal, Metronome.TickListener {

private final @NotNull Metronome metronome;
private final @NotNull MonotonicTicker ticker;
private final @NotNull IDebugManager debugManager;
private final @NotNull ThreadPoster poster;
private final long suspiciousThresholdMs;

private volatile long probeRunMs;

private final @NotNull Runnable probe = () -> probeRunMs = nowMs();

private @Nullable HangSignalListener listener;
private long probeFloorMs;
private boolean suspicious;
private long suspiciousSinceMs;

public ThreadHangSignal(
final @NotNull Metronome metronome,
final @NotNull MonotonicTicker ticker,
final @NotNull IDebugManager debugManager,
final @NotNull ThreadPoster poster,
final long suspiciousThresholdMs) {
this.metronome = metronome;
this.ticker = ticker;
this.debugManager = debugManager;
this.poster = poster;
this.suspiciousThresholdMs = suspiciousThresholdMs;
}

@Override
public void start(final @NotNull HangSignalListener listener) {
this.listener = listener;
suspicious = false;
// The probe may not have run since the last stop, so its time would read as a hang.
probeFloorMs = nowMs();
metronome.addTickListener(this);
}

@Override
public void stop() {
metronome.removeTickListener(this);
poster.remove(probe);
listener = null;
suspicious = false;
}

@Override
public void close() {
stop();
}

@Override
public void onMetronomeTick(final long nowMs) {
check(nowMs);
poster.remove(probe);
poster.post(probe);
}

private void check(final long nowMs) {
final @Nullable HangSignalListener currentListener = listener;
if (currentListener == null) {
return;
}

// A breakpoint is not a hang.
if (debugManager.isDebuggerConnected()) {
probeFloorMs = nowMs;
if (suspicious) {
suspicious = false;
currentListener.onSignalCanceled(this);
}
return;
}

final long lastProbeMs = Math.max(probeRunMs, probeFloorMs);
if (suspicious) {
if (lastProbeMs > suspiciousSinceMs) {
suspicious = false;
currentListener.onSignalChanged(this, State.IDLE, nowMs - lastProbeMs);
}
} else if (nowMs - lastProbeMs > suspiciousThresholdMs) {
suspicious = true;
suspiciousSinceMs = lastProbeMs;
currentListener.onSignalChanged(this, State.SUSPICIOUS, nowMs - lastProbeMs);
}
}

private long nowMs() {
return TimeUnit.NANOSECONDS.toMillis(ticker.tickNanos());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import io.sentry.android.core.hang.HangRecorder.DEFAULT_POLLING_INTERVAL_MS
import io.sentry.test.DeferredExecutorService
import io.sentry.test.NonOverridableNoOpSentryExecutorService
import io.sentry.time.EpochClock
import io.sentry.time.JavaMonotonicTicker
import io.sentry.time.TestMonotonicTicker
import io.sentry.time.Timestamp
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MILLISECONDS
Expand Down Expand Up @@ -665,6 +667,48 @@ class HangRecorderTest {
assertThat(collector.calls).containsExactly("close")
}

@Test
fun `recorder on the default executor posts the probe of a thread signal`() {
val posted = CountDownLatch(1)
val poster =
object : ThreadPoster {
override fun post(runnable: Runnable) = posted.countDown()

override fun remove(runnable: Runnable) = Unit
}
appStateProvider.foreground()
val created =
HangRecorder(
options,
appStateProvider,
JavaMonotonicTicker.getInstance(),
epochClock,
DEFAULT_POLLING_INTERVAL_MS,
DEFAULT_MAX_RECORDING_TIME_MS,
HangRecorder.DEFAULT_EXECUTOR_FACTORY,
{ metronome ->
listOf(
ThreadHangSignal(
metronome,
JavaMonotonicTicker.getInstance(),
{ false },
poster,
HangRecorder.DEFAULT_SUSPICIOUS_THRESHOLD_MS,
),
)
},
emptyList(),
)

created.subscribe(listener)

try {
assertThat(posted.await(10, TimeUnit.SECONDS)).isTrue()
} finally {
created.close()
}
}

private class FakeAppStateProvider : AppStateProvider {
val listeners = mutableListOf<AppState.AppStateListener>()
private var inBackground: Boolean? = null
Expand Down
Loading
Loading