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