OSDN Git Service

Wire up zram setting, system property
authorDaniel Colascione <dancol@google.com>
Tue, 9 Jan 2018 03:10:36 +0000 (19:10 -0800)
committerDaniel Colascione <dancol@google.com>
Thu, 11 Jan 2018 13:37:24 +0000 (05:37 -0800)
The experiment framework toggles the system setting, which manipulates
the system property that init reads during boot to determine whether
we should enable zram.

Test: manual
Change-Id: I4b4f1b1673a5aa5e7f721c7dec7ebe3ea7908af2

core/java/android/provider/Settings.java
core/proto/android/providers/settings.proto
core/tests/coretests/src/android/provider/SettingsBackupTest.java
packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java
services/core/java/com/android/server/StorageManagerService.java

index 009fc39..4e34240 100644 (file)
@@ -11221,6 +11221,15 @@ public final class Settings {
          */
         public static final String ENABLE_GNSS_RAW_MEAS_FULL_TRACKING =
                 "enable_gnss_raw_meas_full_tracking";
+
+        /**
+         * Whether we've enabled zram on this device. Takes effect on
+         * reboot. The value "1" enables zram; "0" disables it, and
+         * everything else is unspecified.
+         * @hide
+         */
+        public static final String ZRAM_ENABLED =
+                "zram_enabled";
     }
 
     /**
index fb0ebed..79c2942 100644 (file)
@@ -388,8 +388,9 @@ message GlobalSettingsProto {
     optional SettingProto enable_deletion_helper_no_threshold_toggle = 340;
     optional SettingProto notification_snooze_options = 341;
     optional SettingProto enable_gnss_raw_meas_full_tracking = 346;
+    optional SettingProto zram_enabled = 347;
 
-    // Next tag = 347;
+    // Next tag = 348;
 }
 
 message SecureSettingsProto {
index fc86500..c37ee5c 100644 (file)
@@ -418,7 +418,8 @@ public class SettingsBackupTest {
                     Settings.Global.WTF_IS_FATAL,
                     Settings.Global.ZEN_MODE,
                     Settings.Global.ZEN_MODE_CONFIG_ETAG,
-                    Settings.Global.ZEN_MODE_RINGER_LEVEL);
+                    Settings.Global.ZEN_MODE_RINGER_LEVEL,
+                    Settings.Global.ZRAM_ENABLED);
 
     private static final Set<String> BACKUP_BLACKLISTED_SECURE_SETTINGS =
              newHashSet(
index 1d3f26e..4e0c0ab 100644 (file)
@@ -1119,6 +1119,9 @@ class SettingsProtoDumpUtil {
         dumpSetting(s, p,
                 Settings.Global.NOTIFICATION_SNOOZE_OPTIONS,
                 GlobalSettingsProto.NOTIFICATION_SNOOZE_OPTIONS);
+        dumpSetting(s, p,
+                    Settings.Global.ZRAM_ENABLED,
+                    GlobalSettingsProto.ZRAM_ENABLED);
     }
 
     /** Dump a single {@link SettingsState.Setting} to a proto buf */
index dc2f2a5..7361e70 100644 (file)
@@ -49,6 +49,7 @@ import android.content.pm.ProviderInfo;
 import android.content.pm.UserInfo;
 import android.content.res.Configuration;
 import android.content.res.ObbInfo;
+import android.database.ContentObserver;
 import android.net.TrafficStats;
 import android.net.Uri;
 import android.os.Binder;
@@ -170,6 +171,10 @@ class StorageManagerService extends IStorageManager.Stub
     // Static direct instance pointer for the tightly-coupled idle service to use
     static StorageManagerService sSelf = null;
 
+    /* Read during boot to decide whether to enable zram when available */
+    private static final String ZRAM_ENABLED_PROPERTY =
+        "persist.sys.zram_enabled";
+
     public static class Lifecycle extends SystemService {
         private StorageManagerService mStorageManagerService;
 
@@ -733,6 +738,41 @@ class StorageManagerService extends IStorageManager.Stub
 
         // Start scheduling nominally-daily fstrim operations
         MountServiceIdler.scheduleIdlePass(mContext);
+
+        // Toggle zram-enable system property in response to settings
+        mContext.getContentResolver().registerContentObserver(
+            Settings.Global.getUriFor(Settings.Global.ZRAM_ENABLED),
+            false /*notifyForDescendants*/,
+            new ContentObserver(null /* current thread */) {
+                @Override
+                public void onChange(boolean selfChange) {
+                    refreshZramSettings();
+                }
+            });
+        refreshZramSettings();
+    }
+
+    /**
+     * Update the zram_enabled system property (which init reads to
+     * decide whether to enable zram) to reflect the zram_enabled
+     * preference (which we can change for experimentation purposes).
+     */
+    private void refreshZramSettings() {
+        String propertyValue = SystemProperties.get(ZRAM_ENABLED_PROPERTY);
+        if ("".equals(propertyValue)) {
+            return;  // System doesn't have zram toggling support
+        }
+        String desiredPropertyValue =
+            Settings.Global.getInt(mContext.getContentResolver(),
+                                   Settings.Global.ZRAM_ENABLED,
+                                   1) != 0
+            ? "1" : "0";
+        if (!desiredPropertyValue.equals(propertyValue)) {
+            // Avoid redundant disk writes by setting only if we're
+            // changing the property value. There's no race: we're the
+            // sole writer.
+            SystemProperties.set(ZRAM_ENABLED_PROPERTY, desiredPropertyValue);
+        }
     }
 
     /**