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

Commit 7cb8302a authored by Chih-Yu Huang's avatar Chih-Yu Huang
Browse files

psc: Introduce ActiveUidsInternal interface

Introduce `ActiveUidsInternal` interface to provide an abstraction for
managing active UID states. `ActiveUids` now implements this interface,
exposing its core methods publicly.

Update `OomAdjuster` and `OomAdjusterImpl` to utilize the new interface
type for UID state references, enabling more flexible and testable
implementations.

Bug: 425766486
Test: m services.core
Test: atest MockingOomAdjusterTests OomAdjusterTests
Test: atest FrameworksServicesTestsRavenwood_ProcessStateController
Flag: EXEMPT pure refactor

Change-Id: I40db690023e731d745dffa144fe853c5dd86f0a8
parent ff28f580
Loading
Loading
Loading
Loading
+24 −8
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 * limitations under the License.
 */

package com.android.server.am;
@@ -21,6 +21,9 @@ import android.os.UserHandle;
import android.util.SparseArray;
import android.util.proto.ProtoOutputStream;

import com.android.server.am.psc.ActiveUidsInternal;
import com.android.server.am.psc.UidStateRecord;

import java.io.PrintWriter;

/**
@@ -30,7 +33,7 @@ import java.io.PrintWriter;
 * TODO(b/425766486): Maybe rename it to "UidRecordMap", so we can reuse it for other purposes than
 *   "active" UIDs.
 */
public final class ActiveUids {
public final class ActiveUids implements ActiveUidsInternal {
    /**
     * Interface for observing changes in UID active/inactive states based on state changes of
     * processes running for that UID.
@@ -64,32 +67,45 @@ public final class ActiveUids {
        }
    }

    void remove(int uid) {
    @Override
    public void put(int uid, UidStateRecord value) {
        // Only UidRecord implements the UidStateRecord, so it's safe to cast directly.
        put(uid, (UidRecord) value);
    }


    @Override
    public void remove(int uid) {
        mActiveUids.remove(uid);
        if (mObserver != null) {
            mObserver.onUidInactive(uid);
        }
    }

    void clear() {
    @Override
    public void clear() {
        mActiveUids.clear();
        // It is only called for a temporal container with mObserver == null or test case.
        // So there is no need to notify activity task manager.
    }

    UidRecord get(int uid) {
    @Override
    public UidRecord get(int uid) {
        return mActiveUids.get(uid);
    }

    int size() {
    @Override
    public int size() {
        return mActiveUids.size();
    }

    UidRecord valueAt(int index) {
    @Override
    public UidRecord valueAt(int index) {
        return mActiveUids.valueAt(index);
    }

    int keyAt(int index) {
    @Override
    public int keyAt(int index) {
        return mActiveUids.keyAt(index);
    }

+4 −3
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ import com.android.internal.annotations.CompositeRWLock;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.ServiceThread;
import com.android.server.am.psc.ActiveUidsInternal;
import com.android.server.am.psc.PlatformCompatCache;
import com.android.server.am.psc.PlatformCompatCache.CachedCompatChangeId;
import com.android.server.am.psc.ProcessStateRecord;
@@ -685,7 +686,8 @@ public abstract class OomAdjuster {
     */
    @GuardedBy("mService")
    protected boolean collectReachableProcessesLocked(ArraySet<ProcessRecord> apps,
            ArrayList<ProcessRecord> processes, ActiveUids uids) {
            ArrayList<ProcessRecord> processes) {
        final ActiveUidsInternal uids = mTmpUidRecords;
        final ArrayDeque<ProcessRecord> queue = mTmpQueue;
        queue.clear();
        processes.clear();
@@ -2712,9 +2714,8 @@ public abstract class OomAdjuster {
                collectReachableProcessesLSP(processes);
            }
        } else {
            final ActiveUids uids = mTmpUidRecords;
            mTmpProcessSet.add(app);
            collectReachableProcessesLocked(mTmpProcessSet, processes, uids);
            collectReachableProcessesLocked(mTmpProcessSet, processes);
            mTmpProcessSet.clear();
        }
        // Now processes contains app's downstream and app
+6 −4
Original line number Diff line number Diff line
@@ -104,6 +104,7 @@ import android.util.Slog;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.ServiceThread;
import com.android.server.am.psc.ActiveUidsInternal;
import com.android.server.am.psc.ProcessStateRecord;
import com.android.server.am.psc.UidStateRecord;
import com.android.server.wm.ActivityServiceConnectionsHolder;
@@ -477,11 +478,12 @@ public class OomAdjusterImpl extends OomAdjuster {
        long mNow;
        int mCachedAdj;
        @OomAdjReason int mOomAdjReason;
        @NonNull ActiveUids mUids;
        @NonNull
        ActiveUidsInternal mUids;
        boolean mFullUpdate;

        void update(ProcessRecord topApp, long now, int cachedAdj,
                @OomAdjReason int oomAdjReason, @NonNull ActiveUids uids, boolean fullUpdate) {
        void update(ProcessRecord topApp, long now, int cachedAdj, @OomAdjReason int oomAdjReason,
                @NonNull ActiveUidsInternal uids, boolean fullUpdate) {
            mTopApp = topApp;
            mNow = now;
            mCachedAdj = cachedAdj;
@@ -618,7 +620,7 @@ public class OomAdjusterImpl extends OomAdjuster {
        @Override
        public void accept(OomAdjusterArgs args) {
            final ProcessRecord app = args.mApp;
            final ActiveUids uids = args.mUids;
            final ActiveUidsInternal uids = args.mUids;

            // This process was updated in some way, mark that it was last calculated this sequence.
            app.mState.setCompletedAdjSeq(mAdjSeq);
+51 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.am.psc;

/**
 * Interface for tracking the activity state of UIDs based on their running processes.
 * This provides a contract for implementations that store and retrieve
 * {@link UidStateRecord} objects by UID.
 *
 * TODO(b/425766486): Mark the mutable methods as package-private after moving the user classes
 *   (e.g. OomAdjuster, ProcessList) into psc package.
 */
public interface ActiveUidsInternal {
    /** Associates the specified UID with the given {@link UidStateRecord}. */
    void put(int uid, UidStateRecord value);

    /** Removes the mapping for a specified UID from this tracking mechanism. */
    void remove(int uid);

    /** Clears all active UID states. */
    void clear();

    /** Returns the number of active UIDs currently being tracked. */
    int size();

    /**
     * Returns the {@link UidStateRecord} associated with the specified UID,
     * or {@code null} if no mapping for the UID exists.
     */
    UidStateRecord get(int uid);

    /** Returns the {@link UidStateRecord} at the specified index. */
    UidStateRecord valueAt(int index);

    /** Returns the UID (key) at the specified index. */
    int keyAt(int index);
}