diff options
| author | 2024-01-02 22:54:04 +0000 | |
|---|---|---|
| committer | 2024-01-03 16:53:41 +0000 | |
| commit | b6f3f88440f394a5168578b13683b03b55ee85ab (patch) | |
| tree | 0d7e7f2aa6a93183f1cd3ac680ce100cb101e46d | |
| parent | 2619e6708b63a746e66b5cae0afb7e55c3bfd4fa (diff) | |
Cache StrictMode handle to IStorageManager
Certain StrictMode paths can result in repeated and frequent queries
to IStorageManager.isCeStorageUnlocked. Avoid associated overhead by
caching the IStorageManager handle when necessary.
Note that StorageManager.isCeStorageUnlocked has similar handle caching
behavior. We could simply reuse that method, however, it has slightly
different semantics when the query fails (for StrictMode, we simply
ignore those failures).
Bug: 318403642
Test: presubmit
Change-Id: Ibe52808b314e8f8044d423761d99df680ce2c8e4
| -rw-r--r-- | core/java/android/os/StrictMode.java | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java index 94f90ccb2d9d..f13e89e97823 100644 --- a/core/java/android/os/StrictMode.java +++ b/core/java/android/os/StrictMode.java @@ -2373,13 +2373,29 @@ public final class StrictMode { /** Assume locked until we hear otherwise */ private static volatile boolean sCeStorageUnlocked = false; + /** + * Avoid (potentially) costly and repeated lookups to the same mount service. + * Note that we don't use the Singleton wrapper as lookup may fail early during boot. + */ + private static volatile IStorageManager sStorageManager; + private static boolean isCeStorageUnlocked(int userId) { - final IStorageManager storage = IStorageManager.Stub + IStorageManager storage = sStorageManager; + if (storage == null) { + storage = IStorageManager.Stub .asInterface(ServiceManager.getService("mount")); + // As the queried handle may be null early during boot, only stash valid handles, + // avoiding races with concurrent service queries. + if (storage != null) { + sStorageManager = storage; + } + } if (storage != null) { try { return storage.isCeStorageUnlocked(userId); } catch (RemoteException ignored) { + // Conservatively clear the ref, allowing refresh if the remote process restarts. + sStorageManager = null; } } return false; |