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

Commit 43e54140 authored by Lee Shombert's avatar Lee Shombert
Browse files

Small reflow of the AnrTimer class

The AnrTimer class is now abstract and requires two methods when
instantiated with a type V.  The methods extract a Linux pid and uid
from an object of type V.  The public start() method, which used to
take an explicit pid and uid now computes the necessary pid/uid from
the supplied instance of V.

It is permitted to for the pid/uid methods to simply return 0, to
indicate that there is no value.

This change allows AnrTimer clients to compute pid/uid as necessary,
without storing the information in the AnTimer system itself.

Test: atest
 * FrameworksServicesTests:com.android.server.am
 * FrameworksMockingServicesTests:com.android.server.am
 * FrameworksServicesTests:AnrTimerTest

Flag: com.android.server.utils.anr_timer_service
Bug: 325594551

Change-Id: Ifc7779ce23d0884a7943b52d16aadc1419db96b7
parent 582b66b9
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -7560,8 +7560,14 @@ public final class ActiveServices {
            super(Objects.requireNonNull(am).mHandler, msg, label);
        }

        void start(@NonNull ProcessRecord proc, long millis) {
            start(proc, proc.getPid(), proc.uid, millis);
        @Override
        public int getPid(@NonNull ProcessRecord proc) {
            return proc.getPid();
        }

        @Override
        public int getUid(@NonNull ProcessRecord proc) {
            return proc.uid;
        }
    }

@@ -7571,11 +7577,14 @@ public final class ActiveServices {
            super(Objects.requireNonNull(am).mHandler, msg, label);
        }

        void start(@NonNull ServiceRecord service, long millis) {
            start(service,
                    (service.app != null) ? service.app.getPid() : 0,
                    service.appInfo.uid,
                    millis);
        @Override
        public int getPid(@NonNull ServiceRecord service) {
            return (service.app != null) ? service.app.getPid() : 0;
        }

        @Override
        public int getUid(@NonNull ServiceRecord service) {
            return (service.appInfo != null) ? service.appInfo.uid : 0;
        }
    }

+8 −2
Original line number Diff line number Diff line
@@ -1339,8 +1339,14 @@ class BroadcastQueueModernImpl extends BroadcastQueue {
                    MSG_DELIVERY_TIMEOUT, "BROADCAST_TIMEOUT", true);
        }

        void start(@NonNull BroadcastProcessQueue queue, long timeoutMillis) {
            start(queue, queue.app.getPid(), queue.app.uid, timeoutMillis);
        @Override
        public int getPid(@NonNull BroadcastProcessQueue queue) {
            return (queue.app != null) ? queue.app.getPid() : 0;
        }

        @Override
        public int getUid(@NonNull BroadcastProcessQueue queue) {
            return (queue.app != null) ? queue.app.uid : 0;
        }
    }

+22 −6
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ import java.util.Objects;
 * mode, the timer just sends a delayed message.  In modern mode, the timer is implemented in
 * native code; on expiration, the message is sent without delay.
 *
 * <p>There are four external operations on a timer:
 * <p>There are five external operations on a timer:
 * <ul>
 *
 * <li>{@link #start} starts a timer.  The timer is started with an object that the message
@@ -74,9 +74,13 @@ import java.util.Objects;
 * exit. (So, instances in system server generally need not be explicitly closed since they are
 * created during process start and will last until process exit.)
 *
 * <p>AnrTimer parameterized by the type <code>V</code>.  The public methods on AnrTimer require
 * an instance of <code>V</code>; the instance of <code>V</code> is a key that identifies a
 * specific timer.
 *
 * @hide
 */
public class AnrTimer<V> implements AutoCloseable {
public abstract class AnrTimer<V> implements AutoCloseable {

    /**
     * The log tag.
@@ -100,6 +104,20 @@ public class AnrTimer<V> implements AutoCloseable {
     */
    private static final long TRACE_TAG = Trace.TRACE_TAG_ACTIVITY_MANAGER;

    /**
     * Fetch the Linux pid from the object. The returned value may be zero to indicate that there
     * is no valid pid available.
     * @return a valid pid or zero.
     */
    public abstract int getPid(V obj);

    /**
     * Fetch the Linux uid from the object. The returned value may be zero to indicate that there
     * is no valid uid available.
     * @return a valid uid or zero.
     */
    public abstract int getUid(V obj);

    /**
     * Return true if the feature is enabled.  By default, the value is take from the Flags class
     * but it can be changed for local testing.
@@ -564,13 +582,11 @@ public class AnrTimer<V> implements AutoCloseable {
     * allows a client to deliver an immediate timeout via the AnrTimer.
     *
     * @param arg The key by which the timer is known.  This is never examined or modified.
     * @param pid The Linux process ID of the target being timed.
     * @param uid The Linux user ID of the target being timed.
     * @param timeoutMs The timer timeout, in milliseconds.
     */
    public void start(@NonNull V arg, int pid, int uid, long timeoutMs) {
    public void start(@NonNull V arg, long timeoutMs) {
        if (timeoutMs < 0) timeoutMs = 0;
        mFeature.start(arg, pid, uid, timeoutMs);
        mFeature.start(arg, getPid(arg), getUid(arg), timeoutMs);
    }

    /**
+8 −2
Original line number Diff line number Diff line
@@ -136,8 +136,14 @@ public class AnrTimerTest {
            this(helper.mHandler, MSG_TIMEOUT, caller());
        }

        void start(TestArg arg, long millis) {
            start(arg, arg.pid, arg.uid, millis);
        @Override
        public int getPid(TestArg arg) {
            return arg.pid;
        }

        @Override
        public int getUid(TestArg arg) {
            return arg.uid;
        }

        // Return the name of method that called the constructor, assuming that this function is