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 | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame^] | 23 | #include "base/memory_tool.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 25 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 26 | #include "interpreter/interpreter.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 27 | #include "java_vm_ext.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 28 | #include "jit_code_cache.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 29 | #include "oat_file_manager.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 30 | #include "oat_quick_method_header.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 31 | #include "profile_compilation_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 32 | #include "profile_saver.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 33 | #include "runtime.h" |
| 34 | #include "runtime_options.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 35 | #include "stack_map.h" |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 36 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace jit { |
| 41 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 42 | static constexpr bool kEnableOnStackReplacement = true; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 43 | // At what priority to schedule jit threads. 9 is the lowest foreground priority on device. |
| 44 | static constexpr int kJitPoolThreadPthreadPriority = 9; |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 45 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 46 | // JIT compiler |
| 47 | void* Jit::jit_library_handle_= nullptr; |
| 48 | void* Jit::jit_compiler_handle_ = nullptr; |
| 49 | void* (*Jit::jit_load_)(bool*) = nullptr; |
| 50 | void (*Jit::jit_unload_)(void*) = nullptr; |
| 51 | bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr; |
| 52 | void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr; |
| 53 | bool Jit::generate_debug_info_ = false; |
| 54 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 55 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 56 | auto* jit_options = new JitOptions; |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 57 | jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 58 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 59 | jit_options->code_cache_initial_capacity_ = |
| 60 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 61 | jit_options->code_cache_max_capacity_ = |
| 62 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 63 | jit_options->dump_info_on_shutdown_ = |
| 64 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 65 | jit_options->profile_saver_options_ = |
| 66 | options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 67 | |
| 68 | jit_options->compile_threshold_ = options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
| 69 | if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 70 | LOG(FATAL) << "Method compilation threshold is above its internal limit."; |
| 71 | } |
| 72 | |
| 73 | if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) { |
| 74 | jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold); |
| 75 | if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 76 | LOG(FATAL) << "Method warmup threshold is above its internal limit."; |
| 77 | } |
| 78 | } else { |
| 79 | jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2; |
| 80 | } |
| 81 | |
| 82 | if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) { |
| 83 | jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold); |
| 84 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 85 | LOG(FATAL) << "Method on stack replacement threshold is above its internal limit."; |
| 86 | } |
| 87 | } else { |
| 88 | jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2; |
| 89 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 90 | jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max(); |
| 91 | } |
| 92 | } |
| 93 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 94 | if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) { |
| 95 | jit_options->priority_thread_weight_ = |
| 96 | *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight); |
| 97 | if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) { |
| 98 | LOG(FATAL) << "Priority thread weight is above the warmup threshold."; |
| 99 | } else if (jit_options->priority_thread_weight_ == 0) { |
| 100 | LOG(FATAL) << "Priority thread weight cannot be 0."; |
| 101 | } |
| 102 | } else { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 103 | jit_options->priority_thread_weight_ = std::max( |
| 104 | jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio, |
| 105 | static_cast<size_t>(1)); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 108 | if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 109 | jit_options->invoke_transition_weight_ = |
| 110 | *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 111 | if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) { |
| 112 | LOG(FATAL) << "Invoke transition weight is above the warmup threshold."; |
| 113 | } else if (jit_options->invoke_transition_weight_ == 0) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 114 | LOG(FATAL) << "Invoke transition weight cannot be 0."; |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 115 | } |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 116 | } else { |
| 117 | jit_options->invoke_transition_weight_ = std::max( |
| 118 | jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 119 | static_cast<size_t>(1)); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 122 | return jit_options; |
| 123 | } |
| 124 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 125 | bool Jit::ShouldUsePriorityThreadWeight() { |
Calin Juravle | 97cbc92 | 2016-04-15 16:16:35 +0100 | [diff] [blame] | 126 | return Runtime::Current()->InJankPerceptibleProcessState() |
| 127 | && Thread::Current()->IsJitSensitiveThread(); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 130 | void Jit::DumpInfo(std::ostream& os) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 131 | code_cache_->Dump(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 132 | cumulative_timings_.Dump(os); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 133 | MutexLock mu(Thread::Current(), lock_); |
| 134 | memory_use_.PrintMemoryUse(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 137 | void Jit::DumpForSigQuit(std::ostream& os) { |
| 138 | DumpInfo(os); |
| 139 | ProfileSaver::DumpInstanceInfo(os); |
| 140 | } |
| 141 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 142 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 143 | cumulative_timings_.AddLogger(logger); |
| 144 | } |
| 145 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 146 | Jit::Jit() : dump_info_on_shutdown_(false), |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 147 | cumulative_timings_("JIT timings"), |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 148 | memory_use_("Memory used for compilation", 16), |
| 149 | lock_("JIT memory use lock"), |
Andreas Gampe | 4471e4f | 2017-01-30 16:40:49 +0000 | [diff] [blame] | 150 | use_jit_compilation_(true), |
| 151 | hot_method_threshold_(0), |
| 152 | warm_method_threshold_(0), |
| 153 | osr_method_threshold_(0), |
| 154 | priority_thread_weight_(0), |
| 155 | invoke_transition_weight_(0) {} |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 156 | |
| 157 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 158 | DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 159 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 160 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 161 | if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 162 | return nullptr; |
| 163 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 164 | jit->code_cache_.reset(JitCodeCache::Create( |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 165 | options->GetCodeCacheInitialCapacity(), |
| 166 | options->GetCodeCacheMaxCapacity(), |
| 167 | jit->generate_debug_info_, |
| 168 | error_msg)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 169 | if (jit->GetCodeCache() == nullptr) { |
| 170 | return nullptr; |
| 171 | } |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 172 | jit->use_jit_compilation_ = options->UseJitCompilation(); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 173 | jit->profile_saver_options_ = options->GetProfileSaverOptions(); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 174 | VLOG(jit) << "JIT created with initial_capacity=" |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 175 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 176 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 177 | << ", compile_threshold=" << options->GetCompileThreshold() |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 178 | << ", profile_saver_options=" << options->GetProfileSaverOptions(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 179 | |
| 180 | |
| 181 | jit->hot_method_threshold_ = options->GetCompileThreshold(); |
| 182 | jit->warm_method_threshold_ = options->GetWarmupThreshold(); |
| 183 | jit->osr_method_threshold_ = options->GetOsrThreshold(); |
Nicolas Geoffray | ba6aae0 | 2016-04-14 14:17:29 +0100 | [diff] [blame] | 184 | jit->priority_thread_weight_ = options->GetPriorityThreadWeight(); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 185 | jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 186 | |
| 187 | jit->CreateThreadPool(); |
| 188 | |
| 189 | // Notify native debugger about the classes already loaded before the creation of the jit. |
| 190 | jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 191 | return jit.release(); |
| 192 | } |
| 193 | |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 194 | bool Jit::LoadCompilerLibrary(std::string* error_msg) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 195 | jit_library_handle_ = dlopen( |
| 196 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 197 | if (jit_library_handle_ == nullptr) { |
| 198 | std::ostringstream oss; |
| 199 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 200 | *error_msg = oss.str(); |
| 201 | return false; |
| 202 | } |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 203 | jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 204 | if (jit_load_ == nullptr) { |
| 205 | dlclose(jit_library_handle_); |
| 206 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 207 | return false; |
| 208 | } |
| 209 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 210 | dlsym(jit_library_handle_, "jit_unload")); |
| 211 | if (jit_unload_ == nullptr) { |
| 212 | dlclose(jit_library_handle_); |
| 213 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 214 | return false; |
| 215 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 216 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 217 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 218 | if (jit_compile_method_ == nullptr) { |
| 219 | dlclose(jit_library_handle_); |
| 220 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 221 | return false; |
| 222 | } |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 223 | jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>( |
| 224 | dlsym(jit_library_handle_, "jit_types_loaded")); |
| 225 | if (jit_types_loaded_ == nullptr) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 226 | dlclose(jit_library_handle_); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 227 | *error_msg = "JIT couldn't find jit_types_loaded entry point"; |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 234 | if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) { |
| 235 | return false; |
| 236 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 237 | bool will_generate_debug_symbols = false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 238 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 239 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 240 | jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 241 | if (jit_compiler_handle_ == nullptr) { |
| 242 | dlclose(jit_library_handle_); |
| 243 | *error_msg = "JIT couldn't load compiler"; |
| 244 | return false; |
| 245 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 246 | generate_debug_info_ = will_generate_debug_symbols; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 250 | bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 251 | DCHECK(Runtime::Current()->UseJitCompilation()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 252 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 253 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 254 | // Don't compile the method if it has breakpoints. |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 255 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 256 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to breakpoint"; |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 257 | return false; |
| 258 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 259 | |
| 260 | // Don't compile the method if we are supposed to be deoptimized. |
| 261 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 262 | if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 263 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization"; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 267 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 268 | // of that proxy method, as the compiler does not expect a proxy method. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 269 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 270 | if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 271 | return false; |
| 272 | } |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 273 | |
| 274 | VLOG(jit) << "Compiling method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 275 | << ArtMethod::PrettyMethod(method_to_compile) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 276 | << " osr=" << std::boolalpha << osr; |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 277 | bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 278 | code_cache_->DoneCompiling(method_to_compile, self, osr); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 279 | if (!success) { |
| 280 | VLOG(jit) << "Failed to compile method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 281 | << ArtMethod::PrettyMethod(method_to_compile) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 282 | << " osr=" << std::boolalpha << osr; |
| 283 | } |
Andreas Gampe | 320ba91 | 2016-11-18 17:39:45 -0800 | [diff] [blame] | 284 | if (kIsDebugBuild) { |
| 285 | if (self->IsExceptionPending()) { |
| 286 | mirror::Throwable* exception = self->GetException(); |
| 287 | LOG(FATAL) << "No pending exception expected after compiling " |
| 288 | << ArtMethod::PrettyMethod(method) |
| 289 | << ": " |
| 290 | << exception->Dump(); |
| 291 | } |
| 292 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 293 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void Jit::CreateThreadPool() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 297 | // There is a DCHECK in the 'AddSamples' method to ensure the tread pool |
| 298 | // is not null when we instrument. |
Andreas Gampe | 4471e4f | 2017-01-30 16:40:49 +0000 | [diff] [blame] | 299 | |
| 300 | // We need peers as we may report the JIT thread, e.g., in the debugger. |
| 301 | constexpr bool kJitPoolNeedsPeers = true; |
| 302 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers)); |
| 303 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 304 | thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority); |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 305 | Start(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void Jit::DeleteThreadPool() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 309 | Thread* self = Thread::Current(); |
| 310 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 311 | if (thread_pool_ != nullptr) { |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame^] | 312 | std::unique_ptr<ThreadPool> pool; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 313 | { |
| 314 | ScopedSuspendAll ssa(__FUNCTION__); |
| 315 | // Clear thread_pool_ field while the threads are suspended. |
| 316 | // A mutator in the 'AddSamples' method will check against it. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame^] | 317 | pool = std::move(thread_pool_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 318 | } |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame^] | 319 | |
| 320 | // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue. |
| 321 | if (!RUNNING_ON_MEMORY_TOOL) { |
| 322 | pool->StopWorkers(self); |
| 323 | pool->RemoveAllTasks(self); |
| 324 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 325 | // We could just suspend all threads, but we know those threads |
| 326 | // will finish in a short period, so it's not worth adding a suspend logic |
| 327 | // here. Besides, this is only done for shutdown. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame^] | 328 | pool->Wait(self, false, false); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 332 | void Jit::StartProfileSaver(const std::string& filename, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 333 | const std::vector<std::string>& code_paths) { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 334 | if (profile_saver_options_.IsEnabled()) { |
| 335 | ProfileSaver::Start(profile_saver_options_, |
| 336 | filename, |
| 337 | code_cache_.get(), |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 338 | code_paths); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 339 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void Jit::StopProfileSaver() { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 343 | if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) { |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 344 | ProfileSaver::Stop(dump_info_on_shutdown_); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 348 | bool Jit::JitAtFirstUse() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 349 | return HotMethodThreshold() == 0; |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 352 | bool Jit::CanInvokeCompiledCode(ArtMethod* method) { |
| 353 | return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode()); |
| 354 | } |
| 355 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 356 | Jit::~Jit() { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 357 | DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted()); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 358 | if (dump_info_on_shutdown_) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 359 | DumpInfo(LOG_STREAM(INFO)); |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 360 | Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO)); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 361 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 362 | DeleteThreadPool(); |
| 363 | if (jit_compiler_handle_ != nullptr) { |
| 364 | jit_unload_(jit_compiler_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 365 | jit_compiler_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 366 | } |
| 367 | if (jit_library_handle_ != nullptr) { |
| 368 | dlclose(jit_library_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 369 | jit_library_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 373 | void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 374 | if (!Runtime::Current()->UseJitCompilation()) { |
| 375 | // No need to notify if we only use the JIT to save profiles. |
| 376 | return; |
| 377 | } |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 378 | jit::Jit* jit = Runtime::Current()->GetJit(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 379 | if (jit->generate_debug_info_) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 380 | DCHECK(jit->jit_types_loaded_ != nullptr); |
| 381 | jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { |
| 386 | struct CollectClasses : public ClassVisitor { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 387 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 388 | classes_.push_back(klass.Ptr()); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 389 | return true; |
| 390 | } |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 391 | std::vector<mirror::Class*> classes_; |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | if (generate_debug_info_) { |
| 395 | ScopedObjectAccess so(Thread::Current()); |
| 396 | |
| 397 | CollectClasses visitor; |
| 398 | linker->VisitClasses(&visitor); |
| 399 | jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size()); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 403 | extern "C" void art_quick_osr_stub(void** stack, |
| 404 | uint32_t stack_size_in_bytes, |
| 405 | const uint8_t* native_pc, |
| 406 | JValue* result, |
| 407 | const char* shorty, |
| 408 | Thread* self); |
| 409 | |
| 410 | bool Jit::MaybeDoOnStackReplacement(Thread* thread, |
| 411 | ArtMethod* method, |
| 412 | uint32_t dex_pc, |
| 413 | int32_t dex_pc_offset, |
| 414 | JValue* result) { |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 415 | if (!kEnableOnStackReplacement) { |
| 416 | return false; |
| 417 | } |
| 418 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 419 | Jit* jit = Runtime::Current()->GetJit(); |
| 420 | if (jit == nullptr) { |
| 421 | return false; |
| 422 | } |
| 423 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 424 | if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) { |
| 425 | // Don't attempt to do an OSR if we are close to the stack limit. Since |
| 426 | // the interpreter frames are still on stack, OSR has the potential |
| 427 | // to stack overflow even for a simple loop. |
| 428 | // b/27094810. |
| 429 | return false; |
| 430 | } |
| 431 | |
Nicolas Geoffray | d9bc433 | 2016-02-05 23:32:25 +0000 | [diff] [blame] | 432 | // Get the actual Java method if this method is from a proxy class. The compiler |
| 433 | // and the JIT code cache do not expect methods from proxy classes. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 434 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Nicolas Geoffray | d9bc433 | 2016-02-05 23:32:25 +0000 | [diff] [blame] | 435 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 436 | // Cheap check if the method has been compiled already. That's an indicator that we should |
| 437 | // osr into it. |
| 438 | if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 439 | return false; |
| 440 | } |
| 441 | |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 442 | // Fetch some data before looking up for an OSR method. We don't want thread |
| 443 | // suspension once we hold an OSR method, as the JIT code cache could delete the OSR |
| 444 | // method while we are being suspended. |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 445 | const size_t number_of_vregs = method->GetCodeItem()->registers_size_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 446 | const char* shorty = method->GetShorty(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 447 | std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : ""); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 448 | void** memory = nullptr; |
| 449 | size_t frame_size = 0; |
| 450 | ShadowFrame* shadow_frame = nullptr; |
| 451 | const uint8_t* native_pc = nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 452 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 453 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 454 | ScopedAssertNoThreadSuspension sts("Holding OSR method"); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 455 | const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method); |
| 456 | if (osr_method == nullptr) { |
| 457 | // No osr method yet, just return to the interpreter. |
| 458 | return false; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 459 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 460 | |
| 461 | CodeInfo code_info = osr_method->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 462 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 463 | |
| 464 | // Find stack map starting at the target dex_pc. |
| 465 | StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding); |
| 466 | if (!stack_map.IsValid()) { |
| 467 | // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the |
| 468 | // hope that the next branch has one. |
| 469 | return false; |
| 470 | } |
| 471 | |
Aart Bik | 29bdaee | 2016-05-18 15:44:07 -0700 | [diff] [blame] | 472 | // Before allowing the jump, make sure the debugger is not active to avoid jumping from |
| 473 | // interpreter to OSR while e.g. single stepping. Note that we could selectively disable |
| 474 | // OSR when single stepping, but that's currently hard to know at this point. |
| 475 | if (Dbg::IsDebuggerActive()) { |
| 476 | return false; |
| 477 | } |
| 478 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 479 | // We found a stack map, now fill the frame with dex register values from the interpreter's |
| 480 | // shadow frame. |
| 481 | DexRegisterMap vreg_map = |
| 482 | code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs); |
| 483 | |
| 484 | frame_size = osr_method->GetFrameSizeInBytes(); |
| 485 | |
| 486 | // Allocate memory to put shadow frame values. The osr stub will copy that memory to |
| 487 | // stack. |
| 488 | // Note that we could pass the shadow frame to the stub, and let it copy the values there, |
| 489 | // but that is engineering complexity not worth the effort for something like OSR. |
| 490 | memory = reinterpret_cast<void**>(malloc(frame_size)); |
| 491 | CHECK(memory != nullptr); |
| 492 | memset(memory, 0, frame_size); |
| 493 | |
| 494 | // Art ABI: ArtMethod is at the bottom of the stack. |
| 495 | memory[0] = method; |
| 496 | |
| 497 | shadow_frame = thread->PopShadowFrame(); |
| 498 | if (!vreg_map.IsValid()) { |
| 499 | // If we don't have a dex register map, then there are no live dex registers at |
| 500 | // this dex pc. |
| 501 | } else { |
| 502 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
| 503 | DexRegisterLocation::Kind location = |
| 504 | vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); |
| 505 | if (location == DexRegisterLocation::Kind::kNone) { |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 506 | // Dex register is dead or uninitialized. |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 507 | continue; |
| 508 | } |
| 509 | |
| 510 | if (location == DexRegisterLocation::Kind::kConstant) { |
| 511 | // We skip constants because the compiled code knows how to handle them. |
| 512 | continue; |
| 513 | } |
| 514 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 515 | DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 516 | |
| 517 | int32_t vreg_value = shadow_frame->GetVReg(vreg); |
| 518 | int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg, |
| 519 | number_of_vregs, |
| 520 | code_info, |
| 521 | encoding); |
| 522 | DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size)); |
| 523 | DCHECK_GT(slot_offset, 0); |
| 524 | (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value; |
| 525 | } |
| 526 | } |
| 527 | |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 528 | native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) + |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 529 | osr_method->GetEntryPoint(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 530 | VLOG(jit) << "Jumping to " |
| 531 | << method_name |
| 532 | << "@" |
| 533 | << std::hex << reinterpret_cast<uintptr_t>(native_pc); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 536 | { |
| 537 | ManagedStack fragment; |
| 538 | thread->PushManagedStackFragment(&fragment); |
| 539 | (*art_quick_osr_stub)(memory, |
| 540 | frame_size, |
| 541 | native_pc, |
| 542 | result, |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 543 | shorty, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 544 | thread); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 545 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 546 | if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) { |
| 547 | thread->DeoptimizeWithDeoptimizationException(result); |
| 548 | } |
| 549 | thread->PopManagedStackFragment(fragment); |
| 550 | } |
| 551 | free(memory); |
| 552 | thread->PushShadowFrame(shadow_frame); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 553 | VLOG(jit) << "Done running OSR code for " << method_name; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 557 | void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) { |
| 558 | if (bytes > 4 * MB) { |
| 559 | LOG(INFO) << "Compiler allocated " |
| 560 | << PrettySize(bytes) |
| 561 | << " to compile " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 562 | << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 563 | } |
| 564 | MutexLock mu(Thread::Current(), lock_); |
| 565 | memory_use_.AddValue(bytes); |
| 566 | } |
| 567 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 568 | class JitCompileTask FINAL : public Task { |
| 569 | public: |
| 570 | enum TaskKind { |
| 571 | kAllocateProfile, |
| 572 | kCompile, |
| 573 | kCompileOsr |
| 574 | }; |
| 575 | |
| 576 | JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) { |
| 577 | ScopedObjectAccess soa(Thread::Current()); |
| 578 | // Add a global ref to the class to prevent class unloading until compilation is done. |
| 579 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 580 | CHECK(klass_ != nullptr); |
| 581 | } |
| 582 | |
| 583 | ~JitCompileTask() { |
| 584 | ScopedObjectAccess soa(Thread::Current()); |
| 585 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 586 | } |
| 587 | |
| 588 | void Run(Thread* self) OVERRIDE { |
| 589 | ScopedObjectAccess soa(self); |
| 590 | if (kind_ == kCompile) { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 591 | Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 592 | } else if (kind_ == kCompileOsr) { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 593 | Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 594 | } else { |
| 595 | DCHECK(kind_ == kAllocateProfile); |
| 596 | if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 597 | VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 598 | } |
| 599 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 600 | ProfileSaver::NotifyJitActivity(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void Finalize() OVERRIDE { |
| 604 | delete this; |
| 605 | } |
| 606 | |
| 607 | private: |
| 608 | ArtMethod* const method_; |
| 609 | const TaskKind kind_; |
| 610 | jobject klass_; |
| 611 | |
| 612 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
| 613 | }; |
| 614 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 615 | void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 616 | if (thread_pool_ == nullptr) { |
| 617 | // Should only see this when shutting down. |
| 618 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 619 | return; |
| 620 | } |
| 621 | |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 622 | if (method->IsClassInitializer() || method->IsNative() || !method->IsCompilable()) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 623 | // We do not want to compile such methods. |
| 624 | return; |
| 625 | } |
| 626 | DCHECK(thread_pool_ != nullptr); |
| 627 | DCHECK_GT(warm_method_threshold_, 0); |
| 628 | DCHECK_GT(hot_method_threshold_, warm_method_threshold_); |
| 629 | DCHECK_GT(osr_method_threshold_, hot_method_threshold_); |
| 630 | DCHECK_GE(priority_thread_weight_, 1); |
| 631 | DCHECK_LE(priority_thread_weight_, hot_method_threshold_); |
| 632 | |
| 633 | int32_t starting_count = method->GetCounter(); |
| 634 | if (Jit::ShouldUsePriorityThreadWeight()) { |
| 635 | count *= priority_thread_weight_; |
| 636 | } |
| 637 | int32_t new_count = starting_count + count; // int32 here to avoid wrap-around; |
| 638 | if (starting_count < warm_method_threshold_) { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 639 | if ((new_count >= warm_method_threshold_) && |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 640 | (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 641 | bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false); |
| 642 | if (success) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 643 | VLOG(jit) << "Start profiling " << method->PrettyMethod(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | if (thread_pool_ == nullptr) { |
| 647 | // Calling ProfilingInfo::Create might put us in a suspended state, which could |
| 648 | // lead to the thread pool being deleted when we are shutting down. |
| 649 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | if (!success) { |
| 654 | // We failed allocating. Instead of doing the collection on the Java thread, we push |
| 655 | // an allocation to a compiler thread, that will do the collection. |
| 656 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile)); |
| 657 | } |
| 658 | } |
| 659 | // Avoid jumping more than one state at a time. |
| 660 | new_count = std::min(new_count, hot_method_threshold_ - 1); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 661 | } else if (use_jit_compilation_) { |
| 662 | if (starting_count < hot_method_threshold_) { |
| 663 | if ((new_count >= hot_method_threshold_) && |
| 664 | !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 665 | DCHECK(thread_pool_ != nullptr); |
| 666 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile)); |
| 667 | } |
| 668 | // Avoid jumping more than one state at a time. |
| 669 | new_count = std::min(new_count, osr_method_threshold_ - 1); |
| 670 | } else if (starting_count < osr_method_threshold_) { |
| 671 | if (!with_backedges) { |
| 672 | // If the samples don't contain any back edge, we don't increment the hotness. |
| 673 | return; |
| 674 | } |
| 675 | if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) { |
| 676 | DCHECK(thread_pool_ != nullptr); |
| 677 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr)); |
| 678 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | // Update hotness counter |
| 682 | method->SetCounter(new_count); |
| 683 | } |
| 684 | |
| 685 | void Jit::MethodEntered(Thread* thread, ArtMethod* method) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 686 | Runtime* runtime = Runtime::Current(); |
| 687 | if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 688 | // The compiler requires a ProfilingInfo object. |
| 689 | ProfilingInfo::Create(thread, method, /* retry_allocation */ true); |
| 690 | JitCompileTask compile_task(method, JitCompileTask::kCompile); |
| 691 | compile_task.Run(thread); |
| 692 | return; |
| 693 | } |
| 694 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 695 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 696 | // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it |
| 697 | // instead of interpreting the method. |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 698 | if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) { |
| 699 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 700 | method, profiling_info->GetSavedEntryPoint()); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 701 | } else { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 702 | AddSamples(thread, method, 1, /* with_backedges */false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 706 | void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object, |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 707 | ArtMethod* caller, |
| 708 | uint32_t dex_pc, |
| 709 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 710 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 711 | DCHECK(this_object != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 712 | ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 713 | if (info != nullptr) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 714 | info->AddInvokeInfo(dex_pc, this_object->GetClass()); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | void Jit::WaitForCompilationToFinish(Thread* self) { |
| 719 | if (thread_pool_ != nullptr) { |
| 720 | thread_pool_->Wait(self, false, false); |
| 721 | } |
| 722 | } |
| 723 | |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 724 | void Jit::Stop() { |
| 725 | Thread* self = Thread::Current(); |
| 726 | // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice. |
| 727 | WaitForCompilationToFinish(self); |
| 728 | GetThreadPool()->StopWorkers(self); |
| 729 | WaitForCompilationToFinish(self); |
| 730 | } |
| 731 | |
| 732 | void Jit::Start() { |
| 733 | GetThreadPool()->StartWorkers(Thread::Current()); |
| 734 | } |
| 735 | |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 736 | ScopedJitSuspend::ScopedJitSuspend() { |
| 737 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 738 | was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr); |
| 739 | if (was_on_) { |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 740 | jit->Stop(); |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | ScopedJitSuspend::~ScopedJitSuspend() { |
| 745 | if (was_on_) { |
| 746 | DCHECK(Runtime::Current()->GetJit() != nullptr); |
| 747 | DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr); |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 748 | Runtime::Current()->GetJit()->Start(); |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 752 | } // namespace jit |
| 753 | } // namespace art |