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

Commit 1dd991ee authored by Yi Kong's avatar Yi Kong Committed by Gerrit Code Review
Browse files

Merge "profcollect dex2oat observer" into main

parents 3cf2db8a f8add98c
Loading
Loading
Loading
Loading
+15 −14
Original line number Diff line number Diff line
@@ -41,5 +41,6 @@ java_library_static {
    name: "services.profcollect",
    defaults: ["platform_service_defaults"],
    srcs: [":services.profcollect-sources"],
  libs: ["services.core"],
    static_libs: ["services.core"],
    libs: ["service-art.stubs.system_server"],
}
+42 −0
Original line number Diff line number Diff line
@@ -41,12 +41,15 @@ import android.util.Log;
import com.android.internal.R;
import com.android.internal.os.BackgroundThread;
import com.android.server.IoThread;
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.wm.ActivityMetricsLaunchObserver;
import com.android.server.wm.ActivityMetricsLaunchObserverRegistry;
import com.android.server.wm.ActivityTaskManagerInternal;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@@ -261,6 +264,7 @@ public final class ProfcollectForwardingService extends SystemService {
        BackgroundThread.get().getThreadHandler().post(
                () -> {
                    registerAppLaunchObserver();
                    registerDex2oatObserver();
                    registerOTAObserver();
                });
    }
@@ -304,6 +308,44 @@ public final class ProfcollectForwardingService extends SystemService {
        }
    }

    private void registerDex2oatObserver() {
        ArtManagerLocal aml = LocalManagerRegistry.getManager(ArtManagerLocal.class);
        if (aml == null) {
            Log.w(LOG_TAG, "Couldn't get ArtManagerLocal");
            return;
        }
        aml.setBatchDexoptStartCallback(ForkJoinPool.commonPool(),
                (snapshot, reason, defaultPackages, builder, passedSignal) -> {
                    traceOnDex2oatStart();
                });
    }

    private void traceOnDex2oatStart() {
        if (mIProfcollect == null) {
            return;
        }
        // Sample for a fraction of dex2oat runs.
        final int traceFrequency =
            DeviceConfig.getInt(DeviceConfig.NAMESPACE_PROFCOLLECT_NATIVE_BOOT,
                "dex2oat_trace_freq", 10);
        int randomNum = ThreadLocalRandom.current().nextInt(100);
        if (randomNum < traceFrequency) {
            if (DEBUG) {
                Log.d(LOG_TAG, "Tracing on dex2oat event");
            }
            BackgroundThread.get().getThreadHandler().post(() -> {
                try {
                    // Dex2oat could take a while before it starts. Add a short delay before start
                    // tracing.
                    Thread.sleep(1000);
                    mIProfcollect.trace_once("dex2oat");
                } catch (RemoteException | InterruptedException e) {
                    Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
                }
            });
        }
    }

    private void registerOTAObserver() {
        UpdateEngine updateEngine = new UpdateEngine();
        updateEngine.bind(new UpdateEngineCallback() {