Merge "profcollect: Refactor tracing code to a util class" into main am: 7ef0560fe3 am: 7eb4562957

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

Change-Id: I97e7c091d2c6e8d9b0d64fe02bbb1fc20dae9b5d
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
index 8332b8b..a4dfaa1 100644
--- a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
+++ b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
@@ -275,26 +275,15 @@
         launchObserverRegistry.registerLaunchObserver(mAppLaunchObserver);
     }
 
-    private void traceOnAppStart(String packageName) {
-        if (mIProfcollect == null) {
-            return;
-        }
-
-        if (Utils.withFrequency("applaunch_trace_freq", 2)) {
-            BackgroundThread.get().getThreadHandler().post(() -> {
-                try {
-                    mIProfcollect.trace_system("applaunch");
-                } catch (RemoteException e) {
-                    Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
-                }
-            });
-        }
-    }
-
     private class AppLaunchObserver extends ActivityMetricsLaunchObserver {
         @Override
         public void onIntentStarted(Intent intent, long timestampNanos) {
-            traceOnAppStart(intent.getPackage());
+            if (mIProfcollect == null) {
+                return;
+            }
+            if (Utils.withFrequency("applaunch_trace_freq", 2)) {
+                Utils.traceSystem(mIProfcollect, "applaunch");
+            }
         }
     }
 
@@ -316,13 +305,7 @@
         }
         if (Utils.withFrequency("dex2oat_trace_freq", 25)) {
             // Dex2oat could take a while before it starts. Add a short delay before start tracing.
-            BackgroundThread.get().getThreadHandler().postDelayed(() -> {
-                try {
-                    mIProfcollect.trace_system("dex2oat");
-                } catch (RemoteException e) {
-                    Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
-                }
-            }, 1000);
+            Utils.traceSystem(mIProfcollect, "dex2oat", /* delayMs */ 1000);
         }
     }
 
@@ -385,20 +368,10 @@
                     return;
                 }
                 if (Utils.withFrequency("camera_trace_freq", 10)) {
-                    final int traceDuration = 5000;
-                    final String traceTag = "camera";
-                    BackgroundThread.get().getThreadHandler().post(() -> {
-                        if (mIProfcollect == null) {
-                            return;
-                        }
-                        try {
-                            mIProfcollect.trace_process(traceTag,
-                                "android.hardware.camera.provider",
-                                traceDuration);
-                        } catch (RemoteException e) {
-                            Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
-                        }
-                    });
+                    Utils.traceProcess(mIProfcollect,
+                            "camera",
+                            "android.hardware.camera.provider",
+                            /* durationMs */ 5000);
                 }
             }
         }, null);
diff --git a/services/profcollect/src/com/android/server/profcollect/Utils.java b/services/profcollect/src/com/android/server/profcollect/Utils.java
index d5ef14c..8508802 100644
--- a/services/profcollect/src/com/android/server/profcollect/Utils.java
+++ b/services/profcollect/src/com/android/server/profcollect/Utils.java
@@ -16,17 +16,67 @@
 
 package com.android.server.profcollect;
 
+import static com.android.server.profcollect.ProfcollectForwardingService.LOG_TAG;
+
+import android.os.RemoteException;
 import android.provider.DeviceConfig;
+import android.util.Log;
+
+import com.android.internal.os.BackgroundThread;
 
 import java.util.concurrent.ThreadLocalRandom;
 
 public final class Utils {
 
-  public static boolean withFrequency(String configName, int defaultFrequency) {
+    public static boolean withFrequency(String configName, int defaultFrequency) {
         int threshold = DeviceConfig.getInt(
                 DeviceConfig.NAMESPACE_PROFCOLLECT_NATIVE_BOOT, configName, defaultFrequency);
         int randomNum = ThreadLocalRandom.current().nextInt(100);
         return randomNum < threshold;
     }
 
+    public static boolean traceSystem(IProfCollectd mIProfcollect, String eventName) {
+        if (mIProfcollect == null) {
+            return false;
+        }
+        BackgroundThread.get().getThreadHandler().post(() -> {
+            try {
+                mIProfcollect.trace_system(eventName);
+            } catch (RemoteException e) {
+                Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
+            }
+        });
+        return true;
+    }
+
+    public static boolean traceSystem(IProfCollectd mIProfcollect, String eventName, int delayMs) {
+        if (mIProfcollect == null) {
+            return false;
+        }
+        BackgroundThread.get().getThreadHandler().postDelayed(() -> {
+            try {
+                mIProfcollect.trace_system(eventName);
+            } catch (RemoteException e) {
+                Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
+            }
+        }, delayMs);
+        return true;
+    }
+
+    public static boolean traceProcess(IProfCollectd mIProfcollect,
+            String eventName, String processName, int durationMs) {
+        if (mIProfcollect == null) {
+            return false;
+        }
+        BackgroundThread.get().getThreadHandler().post(() -> {
+            try {
+                mIProfcollect.trace_process(eventName,
+                        processName,
+                        durationMs);
+            } catch (RemoteException e) {
+                Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
+            }
+        });
+        return true;
+    }
 }
\ No newline at end of file