From 40d5446c4be5c5901e28de199cdd739d7739ac4a Mon Sep 17 00:00:00 2001 From: t-p-white Date: Wed, 21 Jun 2023 15:54:09 +0100 Subject: [PATCH] Bug 1840341 - Add memory telemetry for experiment --- app/metrics.yaml | 18 ++++++++++++ .../org/mozilla/fenix/FenixApplication.kt | 28 +++++++++++++++++++ .../org/mozilla/fenix/FenixApplicationTest.kt | 9 +++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app/metrics.yaml b/app/metrics.yaml index 3e95597ca..ac008fceb 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -2434,6 +2434,24 @@ metrics: metadata: tags: - Notifications + ram_more_than_threshold: + type: boolean + lifetime: application + description: True if the device's asserted 'advertised' RAM is more than the given threshold. + send_in_pings: + - metrics + bugs: + - https://bugzilla.mozilla.org/show_bug.cgi?id=1840341 + data_reviews: + - https://github.com/mozilla-mobile/firefox-android/pull/2620 + data_sensitivity: + - technical + notification_emails: + - android-probes@mozilla.com + expires: 128 + metadata: + tags: + - Experiments customize_home: most_visited_sites: diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index d87bf8307..ed9424100 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -5,6 +5,8 @@ package org.mozilla.fenix import android.annotation.SuppressLint +import android.app.ActivityManager +import android.content.Context import android.net.Uri import android.os.Build import android.os.Build.VERSION.SDK_INT @@ -109,6 +111,19 @@ import org.mozilla.fenix.wallpapers.Wallpaper import java.util.UUID import java.util.concurrent.TimeUnit +/** + * The actual RAM threshold is 2GB. + * + * To enable simpler reporting, we want to use the device's 'advertised' RAM. + * As [ActivityManager.MemoryInfo.totalMem] is not the device's 'advertised' RAM spec & we cannot + * access [ActivityManager.MemoryInfo.advertisedMem] across all Android versions, we will use a + * proxy value of 1.6GB. This is based on 1.5GB with a small 'excess' buffer. We assert that all + * values above this proxy value are 2GB or more. + */ +private const val RAM_THRESHOLD_PROXY_GB = 1.6F + +private const val RAM_THRESHOLD_BYTES = RAM_THRESHOLD_PROXY_GB * (1e+9).toLong() + /** *The main application class for Fenix. Records data to measure initialization performance. * Installs [CrashReporter], initializes [Glean] in fenix builds and setup Megazord in the main process. @@ -689,6 +704,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { settings: Settings, browsersCache: BrowsersCache = BrowsersCache, mozillaProductDetector: MozillaProductDetector = MozillaProductDetector, + isDeviceRamAboveThreshold: Boolean = isDeviceRamAboveThreshold(), ) { setPreferenceMetrics(settings) with(Metrics) { @@ -786,6 +802,8 @@ open class FenixApplication : LocaleAwareApplication(), Provider { marketingNotificationAllowed.set( notificationManagerCompat.isNotificationChannelEnabled(MARKETING_CHANNEL_ID), ) + + ramMoreThanThreshold.set(isDeviceRamAboveThreshold) } with(AndroidAutofill) { @@ -826,6 +844,16 @@ open class FenixApplication : LocaleAwareApplication(), Provider { } } + private fun deviceRamBytes(): Long { + val memoryInfo = ActivityManager.MemoryInfo() + val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + activityManager.getMemoryInfo(memoryInfo) + + return memoryInfo.totalMem + } + + private fun isDeviceRamAboveThreshold() = deviceRamBytes() > RAM_THRESHOLD_BYTES + @Suppress("ComplexMethod") private fun setPreferenceMetrics( settings: Settings, diff --git a/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt b/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt index a9c8fd65e..984546cc3 100644 --- a/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt +++ b/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt @@ -148,7 +148,13 @@ class FenixApplicationTest { assertTrue(settings.contileContextId.isEmpty()) assertNull(TopSites.contextId.testGetValue()) - application.setStartupMetrics(browserStore, settings, browsersCache, mozillaProductDetector) + application.setStartupMetrics( + browserStore = browserStore, + settings = settings, + browsersCache = browsersCache, + mozillaProductDetector = mozillaProductDetector, + isDeviceRamAboveThreshold = true, + ) // Verify that browser defaults metrics are set. assertEquals("Mozilla", Metrics.distributionId.testGetValue()) @@ -186,6 +192,7 @@ class FenixApplicationTest { assertEquals(true, Preferences.inactiveTabsEnabled.testGetValue()) assertEquals(expectedAppInstallSource, Metrics.installSource.testGetValue()) assertEquals(true, Metrics.defaultWallpaper.testGetValue()) + assertEquals(true, Metrics.ramMoreThanThreshold.testGetValue()) val contextId = TopSites.contextId.testGetValue()!!.toString()