Load jit compiler pre zygote fork

Reduces PSS for libart-compiler.so from 115KB to 2KB per app.

Also some clean-up.

Bug: 27810774

Change-Id: I9938b022e90c58e7978c5038338d0bcbe2a1dc2b
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index 8ca60b2..70d47ef 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -38,6 +38,15 @@
 
 static constexpr bool kEnableOnStackReplacement = true;
 
+// JIT compiler
+void* Jit::jit_library_handle_= nullptr;
+void* Jit::jit_compiler_handle_ = nullptr;
+void* (*Jit::jit_load_)(bool*) = nullptr;
+void (*Jit::jit_unload_)(void*) = nullptr;
+bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
+void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
+bool Jit::generate_debug_info_ = false;
+
 JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
   auto* jit_options = new JitOptions;
   jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
@@ -96,22 +105,16 @@
   cumulative_timings_.AddLogger(logger);
 }
 
-Jit::Jit() : jit_library_handle_(nullptr),
-             jit_compiler_handle_(nullptr),
-             jit_load_(nullptr),
-             jit_compile_method_(nullptr),
-             dump_info_on_shutdown_(false),
+Jit::Jit() : dump_info_on_shutdown_(false),
              cumulative_timings_("JIT timings"),
              memory_use_("Memory used for compilation", 16),
              lock_("JIT memory use lock"),
-             save_profiling_info_(false),
-             generate_debug_info_(false) {
-}
+             save_profiling_info_(false) {}
 
 Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
   std::unique_ptr<Jit> jit(new Jit);
   jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
-  if (!jit->LoadCompiler(error_msg)) {
+  if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
     return nullptr;
   }
   jit->code_cache_.reset(JitCodeCache::Create(
@@ -252,9 +255,11 @@
   DeleteThreadPool();
   if (jit_compiler_handle_ != nullptr) {
     jit_unload_(jit_compiler_handle_);
+    jit_compiler_handle_ = nullptr;
   }
   if (jit_library_handle_ != nullptr) {
     dlclose(jit_library_handle_);
+    jit_library_handle_ = nullptr;
   }
 }
 
diff --git a/runtime/jit/jit.h b/runtime/jit/jit.h
index e3fa89d..8786828 100644
--- a/runtime/jit/jit.h
+++ b/runtime/jit/jit.h
@@ -110,17 +110,18 @@
                                         JValue* result)
       SHARED_REQUIRES(Locks::mutator_lock_);
 
+  static bool LoadCompiler(std::string* error_msg);
+
  private:
   Jit();
-  bool LoadCompiler(std::string* error_msg);
 
   // JIT compiler
-  void* jit_library_handle_;
-  void* jit_compiler_handle_;
-  void* (*jit_load_)(bool*);
-  void (*jit_unload_)(void*);
-  bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
-  void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
+  static void* jit_library_handle_;
+  static void* jit_compiler_handle_;
+  static void* (*jit_load_)(bool*);
+  static void (*jit_unload_)(void*);
+  static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
+  static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
 
   // Performance monitoring.
   bool dump_info_on_shutdown_;
@@ -132,7 +133,7 @@
   std::unique_ptr<jit::JitCodeCache> code_cache_;
 
   bool save_profiling_info_;
-  bool generate_debug_info_;
+  static bool generate_debug_info_;
 
   DISALLOW_COPY_AND_ASSIGN(Jit);
 };
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 79af14f..b78a3d4 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -567,10 +567,16 @@
     }
   }
 
-  // If we are the zygote then we need to wait until after forking to create the code cache
-  // due to SELinux restrictions on r/w/x memory regions.
-  if (!IsZygote() && jit_options_->UseJIT()) {
-    CreateJit();
+  if (jit_options_->UseJIT()) {
+    std::string error_msg;
+    if (!IsZygote()) {
+    // If we are the zygote then we need to wait until after forking to create the code cache
+    // due to SELinux restrictions on r/w/x memory regions.
+      CreateJit();
+    } else if (!jit::Jit::LoadCompiler(&error_msg)) {
+      // Try to load compiler pre zygote to reduce PSS. b/27744947
+      LOG(WARNING) << "Failed to load JIT compiler with error " << error_msg;
+    }
   }
 
   if (!IsImageDex2OatEnabled() || !GetHeap()->HasBootImageSpace()) {