diff options
author | Amith Yamasani <yamasani@google.com> | 2020-05-29 15:20:18 -0700 |
---|---|---|
committer | Amith Yamasani <yamasani@google.com> | 2020-06-18 16:43:25 -0700 |
commit | 62b19914af318a3ed2b9cce329b8fd83ef4acbef (patch) | |
tree | df8a41e096defb70872d49ae61ea623a0e99af3c /apex/jobscheduler | |
parent | cd259caf54f903245a98eda557af147103f9bc87 (diff) |
Speed up NetworkPolicy thread during user creation
Network Policy Manager makes a ton of calls to
UsageStats.isAppIdle, which in turn tries to get
the network scorer and results in a lot of calls to
PackageManager. Caching the network scorer reduces a
lot of the lock contention with PackageManager.
Also, caching calls to check for an app's internet
permission.
Handler for USER_ADDED broadcast reduced in wallclock
duration from 2+ seconds to 114ms.
Bug: 156582823
Test: atest UserLifecycleTests#createAndStartUser
Test: atest AppStandbyTests
Change-Id: I369f9c129ca8a13016e00b1bec2776111fa04513
Diffstat (limited to 'apex/jobscheduler')
-rw-r--r-- | apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java b/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java index 280a6870a5e1..8b5602bc7cd9 100644 --- a/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java +++ b/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java @@ -90,6 +90,7 @@ import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; +import android.os.Trace; import android.os.UserHandle; import android.provider.Settings.Global; import android.telephony.TelephonyManager; @@ -238,6 +239,14 @@ public class AppStandbyController implements AppStandbyInternal { private final CountDownLatch mAdminDataAvailableLatch = new CountDownLatch(1); + // Cache the active network scorer queried from the network scorer service + private volatile String mCachedNetworkScorer = null; + // The last time the network scorer service was queried + private volatile long mCachedNetworkScorerAtMillis = 0L; + // How long before querying the network scorer again. During this time, subsequent queries will + // get the cached value + private static final long NETWORK_SCORER_CACHE_DURATION_MILLIS = 5000L; + // Messages for the handler static final int MSG_INFORM_LISTENERS = 3; static final int MSG_FORCE_IDLE_STATE = 4; @@ -1154,6 +1163,8 @@ public class AppStandbyController implements AppStandbyInternal { return new int[0]; } + Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "getIdleUidsForUser"); + final long elapsedRealtime = mInjector.elapsedRealtime(); List<ApplicationInfo> apps; @@ -1189,6 +1200,7 @@ public class AppStandbyController implements AppStandbyInternal { uidStates.setValueAt(index, value + 1 + (idle ? 1<<16 : 0)); } } + if (DEBUG) { Slog.d(TAG, "getIdleUids took " + (mInjector.elapsedRealtime() - elapsedRealtime)); } @@ -1210,6 +1222,8 @@ public class AppStandbyController implements AppStandbyInternal { } } + Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); + return res; } @@ -1576,8 +1590,16 @@ public class AppStandbyController implements AppStandbyInternal { } private boolean isActiveNetworkScorer(String packageName) { - String activeScorer = mInjector.getActiveNetworkScorer(); - return packageName != null && packageName.equals(activeScorer); + // Validity of network scorer cache is limited to a few seconds. Fetch it again + // if longer since query. + // This is a temporary optimization until there's a callback mechanism for changes to network scorer. + final long now = SystemClock.elapsedRealtime(); + if (mCachedNetworkScorer == null + || mCachedNetworkScorerAtMillis < now - NETWORK_SCORER_CACHE_DURATION_MILLIS) { + mCachedNetworkScorer = mInjector.getActiveNetworkScorer(); + mCachedNetworkScorerAtMillis = now; + } + return packageName != null && packageName.equals(mCachedNetworkScorer); } private void informListeners(String packageName, int userId, int bucket, int reason, |