Move startup perf measurements to a background thread

This is required to avoid loading the Glean core library (provided by libxul) early on.
Fenix needs to do the time measurement itself for now.
This commit is contained in:
Jan-Erik Rediger 2022-07-22 13:14:17 +02:00 committed by mergify[bot]
parent 1a2b357119
commit 06488cdabe
2 changed files with 22 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import android.annotation.SuppressLint
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.StrictMode
import android.os.SystemClock
import android.util.Log.INFO
import androidx.annotation.CallSuper
import androidx.annotation.VisibleForTesting
@ -113,8 +114,8 @@ open class FenixApplication : LocaleAwareApplication(), Provider {
private set
override fun onCreate() {
// We use start/stop instead of measure so we don't measure outside the main process.
val completeMethodDurationTimerId = PerfStartup.applicationOnCreate.start() // DO NOT MOVE ANYTHING ABOVE HERE.
// We measure ourselves to avoid a call into Glean before its loaded.
val start = SystemClock.elapsedRealtimeNanos()
super.onCreate()
@ -134,8 +135,16 @@ open class FenixApplication : LocaleAwareApplication(), Provider {
setupInMainProcessOnly()
downloadWallpapers()
// DO NOT MOVE ANYTHING BELOW THIS stop CALL.
PerfStartup.applicationOnCreate.stopAndAccumulate(completeMethodDurationTimerId)
// DO NOT MOVE ANYTHING BELOW THIS elapsedRealtimeNanos CALL.
val stop = SystemClock.elapsedRealtimeNanos()
val durationMillis = TimeUnit.NANOSECONDS.toMillis(stop - start)
// We avoid blocking the main thread on startup by calling into Glean on the background thread.
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(Dispatchers.IO) {
PerfStartup.applicationOnCreate.accumulateSamples(listOf(durationMillis))
}
}
@OptIn(DelicateCoroutinesApi::class) // GlobalScope usage

View File

@ -10,6 +10,10 @@ import androidx.annotation.VisibleForTesting.PRIVATE
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.GleanMetrics.PerfStartup
import org.mozilla.fenix.HomeActivity
@ -69,8 +73,11 @@ class StartupTypeTelemetry(
val startupPath = startupPathProvider.startupPathForActivity
val label = getTelemetryLabel(startupState, startupPath)
PerfStartup.startupType[label].add(1)
logger.info("Recorded start up: $label")
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(Dispatchers.IO) {
PerfStartup.startupType[label].add(1)
logger.info("Recorded start up: $label")
}
}
@VisibleForTesting(otherwise = PRIVATE)