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