Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d581ba49 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Consistently use CPU uptime for temporary allowlist" into main

parents c3844154 c40fd129
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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
    }
}
+26 −6
Original line number Diff line number Diff line
@@ -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;

@@ -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;
@@ -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);
@@ -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) {
@@ -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);
@@ -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);

@@ -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=");
+4 −2
Original line number Diff line number Diff line
@@ -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);
@@ -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();
            });
+11 −6
Original line number Diff line number Diff line
@@ -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>
@@ -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<>();
@@ -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"
@@ -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);
        }
    }

+2 −2
Original line number Diff line number Diff line
@@ -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",
    ],
@@ -30,7 +30,7 @@ package {
android_test {
    name: "FrameworksMockingServicesTests",
    defaults: [
        "FrameworkMockingServicesTests-jni-defaults",
        "FrameworksMockingServicesTests-jni-defaults",
        "modules-utils-testable-device-config-defaults",
    ],

Loading