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" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 27 | #include "oat_file_manager.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 28 | #include "oat_quick_method_header.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 29 | #include "offline_profiling_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 30 | #include "profile_saver.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 31 | #include "runtime.h" |
| 32 | #include "runtime_options.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 33 | #include "stack_map.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "utils.h" |
| 35 | |
| 36 | namespace art { |
| 37 | namespace jit { |
| 38 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 39 | static constexpr bool kEnableOnStackReplacement = true; |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 40 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 41 | // JIT compiler |
| 42 | void* Jit::jit_library_handle_= nullptr; |
| 43 | void* Jit::jit_compiler_handle_ = nullptr; |
| 44 | void* (*Jit::jit_load_)(bool*) = nullptr; |
| 45 | void (*Jit::jit_unload_)(void*) = nullptr; |
| 46 | bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr; |
| 47 | void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr; |
| 48 | bool Jit::generate_debug_info_ = false; |
| 49 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 50 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 51 | auto* jit_options = new JitOptions; |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 52 | jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 53 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 54 | jit_options->code_cache_initial_capacity_ = |
| 55 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 56 | jit_options->code_cache_max_capacity_ = |
| 57 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 58 | jit_options->dump_info_on_shutdown_ = |
| 59 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 60 | jit_options->save_profiling_info_ = |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 61 | options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo); |
| 62 | |
| 63 | jit_options->compile_threshold_ = options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
| 64 | if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 65 | LOG(FATAL) << "Method compilation threshold is above its internal limit."; |
| 66 | } |
| 67 | |
| 68 | if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) { |
| 69 | jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold); |
| 70 | if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 71 | LOG(FATAL) << "Method warmup threshold is above its internal limit."; |
| 72 | } |
| 73 | } else { |
| 74 | jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2; |
| 75 | } |
| 76 | |
| 77 | if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) { |
| 78 | jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold); |
| 79 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 80 | LOG(FATAL) << "Method on stack replacement threshold is above its internal limit."; |
| 81 | } |
| 82 | } else { |
| 83 | jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2; |
| 84 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 85 | jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max(); |
| 86 | } |
| 87 | } |
| 88 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 89 | if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) { |
| 90 | jit_options->priority_thread_weight_ = |
| 91 | *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight); |
| 92 | if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) { |
| 93 | LOG(FATAL) << "Priority thread weight is above the warmup threshold."; |
| 94 | } else if (jit_options->priority_thread_weight_ == 0) { |
| 95 | LOG(FATAL) << "Priority thread weight cannot be 0."; |
| 96 | } |
| 97 | } else { |
| 98 | jit_options->priority_thread_weight_ = |
| 99 | std::max(jit_options->compile_threshold_ / 2000, static_cast<size_t>(1)); |
| 100 | } |
| 101 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 102 | return jit_options; |
| 103 | } |
| 104 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 105 | bool Jit::ShouldUsePriorityThreadWeight() { |
| 106 | // TODO(calin): verify that IsSensitiveThread covers only the cases we are interested on. |
| 107 | // In particular if apps can set StrictMode policies for any of their threads, case in which |
| 108 | // we need to find another way to track sensitive threads. |
| 109 | return Runtime::Current()->InJankPerceptibleProcessState() && Thread::IsSensitiveThread(); |
| 110 | } |
| 111 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 112 | void Jit::DumpInfo(std::ostream& os) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 113 | code_cache_->Dump(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 114 | cumulative_timings_.Dump(os); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 115 | MutexLock mu(Thread::Current(), lock_); |
| 116 | memory_use_.PrintMemoryUse(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 120 | cumulative_timings_.AddLogger(logger); |
| 121 | } |
| 122 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 123 | Jit::Jit() : dump_info_on_shutdown_(false), |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 124 | cumulative_timings_("JIT timings"), |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 125 | memory_use_("Memory used for compilation", 16), |
| 126 | lock_("JIT memory use lock"), |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 127 | save_profiling_info_(false) {} |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 128 | |
| 129 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 130 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 131 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 132 | if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 133 | return nullptr; |
| 134 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 135 | jit->code_cache_.reset(JitCodeCache::Create( |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 136 | options->GetCodeCacheInitialCapacity(), |
| 137 | options->GetCodeCacheMaxCapacity(), |
| 138 | jit->generate_debug_info_, |
| 139 | error_msg)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 140 | if (jit->GetCodeCache() == nullptr) { |
| 141 | return nullptr; |
| 142 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 143 | jit->save_profiling_info_ = options->GetSaveProfilingInfo(); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 144 | VLOG(jit) << "JIT created with initial_capacity=" |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 145 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 146 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 147 | << ", compile_threshold=" << options->GetCompileThreshold() |
| 148 | << ", save_profiling_info=" << options->GetSaveProfilingInfo(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 149 | return jit.release(); |
| 150 | } |
| 151 | |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 152 | bool Jit::LoadCompilerLibrary(std::string* error_msg) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 153 | jit_library_handle_ = dlopen( |
| 154 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 155 | if (jit_library_handle_ == nullptr) { |
| 156 | std::ostringstream oss; |
| 157 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 158 | *error_msg = oss.str(); |
| 159 | return false; |
| 160 | } |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 161 | jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 162 | if (jit_load_ == nullptr) { |
| 163 | dlclose(jit_library_handle_); |
| 164 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 165 | return false; |
| 166 | } |
| 167 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 168 | dlsym(jit_library_handle_, "jit_unload")); |
| 169 | if (jit_unload_ == nullptr) { |
| 170 | dlclose(jit_library_handle_); |
| 171 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 172 | return false; |
| 173 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 174 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 175 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 176 | if (jit_compile_method_ == nullptr) { |
| 177 | dlclose(jit_library_handle_); |
| 178 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 179 | return false; |
| 180 | } |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 181 | jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>( |
| 182 | dlsym(jit_library_handle_, "jit_types_loaded")); |
| 183 | if (jit_types_loaded_ == nullptr) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 184 | dlclose(jit_library_handle_); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 185 | *error_msg = "JIT couldn't find jit_types_loaded entry point"; |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 192 | if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) { |
| 193 | return false; |
| 194 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 195 | bool will_generate_debug_symbols = false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 196 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 197 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 198 | jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 199 | if (jit_compiler_handle_ == nullptr) { |
| 200 | dlclose(jit_library_handle_); |
| 201 | *error_msg = "JIT couldn't load compiler"; |
| 202 | return false; |
| 203 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 204 | generate_debug_info_ = will_generate_debug_symbols; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 208 | bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 209 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 210 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 211 | // Don't compile the method if it has breakpoints. |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 212 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 213 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 214 | return false; |
| 215 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 216 | |
| 217 | // Don't compile the method if we are supposed to be deoptimized. |
| 218 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 219 | if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 220 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to deoptimization"; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 224 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 225 | // of that proxy method, as the compiler does not expect a proxy method. |
| 226 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*)); |
| 227 | if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 228 | return false; |
| 229 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 230 | bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 231 | code_cache_->DoneCompiling(method_to_compile, self, osr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 232 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void Jit::CreateThreadPool() { |
| 236 | CHECK(instrumentation_cache_.get() != nullptr); |
| 237 | instrumentation_cache_->CreateThreadPool(); |
| 238 | } |
| 239 | |
| 240 | void Jit::DeleteThreadPool() { |
| 241 | if (instrumentation_cache_.get() != nullptr) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 242 | instrumentation_cache_->DeleteThreadPool(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 246 | void Jit::StartProfileSaver(const std::string& filename, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 247 | const std::vector<std::string>& code_paths, |
| 248 | const std::string& foreign_dex_profile_path, |
| 249 | const std::string& app_dir) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 250 | if (save_profiling_info_) { |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 251 | ProfileSaver::Start(filename, code_cache_.get(), code_paths, foreign_dex_profile_path, app_dir); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 252 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void Jit::StopProfileSaver() { |
| 256 | if (save_profiling_info_ && ProfileSaver::IsStarted()) { |
| 257 | ProfileSaver::Stop(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 261 | bool Jit::JitAtFirstUse() { |
| 262 | if (instrumentation_cache_ != nullptr) { |
| 263 | return instrumentation_cache_->HotMethodThreshold() == 0; |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 268 | bool Jit::CanInvokeCompiledCode(ArtMethod* method) { |
| 269 | return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode()); |
| 270 | } |
| 271 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 272 | Jit::~Jit() { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 273 | DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted()); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 274 | if (dump_info_on_shutdown_) { |
| 275 | DumpInfo(LOG(INFO)); |
| 276 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 277 | DeleteThreadPool(); |
| 278 | if (jit_compiler_handle_ != nullptr) { |
| 279 | jit_unload_(jit_compiler_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 280 | jit_compiler_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 281 | } |
| 282 | if (jit_library_handle_ != nullptr) { |
| 283 | dlclose(jit_library_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 284 | jit_library_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 288 | void Jit::CreateInstrumentationCache(size_t compile_threshold, |
| 289 | size_t warmup_threshold, |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 290 | size_t osr_threshold, |
| 291 | uint16_t priority_thread_weight) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 292 | instrumentation_cache_.reset( |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 293 | new jit::JitInstrumentationCache(compile_threshold, |
| 294 | warmup_threshold, |
| 295 | osr_threshold, |
| 296 | priority_thread_weight)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 299 | void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { |
| 300 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 301 | if (jit != nullptr && jit->generate_debug_info_) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 302 | DCHECK(jit->jit_types_loaded_ != nullptr); |
| 303 | jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { |
| 308 | struct CollectClasses : public ClassVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 309 | bool operator()(mirror::Class* klass) override { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 310 | classes_.push_back(klass); |
| 311 | return true; |
| 312 | } |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 313 | std::vector<mirror::Class*> classes_; |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | if (generate_debug_info_) { |
| 317 | ScopedObjectAccess so(Thread::Current()); |
| 318 | |
| 319 | CollectClasses visitor; |
| 320 | linker->VisitClasses(&visitor); |
| 321 | jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size()); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 325 | extern "C" void art_quick_osr_stub(void** stack, |
| 326 | uint32_t stack_size_in_bytes, |
| 327 | const uint8_t* native_pc, |
| 328 | JValue* result, |
| 329 | const char* shorty, |
| 330 | Thread* self); |
| 331 | |
| 332 | bool Jit::MaybeDoOnStackReplacement(Thread* thread, |
| 333 | ArtMethod* method, |
| 334 | uint32_t dex_pc, |
| 335 | int32_t dex_pc_offset, |
| 336 | JValue* result) { |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 337 | if (!kEnableOnStackReplacement) { |
| 338 | return false; |
| 339 | } |
| 340 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 341 | Jit* jit = Runtime::Current()->GetJit(); |
| 342 | if (jit == nullptr) { |
| 343 | return false; |
| 344 | } |
| 345 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 346 | if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) { |
| 347 | // Don't attempt to do an OSR if we are close to the stack limit. Since |
| 348 | // the interpreter frames are still on stack, OSR has the potential |
| 349 | // to stack overflow even for a simple loop. |
| 350 | // b/27094810. |
| 351 | return false; |
| 352 | } |
| 353 | |
Nicolas Geoffray | d9bc433 | 2016-02-05 23:32:25 +0000 | [diff] [blame] | 354 | // Get the actual Java method if this method is from a proxy class. The compiler |
| 355 | // and the JIT code cache do not expect methods from proxy classes. |
| 356 | method = method->GetInterfaceMethodIfProxy(sizeof(void*)); |
| 357 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 358 | // Cheap check if the method has been compiled already. That's an indicator that we should |
| 359 | // osr into it. |
| 360 | if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 361 | return false; |
| 362 | } |
| 363 | |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 364 | // Fetch some data before looking up for an OSR method. We don't want thread |
| 365 | // suspension once we hold an OSR method, as the JIT code cache could delete the OSR |
| 366 | // method while we are being suspended. |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 367 | const size_t number_of_vregs = method->GetCodeItem()->registers_size_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 368 | const char* shorty = method->GetShorty(); |
| 369 | std::string method_name(VLOG_IS_ON(jit) ? PrettyMethod(method) : ""); |
| 370 | void** memory = nullptr; |
| 371 | size_t frame_size = 0; |
| 372 | ShadowFrame* shadow_frame = nullptr; |
| 373 | const uint8_t* native_pc = nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 374 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 375 | { |
| 376 | ScopedAssertNoThreadSuspension sts(thread, "Holding OSR method"); |
| 377 | const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method); |
| 378 | if (osr_method == nullptr) { |
| 379 | // No osr method yet, just return to the interpreter. |
| 380 | return false; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 381 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 382 | |
| 383 | CodeInfo code_info = osr_method->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 384 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 385 | |
| 386 | // Find stack map starting at the target dex_pc. |
| 387 | StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding); |
| 388 | if (!stack_map.IsValid()) { |
| 389 | // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the |
| 390 | // hope that the next branch has one. |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | // We found a stack map, now fill the frame with dex register values from the interpreter's |
| 395 | // shadow frame. |
| 396 | DexRegisterMap vreg_map = |
| 397 | code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs); |
| 398 | |
| 399 | frame_size = osr_method->GetFrameSizeInBytes(); |
| 400 | |
| 401 | // Allocate memory to put shadow frame values. The osr stub will copy that memory to |
| 402 | // stack. |
| 403 | // Note that we could pass the shadow frame to the stub, and let it copy the values there, |
| 404 | // but that is engineering complexity not worth the effort for something like OSR. |
| 405 | memory = reinterpret_cast<void**>(malloc(frame_size)); |
| 406 | CHECK(memory != nullptr); |
| 407 | memset(memory, 0, frame_size); |
| 408 | |
| 409 | // Art ABI: ArtMethod is at the bottom of the stack. |
| 410 | memory[0] = method; |
| 411 | |
| 412 | shadow_frame = thread->PopShadowFrame(); |
| 413 | if (!vreg_map.IsValid()) { |
| 414 | // If we don't have a dex register map, then there are no live dex registers at |
| 415 | // this dex pc. |
| 416 | } else { |
| 417 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
| 418 | DexRegisterLocation::Kind location = |
| 419 | vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); |
| 420 | if (location == DexRegisterLocation::Kind::kNone) { |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 421 | // Dex register is dead or uninitialized. |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 422 | continue; |
| 423 | } |
| 424 | |
| 425 | if (location == DexRegisterLocation::Kind::kConstant) { |
| 426 | // We skip constants because the compiled code knows how to handle them. |
| 427 | continue; |
| 428 | } |
| 429 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 430 | DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 431 | |
| 432 | int32_t vreg_value = shadow_frame->GetVReg(vreg); |
| 433 | int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg, |
| 434 | number_of_vregs, |
| 435 | code_info, |
| 436 | encoding); |
| 437 | DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size)); |
| 438 | DCHECK_GT(slot_offset, 0); |
| 439 | (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value; |
| 440 | } |
| 441 | } |
| 442 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 443 | native_pc = stack_map.GetNativePcOffset(encoding.stack_map_encoding) + |
| 444 | osr_method->GetEntryPoint(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 445 | VLOG(jit) << "Jumping to " |
| 446 | << method_name |
| 447 | << "@" |
| 448 | << std::hex << reinterpret_cast<uintptr_t>(native_pc); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 451 | { |
| 452 | ManagedStack fragment; |
| 453 | thread->PushManagedStackFragment(&fragment); |
| 454 | (*art_quick_osr_stub)(memory, |
| 455 | frame_size, |
| 456 | native_pc, |
| 457 | result, |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 458 | shorty, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 459 | thread); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 460 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 461 | if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) { |
| 462 | thread->DeoptimizeWithDeoptimizationException(result); |
| 463 | } |
| 464 | thread->PopManagedStackFragment(fragment); |
| 465 | } |
| 466 | free(memory); |
| 467 | thread->PushShadowFrame(shadow_frame); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 468 | VLOG(jit) << "Done running OSR code for " << method_name; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 472 | void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) { |
| 473 | if (bytes > 4 * MB) { |
| 474 | LOG(INFO) << "Compiler allocated " |
| 475 | << PrettySize(bytes) |
| 476 | << " to compile " |
| 477 | << PrettyMethod(method); |
| 478 | } |
| 479 | MutexLock mu(Thread::Current(), lock_); |
| 480 | memory_use_.AddValue(bytes); |
| 481 | } |
| 482 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 483 | } // namespace jit |
| 484 | } // namespace art |