blob: e876a1bc89508582ca7344e40cf275ddef3339d9 [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"
Andreas Gampe57943812017-12-06 21:39:13 -080023#include "base/logging.h" // For VLOG.
Andreas Gampe0897e1c2017-05-16 08:36:56 -070024#include "base/memory_tool.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080025#include "base/runtime_debug.h"
David Sehrc431b9d2018-03-02 12:01:51 -080026#include "base/utils.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010027#include "class_root.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070028#include "debugger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "entrypoints/runtime_asm_entrypoints.h"
30#include "interpreter/interpreter.h"
31#include "jit_code_cache.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010032#include "jni/java_vm_ext.h"
Orion Hodson52f5a1f2018-05-02 11:05:44 +010033#include "mirror/method_handle_impl.h"
34#include "mirror/var_handle.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010035#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000036#include "oat_quick_method_header.h"
David Sehr82d046e2018-04-23 08:14:19 -070037#include "profile/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000038#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039#include "runtime.h"
40#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070041#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000042#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070043#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010044#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045
46namespace art {
47namespace jit {
48
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000049static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000050
Andreas Gampe7897cec2017-07-19 16:28:59 -070051// Different compilation threshold constants. These can be overridden on the command line.
52static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
53static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
54static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
55
Mathieu Chartier72918ea2016-03-24 11:07:06 -070056// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080057void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070058void* Jit::jit_compiler_handle_ = nullptr;
59void* (*Jit::jit_load_)(bool*) = nullptr;
60void (*Jit::jit_unload_)(void*) = nullptr;
61bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
62void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
63bool Jit::generate_debug_info_ = false;
64
Andreas Gampe7897cec2017-07-19 16:28:59 -070065struct StressModeHelper {
66 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
67};
68DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
69
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010072 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000073
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000074 jit_options->code_cache_initial_capacity_ =
75 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
76 jit_options->code_cache_max_capacity_ =
77 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070078 jit_options->dump_info_on_shutdown_ =
79 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010080 jit_options->profile_saver_options_ =
81 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010082 jit_options->thread_pool_pthread_priority_ =
83 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000084
Andreas Gampe7897cec2017-07-19 16:28:59 -070085 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
86 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
87 } else {
88 jit_options->compile_threshold_ =
89 kIsDebugBuild
90 ? (StressModeHelper::kSlowMode
91 ? kJitSlowStressDefaultCompileThreshold
92 : kJitStressDefaultCompileThreshold)
93 : kJitDefaultCompileThreshold;
94 }
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000095 if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
96 LOG(FATAL) << "Method compilation threshold is above its internal limit.";
97 }
98
99 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
100 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
101 if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
102 LOG(FATAL) << "Method warmup threshold is above its internal limit.";
103 }
104 } else {
105 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
106 }
107
108 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
109 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
110 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
111 LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
112 }
113 } else {
114 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
115 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
116 jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
117 }
118 }
119
Calin Juravleb2771b42016-04-07 17:09:25 +0100120 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
121 jit_options->priority_thread_weight_ =
122 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
123 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
124 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
125 } else if (jit_options->priority_thread_weight_ == 0) {
126 LOG(FATAL) << "Priority thread weight cannot be 0.";
127 }
128 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100129 jit_options->priority_thread_weight_ = std::max(
130 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
131 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100132 }
133
Calin Juravle155ff3d2016-04-27 14:14:58 +0100134 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100135 jit_options->invoke_transition_weight_ =
136 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100137 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
138 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
139 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100140 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100141 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100142 } else {
143 jit_options->invoke_transition_weight_ = std::max(
144 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800145 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100146 }
147
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 return jit_options;
149}
150
Vladimir Markoa710d912017-09-12 14:56:07 +0100151bool Jit::ShouldUsePriorityThreadWeight(Thread* self) {
152 return self->IsJitSensitiveThread() && Runtime::Current()->InJankPerceptibleProcessState();
Calin Juravleb2771b42016-04-07 17:09:25 +0100153}
154
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700155void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000156 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700157 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000158 MutexLock mu(Thread::Current(), lock_);
159 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700160}
161
Calin Juravleb8e69992016-03-09 15:37:48 +0000162void Jit::DumpForSigQuit(std::ostream& os) {
163 DumpInfo(os);
164 ProfileSaver::DumpInstanceInfo(os);
165}
166
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700167void Jit::AddTimingLogger(const TimingLogger& logger) {
168 cumulative_timings_.AddLogger(logger);
169}
170
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100171Jit::Jit(JitCodeCache* code_cache, JitOptions* options)
172 : code_cache_(code_cache),
173 options_(options),
174 cumulative_timings_("JIT timings"),
175 memory_use_("Memory used for compilation", 16),
176 lock_("JIT memory use lock") {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100178Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) {
179 CHECK(jit_compiler_handle_ != nullptr) << "Jit::LoadLibrary() needs to be called first";
180 std::unique_ptr<Jit> jit(new Jit(code_cache, options));
181 if (jit_compiler_handle_ == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 return nullptr;
183 }
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100184
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000185 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000186 << PrettySize(options->GetCodeCacheInitialCapacity())
187 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000188 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100189 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100190
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100191 jit->CreateThreadPool();
192
193 // Notify native debugger about the classes already loaded before the creation of the jit.
194 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 return jit.release();
196}
197
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100198bool Jit::BindCompilerMethods(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199 jit_library_handle_ = dlopen(
200 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
201 if (jit_library_handle_ == nullptr) {
202 std::ostringstream oss;
203 oss << "JIT could not load libart-compiler.so: " << dlerror();
204 *error_msg = oss.str();
205 return false;
206 }
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000207 jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800208 if (jit_load_ == nullptr) {
209 dlclose(jit_library_handle_);
210 *error_msg = "JIT couldn't find jit_load entry point";
211 return false;
212 }
213 jit_unload_ = reinterpret_cast<void (*)(void*)>(
214 dlsym(jit_library_handle_, "jit_unload"));
215 if (jit_unload_ == nullptr) {
216 dlclose(jit_library_handle_);
217 *error_msg = "JIT couldn't find jit_unload entry point";
218 return false;
219 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000220 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800221 dlsym(jit_library_handle_, "jit_compile_method"));
222 if (jit_compile_method_ == nullptr) {
223 dlclose(jit_library_handle_);
224 *error_msg = "JIT couldn't find jit_compile_method entry point";
225 return false;
226 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000227 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
228 dlsym(jit_library_handle_, "jit_types_loaded"));
229 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000230 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000231 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000232 return false;
233 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700234 return true;
235}
236
237bool Jit::LoadCompiler(std::string* error_msg) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100238 if (jit_library_handle_ == nullptr && !BindCompilerMethods(error_msg)) {
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700239 return false;
240 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000241 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800242 VLOG(jit) << "Calling JitLoad interpreter_only="
243 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000244 jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800245 if (jit_compiler_handle_ == nullptr) {
246 dlclose(jit_library_handle_);
247 *error_msg = "JIT couldn't load compiler";
248 return false;
249 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000250 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800251 return true;
252}
253
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000254bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100255 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800256 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000257
Alex Light0fa17862017-10-24 13:43:05 -0700258 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100259 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700260 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
261 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
262 << " due to not being safe to jit according to runtime-callbacks. For example, there"
263 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700264 return false;
265 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100266
267 // Don't compile the method if we are supposed to be deoptimized.
268 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
269 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700270 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100271 return false;
272 }
273
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000274 // If we get a request to compile a proxy method, we pass the actual Java method
275 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700276 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000277 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100278 return false;
279 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100280
281 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700282 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100283 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000284 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700285 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100286 if (!success) {
287 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700288 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100289 << " osr=" << std::boolalpha << osr;
290 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800291 if (kIsDebugBuild) {
292 if (self->IsExceptionPending()) {
293 mirror::Throwable* exception = self->GetException();
294 LOG(FATAL) << "No pending exception expected after compiling "
295 << ArtMethod::PrettyMethod(method)
296 << ": "
297 << exception->Dump();
298 }
299 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100300 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800301}
302
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100303bool Jit::ShouldGenerateDebugInfo() {
304 CHECK(CompilerIsLoaded());
305 return generate_debug_info_;
306}
307
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800308void Jit::CreateThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100309 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
310 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000311
312 // We need peers as we may report the JIT thread, e.g., in the debugger.
313 constexpr bool kJitPoolNeedsPeers = true;
314 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
315
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100316 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000317 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800318}
319
320void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100321 Thread* self = Thread::Current();
322 DCHECK(Runtime::Current()->IsShuttingDown(self));
323 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700324 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100325 {
326 ScopedSuspendAll ssa(__FUNCTION__);
327 // Clear thread_pool_ field while the threads are suspended.
328 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700329 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100330 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700331
332 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000333 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700334 pool->StopWorkers(self);
335 pool->RemoveAllTasks(self);
336 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100337 // We could just suspend all threads, but we know those threads
338 // will finish in a short period, so it's not worth adding a suspend logic
339 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700340 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800341 }
342}
343
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000344void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800345 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100346 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100347 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100348 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000349}
350
351void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100352 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
353 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100354 }
355}
356
Siva Chandra05d24152016-01-05 17:43:17 -0800357bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100358 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800359}
360
Nicolas Geoffray35122442016-03-02 12:05:30 +0000361bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
362 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
363}
364
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800365Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100366 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
367 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700368 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100369 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700370 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800371 DeleteThreadPool();
372 if (jit_compiler_handle_ != nullptr) {
373 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700374 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800375 }
376 if (jit_library_handle_ != nullptr) {
377 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700378 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800379 }
380}
381
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000382void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100383 if (!Runtime::Current()->UseJitCompilation()) {
384 // No need to notify if we only use the JIT to save profiles.
385 return;
386 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000387 jit::Jit* jit = Runtime::Current()->GetJit();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100388 if (generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000389 DCHECK(jit->jit_types_loaded_ != nullptr);
390 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
391 }
392}
393
394void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
395 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100396 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700397 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000398 return true;
399 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800400 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000401 };
402
403 if (generate_debug_info_) {
404 ScopedObjectAccess so(Thread::Current());
405
406 CollectClasses visitor;
407 linker->VisitClasses(&visitor);
408 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000409 }
410}
411
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000412extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700413 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000414 const uint8_t* native_pc,
415 JValue* result,
416 const char* shorty,
417 Thread* self);
418
419bool Jit::MaybeDoOnStackReplacement(Thread* thread,
420 ArtMethod* method,
421 uint32_t dex_pc,
422 int32_t dex_pc_offset,
423 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000424 if (!kEnableOnStackReplacement) {
425 return false;
426 }
427
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000428 Jit* jit = Runtime::Current()->GetJit();
429 if (jit == nullptr) {
430 return false;
431 }
432
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000433 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
434 // Don't attempt to do an OSR if we are close to the stack limit. Since
435 // the interpreter frames are still on stack, OSR has the potential
436 // to stack overflow even for a simple loop.
437 // b/27094810.
438 return false;
439 }
440
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000441 // Get the actual Java method if this method is from a proxy class. The compiler
442 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700443 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000444
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000445 // Cheap check if the method has been compiled already. That's an indicator that we should
446 // osr into it.
447 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
448 return false;
449 }
450
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000451 // Fetch some data before looking up for an OSR method. We don't want thread
452 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
453 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000454 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800455 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000456 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700457 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000458 void** memory = nullptr;
459 size_t frame_size = 0;
460 ShadowFrame* shadow_frame = nullptr;
461 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000462
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000463 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700464 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000465 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
466 if (osr_method == nullptr) {
467 // No osr method yet, just return to the interpreter.
468 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000469 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000470
David Srbecky052f8ca2018-04-26 15:42:54 +0100471 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000472
473 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100474 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000475 if (!stack_map.IsValid()) {
476 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
477 // hope that the next branch has one.
478 return false;
479 }
480
Alex Light21611932017-09-26 13:07:39 -0700481 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
482 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
483 // disable OSR when single stepping, but that's currently hard to know at this point.
484 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700485 return false;
486 }
487
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000488 // We found a stack map, now fill the frame with dex register values from the interpreter's
489 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100490 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000491
492 frame_size = osr_method->GetFrameSizeInBytes();
493
494 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
495 // stack.
496 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
497 // but that is engineering complexity not worth the effort for something like OSR.
498 memory = reinterpret_cast<void**>(malloc(frame_size));
499 CHECK(memory != nullptr);
500 memset(memory, 0, frame_size);
501
502 // Art ABI: ArtMethod is at the bottom of the stack.
503 memory[0] = method;
504
505 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100506 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000507 // If we don't have a dex register map, then there are no live dex registers at
508 // this dex pc.
509 } else {
David Srbeckyfd89b072018-06-03 12:00:22 +0100510 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000511 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100512 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000513 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000514 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000515 continue;
516 }
517
518 if (location == DexRegisterLocation::Kind::kConstant) {
519 // We skip constants because the compiled code knows how to handle them.
520 continue;
521 }
522
David Srbecky7dc11782016-02-25 13:23:56 +0000523 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000524
525 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100526 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000527 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
528 DCHECK_GT(slot_offset, 0);
529 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
530 }
531 }
532
David Srbecky052f8ca2018-04-26 15:42:54 +0100533 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000534 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000535 VLOG(jit) << "Jumping to "
536 << method_name
537 << "@"
538 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000539 }
540
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000541 {
542 ManagedStack fragment;
543 thread->PushManagedStackFragment(&fragment);
544 (*art_quick_osr_stub)(memory,
545 frame_size,
546 native_pc,
547 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000548 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000549 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000550
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000551 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
552 thread->DeoptimizeWithDeoptimizationException(result);
553 }
554 thread->PopManagedStackFragment(fragment);
555 }
556 free(memory);
557 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000558 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000559 return true;
560}
561
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000562void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
563 if (bytes > 4 * MB) {
564 LOG(INFO) << "Compiler allocated "
565 << PrettySize(bytes)
566 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700567 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000568 }
569 MutexLock mu(Thread::Current(), lock_);
570 memory_use_.AddValue(bytes);
571}
572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100573class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100574 public:
575 enum TaskKind {
576 kAllocateProfile,
577 kCompile,
578 kCompileOsr
579 };
580
581 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
582 ScopedObjectAccess soa(Thread::Current());
583 // Add a global ref to the class to prevent class unloading until compilation is done.
584 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
585 CHECK(klass_ != nullptr);
586 }
587
588 ~JitCompileTask() {
589 ScopedObjectAccess soa(Thread::Current());
590 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
591 }
592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100593 void Run(Thread* self) override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100594 ScopedObjectAccess soa(self);
595 if (kind_ == kCompile) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700596 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr= */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100597 } else if (kind_ == kCompileOsr) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700598 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr= */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100599 } else {
600 DCHECK(kind_ == kAllocateProfile);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700601 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700602 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100603 }
604 }
Calin Juravlea2638922016-04-29 16:44:11 +0100605 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100606 }
607
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100608 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100609 delete this;
610 }
611
612 private:
613 ArtMethod* const method_;
614 const TaskKind kind_;
615 jobject klass_;
616
617 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
618};
619
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100620static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
621 if (method->IsClassInitializer() || !method->IsCompilable()) {
622 // We do not want to compile such methods.
623 return true;
624 }
625 if (method->IsNative()) {
626 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100627 if (klass == GetClassRoot<mirror::MethodHandle>() ||
628 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100629 // MethodHandle and VarHandle invocation methods are required to throw an
630 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
631 // implementations that arise the exception. We need to disable JIT compilation of these JNI
632 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
633 // stubs. Since these stubs have different stack representations we can then crash in stack
634 // walking (b/78151261).
635 return true;
636 }
637 }
638 return false;
639}
640
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100641void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100642 if (thread_pool_ == nullptr) {
643 // Should only see this when shutting down.
644 DCHECK(Runtime::Current()->IsShuttingDown(self));
645 return;
646 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100647 if (IgnoreSamplesForMethod(method)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100648 return;
649 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100650 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000651 // Tests might request JIT on first use (compiled synchronously in the interpreter).
652 return;
653 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100654 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100655 DCHECK_GT(WarmMethodThreshold(), 0);
656 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
657 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
658 DCHECK_GE(PriorityThreadWeight(), 1);
659 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100660
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100661 uint16_t starting_count = method->GetCounter();
Vladimir Markoa710d912017-09-12 14:56:07 +0100662 if (Jit::ShouldUsePriorityThreadWeight(self)) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100663 count *= PriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100664 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100665 uint32_t new_count = starting_count + count;
Vladimir Marko2196c652017-11-30 16:16:07 +0000666 // Note: Native method have no "warm" state or profiling info.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100667 if (LIKELY(!method->IsNative()) && starting_count < WarmMethodThreshold()) {
668 if ((new_count >= WarmMethodThreshold()) &&
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000669 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700670 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000671 if (success) {
672 VLOG(jit) << "Start profiling " << method->PrettyMethod();
673 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100674
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000675 if (thread_pool_ == nullptr) {
676 // Calling ProfilingInfo::Create might put us in a suspended state, which could
677 // lead to the thread pool being deleted when we are shutting down.
678 DCHECK(Runtime::Current()->IsShuttingDown(self));
679 return;
680 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100681
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000682 if (!success) {
683 // We failed allocating. Instead of doing the collection on the Java thread, we push
684 // an allocation to a compiler thread, that will do the collection.
685 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
686 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100687 }
688 // Avoid jumping more than one state at a time.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100689 new_count = std::min(new_count, static_cast<uint32_t>(HotMethodThreshold() - 1));
690 } else if (UseJitCompilation()) {
691 if (starting_count < HotMethodThreshold()) {
692 if ((new_count >= HotMethodThreshold()) &&
Calin Juravleffc87072016-04-20 14:22:09 +0100693 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
694 DCHECK(thread_pool_ != nullptr);
695 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
696 }
697 // Avoid jumping more than one state at a time.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100698 new_count = std::min(new_count, static_cast<uint32_t>(OSRMethodThreshold() - 1));
699 } else if (starting_count < OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100700 if (!with_backedges) {
701 // If the samples don't contain any back edge, we don't increment the hotness.
702 return;
703 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000704 DCHECK(!method->IsNative()); // No back edges reported for native methods.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100705 if ((new_count >= OSRMethodThreshold()) && !code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100706 DCHECK(thread_pool_ != nullptr);
707 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
708 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100709 }
710 }
711 // Update hotness counter
712 method->SetCounter(new_count);
713}
714
Alex Light59b950f2018-10-08 10:43:06 -0700715class ScopedSetRuntimeThread {
716 public:
717 explicit ScopedSetRuntimeThread(Thread* self)
718 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
719 self_->SetIsRuntimeThread(true);
720 }
721
722 ~ScopedSetRuntimeThread() {
723 self_->SetIsRuntimeThread(was_runtime_thread_);
724 }
725
726 private:
727 Thread* self_;
728 bool was_runtime_thread_;
729};
730
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100731void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100732 Runtime* runtime = Runtime::Current();
733 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000734 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000735 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000736 if (!np_method->IsNative()) {
737 // The compiler requires a ProfilingInfo object for non-native methods.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700738 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
Vladimir Markof8655b32018-03-21 17:53:56 +0000739 }
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000740 JitCompileTask compile_task(method, JitCompileTask::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -0700741 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
742 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000743 compile_task.Run(thread);
744 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100745 return;
746 }
747
Andreas Gampe542451c2016-07-26 09:02:02 -0700748 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100749 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -0700750 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
751 // must remain the instrumentation entrypoint.
752 if ((profiling_info != nullptr) &&
753 (profiling_info->GetSavedEntryPoint() != nullptr) &&
754 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100755 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
756 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100757 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700758 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100759 }
760}
761
Mathieu Chartieref41db72016-10-25 15:08:01 -0700762void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100763 ArtMethod* caller,
764 uint32_t dex_pc,
765 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700766 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100767 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700768 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100769 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100770 info->AddInvokeInfo(dex_pc, this_object->GetClass());
771 }
772}
773
774void Jit::WaitForCompilationToFinish(Thread* self) {
775 if (thread_pool_ != nullptr) {
776 thread_pool_->Wait(self, false, false);
777 }
778}
779
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000780void Jit::Stop() {
781 Thread* self = Thread::Current();
782 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
783 WaitForCompilationToFinish(self);
784 GetThreadPool()->StopWorkers(self);
785 WaitForCompilationToFinish(self);
786}
787
788void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000789 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000790}
791
Andreas Gampef149b3f2016-11-16 14:58:24 -0800792ScopedJitSuspend::ScopedJitSuspend() {
793 jit::Jit* jit = Runtime::Current()->GetJit();
794 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
795 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000796 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800797 }
798}
799
800ScopedJitSuspend::~ScopedJitSuspend() {
801 if (was_on_) {
802 DCHECK(Runtime::Current()->GetJit() != nullptr);
803 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000804 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800805 }
806}
807
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800808} // namespace jit
809} // namespace art