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

Commit 4c0faa6b authored by Yasin Kilicdere's avatar Yasin Kilicdere Committed by Android (Google) Code Review
Browse files

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

parents 5f8269e7 26130d14
Loading
Loading
Loading
Loading
+24 −23
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 long COOL_OFF_PERIOD_MS = 1000;
    private static final int CPU_IDLE_TIMEOUT_MS = 60 * 1000;
    private static final int TIMEOUT_IN_SECONDS = 45;
    private static final int CPU_IDLE_THRESHOLD_PERCENTAGE = 90;

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

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

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

    private void waitForBroadcastIdle() {
        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");
        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);
        }
    private void waitForCpuIdle() {
        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");

    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) {
                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) {
@@ -211,10 +210,12 @@ 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