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