-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35bf6ee
commit 5f932c9
Showing
7 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
platform/jvm/capture/src/main/kotlin/io/bitdrift/capture/events/lifecycle/IWindowListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.bitdrift.capture.events.lifecycle | ||
|
||
import android.view.Window | ||
|
||
/** | ||
* Emits when a [android.view.Window] is available or removed | ||
*/ | ||
interface IWindowListener { | ||
/** | ||
* Reports when [android.view.Window] is available | ||
*/ | ||
fun onWindowAvailable(window: Window) | ||
|
||
/** | ||
* Reports when the current window is not around | ||
*/ | ||
fun onWindowRemoved() | ||
} |
74 changes: 74 additions & 0 deletions
74
...rm/jvm/capture/src/main/kotlin/io/bitdrift/capture/events/performance/JankStatsMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.bitdrift.capture.events.performance | ||
|
||
import android.view.Window | ||
import androidx.metrics.performance.FrameData | ||
import androidx.metrics.performance.JankStats | ||
import io.bitdrift.capture.LogLevel | ||
import io.bitdrift.capture.LogType | ||
import io.bitdrift.capture.LoggerImpl | ||
import io.bitdrift.capture.common.Runtime | ||
import io.bitdrift.capture.common.RuntimeFeature | ||
import io.bitdrift.capture.events.lifecycle.IWindowListener | ||
import io.bitdrift.capture.providers.toFields | ||
|
||
/** | ||
* Reports Jank Frames and its duration in ms | ||
* | ||
* This will be a no-op when `client_feature.android.jank_stats_reporting` kill switch is disabled | ||
*/ | ||
internal class JankStatsMonitor( | ||
private val logger: LoggerImpl, | ||
runtime: Runtime, | ||
) : IWindowListener, | ||
JankStats.OnFrameListener { | ||
private val shouldDisableMonitorViaKillSwitch = !runtime.isEnabled(RuntimeFeature.JANK_STATS_EVENTS) | ||
|
||
private var jankStats: JankStats? = null | ||
private var currentWindow: Window? = null | ||
|
||
override fun onFrame(volatileFrameData: FrameData) { | ||
if (volatileFrameData.shouldReportJankyFrame()) { | ||
sendJankStatData(volatileFrameData) | ||
} | ||
} | ||
|
||
/** | ||
* Called when we have a new window available | ||
*/ | ||
override fun onWindowAvailable(window: Window) { | ||
if (shouldDisableMonitorViaKillSwitch || currentWindow == window) { | ||
return | ||
} | ||
currentWindow = window | ||
jankStats = JankStats.createAndTrack(window, this) | ||
} | ||
|
||
/** | ||
* Called when Application is Stopped/Destroyed | ||
*/ | ||
override fun onWindowRemoved() { | ||
if (shouldDisableMonitorViaKillSwitch) { | ||
return | ||
} | ||
jankStats?.isTrackingEnabled = false | ||
jankStats = null | ||
} | ||
|
||
private fun sendJankStatData(frameData: FrameData) { | ||
val message = "$ROW_MESSAGE ${frameData.durationToMilli()} ms" | ||
val fields = mapOf(JANKY_FRAME_DURATION_KEY to "${frameData.durationToMilli()}") | ||
logger.log(LogType.LIFECYCLE, LogLevel.WARNING, fields.toFields()) { message } | ||
} | ||
|
||
private fun FrameData.durationToMilli(): Long = this.frameDurationUiNanos / TO_MILLI | ||
|
||
private fun FrameData.shouldReportJankyFrame(): Boolean = | ||
this.isJank && this.durationToMilli() >= MIN_THRESHOLD_JANKY_FRAME_DURATION_IN_MILLI | ||
|
||
private companion object { | ||
private const val TO_MILLI = 1000000L | ||
private const val MIN_THRESHOLD_JANKY_FRAME_DURATION_IN_MILLI = 16L | ||
private const val JANKY_FRAME_DURATION_KEY = "_jank_frame_duration" | ||
private const val ROW_MESSAGE = "Frozen frame with a duration of" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters