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

Commit cd8ad005 authored by Yi Kong's avatar Yi Kong
Browse files

profcollect: Refactor to use a common method for calculating frequency threshold

Change-Id: If05f941bb8397b42c68f44c504a85230a6f10055
Test: presubmit
parent cac166a1
Loading
Loading
Loading
Loading
+18 −32
Original line number Diff line number Diff line
@@ -46,12 +46,12 @@ import com.android.server.LocalManagerRegistry;
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 @@ public final class ProfcollectForwardingService extends SystemService {
            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 @@ public final class ProfcollectForwardingService extends SystemService {
        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,14 +384,7 @@ public final class ProfcollectForwardingService extends SystemService {
                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;
                }
                if (Utils.withFrequency("camera_trace_freq", 10)) {
                    final int traceDuration = 5000;
                    final String traceTag = "camera";
                    BackgroundThread.get().getThreadHandler().post(() -> {
@@ -408,13 +392,15 @@ public final class ProfcollectForwardingService extends SystemService {
                            return;
                        }
                        try {
                        mIProfcollect.trace_process(traceTag, "android.hardware.camera.provider",
                            mIProfcollect.trace_process(traceTag,
                                "android.hardware.camera.provider",
                                traceDuration);
                        } catch (RemoteException e) {
                            Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
                        }
                    });
                }
            }
        }, null);
    }
}
+32 −0
Original line number Diff line number Diff line
/**
 * 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