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