Limit PerfettoTrigger to max 1 trigger per minute

Test: manual
Bug: 161781469
Change-Id: Ie35e1fe7e6a12ab75a60c21f442adcd73372050f
This commit is contained in:
Marcin Oczeretko
2021-02-10 14:26:26 +00:00
parent 13e0d8cfc4
commit a87fab667c

View File

@@ -16,6 +16,7 @@
package com.android.internal.util;
import android.os.SystemClock;
import android.util.Log;
import java.io.IOException;
@@ -27,16 +28,28 @@ import java.io.IOException;
public class PerfettoTrigger {
private static final String TAG = "PerfettoTrigger";
private static final String TRIGGER_COMMAND = "/system/bin/trigger_perfetto";
private static final long THROTTLE_MILLIS = 60000;
private static volatile long sLastTriggerTime = -THROTTLE_MILLIS;
/**
* @param triggerName The name of the trigger. Must match the value defined in the AOT
* Perfetto config.
*/
public static void trigger(String triggerName) {
// Trace triggering has a non-negligible cost (fork+exec).
// To mitigate potential excessive triggering by the API client we ignore calls that happen
// too quickl after the most recent trigger.
long sinceLastTrigger = SystemClock.elapsedRealtime() - sLastTriggerTime;
if (sinceLastTrigger < THROTTLE_MILLIS) {
Log.v(TAG, "Not triggering " + triggerName + " - not enough time since last trigger");
return;
}
try {
ProcessBuilder pb = new ProcessBuilder(TRIGGER_COMMAND, triggerName);
Log.v(TAG, "Triggering " + String.join(" ", pb.command()));
Process process = pb.start();
pb.start();
sLastTriggerTime = SystemClock.elapsedRealtime();
} catch (IOException e) {
Log.w(TAG, "Failed to trigger " + triggerName, e);
}