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