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

Commit 8b4d509d authored by Tim Murray's avatar Tim Murray Committed by Android (Google) Code Review
Browse files

Merge "compaction: make actions and throttling experimentable"

parents bf08b911 bbcbc2c8
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -70,6 +70,12 @@ final class ActivityManagerConstants extends ContentObserver {
    static final String KEY_MEMORY_INFO_THROTTLE_TIME = "memory_info_throttle_time";
    static final String KEY_TOP_TO_FGS_GRACE_DURATION = "top_to_fgs_grace_duration";
    static final String KEY_USE_COMPACTION = "use_compaction";
    static final String KEY_COMPACT_ACTION_1 = "compact_action_1";
    static final String KEY_COMPACT_ACTION_2 = "compact_action_2";
    static final String KEY_COMPACT_THROTTLE_1 = "compact_throttle_1";
    static final String KEY_COMPACT_THROTTLE_2 = "compact_throttle_2";
    static final String KEY_COMPACT_THROTTLE_3 = "compact_throttle_3";
    static final String KEY_COMPACT_THROTTLE_4 = "compact_throttle_4";

    private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
    private static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60*1000;
@@ -101,6 +107,12 @@ final class ActivityManagerConstants extends ContentObserver {
    private static final long DEFAULT_MEMORY_INFO_THROTTLE_TIME = 5*60*1000;
    private static final long DEFAULT_TOP_TO_FGS_GRACE_DURATION = 15 * 1000;
    private static final boolean DEFAULT_USE_COMPACTION = false;
    public static final int DEFAULT_COMPACT_ACTION_1 = 1;
    public static final int DEFAULT_COMPACT_ACTION_2 = 3;
    public static final long DEFAULT_COMPACT_THROTTLE_1 = 5000;
    public static final long DEFAULT_COMPACT_THROTTLE_2 = 10000;
    public static final long DEFAULT_COMPACT_THROTTLE_3 = 500;
    public static final long DEFAULT_COMPACT_THROTTLE_4 = 10000;

    // Maximum number of cached processes we will allow.
    public int MAX_CACHED_PROCESSES = DEFAULT_MAX_CACHED_PROCESSES;
@@ -223,6 +235,20 @@ final class ActivityManagerConstants extends ContentObserver {
    // Use compaction for background apps.
    public boolean USE_COMPACTION = DEFAULT_USE_COMPACTION;

    // Action for compactAppSome.
    public int COMPACT_ACTION_1 = DEFAULT_COMPACT_ACTION_1;
    // Action for compactAppFull;
    public int COMPACT_ACTION_2 = DEFAULT_COMPACT_ACTION_2;

    // How long we'll skip second compactAppSome after first compactAppSome
    public long COMPACT_THROTTLE_1 = DEFAULT_COMPACT_THROTTLE_1;
    // How long we'll skip compactAppSome after compactAppFull
    public long COMPACT_THROTTLE_2 = DEFAULT_COMPACT_THROTTLE_2;
    // How long we'll skip compactAppFull after compactAppSome
    public long COMPACT_THROTTLE_3 = DEFAULT_COMPACT_THROTTLE_3;
    // How long we'll skip second compactAppFull after first compactAppFull
    public long COMPACT_THROTTLE_4 = DEFAULT_COMPACT_THROTTLE_4;

    // Indicates whether the activity starts logging is enabled.
    // Controlled by Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED
    volatile boolean mFlagActivityStartsLoggingEnabled;
@@ -381,6 +407,12 @@ final class ActivityManagerConstants extends ContentObserver {
            TOP_TO_FGS_GRACE_DURATION = mParser.getDurationMillis(KEY_TOP_TO_FGS_GRACE_DURATION,
                    DEFAULT_TOP_TO_FGS_GRACE_DURATION);
            USE_COMPACTION = mParser.getBoolean(KEY_USE_COMPACTION, DEFAULT_USE_COMPACTION);
            COMPACT_ACTION_1 = mParser.getInt(KEY_COMPACT_ACTION_1, DEFAULT_COMPACT_ACTION_1);
            COMPACT_ACTION_2 = mParser.getInt(KEY_COMPACT_ACTION_2, DEFAULT_COMPACT_ACTION_2);
            COMPACT_THROTTLE_1 = mParser.getLong(KEY_COMPACT_THROTTLE_1, DEFAULT_COMPACT_THROTTLE_1);
            COMPACT_THROTTLE_2 = mParser.getLong(KEY_COMPACT_THROTTLE_2, DEFAULT_COMPACT_THROTTLE_2);
            COMPACT_THROTTLE_3 = mParser.getLong(KEY_COMPACT_THROTTLE_3, DEFAULT_COMPACT_THROTTLE_3);
            COMPACT_THROTTLE_4 = mParser.getLong(KEY_COMPACT_THROTTLE_4, DEFAULT_COMPACT_THROTTLE_4);

            updateMaxCachedProcesses();
        }
+63 −17
Original line number Diff line number Diff line
@@ -44,7 +44,11 @@ public final class AppCompactor {
     */
    final ArrayList<ProcessRecord> mPendingCompactionProcesses = new ArrayList<ProcessRecord>();

    /*
    static final int COMPACT_PROCESS_SOME = 1;
    static final int COMPACT_PROCESS_FULL = 2;
    static final int COMPACT_PROCESS_MSG = 1;

    /**
     * This thread must be moved to the system background cpuset.
     * If that doesn't happen, it's probably going to draw a lot of power.
     * However, this has to happen after the first updateOomAdjLocked, because
@@ -53,13 +57,22 @@ public final class AppCompactor {
     */
    final ServiceThread mCompactionThread;

    static final int COMPACT_PROCESS_SOME = 1;
    static final int COMPACT_PROCESS_FULL = 2;
    static final int COMPACT_PROCESS_MSG = 1;
    final Handler mCompactionHandler;
    final private Handler mCompactionHandler;

    final private ActivityManagerService mAm;
    final private ActivityManagerConstants mConstants;

    final ActivityManagerService mAm;
    final ActivityManagerConstants mConstants;
    final private String COMPACT_ACTION_FILE = "file";
    final private String COMPACT_ACTION_ANON = "anon";
    final private String COMPACT_ACTION_FULL = "full";

    final private String compactActionSome;
    final private String compactActionFull;

    final private long throttleSomeSome;
    final private long throttleSomeFull;
    final private long throttleFullSome;
    final private long throttleFullFull;

    public AppCompactor(ActivityManagerService am) {
        mAm = am;
@@ -69,6 +82,41 @@ public final class AppCompactor {
                THREAD_PRIORITY_FOREGROUND, true);
        mCompactionThread.start();
        mCompactionHandler = new MemCompactionHandler(this);

        switch(mConstants.COMPACT_ACTION_1) {
            case 1:
                compactActionSome = COMPACT_ACTION_FILE;
                break;
            case 2:
                compactActionSome = COMPACT_ACTION_ANON;
                break;
            case 3:
                compactActionSome = COMPACT_ACTION_FULL;
                break;
            default:
                compactActionSome = COMPACT_ACTION_FILE;
                break;
        }

        switch(mConstants.COMPACT_ACTION_2) {
            case 1:
                compactActionFull = COMPACT_ACTION_FILE;
                break;
            case 2:
                compactActionFull = COMPACT_ACTION_ANON;
                break;
            case 3:
                compactActionFull = COMPACT_ACTION_FULL;
                break;
            default:
                compactActionFull = COMPACT_ACTION_FULL;
                break;
        }

        throttleSomeSome = mConstants.COMPACT_THROTTLE_1;
        throttleSomeFull = mConstants.COMPACT_THROTTLE_2;
        throttleFullSome = mConstants.COMPACT_THROTTLE_3;
        throttleFullFull = mConstants.COMPACT_THROTTLE_4;
    }

    // Must be called while holding AMS lock.
@@ -128,18 +176,16 @@ public final class AppCompactor {
                }

                // basic throttling
                // use the ActivityManagerConstants knobs to determine whether current/prevous
                // compaction combo should be throtted or not
                if (pendingAction == COMPACT_PROCESS_SOME) {
                    // if we're compacting some, then compact if >10s after last full
                    // or >5s after last some
                    if ((lastCompactAction == COMPACT_PROCESS_SOME && (start - lastCompactTime < 5000)) ||
                        (lastCompactAction == COMPACT_PROCESS_FULL && (start - lastCompactTime < 10000))) {
                    if ((lastCompactAction == COMPACT_PROCESS_SOME && (start - lastCompactTime < throttleSomeSome)) ||
                        (lastCompactAction == COMPACT_PROCESS_FULL && (start - lastCompactTime < throttleSomeFull))) {
                        return;
                    }
                } else {
                    // if we're compacting full, then compact if >10s after last full
                    // or >.5s after last some
                    if ((lastCompactAction == COMPACT_PROCESS_SOME && (start - lastCompactTime < 500)) ||
                        (lastCompactAction == COMPACT_PROCESS_FULL && (start - lastCompactTime < 10000))) {
                    if ((lastCompactAction == COMPACT_PROCESS_SOME && (start - lastCompactTime < throttleFullSome)) ||
                        (lastCompactAction == COMPACT_PROCESS_FULL && (start - lastCompactTime < throttleFullFull))) {
                        return;
                    }
                }
@@ -151,9 +197,9 @@ public final class AppCompactor {
                    long[] rssBefore = Process.getRss(pid);
                    FileOutputStream fos = new FileOutputStream("/proc/" + pid + "/reclaim");
                    if (pendingAction == COMPACT_PROCESS_SOME) {
                        action = "file";
                        action = compactActionSome;
                    } else {
                        action = "all";
                        action = compactActionFull;
                    }
                    fos.write(action.getBytes());
                    fos.close();