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

Commit 742b4afd authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Ignore shouldNotFreeze when CPU_TIME flag is on

When cpu_time is used to decide freezability, we need to ignore
shouldNotFreeze that is continued to be computed for audit and logging
purposes.

Test: atest FrameworksMockingServicesTests:CachedAppOptimizerTest

Flag: com.android.server.am.cpu_time_capability_based_freeze_policy

Bug: 440461047
Change-Id: Iec7a14e92d1a15b609c3327214dacb1e930f132a
parent 193ac83c
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -2024,7 +2024,7 @@ public class CachedAppOptimizer {
         */
        @GuardedBy({"mAm"})
        private void freezeProcess(final ProcessRecord proc) {
            int pid = proc.getPid(); // Unlocked intentionally
            final int pid;
            final String name = proc.processName;
            final long unfrozenDuration;
            final boolean frozen;
@@ -2046,7 +2046,7 @@ public class CachedAppOptimizer {
                    return;
                }

                if (opt.shouldNotFreeze()) {
                if (opt.shouldNotFreeze() && !Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                    if (DEBUG_FREEZER) {
                        Slog.d(TAG_AM, "Skipping freeze because process is marked "
                                + "should not be frozen");
+75 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.server.am.ActivityManagerService.Injector;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
@@ -37,7 +38,10 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.MessageQueue;
import android.os.Process;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.Presubmit;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.DeviceConfig;
import android.text.TextUtils;

@@ -72,6 +76,9 @@ import java.util.concurrent.TimeUnit;
@Presubmit
public final class CachedAppOptimizerTest {

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private ServiceThread mThread;

    @Mock
@@ -1061,6 +1068,74 @@ public final class CachedAppOptimizerTest {
        assertTrue(mFreezeCounter.await(5, TimeUnit.SECONDS));
    }

    @EnableFlags(Flags.FLAG_CPU_TIME_CAPABILITY_BASED_FREEZE_POLICY)
    @Test
    public void shouldNotFreezeIgnored() throws InterruptedException {
        mProcessDependencies.setRss(new long[] {
                0 /*total_rss*/,
                0 /*file*/,
                0 /*anon*/,
                0 /*swap*/,
                0 /*shmem*/
        });
        mUseFreezer = true;
        // Force the system to use the freezer
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT,
                CachedAppOptimizer.KEY_USE_FREEZER, "true", false);
        mCachedAppOptimizerUnderTest.init();
        initActivityManagerService();

        int pid = 10000;
        int uid = 2;
        int pkgUid = 3;
        final ProcessRecord app = makeProcessRecord(pid, uid, pkgUid, "p1", "app1");
        app.setShouldNotFreeze(true, false, 0, 0);

        assertNotNull(app.mOptRecord);
        assertFalse(app.mOptRecord.isFrozen());

        mFreezeCounter = new CountDownLatch(1);
        mCachedAppOptimizerUnderTest.forceFreezeForTest(app, true);
        waitForHandler();

        assertTrue(mFreezeCounter.await(0, TimeUnit.SECONDS));
        assertTrue(app.mOptRecord.isFrozen());
    }

    @DisableFlags(Flags.FLAG_CPU_TIME_CAPABILITY_BASED_FREEZE_POLICY)
    @Test
    public void shouldNotFreezeAbortsFreeze() throws InterruptedException {
        mProcessDependencies.setRss(new long[] {
                0 /*total_rss*/,
                0 /*file*/,
                0 /*anon*/,
                0 /*swap*/,
                0 /*shmem*/
        });
        mUseFreezer = true;
        // Force the system to use the freezer
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_NATIVE_BOOT,
                CachedAppOptimizer.KEY_USE_FREEZER, "true", false);
        mCachedAppOptimizerUnderTest.init();
        initActivityManagerService();

        int pid = 10000;
        int uid = 2;
        int pkgUid = 3;
        final ProcessRecord app = makeProcessRecord(pid, uid, pkgUid, "p1", "app1");
        app.setShouldNotFreeze(true, false, 0, 0);

        assertNotNull(app.mOptRecord);
        assertFalse(app.mOptRecord.isFrozen());

        mFreezeCounter = new CountDownLatch(1);
        mCachedAppOptimizerUnderTest.forceFreezeForTest(app, true);
        waitForHandler();

        assertFalse(mFreezeCounter.await(0, TimeUnit.SECONDS));
        assertFalse(app.mOptRecord.isFrozen());
    }

    private void setFlag(String key, String value, boolean defaultValue) throws Exception {
        mCountDown = new CountDownLatch(1);
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, key, value, defaultValue);