From 4b44ad30a010d7503f53cacdae37940cef2471d2 Mon Sep 17 00:00:00 2001 From: Songchun Fan Date: Fri, 22 Apr 2022 17:44:50 +0000 Subject: [PATCH] [AppsFilter] skip recompute mQueriesViaComponent for snapshot With lazy recomputeComponentVisibility, it's possible that snapshots would need to do the recomputation and this leads to duplicated work because the changes are not reflected in other snapshots. BUG: 228712570 Test: tradefed.sh run google/continuous/boottime-successive Test: saves about 200ms-500ms total boot time on my local build Test: saves about 300ms on forrest boot time test Change-Id: I533b99f0e10ae68594df08b4cdd67b908ac4ef89 --- .../com/android/server/pm/AppsFilterImpl.java | 61 ++++++++++++++++--- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/pm/AppsFilterImpl.java b/services/core/java/com/android/server/pm/AppsFilterImpl.java index c447880e75e9b..f757405645b98 100644 --- a/services/core/java/com/android/server/pm/AppsFilterImpl.java +++ b/services/core/java/com/android/server/pm/AppsFilterImpl.java @@ -189,6 +189,8 @@ public class AppsFilterImpl implements AppsFilterSnapshot, Watchable, Snappable private final Object mCacheLock = new Object(); + private final boolean mIsSnapshot; + /** * This structure maps uid -> uid and indicates whether access from the first should be * filtered to the second. It's essentially a cache of the @@ -322,6 +324,7 @@ public class AppsFilterImpl implements AppsFilterSnapshot, Watchable, Snappable mProtectedBroadcasts, mProtectedBroadcasts, "AppsFilter.mProtectedBroadcasts"); mSnapshot = makeCache(); + mIsSnapshot = false; } /** @@ -360,6 +363,7 @@ public class AppsFilterImpl implements AppsFilterSnapshot, Watchable, Snappable mBackgroundExecutor = null; mSnapshot = new SnapshotCache.Sealed<>(); mSystemReady = orig.mSystemReady; + mIsSnapshot = true; } /** @@ -1136,6 +1140,7 @@ public class AppsFilterImpl implements AppsFilterSnapshot, Watchable, Snappable } } mQueriesViaComponentRequireRecompute = false; + onChanged(); } /** @@ -1405,6 +1410,7 @@ public class AppsFilterImpl implements AppsFilterSnapshot, Watchable, Snappable } } + @SuppressWarnings("GuardedBy") private boolean shouldFilterApplicationInternal(PackageDataSnapshot snapshot, int callingUid, Object callingSetting, PackageStateInternal targetPkgSetting, int targetUserId) { if (DEBUG_TRACING) { @@ -1563,16 +1569,53 @@ public class AppsFilterImpl implements AppsFilterSnapshot, Watchable, Snappable if (DEBUG_TRACING) { Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "mQueriesViaComponent"); } - if (mQueriesViaComponentRequireRecompute) { - recomputeComponentVisibility(snapshot.getPackageStates()); - onChanged(); - } - synchronized (mLock) { - if (mQueriesViaComponent.contains(callingAppId, targetAppId)) { - if (DEBUG_LOGGING) { - log(callingSetting, targetPkgSetting, "queries component"); + if (!mQueriesViaComponentRequireRecompute) { + synchronized (mLock) { + if (mQueriesViaComponent.contains(callingAppId, targetAppId)) { + if (DEBUG_LOGGING) { + log(callingSetting, targetPkgSetting, "queries component"); + } + return false; + } + } + } else { // mQueriesViaComponent is stale + if (!mIsSnapshot) { + // Only recompute mQueriesViaComponent if not in snapshot + recomputeComponentVisibility(snapshot.getPackageStates()); + synchronized (mLock) { + if (mQueriesViaComponent.contains(callingAppId, targetAppId)) { + if (DEBUG_LOGGING) { + log(callingSetting, targetPkgSetting, "queries component"); + } + return false; + } + } + } else { + // Do no recompute or use mQueriesViaComponent if it's stale in snapshot + // Since we know we are in the snapshot, no need to acquire mLock because + // mProtectedBroadcasts will not change + if (callingPkgSetting != null) { + if (callingPkgSetting.getPkg() != null + && canQueryViaComponents(callingPkgSetting.getPkg(), targetPkg, + mProtectedBroadcasts)) { + if (DEBUG_LOGGING) { + log(callingSetting, targetPkgSetting, "queries component"); + } + return false; + } + } else { + for (int i = callingSharedPkgSettings.size() - 1; i >= 0; i--) { + final AndroidPackage pkg = + callingSharedPkgSettings.valueAt(i).getPkg(); + if (pkg != null && canQueryViaComponents(pkg, targetPkg, + mProtectedBroadcasts)) { + if (DEBUG_LOGGING) { + log(callingSetting, targetPkgSetting, "queries component"); + } + return false; + } + } } - return false; } } } finally {