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

Commit cd2624a2 authored by Max Dashouk's avatar Max Dashouk Committed by Android (Google) Code Review
Browse files

Merge "Adds a flag to allow collection of Looper and Binder stats even when...

Merge "Adds a flag to allow collection of Looper and Binder stats even when charging. This is done for automotive form factor. Notion of battery is not as significant as it is in phones."
parents 811e7f2f c36ef092
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ public class BinderCallsStats implements BinderInternal.Observer {
    public static final int PERIODIC_SAMPLING_INTERVAL_DEFAULT = 1000;
    public static final boolean DEFAULT_TRACK_SCREEN_INTERACTIVE = false;
    public static final boolean DEFAULT_TRACK_DIRECT_CALLING_UID = true;
    public static final boolean DEFAULT_IGNORE_BATTERY_STATUS = false;
    public static final int MAX_BINDER_CALL_STATS_COUNT_DEFAULT = 1500;
    private static final String DEBUG_ENTRY_PREFIX = "__DEBUG_";

@@ -95,6 +96,7 @@ public class BinderCallsStats implements BinderInternal.Observer {
    private boolean mAddDebugEntries = false;
    private boolean mTrackDirectCallingUid = DEFAULT_TRACK_DIRECT_CALLING_UID;
    private boolean mTrackScreenInteractive = DEFAULT_TRACK_SCREEN_INTERACTIVE;
    private boolean mIgnoreBatteryStatus = DEFAULT_IGNORE_BATTERY_STATUS;

    private CachedDeviceState.Readonly mDeviceState;
    private CachedDeviceState.TimeInStateStopwatch mBatteryStopwatch;
@@ -185,8 +187,7 @@ public class BinderCallsStats implements BinderInternal.Observer {
    public CallSession callStarted(Binder binder, int code, int workSourceUid) {
        noteNativeThreadId();

        if (!mRecordingAllTransactionsForUid
                && (mDeviceState == null || mDeviceState.isCharging())) {
        if (!canCollect()) {
            return null;
        }

@@ -255,8 +256,7 @@ public class BinderCallsStats implements BinderInternal.Observer {

        synchronized (mLock) {
            // This was already checked in #callStart but check again while synchronized.
            if (!mRecordingAllTransactionsForUid
                    && (mDeviceState == null || mDeviceState.isCharging())) {
            if (!canCollect()) {
                return;
            }

@@ -372,6 +372,22 @@ public class BinderCallsStats implements BinderInternal.Observer {
        mCallStatsObserver.noteBinderThreadNativeIds(getNativeTids());
    }

    private boolean canCollect() {
        if (mRecordingAllTransactionsForUid) {
            return true;
        }
        if (mIgnoreBatteryStatus) {
            return true;
        }
        if (mDeviceState == null) {
            return false;
        }
        if (mDeviceState.isCharging()) {
            return false;
        }
        return true;
    }

    /**
     * This method is expensive to call.
     */
@@ -671,6 +687,18 @@ public class BinderCallsStats implements BinderInternal.Observer {
        }
    }

    /**
     * Whether to ignore battery status when collecting stats
     */
    public void setIgnoreBatteryStatus(boolean ignored) {
        synchronized (mLock) {
            if (ignored != mIgnoreBatteryStatus) {
                mIgnoreBatteryStatus = ignored;
                reset();
            }
        }
    }

    /**
     * Marks the specified work source UID for total binder call tracking: detailed information
     * will be recorded for all calls from this source ID.
+16 −2
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ public class LooperStats implements Looper.Observer {
    public static final String DEBUG_ENTRY_PREFIX = "__DEBUG_";
    private static final int SESSION_POOL_SIZE = 50;
    private static final boolean DISABLED_SCREEN_STATE_TRACKING_VALUE = false;
    public static final boolean DEFAULT_IGNORE_BATTERY_STATUS = false;

    @GuardedBy("mLock")
    private final SparseArray<Entry> mEntries = new SparseArray<>(512);
@@ -56,6 +57,7 @@ public class LooperStats implements Looper.Observer {
    private long mStartElapsedTime = SystemClock.elapsedRealtime();
    private boolean mAddDebugEntries = false;
    private boolean mTrackScreenInteractive = false;
    private boolean mIgnoreBatteryStatus = DEFAULT_IGNORE_BATTERY_STATUS;

    public LooperStats(int samplingInterval, int entriesSizeCap) {
        this.mSamplingInterval = samplingInterval;
@@ -139,8 +141,16 @@ public class LooperStats implements Looper.Observer {
    }

    private boolean deviceStateAllowsCollection() {
        // Do not collect data if on charger or the state is not set.
        return mDeviceState != null && !mDeviceState.isCharging();
        if (mIgnoreBatteryStatus) {
            return true;
        }
        if (mDeviceState == null) {
            return false;
        }
        if (mDeviceState.isCharging()) {
            return false;
        }
        return true;
    }

    /** Returns an array of {@link ExportedEntry entries} with the aggregated statistics. */
@@ -225,6 +235,10 @@ public class LooperStats implements Looper.Observer {
        mTrackScreenInteractive = enabled;
    }

    public void setIgnoreBatteryStatus(boolean ignore) {
        mIgnoreBatteryStatus = ignore;
    }

    @Nullable
    private Entry findEntry(Message msg, boolean allowCreateNew) {
        final boolean isInteractive = mTrackScreenInteractive
+13 −1
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
@@ -427,6 +426,19 @@ public class BinderCallsStatsTest {
        assertEquals(0, bcs.getUidEntries().size());
    }

    @Test
    public void testIgnoreBatteryStatusFlag() {
        TestBinderCallsStats bcs = new TestBinderCallsStats();
        mDeviceState.setCharging(true);
        bcs.setIgnoreBatteryStatus(true);

        Binder binder = new Binder();
        CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID);
        bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID);

        assertEquals(1, bcs.getExportedCallStats().size());
    }

    @Test
    public void testScreenOff() {
        TestBinderCallsStats bcs = new TestBinderCallsStats();
+17 −0
Original line number Diff line number Diff line
@@ -321,6 +321,23 @@ public final class LooperStatsTest {
        assertThat(entries).hasSize(0);
    }

    @Test
    public void testDataCollectedIfIgnoreBatteryStatusFlagSet() {
        TestableLooperStats looperStats = new TestableLooperStats(1, 100);
        mDeviceState.setCharging(true);
        looperStats.setIgnoreBatteryStatus(true);

        Object token1 = looperStats.messageDispatchStarting();
        looperStats.messageDispatched(token1, mHandlerFirst.obtainMessage(1000));
        Object token2 = looperStats.messageDispatchStarting();
        looperStats.dispatchingThrewException(token2, mHandlerFirst.obtainMessage(1000),
                new IllegalArgumentException());

        List<LooperStats.ExportedEntry> entries = looperStats.getEntries();
        assertThat(entries).hasSize(1);

    }

    @Test
    public void testScreenStateCollected() {
        TestableLooperStats looperStats = new TestableLooperStats(1, 100);
+4 −0
Original line number Diff line number Diff line
@@ -131,6 +131,7 @@ public class BinderCallsStatsService extends Binder {
        private static final String SETTINGS_TRACK_SCREEN_INTERACTIVE_KEY = "track_screen_state";
        private static final String SETTINGS_TRACK_DIRECT_CALLING_UID_KEY = "track_calling_uid";
        private static final String SETTINGS_MAX_CALL_STATS_KEY = "max_call_stats_count";
        private static final String SETTINGS_IGNORE_BATTERY_STATUS_KEY = "ignore_battery_status";

        private boolean mEnabled;
        private final Uri mUri = Settings.Global.getUriFor(Settings.Global.BINDER_CALLS_STATS);
@@ -184,6 +185,9 @@ public class BinderCallsStatsService extends Binder {
            mBinderCallsStats.setTrackDirectCallerUid(
                    mParser.getBoolean(SETTINGS_TRACK_DIRECT_CALLING_UID_KEY,
                    BinderCallsStats.DEFAULT_TRACK_DIRECT_CALLING_UID));
            mBinderCallsStats.setIgnoreBatteryStatus(
                    mParser.getBoolean(SETTINGS_IGNORE_BATTERY_STATUS_KEY,
                    BinderCallsStats.DEFAULT_IGNORE_BATTERY_STATUS));


            final boolean enabled =
Loading