Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "jit.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame^] | 22 | #include "debugger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 23 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 24 | #include "interpreter/interpreter.h" |
| 25 | #include "jit_code_cache.h" |
| 26 | #include "jit_instrumentation.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 27 | #include "runtime.h" |
| 28 | #include "runtime_options.h" |
| 29 | #include "thread_list.h" |
| 30 | #include "utils.h" |
| 31 | |
| 32 | namespace art { |
| 33 | namespace jit { |
| 34 | |
| 35 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 36 | auto* jit_options = new JitOptions; |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 37 | jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 38 | jit_options->code_cache_capacity_ = |
| 39 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheCapacity); |
| 40 | jit_options->compile_threshold_ = |
| 41 | options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 42 | jit_options->dump_info_on_shutdown_ = |
| 43 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 44 | return jit_options; |
| 45 | } |
| 46 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 47 | void Jit::DumpInfo(std::ostream& os) { |
| 48 | os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize()) |
| 49 | << " data cache size=" << PrettySize(code_cache_->DataCacheSize()) |
| 50 | << " num methods=" << code_cache_->NumMethods() |
| 51 | << "\n"; |
| 52 | cumulative_timings_.Dump(os); |
| 53 | } |
| 54 | |
| 55 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 56 | cumulative_timings_.AddLogger(logger); |
| 57 | } |
| 58 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 59 | Jit::Jit() |
| 60 | : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr), |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 61 | jit_compile_method_(nullptr), dump_info_on_shutdown_(false), |
| 62 | cumulative_timings_("JIT timings") { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 66 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 67 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 68 | if (!jit->LoadCompiler(error_msg)) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg)); |
| 72 | if (jit->GetCodeCache() == nullptr) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | LOG(INFO) << "JIT created with code_cache_capacity=" |
| 76 | << PrettySize(options->GetCodeCacheCapacity()) |
| 77 | << " compile_threshold=" << options->GetCompileThreshold(); |
| 78 | return jit.release(); |
| 79 | } |
| 80 | |
| 81 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 82 | jit_library_handle_ = dlopen( |
| 83 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 84 | if (jit_library_handle_ == nullptr) { |
| 85 | std::ostringstream oss; |
| 86 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 87 | *error_msg = oss.str(); |
| 88 | return false; |
| 89 | } |
| 90 | jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>( |
| 91 | dlsym(jit_library_handle_, "jit_load")); |
| 92 | if (jit_load_ == nullptr) { |
| 93 | dlclose(jit_library_handle_); |
| 94 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 95 | return false; |
| 96 | } |
| 97 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 98 | dlsym(jit_library_handle_, "jit_unload")); |
| 99 | if (jit_unload_ == nullptr) { |
| 100 | dlclose(jit_library_handle_); |
| 101 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 102 | return false; |
| 103 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 104 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 105 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 106 | if (jit_compile_method_ == nullptr) { |
| 107 | dlclose(jit_library_handle_); |
| 108 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 109 | return false; |
| 110 | } |
| 111 | CompilerCallbacks* callbacks = nullptr; |
| 112 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 113 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
| 114 | jit_compiler_handle_ = (jit_load_)(&callbacks); |
| 115 | if (jit_compiler_handle_ == nullptr) { |
| 116 | dlclose(jit_library_handle_); |
| 117 | *error_msg = "JIT couldn't load compiler"; |
| 118 | return false; |
| 119 | } |
| 120 | if (callbacks == nullptr) { |
| 121 | dlclose(jit_library_handle_); |
| 122 | *error_msg = "JIT compiler callbacks were not set"; |
| 123 | jit_compiler_handle_ = nullptr; |
| 124 | return false; |
| 125 | } |
| 126 | compiler_callbacks_ = callbacks; |
| 127 | return true; |
| 128 | } |
| 129 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 130 | bool Jit::CompileMethod(ArtMethod* method, Thread* self) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 131 | DCHECK(!method->IsRuntimeMethod()); |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 132 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 133 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 134 | return false; |
| 135 | } |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 136 | return jit_compile_method_(jit_compiler_handle_, method, self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void Jit::CreateThreadPool() { |
| 140 | CHECK(instrumentation_cache_.get() != nullptr); |
| 141 | instrumentation_cache_->CreateThreadPool(); |
| 142 | } |
| 143 | |
| 144 | void Jit::DeleteThreadPool() { |
| 145 | if (instrumentation_cache_.get() != nullptr) { |
| 146 | instrumentation_cache_->DeleteThreadPool(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | Jit::~Jit() { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 151 | if (dump_info_on_shutdown_) { |
| 152 | DumpInfo(LOG(INFO)); |
| 153 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 154 | DeleteThreadPool(); |
| 155 | if (jit_compiler_handle_ != nullptr) { |
| 156 | jit_unload_(jit_compiler_handle_); |
| 157 | } |
| 158 | if (jit_library_handle_ != nullptr) { |
| 159 | dlclose(jit_library_handle_); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void Jit::CreateInstrumentationCache(size_t compile_threshold) { |
| 164 | CHECK_GT(compile_threshold, 0U); |
| 165 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 166 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 167 | // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile |
| 168 | // something. |
| 169 | instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold)); |
| 170 | runtime->GetInstrumentation()->AddListener( |
| 171 | new jit::JitInstrumentationListener(instrumentation_cache_.get()), |
| 172 | instrumentation::Instrumentation::kMethodEntered | |
| 173 | instrumentation::Instrumentation::kBackwardBranch); |
| 174 | runtime->GetThreadList()->ResumeAll(); |
| 175 | } |
| 176 | |
| 177 | } // namespace jit |
| 178 | } // namespace art |