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