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

Commit 49351e1b authored by Chih-Yu Huang's avatar Chih-Yu Huang
Browse files

psc: Extract ConnectionRecord flags/ongoing calls to internal class

Moves `mFlags` and `mOngoingCalls` fields, along with their accessor
methods from `ConnectionRecord` to a new abstract base class,
`ConnectionRecordInternal`, in the `psc` package.

`ConnectionRecord` now extends `ConnectionRecordInternal` to inherit
these common attributes and methods, preparing for future refactoring
related to process state controller.

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

Change-Id: If1aac17a0d5251bfb7d8d90ff8d8dd11c95146a6
parent 958006fa
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -685,6 +685,7 @@ com.android.server.SystemServiceManager

com.android.server.am.BoundServiceSession
com.android.server.am.ConnectionRecord
com.android.server.am.psc.ConnectionRecordInternal
com.android.server.utils.TimingsTraceAndSlog

com.google.android.collect.Lists
+10 −38
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.util.proto.ProtoUtils;

import com.android.internal.app.procstats.AssociationState;
import com.android.internal.app.procstats.ProcessStats;
import com.android.server.am.psc.ConnectionRecordInternal;
import com.android.server.am.psc.ProcessRecordInternal;
import com.android.server.wm.ActivityServiceConnectionsHolder;

@@ -41,15 +42,13 @@ import java.io.PrintWriter;
 * Description of a single binding to a service.
 */
@RavenwoodKeepWholeClass
final class ConnectionRecord implements OomAdjusterImpl.Connection{
final class ConnectionRecord extends ConnectionRecordInternal implements OomAdjusterImpl.Connection{
    BoundServiceSession mBoundServiceSession;  // The associated bound service session if created.
    final AppBindRecord binding;    // The application/service binding.
    final ActivityServiceConnectionsHolder<ConnectionRecord> activity;  // If non-null, the owning activity.
    final IServiceConnection conn;  // The client connection.
    private final long flags;                // Binding options.
    final int clientLabel;          // String resource labeling this client.
    final PendingIntent clientIntent; // How to launch the client.
    private boolean mOngoingCalls;  // Any ongoing transactions over this connection?
    final int clientUid;            // The identity of this connection's client
    final String clientProcessName; // The source process of this connection's client
    final String clientPackageName; // The source package of this connection's client
@@ -112,11 +111,11 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
            activity.dump(pw, prefix);
        }
        pw.println(prefix + "conn=" + conn.asBinder()
                + " flags=0x" + Long.toHexString(flags));
                + " flags=0x" + Long.toHexString(getFlags()));

        pw.print(prefix);
        pw.print("ongoingCalls=");
        pw.println(mOngoingCalls);
        pw.println(getOngoingCalls());
        if (mBoundServiceSession != null) {
            mBoundServiceSession.dump(new IndentingPrintWriter(pw, "  ", prefix));
        }
@@ -128,10 +127,11 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
            int _clientLabel, PendingIntent _clientIntent,
            int _clientUid, String _clientProcessName, String _clientPackageName,
            ComponentName _aliasComponent) {
        super(_flags);

        binding = _binding;
        activity = _activity;
        conn = _conn;
        flags = _flags;
        clientLabel = _clientLabel;
        clientIntent = _clientIntent;
        clientUid = _clientUid;
@@ -140,14 +140,6 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
        aliasComponent = _aliasComponent;
    }

    boolean setOngoingCalls(boolean ongoingCalls) {
        if (mOngoingCalls != ongoingCalls) {
            mOngoingCalls = ongoingCalls;
            return true;
        }
        return false;
    }

    @Override
    public void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecordInternal host,
            ProcessRecordInternal client, long now, ProcessRecordInternal topApp, boolean doingAll,
@@ -163,7 +155,7 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{

    @Override
    public int cpuTimeTransmissionType() {
        if (mOngoingCalls) {
        if (getOngoingCalls()) {
            return CPU_TIME_TRANSMISSION_NORMAL;
        }
        if (hasFlag(Context.BIND_ALLOW_FREEZE)) {
@@ -173,26 +165,6 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
                : CPU_TIME_TRANSMISSION_NORMAL;
    }

    public long getFlags() {
        return flags;
    }

    public boolean hasFlag(final int flag) {
        return (flags & Integer.toUnsignedLong(flag)) != 0;
    }

    public boolean hasFlag(final long flag) {
        return (flags & flag) != 0;
    }

    public boolean notHasFlag(final int flag) {
        return !hasFlag(flag);
    }

    public boolean notHasFlag(final long flag) {
        return !hasFlag(flag);
    }

    public void startAssociationIfNeeded() {
        // If we don't already have an active association, create one...  but only if this
        // is an association between two different processes.
@@ -244,7 +216,7 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
        sb.append(binding.client.mPid);
        sb.append("->");
        sb.append(binding.service.shortInstanceName);
        sb.append(" flags=0x" + Long.toHexString(flags));
        sb.append(" flags=0x" + Long.toHexString(getFlags()));
        sb.append('}');
        return sb.toString();
    }
@@ -328,7 +300,7 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
        sb.append(binding.service.shortInstanceName);
        sb.append(":@");
        sb.append(Integer.toHexString(System.identityHashCode(conn.asBinder())));
        sb.append(" flags=0x" + Long.toHexString(flags));
        sb.append(" flags=0x" + Long.toHexString(getFlags()));
        sb.append('}');
        return stringName = sb.toString();
    }
@@ -343,7 +315,7 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
            proto.write(ConnectionRecordProto.CLIENT_PID, binding.client.mPid);
        }
        ProtoUtils.writeBitWiseFlagsToProtoEnum(proto, ConnectionRecordProto.FLAGS,
                flags, BIND_ORIG_ENUMS, BIND_PROTO_ENUMS);
                getFlags(), BIND_ORIG_ENUMS, BIND_PROTO_ENUMS);
        if (serviceDead) {
            proto.write(ConnectionRecordProto.FLAGS, ConnectionRecordProto.DEAD);
        }
+82 −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;

import android.ravenwood.annotation.RavenwoodKeepWholeClass;

/**
 * An abstract base class encapsulating common internal properties and state for a single binding
 * to a service.
 */
@RavenwoodKeepWholeClass
public abstract class ConnectionRecordInternal {
    /** The service binding operation. */
    private final long mFlags;
    /** Whether there are currently ongoing transactions over this service connection. */
    private boolean mOngoingCalls;

    public ConnectionRecordInternal(long flags) {
        this.mFlags = flags;
    }

    public long getFlags() {
        return mFlags;
    }

    /**
     * Checks if any of specific flags (int) is set for this connection.
     * Because the bind flags are bitwise flags, we can check for multiple flags by
     * bitwise-ORing them together (e.g., {@code hasFlag(FLAG1 | FLAG2)}). In this
     * case, the method returns true if *any* of the specified flags are present.
     */
    public boolean hasFlag(final int flag) {
        return hasFlag(Integer.toUnsignedLong(flag));
    }

    /**
     * Checks if any of specific flags (long) is set for this connection.
     * Because the bind flags are bitwise flags, we can check for multiple flags by
     * bitwise-ORing them together (e.g., {@code hasFlag(FLAG1 | FLAG2)}). In this
     * case, the method returns true if *any* of the specified flags are present.
     */
    public boolean hasFlag(final long flag) {
        return (mFlags & flag) != 0;
    }

    /** Checks if all of the specific flags (int) are NOT set for this connection. */
    public boolean notHasFlag(final int flag) {
        return !hasFlag(flag);
    }

    /** Checks if all of the specific flag (long) are NOT set for this connection. */
    public boolean notHasFlag(final long flag) {
        return !hasFlag(flag);
    }

    public boolean getOngoingCalls() {
        return mOngoingCalls;
    }

    /** Sets the ongoing calls status for this connection. Returns true if the status is changed. */
    public boolean setOngoingCalls(boolean ongoingCalls) {
        if (mOngoingCalls != ongoingCalls) {
            mOngoingCalls = ongoingCalls;
            return true;
        }
        return false;
    }
}