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); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 42 | jit_options->warmup_threshold_ = |
| 43 | options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 44 | jit_options->dump_info_on_shutdown_ = |
| 45 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 46 | return jit_options; |
| 47 | } |
| 48 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 49 | void Jit::DumpInfo(std::ostream& os) { |
| 50 | os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize()) |
| 51 | << " data cache size=" << PrettySize(code_cache_->DataCacheSize()) |
| 52 | << " num methods=" << code_cache_->NumMethods() |
| 53 | << "\n"; |
| 54 | cumulative_timings_.Dump(os); |
| 55 | } |
| 56 | |
| 57 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 58 | cumulative_timings_.AddLogger(logger); |
| 59 | } |
| 60 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 61 | Jit::Jit() |
| 62 | : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr), |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 63 | jit_compile_method_(nullptr), dump_info_on_shutdown_(false), |
| 64 | cumulative_timings_("JIT timings") { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 68 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 69 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 70 | if (!jit->LoadCompiler(error_msg)) { |
| 71 | return nullptr; |
| 72 | } |
| 73 | jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg)); |
| 74 | if (jit->GetCodeCache() == nullptr) { |
| 75 | return nullptr; |
| 76 | } |
| 77 | LOG(INFO) << "JIT created with code_cache_capacity=" |
| 78 | << PrettySize(options->GetCodeCacheCapacity()) |
| 79 | << " compile_threshold=" << options->GetCompileThreshold(); |
| 80 | return jit.release(); |
| 81 | } |
| 82 | |
| 83 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 84 | jit_library_handle_ = dlopen( |
| 85 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 86 | if (jit_library_handle_ == nullptr) { |
| 87 | std::ostringstream oss; |
| 88 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 89 | *error_msg = oss.str(); |
| 90 | return false; |
| 91 | } |
| 92 | jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>( |
| 93 | dlsym(jit_library_handle_, "jit_load")); |
| 94 | if (jit_load_ == nullptr) { |
| 95 | dlclose(jit_library_handle_); |
| 96 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 97 | return false; |
| 98 | } |
| 99 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 100 | dlsym(jit_library_handle_, "jit_unload")); |
| 101 | if (jit_unload_ == nullptr) { |
| 102 | dlclose(jit_library_handle_); |
| 103 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 104 | return false; |
| 105 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 106 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 107 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 108 | if (jit_compile_method_ == nullptr) { |
| 109 | dlclose(jit_library_handle_); |
| 110 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 111 | return false; |
| 112 | } |
| 113 | CompilerCallbacks* callbacks = nullptr; |
| 114 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 115 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
| 116 | jit_compiler_handle_ = (jit_load_)(&callbacks); |
| 117 | if (jit_compiler_handle_ == nullptr) { |
| 118 | dlclose(jit_library_handle_); |
| 119 | *error_msg = "JIT couldn't load compiler"; |
| 120 | return false; |
| 121 | } |
| 122 | if (callbacks == nullptr) { |
| 123 | dlclose(jit_library_handle_); |
| 124 | *error_msg = "JIT compiler callbacks were not set"; |
| 125 | jit_compiler_handle_ = nullptr; |
| 126 | return false; |
| 127 | } |
| 128 | compiler_callbacks_ = callbacks; |
| 129 | return true; |
| 130 | } |
| 131 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 132 | bool Jit::CompileMethod(ArtMethod* method, Thread* self) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 133 | DCHECK(!method->IsRuntimeMethod()); |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 134 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 135 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 136 | return false; |
| 137 | } |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 138 | return jit_compile_method_(jit_compiler_handle_, method, self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void Jit::CreateThreadPool() { |
| 142 | CHECK(instrumentation_cache_.get() != nullptr); |
| 143 | instrumentation_cache_->CreateThreadPool(); |
| 144 | } |
| 145 | |
| 146 | void Jit::DeleteThreadPool() { |
| 147 | if (instrumentation_cache_.get() != nullptr) { |
| 148 | instrumentation_cache_->DeleteThreadPool(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | Jit::~Jit() { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 153 | if (dump_info_on_shutdown_) { |
| 154 | DumpInfo(LOG(INFO)); |
| 155 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 156 | DeleteThreadPool(); |
| 157 | if (jit_compiler_handle_ != nullptr) { |
| 158 | jit_unload_(jit_compiler_handle_); |
| 159 | } |
| 160 | if (jit_library_handle_ != nullptr) { |
| 161 | dlclose(jit_library_handle_); |
| 162 | } |
| 163 | } |
| 164 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 165 | void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 166 | CHECK_GT(compile_threshold, 0U); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 167 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 168 | // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile |
| 169 | // something. |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 170 | instrumentation_cache_.reset( |
| 171 | new jit::JitInstrumentationCache(compile_threshold, warmup_threshold)); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 172 | Runtime::Current()->GetInstrumentation()->AddListener( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 173 | new jit::JitInstrumentationListener(instrumentation_cache_.get()), |
| 174 | instrumentation::Instrumentation::kMethodEntered | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 175 | instrumentation::Instrumentation::kBackwardBranch | |
| 176 | instrumentation::Instrumentation::kInvokeVirtualOrInterface); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } // namespace jit |
| 180 | } // namespace art |