Fix JIT creation so it picks up runtime flags.

bug: 119063276
Test: benchmarks, take a bugreport, check that JIT is enabled.
Test: atest ZenModeFilteringTest with libartd and JIT at first use.
Change-Id: Id9f0be316467064dc21edfb0a0493149678630e0
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index e876a1b..d67d9dc 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -60,7 +60,6 @@
 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;
 
 struct StressModeHelper {
   DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
@@ -176,11 +175,24 @@
       lock_("JIT memory use lock") {}
 
 Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) {
-  CHECK(jit_compiler_handle_ != nullptr) << "Jit::LoadLibrary() needs to be called first";
-  std::unique_ptr<Jit> jit(new Jit(code_cache, options));
-  if (jit_compiler_handle_ == nullptr) {
+  if (jit_load_ == nullptr) {
+    LOG(WARNING) << "Not creating JIT: library not loaded";
     return nullptr;
   }
+  bool will_generate_debug_symbols = false;
+  jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
+  if (jit_compiler_handle_ == nullptr) {
+    LOG(WARNING) << "Not creating JIT: failed to allocate a compiler";
+    return nullptr;
+  }
+  std::unique_ptr<Jit> jit(new Jit(code_cache, options));
+  jit->generate_debug_info_ = will_generate_debug_symbols;
+
+  // With 'perf', we want a 1-1 mapping between an address and a method.
+  // We aren't able to keep method pointers live during the instrumentation method entry trampoline
+  // so we will just disable jit-gc if we are doing that.
+  code_cache->SetGarbageCollectCode(!jit->generate_debug_info_ &&
+      !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
 
   VLOG(jit) << "JIT created with initial_capacity="
       << PrettySize(options->GetCodeCacheInitialCapacity())
@@ -195,7 +207,7 @@
   return jit.release();
 }
 
-bool Jit::BindCompilerMethods(std::string* error_msg) {
+bool Jit::LoadCompilerLibrary(std::string* error_msg) {
   jit_library_handle_ = dlopen(
       kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
   if (jit_library_handle_ == nullptr) {
@@ -234,23 +246,6 @@
   return true;
 }
 
-bool Jit::LoadCompiler(std::string* error_msg) {
-  if (jit_library_handle_ == nullptr && !BindCompilerMethods(error_msg)) {
-    return false;
-  }
-  bool will_generate_debug_symbols = false;
-  VLOG(jit) << "Calling JitLoad interpreter_only="
-      << Runtime::Current()->GetInstrumentation()->InterpretOnly();
-  jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
-  if (jit_compiler_handle_ == nullptr) {
-    dlclose(jit_library_handle_);
-    *error_msg = "JIT couldn't load compiler";
-    return false;
-  }
-  generate_debug_info_ = will_generate_debug_symbols;
-  return true;
-}
-
 bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
   DCHECK(Runtime::Current()->UseJitCompilation());
   DCHECK(!method->IsRuntimeMethod());
@@ -300,11 +295,6 @@
   return success;
 }
 
-bool Jit::ShouldGenerateDebugInfo() {
-  CHECK(CompilerIsLoaded());
-  return generate_debug_info_;
-}
-
 void Jit::CreateThreadPool() {
   // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
   // is not null when we instrument.
@@ -385,7 +375,7 @@
     return;
   }
   jit::Jit* jit = Runtime::Current()->GetJit();
-  if (generate_debug_info_) {
+  if (jit->generate_debug_info_) {
     DCHECK(jit->jit_types_loaded_ != nullptr);
     jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
   }
diff --git a/runtime/jit/jit.h b/runtime/jit/jit.h
index b0ea19b..46b0762 100644
--- a/runtime/jit/jit.h
+++ b/runtime/jit/jit.h
@@ -282,13 +282,8 @@
                                         JValue* result)
       REQUIRES_SHARED(Locks::mutator_lock_);
 
-  // Load and initialize compiler.
-  static bool LoadCompiler(std::string* error_msg);
-
-  static bool CompilerIsLoaded() { return jit_compiler_handle_ != nullptr; }
-
-  // Return whether debug info should be generated. Requires LoadCompiler() to have been called.
-  static bool ShouldGenerateDebugInfo();
+  // Load the compiler library.
+  static bool LoadCompilerLibrary(std::string* error_msg);
 
   ThreadPool* GetThreadPool() const {
     return thread_pool_.get();
@@ -313,8 +308,8 @@
   static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
   static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
 
-  // We make this static to simplify the interaction with libart-compiler.so.
-  static bool generate_debug_info_;
+  // Whether we should generate debug info when compiling.
+  bool generate_debug_info_;
 
   // JIT resources owned by runtime.
   jit::JitCodeCache* const code_cache_;
diff --git a/runtime/jit/jit_code_cache.cc b/runtime/jit/jit_code_cache.cc
index a15a9be..359f97e 100644
--- a/runtime/jit/jit_code_cache.cc
+++ b/runtime/jit/jit_code_cache.cc
@@ -436,12 +436,6 @@
     SetFootprintLimit(current_capacity_);
   }
 
-  // With 'perf', we want a 1-1 mapping between an address and a method.
-  // We aren't able to keep method pointers live during the instrumentation method entry trampoline
-  // so we will just disable jit-gc if we are doing that.
-  garbage_collect_code_ = !Jit::ShouldGenerateDebugInfo() &&
-      !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
-
   VLOG(jit) << "Created jit code cache: initial data size="
             << PrettySize(initial_data_capacity)
             << ", initial code size="
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 3c0125b..e577266 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -789,7 +789,7 @@
   if (!safe_mode_ && (jit_options_->UseJitCompilation() || jit_options_->GetSaveProfilingInfo())) {
     // Try to load compiler pre zygote to reduce PSS. b/27744947
     std::string error_msg;
-    if (!jit::Jit::LoadCompiler(&error_msg)) {
+    if (!jit::Jit::LoadCompilerLibrary(&error_msg)) {
       LOG(WARNING) << "Failed to load JIT compiler with error " << error_msg;
     }
   }