diff options
| author | John Reck <jreck@google.com> | 2016-09-20 14:24:21 -0700 |
|---|---|---|
| committer | John Reck <jreck@google.com> | 2016-09-21 16:10:54 -0700 |
| commit | aa67f684ff43c81e3280c846245ec6ebe907787e (patch) | |
| tree | 242c5cfecd604584ce8c70dabe46b92d3bce43e3 /core/java/android/os/SystemProperties.java | |
| parent | d8a53abd804d49a10971a5ecb4e025fe10808bb6 (diff) | |
Fix a bunch of repeated reads of a ro.* property
SystemProperties.get() is not particularly fast,
especially if a string is returned. Since ro.* values
are unable to be changed, there's no need to
continously re-query them. Cache the value at
static init time to trivially fix this.
Test: refactoring CL.
Change-Id: Iccb021d3cb2ba3a4a1d0048ddec6811bb7409eec
Diffstat (limited to 'core/java/android/os/SystemProperties.java')
| -rw-r--r-- | core/java/android/os/SystemProperties.java | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/core/java/android/os/SystemProperties.java b/core/java/android/os/SystemProperties.java index 1479035df978..d31036c532c7 100644 --- a/core/java/android/os/SystemProperties.java +++ b/core/java/android/os/SystemProperties.java @@ -16,7 +16,13 @@ package android.os; +import android.util.Log; +import android.util.MutableInt; + +import com.android.internal.annotations.GuardedBy; + import java.util.ArrayList; +import java.util.HashMap; /** @@ -25,13 +31,45 @@ import java.util.ArrayList; * * {@hide} */ -public class SystemProperties -{ +public class SystemProperties { + private static final String TAG = "SystemProperties"; + private static final boolean TRACK_KEY_ACCESS = false; + public static final int PROP_NAME_MAX = 31; public static final int PROP_VALUE_MAX = 91; private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>(); + @GuardedBy("sRoReads") + private static final HashMap<String, MutableInt> sRoReads; + static { + if (TRACK_KEY_ACCESS) { + sRoReads = new HashMap<>(); + } else { + sRoReads = null; + } + } + + private static void onKeyAccess(String key) { + if (!TRACK_KEY_ACCESS) return; + + if (key != null && key.startsWith("ro.")) { + synchronized (sRoReads) { + MutableInt numReads = sRoReads.getOrDefault(key, null); + if (numReads == null) { + numReads = new MutableInt(0); + sRoReads.put(key, numReads); + } + numReads.value++; + if (numReads.value > 3) { + Log.d(TAG, "Repeated read (count=" + numReads.value + + ") of a read-only system property '" + key + "'", + new Exception()); + } + } + } + } + private static native String native_get(String key); private static native String native_get(String key, String def); private static native int native_get_int(String key, int def); @@ -49,6 +87,7 @@ public class SystemProperties if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } + if (TRACK_KEY_ACCESS) onKeyAccess(key); return native_get(key); } @@ -61,6 +100,7 @@ public class SystemProperties if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } + if (TRACK_KEY_ACCESS) onKeyAccess(key); return native_get(key, def); } @@ -76,6 +116,7 @@ public class SystemProperties if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } + if (TRACK_KEY_ACCESS) onKeyAccess(key); return native_get_int(key, def); } @@ -91,6 +132,7 @@ public class SystemProperties if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } + if (TRACK_KEY_ACCESS) onKeyAccess(key); return native_get_long(key, def); } @@ -111,6 +153,7 @@ public class SystemProperties if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } + if (TRACK_KEY_ACCESS) onKeyAccess(key); return native_get_boolean(key, def); } @@ -127,6 +170,7 @@ public class SystemProperties throw new IllegalArgumentException("val.length > " + PROP_VALUE_MAX); } + if (TRACK_KEY_ACCESS) onKeyAccess(key); native_set(key, val); } |
