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