Loading apex/jobscheduler/service/aconfig/device_idle.aconfig +10 −0 Original line number Diff line number Diff line Loading @@ -17,3 +17,13 @@ flag { description: "Disable wakelocks for background apps while Light Device Idle is active" bug: "326607666" } flag { name: "use_cpu_time_for_temp_allowlist" namespace: "backstage_power" description: "Use CPU time for temporary allowlists" bug: "376561328" metadata { purpose: PURPOSE_BUGFIX } } apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java +26 −6 Original line number Diff line number Diff line Loading @@ -620,8 +620,8 @@ public class DeviceIdleController extends SystemService * the network and acquire wakelocks. Times are in milliseconds. */ @GuardedBy("this") private final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes = new SparseArray<>(); @VisibleForTesting final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes = new SparseArray<>(); private NetworkPolicyManagerInternal mNetworkPolicyManagerInternal; Loading Loading @@ -1941,7 +1941,8 @@ public class DeviceIdleController extends SystemService private static final int MSG_REPORT_IDLE_ON_LIGHT = 3; private static final int MSG_REPORT_IDLE_OFF = 4; private static final int MSG_REPORT_ACTIVE = 5; private static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6; @VisibleForTesting static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6; @VisibleForTesting static final int MSG_REPORT_STATIONARY_STATUS = 7; private static final int MSG_FINISH_IDLE_OP = 8; Loading Loading @@ -2511,6 +2512,11 @@ public class DeviceIdleController extends SystemService return SystemClock.elapsedRealtime(); } /** Returns the current elapsed realtime in milliseconds. */ long getUptimeMillis() { return SystemClock.uptimeMillis(); } LocationManager getLocationManager() { if (mLocationManager == null) { mLocationManager = mContext.getSystemService(LocationManager.class); Loading Loading @@ -3264,7 +3270,8 @@ public class DeviceIdleController extends SystemService void addPowerSaveTempWhitelistAppDirectInternal(int callingUid, int uid, long duration, @TempAllowListType int tempAllowListType, boolean sync, @ReasonCode int reasonCode, @Nullable String reason) { final long timeNow = SystemClock.elapsedRealtime(); final long timeNow = Flags.useCpuTimeForTempAllowlist() ? mInjector.getUptimeMillis() : mInjector.getElapsedRealtime(); boolean informWhitelistChanged = false; int appId = UserHandle.getAppId(uid); synchronized (this) { Loading Loading @@ -3350,7 +3357,8 @@ public class DeviceIdleController extends SystemService } void checkTempAppWhitelistTimeout(int uid) { final long timeNow = SystemClock.elapsedRealtime(); final long timeNow = Flags.useCpuTimeForTempAllowlist() ? mInjector.getUptimeMillis() : mInjector.getElapsedRealtime(); final int appId = UserHandle.getAppId(uid); if (DEBUG) { Slog.d(TAG, "checkTempAppWhitelistTimeout: uid=" + uid + ", timeNow=" + timeNow); Loading Loading @@ -5219,6 +5227,17 @@ public class DeviceIdleController extends SystemService } } pw.println(" Flags:"); pw.print(" "); pw.print(Flags.FLAG_USE_CPU_TIME_FOR_TEMP_ALLOWLIST); pw.print("="); pw.println(Flags.useCpuTimeForTempAllowlist()); pw.print(" "); pw.print(Flags.FLAG_REMOVE_IDLE_LOCATION); pw.print("="); pw.println(Flags.removeIdleLocation()); pw.println(); synchronized (this) { mConstants.dump(pw); Loading Loading @@ -5449,7 +5468,8 @@ public class DeviceIdleController extends SystemService pw.println(" Temp whitelist schedule:"); prefix = " "; } final long timeNow = SystemClock.elapsedRealtime(); final long timeNow = Flags.useCpuTimeForTempAllowlist() ? mInjector.getUptimeMillis() : mInjector.getElapsedRealtime(); for (int i = 0; i < size; i++) { pw.print(prefix); pw.print("UID="); Loading services/core/java/com/android/server/am/ActivityManagerService.java +4 −2 Original line number Diff line number Diff line Loading @@ -11337,7 +11337,9 @@ public class ActivityManagerService extends IActivityManager.Stub } pw.println(" mFgsStartTempAllowList:"); final long currentTimeNow = System.currentTimeMillis(); final long elapsedRealtimeNow = SystemClock.elapsedRealtime(); final long tempAllowlistCurrentTime = com.android.server.deviceidle.Flags.useCpuTimeForTempAllowlist() ? SystemClock.uptimeMillis() : SystemClock.elapsedRealtime(); mFgsStartTempAllowList.forEach((uid, entry) -> { pw.print(" " + UserHandle.formatUid(uid) + ": "); entry.second.dump(pw); Loading @@ -11345,7 +11347,7 @@ public class ActivityManagerService extends IActivityManager.Stub // Convert entry.mExpirationTime, which is an elapsed time since boot, // to a time since epoch (i.e. System.currentTimeMillis()-based time.) final long expirationInCurrentTime = currentTimeNow - elapsedRealtimeNow + entry.first; currentTimeNow - tempAllowlistCurrentTime + entry.first; TimeUtils.dumpTimeWithDelta(pw, expirationInCurrentTime, currentTimeNow); pw.println(); }); Loading services/core/java/com/android/server/am/FgsTempAllowList.java +11 −6 Original line number Diff line number Diff line Loading @@ -29,7 +29,7 @@ import java.util.function.BiConsumer; /** * List of keys that have expiration time. * If the expiration time is less than current elapsedRealtime, the key has expired. * If the expiration time is less than current uptime, the key has expired. * Otherwise it is valid (or allowed). * * <p>This is used for both FGS-BG-start restriction, and FGS-while-in-use permissions check.</p> Loading @@ -42,7 +42,7 @@ public class FgsTempAllowList<E> { private static final int DEFAULT_MAX_SIZE = 100; /** * The value is Pair type, Pair.first is the expirationTime(an elapsedRealtime), * The value is Pair type, Pair.first is the expirationTime(in cpu uptime), * Pair.second is the optional information entry about this key. */ private final SparseArray<Pair<Long, E>> mTempAllowList = new SparseArray<>(); Loading Loading @@ -82,7 +82,9 @@ public class FgsTempAllowList<E> { } // The temp allowlist should be a short list with only a few entries in it. // for a very large list, HashMap structure should be used. final long now = SystemClock.elapsedRealtime(); final long now = com.android.server.deviceidle.Flags.useCpuTimeForTempAllowlist() ? SystemClock.uptimeMillis() : SystemClock.elapsedRealtime(); final int size = mTempAllowList.size(); if (size > mMaxSize) { Slog.w(TAG_AM, "FgsTempAllowList length:" + size + " exceeds maxSize" Loading Loading @@ -112,12 +114,15 @@ public class FgsTempAllowList<E> { final int index = mTempAllowList.indexOfKey(uid); if (index < 0) { return null; } else if (mTempAllowList.valueAt(index).first < SystemClock.elapsedRealtime()) { } final long timeNow = com.android.server.deviceidle.Flags.useCpuTimeForTempAllowlist() ? SystemClock.uptimeMillis() : SystemClock.elapsedRealtime(); if (mTempAllowList.valueAt(index).first < timeNow) { mTempAllowList.removeAt(index); return null; } else { return mTempAllowList.valueAt(index); } return mTempAllowList.valueAt(index); } } Loading services/tests/mockingservicestests/Android.bp +2 −2 Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. java_defaults { name: "FrameworkMockingServicesTests-jni-defaults", name: "FrameworksMockingServicesTests-jni-defaults", jni_libs: [ "libmockingservicestestjni", ], Loading @@ -30,7 +30,7 @@ package { android_test { name: "FrameworksMockingServicesTests", defaults: [ "FrameworkMockingServicesTests-jni-defaults", "FrameworksMockingServicesTests-jni-defaults", "modules-utils-testable-device-config-defaults", ], Loading Loading
apex/jobscheduler/service/aconfig/device_idle.aconfig +10 −0 Original line number Diff line number Diff line Loading @@ -17,3 +17,13 @@ flag { description: "Disable wakelocks for background apps while Light Device Idle is active" bug: "326607666" } flag { name: "use_cpu_time_for_temp_allowlist" namespace: "backstage_power" description: "Use CPU time for temporary allowlists" bug: "376561328" metadata { purpose: PURPOSE_BUGFIX } }
apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java +26 −6 Original line number Diff line number Diff line Loading @@ -620,8 +620,8 @@ public class DeviceIdleController extends SystemService * the network and acquire wakelocks. Times are in milliseconds. */ @GuardedBy("this") private final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes = new SparseArray<>(); @VisibleForTesting final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes = new SparseArray<>(); private NetworkPolicyManagerInternal mNetworkPolicyManagerInternal; Loading Loading @@ -1941,7 +1941,8 @@ public class DeviceIdleController extends SystemService private static final int MSG_REPORT_IDLE_ON_LIGHT = 3; private static final int MSG_REPORT_IDLE_OFF = 4; private static final int MSG_REPORT_ACTIVE = 5; private static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6; @VisibleForTesting static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6; @VisibleForTesting static final int MSG_REPORT_STATIONARY_STATUS = 7; private static final int MSG_FINISH_IDLE_OP = 8; Loading Loading @@ -2511,6 +2512,11 @@ public class DeviceIdleController extends SystemService return SystemClock.elapsedRealtime(); } /** Returns the current elapsed realtime in milliseconds. */ long getUptimeMillis() { return SystemClock.uptimeMillis(); } LocationManager getLocationManager() { if (mLocationManager == null) { mLocationManager = mContext.getSystemService(LocationManager.class); Loading Loading @@ -3264,7 +3270,8 @@ public class DeviceIdleController extends SystemService void addPowerSaveTempWhitelistAppDirectInternal(int callingUid, int uid, long duration, @TempAllowListType int tempAllowListType, boolean sync, @ReasonCode int reasonCode, @Nullable String reason) { final long timeNow = SystemClock.elapsedRealtime(); final long timeNow = Flags.useCpuTimeForTempAllowlist() ? mInjector.getUptimeMillis() : mInjector.getElapsedRealtime(); boolean informWhitelistChanged = false; int appId = UserHandle.getAppId(uid); synchronized (this) { Loading Loading @@ -3350,7 +3357,8 @@ public class DeviceIdleController extends SystemService } void checkTempAppWhitelistTimeout(int uid) { final long timeNow = SystemClock.elapsedRealtime(); final long timeNow = Flags.useCpuTimeForTempAllowlist() ? mInjector.getUptimeMillis() : mInjector.getElapsedRealtime(); final int appId = UserHandle.getAppId(uid); if (DEBUG) { Slog.d(TAG, "checkTempAppWhitelistTimeout: uid=" + uid + ", timeNow=" + timeNow); Loading Loading @@ -5219,6 +5227,17 @@ public class DeviceIdleController extends SystemService } } pw.println(" Flags:"); pw.print(" "); pw.print(Flags.FLAG_USE_CPU_TIME_FOR_TEMP_ALLOWLIST); pw.print("="); pw.println(Flags.useCpuTimeForTempAllowlist()); pw.print(" "); pw.print(Flags.FLAG_REMOVE_IDLE_LOCATION); pw.print("="); pw.println(Flags.removeIdleLocation()); pw.println(); synchronized (this) { mConstants.dump(pw); Loading Loading @@ -5449,7 +5468,8 @@ public class DeviceIdleController extends SystemService pw.println(" Temp whitelist schedule:"); prefix = " "; } final long timeNow = SystemClock.elapsedRealtime(); final long timeNow = Flags.useCpuTimeForTempAllowlist() ? mInjector.getUptimeMillis() : mInjector.getElapsedRealtime(); for (int i = 0; i < size; i++) { pw.print(prefix); pw.print("UID="); Loading
services/core/java/com/android/server/am/ActivityManagerService.java +4 −2 Original line number Diff line number Diff line Loading @@ -11337,7 +11337,9 @@ public class ActivityManagerService extends IActivityManager.Stub } pw.println(" mFgsStartTempAllowList:"); final long currentTimeNow = System.currentTimeMillis(); final long elapsedRealtimeNow = SystemClock.elapsedRealtime(); final long tempAllowlistCurrentTime = com.android.server.deviceidle.Flags.useCpuTimeForTempAllowlist() ? SystemClock.uptimeMillis() : SystemClock.elapsedRealtime(); mFgsStartTempAllowList.forEach((uid, entry) -> { pw.print(" " + UserHandle.formatUid(uid) + ": "); entry.second.dump(pw); Loading @@ -11345,7 +11347,7 @@ public class ActivityManagerService extends IActivityManager.Stub // Convert entry.mExpirationTime, which is an elapsed time since boot, // to a time since epoch (i.e. System.currentTimeMillis()-based time.) final long expirationInCurrentTime = currentTimeNow - elapsedRealtimeNow + entry.first; currentTimeNow - tempAllowlistCurrentTime + entry.first; TimeUtils.dumpTimeWithDelta(pw, expirationInCurrentTime, currentTimeNow); pw.println(); }); Loading
services/core/java/com/android/server/am/FgsTempAllowList.java +11 −6 Original line number Diff line number Diff line Loading @@ -29,7 +29,7 @@ import java.util.function.BiConsumer; /** * List of keys that have expiration time. * If the expiration time is less than current elapsedRealtime, the key has expired. * If the expiration time is less than current uptime, the key has expired. * Otherwise it is valid (or allowed). * * <p>This is used for both FGS-BG-start restriction, and FGS-while-in-use permissions check.</p> Loading @@ -42,7 +42,7 @@ public class FgsTempAllowList<E> { private static final int DEFAULT_MAX_SIZE = 100; /** * The value is Pair type, Pair.first is the expirationTime(an elapsedRealtime), * The value is Pair type, Pair.first is the expirationTime(in cpu uptime), * Pair.second is the optional information entry about this key. */ private final SparseArray<Pair<Long, E>> mTempAllowList = new SparseArray<>(); Loading Loading @@ -82,7 +82,9 @@ public class FgsTempAllowList<E> { } // The temp allowlist should be a short list with only a few entries in it. // for a very large list, HashMap structure should be used. final long now = SystemClock.elapsedRealtime(); final long now = com.android.server.deviceidle.Flags.useCpuTimeForTempAllowlist() ? SystemClock.uptimeMillis() : SystemClock.elapsedRealtime(); final int size = mTempAllowList.size(); if (size > mMaxSize) { Slog.w(TAG_AM, "FgsTempAllowList length:" + size + " exceeds maxSize" Loading Loading @@ -112,12 +114,15 @@ public class FgsTempAllowList<E> { final int index = mTempAllowList.indexOfKey(uid); if (index < 0) { return null; } else if (mTempAllowList.valueAt(index).first < SystemClock.elapsedRealtime()) { } final long timeNow = com.android.server.deviceidle.Flags.useCpuTimeForTempAllowlist() ? SystemClock.uptimeMillis() : SystemClock.elapsedRealtime(); if (mTempAllowList.valueAt(index).first < timeNow) { mTempAllowList.removeAt(index); return null; } else { return mTempAllowList.valueAt(index); } return mTempAllowList.valueAt(index); } } Loading
services/tests/mockingservicestests/Android.bp +2 −2 Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. java_defaults { name: "FrameworkMockingServicesTests-jni-defaults", name: "FrameworksMockingServicesTests-jni-defaults", jni_libs: [ "libmockingservicestestjni", ], Loading @@ -30,7 +30,7 @@ package { android_test { name: "FrameworksMockingServicesTests", defaults: [ "FrameworkMockingServicesTests-jni-defaults", "FrameworksMockingServicesTests-jni-defaults", "modules-utils-testable-device-config-defaults", ], Loading