profcollect: Refactor to use a common method for calculating frequency threshold
Change-Id: If05f941bb8397b42c68f44c504a85230a6f10055
Test: presubmit
diff --git a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
index dab3978..8332b8b 100644
--- a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
+++ b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
@@ -46,12 +46,12 @@
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.art.ArtManagerLocal;
+import com.android.server.profcollect.Utils;
import com.android.server.wm.ActivityMetricsLaunchObserver;
import com.android.server.wm.ActivityMetricsLaunchObserverRegistry;
import com.android.server.wm.ActivityTaskManagerInternal;
import java.util.Arrays;
-import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
/**
@@ -280,11 +280,7 @@
return;
}
- // Sample for a fraction of app launches.
- int traceFrequency = DeviceConfig.getInt(DeviceConfig.NAMESPACE_PROFCOLLECT_NATIVE_BOOT,
- "applaunch_trace_freq", 2);
- int randomNum = ThreadLocalRandom.current().nextInt(100);
- if (randomNum < traceFrequency) {
+ if (Utils.withFrequency("applaunch_trace_freq", 2)) {
BackgroundThread.get().getThreadHandler().post(() -> {
try {
mIProfcollect.trace_system("applaunch");
@@ -318,12 +314,7 @@
if (mIProfcollect == null) {
return;
}
- // Sample for a fraction of dex2oat runs.
- final int traceFrequency =
- DeviceConfig.getInt(DeviceConfig.NAMESPACE_PROFCOLLECT_NATIVE_BOOT,
- "dex2oat_trace_freq", 25);
- int randomNum = ThreadLocalRandom.current().nextInt(100);
- if (randomNum < traceFrequency) {
+ 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 {
@@ -393,27 +384,22 @@
if (Arrays.asList(cameraSkipPackages).contains(packageId)) {
return;
}
- // Sample for a fraction of camera events.
- final int traceFrequency =
- DeviceConfig.getInt(DeviceConfig.NAMESPACE_PROFCOLLECT_NATIVE_BOOT,
- "camera_trace_freq", 10);
- int randomNum = ThreadLocalRandom.current().nextInt(100);
- if (randomNum >= traceFrequency) {
- return;
- }
- 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",
+ 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());
- }
- });
+ } catch (RemoteException e) {
+ Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
+ }
+ });
+ }
}
}, null);
}
diff --git a/services/profcollect/src/com/android/server/profcollect/Utils.java b/services/profcollect/src/com/android/server/profcollect/Utils.java
new file mode 100644
index 0000000..d5ef14c
--- /dev/null
+++ b/services/profcollect/src/com/android/server/profcollect/Utils.java
@@ -0,0 +1,32 @@
+/**
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.profcollect;
+
+import android.provider.DeviceConfig;
+
+import java.util.concurrent.ThreadLocalRandom;
+
+public final class Utils {
+
+ 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;
+ }
+
+}
\ No newline at end of file