diff --git a/services/profcollect/Android.bp b/services/profcollect/Android.bp index 2040bb6e544f4e7d90e235739a60a18005cbb8bb..fe431f5c88678f51d43b96d49cd53638b5ec76a3 100644 --- a/services/profcollect/Android.bp +++ b/services/profcollect/Android.bp @@ -22,24 +22,25 @@ package { } filegroup { - name: "services.profcollect-javasources", - srcs: ["src/**/*.java"], - path: "src", - visibility: ["//frameworks/base/services"], + name: "services.profcollect-javasources", + srcs: ["src/**/*.java"], + path: "src", + visibility: ["//frameworks/base/services"], } filegroup { - name: "services.profcollect-sources", - srcs: [ - ":services.profcollect-javasources", - ":profcollectd_aidl", - ], - visibility: ["//frameworks/base/services:__subpackages__"], + name: "services.profcollect-sources", + srcs: [ + ":services.profcollect-javasources", + ":profcollectd_aidl", + ], + visibility: ["//frameworks/base/services:__subpackages__"], } java_library_static { - name: "services.profcollect", - defaults: ["platform_service_defaults"], - srcs: [":services.profcollect-sources"], - libs: ["services.core"], + name: "services.profcollect", + defaults: ["platform_service_defaults"], + srcs: [":services.profcollect-sources"], + static_libs: ["services.core"], + libs: ["service-art.stubs.system_server"], } diff --git a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java index 4007672a0599c78335cdaf89d100a2888b35151f..582b712ec3fc9c536d0cc013fcc6252ec0011faf 100644 --- a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java +++ b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java @@ -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() {