Preload perfetto_hprof library in Zygote.

This is to reduce the startup time for profileable or debuggable apps.

Test: flash, run benchmarks

(cherry picked from commit 0278b4bfb6528c929120f99dbef67d36e55bc6de)

Bug: 147667830
Change-Id: Iee5d60664cad5b6e445c2291d6de07f2bb555017
Merged-In: Iee5d60664cad5b6e445c2291d6de07f2bb555017
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 8345d77..7646a13 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1788,6 +1788,14 @@
     callbacks_->NextRuntimePhase(RuntimePhaseCallback::RuntimePhase::kInitialAgents);
   }
 
+  if (IsZygote() && IsPerfettoHprofEnabled()) {
+      constexpr const char* plugin_name = kIsDebugBuild ?
+      "libperfetto_hprofd.so" : "libperfetto_hprof.so";
+    // Load eagerly in Zygote to improve app startup times. This will make
+    // subsequent dlopens for the library no-ops.
+    dlopen(plugin_name, RTLD_NOW | RTLD_LOCAL);
+  }
+
   VLOG(startup) << "Runtime::Init exiting";
 
   // Set OnlyUseSystemOatFiles only after boot classpath has been set up.