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

Commit de9b3c9a authored by Edgar Arriaga's avatar Edgar Arriaga Committed by Cherrypicker Worker
Browse files

Add debug support for native compactions

This allows for performing compactions using shell
command which is useful for testing and debugging
compaction locally automated tests to allow compacting
applications not managed by ActivityManager such as
purely native binaries.

Test: am compact native full <pid>
Bug: 267188919
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:5fa297d3c198ed2e93c788b332c3a79ff8efb8d3)
Merged-In: I355bd4c55c96ada392d81cf89381870b9dc79c17
Change-Id: I355bd4c55c96ada392d81cf89381870b9dc79c17
parent 6cc5e383
Loading
Loading
Loading
Loading
+30 −2
Original line number Original line Diff line number Diff line
@@ -1111,6 +1111,28 @@ final class ActivityManagerShellCommand extends ShellCommand {
                mInternal.mOomAdjuster.mCachedAppOptimizer.compactAllSystem();
                mInternal.mOomAdjuster.mCachedAppOptimizer.compactAllSystem();
            }
            }
            pw.println("Finished system compaction");
            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 {
        } else {
            getErrPrintWriter().println("Error: unknown compact command '" + op + "'");
            getErrPrintWriter().println("Error: unknown compact command '" + op + "'");
            return -1;
            return -1;
@@ -4028,11 +4050,17 @@ final class ActivityManagerShellCommand extends ShellCommand {
            pw.println("      --allow-background-activity-starts: The receiver may start activities");
            pw.println("      --allow-background-activity-starts: The receiver may start activities");
            pw.println("          even if in the background.");
            pw.println("          even if in the background.");
            pw.println("      --async: Send without waiting for the completion of the receiver.");
            pw.println("      --async: Send without waiting for the completion of the receiver.");
            pw.println("  compact [some|full|system] <process_name> [--user <USER_ID>]");
            pw.println("  compact [some|full] <process_name> [--user <USER_ID>]");
            pw.println("      Force process compaction.");
            pw.println("      Perform a single process compaction.");
            pw.println("      some: execute file compaction.");
            pw.println("      some: execute file compaction.");
            pw.println("      full: execute anon + file compaction.");
            pw.println("      full: execute anon + file compaction.");
            pw.println("      system: system 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("  instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w]");
            pw.println("          [--user <USER_ID> | current]");
            pw.println("          [--user <USER_ID> | current]");
            pw.println("          [--no-hidden-api-checks [--no-test-api-access]]");
            pw.println("          [--no-hidden-api-checks [--no-test-api-access]]");
+21 −0
Original line number Original line Diff line number Diff line
@@ -169,6 +169,7 @@ public final class CachedAppOptimizer {
    static final int COMPACT_SYSTEM_MSG = 2;
    static final int COMPACT_SYSTEM_MSG = 2;
    static final int SET_FROZEN_PROCESS_MSG = 3;
    static final int SET_FROZEN_PROCESS_MSG = 3;
    static final int REPORT_UNFREEZE_MSG = 4;
    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)
    // When free swap falls below this percentage threshold any full (file + anon)
    // compactions will be downgraded to file only compactions to reduce pressure
    // compactions will be downgraded to file only compactions to reduce pressure
@@ -731,6 +732,11 @@ public final class CachedAppOptimizer {
        return false;
        return false;
    }
    }


    void compactNative(CompactProfile compactProfile, int pid) {
        mCompactionHandler.sendMessage(mCompactionHandler.obtainMessage(
                COMPACT_NATIVE_MSG, pid, compactProfile.ordinal()));
    }

    private AggregatedProcessCompactionStats getPerProcessAggregatedCompactStat(
    private AggregatedProcessCompactionStats getPerProcessAggregatedCompactStat(
            String processName) {
            String processName) {
        if (processName == null) {
        if (processName == null) {
@@ -1864,6 +1870,21 @@ public final class CachedAppOptimizer {
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;
                    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;
                }
            }
            }
        }
        }
    }
    }