blob: 788c10fbd138e62ae10a77dcab4f6bba3edf6f57 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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 Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +000023#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080024#include "base/logging.h" // For VLOG.
Andreas Gampe0897e1c2017-05-16 08:36:56 -070025#include "base/memory_tool.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080026#include "base/runtime_debug.h"
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +000027#include "base/scoped_flock.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/utils.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010029#include "class_root.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070030#include "debugger.h"
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +010031#include "dex/type_lookup_table.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "entrypoints/runtime_asm_entrypoints.h"
33#include "interpreter/interpreter.h"
David Srbeckye3fc2d12018-11-30 13:41:14 +000034#include "jit-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035#include "jit_code_cache.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010036#include "jni/java_vm_ext.h"
Orion Hodson52f5a1f2018-05-02 11:05:44 +010037#include "mirror/method_handle_impl.h"
38#include "mirror/var_handle.h"
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +010039#include "oat_file.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010040#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000041#include "oat_quick_method_header.h"
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +010042#include "profile/profile_boot_info.h"
David Sehr82d046e2018-04-23 08:14:19 -070043#include "profile/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000044#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045#include "runtime.h"
46#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070047#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000048#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070049#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010050#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051
52namespace art {
53namespace jit {
54
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000055static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000056
Orion Hodson4fa78a02019-09-12 09:35:24 +010057// Maximum permitted threshold value.
58static constexpr uint32_t kJitMaxThreshold = std::numeric_limits<uint16_t>::max();
59
Andreas Gampe7897cec2017-07-19 16:28:59 -070060// Different compilation threshold constants. These can be overridden on the command line.
Orion Hodson4fa78a02019-09-12 09:35:24 +010061
62// Non-debug default
63static constexpr uint32_t kJitDefaultCompileThreshold = 20 * kJitSamplesBatchSize;
64// Fast-debug build.
65static constexpr uint32_t kJitStressDefaultCompileThreshold = 2 * kJitSamplesBatchSize;
66// Slow-debug build.
67static constexpr uint32_t kJitSlowStressDefaultCompileThreshold = 2;
68
69// Different warm-up threshold constants. These default to the equivalent compile thresholds divided
70// by 2, but can be overridden at the command-line.
71static constexpr uint32_t kJitDefaultWarmUpThreshold = kJitDefaultCompileThreshold / 2;
72static constexpr uint32_t kJitStressDefaultWarmUpThreshold = kJitStressDefaultCompileThreshold / 2;
73static constexpr uint32_t kJitSlowStressDefaultWarmUpThreshold =
74 kJitSlowStressDefaultCompileThreshold / 2;
Andreas Gampe7897cec2017-07-19 16:28:59 -070075
David Srbecky21821472019-07-29 15:10:31 +010076DEFINE_RUNTIME_DEBUG_FLAG(Jit, kSlowMode);
77
Mathieu Chartier72918ea2016-03-24 11:07:06 -070078// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080079void* Jit::jit_library_handle_ = nullptr;
David Srbecky46b53532019-08-06 13:39:05 +010080JitCompilerInterface* Jit::jit_compiler_ = nullptr;
81JitCompilerInterface* (*Jit::jit_load_)(void) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070082
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080083JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080084 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010085 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000086
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000087 jit_options->code_cache_initial_capacity_ =
88 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
89 jit_options->code_cache_max_capacity_ =
90 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070091 jit_options->dump_info_on_shutdown_ =
92 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010093 jit_options->profile_saver_options_ =
94 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010095 jit_options->thread_pool_pthread_priority_ =
96 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000097
Orion Hodson4fa78a02019-09-12 09:35:24 +010098 // Set default compile threshold to aide with sanity checking defaults.
99 jit_options->compile_threshold_ =
100 kIsDebugBuild
101 ? (Jit::kSlowMode
102 ? kJitSlowStressDefaultCompileThreshold
103 : kJitStressDefaultCompileThreshold)
104 : kJitDefaultCompileThreshold;
105
106 // When not running in slow-mode, thresholds are quantized to kJitSamplesbatchsize.
107 const uint32_t kJitThresholdStep = Jit::kSlowMode ? 1u : kJitSamplesBatchSize;
108
109 // Set default warm-up threshold to aide with sanity checking defaults.
110 jit_options->warmup_threshold_ =
111 kIsDebugBuild ? (Jit::kSlowMode
112 ? kJitSlowStressDefaultWarmUpThreshold
113 : kJitStressDefaultWarmUpThreshold)
114 : kJitDefaultWarmUpThreshold;
115
116 // Warmup threshold should be less than compile threshold (so long as compile threshold is not
117 // zero == JIT-on-first-use).
118 DCHECK_LT(jit_options->warmup_threshold_, jit_options->compile_threshold_);
119 DCHECK_EQ(RoundUp(jit_options->warmup_threshold_, kJitThresholdStep),
120 jit_options->warmup_threshold_);
121
Andreas Gampe7897cec2017-07-19 16:28:59 -0700122 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
123 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
Andreas Gampe7897cec2017-07-19 16:28:59 -0700124 }
Orion Hodson4fa78a02019-09-12 09:35:24 +0100125 jit_options->compile_threshold_ = RoundUp(jit_options->compile_threshold_, kJitThresholdStep);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000126
127 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
128 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000129 }
Orion Hodson4fa78a02019-09-12 09:35:24 +0100130 jit_options->warmup_threshold_ = RoundUp(jit_options->warmup_threshold_, kJitThresholdStep);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000131
132 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
133 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000134 } else {
135 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
Orion Hodson4fa78a02019-09-12 09:35:24 +0100136 if (jit_options->osr_threshold_ > kJitMaxThreshold) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000137 jit_options->osr_threshold_ =
Orion Hodson4fa78a02019-09-12 09:35:24 +0100138 RoundDown(kJitMaxThreshold, kJitThresholdStep);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000139 }
140 }
Orion Hodson4fa78a02019-09-12 09:35:24 +0100141 jit_options->osr_threshold_ = RoundUp(jit_options->osr_threshold_, kJitThresholdStep);
142
143 // Enforce ordering constraints between thresholds if not jit-on-first-use (when the compile
144 // threshold is 0).
145 if (jit_options->compile_threshold_ != 0) {
146 // Clamp thresholds such that OSR > compile > warm-up (see Jit::MaybeCompileMethod).
147 jit_options->osr_threshold_ = std::clamp(jit_options->osr_threshold_,
148 2u * kJitThresholdStep,
149 RoundDown(kJitMaxThreshold, kJitThresholdStep));
150 jit_options->compile_threshold_ = std::clamp(jit_options->compile_threshold_,
151 kJitThresholdStep,
152 jit_options->osr_threshold_ - kJitThresholdStep);
153 jit_options->warmup_threshold_ =
154 std::clamp(jit_options->warmup_threshold_,
155 0u,
156 jit_options->compile_threshold_ - kJitThresholdStep);
157 }
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000158
Calin Juravleb2771b42016-04-07 17:09:25 +0100159 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
160 jit_options->priority_thread_weight_ =
161 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
162 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
163 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
164 } else if (jit_options->priority_thread_weight_ == 0) {
165 LOG(FATAL) << "Priority thread weight cannot be 0.";
166 }
167 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100168 jit_options->priority_thread_weight_ = std::max(
169 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
170 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100171 }
172
Calin Juravle155ff3d2016-04-27 14:14:58 +0100173 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100174 jit_options->invoke_transition_weight_ =
175 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100176 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
177 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
178 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100179 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100180 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100181 } else {
182 jit_options->invoke_transition_weight_ = std::max(
183 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800184 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100185 }
186
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187 return jit_options;
188}
189
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700190void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000191 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700192 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000193 MutexLock mu(Thread::Current(), lock_);
194 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700195}
196
Calin Juravleb8e69992016-03-09 15:37:48 +0000197void Jit::DumpForSigQuit(std::ostream& os) {
198 DumpInfo(os);
199 ProfileSaver::DumpInstanceInfo(os);
200}
201
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700202void Jit::AddTimingLogger(const TimingLogger& logger) {
203 cumulative_timings_.AddLogger(logger);
204}
205
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100206Jit::Jit(JitCodeCache* code_cache, JitOptions* options)
207 : code_cache_(code_cache),
208 options_(options),
David Srbeckybfcea3d2019-08-05 15:44:00 +0100209 boot_completed_lock_("Jit::boot_completed_lock_"),
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100210 cumulative_timings_("JIT timings"),
211 memory_use_("Memory used for compilation", 16),
212 lock_("JIT memory use lock") {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800213
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100214Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) {
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000215 if (jit_load_ == nullptr) {
216 LOG(WARNING) << "Not creating JIT: library not loaded";
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 return nullptr;
218 }
David Srbecky46b53532019-08-06 13:39:05 +0100219 jit_compiler_ = (jit_load_)();
220 if (jit_compiler_ == nullptr) {
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000221 LOG(WARNING) << "Not creating JIT: failed to allocate a compiler";
222 return nullptr;
223 }
224 std::unique_ptr<Jit> jit(new Jit(code_cache, options));
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000225
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000226 // If the code collector is enabled, check if that still holds:
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000227 // With 'perf', we want a 1-1 mapping between an address and a method.
228 // We aren't able to keep method pointers live during the instrumentation method entry trampoline
229 // so we will just disable jit-gc if we are doing that.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000230 if (code_cache->GetGarbageCollectCode()) {
David Srbecky46b53532019-08-06 13:39:05 +0100231 code_cache->SetGarbageCollectCode(!jit_compiler_->GenerateDebugInfo() &&
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000232 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
233 }
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100234
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000235 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000236 << PrettySize(options->GetCodeCacheInitialCapacity())
237 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000238 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100239 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100240
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100241 // Notify native debugger about the classes already loaded before the creation of the jit.
242 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800243 return jit.release();
244}
245
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000246template <typename T>
247bool Jit::LoadSymbol(T* address, const char* name, std::string* error_msg) {
248 *address = reinterpret_cast<T>(dlsym(jit_library_handle_, name));
249 if (*address == nullptr) {
250 *error_msg = std::string("JIT couldn't find ") + name + std::string(" entry point");
251 return false;
252 }
253 return true;
254}
255
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000256bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257 jit_library_handle_ = dlopen(
258 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
259 if (jit_library_handle_ == nullptr) {
260 std::ostringstream oss;
261 oss << "JIT could not load libart-compiler.so: " << dlerror();
262 *error_msg = oss.str();
263 return false;
264 }
David Srbecky46b53532019-08-06 13:39:05 +0100265 if (!LoadSymbol(&jit_load_, "jit_load", error_msg)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800266 dlclose(jit_library_handle_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000267 return false;
268 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700269 return true;
270}
271
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100272bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool baseline, bool osr, bool prejit) {
Calin Juravleffc87072016-04-20 14:22:09 +0100273 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800274 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000275
Alex Light0fa17862017-10-24 13:43:05 -0700276 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100277 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700278 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
279 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
280 << " due to not being safe to jit according to runtime-callbacks. For example, there"
281 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700282 return false;
283 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100284
285 // Don't compile the method if we are supposed to be deoptimized.
286 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
287 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700288 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100289 return false;
290 }
291
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000292 JitMemoryRegion* region = GetCodeCache()->GetCurrentRegion();
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100293 if (osr && GetCodeCache()->IsSharedRegion(*region)) {
294 VLOG(jit) << "JIT not osr compiling "
295 << method->PrettyMethod()
296 << " due to using shared region";
297 return false;
298 }
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000299
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000300 // If we get a request to compile a proxy method, we pass the actual Java method
301 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700302 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000303 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr, prejit, region)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100304 return false;
305 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100306
307 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700308 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100309 << " osr=" << std::boolalpha << osr;
David Srbecky46b53532019-08-06 13:39:05 +0100310 bool success = jit_compiler_->CompileMethod(self, region, method_to_compile, baseline, osr);
buzbee454b3b62016-04-07 14:42:47 -0700311 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100312 if (!success) {
313 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700314 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100315 << " osr=" << std::boolalpha << osr;
316 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800317 if (kIsDebugBuild) {
318 if (self->IsExceptionPending()) {
319 mirror::Throwable* exception = self->GetException();
320 LOG(FATAL) << "No pending exception expected after compiling "
321 << ArtMethod::PrettyMethod(method)
322 << ": "
323 << exception->Dump();
324 }
325 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100326 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800327}
328
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800329void Jit::WaitForWorkersToBeCreated() {
330 if (thread_pool_ != nullptr) {
331 thread_pool_->WaitForWorkersToBeCreated();
332 }
333}
334
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800335void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100336 Thread* self = Thread::Current();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100337 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700338 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100339 {
340 ScopedSuspendAll ssa(__FUNCTION__);
341 // Clear thread_pool_ field while the threads are suspended.
342 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700343 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100344 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700345
346 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000347 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700348 pool->StopWorkers(self);
349 pool->RemoveAllTasks(self);
350 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100351 // We could just suspend all threads, but we know those threads
352 // will finish in a short period, so it's not worth adding a suspend logic
353 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700354 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800355 }
356}
357
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000358void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800359 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100360 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100361 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100362 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000363}
364
365void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100366 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
367 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100368 }
369}
370
Siva Chandra05d24152016-01-05 17:43:17 -0800371bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100372 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800373}
374
Nicolas Geoffray35122442016-03-02 12:05:30 +0000375bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
376 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
377}
378
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800379Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100380 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
381 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700382 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100383 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700384 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800385 DeleteThreadPool();
David Srbecky46b53532019-08-06 13:39:05 +0100386 if (jit_compiler_ != nullptr) {
387 delete jit_compiler_;
388 jit_compiler_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800389 }
390 if (jit_library_handle_ != nullptr) {
391 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700392 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800393 }
394}
395
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000396void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100397 if (!Runtime::Current()->UseJitCompilation()) {
398 // No need to notify if we only use the JIT to save profiles.
399 return;
400 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000401 jit::Jit* jit = Runtime::Current()->GetJit();
David Srbecky46b53532019-08-06 13:39:05 +0100402 if (jit->jit_compiler_->GenerateDebugInfo()) {
403 jit_compiler_->TypesLoaded(&type, 1);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000404 }
405}
406
407void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
408 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100409 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700410 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000411 return true;
412 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800413 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000414 };
415
David Srbecky46b53532019-08-06 13:39:05 +0100416 if (jit_compiler_->GenerateDebugInfo()) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000417 ScopedObjectAccess so(Thread::Current());
418
419 CollectClasses visitor;
420 linker->VisitClasses(&visitor);
David Srbecky46b53532019-08-06 13:39:05 +0100421 jit_compiler_->TypesLoaded(visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000422 }
423}
424
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000425extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700426 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000427 const uint8_t* native_pc,
428 JValue* result,
429 const char* shorty,
430 Thread* self);
431
432bool Jit::MaybeDoOnStackReplacement(Thread* thread,
433 ArtMethod* method,
434 uint32_t dex_pc,
435 int32_t dex_pc_offset,
436 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000437 if (!kEnableOnStackReplacement) {
438 return false;
439 }
440
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000441 Jit* jit = Runtime::Current()->GetJit();
442 if (jit == nullptr) {
443 return false;
444 }
445
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000446 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
447 // Don't attempt to do an OSR if we are close to the stack limit. Since
448 // the interpreter frames are still on stack, OSR has the potential
449 // to stack overflow even for a simple loop.
450 // b/27094810.
451 return false;
452 }
453
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000454 // Get the actual Java method if this method is from a proxy class. The compiler
455 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700456 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000457
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000458 // Cheap check if the method has been compiled already. That's an indicator that we should
459 // osr into it.
460 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
461 return false;
462 }
463
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000464 // Fetch some data before looking up for an OSR method. We don't want thread
465 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
466 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000467 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800468 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000469 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700470 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000471 void** memory = nullptr;
472 size_t frame_size = 0;
473 ShadowFrame* shadow_frame = nullptr;
474 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000475
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000476 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700477 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000478 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
479 if (osr_method == nullptr) {
480 // No osr method yet, just return to the interpreter.
481 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000482 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000483
David Srbecky052f8ca2018-04-26 15:42:54 +0100484 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000485
486 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100487 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000488 if (!stack_map.IsValid()) {
489 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
490 // hope that the next branch has one.
491 return false;
492 }
493
Alex Light21611932017-09-26 13:07:39 -0700494 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
495 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
496 // disable OSR when single stepping, but that's currently hard to know at this point.
497 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700498 return false;
499 }
500
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000501 // We found a stack map, now fill the frame with dex register values from the interpreter's
502 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100503 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Artem Serov2808be82018-12-20 19:15:11 +0000504 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000505
506 frame_size = osr_method->GetFrameSizeInBytes();
507
508 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
509 // stack.
510 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
511 // but that is engineering complexity not worth the effort for something like OSR.
512 memory = reinterpret_cast<void**>(malloc(frame_size));
513 CHECK(memory != nullptr);
514 memset(memory, 0, frame_size);
515
516 // Art ABI: ArtMethod is at the bottom of the stack.
517 memory[0] = method;
518
519 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100520 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000521 // 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 Srbeckye1402122018-06-13 18:20:45 +0100525 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000526 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000527 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000528 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 Srbecky7dc11782016-02-25 13:23:56 +0000536 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000537
538 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100539 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000540 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
541 DCHECK_GT(slot_offset, 0);
542 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
543 }
544 }
545
David Srbecky052f8ca2018-04-26 15:42:54 +0100546 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000547 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000548 VLOG(jit) << "Jumping to "
549 << method_name
550 << "@"
551 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000552 }
553
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000554 {
555 ManagedStack fragment;
556 thread->PushManagedStackFragment(&fragment);
557 (*art_quick_osr_stub)(memory,
558 frame_size,
559 native_pc,
560 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000561 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000562 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000563
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000564 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
565 thread->DeoptimizeWithDeoptimizationException(result);
566 }
567 thread->PopManagedStackFragment(fragment);
568 }
569 free(memory);
570 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000571 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000572 return true;
573}
574
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000575void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
576 if (bytes > 4 * MB) {
577 LOG(INFO) << "Compiler allocated "
578 << PrettySize(bytes)
579 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700580 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000581 }
582 MutexLock mu(Thread::Current(), lock_);
583 memory_use_.AddValue(bytes);
584}
585
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100586class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100587 public:
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000588 enum class TaskKind {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100589 kAllocateProfile,
590 kCompile,
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000591 kCompileBaseline,
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000592 kCompileOsr,
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100593 kPreCompile,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100594 };
595
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000596 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind), klass_(nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100597 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000598 // For a non-bootclasspath class, add a global ref to the class to prevent class unloading
599 // until compilation is done.
600 if (method->GetDeclaringClass()->GetClassLoader() != nullptr) {
601 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
602 CHECK(klass_ != nullptr);
603 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100604 }
605
606 ~JitCompileTask() {
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000607 if (klass_ != nullptr) {
608 ScopedObjectAccess soa(Thread::Current());
609 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
610 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100611 }
612
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100613 void Run(Thread* self) override {
Andreas Gampe4bd52342019-07-15 15:09:04 -0700614 {
615 ScopedObjectAccess soa(self);
616 switch (kind_) {
617 case TaskKind::kPreCompile:
618 case TaskKind::kCompile:
619 case TaskKind::kCompileBaseline:
620 case TaskKind::kCompileOsr: {
621 Runtime::Current()->GetJit()->CompileMethod(
622 method_,
623 self,
624 /* baseline= */ (kind_ == TaskKind::kCompileBaseline),
625 /* osr= */ (kind_ == TaskKind::kCompileOsr),
626 /* prejit= */ (kind_ == TaskKind::kPreCompile));
627 break;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000628 }
Andreas Gampe4bd52342019-07-15 15:09:04 -0700629 case TaskKind::kAllocateProfile: {
630 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
631 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
632 }
633 break;
634 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100635 }
636 }
Calin Juravlea2638922016-04-29 16:44:11 +0100637 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100638 }
639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100640 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100641 delete this;
642 }
643
644 private:
645 ArtMethod* const method_;
646 const TaskKind kind_;
647 jobject klass_;
648
649 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
650};
651
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100652static std::string GetProfileFile(const std::string& dex_location) {
653 // Hardcoded assumption where the profile file is.
654 // TODO(ngeoffray): this is brittle and we would need to change change if we
655 // wanted to do more eager JITting of methods in a profile. This is
656 // currently only for system server.
657 return dex_location + ".prof";
658}
659
660static std::string GetBootProfileFile(const std::string& profile) {
661 // The boot profile can be found next to the compilation profile, with a
662 // different extension.
663 return ReplaceFileExtension(profile, "bprof");
664}
665
Nicolas Geoffrayccb0b5f2019-08-15 18:10:50 +0100666/**
667 * A JIT task to madvise DONTNEED dex files after we're done compiling methods.
668 */
669class JitMadviseDontNeedTask final : public SelfDeletingTask {
670 public:
671 explicit JitMadviseDontNeedTask(const std::vector<const DexFile*>& dex_files)
672 : dex_files_(dex_files) {}
673
674 void Run(Thread* self ATTRIBUTE_UNUSED) override {
675 for (const DexFile* dex_file : dex_files_) {
676 if (IsAddressKnownBackedByFileOrShared(dex_file->Begin())) {
677 int result = madvise(const_cast<uint8_t*>(AlignDown(dex_file->Begin(), kPageSize)),
678 RoundUp(dex_file->Size(), kPageSize),
679 MADV_DONTNEED);
680 if (result == -1) {
681 PLOG(WARNING) << "Madvise failed";
682 }
683 }
684 }
685 }
686
687 private:
688 std::vector<const DexFile*> dex_files_;
689
690 DISALLOW_COPY_AND_ASSIGN(JitMadviseDontNeedTask);
691};
692
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000693class ZygoteTask final : public Task {
694 public:
695 ZygoteTask() {}
696
697 void Run(Thread* self) override {
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100698 Runtime* runtime = Runtime::Current();
699 std::string profile_file;
700 for (const std::string& option : runtime->GetImageCompilerOptions()) {
701 if (android::base::StartsWith(option, "--profile-file=")) {
702 profile_file = option.substr(strlen("--profile-file="));
703 break;
704 }
705 }
706
707 const std::vector<const DexFile*>& boot_class_path =
708 runtime->GetClassLinker()->GetBootClassPath();
709 ScopedNullHandle<mirror::ClassLoader> null_handle;
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100710 std::string boot_profile = GetBootProfileFile(profile_file);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100711 // We add to the queue for zygote so that we can fork processes in-between
712 // compilations.
Nicolas Geoffraya6f35832019-08-09 13:34:19 +0100713 uint32_t added_to_queue = 0;
714 if (Runtime::Current()->IsPrimaryZygote()) {
715 // We avoid doing compilation at boot for the secondary zygote, as apps
716 // forked from it are not critical for boot.
717 added_to_queue += runtime->GetJit()->CompileMethodsFromBootProfile(
718 self, boot_class_path, boot_profile, null_handle, /* add_to_queue= */ true);
719 }
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100720 added_to_queue += runtime->GetJit()->CompileMethodsFromProfile(
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100721 self, boot_class_path, profile_file, null_handle, /* add_to_queue= */ true);
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100722
723 JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache();
724 code_cache->GetZygoteMap()->Initialize(added_to_queue);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100725 }
726
727 void Finalize() override {
728 delete this;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000729 }
730
731 private:
732 DISALLOW_COPY_AND_ASSIGN(ZygoteTask);
733};
734
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100735class JitProfileTask final : public Task {
736 public:
737 JitProfileTask(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100738 jobject class_loader) {
739 ScopedObjectAccess soa(Thread::Current());
740 StackHandleScope<1> hs(soa.Self());
741 Handle<mirror::ClassLoader> h_loader(hs.NewHandle(
742 soa.Decode<mirror::ClassLoader>(class_loader)));
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100743 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
744 for (const auto& dex_file : dex_files) {
745 dex_files_.push_back(dex_file.get());
746 // Register the dex file so that we can guarantee it doesn't get deleted
747 // while reading it during the task.
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100748 class_linker->RegisterDexFile(*dex_file.get(), h_loader.Get());
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100749 }
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100750 // We also create our own global ref to use this class loader later.
751 class_loader_ = soa.Vm()->AddGlobalRef(soa.Self(), h_loader.Get());
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100752 }
753
754 void Run(Thread* self) override {
755 ScopedObjectAccess soa(self);
756 StackHandleScope<1> hs(self);
757 Handle<mirror::ClassLoader> loader = hs.NewHandle<mirror::ClassLoader>(
758 soa.Decode<mirror::ClassLoader>(class_loader_));
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100759
760 std::string profile = GetProfileFile(dex_files_[0]->GetLocation());
761 std::string boot_profile = GetBootProfileFile(profile);
762
Nicolas Geoffrayccb0b5f2019-08-15 18:10:50 +0100763 Jit* jit = Runtime::Current()->GetJit();
764
765 jit->CompileMethodsFromBootProfile(
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100766 self,
767 dex_files_,
768 boot_profile,
769 loader,
770 /* add_to_queue= */ false);
771
Nicolas Geoffrayccb0b5f2019-08-15 18:10:50 +0100772 jit->CompileMethodsFromProfile(
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100773 self,
774 dex_files_,
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100775 profile,
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100776 loader,
David Srbeckybfcea3d2019-08-05 15:44:00 +0100777 /* add_to_queue= */ true);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100778 }
779
780 void Finalize() override {
781 delete this;
782 }
783
Nicolas Geoffrayf5a07ae2019-06-20 14:59:07 +0100784 ~JitProfileTask() {
785 ScopedObjectAccess soa(Thread::Current());
786 soa.Vm()->DeleteGlobalRef(soa.Self(), class_loader_);
787 }
788
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100789 private:
790 std::vector<const DexFile*> dex_files_;
791 jobject class_loader_;
792
793 DISALLOW_COPY_AND_ASSIGN(JitProfileTask);
794};
795
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000796void Jit::CreateThreadPool() {
797 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
798 // is not null when we instrument.
799
800 // We need peers as we may report the JIT thread, e.g., in the debugger.
801 constexpr bool kJitPoolNeedsPeers = true;
802 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
803
804 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
805 Start();
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000806
807 // If we're not using the default boot image location, request a JIT task to
808 // compile all methods in the boot image profile.
809 Runtime* runtime = Runtime::Current();
David Srbecky3db3d372019-04-17 18:19:17 +0100810 if (runtime->IsZygote() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) {
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000811 thread_pool_->AddTask(Thread::Current(), new ZygoteTask());
812 }
813}
814
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100815void Jit::RegisterDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100816 jobject class_loader) {
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100817 if (dex_files.empty()) {
818 return;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000819 }
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100820 Runtime* runtime = Runtime::Current();
821 if (runtime->IsSystemServer() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) {
822 thread_pool_->AddTask(Thread::Current(), new JitProfileTask(dex_files, class_loader));
823 }
824}
825
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100826bool Jit::CompileMethodFromProfile(Thread* self,
827 ClassLinker* class_linker,
828 uint32_t method_idx,
829 Handle<mirror::DexCache> dex_cache,
830 Handle<mirror::ClassLoader> class_loader,
David Srbeckybfcea3d2019-08-05 15:44:00 +0100831 bool add_to_queue,
832 bool compile_after_boot) {
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100833 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
834 method_idx, dex_cache, class_loader);
835 if (method == nullptr) {
836 self->ClearException();
837 return false;
838 }
839 if (!method->IsCompilable() || !method->IsInvokable()) {
840 return false;
841 }
Nicolas Geoffraya6f35832019-08-09 13:34:19 +0100842 if (method->IsPreCompiled()) {
843 // Already seen by another profile.
844 return false;
845 }
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100846 const void* entry_point = method->GetEntryPointFromQuickCompiledCode();
847 if (class_linker->IsQuickToInterpreterBridge(entry_point) ||
848 class_linker->IsQuickGenericJniStub(entry_point) ||
849 class_linker->IsQuickResolutionStub(entry_point)) {
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100850 method->SetPreCompiled();
Nicolas Geoffraya6f35832019-08-09 13:34:19 +0100851 if (!add_to_queue) {
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100852 CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ true);
853 } else {
David Srbeckybfcea3d2019-08-05 15:44:00 +0100854 Task* task = new JitCompileTask(method, JitCompileTask::TaskKind::kPreCompile);
855 if (compile_after_boot) {
856 MutexLock mu(Thread::Current(), boot_completed_lock_);
857 if (!boot_completed_) {
858 tasks_after_boot_.push_back(task);
859 return true;
860 }
861 DCHECK(tasks_after_boot_.empty());
862 }
863 thread_pool_->AddTask(self, task);
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100864 return true;
865 }
866 }
867 return false;
868}
869
870uint32_t Jit::CompileMethodsFromBootProfile(
871 Thread* self,
872 const std::vector<const DexFile*>& dex_files,
873 const std::string& profile_file,
874 Handle<mirror::ClassLoader> class_loader,
875 bool add_to_queue) {
876 unix_file::FdFile profile(profile_file.c_str(), O_RDONLY, true);
877
878 if (profile.Fd() == -1) {
879 PLOG(WARNING) << "No boot profile: " << profile_file;
880 return 0u;
881 }
882
883 ProfileBootInfo profile_info;
884 if (!profile_info.Load(profile.Fd(), dex_files)) {
885 LOG(ERROR) << "Could not load profile file: " << profile_file;
886 return 0u;
887 }
888
889 ScopedObjectAccess soa(self);
890 VariableSizedHandleScope handles(self);
891 std::vector<Handle<mirror::DexCache>> dex_caches;
892 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
893 for (const DexFile* dex_file : profile_info.GetDexFiles()) {
894 dex_caches.push_back(handles.NewHandle(class_linker->FindDexCache(self, *dex_file)));
895 }
896
897 uint32_t added_to_queue = 0;
898 for (const std::pair<uint32_t, uint32_t>& pair : profile_info.GetMethods()) {
899 if (CompileMethodFromProfile(self,
900 class_linker,
901 pair.second,
902 dex_caches[pair.first],
903 class_loader,
David Srbeckybfcea3d2019-08-05 15:44:00 +0100904 add_to_queue,
905 /*compile_after_boot=*/false)) {
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100906 ++added_to_queue;
907 }
908 }
909 return added_to_queue;
910}
911
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100912uint32_t Jit::CompileMethodsFromProfile(
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100913 Thread* self,
914 const std::vector<const DexFile*>& dex_files,
915 const std::string& profile_file,
916 Handle<mirror::ClassLoader> class_loader,
917 bool add_to_queue) {
918
919 if (profile_file.empty()) {
920 LOG(WARNING) << "Expected a profile file in JIT zygote mode";
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100921 return 0u;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000922 }
923
Nicolas Geoffraya6f35832019-08-09 13:34:19 +0100924 // We don't generate boot profiles on device, therefore we don't
925 // need to lock the file.
926 unix_file::FdFile profile(profile_file.c_str(), O_RDONLY, true);
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000927
Nicolas Geoffraya6f35832019-08-09 13:34:19 +0100928 if (profile.Fd() == -1) {
929 PLOG(WARNING) << "No profile: " << profile_file;
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100930 return 0u;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000931 }
932
933 ProfileCompilationInfo profile_info;
Nicolas Geoffraya6f35832019-08-09 13:34:19 +0100934 if (!profile_info.Load(profile.Fd())) {
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000935 LOG(ERROR) << "Could not load profile file";
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100936 return 0u;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000937 }
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000938 ScopedObjectAccess soa(self);
939 StackHandleScope<1> hs(self);
940 MutableHandle<mirror::DexCache> dex_cache = hs.NewHandle<mirror::DexCache>(nullptr);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100941 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100942 uint32_t added_to_queue = 0u;
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100943 for (const DexFile* dex_file : dex_files) {
Martin Stjernholme58624f2019-09-20 15:53:40 +0100944 if (LocationIsOnArtModule(dex_file->GetLocation().c_str())) {
945 // The ART module jars are already preopted.
Nicolas Geoffrayf59bc112019-04-03 10:29:29 +0100946 continue;
947 }
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +0100948 // To speed up class lookups, generate a type lookup table for
949 // the dex file.
Nicolas Geoffrayc5e3a522019-04-30 09:47:55 +0100950 if (dex_file->GetOatDexFile() == nullptr) {
951 TypeLookupTable type_lookup_table = TypeLookupTable::Create(*dex_file);
952 type_lookup_tables_.push_back(
953 std::make_unique<art::OatDexFile>(std::move(type_lookup_table)));
954 dex_file->SetOatDexFile(type_lookup_tables_.back().get());
955 }
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +0100956
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000957 std::set<dex::TypeIndex> class_types;
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000958 std::set<uint16_t> all_methods;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000959 if (!profile_info.GetClassesAndMethods(*dex_file,
960 &class_types,
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000961 &all_methods,
962 &all_methods,
963 &all_methods)) {
Nicolas Geoffrayf59bc112019-04-03 10:29:29 +0100964 // This means the profile file did not reference the dex file, which is the case
965 // if there's no classes and methods of that dex file in the profile.
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000966 continue;
967 }
968 dex_cache.Assign(class_linker->FindDexCache(self, *dex_file));
969 CHECK(dex_cache != nullptr) << "Could not find dex cache for " << dex_file->GetLocation();
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000970
971 for (uint16_t method_idx : all_methods) {
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100972 if (CompileMethodFromProfile(self,
973 class_linker,
974 method_idx,
975 dex_cache,
976 class_loader,
David Srbeckybfcea3d2019-08-05 15:44:00 +0100977 add_to_queue,
978 /*compile_after_boot=*/true)) {
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100979 ++added_to_queue;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000980 }
981 }
982 }
Nicolas Geoffrayccb0b5f2019-08-15 18:10:50 +0100983
984 // Add a madvise task to release dex file pages once all compilation is done.
985 JitMadviseDontNeedTask* task = new JitMadviseDontNeedTask(dex_files);
986 MutexLock mu(Thread::Current(), boot_completed_lock_);
987 if (!boot_completed_) {
988 tasks_after_boot_.push_back(task);
989 } else {
990 DCHECK(tasks_after_boot_.empty());
991 thread_pool_->AddTask(self, task);
992 }
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100993 return added_to_queue;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000994}
995
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100996static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray32384402019-07-17 20:06:44 +0100997 if (method->IsClassInitializer() || !method->IsCompilable() || method->IsPreCompiled()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100998 // We do not want to compile such methods.
999 return true;
1000 }
1001 if (method->IsNative()) {
1002 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001003 if (klass == GetClassRoot<mirror::MethodHandle>() ||
1004 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +01001005 // MethodHandle and VarHandle invocation methods are required to throw an
1006 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001007 // implementations that raise the exception. We need to disable JIT compilation of these JNI
Orion Hodson52f5a1f2018-05-02 11:05:44 +01001008 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
1009 // stubs. Since these stubs have different stack representations we can then crash in stack
1010 // walking (b/78151261).
1011 return true;
1012 }
1013 }
1014 return false;
1015}
1016
David Srbeckye3fc2d12018-11-30 13:41:14 +00001017bool Jit::MaybeCompileMethod(Thread* self,
1018 ArtMethod* method,
1019 uint32_t old_count,
1020 uint32_t new_count,
1021 bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001022 if (thread_pool_ == nullptr) {
David Srbeckye3fc2d12018-11-30 13:41:14 +00001023 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001024 }
David Srbecky1fc4d422019-07-23 16:55:19 +01001025 if (UNLIKELY(method->IsPreCompiled()) && !with_backedges /* don't check for OSR */) {
Nicolas Geoffray5a0b6722019-09-24 15:09:40 +01001026 if (!method->NeedsInitializationCheck()) {
1027 const void* entry_point = code_cache_->GetSavedEntryPointOfPreCompiledMethod(method);
1028 if (entry_point != nullptr) {
1029 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(method, entry_point);
1030 return true;
1031 }
David Srbecky1fc4d422019-07-23 16:55:19 +01001032 }
1033 }
Nicolas Geoffray5a0b6722019-09-24 15:09:40 +01001034
Orion Hodson52f5a1f2018-05-02 11:05:44 +01001035 if (IgnoreSamplesForMethod(method)) {
David Srbeckye3fc2d12018-11-30 13:41:14 +00001036 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001037 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +01001038 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +00001039 // Tests might request JIT on first use (compiled synchronously in the interpreter).
David Srbeckye3fc2d12018-11-30 13:41:14 +00001040 return false;
David Srbeckyf4886df2017-12-11 16:06:29 +00001041 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +01001042 DCHECK_GT(WarmMethodThreshold(), 0);
1043 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
1044 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
1045 DCHECK_GE(PriorityThreadWeight(), 1);
1046 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001047
David Srbeckye3fc2d12018-11-30 13:41:14 +00001048 if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) {
1049 // Note: Native method have no "warm" state or profiling info.
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001050 if (!method->IsNative() &&
1051 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) &&
1052 code_cache_->CanAllocateProfilingInfo()) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001053 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +00001054 if (success) {
1055 VLOG(jit) << "Start profiling " << method->PrettyMethod();
1056 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001057
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +00001058 if (thread_pool_ == nullptr) {
1059 // Calling ProfilingInfo::Create might put us in a suspended state, which could
1060 // lead to the thread pool being deleted when we are shutting down.
David Srbeckye3fc2d12018-11-30 13:41:14 +00001061 return false;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +00001062 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001063
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +00001064 if (!success) {
1065 // We failed allocating. Instead of doing the collection on the Java thread, we push
1066 // an allocation to a compiler thread, that will do the collection.
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001067 thread_pool_->AddTask(
1068 self, new JitCompileTask(method, JitCompileTask::TaskKind::kAllocateProfile));
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +00001069 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001070 }
David Srbeckye3fc2d12018-11-30 13:41:14 +00001071 }
1072 if (UseJitCompilation()) {
David Srbeckyc45b5892019-04-24 10:32:04 +01001073 if (old_count == 0 &&
1074 method->IsNative() &&
1075 Runtime::Current()->IsUsingApexBootImageLocation()) {
1076 // jitzygote: Compile JNI stub on first use to avoid the expensive generic stub.
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +01001077 CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ false);
David Srbeckyc45b5892019-04-24 10:32:04 +01001078 return true;
1079 }
David Srbeckye3fc2d12018-11-30 13:41:14 +00001080 if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) {
1081 if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Calin Juravleffc87072016-04-20 14:22:09 +01001082 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001083 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompile));
Calin Juravleffc87072016-04-20 14:22:09 +01001084 }
David Srbeckye3fc2d12018-11-30 13:41:14 +00001085 }
1086 if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +01001087 if (!with_backedges) {
David Srbeckye3fc2d12018-11-30 13:41:14 +00001088 return false;
Calin Juravleffc87072016-04-20 14:22:09 +01001089 }
Vladimir Marko2196c652017-11-30 16:16:07 +00001090 DCHECK(!method->IsNative()); // No back edges reported for native methods.
David Srbeckye3fc2d12018-11-30 13:41:14 +00001091 if (!code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +01001092 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001093 thread_pool_->AddTask(
1094 self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr));
Calin Juravleffc87072016-04-20 14:22:09 +01001095 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001096 }
1097 }
David Srbeckye3fc2d12018-11-30 13:41:14 +00001098 return true;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001099}
1100
Alex Light59b950f2018-10-08 10:43:06 -07001101class ScopedSetRuntimeThread {
1102 public:
1103 explicit ScopedSetRuntimeThread(Thread* self)
1104 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
1105 self_->SetIsRuntimeThread(true);
1106 }
1107
1108 ~ScopedSetRuntimeThread() {
1109 self_->SetIsRuntimeThread(was_runtime_thread_);
1110 }
1111
1112 private:
1113 Thread* self_;
1114 bool was_runtime_thread_;
1115};
1116
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001117void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +01001118 Runtime* runtime = Runtime::Current();
Nicolas Geoffray5e33ccd2019-07-03 10:53:08 +01001119 if (UNLIKELY(runtime->UseJitCompilation() && JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +00001120 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +00001121 if (np_method->IsCompilable()) {
Nicolas Geoffray5e33ccd2019-07-03 10:53:08 +01001122 if (!np_method->IsNative() && GetCodeCache()->CanAllocateProfilingInfo()) {
Nicolas Geoffray29bb8032019-06-06 10:32:24 +01001123 // The compiler requires a ProfilingInfo object for non-native methods.
1124 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
1125 }
1126 // TODO(ngeoffray): For JIT at first use, use kPreCompile. Currently we don't due to
1127 // conflicts with jitzygote optimizations.
1128 JitCompileTask compile_task(method, JitCompileTask::TaskKind::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -07001129 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
1130 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +00001131 compile_task.Run(thread);
1132 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001133 return;
1134 }
1135
Andreas Gampe542451c2016-07-26 09:02:02 -07001136 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001137 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -07001138 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
1139 // must remain the instrumentation entrypoint.
1140 if ((profiling_info != nullptr) &&
1141 (profiling_info->GetSavedEntryPoint() != nullptr) &&
1142 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +01001143 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1144 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001145 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001146 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001147 }
1148}
1149
Mathieu Chartieref41db72016-10-25 15:08:01 -07001150void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001151 ArtMethod* caller,
1152 uint32_t dex_pc,
1153 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -07001154 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001155 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001156 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001157 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001158 info->AddInvokeInfo(dex_pc, this_object->GetClass());
1159 }
1160}
1161
1162void Jit::WaitForCompilationToFinish(Thread* self) {
1163 if (thread_pool_ != nullptr) {
1164 thread_pool_->Wait(self, false, false);
1165 }
1166}
1167
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001168void Jit::Stop() {
1169 Thread* self = Thread::Current();
1170 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
1171 WaitForCompilationToFinish(self);
1172 GetThreadPool()->StopWorkers(self);
1173 WaitForCompilationToFinish(self);
1174}
1175
1176void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +00001177 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001178}
1179
Andreas Gampef149b3f2016-11-16 14:58:24 -08001180ScopedJitSuspend::ScopedJitSuspend() {
1181 jit::Jit* jit = Runtime::Current()->GetJit();
1182 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
1183 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001184 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -08001185 }
1186}
1187
1188ScopedJitSuspend::~ScopedJitSuspend() {
1189 if (was_on_) {
1190 DCHECK(Runtime::Current()->GetJit() != nullptr);
1191 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001192 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -08001193 }
1194}
1195
Nicolas Geoffray0d54cfb2019-05-03 09:13:52 +01001196void Jit::PostForkChildAction(bool is_system_server, bool is_zygote) {
Nicolas Geoffraye9455f62019-08-15 20:57:04 +01001197 // Clear the potential boot tasks inherited from the zygote.
1198 {
1199 MutexLock mu(Thread::Current(), boot_completed_lock_);
1200 tasks_after_boot_.clear();
1201 }
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001202 if (is_zygote || Runtime::Current()->IsSafeMode()) {
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001203 // Delete the thread pool, we are not going to JIT.
1204 thread_pool_.reset(nullptr);
1205 return;
1206 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +00001207 // At this point, the compiler options have been adjusted to the particular configuration
1208 // of the forked child. Parse them again.
David Srbecky46b53532019-08-06 13:39:05 +01001209 jit_compiler_->ParseCompilerOptions();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +00001210
1211 // Adjust the status of code cache collection: the status from zygote was to not collect.
David Srbecky46b53532019-08-06 13:39:05 +01001212 code_cache_->SetGarbageCollectCode(!jit_compiler_->GenerateDebugInfo() &&
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +00001213 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001214
Nicolas Geoffraya67daeb2019-08-12 10:41:25 +01001215 if (is_system_server && Runtime::Current()->IsUsingApexBootImageLocation()) {
1216 // Disable garbage collection: we don't want it to delete methods we're compiling
1217 // through boot and system server profiles.
1218 // TODO(ngeoffray): Fix this so we still collect deoptimized and unused code.
1219 code_cache_->SetGarbageCollectCode(false);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001220 }
Nicolas Geoffray371390f2019-09-27 09:57:38 +01001221
1222 // We do this here instead of PostZygoteFork, as NativeDebugInfoPostFork only
1223 // applies to a child.
1224 NativeDebugInfoPostFork();
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001225}
1226
1227void Jit::PreZygoteFork() {
1228 if (thread_pool_ == nullptr) {
1229 return;
1230 }
1231 thread_pool_->DeleteThreads();
David Srbecky1550a662019-08-14 17:16:46 +01001232
1233 NativeDebugInfoPreFork();
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001234}
1235
1236void Jit::PostZygoteFork() {
1237 if (thread_pool_ == nullptr) {
1238 return;
1239 }
1240 thread_pool_->CreateThreads();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +00001241}
1242
David Srbeckybfcea3d2019-08-05 15:44:00 +01001243void Jit::BootCompleted() {
1244 Thread* self = Thread::Current();
1245 std::deque<Task*> tasks;
1246 {
1247 MutexLock mu(self, boot_completed_lock_);
1248 tasks = std::move(tasks_after_boot_);
1249 boot_completed_ = true;
1250 }
1251 for (Task* task : tasks) {
1252 thread_pool_->AddTask(self, task);
1253 }
1254}
1255
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001256bool Jit::CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const {
1257 return !is_for_shared_region ||
1258 Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(method->GetDeclaringClass());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001259}
1260
1261bool Jit::CanEncodeClass(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001262 return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001263}
1264
1265bool Jit::CanEncodeString(ObjPtr<mirror::String> string, bool is_for_shared_region) const {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001266 return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001267}
1268
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001269bool Jit::CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const {
1270 if (!is_for_shared_region) {
1271 return cls->IsInitialized();
1272 } else {
1273 // Look up the class status in the oat file.
1274 const DexFile& dex_file = *cls->GetDexCache()->GetDexFile();
1275 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1276 // In case we run without an image there won't be a backing oat file.
1277 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
1278 return false;
1279 }
1280 uint16_t class_def_index = cls->GetDexClassDefIndex();
1281 return oat_dex_file->GetOatClass(class_def_index).GetStatus() >= ClassStatus::kInitialized;
1282 }
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001283}
1284
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001285} // namespace jit
1286} // namespace art