blob: 737acdaa807affadc30d5e4ef8776525867c6a18 [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"
David Sehr82d046e2018-04-23 08:14:19 -070042#include "profile/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000043#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044#include "runtime.h"
45#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070046#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000047#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070048#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010049#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050
51namespace art {
52namespace jit {
53
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000054static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000055
Andreas Gampe7897cec2017-07-19 16:28:59 -070056// Different compilation threshold constants. These can be overridden on the command line.
57static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
58static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
59static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
60
Mathieu Chartier72918ea2016-03-24 11:07:06 -070061// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080062void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070063void* Jit::jit_compiler_handle_ = nullptr;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000064void* (*Jit::jit_load_)(void) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070065void (*Jit::jit_unload_)(void*) = nullptr;
Nicolas Geoffray7f7539b2019-06-06 16:20:54 +010066bool (*Jit::jit_compile_method_)(void*, JitMemoryRegion*, ArtMethod*, Thread*, bool, bool)
67 = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070068void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000069bool (*Jit::jit_generate_debug_info_)(void*) = nullptr;
70void (*Jit::jit_update_options_)(void*) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070071
Andreas Gampe7897cec2017-07-19 16:28:59 -070072struct StressModeHelper {
73 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
74};
75DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
76
David Srbeckye3fc2d12018-11-30 13:41:14 +000077uint32_t JitOptions::RoundUpThreshold(uint32_t threshold) {
78 if (threshold > kJitSamplesBatchSize) {
79 threshold = RoundUp(threshold, kJitSamplesBatchSize);
80 }
81 CHECK_LE(threshold, std::numeric_limits<uint16_t>::max());
82 return threshold;
83}
84
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010087 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000088
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000089 jit_options->code_cache_initial_capacity_ =
90 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
91 jit_options->code_cache_max_capacity_ =
92 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070093 jit_options->dump_info_on_shutdown_ =
94 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010095 jit_options->profile_saver_options_ =
96 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010097 jit_options->thread_pool_pthread_priority_ =
98 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000099
Andreas Gampe7897cec2017-07-19 16:28:59 -0700100 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
101 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
102 } else {
103 jit_options->compile_threshold_ =
104 kIsDebugBuild
105 ? (StressModeHelper::kSlowMode
106 ? kJitSlowStressDefaultCompileThreshold
107 : kJitStressDefaultCompileThreshold)
108 : kJitDefaultCompileThreshold;
109 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000110 jit_options->compile_threshold_ = RoundUpThreshold(jit_options->compile_threshold_);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000111
112 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
113 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000114 } else {
115 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
116 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000117 jit_options->warmup_threshold_ = RoundUpThreshold(jit_options->warmup_threshold_);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000118
119 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
120 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000121 } else {
122 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
123 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000124 jit_options->osr_threshold_ =
125 RoundDown(std::numeric_limits<uint16_t>::max(), kJitSamplesBatchSize);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000126 }
127 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000128 jit_options->osr_threshold_ = RoundUpThreshold(jit_options->osr_threshold_);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000129
Calin Juravleb2771b42016-04-07 17:09:25 +0100130 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
131 jit_options->priority_thread_weight_ =
132 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
133 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
134 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
135 } else if (jit_options->priority_thread_weight_ == 0) {
136 LOG(FATAL) << "Priority thread weight cannot be 0.";
137 }
138 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100139 jit_options->priority_thread_weight_ = std::max(
140 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
141 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100142 }
143
Calin Juravle155ff3d2016-04-27 14:14:58 +0100144 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100145 jit_options->invoke_transition_weight_ =
146 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100147 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
148 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
149 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100150 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100151 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100152 } else {
153 jit_options->invoke_transition_weight_ = std::max(
154 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800155 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100156 }
157
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 return jit_options;
159}
160
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700161void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000162 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700163 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000164 MutexLock mu(Thread::Current(), lock_);
165 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700166}
167
Calin Juravleb8e69992016-03-09 15:37:48 +0000168void Jit::DumpForSigQuit(std::ostream& os) {
169 DumpInfo(os);
170 ProfileSaver::DumpInstanceInfo(os);
171}
172
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700173void Jit::AddTimingLogger(const TimingLogger& logger) {
174 cumulative_timings_.AddLogger(logger);
175}
176
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100177Jit::Jit(JitCodeCache* code_cache, JitOptions* options)
178 : code_cache_(code_cache),
179 options_(options),
180 cumulative_timings_("JIT timings"),
181 memory_use_("Memory used for compilation", 16),
182 lock_("JIT memory use lock") {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800183
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100184Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) {
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000185 if (jit_load_ == nullptr) {
186 LOG(WARNING) << "Not creating JIT: library not loaded";
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187 return nullptr;
188 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000189 jit_compiler_handle_ = (jit_load_)();
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000190 if (jit_compiler_handle_ == nullptr) {
191 LOG(WARNING) << "Not creating JIT: failed to allocate a compiler";
192 return nullptr;
193 }
194 std::unique_ptr<Jit> jit(new Jit(code_cache, options));
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000195
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000196 // If the code collector is enabled, check if that still holds:
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000197 // With 'perf', we want a 1-1 mapping between an address and a method.
198 // We aren't able to keep method pointers live during the instrumentation method entry trampoline
199 // so we will just disable jit-gc if we are doing that.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000200 if (code_cache->GetGarbageCollectCode()) {
201 code_cache->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
202 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
203 }
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100204
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000205 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000206 << PrettySize(options->GetCodeCacheInitialCapacity())
207 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000208 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100209 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100210
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100211 // Notify native debugger about the classes already loaded before the creation of the jit.
212 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800213 return jit.release();
214}
215
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000216template <typename T>
217bool Jit::LoadSymbol(T* address, const char* name, std::string* error_msg) {
218 *address = reinterpret_cast<T>(dlsym(jit_library_handle_, name));
219 if (*address == nullptr) {
220 *error_msg = std::string("JIT couldn't find ") + name + std::string(" entry point");
221 return false;
222 }
223 return true;
224}
225
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000226bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800227 jit_library_handle_ = dlopen(
228 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
229 if (jit_library_handle_ == nullptr) {
230 std::ostringstream oss;
231 oss << "JIT could not load libart-compiler.so: " << dlerror();
232 *error_msg = oss.str();
233 return false;
234 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000235 bool all_resolved = true;
236 all_resolved = all_resolved && LoadSymbol(&jit_load_, "jit_load", error_msg);
237 all_resolved = all_resolved && LoadSymbol(&jit_unload_, "jit_unload", error_msg);
238 all_resolved = all_resolved && LoadSymbol(&jit_compile_method_, "jit_compile_method", error_msg);
239 all_resolved = all_resolved && LoadSymbol(&jit_types_loaded_, "jit_types_loaded", error_msg);
240 all_resolved = all_resolved && LoadSymbol(&jit_update_options_, "jit_update_options", error_msg);
241 all_resolved = all_resolved &&
242 LoadSymbol(&jit_generate_debug_info_, "jit_generate_debug_info", error_msg);
243 if (!all_resolved) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800244 dlclose(jit_library_handle_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000245 return false;
246 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700247 return true;
248}
249
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100250bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool baseline, bool osr, bool prejit) {
Calin Juravleffc87072016-04-20 14:22:09 +0100251 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800252 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000253
Alex Light0fa17862017-10-24 13:43:05 -0700254 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100255 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700256 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
257 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
258 << " due to not being safe to jit according to runtime-callbacks. For example, there"
259 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700260 return false;
261 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100262
263 // Don't compile the method if we are supposed to be deoptimized.
264 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
265 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700266 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100267 return false;
268 }
269
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000270 JitMemoryRegion* region = GetCodeCache()->GetCurrentRegion();
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100271 if (osr && GetCodeCache()->IsSharedRegion(*region)) {
272 VLOG(jit) << "JIT not osr compiling "
273 << method->PrettyMethod()
274 << " due to using shared region";
275 return false;
276 }
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000277
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000278 // If we get a request to compile a proxy method, we pass the actual Java method
279 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700280 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000281 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr, prejit, region)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100282 return false;
283 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100284
285 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700286 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100287 << " osr=" << std::boolalpha << osr;
Nicolas Geoffray7f7539b2019-06-06 16:20:54 +0100288 bool success = jit_compile_method_(
289 jit_compiler_handle_, region, method_to_compile, self, baseline, osr);
buzbee454b3b62016-04-07 14:42:47 -0700290 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100291 if (!success) {
292 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700293 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100294 << " osr=" << std::boolalpha << osr;
295 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800296 if (kIsDebugBuild) {
297 if (self->IsExceptionPending()) {
298 mirror::Throwable* exception = self->GetException();
299 LOG(FATAL) << "No pending exception expected after compiling "
300 << ArtMethod::PrettyMethod(method)
301 << ": "
302 << exception->Dump();
303 }
304 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100305 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800306}
307
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800308void Jit::WaitForWorkersToBeCreated() {
309 if (thread_pool_ != nullptr) {
310 thread_pool_->WaitForWorkersToBeCreated();
311 }
312}
313
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800314void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100315 Thread* self = Thread::Current();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100316 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700317 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100318 {
319 ScopedSuspendAll ssa(__FUNCTION__);
320 // Clear thread_pool_ field while the threads are suspended.
321 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700322 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100323 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700324
325 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000326 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700327 pool->StopWorkers(self);
328 pool->RemoveAllTasks(self);
329 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100330 // We could just suspend all threads, but we know those threads
331 // will finish in a short period, so it's not worth adding a suspend logic
332 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700333 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800334 }
335}
336
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000337void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800338 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100339 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100340 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100341 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000342}
343
344void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100345 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
346 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100347 }
348}
349
Siva Chandra05d24152016-01-05 17:43:17 -0800350bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100351 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800352}
353
Nicolas Geoffray35122442016-03-02 12:05:30 +0000354bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
355 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
356}
357
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800358Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100359 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
360 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700361 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100362 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700363 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800364 DeleteThreadPool();
365 if (jit_compiler_handle_ != nullptr) {
366 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700367 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800368 }
369 if (jit_library_handle_ != nullptr) {
370 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700371 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800372 }
373}
374
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000375void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100376 if (!Runtime::Current()->UseJitCompilation()) {
377 // No need to notify if we only use the JIT to save profiles.
378 return;
379 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000380 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000381 if (jit_generate_debug_info_(jit->jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000382 DCHECK(jit->jit_types_loaded_ != nullptr);
383 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
384 }
385}
386
387void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
388 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100389 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700390 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000391 return true;
392 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800393 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000394 };
395
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000396 if (jit_generate_debug_info_(jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000397 ScopedObjectAccess so(Thread::Current());
398
399 CollectClasses visitor;
400 linker->VisitClasses(&visitor);
401 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000402 }
403}
404
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000405extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700406 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000407 const uint8_t* native_pc,
408 JValue* result,
409 const char* shorty,
410 Thread* self);
411
412bool Jit::MaybeDoOnStackReplacement(Thread* thread,
413 ArtMethod* method,
414 uint32_t dex_pc,
415 int32_t dex_pc_offset,
416 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000417 if (!kEnableOnStackReplacement) {
418 return false;
419 }
420
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000421 Jit* jit = Runtime::Current()->GetJit();
422 if (jit == nullptr) {
423 return false;
424 }
425
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000426 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
427 // Don't attempt to do an OSR if we are close to the stack limit. Since
428 // the interpreter frames are still on stack, OSR has the potential
429 // to stack overflow even for a simple loop.
430 // b/27094810.
431 return false;
432 }
433
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000434 // Get the actual Java method if this method is from a proxy class. The compiler
435 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700436 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000437
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000438 // Cheap check if the method has been compiled already. That's an indicator that we should
439 // osr into it.
440 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
441 return false;
442 }
443
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000444 // Fetch some data before looking up for an OSR method. We don't want thread
445 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
446 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000447 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800448 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000449 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700450 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000451 void** memory = nullptr;
452 size_t frame_size = 0;
453 ShadowFrame* shadow_frame = nullptr;
454 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000455
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000456 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700457 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000458 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
459 if (osr_method == nullptr) {
460 // No osr method yet, just return to the interpreter.
461 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000462 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000463
David Srbecky052f8ca2018-04-26 15:42:54 +0100464 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000465
466 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100467 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000468 if (!stack_map.IsValid()) {
469 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
470 // hope that the next branch has one.
471 return false;
472 }
473
Alex Light21611932017-09-26 13:07:39 -0700474 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
475 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
476 // disable OSR when single stepping, but that's currently hard to know at this point.
477 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700478 return false;
479 }
480
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000481 // We found a stack map, now fill the frame with dex register values from the interpreter's
482 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100483 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Artem Serov2808be82018-12-20 19:15:11 +0000484 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000485
486 frame_size = osr_method->GetFrameSizeInBytes();
487
488 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
489 // stack.
490 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
491 // but that is engineering complexity not worth the effort for something like OSR.
492 memory = reinterpret_cast<void**>(malloc(frame_size));
493 CHECK(memory != nullptr);
494 memset(memory, 0, frame_size);
495
496 // Art ABI: ArtMethod is at the bottom of the stack.
497 memory[0] = method;
498
499 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100500 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000501 // If we don't have a dex register map, then there are no live dex registers at
502 // this dex pc.
503 } else {
504 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100505 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000506 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000507 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000508 continue;
509 }
510
511 if (location == DexRegisterLocation::Kind::kConstant) {
512 // We skip constants because the compiled code knows how to handle them.
513 continue;
514 }
515
David Srbecky7dc11782016-02-25 13:23:56 +0000516 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000517
518 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100519 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000520 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
521 DCHECK_GT(slot_offset, 0);
522 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
523 }
524 }
525
David Srbecky052f8ca2018-04-26 15:42:54 +0100526 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000527 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000528 VLOG(jit) << "Jumping to "
529 << method_name
530 << "@"
531 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000532 }
533
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000534 {
535 ManagedStack fragment;
536 thread->PushManagedStackFragment(&fragment);
537 (*art_quick_osr_stub)(memory,
538 frame_size,
539 native_pc,
540 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000541 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000542 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000543
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000544 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
545 thread->DeoptimizeWithDeoptimizationException(result);
546 }
547 thread->PopManagedStackFragment(fragment);
548 }
549 free(memory);
550 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000551 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000552 return true;
553}
554
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000555void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
556 if (bytes > 4 * MB) {
557 LOG(INFO) << "Compiler allocated "
558 << PrettySize(bytes)
559 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700560 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000561 }
562 MutexLock mu(Thread::Current(), lock_);
563 memory_use_.AddValue(bytes);
564}
565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100566class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100567 public:
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000568 enum class TaskKind {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100569 kAllocateProfile,
570 kCompile,
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000571 kCompileBaseline,
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000572 kCompileOsr,
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100573 kPreCompile,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100574 };
575
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000576 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind), klass_(nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100577 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000578 // For a non-bootclasspath class, add a global ref to the class to prevent class unloading
579 // until compilation is done.
580 if (method->GetDeclaringClass()->GetClassLoader() != nullptr) {
581 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
582 CHECK(klass_ != nullptr);
583 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100584 }
585
586 ~JitCompileTask() {
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000587 if (klass_ != nullptr) {
588 ScopedObjectAccess soa(Thread::Current());
589 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
590 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100591 }
592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100593 void Run(Thread* self) override {
Andreas Gampe4bd52342019-07-15 15:09:04 -0700594 {
595 ScopedObjectAccess soa(self);
596 switch (kind_) {
597 case TaskKind::kPreCompile:
598 case TaskKind::kCompile:
599 case TaskKind::kCompileBaseline:
600 case TaskKind::kCompileOsr: {
601 Runtime::Current()->GetJit()->CompileMethod(
602 method_,
603 self,
604 /* baseline= */ (kind_ == TaskKind::kCompileBaseline),
605 /* osr= */ (kind_ == TaskKind::kCompileOsr),
606 /* prejit= */ (kind_ == TaskKind::kPreCompile));
607 break;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000608 }
Andreas Gampe4bd52342019-07-15 15:09:04 -0700609 case TaskKind::kAllocateProfile: {
610 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
611 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
612 }
613 break;
614 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100615 }
616 }
Calin Juravlea2638922016-04-29 16:44:11 +0100617 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100618 }
619
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100620 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100621 delete this;
622 }
623
624 private:
625 ArtMethod* const method_;
626 const TaskKind kind_;
627 jobject klass_;
628
629 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
630};
631
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000632class ZygoteTask final : public Task {
633 public:
634 ZygoteTask() {}
635
636 void Run(Thread* self) override {
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100637 Runtime* runtime = Runtime::Current();
638 std::string profile_file;
639 for (const std::string& option : runtime->GetImageCompilerOptions()) {
640 if (android::base::StartsWith(option, "--profile-file=")) {
641 profile_file = option.substr(strlen("--profile-file="));
642 break;
643 }
644 }
645
646 const std::vector<const DexFile*>& boot_class_path =
647 runtime->GetClassLinker()->GetBootClassPath();
648 ScopedNullHandle<mirror::ClassLoader> null_handle;
649 // We add to the queue for zygote so that we can fork processes in-between
650 // compilations.
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100651 uint32_t added_to_queue = runtime->GetJit()->CompileMethodsFromProfile(
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100652 self, boot_class_path, profile_file, null_handle, /* add_to_queue= */ true);
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100653
654 JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache();
655 code_cache->GetZygoteMap()->Initialize(added_to_queue);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100656 }
657
658 void Finalize() override {
659 delete this;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000660 }
661
662 private:
663 DISALLOW_COPY_AND_ASSIGN(ZygoteTask);
664};
665
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100666static std::string GetProfileFile(const std::string& dex_location) {
667 // Hardcoded assumption where the profile file is.
668 // TODO(ngeoffray): this is brittle and we would need to change change if we
669 // wanted to do more eager JITting of methods in a profile. This is
670 // currently only for system server.
671 return dex_location + ".prof";
672}
673
674class JitProfileTask final : public Task {
675 public:
676 JitProfileTask(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100677 jobject class_loader) {
678 ScopedObjectAccess soa(Thread::Current());
679 StackHandleScope<1> hs(soa.Self());
680 Handle<mirror::ClassLoader> h_loader(hs.NewHandle(
681 soa.Decode<mirror::ClassLoader>(class_loader)));
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100682 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
683 for (const auto& dex_file : dex_files) {
684 dex_files_.push_back(dex_file.get());
685 // Register the dex file so that we can guarantee it doesn't get deleted
686 // while reading it during the task.
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100687 class_linker->RegisterDexFile(*dex_file.get(), h_loader.Get());
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100688 }
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100689 // We also create our own global ref to use this class loader later.
690 class_loader_ = soa.Vm()->AddGlobalRef(soa.Self(), h_loader.Get());
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100691 }
692
693 void Run(Thread* self) override {
694 ScopedObjectAccess soa(self);
695 StackHandleScope<1> hs(self);
696 Handle<mirror::ClassLoader> loader = hs.NewHandle<mirror::ClassLoader>(
697 soa.Decode<mirror::ClassLoader>(class_loader_));
698 Runtime::Current()->GetJit()->CompileMethodsFromProfile(
699 self,
700 dex_files_,
701 GetProfileFile(dex_files_[0]->GetLocation()),
702 loader,
703 /* add_to_queue= */ false);
704 }
705
706 void Finalize() override {
707 delete this;
708 }
709
Nicolas Geoffrayf5a07ae2019-06-20 14:59:07 +0100710 ~JitProfileTask() {
711 ScopedObjectAccess soa(Thread::Current());
712 soa.Vm()->DeleteGlobalRef(soa.Self(), class_loader_);
713 }
714
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100715 private:
716 std::vector<const DexFile*> dex_files_;
717 jobject class_loader_;
718
719 DISALLOW_COPY_AND_ASSIGN(JitProfileTask);
720};
721
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000722void Jit::CreateThreadPool() {
723 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
724 // is not null when we instrument.
725
726 // We need peers as we may report the JIT thread, e.g., in the debugger.
727 constexpr bool kJitPoolNeedsPeers = true;
728 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
729
730 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
731 Start();
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000732
733 // If we're not using the default boot image location, request a JIT task to
734 // compile all methods in the boot image profile.
735 Runtime* runtime = Runtime::Current();
David Srbecky3db3d372019-04-17 18:19:17 +0100736 if (runtime->IsZygote() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) {
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000737 thread_pool_->AddTask(Thread::Current(), new ZygoteTask());
738 }
739}
740
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100741void Jit::RegisterDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100742 jobject class_loader) {
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100743 if (dex_files.empty()) {
744 return;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000745 }
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100746 Runtime* runtime = Runtime::Current();
747 if (runtime->IsSystemServer() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) {
748 thread_pool_->AddTask(Thread::Current(), new JitProfileTask(dex_files, class_loader));
749 }
750}
751
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100752uint32_t Jit::CompileMethodsFromProfile(
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100753 Thread* self,
754 const std::vector<const DexFile*>& dex_files,
755 const std::string& profile_file,
756 Handle<mirror::ClassLoader> class_loader,
757 bool add_to_queue) {
758
759 if (profile_file.empty()) {
760 LOG(WARNING) << "Expected a profile file in JIT zygote mode";
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100761 return 0u;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000762 }
763
764 std::string error_msg;
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100765 ScopedFlock profile = LockedFile::Open(
766 profile_file.c_str(), O_RDONLY, /* block= */ false, &error_msg);
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000767
768 // Return early if we're unable to obtain a lock on the profile.
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100769 if (profile.get() == nullptr) {
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000770 LOG(ERROR) << "Cannot lock profile: " << error_msg;
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100771 return 0u;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000772 }
773
774 ProfileCompilationInfo profile_info;
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100775 if (!profile_info.Load(profile->Fd())) {
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000776 LOG(ERROR) << "Could not load profile file";
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100777 return 0u;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000778 }
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000779 ScopedObjectAccess soa(self);
780 StackHandleScope<1> hs(self);
781 MutableHandle<mirror::DexCache> dex_cache = hs.NewHandle<mirror::DexCache>(nullptr);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100782 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100783 uint32_t added_to_queue = 0u;
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100784 for (const DexFile* dex_file : dex_files) {
Nicolas Geoffrayf59bc112019-04-03 10:29:29 +0100785 if (LocationIsOnRuntimeModule(dex_file->GetLocation().c_str())) {
786 // The runtime module jars are already preopted.
787 continue;
788 }
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +0100789 // To speed up class lookups, generate a type lookup table for
790 // the dex file.
Nicolas Geoffrayc5e3a522019-04-30 09:47:55 +0100791 if (dex_file->GetOatDexFile() == nullptr) {
792 TypeLookupTable type_lookup_table = TypeLookupTable::Create(*dex_file);
793 type_lookup_tables_.push_back(
794 std::make_unique<art::OatDexFile>(std::move(type_lookup_table)));
795 dex_file->SetOatDexFile(type_lookup_tables_.back().get());
796 }
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +0100797
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000798 std::set<dex::TypeIndex> class_types;
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000799 std::set<uint16_t> all_methods;
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000800 if (!profile_info.GetClassesAndMethods(*dex_file,
801 &class_types,
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000802 &all_methods,
803 &all_methods,
804 &all_methods)) {
Nicolas Geoffrayf59bc112019-04-03 10:29:29 +0100805 // This means the profile file did not reference the dex file, which is the case
806 // if there's no classes and methods of that dex file in the profile.
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000807 continue;
808 }
809 dex_cache.Assign(class_linker->FindDexCache(self, *dex_file));
810 CHECK(dex_cache != nullptr) << "Could not find dex cache for " << dex_file->GetLocation();
Nicolas Geoffray1d077ac2019-02-27 15:53:28 +0000811
812 for (uint16_t method_idx : all_methods) {
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000813 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100814 method_idx, dex_cache, class_loader);
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000815 if (method == nullptr) {
816 self->ClearException();
817 continue;
818 }
819 if (!method->IsCompilable() || !method->IsInvokable()) {
820 continue;
821 }
822 const void* entry_point = method->GetEntryPointFromQuickCompiledCode();
823 if (class_linker->IsQuickToInterpreterBridge(entry_point) ||
Nicolas Geoffray7989ac92019-04-10 12:42:30 +0100824 class_linker->IsQuickGenericJniStub(entry_point) ||
825 class_linker->IsQuickResolutionStub(entry_point)) {
Nicolas Geoffrayb10f0282019-04-12 17:30:52 +0100826 // Special case ZygoteServer class so that it gets compiled before the
827 // zygote enters it. This avoids needing to do OSR during app startup.
828 // TODO: have a profile instead.
Nicolas Geoffray32384402019-07-17 20:06:44 +0100829 method->SetPreCompiled();
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100830 if (!add_to_queue || method->GetDeclaringClass()->DescriptorEquals(
Nicolas Geoffrayb10f0282019-04-12 17:30:52 +0100831 "Lcom/android/internal/os/ZygoteServer;")) {
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100832 CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ true);
Nicolas Geoffrayb10f0282019-04-12 17:30:52 +0100833 } else {
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100834 ++added_to_queue;
Nicolas Geoffrayb10f0282019-04-12 17:30:52 +0100835 thread_pool_->AddTask(self,
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100836 new JitCompileTask(method, JitCompileTask::TaskKind::kPreCompile));
Nicolas Geoffrayb10f0282019-04-12 17:30:52 +0100837 }
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000838 }
839 }
840 }
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100841 return added_to_queue;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000842}
843
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100844static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray32384402019-07-17 20:06:44 +0100845 if (method->IsClassInitializer() || !method->IsCompilable() || method->IsPreCompiled()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100846 // We do not want to compile such methods.
847 return true;
848 }
849 if (method->IsNative()) {
850 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100851 if (klass == GetClassRoot<mirror::MethodHandle>() ||
852 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100853 // MethodHandle and VarHandle invocation methods are required to throw an
854 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000855 // implementations that raise the exception. We need to disable JIT compilation of these JNI
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100856 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
857 // stubs. Since these stubs have different stack representations we can then crash in stack
858 // walking (b/78151261).
859 return true;
860 }
861 }
862 return false;
863}
864
David Srbeckye3fc2d12018-11-30 13:41:14 +0000865bool Jit::MaybeCompileMethod(Thread* self,
866 ArtMethod* method,
867 uint32_t old_count,
868 uint32_t new_count,
869 bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100870 if (thread_pool_ == nullptr) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000871 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100872 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100873 if (IgnoreSamplesForMethod(method)) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000874 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100875 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100876 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000877 // Tests might request JIT on first use (compiled synchronously in the interpreter).
David Srbeckye3fc2d12018-11-30 13:41:14 +0000878 return false;
David Srbeckyf4886df2017-12-11 16:06:29 +0000879 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100880 DCHECK_GT(WarmMethodThreshold(), 0);
881 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
882 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
883 DCHECK_GE(PriorityThreadWeight(), 1);
884 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100885
David Srbeckye3fc2d12018-11-30 13:41:14 +0000886 if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) {
887 // Note: Native method have no "warm" state or profiling info.
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000888 if (!method->IsNative() &&
889 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) &&
890 code_cache_->CanAllocateProfilingInfo()) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700891 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000892 if (success) {
893 VLOG(jit) << "Start profiling " << method->PrettyMethod();
894 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100895
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000896 if (thread_pool_ == nullptr) {
897 // Calling ProfilingInfo::Create might put us in a suspended state, which could
898 // lead to the thread pool being deleted when we are shutting down.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000899 return false;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000900 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100901
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000902 if (!success) {
903 // We failed allocating. Instead of doing the collection on the Java thread, we push
904 // an allocation to a compiler thread, that will do the collection.
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000905 thread_pool_->AddTask(
906 self, new JitCompileTask(method, JitCompileTask::TaskKind::kAllocateProfile));
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000907 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100908 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000909 }
910 if (UseJitCompilation()) {
David Srbeckyc45b5892019-04-24 10:32:04 +0100911 if (old_count == 0 &&
912 method->IsNative() &&
913 Runtime::Current()->IsUsingApexBootImageLocation()) {
914 // jitzygote: Compile JNI stub on first use to avoid the expensive generic stub.
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100915 CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ false);
David Srbeckyc45b5892019-04-24 10:32:04 +0100916 return true;
917 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000918 if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) {
919 if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Calin Juravleffc87072016-04-20 14:22:09 +0100920 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000921 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompile));
Calin Juravleffc87072016-04-20 14:22:09 +0100922 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000923 }
924 if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100925 if (!with_backedges) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000926 return false;
Calin Juravleffc87072016-04-20 14:22:09 +0100927 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000928 DCHECK(!method->IsNative()); // No back edges reported for native methods.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000929 if (!code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100930 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000931 thread_pool_->AddTask(
932 self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr));
Calin Juravleffc87072016-04-20 14:22:09 +0100933 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100934 }
935 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000936 return true;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100937}
938
Alex Light59b950f2018-10-08 10:43:06 -0700939class ScopedSetRuntimeThread {
940 public:
941 explicit ScopedSetRuntimeThread(Thread* self)
942 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
943 self_->SetIsRuntimeThread(true);
944 }
945
946 ~ScopedSetRuntimeThread() {
947 self_->SetIsRuntimeThread(was_runtime_thread_);
948 }
949
950 private:
951 Thread* self_;
952 bool was_runtime_thread_;
953};
954
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100955void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100956 Runtime* runtime = Runtime::Current();
Nicolas Geoffray5e33ccd2019-07-03 10:53:08 +0100957 if (UNLIKELY(runtime->UseJitCompilation() && JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000958 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000959 if (np_method->IsCompilable()) {
Nicolas Geoffray5e33ccd2019-07-03 10:53:08 +0100960 if (!np_method->IsNative() && GetCodeCache()->CanAllocateProfilingInfo()) {
Nicolas Geoffray29bb8032019-06-06 10:32:24 +0100961 // The compiler requires a ProfilingInfo object for non-native methods.
962 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
963 }
964 // TODO(ngeoffray): For JIT at first use, use kPreCompile. Currently we don't due to
965 // conflicts with jitzygote optimizations.
966 JitCompileTask compile_task(method, JitCompileTask::TaskKind::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -0700967 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
968 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000969 compile_task.Run(thread);
970 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100971 return;
972 }
973
Nicolas Geoffray32384402019-07-17 20:06:44 +0100974 if (UNLIKELY(method->IsPreCompiled())) {
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100975 const void* code_ptr = code_cache_->GetZygoteMap()->GetCodeFor(method);
976 if (code_ptr != nullptr) {
977 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(method, code_ptr);
978 return;
979 }
980 }
981
Andreas Gampe542451c2016-07-26 09:02:02 -0700982 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100983 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -0700984 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
985 // must remain the instrumentation entrypoint.
986 if ((profiling_info != nullptr) &&
987 (profiling_info->GetSavedEntryPoint() != nullptr) &&
988 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100989 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
990 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100991 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700992 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100993 }
994}
995
Mathieu Chartieref41db72016-10-25 15:08:01 -0700996void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100997 ArtMethod* caller,
998 uint32_t dex_pc,
999 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -07001000 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001001 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001002 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001003 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +01001004 info->AddInvokeInfo(dex_pc, this_object->GetClass());
1005 }
1006}
1007
1008void Jit::WaitForCompilationToFinish(Thread* self) {
1009 if (thread_pool_ != nullptr) {
1010 thread_pool_->Wait(self, false, false);
1011 }
1012}
1013
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001014void Jit::Stop() {
1015 Thread* self = Thread::Current();
1016 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
1017 WaitForCompilationToFinish(self);
1018 GetThreadPool()->StopWorkers(self);
1019 WaitForCompilationToFinish(self);
1020}
1021
1022void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +00001023 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001024}
1025
Andreas Gampef149b3f2016-11-16 14:58:24 -08001026ScopedJitSuspend::ScopedJitSuspend() {
1027 jit::Jit* jit = Runtime::Current()->GetJit();
1028 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
1029 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001030 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -08001031 }
1032}
1033
1034ScopedJitSuspend::~ScopedJitSuspend() {
1035 if (was_on_) {
1036 DCHECK(Runtime::Current()->GetJit() != nullptr);
1037 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +00001038 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -08001039 }
1040}
1041
Nicolas Geoffray0d54cfb2019-05-03 09:13:52 +01001042void Jit::PostForkChildAction(bool is_system_server, bool is_zygote) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001043 if (is_zygote || Runtime::Current()->IsSafeMode()) {
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001044 // Delete the thread pool, we are not going to JIT.
1045 thread_pool_.reset(nullptr);
1046 return;
1047 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +00001048 // At this point, the compiler options have been adjusted to the particular configuration
1049 // of the forked child. Parse them again.
1050 jit_update_options_(jit_compiler_handle_);
1051
1052 // Adjust the status of code cache collection: the status from zygote was to not collect.
1053 code_cache_->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
1054 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001055
1056 if (thread_pool_ != nullptr) {
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +01001057 if (is_system_server &&
1058 Runtime::Current()->IsUsingApexBootImageLocation() &&
1059 UseJitCompilation()) {
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +01001060 // Disable garbage collection: we don't want it to delete methods we're compiling
1061 // through boot and system server profiles.
1062 // TODO(ngeoffray): Fix this so we still collect deoptimized and unused code.
1063 code_cache_->SetGarbageCollectCode(false);
Nicolas Geoffray0d54cfb2019-05-03 09:13:52 +01001064 }
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +00001065
1066 // Resume JIT compilation.
1067 thread_pool_->CreateThreads();
1068 }
1069}
1070
1071void Jit::PreZygoteFork() {
1072 if (thread_pool_ == nullptr) {
1073 return;
1074 }
1075 thread_pool_->DeleteThreads();
1076}
1077
1078void Jit::PostZygoteFork() {
1079 if (thread_pool_ == nullptr) {
1080 return;
1081 }
1082 thread_pool_->CreateThreads();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +00001083}
1084
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001085bool Jit::CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const {
1086 return !is_for_shared_region ||
1087 Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(method->GetDeclaringClass());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001088}
1089
1090bool Jit::CanEncodeClass(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001091 return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001092}
1093
1094bool Jit::CanEncodeString(ObjPtr<mirror::String> string, bool is_for_shared_region) const {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001095 return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001096}
1097
Nicolas Geoffray05b41c42019-06-28 12:46:33 +01001098bool Jit::CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const {
1099 if (!is_for_shared_region) {
1100 return cls->IsInitialized();
1101 } else {
1102 // Look up the class status in the oat file.
1103 const DexFile& dex_file = *cls->GetDexCache()->GetDexFile();
1104 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1105 // In case we run without an image there won't be a backing oat file.
1106 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
1107 return false;
1108 }
1109 uint16_t class_def_index = cls->GetDexClassDefIndex();
1110 return oat_dex_file->GetOatClass(class_def_index).GetStatus() >= ClassStatus::kInitialized;
1111 }
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001112}
1113
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001114} // namespace jit
1115} // namespace art