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