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. |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 25 | #include "base/memfd.h" |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 26 | #include "base/memory_tool.h" |
Andreas Gampe | dcc528d | 2017-12-07 13:37:10 -0800 | [diff] [blame] | 27 | #include "base/runtime_debug.h" |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 28 | #include "base/scoped_flock.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/utils.h" |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 30 | #include "class_root.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 31 | #include "debugger.h" |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 32 | #include "dex/type_lookup_table.h" |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 33 | #include "gc/space/image_space.h" |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 34 | #include "entrypoints/entrypoint_utils-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | #include "entrypoints/runtime_asm_entrypoints.h" |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 36 | #include "image-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | #include "interpreter/interpreter.h" |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 38 | #include "jit-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 39 | #include "jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 40 | #include "jni/java_vm_ext.h" |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 41 | #include "mirror/method_handle_impl.h" |
| 42 | #include "mirror/var_handle.h" |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 43 | #include "oat_file.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 44 | #include "oat_file_manager.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 45 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 46 | #include "profile/profile_boot_info.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 47 | #include "profile/profile_compilation_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 48 | #include "profile_saver.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 49 | #include "runtime.h" |
| 50 | #include "runtime_options.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 51 | #include "stack.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 52 | #include "stack_map.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 53 | #include "thread-inl.h" |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 54 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 55 | |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 56 | using android::base::unique_fd; |
| 57 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 58 | namespace art { |
| 59 | namespace jit { |
| 60 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 61 | static constexpr bool kEnableOnStackReplacement = true; |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 62 | |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 63 | // Maximum permitted threshold value. |
| 64 | static constexpr uint32_t kJitMaxThreshold = std::numeric_limits<uint16_t>::max(); |
| 65 | |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 66 | // Different compilation threshold constants. These can be overridden on the command line. |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 67 | |
| 68 | // Non-debug default |
| 69 | static constexpr uint32_t kJitDefaultCompileThreshold = 20 * kJitSamplesBatchSize; |
| 70 | // Fast-debug build. |
| 71 | static constexpr uint32_t kJitStressDefaultCompileThreshold = 2 * kJitSamplesBatchSize; |
| 72 | // Slow-debug build. |
| 73 | static constexpr uint32_t kJitSlowStressDefaultCompileThreshold = 2; |
| 74 | |
| 75 | // Different warm-up threshold constants. These default to the equivalent compile thresholds divided |
| 76 | // by 2, but can be overridden at the command-line. |
| 77 | static constexpr uint32_t kJitDefaultWarmUpThreshold = kJitDefaultCompileThreshold / 2; |
| 78 | static constexpr uint32_t kJitStressDefaultWarmUpThreshold = kJitStressDefaultCompileThreshold / 2; |
| 79 | static constexpr uint32_t kJitSlowStressDefaultWarmUpThreshold = |
| 80 | kJitSlowStressDefaultCompileThreshold / 2; |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 81 | |
David Srbecky | 2182147 | 2019-07-29 15:10:31 +0100 | [diff] [blame] | 82 | DEFINE_RUNTIME_DEBUG_FLAG(Jit, kSlowMode); |
| 83 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 84 | // JIT compiler |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 85 | void* Jit::jit_library_handle_ = nullptr; |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 86 | JitCompilerInterface* Jit::jit_compiler_ = nullptr; |
| 87 | JitCompilerInterface* (*Jit::jit_load_)(void) = nullptr; |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 88 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 89 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 90 | auto* jit_options = new JitOptions; |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 91 | jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation); |
Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 92 | jit_options->use_tiered_jit_compilation_ = |
| 93 | options.GetOrDefault(RuntimeArgumentMap::UseTieredJitCompilation); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 94 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 95 | jit_options->code_cache_initial_capacity_ = |
| 96 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 97 | jit_options->code_cache_max_capacity_ = |
| 98 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 99 | jit_options->dump_info_on_shutdown_ = |
| 100 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 101 | jit_options->profile_saver_options_ = |
| 102 | options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts); |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 103 | jit_options->thread_pool_pthread_priority_ = |
| 104 | options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 105 | |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 106 | // Set default compile threshold to aide with sanity checking defaults. |
| 107 | jit_options->compile_threshold_ = |
| 108 | kIsDebugBuild |
| 109 | ? (Jit::kSlowMode |
| 110 | ? kJitSlowStressDefaultCompileThreshold |
| 111 | : kJitStressDefaultCompileThreshold) |
| 112 | : kJitDefaultCompileThreshold; |
| 113 | |
| 114 | // When not running in slow-mode, thresholds are quantized to kJitSamplesbatchsize. |
| 115 | const uint32_t kJitThresholdStep = Jit::kSlowMode ? 1u : kJitSamplesBatchSize; |
| 116 | |
| 117 | // Set default warm-up threshold to aide with sanity checking defaults. |
| 118 | jit_options->warmup_threshold_ = |
| 119 | kIsDebugBuild ? (Jit::kSlowMode |
| 120 | ? kJitSlowStressDefaultWarmUpThreshold |
| 121 | : kJitStressDefaultWarmUpThreshold) |
| 122 | : kJitDefaultWarmUpThreshold; |
| 123 | |
| 124 | // Warmup threshold should be less than compile threshold (so long as compile threshold is not |
| 125 | // zero == JIT-on-first-use). |
| 126 | DCHECK_LT(jit_options->warmup_threshold_, jit_options->compile_threshold_); |
| 127 | DCHECK_EQ(RoundUp(jit_options->warmup_threshold_, kJitThresholdStep), |
| 128 | jit_options->warmup_threshold_); |
| 129 | |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 130 | if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) { |
| 131 | jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold); |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 132 | } |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 133 | jit_options->compile_threshold_ = RoundUp(jit_options->compile_threshold_, kJitThresholdStep); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 134 | |
| 135 | if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) { |
| 136 | jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 137 | } |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 138 | jit_options->warmup_threshold_ = RoundUp(jit_options->warmup_threshold_, kJitThresholdStep); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 139 | |
| 140 | if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) { |
| 141 | jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 142 | } else { |
| 143 | jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2; |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 144 | if (jit_options->osr_threshold_ > kJitMaxThreshold) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 145 | jit_options->osr_threshold_ = |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 146 | RoundDown(kJitMaxThreshold, kJitThresholdStep); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
Orion Hodson | 4fa78a0 | 2019-09-12 09:35:24 +0100 | [diff] [blame] | 149 | jit_options->osr_threshold_ = RoundUp(jit_options->osr_threshold_, kJitThresholdStep); |
| 150 | |
| 151 | // Enforce ordering constraints between thresholds if not jit-on-first-use (when the compile |
| 152 | // threshold is 0). |
| 153 | if (jit_options->compile_threshold_ != 0) { |
| 154 | // Clamp thresholds such that OSR > compile > warm-up (see Jit::MaybeCompileMethod). |
| 155 | jit_options->osr_threshold_ = std::clamp(jit_options->osr_threshold_, |
| 156 | 2u * kJitThresholdStep, |
| 157 | RoundDown(kJitMaxThreshold, kJitThresholdStep)); |
| 158 | jit_options->compile_threshold_ = std::clamp(jit_options->compile_threshold_, |
| 159 | kJitThresholdStep, |
| 160 | jit_options->osr_threshold_ - kJitThresholdStep); |
| 161 | jit_options->warmup_threshold_ = |
| 162 | std::clamp(jit_options->warmup_threshold_, |
| 163 | 0u, |
| 164 | jit_options->compile_threshold_ - kJitThresholdStep); |
| 165 | } |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 166 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 167 | if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) { |
| 168 | jit_options->priority_thread_weight_ = |
| 169 | *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight); |
| 170 | if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) { |
| 171 | LOG(FATAL) << "Priority thread weight is above the warmup threshold."; |
| 172 | } else if (jit_options->priority_thread_weight_ == 0) { |
| 173 | LOG(FATAL) << "Priority thread weight cannot be 0."; |
| 174 | } |
| 175 | } else { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 176 | jit_options->priority_thread_weight_ = std::max( |
| 177 | jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio, |
| 178 | static_cast<size_t>(1)); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 181 | if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 182 | jit_options->invoke_transition_weight_ = |
| 183 | *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 184 | if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) { |
| 185 | LOG(FATAL) << "Invoke transition weight is above the warmup threshold."; |
| 186 | } else if (jit_options->invoke_transition_weight_ == 0) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 187 | LOG(FATAL) << "Invoke transition weight cannot be 0."; |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 188 | } |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 189 | } else { |
| 190 | jit_options->invoke_transition_weight_ = std::max( |
| 191 | jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 192 | static_cast<size_t>(1)); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 195 | return jit_options; |
| 196 | } |
| 197 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 198 | void Jit::DumpInfo(std::ostream& os) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 199 | code_cache_->Dump(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 200 | cumulative_timings_.Dump(os); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 201 | MutexLock mu(Thread::Current(), lock_); |
| 202 | memory_use_.PrintMemoryUse(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 205 | void Jit::DumpForSigQuit(std::ostream& os) { |
| 206 | DumpInfo(os); |
| 207 | ProfileSaver::DumpInstanceInfo(os); |
| 208 | } |
| 209 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 210 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 211 | cumulative_timings_.AddLogger(logger); |
| 212 | } |
| 213 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 214 | Jit::Jit(JitCodeCache* code_cache, JitOptions* options) |
| 215 | : code_cache_(code_cache), |
| 216 | options_(options), |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 217 | boot_completed_lock_("Jit::boot_completed_lock_"), |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 218 | cumulative_timings_("JIT timings"), |
| 219 | memory_use_("Memory used for compilation", 16), |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 220 | lock_("JIT memory use lock"), |
| 221 | zygote_mapping_methods_(), |
| 222 | fd_methods_(-1), |
| 223 | fd_methods_size_(0) {} |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 224 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 225 | Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) { |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 226 | if (jit_load_ == nullptr) { |
| 227 | LOG(WARNING) << "Not creating JIT: library not loaded"; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 228 | return nullptr; |
| 229 | } |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 230 | jit_compiler_ = (jit_load_)(); |
| 231 | if (jit_compiler_ == nullptr) { |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 232 | LOG(WARNING) << "Not creating JIT: failed to allocate a compiler"; |
| 233 | return nullptr; |
| 234 | } |
| 235 | std::unique_ptr<Jit> jit(new Jit(code_cache, options)); |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 236 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 237 | // If the code collector is enabled, check if that still holds: |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 238 | // With 'perf', we want a 1-1 mapping between an address and a method. |
| 239 | // We aren't able to keep method pointers live during the instrumentation method entry trampoline |
| 240 | // so we will just disable jit-gc if we are doing that. |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 241 | if (code_cache->GetGarbageCollectCode()) { |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 242 | code_cache->SetGarbageCollectCode(!jit_compiler_->GenerateDebugInfo() && |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 243 | !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()); |
| 244 | } |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 245 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 246 | VLOG(jit) << "JIT created with initial_capacity=" |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 247 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 248 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 249 | << ", compile_threshold=" << options->GetCompileThreshold() |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 250 | << ", profile_saver_options=" << options->GetProfileSaverOptions(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 251 | |
Nicolas Geoffray | b0a9747 | 2019-12-05 15:17:46 +0000 | [diff] [blame] | 252 | // We want to know whether the compiler is compiling baseline, as this |
| 253 | // affects how we GC ProfilingInfos. |
| 254 | for (const std::string& option : Runtime::Current()->GetCompilerOptions()) { |
| 255 | if (option == "--baseline") { |
| 256 | options->SetUseBaselineCompiler(); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 261 | // Notify native debugger about the classes already loaded before the creation of the jit. |
| 262 | jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 263 | return jit.release(); |
| 264 | } |
| 265 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 266 | template <typename T> |
| 267 | bool Jit::LoadSymbol(T* address, const char* name, std::string* error_msg) { |
| 268 | *address = reinterpret_cast<T>(dlsym(jit_library_handle_, name)); |
| 269 | if (*address == nullptr) { |
| 270 | *error_msg = std::string("JIT couldn't find ") + name + std::string(" entry point"); |
| 271 | return false; |
| 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 276 | bool Jit::LoadCompilerLibrary(std::string* error_msg) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 277 | jit_library_handle_ = dlopen( |
| 278 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 279 | if (jit_library_handle_ == nullptr) { |
| 280 | std::ostringstream oss; |
| 281 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 282 | *error_msg = oss.str(); |
| 283 | return false; |
| 284 | } |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 285 | if (!LoadSymbol(&jit_load_, "jit_load", error_msg)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 286 | dlclose(jit_library_handle_); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 287 | return false; |
| 288 | } |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 292 | 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] | 293 | DCHECK(Runtime::Current()->UseJitCompilation()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 294 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 295 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 296 | RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 297 | // Don't compile the method if it has breakpoints. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 298 | if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) { |
| 299 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() |
| 300 | << " due to not being safe to jit according to runtime-callbacks. For example, there" |
| 301 | << " could be breakpoints in this method."; |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 304 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 305 | if (!method->IsCompilable()) { |
| 306 | DCHECK(method->GetDeclaringClass()->IsObsoleteObject() || |
| 307 | method->IsProxyMethod()) << method->PrettyMethod(); |
| 308 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to method being made " |
| 309 | << "obsolete while waiting for JIT task to run. This probably happened due to " |
| 310 | << "concurrent structural class redefinition."; |
| 311 | return false; |
| 312 | } |
| 313 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 314 | // Don't compile the method if we are supposed to be deoptimized. |
| 315 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 316 | if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 317 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization"; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 321 | JitMemoryRegion* region = GetCodeCache()->GetCurrentRegion(); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 322 | if (osr && GetCodeCache()->IsSharedRegion(*region)) { |
| 323 | VLOG(jit) << "JIT not osr compiling " |
| 324 | << method->PrettyMethod() |
| 325 | << " due to using shared region"; |
| 326 | return false; |
| 327 | } |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 328 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 329 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 330 | // 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] | 331 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 332 | if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr, prejit, baseline, region)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 333 | return false; |
| 334 | } |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 335 | |
| 336 | VLOG(jit) << "Compiling method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 337 | << ArtMethod::PrettyMethod(method_to_compile) |
Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 338 | << " osr=" << std::boolalpha << osr |
| 339 | << " baseline=" << std::boolalpha << baseline; |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 340 | bool success = jit_compiler_->CompileMethod(self, region, method_to_compile, baseline, osr); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 341 | code_cache_->DoneCompiling(method_to_compile, self, osr); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 342 | if (!success) { |
| 343 | VLOG(jit) << "Failed to compile method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 344 | << ArtMethod::PrettyMethod(method_to_compile) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 345 | << " osr=" << std::boolalpha << osr; |
| 346 | } |
Andreas Gampe | 320ba91 | 2016-11-18 17:39:45 -0800 | [diff] [blame] | 347 | if (kIsDebugBuild) { |
| 348 | if (self->IsExceptionPending()) { |
| 349 | mirror::Throwable* exception = self->GetException(); |
| 350 | LOG(FATAL) << "No pending exception expected after compiling " |
| 351 | << ArtMethod::PrettyMethod(method) |
| 352 | << ": " |
| 353 | << exception->Dump(); |
| 354 | } |
| 355 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 356 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 359 | void Jit::WaitForWorkersToBeCreated() { |
| 360 | if (thread_pool_ != nullptr) { |
| 361 | thread_pool_->WaitForWorkersToBeCreated(); |
| 362 | } |
| 363 | } |
| 364 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 365 | void Jit::DeleteThreadPool() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 366 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 367 | if (thread_pool_ != nullptr) { |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 368 | std::unique_ptr<ThreadPool> pool; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 369 | { |
| 370 | ScopedSuspendAll ssa(__FUNCTION__); |
| 371 | // Clear thread_pool_ field while the threads are suspended. |
| 372 | // A mutator in the 'AddSamples' method will check against it. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 373 | pool = std::move(thread_pool_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 374 | } |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 375 | |
| 376 | // 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] | 377 | if (!kRunningOnMemoryTool) { |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 378 | pool->StopWorkers(self); |
| 379 | pool->RemoveAllTasks(self); |
| 380 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 381 | // We could just suspend all threads, but we know those threads |
| 382 | // will finish in a short period, so it's not worth adding a suspend logic |
| 383 | // here. Besides, this is only done for shutdown. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 384 | pool->Wait(self, false, false); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 388 | void Jit::StartProfileSaver(const std::string& filename, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 389 | const std::vector<std::string>& code_paths) { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 390 | if (options_->GetSaveProfilingInfo()) { |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 391 | ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 392 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void Jit::StopProfileSaver() { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 396 | if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) { |
| 397 | ProfileSaver::Stop(options_->DumpJitInfoOnShutdown()); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 401 | bool Jit::JitAtFirstUse() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 402 | return HotMethodThreshold() == 0; |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 405 | bool Jit::CanInvokeCompiledCode(ArtMethod* method) { |
| 406 | return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode()); |
| 407 | } |
| 408 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 409 | Jit::~Jit() { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 410 | DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted()); |
| 411 | if (options_->DumpJitInfoOnShutdown()) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 412 | DumpInfo(LOG_STREAM(INFO)); |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 413 | Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO)); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 414 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 415 | DeleteThreadPool(); |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 416 | if (jit_compiler_ != nullptr) { |
| 417 | delete jit_compiler_; |
| 418 | jit_compiler_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 419 | } |
| 420 | if (jit_library_handle_ != nullptr) { |
| 421 | dlclose(jit_library_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 422 | jit_library_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 426 | void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 427 | if (!Runtime::Current()->UseJitCompilation()) { |
| 428 | // No need to notify if we only use the JIT to save profiles. |
| 429 | return; |
| 430 | } |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 431 | jit::Jit* jit = Runtime::Current()->GetJit(); |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 432 | if (jit->jit_compiler_->GenerateDebugInfo()) { |
| 433 | jit_compiler_->TypesLoaded(&type, 1); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { |
| 438 | struct CollectClasses : public ClassVisitor { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 439 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 440 | classes_.push_back(klass.Ptr()); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 441 | return true; |
| 442 | } |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 443 | std::vector<mirror::Class*> classes_; |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 446 | if (jit_compiler_->GenerateDebugInfo()) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 447 | ScopedObjectAccess so(Thread::Current()); |
| 448 | |
| 449 | CollectClasses visitor; |
| 450 | linker->VisitClasses(&visitor); |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 451 | jit_compiler_->TypesLoaded(visitor.classes_.data(), visitor.classes_.size()); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 455 | extern "C" void art_quick_osr_stub(void** stack, |
Evgenii Stepanov | 6d90fde | 2018-09-04 17:50:38 -0700 | [diff] [blame] | 456 | size_t stack_size_in_bytes, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 457 | const uint8_t* native_pc, |
| 458 | JValue* result, |
| 459 | const char* shorty, |
| 460 | Thread* self); |
| 461 | |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 462 | OsrData* Jit::PrepareForOsr(ArtMethod* method, uint32_t dex_pc, uint32_t* vregs) { |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 463 | if (!kEnableOnStackReplacement) { |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 464 | return nullptr; |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 467 | // Cheap check if the method has been compiled already. That's an indicator that we should |
| 468 | // osr into it. |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 469 | if (!GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 470 | return nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 473 | // Fetch some data before looking up for an OSR method. We don't want thread |
| 474 | // suspension once we hold an OSR method, as the JIT code cache could delete the OSR |
| 475 | // method while we are being suspended. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 476 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 477 | const size_t number_of_vregs = accessor.RegistersSize(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 478 | std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : ""); |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 479 | OsrData* osr_data = nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 480 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 481 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 482 | ScopedAssertNoThreadSuspension sts("Holding OSR method"); |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 483 | const OatQuickMethodHeader* osr_method = GetCodeCache()->LookupOsrMethodHeader(method); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 484 | if (osr_method == nullptr) { |
| 485 | // No osr method yet, just return to the interpreter. |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 486 | return nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 487 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 488 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 489 | CodeInfo code_info(osr_method); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 490 | |
| 491 | // Find stack map starting at the target dex_pc. |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 492 | StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 493 | if (!stack_map.IsValid()) { |
| 494 | // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the |
| 495 | // hope that the next branch has one. |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 496 | return nullptr; |
Aart Bik | 29bdaee | 2016-05-18 15:44:07 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 499 | // We found a stack map, now fill the frame with dex register values from the interpreter's |
| 500 | // shadow frame. |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 501 | DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map); |
Artem Serov | 2808be8 | 2018-12-20 19:15:11 +0000 | [diff] [blame] | 502 | DCHECK_EQ(vreg_map.size(), number_of_vregs); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 503 | |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 504 | size_t frame_size = osr_method->GetFrameSizeInBytes(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 505 | |
| 506 | // Allocate memory to put shadow frame values. The osr stub will copy that memory to |
| 507 | // stack. |
| 508 | // Note that we could pass the shadow frame to the stub, and let it copy the values there, |
| 509 | // but that is engineering complexity not worth the effort for something like OSR. |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 510 | osr_data = reinterpret_cast<OsrData*>(malloc(sizeof(OsrData) + frame_size)); |
| 511 | if (osr_data == nullptr) { |
| 512 | return nullptr; |
| 513 | } |
| 514 | memset(osr_data, 0, sizeof(OsrData) + frame_size); |
| 515 | osr_data->frame_size = frame_size; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 516 | |
| 517 | // Art ABI: ArtMethod is at the bottom of the stack. |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 518 | osr_data->memory[0] = method; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 519 | |
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 | |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 538 | int32_t vreg_value = vregs[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); |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 542 | (reinterpret_cast<int32_t*>(osr_data->memory))[slot_offset / sizeof(int32_t)] = vreg_value; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 546 | osr_data->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 | << "@" |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 551 | << std::hex << reinterpret_cast<uintptr_t>(osr_data->native_pc); |
| 552 | } |
| 553 | return osr_data; |
| 554 | } |
| 555 | |
| 556 | bool Jit::MaybeDoOnStackReplacement(Thread* thread, |
| 557 | ArtMethod* method, |
| 558 | uint32_t dex_pc, |
| 559 | int32_t dex_pc_offset, |
| 560 | JValue* result) { |
| 561 | Jit* jit = Runtime::Current()->GetJit(); |
| 562 | if (jit == nullptr) { |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) { |
| 567 | // Don't attempt to do an OSR if we are close to the stack limit. Since |
| 568 | // the interpreter frames are still on stack, OSR has the potential |
| 569 | // to stack overflow even for a simple loop. |
| 570 | // b/27094810. |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | // Get the actual Java method if this method is from a proxy class. The compiler |
| 575 | // and the JIT code cache do not expect methods from proxy classes. |
| 576 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 577 | |
| 578 | // Before allowing the jump, make sure no code is actively inspecting the method to avoid |
| 579 | // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively |
| 580 | // disable OSR when single stepping, but that's currently hard to know at this point. |
| 581 | if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) { |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | ShadowFrame* shadow_frame = thread->GetManagedStack()->GetTopShadowFrame(); |
| 586 | OsrData* osr_data = jit->PrepareForOsr(method, |
| 587 | dex_pc + dex_pc_offset, |
| 588 | shadow_frame->GetVRegArgs(0)); |
| 589 | |
| 590 | if (osr_data == nullptr) { |
| 591 | return false; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 594 | { |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 595 | thread->PopShadowFrame(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 596 | ManagedStack fragment; |
| 597 | thread->PushManagedStackFragment(&fragment); |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 598 | (*art_quick_osr_stub)(osr_data->memory, |
| 599 | osr_data->frame_size, |
| 600 | osr_data->native_pc, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 601 | result, |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 602 | method->GetShorty(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 603 | thread); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 604 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 605 | if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) { |
| 606 | thread->DeoptimizeWithDeoptimizationException(result); |
| 607 | } |
| 608 | thread->PopManagedStackFragment(fragment); |
| 609 | } |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 610 | free(osr_data); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 611 | thread->PushShadowFrame(shadow_frame); |
Nicolas Geoffray | 57cacb7 | 2019-12-08 22:07:08 +0000 | [diff] [blame] | 612 | VLOG(jit) << "Done running OSR code for " << method->PrettyMethod(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 613 | return true; |
| 614 | } |
| 615 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 616 | void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) { |
| 617 | if (bytes > 4 * MB) { |
| 618 | LOG(INFO) << "Compiler allocated " |
| 619 | << PrettySize(bytes) |
| 620 | << " to compile " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 621 | << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 622 | } |
| 623 | MutexLock mu(Thread::Current(), lock_); |
| 624 | memory_use_.AddValue(bytes); |
| 625 | } |
| 626 | |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 627 | void Jit::NotifyZygoteCompilationDone() { |
| 628 | if (fd_methods_ == -1) { |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | size_t offset = 0; |
| 633 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 634 | const ImageHeader& header = space->GetImageHeader(); |
| 635 | const ImageSection& section = header.GetMethodsSection(); |
| 636 | // Because mremap works at page boundaries, we can only handle methods |
| 637 | // within a page range. For methods that falls above or below the range, |
| 638 | // the child processes will copy their contents to their private mapping |
| 639 | // in `child_mapping_methods`. See `MapBootImageMethods`. |
| 640 | uint8_t* page_start = AlignUp(header.GetImageBegin() + section.Offset(), kPageSize); |
| 641 | uint8_t* page_end = |
| 642 | AlignDown(header.GetImageBegin() + section.Offset() + section.Size(), kPageSize); |
| 643 | if (page_end > page_start) { |
| 644 | uint64_t capacity = page_end - page_start; |
| 645 | memcpy(zygote_mapping_methods_.Begin() + offset, page_start, capacity); |
| 646 | offset += capacity; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // Do an msync to ensure we are not affected by writes still being in caches. |
| 651 | if (msync(zygote_mapping_methods_.Begin(), fd_methods_size_, MS_SYNC) != 0) { |
| 652 | PLOG(WARNING) << "Failed to sync boot image methods memory"; |
| 653 | code_cache_->GetZygoteMap()->SetCompilationState(ZygoteCompilationState::kNotifiedFailure); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | // We don't need the shared mapping anymore, and we need to drop it in case |
| 658 | // the file hasn't been sealed writable. |
| 659 | zygote_mapping_methods_ = MemMap::Invalid(); |
| 660 | |
Nicolas Geoffray | 831f20f | 2019-11-03 20:54:28 +0000 | [diff] [blame] | 661 | // Seal writes now. Zygote and children will map the memory private in order |
| 662 | // to write to it. |
| 663 | if (fcntl(fd_methods_, F_ADD_SEALS, F_SEAL_SEAL | F_SEAL_WRITE) == -1) { |
| 664 | PLOG(WARNING) << "Failed to seal boot image methods file descriptor"; |
| 665 | code_cache_->GetZygoteMap()->SetCompilationState(ZygoteCompilationState::kNotifiedFailure); |
| 666 | return; |
| 667 | } |
| 668 | |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 669 | std::string error_str; |
| 670 | MemMap child_mapping_methods = MemMap::MapFile( |
| 671 | fd_methods_size_, |
| 672 | PROT_READ | PROT_WRITE, |
| 673 | MAP_PRIVATE, |
| 674 | fd_methods_, |
| 675 | /* start= */ 0, |
| 676 | /* low_4gb= */ false, |
| 677 | "boot-image-methods", |
| 678 | &error_str); |
| 679 | |
| 680 | if (!child_mapping_methods.IsValid()) { |
| 681 | LOG(WARNING) << "Failed to create child mapping of boot image methods: " << error_str; |
| 682 | code_cache_->GetZygoteMap()->SetCompilationState(ZygoteCompilationState::kNotifiedFailure); |
| 683 | return; |
| 684 | } |
| 685 | |
Nicolas Geoffray | 831f20f | 2019-11-03 20:54:28 +0000 | [diff] [blame] | 686 | // Ensure the contents are the same as before: there was a window between |
| 687 | // the memcpy and the sealing where other processes could have changed the |
| 688 | // contents. |
| 689 | // Note this would not be needed if we could have used F_SEAL_FUTURE_WRITE, |
| 690 | // see b/143833776. |
| 691 | offset = 0; |
| 692 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 693 | const ImageHeader& header = space->GetImageHeader(); |
| 694 | const ImageSection& section = header.GetMethodsSection(); |
| 695 | // Because mremap works at page boundaries, we can only handle methods |
| 696 | // within a page range. For methods that falls above or below the range, |
| 697 | // the child processes will copy their contents to their private mapping |
| 698 | // in `child_mapping_methods`. See `MapBootImageMethods`. |
| 699 | uint8_t* page_start = AlignUp(header.GetImageBegin() + section.Offset(), kPageSize); |
| 700 | uint8_t* page_end = |
| 701 | AlignDown(header.GetImageBegin() + section.Offset() + section.Size(), kPageSize); |
| 702 | if (page_end > page_start) { |
| 703 | uint64_t capacity = page_end - page_start; |
| 704 | if (memcmp(child_mapping_methods.Begin() + offset, page_start, capacity) != 0) { |
| 705 | LOG(WARNING) << "Contents differ in boot image methods data"; |
| 706 | code_cache_->GetZygoteMap()->SetCompilationState( |
| 707 | ZygoteCompilationState::kNotifiedFailure); |
| 708 | return; |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 709 | } |
Nicolas Geoffray | 831f20f | 2019-11-03 20:54:28 +0000 | [diff] [blame] | 710 | offset += capacity; |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | // Future spawned processes don't need the fd anymore. |
| 715 | fd_methods_.reset(); |
| 716 | |
| 717 | // In order to have the zygote and children share the memory, we also remap |
| 718 | // the memory into the zygote process. |
| 719 | offset = 0; |
| 720 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 721 | const ImageHeader& header = space->GetImageHeader(); |
| 722 | const ImageSection& section = header.GetMethodsSection(); |
| 723 | // Because mremap works at page boundaries, we can only handle methods |
| 724 | // within a page range. For methods that falls above or below the range, |
| 725 | // the child processes will copy their contents to their private mapping |
| 726 | // in `child_mapping_methods`. See `MapBootImageMethods`. |
| 727 | uint8_t* page_start = AlignUp(header.GetImageBegin() + section.Offset(), kPageSize); |
| 728 | uint8_t* page_end = |
| 729 | AlignDown(header.GetImageBegin() + section.Offset() + section.Size(), kPageSize); |
| 730 | if (page_end > page_start) { |
| 731 | uint64_t capacity = page_end - page_start; |
| 732 | if (mremap(child_mapping_methods.Begin() + offset, |
| 733 | capacity, |
| 734 | capacity, |
| 735 | MREMAP_FIXED | MREMAP_MAYMOVE, |
| 736 | page_start) == MAP_FAILED) { |
| 737 | // Failing to remap is safe as the process will just use the old |
| 738 | // contents. |
| 739 | PLOG(WARNING) << "Failed mremap of boot image methods of " << space->GetImageFilename(); |
| 740 | } |
| 741 | offset += capacity; |
| 742 | } |
| 743 | } |
| 744 | |
Nicolas Geoffray | 8852e53 | 2019-10-30 09:43:35 +0000 | [diff] [blame] | 745 | LOG(INFO) << "Successfully notified child processes on sharing boot image methods"; |
| 746 | |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 747 | // Mark that compilation of boot classpath is done, and memory can now be |
| 748 | // shared. Other processes will pick up this information. |
| 749 | code_cache_->GetZygoteMap()->SetCompilationState(ZygoteCompilationState::kNotifiedOk); |
| 750 | |
| 751 | // The private mapping created for this process has been mremaped. We can |
| 752 | // reset it. |
| 753 | child_mapping_methods.Reset(); |
| 754 | } |
| 755 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 756 | class JitCompileTask final : public Task { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 757 | public: |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 758 | enum class TaskKind { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 759 | kAllocateProfile, |
| 760 | kCompile, |
Nicolas Geoffray | 075456e | 2018-12-14 08:54:21 +0000 | [diff] [blame] | 761 | kCompileBaseline, |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 762 | kCompileOsr, |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 763 | kPreCompile, |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 764 | }; |
| 765 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 766 | JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind), klass_(nullptr) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 767 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 768 | // For a non-bootclasspath class, add a global ref to the class to prevent class unloading |
| 769 | // until compilation is done. |
| 770 | if (method->GetDeclaringClass()->GetClassLoader() != nullptr) { |
| 771 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 772 | CHECK(klass_ != nullptr); |
| 773 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | ~JitCompileTask() { |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 777 | if (klass_ != nullptr) { |
| 778 | ScopedObjectAccess soa(Thread::Current()); |
| 779 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 780 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 783 | void Run(Thread* self) override { |
Andreas Gampe | 4bd5234 | 2019-07-15 15:09:04 -0700 | [diff] [blame] | 784 | { |
| 785 | ScopedObjectAccess soa(self); |
| 786 | switch (kind_) { |
| 787 | case TaskKind::kPreCompile: |
| 788 | case TaskKind::kCompile: |
| 789 | case TaskKind::kCompileBaseline: |
| 790 | case TaskKind::kCompileOsr: { |
| 791 | Runtime::Current()->GetJit()->CompileMethod( |
| 792 | method_, |
| 793 | self, |
| 794 | /* baseline= */ (kind_ == TaskKind::kCompileBaseline), |
| 795 | /* osr= */ (kind_ == TaskKind::kCompileOsr), |
| 796 | /* prejit= */ (kind_ == TaskKind::kPreCompile)); |
| 797 | break; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 798 | } |
Andreas Gampe | 4bd5234 | 2019-07-15 15:09:04 -0700 | [diff] [blame] | 799 | case TaskKind::kAllocateProfile: { |
| 800 | if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) { |
| 801 | VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_); |
| 802 | } |
| 803 | break; |
| 804 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 805 | } |
| 806 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 807 | ProfileSaver::NotifyJitActivity(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 810 | void Finalize() override { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 811 | delete this; |
| 812 | } |
| 813 | |
| 814 | private: |
| 815 | ArtMethod* const method_; |
| 816 | const TaskKind kind_; |
| 817 | jobject klass_; |
| 818 | |
| 819 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
| 820 | }; |
| 821 | |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 822 | static std::string GetProfileFile(const std::string& dex_location) { |
| 823 | // Hardcoded assumption where the profile file is. |
| 824 | // TODO(ngeoffray): this is brittle and we would need to change change if we |
| 825 | // wanted to do more eager JITting of methods in a profile. This is |
| 826 | // currently only for system server. |
| 827 | return dex_location + ".prof"; |
| 828 | } |
| 829 | |
| 830 | static std::string GetBootProfileFile(const std::string& profile) { |
| 831 | // The boot profile can be found next to the compilation profile, with a |
| 832 | // different extension. |
| 833 | return ReplaceFileExtension(profile, "bprof"); |
| 834 | } |
| 835 | |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 836 | /** |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 837 | * A JIT task to run after all profile compilation is done. |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 838 | */ |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 839 | class JitDoneCompilingProfileTask final : public SelfDeletingTask { |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 840 | public: |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 841 | explicit JitDoneCompilingProfileTask(const std::vector<const DexFile*>& dex_files) |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 842 | : dex_files_(dex_files) {} |
| 843 | |
| 844 | void Run(Thread* self ATTRIBUTE_UNUSED) override { |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 845 | // Madvise DONTNEED dex files now that we're done compiling methods. |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 846 | for (const DexFile* dex_file : dex_files_) { |
| 847 | if (IsAddressKnownBackedByFileOrShared(dex_file->Begin())) { |
| 848 | int result = madvise(const_cast<uint8_t*>(AlignDown(dex_file->Begin(), kPageSize)), |
| 849 | RoundUp(dex_file->Size(), kPageSize), |
| 850 | MADV_DONTNEED); |
| 851 | if (result == -1) { |
| 852 | PLOG(WARNING) << "Madvise failed"; |
| 853 | } |
| 854 | } |
| 855 | } |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 856 | |
| 857 | if (Runtime::Current()->IsZygote()) { |
Nicolas Geoffray | 8852e53 | 2019-10-30 09:43:35 +0000 | [diff] [blame] | 858 | // Record that we are done compiling the profile. |
| 859 | Runtime::Current()->GetJit()->GetCodeCache()->GetZygoteMap()->SetCompilationState( |
| 860 | ZygoteCompilationState::kDone); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 861 | } |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | private: |
| 865 | std::vector<const DexFile*> dex_files_; |
| 866 | |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 867 | DISALLOW_COPY_AND_ASSIGN(JitDoneCompilingProfileTask); |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 868 | }; |
| 869 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 870 | class ZygoteTask final : public Task { |
| 871 | public: |
| 872 | ZygoteTask() {} |
| 873 | |
| 874 | void Run(Thread* self) override { |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 875 | Runtime* runtime = Runtime::Current(); |
| 876 | std::string profile_file; |
| 877 | for (const std::string& option : runtime->GetImageCompilerOptions()) { |
| 878 | if (android::base::StartsWith(option, "--profile-file=")) { |
| 879 | profile_file = option.substr(strlen("--profile-file=")); |
| 880 | break; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | const std::vector<const DexFile*>& boot_class_path = |
| 885 | runtime->GetClassLinker()->GetBootClassPath(); |
| 886 | ScopedNullHandle<mirror::ClassLoader> null_handle; |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 887 | std::string boot_profile = GetBootProfileFile(profile_file); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 888 | // We add to the queue for zygote so that we can fork processes in-between |
| 889 | // compilations. |
Nicolas Geoffray | a6f3583 | 2019-08-09 13:34:19 +0100 | [diff] [blame] | 890 | uint32_t added_to_queue = 0; |
| 891 | if (Runtime::Current()->IsPrimaryZygote()) { |
| 892 | // We avoid doing compilation at boot for the secondary zygote, as apps |
| 893 | // forked from it are not critical for boot. |
| 894 | added_to_queue += runtime->GetJit()->CompileMethodsFromBootProfile( |
| 895 | self, boot_class_path, boot_profile, null_handle, /* add_to_queue= */ true); |
| 896 | } |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 897 | added_to_queue += runtime->GetJit()->CompileMethodsFromProfile( |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 898 | self, boot_class_path, profile_file, null_handle, /* add_to_queue= */ true); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 899 | |
| 900 | JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 901 | code_cache->GetZygoteMap()->Initialize(added_to_queue); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | void Finalize() override { |
| 905 | delete this; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | private: |
| 909 | DISALLOW_COPY_AND_ASSIGN(ZygoteTask); |
| 910 | }; |
| 911 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 912 | class JitProfileTask final : public Task { |
| 913 | public: |
| 914 | JitProfileTask(const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 915 | jobject class_loader) { |
| 916 | ScopedObjectAccess soa(Thread::Current()); |
| 917 | StackHandleScope<1> hs(soa.Self()); |
| 918 | Handle<mirror::ClassLoader> h_loader(hs.NewHandle( |
| 919 | soa.Decode<mirror::ClassLoader>(class_loader))); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 920 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 921 | for (const auto& dex_file : dex_files) { |
| 922 | dex_files_.push_back(dex_file.get()); |
| 923 | // Register the dex file so that we can guarantee it doesn't get deleted |
| 924 | // while reading it during the task. |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 925 | class_linker->RegisterDexFile(*dex_file.get(), h_loader.Get()); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 926 | } |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 927 | // We also create our own global ref to use this class loader later. |
| 928 | class_loader_ = soa.Vm()->AddGlobalRef(soa.Self(), h_loader.Get()); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | void Run(Thread* self) override { |
| 932 | ScopedObjectAccess soa(self); |
| 933 | StackHandleScope<1> hs(self); |
| 934 | Handle<mirror::ClassLoader> loader = hs.NewHandle<mirror::ClassLoader>( |
| 935 | soa.Decode<mirror::ClassLoader>(class_loader_)); |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 936 | |
| 937 | std::string profile = GetProfileFile(dex_files_[0]->GetLocation()); |
| 938 | std::string boot_profile = GetBootProfileFile(profile); |
| 939 | |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 940 | Jit* jit = Runtime::Current()->GetJit(); |
| 941 | |
| 942 | jit->CompileMethodsFromBootProfile( |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 943 | self, |
| 944 | dex_files_, |
| 945 | boot_profile, |
| 946 | loader, |
| 947 | /* add_to_queue= */ false); |
| 948 | |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 949 | jit->CompileMethodsFromProfile( |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 950 | self, |
| 951 | dex_files_, |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 952 | profile, |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 953 | loader, |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 954 | /* add_to_queue= */ true); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void Finalize() override { |
| 958 | delete this; |
| 959 | } |
| 960 | |
Nicolas Geoffray | f5a07ae | 2019-06-20 14:59:07 +0100 | [diff] [blame] | 961 | ~JitProfileTask() { |
| 962 | ScopedObjectAccess soa(Thread::Current()); |
| 963 | soa.Vm()->DeleteGlobalRef(soa.Self(), class_loader_); |
| 964 | } |
| 965 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 966 | private: |
| 967 | std::vector<const DexFile*> dex_files_; |
| 968 | jobject class_loader_; |
| 969 | |
| 970 | DISALLOW_COPY_AND_ASSIGN(JitProfileTask); |
| 971 | }; |
| 972 | |
Nicolas Geoffray | ff25806 | 2019-10-09 15:33:48 +0100 | [diff] [blame] | 973 | static void CopyIfDifferent(void* s1, const void* s2, size_t n) { |
| 974 | if (memcmp(s1, s2, n) != 0) { |
| 975 | memcpy(s1, s2, n); |
| 976 | } |
| 977 | } |
| 978 | |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 979 | void Jit::MapBootImageMethods() { |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 980 | CHECK_NE(fd_methods_.get(), -1); |
| 981 | if (!code_cache_->GetZygoteMap()->CanMapBootImageMethods()) { |
| 982 | LOG(WARNING) << "Not mapping boot image methods due to error from zygote"; |
| 983 | return; |
| 984 | } |
| 985 | |
| 986 | std::string error_str; |
| 987 | MemMap child_mapping_methods = MemMap::MapFile( |
| 988 | fd_methods_size_, |
| 989 | PROT_READ | PROT_WRITE, |
| 990 | MAP_PRIVATE, |
| 991 | fd_methods_, |
| 992 | /* start= */ 0, |
| 993 | /* low_4gb= */ false, |
| 994 | "boot-image-methods", |
| 995 | &error_str); |
| 996 | |
| 997 | // We don't need the fd anymore. |
| 998 | fd_methods_.reset(); |
| 999 | |
| 1000 | if (!child_mapping_methods.IsValid()) { |
| 1001 | LOG(WARNING) << "Failed to create child mapping of boot image methods: " << error_str; |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1002 | return; |
| 1003 | } |
| 1004 | size_t offset = 0; |
| 1005 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1006 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 1007 | const ImageHeader& header = space->GetImageHeader(); |
| 1008 | const ImageSection& section = header.GetMethodsSection(); |
| 1009 | uint8_t* page_start = AlignUp(header.GetImageBegin() + section.Offset(), kPageSize); |
| 1010 | uint8_t* page_end = |
| 1011 | AlignDown(header.GetImageBegin() + section.Offset() + section.Size(), kPageSize); |
| 1012 | if (page_end <= page_start) { |
| 1013 | // Section doesn't contain one aligned entire page. |
| 1014 | continue; |
| 1015 | } |
| 1016 | uint64_t capacity = page_end - page_start; |
| 1017 | // Walk over methods in the boot image, and check for ones whose class is |
| 1018 | // not initialized in the process, but are in the zygote process. For |
| 1019 | // such methods, we need their entrypoints to be stubs that do the |
| 1020 | // initialization check. |
| 1021 | header.VisitPackedArtMethods([&](ArtMethod& method) NO_THREAD_SAFETY_ANALYSIS { |
Nicolas Geoffray | 382df39 | 2019-10-31 11:57:15 +0000 | [diff] [blame] | 1022 | // Methods in the boot image should never have their single |
| 1023 | // implementation flag set (and therefore never have a `data_` pointing |
| 1024 | // to an ArtMethod for single implementation). |
| 1025 | CHECK(method.IsIntrinsic() || !method.HasSingleImplementationFlag()); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1026 | if (method.IsRuntimeMethod()) { |
| 1027 | return; |
| 1028 | } |
| 1029 | if (method.GetDeclaringClassUnchecked()->IsVisiblyInitialized() || |
| 1030 | !method.IsStatic() || |
| 1031 | method.IsConstructor()) { |
| 1032 | // Method does not need any stub. |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | // We are going to mremap the child mapping into the image: |
| 1037 | // |
| 1038 | // ImageSection ChildMappingMethods |
| 1039 | // |
| 1040 | // section start --> ----------- |
| 1041 | // | | |
| 1042 | // | | |
| 1043 | // page_start --> | | <----- ----------- |
| 1044 | // | | | | |
| 1045 | // | | | | |
| 1046 | // | | | | |
| 1047 | // | | | | |
| 1048 | // | | | | |
| 1049 | // | | | | |
| 1050 | // | | | | |
| 1051 | // page_end --> | | <----- ----------- |
| 1052 | // | | |
| 1053 | // section end --> ----------- |
| 1054 | |
| 1055 | |
| 1056 | uint8_t* pointer = reinterpret_cast<uint8_t*>(&method); |
Mathieu Chartier | 9bab237 | 2019-12-09 08:52:33 -0800 | [diff] [blame] | 1057 | // Note: We could refactor this to only check if the ArtMethod entrypoint is inside the |
| 1058 | // page region. This would remove the need for the edge case handling below. |
| 1059 | if (pointer >= page_start && pointer + sizeof(ArtMethod) < page_end) { |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1060 | // For all the methods in the mapping, put the entrypoint to the |
| 1061 | // resolution stub. |
| 1062 | ArtMethod* new_method = reinterpret_cast<ArtMethod*>( |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1063 | child_mapping_methods.Begin() + offset + (pointer - page_start)); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1064 | const void* code = new_method->GetEntryPointFromQuickCompiledCode(); |
| 1065 | if (!class_linker->IsQuickGenericJniStub(code) && |
| 1066 | !class_linker->IsQuickToInterpreterBridge(code) && |
| 1067 | !class_linker->IsQuickResolutionStub(code)) { |
| 1068 | LOG(INFO) << "Putting back the resolution stub to an ArtMethod"; |
| 1069 | new_method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
| 1070 | } |
| 1071 | } else if (pointer < page_start && (pointer + sizeof(ArtMethod)) > page_start) { |
| 1072 | LOG(INFO) << "Copying parts of the contents of an ArtMethod spanning page_start"; |
| 1073 | // If the method spans `page_start`, copy the contents of the child |
| 1074 | // into the pages we are going to remap into the image. |
| 1075 | // |
| 1076 | // section start --> ----------- |
| 1077 | // | | |
| 1078 | // | | |
| 1079 | // page_start --> |/////////| ----------- |
| 1080 | // |/////////| -> copy -> |/////////| |
| 1081 | // | | | | |
| 1082 | // |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1083 | CopyIfDifferent(child_mapping_methods.Begin() + offset, |
Nicolas Geoffray | ff25806 | 2019-10-09 15:33:48 +0100 | [diff] [blame] | 1084 | page_start, |
| 1085 | pointer + sizeof(ArtMethod) - page_start); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1086 | } else if (pointer < page_end && (pointer + sizeof(ArtMethod)) > page_end) { |
| 1087 | LOG(INFO) << "Copying parts of the contents of an ArtMethod spanning page_end"; |
| 1088 | // If the method spans `page_end`, copy the contents of the child |
| 1089 | // into the pages we are going to remap into the image. |
| 1090 | // |
| 1091 | // | | | | |
| 1092 | // |/////////| -> copy -> |/////////| |
| 1093 | // page_end --> |/////////| ----------- |
| 1094 | // | | |
| 1095 | // section end --> ----------- |
| 1096 | // |
| 1097 | size_t bytes_to_copy = (page_end - pointer); |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1098 | CopyIfDifferent(child_mapping_methods.Begin() + offset + capacity - bytes_to_copy, |
Nicolas Geoffray | ff25806 | 2019-10-09 15:33:48 +0100 | [diff] [blame] | 1099 | page_end - bytes_to_copy, |
| 1100 | bytes_to_copy); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1101 | } |
| 1102 | }, space->Begin(), kRuntimePointerSize); |
| 1103 | |
| 1104 | // Map the memory in the boot image range. |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1105 | if (mremap(child_mapping_methods.Begin() + offset, |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1106 | capacity, |
| 1107 | capacity, |
| 1108 | MREMAP_FIXED | MREMAP_MAYMOVE, |
| 1109 | page_start) == MAP_FAILED) { |
| 1110 | PLOG(WARNING) << "Fail to mremap boot image methods for " << space->GetImageFilename(); |
| 1111 | } |
| 1112 | offset += capacity; |
| 1113 | } |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1114 | |
| 1115 | // The private mapping created for this process has been mremaped. We can |
| 1116 | // reset it. |
| 1117 | child_mapping_methods.Reset(); |
Nicolas Geoffray | 8e23d38 | 2019-10-30 13:53:01 +0000 | [diff] [blame] | 1118 | LOG(INFO) << "Successfully mapped boot image methods"; |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1121 | void Jit::CreateThreadPool() { |
| 1122 | // There is a DCHECK in the 'AddSamples' method to ensure the tread pool |
| 1123 | // is not null when we instrument. |
| 1124 | |
| 1125 | // We need peers as we may report the JIT thread, e.g., in the debugger. |
| 1126 | constexpr bool kJitPoolNeedsPeers = true; |
| 1127 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers)); |
| 1128 | |
| 1129 | thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority()); |
| 1130 | Start(); |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1131 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1132 | Runtime* runtime = Runtime::Current(); |
David Srbecky | 3db3d37 | 2019-04-17 18:19:17 +0100 | [diff] [blame] | 1133 | if (runtime->IsZygote() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) { |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1134 | // If we're not using the default boot image location, request a JIT task to |
| 1135 | // compile all methods in the boot image profile. |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1136 | thread_pool_->AddTask(Thread::Current(), new ZygoteTask()); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1137 | |
| 1138 | // And create mappings to share boot image methods memory from the zygote to |
| 1139 | // child processes. |
| 1140 | |
| 1141 | // Compute the total capacity required for the boot image methods. |
| 1142 | uint64_t total_capacity = 0; |
| 1143 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 1144 | const ImageHeader& header = space->GetImageHeader(); |
| 1145 | const ImageSection& section = header.GetMethodsSection(); |
| 1146 | // Mappings need to be at the page level. |
| 1147 | uint8_t* page_start = AlignUp(header.GetImageBegin() + section.Offset(), kPageSize); |
| 1148 | uint8_t* page_end = |
| 1149 | AlignDown(header.GetImageBegin() + section.Offset() + section.Size(), kPageSize); |
| 1150 | if (page_end > page_start) { |
| 1151 | total_capacity += (page_end - page_start); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | // Create the child and zygote mappings to the boot image methods. |
| 1156 | if (total_capacity > 0) { |
| 1157 | // Start with '/boot' and end with '.art' to match the pattern recognized |
| 1158 | // by android_os_Debug.cpp for boot images. |
| 1159 | const char* name = "/boot-image-methods.art"; |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1160 | unique_fd mem_fd = unique_fd(art::memfd_create(name, /* flags= */ MFD_ALLOW_SEALING)); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1161 | if (mem_fd.get() == -1) { |
| 1162 | PLOG(WARNING) << "Could not create boot image methods file descriptor"; |
| 1163 | return; |
| 1164 | } |
| 1165 | if (ftruncate(mem_fd.get(), total_capacity) != 0) { |
| 1166 | PLOG(WARNING) << "Failed to truncate boot image methods file to " << total_capacity; |
| 1167 | return; |
| 1168 | } |
| 1169 | std::string error_str; |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1170 | |
| 1171 | // Create the shared mapping eagerly, as this prevents other processes |
| 1172 | // from adding the writable seal. |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1173 | zygote_mapping_methods_ = MemMap::MapFile( |
| 1174 | total_capacity, |
| 1175 | PROT_READ | PROT_WRITE, |
| 1176 | MAP_SHARED, |
| 1177 | mem_fd, |
| 1178 | /* start= */ 0, |
| 1179 | /* low_4gb= */ false, |
| 1180 | "boot-image-methods", |
| 1181 | &error_str); |
| 1182 | |
| 1183 | if (!zygote_mapping_methods_.IsValid()) { |
| 1184 | LOG(WARNING) << "Failed to create zygote mapping of boot image methods: " << error_str; |
| 1185 | return; |
| 1186 | } |
| 1187 | if (zygote_mapping_methods_.MadviseDontFork() != 0) { |
| 1188 | LOG(WARNING) << "Failed to madvise dont fork boot image methods"; |
| 1189 | zygote_mapping_methods_ = MemMap(); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
Nicolas Geoffray | 831f20f | 2019-11-03 20:54:28 +0000 | [diff] [blame] | 1193 | // We should use the F_SEAL_FUTURE_WRITE flag, but this has unexpected |
| 1194 | // behavior on private mappings after fork (the mapping becomes shared between |
| 1195 | // parent and children), see b/143833776. |
| 1196 | // We will seal the write once we are done writing to the shared mapping. |
| 1197 | if (fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW) == -1) { |
| 1198 | PLOG(WARNING) << "Failed to seal boot image methods file descriptor"; |
| 1199 | zygote_mapping_methods_ = MemMap(); |
| 1200 | return; |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1201 | } |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1202 | fd_methods_ = unique_fd(mem_fd.release()); |
| 1203 | fd_methods_size_ = total_capacity; |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1204 | } |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1208 | 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] | 1209 | jobject class_loader) { |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1210 | if (dex_files.empty()) { |
| 1211 | return; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1212 | } |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1213 | Runtime* runtime = Runtime::Current(); |
| 1214 | if (runtime->IsSystemServer() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) { |
| 1215 | thread_pool_->AddTask(Thread::Current(), new JitProfileTask(dex_files, class_loader)); |
| 1216 | } |
| 1217 | } |
| 1218 | |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1219 | bool Jit::CompileMethodFromProfile(Thread* self, |
| 1220 | ClassLinker* class_linker, |
| 1221 | uint32_t method_idx, |
| 1222 | Handle<mirror::DexCache> dex_cache, |
| 1223 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 1224 | bool add_to_queue, |
| 1225 | bool compile_after_boot) { |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1226 | ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType( |
| 1227 | method_idx, dex_cache, class_loader); |
| 1228 | if (method == nullptr) { |
| 1229 | self->ClearException(); |
| 1230 | return false; |
| 1231 | } |
| 1232 | if (!method->IsCompilable() || !method->IsInvokable()) { |
| 1233 | return false; |
| 1234 | } |
Nicolas Geoffray | a6f3583 | 2019-08-09 13:34:19 +0100 | [diff] [blame] | 1235 | if (method->IsPreCompiled()) { |
| 1236 | // Already seen by another profile. |
| 1237 | return false; |
| 1238 | } |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1239 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 1240 | if (class_linker->IsQuickToInterpreterBridge(entry_point) || |
| 1241 | class_linker->IsQuickGenericJniStub(entry_point) || |
| 1242 | class_linker->IsQuickResolutionStub(entry_point)) { |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1243 | method->SetPreCompiled(); |
Nicolas Geoffray | a6f3583 | 2019-08-09 13:34:19 +0100 | [diff] [blame] | 1244 | if (!add_to_queue) { |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1245 | CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ true); |
| 1246 | } else { |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 1247 | Task* task = new JitCompileTask(method, JitCompileTask::TaskKind::kPreCompile); |
| 1248 | if (compile_after_boot) { |
| 1249 | MutexLock mu(Thread::Current(), boot_completed_lock_); |
| 1250 | if (!boot_completed_) { |
| 1251 | tasks_after_boot_.push_back(task); |
| 1252 | return true; |
| 1253 | } |
| 1254 | DCHECK(tasks_after_boot_.empty()); |
| 1255 | } |
| 1256 | thread_pool_->AddTask(self, task); |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1257 | return true; |
| 1258 | } |
| 1259 | } |
| 1260 | return false; |
| 1261 | } |
| 1262 | |
| 1263 | uint32_t Jit::CompileMethodsFromBootProfile( |
| 1264 | Thread* self, |
| 1265 | const std::vector<const DexFile*>& dex_files, |
| 1266 | const std::string& profile_file, |
| 1267 | Handle<mirror::ClassLoader> class_loader, |
| 1268 | bool add_to_queue) { |
| 1269 | unix_file::FdFile profile(profile_file.c_str(), O_RDONLY, true); |
| 1270 | |
| 1271 | if (profile.Fd() == -1) { |
| 1272 | PLOG(WARNING) << "No boot profile: " << profile_file; |
| 1273 | return 0u; |
| 1274 | } |
| 1275 | |
| 1276 | ProfileBootInfo profile_info; |
| 1277 | if (!profile_info.Load(profile.Fd(), dex_files)) { |
| 1278 | LOG(ERROR) << "Could not load profile file: " << profile_file; |
| 1279 | return 0u; |
| 1280 | } |
| 1281 | |
| 1282 | ScopedObjectAccess soa(self); |
| 1283 | VariableSizedHandleScope handles(self); |
| 1284 | std::vector<Handle<mirror::DexCache>> dex_caches; |
| 1285 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1286 | for (const DexFile* dex_file : profile_info.GetDexFiles()) { |
| 1287 | dex_caches.push_back(handles.NewHandle(class_linker->FindDexCache(self, *dex_file))); |
| 1288 | } |
| 1289 | |
| 1290 | uint32_t added_to_queue = 0; |
| 1291 | for (const std::pair<uint32_t, uint32_t>& pair : profile_info.GetMethods()) { |
| 1292 | if (CompileMethodFromProfile(self, |
| 1293 | class_linker, |
| 1294 | pair.second, |
| 1295 | dex_caches[pair.first], |
| 1296 | class_loader, |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 1297 | add_to_queue, |
| 1298 | /*compile_after_boot=*/false)) { |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1299 | ++added_to_queue; |
| 1300 | } |
| 1301 | } |
| 1302 | return added_to_queue; |
| 1303 | } |
| 1304 | |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 1305 | uint32_t Jit::CompileMethodsFromProfile( |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1306 | Thread* self, |
| 1307 | const std::vector<const DexFile*>& dex_files, |
| 1308 | const std::string& profile_file, |
| 1309 | Handle<mirror::ClassLoader> class_loader, |
| 1310 | bool add_to_queue) { |
| 1311 | |
| 1312 | if (profile_file.empty()) { |
| 1313 | LOG(WARNING) << "Expected a profile file in JIT zygote mode"; |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 1314 | return 0u; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Nicolas Geoffray | a6f3583 | 2019-08-09 13:34:19 +0100 | [diff] [blame] | 1317 | // We don't generate boot profiles on device, therefore we don't |
| 1318 | // need to lock the file. |
| 1319 | unix_file::FdFile profile(profile_file.c_str(), O_RDONLY, true); |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1320 | |
Nicolas Geoffray | a6f3583 | 2019-08-09 13:34:19 +0100 | [diff] [blame] | 1321 | if (profile.Fd() == -1) { |
| 1322 | PLOG(WARNING) << "No profile: " << profile_file; |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 1323 | return 0u; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | ProfileCompilationInfo profile_info; |
Nicolas Geoffray | a6f3583 | 2019-08-09 13:34:19 +0100 | [diff] [blame] | 1327 | if (!profile_info.Load(profile.Fd())) { |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1328 | LOG(ERROR) << "Could not load profile file"; |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 1329 | return 0u; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1330 | } |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1331 | ScopedObjectAccess soa(self); |
| 1332 | StackHandleScope<1> hs(self); |
| 1333 | MutableHandle<mirror::DexCache> dex_cache = hs.NewHandle<mirror::DexCache>(nullptr); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1334 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 1335 | uint32_t added_to_queue = 0u; |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1336 | for (const DexFile* dex_file : dex_files) { |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 1337 | if (LocationIsOnArtModule(dex_file->GetLocation().c_str())) { |
| 1338 | // The ART module jars are already preopted. |
Nicolas Geoffray | f59bc11 | 2019-04-03 10:29:29 +0100 | [diff] [blame] | 1339 | continue; |
| 1340 | } |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 1341 | // To speed up class lookups, generate a type lookup table for |
| 1342 | // the dex file. |
Nicolas Geoffray | c5e3a52 | 2019-04-30 09:47:55 +0100 | [diff] [blame] | 1343 | if (dex_file->GetOatDexFile() == nullptr) { |
| 1344 | TypeLookupTable type_lookup_table = TypeLookupTable::Create(*dex_file); |
| 1345 | type_lookup_tables_.push_back( |
| 1346 | std::make_unique<art::OatDexFile>(std::move(type_lookup_table))); |
| 1347 | dex_file->SetOatDexFile(type_lookup_tables_.back().get()); |
| 1348 | } |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 1349 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1350 | std::set<dex::TypeIndex> class_types; |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 1351 | std::set<uint16_t> all_methods; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1352 | if (!profile_info.GetClassesAndMethods(*dex_file, |
| 1353 | &class_types, |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 1354 | &all_methods, |
| 1355 | &all_methods, |
| 1356 | &all_methods)) { |
Nicolas Geoffray | f59bc11 | 2019-04-03 10:29:29 +0100 | [diff] [blame] | 1357 | // This means the profile file did not reference the dex file, which is the case |
| 1358 | // 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] | 1359 | continue; |
| 1360 | } |
| 1361 | dex_cache.Assign(class_linker->FindDexCache(self, *dex_file)); |
| 1362 | 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] | 1363 | |
| 1364 | for (uint16_t method_idx : all_methods) { |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1365 | if (CompileMethodFromProfile(self, |
| 1366 | class_linker, |
| 1367 | method_idx, |
| 1368 | dex_cache, |
| 1369 | class_loader, |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 1370 | add_to_queue, |
| 1371 | /*compile_after_boot=*/true)) { |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 1372 | ++added_to_queue; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | } |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 1376 | |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1377 | // Add a task to run when all compilation is done. |
| 1378 | JitDoneCompilingProfileTask* task = new JitDoneCompilingProfileTask(dex_files); |
Nicolas Geoffray | ccb0b5f | 2019-08-15 18:10:50 +0100 | [diff] [blame] | 1379 | MutexLock mu(Thread::Current(), boot_completed_lock_); |
| 1380 | if (!boot_completed_) { |
| 1381 | tasks_after_boot_.push_back(task); |
| 1382 | } else { |
| 1383 | DCHECK(tasks_after_boot_.empty()); |
| 1384 | thread_pool_->AddTask(self, task); |
| 1385 | } |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 1386 | return added_to_queue; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 1389 | static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 1390 | if (method->IsClassInitializer() || !method->IsCompilable() || method->IsPreCompiled()) { |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 1391 | // We do not want to compile such methods. |
| 1392 | return true; |
| 1393 | } |
| 1394 | if (method->IsNative()) { |
| 1395 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1396 | if (klass == GetClassRoot<mirror::MethodHandle>() || |
| 1397 | klass == GetClassRoot<mirror::VarHandle>()) { |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 1398 | // MethodHandle and VarHandle invocation methods are required to throw an |
| 1399 | // UnsupportedOperationException if invoked reflectively. We achieve this by having native |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1400 | // 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] | 1401 | // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI |
| 1402 | // stubs. Since these stubs have different stack representations we can then crash in stack |
| 1403 | // walking (b/78151261). |
| 1404 | return true; |
| 1405 | } |
| 1406 | } |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1410 | bool Jit::MaybeCompileMethod(Thread* self, |
| 1411 | ArtMethod* method, |
| 1412 | uint32_t old_count, |
| 1413 | uint32_t new_count, |
| 1414 | bool with_backedges) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1415 | if (thread_pool_ == nullptr) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1416 | return false; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1417 | } |
David Srbecky | 1fc4d42 | 2019-07-23 16:55:19 +0100 | [diff] [blame] | 1418 | if (UNLIKELY(method->IsPreCompiled()) && !with_backedges /* don't check for OSR */) { |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 1419 | if (!NeedsClinitCheckBeforeCall(method) || |
| 1420 | method->GetDeclaringClass()->IsVisiblyInitialized()) { |
Nicolas Geoffray | 5a0b672 | 2019-09-24 15:09:40 +0100 | [diff] [blame] | 1421 | const void* entry_point = code_cache_->GetSavedEntryPointOfPreCompiledMethod(method); |
| 1422 | if (entry_point != nullptr) { |
| 1423 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(method, entry_point); |
| 1424 | return true; |
| 1425 | } |
David Srbecky | 1fc4d42 | 2019-07-23 16:55:19 +0100 | [diff] [blame] | 1426 | } |
| 1427 | } |
Nicolas Geoffray | 5a0b672 | 2019-09-24 15:09:40 +0100 | [diff] [blame] | 1428 | |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 1429 | if (IgnoreSamplesForMethod(method)) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1430 | return false; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1431 | } |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 1432 | if (HotMethodThreshold() == 0) { |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 1433 | // Tests might request JIT on first use (compiled synchronously in the interpreter). |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1434 | return false; |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 1435 | } |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 1436 | DCHECK_GT(WarmMethodThreshold(), 0); |
| 1437 | DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold()); |
| 1438 | DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold()); |
| 1439 | DCHECK_GE(PriorityThreadWeight(), 1); |
| 1440 | DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold()); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1441 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1442 | if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) { |
| 1443 | // Note: Native method have no "warm" state or profiling info. |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1444 | if (!method->IsNative() && |
| 1445 | (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) && |
Nicolas Geoffray | b0a9747 | 2019-12-05 15:17:46 +0000 | [diff] [blame] | 1446 | code_cache_->CanAllocateProfilingInfo() && |
| 1447 | !options_->UseTieredJitCompilation()) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1448 | bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false); |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 1449 | if (success) { |
| 1450 | VLOG(jit) << "Start profiling " << method->PrettyMethod(); |
| 1451 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1452 | |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 1453 | if (thread_pool_ == nullptr) { |
| 1454 | // Calling ProfilingInfo::Create might put us in a suspended state, which could |
| 1455 | // lead to the thread pool being deleted when we are shutting down. |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1456 | return false; |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 1457 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1458 | |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 1459 | if (!success) { |
| 1460 | // We failed allocating. Instead of doing the collection on the Java thread, we push |
| 1461 | // an allocation to a compiler thread, that will do the collection. |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1462 | thread_pool_->AddTask( |
| 1463 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kAllocateProfile)); |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 1464 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1465 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1466 | } |
| 1467 | if (UseJitCompilation()) { |
David Srbecky | c45b589 | 2019-04-24 10:32:04 +0100 | [diff] [blame] | 1468 | if (old_count == 0 && |
| 1469 | method->IsNative() && |
| 1470 | Runtime::Current()->IsUsingApexBootImageLocation()) { |
| 1471 | // 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] | 1472 | CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ false); |
David Srbecky | c45b589 | 2019-04-24 10:32:04 +0100 | [diff] [blame] | 1473 | return true; |
| 1474 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1475 | if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) { |
| 1476 | if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1477 | DCHECK(thread_pool_ != nullptr); |
Nicolas Geoffray | b0a9747 | 2019-12-05 15:17:46 +0000 | [diff] [blame] | 1478 | JitCompileTask::TaskKind kind = |
| 1479 | (options_->UseTieredJitCompilation() || options_->UseBaselineCompiler()) |
| 1480 | ? JitCompileTask::TaskKind::kCompileBaseline |
| 1481 | : JitCompileTask::TaskKind::kCompile; |
Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 1482 | thread_pool_->AddTask(self, new JitCompileTask(method, kind)); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1483 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1484 | } |
| 1485 | if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1486 | if (!with_backedges) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1487 | return false; |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1488 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1489 | DCHECK(!method->IsNative()); // No back edges reported for native methods. |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1490 | if (!code_cache_->IsOsrCompiled(method)) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1491 | DCHECK(thread_pool_ != nullptr); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1492 | thread_pool_->AddTask( |
| 1493 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr)); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1494 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1495 | } |
| 1496 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 1497 | return true; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 1500 | void Jit::EnqueueOptimizedCompilation(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame^] | 1501 | if (thread_pool_ == nullptr) { |
| 1502 | return; |
| 1503 | } |
Nicolas Geoffray | b0a9747 | 2019-12-05 15:17:46 +0000 | [diff] [blame] | 1504 | // We arrive here after a baseline compiled code has reached its baseline |
| 1505 | // hotness threshold. If tiered compilation is enabled, enqueue a compilation |
| 1506 | // task that will compile optimize the method. |
| 1507 | if (options_->UseTieredJitCompilation()) { |
| 1508 | thread_pool_->AddTask( |
| 1509 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompile)); |
| 1510 | } |
Nicolas Geoffray | a59af8a | 2019-11-27 17:42:32 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Alex Light | 59b950f | 2018-10-08 10:43:06 -0700 | [diff] [blame] | 1513 | class ScopedSetRuntimeThread { |
| 1514 | public: |
| 1515 | explicit ScopedSetRuntimeThread(Thread* self) |
| 1516 | : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) { |
| 1517 | self_->SetIsRuntimeThread(true); |
| 1518 | } |
| 1519 | |
| 1520 | ~ScopedSetRuntimeThread() { |
| 1521 | self_->SetIsRuntimeThread(was_runtime_thread_); |
| 1522 | } |
| 1523 | |
| 1524 | private: |
| 1525 | Thread* self_; |
| 1526 | bool was_runtime_thread_; |
| 1527 | }; |
| 1528 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1529 | void Jit::MethodEntered(Thread* thread, ArtMethod* method) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1530 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 5e33ccd | 2019-07-03 10:53:08 +0100 | [diff] [blame] | 1531 | if (UNLIKELY(runtime->UseJitCompilation() && JitAtFirstUse())) { |
Vladimir Marko | be0c7cf | 2018-03-19 13:40:56 +0000 | [diff] [blame] | 1532 | ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Vladimir Marko | be0c7cf | 2018-03-19 13:40:56 +0000 | [diff] [blame] | 1533 | if (np_method->IsCompilable()) { |
Nicolas Geoffray | 5e33ccd | 2019-07-03 10:53:08 +0100 | [diff] [blame] | 1534 | if (!np_method->IsNative() && GetCodeCache()->CanAllocateProfilingInfo()) { |
Nicolas Geoffray | 29bb803 | 2019-06-06 10:32:24 +0100 | [diff] [blame] | 1535 | // The compiler requires a ProfilingInfo object for non-native methods. |
| 1536 | ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true); |
| 1537 | } |
| 1538 | // TODO(ngeoffray): For JIT at first use, use kPreCompile. Currently we don't due to |
| 1539 | // conflicts with jitzygote optimizations. |
| 1540 | JitCompileTask compile_task(method, JitCompileTask::TaskKind::kCompile); |
Alex Light | 59b950f | 2018-10-08 10:43:06 -0700 | [diff] [blame] | 1541 | // Fake being in a runtime thread so that class-load behavior will be the same as normal jit. |
| 1542 | ScopedSetRuntimeThread ssrt(thread); |
Vladimir Marko | be0c7cf | 2018-03-19 13:40:56 +0000 | [diff] [blame] | 1543 | compile_task.Run(thread); |
| 1544 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1545 | return; |
| 1546 | } |
| 1547 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1548 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1549 | // 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] | 1550 | // instead of interpreting the method. We don't update it for instrumentation as the entrypoint |
| 1551 | // must remain the instrumentation entrypoint. |
| 1552 | if ((profiling_info != nullptr) && |
| 1553 | (profiling_info->GetSavedEntryPoint() != nullptr) && |
| 1554 | (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) { |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 1555 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 1556 | method, profiling_info->GetSavedEntryPoint()); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1557 | } else { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1558 | AddSamples(thread, method, 1, /* with_backedges= */false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1562 | void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object, |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1563 | ArtMethod* caller, |
| 1564 | uint32_t dex_pc, |
| 1565 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1566 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1567 | DCHECK(this_object != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1568 | ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1569 | if (info != nullptr) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1570 | info->AddInvokeInfo(dex_pc, this_object->GetClass()); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | void Jit::WaitForCompilationToFinish(Thread* self) { |
| 1575 | if (thread_pool_ != nullptr) { |
| 1576 | thread_pool_->Wait(self, false, false); |
| 1577 | } |
| 1578 | } |
| 1579 | |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1580 | void Jit::Stop() { |
| 1581 | Thread* self = Thread::Current(); |
| 1582 | // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice. |
| 1583 | WaitForCompilationToFinish(self); |
| 1584 | GetThreadPool()->StopWorkers(self); |
| 1585 | WaitForCompilationToFinish(self); |
| 1586 | } |
| 1587 | |
| 1588 | void Jit::Start() { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 1589 | GetThreadPool()->StartWorkers(Thread::Current()); |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 1592 | ScopedJitSuspend::ScopedJitSuspend() { |
| 1593 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 1594 | was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr); |
| 1595 | if (was_on_) { |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1596 | jit->Stop(); |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | ScopedJitSuspend::~ScopedJitSuspend() { |
| 1601 | if (was_on_) { |
| 1602 | DCHECK(Runtime::Current()->GetJit() != nullptr); |
| 1603 | DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr); |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1604 | Runtime::Current()->GetJit()->Start(); |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1608 | static void* RunPollingThread(void* arg) { |
| 1609 | Jit* jit = reinterpret_cast<Jit*>(arg); |
| 1610 | do { |
| 1611 | sleep(10); |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1612 | } while (!jit->GetCodeCache()->GetZygoteMap()->IsCompilationNotified()); |
Nicolas Geoffray | 8e23d38 | 2019-10-30 13:53:01 +0000 | [diff] [blame] | 1613 | |
| 1614 | // We will suspend other threads: we can only do that if we're attached to the |
| 1615 | // runtime. |
| 1616 | Runtime* runtime = Runtime::Current(); |
| 1617 | bool thread_attached = runtime->AttachCurrentThread( |
| 1618 | "BootImagePollingThread", |
| 1619 | /* as_daemon= */ true, |
| 1620 | /* thread_group= */ nullptr, |
| 1621 | /* create_peer= */ false); |
| 1622 | CHECK(thread_attached); |
| 1623 | |
| 1624 | { |
| 1625 | // Prevent other threads from running while we are remapping the boot image |
| 1626 | // ArtMethod's. Native threads might still be running, but they cannot |
| 1627 | // change the contents of ArtMethod's. |
| 1628 | ScopedSuspendAll ssa(__FUNCTION__); |
| 1629 | runtime->GetJit()->MapBootImageMethods(); |
| 1630 | } |
| 1631 | |
| 1632 | Runtime::Current()->DetachCurrentThread(); |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1633 | return nullptr; |
| 1634 | } |
| 1635 | |
Nicolas Geoffray | 0d54cfb | 2019-05-03 09:13:52 +0100 | [diff] [blame] | 1636 | void Jit::PostForkChildAction(bool is_system_server, bool is_zygote) { |
Nicolas Geoffray | e9455f6 | 2019-08-15 20:57:04 +0100 | [diff] [blame] | 1637 | // Clear the potential boot tasks inherited from the zygote. |
| 1638 | { |
| 1639 | MutexLock mu(Thread::Current(), boot_completed_lock_); |
| 1640 | tasks_after_boot_.clear(); |
| 1641 | } |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1642 | |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 1643 | if (Runtime::Current()->IsUsingApexBootImageLocation() && fd_methods_ != -1) { |
Nicolas Geoffray | 623d4f1 | 2019-09-30 13:45:51 +0100 | [diff] [blame] | 1644 | // Create a thread that will poll the status of zygote compilation, and map |
| 1645 | // the private mapping of boot image methods. |
| 1646 | zygote_mapping_methods_.ResetInForkedProcess(); |
| 1647 | pthread_t polling_thread; |
| 1648 | pthread_attr_t attr; |
| 1649 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); |
| 1650 | CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), |
| 1651 | "PTHREAD_CREATE_DETACHED"); |
| 1652 | CHECK_PTHREAD_CALL( |
| 1653 | pthread_create, |
| 1654 | (&polling_thread, &attr, RunPollingThread, reinterpret_cast<void*>(this)), |
| 1655 | "Methods maps thread"); |
| 1656 | } |
| 1657 | |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1658 | if (is_zygote || Runtime::Current()->IsSafeMode()) { |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1659 | // Delete the thread pool, we are not going to JIT. |
| 1660 | thread_pool_.reset(nullptr); |
| 1661 | return; |
| 1662 | } |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 1663 | // At this point, the compiler options have been adjusted to the particular configuration |
| 1664 | // of the forked child. Parse them again. |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame] | 1665 | jit_compiler_->ParseCompilerOptions(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 1666 | |
| 1667 | // 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] | 1668 | code_cache_->SetGarbageCollectCode(!jit_compiler_->GenerateDebugInfo() && |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 1669 | !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1670 | |
Nicolas Geoffray | a67daeb | 2019-08-12 10:41:25 +0100 | [diff] [blame] | 1671 | if (is_system_server && Runtime::Current()->IsUsingApexBootImageLocation()) { |
| 1672 | // Disable garbage collection: we don't want it to delete methods we're compiling |
| 1673 | // through boot and system server profiles. |
| 1674 | // TODO(ngeoffray): Fix this so we still collect deoptimized and unused code. |
| 1675 | code_cache_->SetGarbageCollectCode(false); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1676 | } |
Nicolas Geoffray | 371390f | 2019-09-27 09:57:38 +0100 | [diff] [blame] | 1677 | |
| 1678 | // We do this here instead of PostZygoteFork, as NativeDebugInfoPostFork only |
| 1679 | // applies to a child. |
| 1680 | NativeDebugInfoPostFork(); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | void Jit::PreZygoteFork() { |
| 1684 | if (thread_pool_ == nullptr) { |
| 1685 | return; |
| 1686 | } |
| 1687 | thread_pool_->DeleteThreads(); |
David Srbecky | 1550a66 | 2019-08-14 17:16:46 +0100 | [diff] [blame] | 1688 | |
| 1689 | NativeDebugInfoPreFork(); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | void Jit::PostZygoteFork() { |
| 1693 | if (thread_pool_ == nullptr) { |
| 1694 | return; |
| 1695 | } |
Nicolas Geoffray | 8852e53 | 2019-10-30 09:43:35 +0000 | [diff] [blame] | 1696 | if (Runtime::Current()->IsZygote() && |
| 1697 | code_cache_->GetZygoteMap()->IsCompilationDoneButNotNotified()) { |
| 1698 | // Copy the boot image methods data to the mappings we created to share |
| 1699 | // with the children. We do this here as we are the only thread running and |
| 1700 | // we don't risk other threads concurrently updating the ArtMethod's. |
| 1701 | CHECK_EQ(GetTaskCount(), 1); |
| 1702 | NotifyZygoteCompilationDone(); |
| 1703 | CHECK(code_cache_->GetZygoteMap()->IsCompilationNotified()); |
| 1704 | } |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1705 | thread_pool_->CreateThreads(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
David Srbecky | bfcea3d | 2019-08-05 15:44:00 +0100 | [diff] [blame] | 1708 | void Jit::BootCompleted() { |
| 1709 | Thread* self = Thread::Current(); |
| 1710 | std::deque<Task*> tasks; |
| 1711 | { |
| 1712 | MutexLock mu(self, boot_completed_lock_); |
| 1713 | tasks = std::move(tasks_after_boot_); |
| 1714 | boot_completed_ = true; |
| 1715 | } |
| 1716 | for (Task* task : tasks) { |
| 1717 | thread_pool_->AddTask(self, task); |
| 1718 | } |
| 1719 | } |
| 1720 | |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 1721 | bool Jit::CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const { |
| 1722 | return !is_for_shared_region || |
| 1723 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(method->GetDeclaringClass()); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | 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] | 1727 | return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | 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] | 1731 | return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 1734 | bool Jit::CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const { |
| 1735 | if (!is_for_shared_region) { |
| 1736 | return cls->IsInitialized(); |
| 1737 | } else { |
| 1738 | // Look up the class status in the oat file. |
| 1739 | const DexFile& dex_file = *cls->GetDexCache()->GetDexFile(); |
| 1740 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 1741 | // In case we run without an image there won't be a backing oat file. |
| 1742 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
| 1743 | return false; |
| 1744 | } |
| 1745 | uint16_t class_def_index = cls->GetDexClassDefIndex(); |
| 1746 | return oat_dex_file->GetOatClass(class_def_index).GetStatus() >= ClassStatus::kInitialized; |
| 1747 | } |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame^] | 1750 | void Jit::EnqueueCompilationFromNterp(ArtMethod* method, Thread* self) { |
| 1751 | if (thread_pool_ == nullptr) { |
| 1752 | return; |
| 1753 | } |
| 1754 | if (GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 1755 | // If we already have compiled code for it, nterp may be stuck in a loop. |
| 1756 | // Compile OSR. |
| 1757 | thread_pool_->AddTask( |
| 1758 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr)); |
| 1759 | return; |
| 1760 | } |
| 1761 | ProfilingInfo::Create(self, method, /* retry_allocation= */ false); |
| 1762 | thread_pool_->AddTask( |
| 1763 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileBaseline)); |
| 1764 | } |
| 1765 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1766 | } // namespace jit |
| 1767 | } // namespace art |