1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.nfc; 18 19 import android.os.SystemClock; 20 import android.util.Log; 21 import android.util.SparseLongArray; 22 23 import java.io.IOException; 24 25 /** 26 * Copied over from frameworks/base/core/java/com/android/internal/util/PerfettoTrigger.java 27 */ 28 public class PerfettoTrigger { 29 private static final String TAG = "PerfettoTrigger"; 30 private static final String TRIGGER_COMMAND = "/system/bin/trigger_perfetto"; 31 private static final long THROTTLE_MILLIS = 300000; 32 private static final SparseLongArray sLastInvocationPerTrigger = new SparseLongArray(100); 33 private static final Object sLock = new Object(); 34 35 /** 36 * @param triggerName The name of the trigger. Must match the value defined in the AOT 37 * Perfetto config. 38 */ trigger(String triggerName)39 public static void trigger(String triggerName) { 40 // Trace triggering has a non-negligible cost (fork+exec). 41 // To mitigate potential excessive triggering by the API client we ignore calls that happen 42 // too quickly after the most recent trigger. 43 synchronized (sLock) { 44 long lastTrigger = sLastInvocationPerTrigger.get(triggerName.hashCode()); 45 long sinceLastTrigger = SystemClock.elapsedRealtime() - lastTrigger; 46 if (sinceLastTrigger < THROTTLE_MILLIS) { 47 Log.v(TAG, "trigger: Not triggering " + triggerName 48 + " - not enough time since last trigger"); 49 return; 50 } 51 52 sLastInvocationPerTrigger.put(triggerName.hashCode(), SystemClock.elapsedRealtime()); 53 } 54 55 try { 56 ProcessBuilder pb = new ProcessBuilder(TRIGGER_COMMAND, triggerName); 57 Log.v(TAG, "trigger: " + String.join(" ", pb.command())); 58 pb.start(); 59 } catch (IOException e) { 60 Log.w(TAG, "trigger: failed " + triggerName, e); 61 } 62 } 63 }