Merge "ART: Ignore hotness updates if jit inactive"
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index 7e73e5c..bfe8d9a 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);
@@ -91,22 +100,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(
@@ -247,9 +250,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 37d0bdb..b98c6d3 100644
--- a/runtime/jit/jit.h
+++ b/runtime/jit/jit.h
@@ -112,17 +112,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_;
@@ -134,7 +135,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 96f41b3..a9ff088 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()) {