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

Commit 15ae0eae authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove timeout from waitForBroadcastIdle to improve spikiness." into main

parents dcb0555f 1e48634c
Loading
Loading
Loading
Loading
+23 −24
Original line number Diff line number Diff line
@@ -22,12 +22,12 @@ import android.perftests.utils.ShellHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

// Based on //platform/frameworks/base/apct-tests/perftests/utils/BenchmarkState.java
public class BenchmarkRunner {
    private static final String TAG = BenchmarkRunner.class.getSimpleName();
    private static final int TIMEOUT_IN_SECONDS = 45;
    private static final long COOL_OFF_PERIOD_MS = 1000;
    private static final int CPU_IDLE_TIMEOUT_MS = 60 * 1000;
    private static final int CPU_IDLE_THRESHOLD_PERCENTAGE = 90;

    private static final int NUM_ITERATIONS = 4;
@@ -82,6 +82,7 @@ public class BenchmarkRunner {
    }

    private void prepareForNextRun() {
        SystemClock.sleep(COOL_OFF_PERIOD_MS);
        waitCoolDownPeriod();
        mStartTimeNs = System.nanoTime();
        mPausedDurationNs = 0;
@@ -165,42 +166,42 @@ public class BenchmarkRunner {
        return null;
    }

    /** Waits for the CPU cores and the broadcast queue to be idle. */
    /** Waits for the broadcast queue and the CPU cores to be idle. */
    public void waitCoolDownPeriod() {
        waitForCpuIdle();
        waitForBroadcastIdle();
        waitForCpuIdle();
    }

    private void waitForBroadcastIdle() {
        try {
            ShellHelper.runShellCommandWithTimeout(
                    "am wait-for-broadcast-idle --flush-broadcast-loopers", TIMEOUT_IN_SECONDS);
        } catch (TimeoutException e) {
            Log.e(TAG, "Ending waitForBroadcastIdle because it didn't finish in "
                    + TIMEOUT_IN_SECONDS + " seconds", e);
        }
        Log.d(TAG, "starting to waitForBroadcastIdle");
        final long startedAt = System.currentTimeMillis();
        ShellHelper.runShellCommand("am wait-for-broadcast-idle --flush-broadcast-loopers");
        final long elapsed = System.currentTimeMillis() - startedAt;
        Log.d(TAG, "waitForBroadcastIdle is complete in " + elapsed + " ms");
    }

    private void waitForCpuIdle() {
        int count = 0;
        int idleCpuPercentage;
        while (count++ < TIMEOUT_IN_SECONDS) {
            idleCpuPercentage = getIdleCpuPercentage();
            Log.d(TAG, "Waiting for CPU idle #" + count + "=" + idleCpuPercentage + "%");
            if (idleCpuPercentage > CPU_IDLE_THRESHOLD_PERCENTAGE) {
        Log.d(TAG, "starting to waitForCpuIdle");
        final long startedAt = System.currentTimeMillis();
        while (true) {
            final int idleCpuPercentage = getIdleCpuPercentage();
            final long elapsed = System.currentTimeMillis() - startedAt;
            Log.d(TAG, "waitForCpuIdle " + idleCpuPercentage + "% (" + elapsed + "ms elapsed)");
            if (idleCpuPercentage >= CPU_IDLE_THRESHOLD_PERCENTAGE) {
                Log.d(TAG, "waitForCpuIdle is complete in " + elapsed + " ms");
                return;
            }
            if (elapsed >= CPU_IDLE_TIMEOUT_MS) {
                Log.e(TAG, "Ending waitForCpuIdle because it didn't finish in "
                        + CPU_IDLE_TIMEOUT_MS + " ms");
                return;
            }
            SystemClock.sleep(1000);
        }
        Log.e(TAG, "Ending waitForCpuIdle because it didn't finish in "
                + TIMEOUT_IN_SECONDS + " seconds");
    }

    private int getIdleCpuPercentage() {
        String output = ShellHelper.runShellCommand("top -m 1 -n 1");

        String[] tokens = output.split("\\s+");

        float totalCpu = -1;
        float idleCpu = -1;
        for (String token : tokens) {
@@ -210,12 +211,10 @@ public class BenchmarkRunner {
                idleCpu = Float.parseFloat(token.split("%")[0]);
            }
        }

        if (totalCpu < 0 || idleCpu < 0) {
            Log.e(TAG, "Could not get idle cpu percentage, output=" + output);
            return -1;
        }

        return (int) (100 * idleCpu / totalCpu);
    }
}
 No newline at end of file