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