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

Commit 19c8d130 authored by Yi Kong's avatar Yi Kong Committed by Automerger Merge Worker
Browse files

Merge "profcollect: introduce a cool down window between trace events" into...

Merge "profcollect: introduce a cool down window between trace events" into main am: ac2f03ec am: e5e9ae27

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/3288642



Change-Id: I80b163e6ee0bb3c96c180eddb5114ffdf7dee642
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 03b9d32f e5e9ae27
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -25,10 +25,14 @@ import android.util.Log;

import com.android.internal.os.BackgroundThread;

import java.time.Instant;
import java.util.concurrent.ThreadLocalRandom;

public final class Utils {

    private static Instant lastTraceTime = Instant.EPOCH;
    private static final int TRACE_COOLDOWN_SECONDS = 30;

    public static boolean withFrequency(String configName, int defaultFrequency) {
        int threshold = DeviceConfig.getInt(
                DeviceConfig.NAMESPACE_PROFCOLLECT_NATIVE_BOOT, configName, defaultFrequency);
@@ -40,6 +44,9 @@ public final class Utils {
        if (mIProfcollect == null) {
            return false;
        }
        if (isInCooldownOrReset()) {
            return false;
        }
        BackgroundThread.get().getThreadHandler().post(() -> {
            try {
                mIProfcollect.trace_system(eventName);
@@ -54,6 +61,9 @@ public final class Utils {
        if (mIProfcollect == null) {
            return false;
        }
        if (isInCooldownOrReset()) {
            return false;
        }
        BackgroundThread.get().getThreadHandler().postDelayed(() -> {
            try {
                mIProfcollect.trace_system(eventName);
@@ -69,6 +79,9 @@ public final class Utils {
        if (mIProfcollect == null) {
            return false;
        }
        if (isInCooldownOrReset()) {
            return false;
        }
        BackgroundThread.get().getThreadHandler().post(() -> {
            try {
                mIProfcollect.trace_process(eventName,
@@ -80,4 +93,16 @@ public final class Utils {
        });
        return true;
    }

    /**
     * Returns true if the last trace is within the cooldown period. If the last trace is outside
     * the cooldown period, the last trace time is reset to the current time.
     */
    private static boolean isInCooldownOrReset() {
        if (!Instant.now().isBefore(lastTraceTime.plusSeconds(TRACE_COOLDOWN_SECONDS))) {
            lastTraceTime = Instant.now();
            return false;
        }
        return true;
    }
}