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

Commit ff642bb9 authored by Edgar Arriaga García's avatar Edgar Arriaga García Committed by Android (Google) Code Review
Browse files

Merge changes from topic "cherrypicker-L09000000959180098:N55700001348327156" into udc-dev

* changes:
  Add debug support for native compactions
  Simplify debug command to force compactions
  Disable file compaction by default
  Remove some no longer used compaction variables
parents 84617c01 de9b3c9a
Loading
Loading
Loading
Loading
+43 −5
Original line number Diff line number Diff line
@@ -1066,16 +1066,26 @@ final class ActivityManagerShellCommand extends ShellCommand {
    }

    @NeverCompile
    int runCompact(PrintWriter pw) {
    int runCompact(PrintWriter pw) throws RemoteException {
        ProcessRecord app;
        String op = getNextArgRequired();
        boolean isFullCompact = op.equals("full");
        boolean isSomeCompact = op.equals("some");
        if (isFullCompact || isSomeCompact) {
            String processName = getNextArgRequired();
            String uid = getNextArgRequired();
            synchronized (mInternal.mProcLock) {
                app = mInternal.getProcessRecordLocked(processName, Integer.parseInt(uid));
                // Default to current user
                int userId = mInterface.getCurrentUserId();
                String userOpt = getNextOption();
                if (userOpt != null && "--user".equals(userOpt)) {
                    int inputUserId = UserHandle.parseUserArg(getNextArgRequired());
                    if (inputUserId != UserHandle.USER_CURRENT) {
                        userId = inputUserId;
                    }
                }
                final int uid =
                        mInternal.getPackageManagerInternal().getPackageUid(processName, 0, userId);
                app = mInternal.getProcessRecordLocked(processName, uid);
            }
            pw.println("Process record found pid: " + app.mPid);
            if (isFullCompact) {
@@ -1101,6 +1111,28 @@ final class ActivityManagerShellCommand extends ShellCommand {
                mInternal.mOomAdjuster.mCachedAppOptimizer.compactAllSystem();
            }
            pw.println("Finished system compaction");
        } else if (op.equals("native")) {
            op = getNextArgRequired();
            isFullCompact = op.equals("full");
            isSomeCompact = op.equals("some");
            int pid;
            String pidStr = getNextArgRequired();
            try {
                pid = Integer.parseInt(pidStr);
            } catch (Exception e) {
                getErrPrintWriter().println("Error: failed to parse '" + pidStr + "' as a PID");
                return -1;
            }
            if (isFullCompact) {
                mInternal.mOomAdjuster.mCachedAppOptimizer.compactNative(
                        CachedAppOptimizer.CompactProfile.FULL, pid);
            } else if (isSomeCompact) {
                mInternal.mOomAdjuster.mCachedAppOptimizer.compactNative(
                        CachedAppOptimizer.CompactProfile.SOME, pid);
            } else {
                getErrPrintWriter().println("Error: unknown compaction type '" + op + "'");
                return -1;
            }
        } else {
            getErrPrintWriter().println("Error: unknown compact command '" + op + "'");
            return -1;
@@ -4018,11 +4050,17 @@ final class ActivityManagerShellCommand extends ShellCommand {
            pw.println("      --allow-background-activity-starts: The receiver may start activities");
            pw.println("          even if in the background.");
            pw.println("      --async: Send without waiting for the completion of the receiver.");
            pw.println("  compact [some|full|system] <process_name> <Package UID>");
            pw.println("      Force process compaction.");
            pw.println("  compact [some|full] <process_name> [--user <USER_ID>]");
            pw.println("      Perform a single process compaction.");
            pw.println("      some: execute file compaction.");
            pw.println("      full: execute anon + file compaction.");
            pw.println("      system: system compaction.");
            pw.println("  compact system");
            pw.println("      Perform a full system compaction.");
            pw.println("  compact native [some|full] <pid>");
            pw.println("      Perform a native compaction for process with <pid>.");
            pw.println("      some: execute file compaction.");
            pw.println("      full: execute anon + file compaction.");
            pw.println("  instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w]");
            pw.println("          [--user <USER_ID> | current]");
            pw.println("          [--no-hidden-api-checks [--no-test-api-access]]");
+65 −87
Original line number Diff line number Diff line
@@ -66,8 +66,6 @@ public final class CachedAppOptimizer {
    // Flags stored in the DeviceConfig API.
    @VisibleForTesting static final String KEY_USE_COMPACTION = "use_compaction";
    @VisibleForTesting static final String KEY_USE_FREEZER = "use_freezer";
    @VisibleForTesting static final String KEY_COMPACT_ACTION_1 = "compact_action_1";
    @VisibleForTesting static final String KEY_COMPACT_ACTION_2 = "compact_action_2";
    @VisibleForTesting static final String KEY_COMPACT_THROTTLE_1 = "compact_throttle_1";
    @VisibleForTesting static final String KEY_COMPACT_THROTTLE_2 = "compact_throttle_2";
    @VisibleForTesting static final String KEY_COMPACT_THROTTLE_3 = "compact_throttle_3";
@@ -99,15 +97,6 @@ public final class CachedAppOptimizer {
    private static final int RSS_ANON_INDEX = 2;
    private static final int RSS_SWAP_INDEX = 3;

    // Phenotype sends int configurations and we map them to the strings we'll use on device,
    // preventing a weird string value entering the kernel.
    private static final int COMPACT_ACTION_NONE = 0;
    private static final int COMPACT_ACTION_FILE = 1;
    private static final int COMPACT_ACTION_ANON = 2;
    private static final int COMPACT_ACTION_ALL = 3;

    private static final String COMPACT_ACTION_STRING[] = {"", "file", "anon", "all"};

    // Keeps these flags in sync with services/core/jni/com_android_server_am_CachedAppOptimizer.cpp
    private static final int COMPACT_ACTION_FILE_FLAG = 1;
    private static final int COMPACT_ACTION_ANON_FLAG = 2;
@@ -117,11 +106,11 @@ public final class CachedAppOptimizer {

    private static final int FREEZE_BINDER_TIMEOUT_MS = 100;

    @VisibleForTesting static final boolean ENABLE_FILE_COMPACT = false;

    // Defaults for phenotype flags.
    @VisibleForTesting static final Boolean DEFAULT_USE_COMPACTION = true;
    @VisibleForTesting static final Boolean DEFAULT_USE_FREEZER = true;
    @VisibleForTesting static final int DEFAULT_COMPACT_ACTION_2 = COMPACT_ACTION_ALL;
    @VisibleForTesting static final int DEFAULT_COMPACT_ACTION_1 = COMPACT_ACTION_FILE;
    @VisibleForTesting static final long DEFAULT_COMPACT_THROTTLE_1 = 5_000;
    @VisibleForTesting static final long DEFAULT_COMPACT_THROTTLE_2 = 10_000;
    @VisibleForTesting static final long DEFAULT_COMPACT_THROTTLE_3 = 500;
@@ -156,24 +145,17 @@ public final class CachedAppOptimizer {
    @VisibleForTesting
    interface ProcessDependencies {
        long[] getRss(int pid);
        void performCompaction(CompactAction action, int pid) throws IOException;
        void performCompaction(CompactProfile action, int pid) throws IOException;
    }

    // This indicates the compaction we want to perform
    public enum CompactProfile {
        NONE, // No compaction
        SOME, // File compaction
        ANON, // Anon compaction
        FULL // File+anon compaction
    }

    // Low level actions that can be performed for compaction
    // currently determined by the compaction profile
    public enum CompactAction {
        NONE, // No compaction
        FILE, // File+anon compaction
        ANON,
        ALL
    }

    // This indicates the process OOM memory state that initiated the compaction request
    public enum CompactSource { APP, PERSISTENT, BFGS }

@@ -187,6 +169,7 @@ public final class CachedAppOptimizer {
    static final int COMPACT_SYSTEM_MSG = 2;
    static final int SET_FROZEN_PROCESS_MSG = 3;
    static final int REPORT_UNFREEZE_MSG = 4;
    static final int COMPACT_NATIVE_MSG = 5;

    // When free swap falls below this percentage threshold any full (file + anon)
    // compactions will be downgraded to file only compactions to reduce pressure
@@ -240,9 +223,6 @@ public final class CachedAppOptimizer {
                        for (String name : properties.getKeyset()) {
                            if (KEY_USE_COMPACTION.equals(name)) {
                                updateUseCompaction();
                            } else if (KEY_COMPACT_ACTION_1.equals(name)
                                    || KEY_COMPACT_ACTION_2.equals(name)) {
                                updateCompactionActions();
                            } else if (KEY_COMPACT_THROTTLE_1.equals(name)
                                    || KEY_COMPACT_THROTTLE_2.equals(name)
                                    || KEY_COMPACT_THROTTLE_3.equals(name)
@@ -314,12 +294,6 @@ public final class CachedAppOptimizer {

    // Configured by phenotype. Updates from the server take effect immediately.
    @GuardedBy("mPhenotypeFlagLock")
    @VisibleForTesting
    volatile CompactAction mCompactActionSome = compactActionIntToAction(DEFAULT_COMPACT_ACTION_1);
    @GuardedBy("mPhenotypeFlagLock")
    @VisibleForTesting
    volatile CompactAction mCompactActionFull = compactActionIntToAction(DEFAULT_COMPACT_ACTION_2);
    @GuardedBy("mPhenotypeFlagLock")
    @VisibleForTesting volatile long mCompactThrottleSomeSome = DEFAULT_COMPACT_THROTTLE_1;
    @GuardedBy("mPhenotypeFlagLock")
    @VisibleForTesting volatile long mCompactThrottleSomeFull = DEFAULT_COMPACT_THROTTLE_2;
@@ -542,7 +516,6 @@ public final class CachedAppOptimizer {
                CACHED_APP_FREEZER_ENABLED_URI, false, mSettingsObserver);
        synchronized (mPhenotypeFlagLock) {
            updateUseCompaction();
            updateCompactionActions();
            updateCompactionThrottles();
            updateCompactStatsdSampleRate();
            updateFreezerStatsdSampleRate();
@@ -587,8 +560,6 @@ public final class CachedAppOptimizer {
        pw.println("CachedAppOptimizer settings");
        synchronized (mPhenotypeFlagLock) {
            pw.println("  " + KEY_USE_COMPACTION + "=" + mUseCompaction);
            pw.println("  " + KEY_COMPACT_ACTION_1 + "=" + mCompactActionSome);
            pw.println("  " + KEY_COMPACT_ACTION_2 + "=" + mCompactActionFull);
            pw.println("  " + KEY_COMPACT_THROTTLE_1 + "=" + mCompactThrottleSomeSome);
            pw.println("  " + KEY_COMPACT_THROTTLE_2 + "=" + mCompactThrottleSomeFull);
            pw.println("  " + KEY_COMPACT_THROTTLE_3 + "=" + mCompactThrottleFullSome);
@@ -761,19 +732,9 @@ public final class CachedAppOptimizer {
        return false;
    }

    private CompactAction resolveCompactActionForProfile(CompactProfile profile) {
        CompactAction action;
        switch (profile) {
            case SOME:
                action = CompactAction.FILE;
                break;
            case FULL:
                action = CompactAction.ALL;
                break;
            default:
                action = CompactAction.NONE;
        }
        return action;
    void compactNative(CompactProfile compactProfile, int pid) {
        mCompactionHandler.sendMessage(mCompactionHandler.obtainMessage(
                COMPACT_NATIVE_MSG, pid, compactProfile.ordinal()));
    }

    private AggregatedProcessCompactionStats getPerProcessAggregatedCompactStat(
@@ -1050,18 +1011,6 @@ public final class CachedAppOptimizer {
        });
    }

    @GuardedBy("mPhenotypeFlagLock")
    private void updateCompactionActions() {
        int compactAction1 = DeviceConfig.getInt(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                KEY_COMPACT_ACTION_1, DEFAULT_COMPACT_ACTION_1);

        int compactAction2 = DeviceConfig.getInt(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                KEY_COMPACT_ACTION_2, DEFAULT_COMPACT_ACTION_2);

        mCompactActionSome = compactActionIntToAction(compactAction1);
        mCompactActionFull = compactActionIntToAction(compactAction2);
    }

    @GuardedBy("mPhenotypeFlagLock")
    private void updateCompactionThrottles() {
        boolean useThrottleDefaults = false;
@@ -1235,14 +1184,6 @@ public final class CachedAppOptimizer {
        return true;
    }

    static CompactAction compactActionIntToAction(int action) {
        if (action < 0 || action >= CompactAction.values().length) {
            return CompactAction.NONE;
        }

        return CompactAction.values()[action];
    }

    // This will ensure app will be out of the freezer for at least mFreezerDebounceTimeout.
    @GuardedBy("mAm")
    void unfreezeTemporarily(ProcessRecord app, @OomAdjuster.OomAdjReason int reason) {
@@ -1475,8 +1416,10 @@ public final class CachedAppOptimizer {

        if (oldAdj <= ProcessList.PERCEPTIBLE_APP_ADJ
                && (newAdj == ProcessList.PREVIOUS_APP_ADJ || newAdj == ProcessList.HOME_APP_ADJ)) {
            if (ENABLE_FILE_COMPACT) {
                // Perform a minor compaction when a perceptible app becomes the prev/home app
                compactApp(app, CompactProfile.SOME, CompactSource.APP, false);
            }
        } else if (oldAdj < ProcessList.CACHED_APP_MIN_ADJ
                && newAdj >= ProcessList.CACHED_APP_MIN_ADJ
                && newAdj <= ProcessList.CACHED_APP_MAX_ADJ) {
@@ -1486,23 +1429,37 @@ public final class CachedAppOptimizer {
    }

    /**
     * Applies a compaction downgrade when swap is low.
     * Computes the final compaction profile to be used which depends on compaction
     * features enabled and swap usage.
     */
    CompactProfile downgradeCompactionIfRequired(CompactProfile profile) {
        // Downgrade compaction under swap memory pressure
    CompactProfile resolveCompactionProfile(CompactProfile profile) {
        if (profile == CompactProfile.FULL) {
            double swapFreePercent = getFreeSwapPercent();
            // Downgrade compaction under swap memory pressure
            if (swapFreePercent < COMPACT_DOWNGRADE_FREE_SWAP_THRESHOLD) {
                profile = CompactProfile.SOME;

                ++mTotalCompactionDowngrades;
                if (DEBUG_COMPACTION) {
                    Slog.d(TAG_AM,
                            "Downgraded compaction to file only due to low swap."
                            "Downgraded compaction to "+ profile +" due to low swap."
                                    + " Swap Free% " + swapFreePercent);
                }
            }
        }

        if (!ENABLE_FILE_COMPACT) {
            if (profile == CompactProfile.SOME) {
                profile = CompactProfile.NONE;
            } else if (profile == CompactProfile.FULL) {
                profile = CompactProfile.ANON;
            }
            if (DEBUG_COMPACTION) {
                Slog.d(TAG_AM,
                        "Final compaction profile "+ profile +" due to file compact disabled");
            }
        }

        return profile;
    }

@@ -1733,7 +1690,6 @@ public final class CachedAppOptimizer {
                    ProcessRecord proc;
                    final ProcessCachedOptimizerRecord opt;
                    int pid;
                    CompactAction resolvedAction;
                    final String name;
                    CompactProfile lastCompactProfile;
                    long lastCompactTime;
@@ -1811,17 +1767,24 @@ public final class CachedAppOptimizer {
                    }

                    CompactProfile resolvedProfile =
                            downgradeCompactionIfRequired(requestedProfile);
                    resolvedAction = resolveCompactActionForProfile(resolvedProfile);
                            resolveCompactionProfile(requestedProfile);
                    if (resolvedProfile == CompactProfile.NONE) {
                        // No point on issuing compaction call as we don't want to compact.
                        if (DEBUG_COMPACTION) {
                            Slog.d(TAG_AM, "Resolved no compaction for "+ name +
                                    " requested profile="+requestedProfile);
                        }
                        return;
                    }

                    try {
                        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
                                "Compact " + resolvedAction.name() + ": " + name
                                "Compact " + resolvedProfile.name() + ": " + name
                                        + " lastOomAdjReason: " + oomAdjReason
                                        + " source: " + compactSource.name());
                        long zramUsedKbBefore = getUsedZramMemory();
                        long startCpuTime = threadCpuTimeNs();
                        mProcessDependencies.performCompaction(resolvedAction, pid);
                        mProcessDependencies.performCompaction(resolvedProfile, pid);
                        long endCpuTime = threadCpuTimeNs();
                        long[] rssAfter = mProcessDependencies.getRss(pid);
                        long end = SystemClock.uptimeMillis();
@@ -1877,7 +1840,7 @@ public final class CachedAppOptimizer {
                                return;
                        }
                        EventLog.writeEvent(EventLogTags.AM_COMPACT, pid, name,
                                resolvedAction.name(), rssBefore[RSS_TOTAL_INDEX],
                                resolvedProfile.name(), rssBefore[RSS_TOTAL_INDEX],
                                rssBefore[RSS_FILE_INDEX], rssBefore[RSS_ANON_INDEX],
                                rssBefore[RSS_SWAP_INDEX], deltaTotalRss, deltaFileRss,
                                deltaAnonRss, deltaSwapRss, time, lastCompactProfile.name(),
@@ -1907,6 +1870,21 @@ public final class CachedAppOptimizer {
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;
                }
                case COMPACT_NATIVE_MSG: {
                    int pid = msg.arg1;
                    CompactProfile compactProfile = CompactProfile.values()[msg.arg2];
                    Slog.d(TAG_AM,
                            "Performing native compaction for pid=" + pid
                                    + " type=" + compactProfile.name());
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "compactSystem");
                    try {
                        mProcessDependencies.performCompaction(compactProfile, pid);
                    } catch (Exception e) {
                        Slog.d(TAG_AM, "Failed compacting native pid= " + pid);
                    }
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;
                }
            }
        }
    }
@@ -2170,13 +2148,13 @@ public final class CachedAppOptimizer {

        // Compact process.
        @Override
        public void performCompaction(CompactAction action, int pid) throws IOException {
        public void performCompaction(CompactProfile profile, int pid) throws IOException {
            mPidCompacting = pid;
            if (action == CompactAction.ALL) {
            if (profile == CompactProfile.FULL) {
                compactProcess(pid, COMPACT_ACTION_FILE_FLAG | COMPACT_ACTION_ANON_FLAG);
            } else if (action == CompactAction.FILE) {
            } else if (profile == CompactProfile.SOME) {
                compactProcess(pid, COMPACT_ACTION_FILE_FLAG);
            } else if (action == CompactAction.ANON) {
            } else if (profile == CompactProfile.ANON) {
                compactProcess(pid, COMPACT_ACTION_ANON_FLAG);
            }
            mPidCompacting = -1;
+12 −94

File changed.

Preview size limit exceeded, changes collapsed.