blob: 5d4b9e8cc960e6a8aacaf3da8e1540e3adc6e69e [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"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070027#include "debugger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028#include "entrypoints/runtime_asm_entrypoints.h"
29#include "interpreter/interpreter.h"
30#include "jit_code_cache.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010031#include "jni/java_vm_ext.h"
Orion Hodson52f5a1f2018-05-02 11:05:44 +010032#include "mirror/method_handle_impl.h"
33#include "mirror/var_handle.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010034#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000035#include "oat_quick_method_header.h"
David Sehr82d046e2018-04-23 08:14:19 -070036#include "profile/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000037#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038#include "runtime.h"
39#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070040#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000041#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070042#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010043#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044
45namespace art {
46namespace jit {
47
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000048static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000049
Andreas Gampe7897cec2017-07-19 16:28:59 -070050// Different compilation threshold constants. These can be overridden on the command line.
51static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
52static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
53static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
54
Mathieu Chartier72918ea2016-03-24 11:07:06 -070055// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080056void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070057void* Jit::jit_compiler_handle_ = nullptr;
58void* (*Jit::jit_load_)(bool*) = nullptr;
59void (*Jit::jit_unload_)(void*) = nullptr;
60bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
61void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
62bool Jit::generate_debug_info_ = false;
63
Andreas Gampe7897cec2017-07-19 16:28:59 -070064struct StressModeHelper {
65 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
66};
67DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
68
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010071 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000072
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000073 jit_options->code_cache_initial_capacity_ =
74 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
75 jit_options->code_cache_max_capacity_ =
76 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070077 jit_options->dump_info_on_shutdown_ =
78 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010079 jit_options->profile_saver_options_ =
80 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010081 jit_options->thread_pool_pthread_priority_ =
82 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000083
Andreas Gampe7897cec2017-07-19 16:28:59 -070084 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
85 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
86 } else {
87 jit_options->compile_threshold_ =
88 kIsDebugBuild
89 ? (StressModeHelper::kSlowMode
90 ? kJitSlowStressDefaultCompileThreshold
91 : kJitStressDefaultCompileThreshold)
92 : kJitDefaultCompileThreshold;
93 }
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000094 if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
95 LOG(FATAL) << "Method compilation threshold is above its internal limit.";
96 }
97
98 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
99 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
100 if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
101 LOG(FATAL) << "Method warmup threshold is above its internal limit.";
102 }
103 } else {
104 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
105 }
106
107 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
108 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
109 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
110 LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
111 }
112 } else {
113 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
114 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
115 jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
116 }
117 }
118
Calin Juravleb2771b42016-04-07 17:09:25 +0100119 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
120 jit_options->priority_thread_weight_ =
121 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
122 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
123 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
124 } else if (jit_options->priority_thread_weight_ == 0) {
125 LOG(FATAL) << "Priority thread weight cannot be 0.";
126 }
127 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100128 jit_options->priority_thread_weight_ = std::max(
129 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
130 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100131 }
132
Calin Juravle155ff3d2016-04-27 14:14:58 +0100133 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100134 jit_options->invoke_transition_weight_ =
135 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100136 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
137 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
138 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100139 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100140 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100141 } else {
142 jit_options->invoke_transition_weight_ = std::max(
143 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800144 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100145 }
146
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800147 return jit_options;
148}
149
Vladimir Markoa710d912017-09-12 14:56:07 +0100150bool Jit::ShouldUsePriorityThreadWeight(Thread* self) {
151 return self->IsJitSensitiveThread() && Runtime::Current()->InJankPerceptibleProcessState();
Calin Juravleb2771b42016-04-07 17:09:25 +0100152}
153
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700154void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000155 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000157 MutexLock mu(Thread::Current(), lock_);
158 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700159}
160
Calin Juravleb8e69992016-03-09 15:37:48 +0000161void Jit::DumpForSigQuit(std::ostream& os) {
162 DumpInfo(os);
163 ProfileSaver::DumpInstanceInfo(os);
164}
165
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700166void Jit::AddTimingLogger(const TimingLogger& logger) {
167 cumulative_timings_.AddLogger(logger);
168}
169
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100170Jit::Jit(JitOptions* options) : options_(options),
171 cumulative_timings_("JIT timings"),
172 memory_use_("Memory used for compilation", 16),
173 lock_("JIT memory use lock") {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800174
175Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100176 DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled());
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100177 std::unique_ptr<Jit> jit(new Jit(options));
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700178 if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179 return nullptr;
180 }
Calin Juravle016fcbe22018-05-03 19:47:35 -0700181 bool code_cache_only_for_profile_data = !options->UseJitCompilation();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000182 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000183 options->GetCodeCacheInitialCapacity(),
184 options->GetCodeCacheMaxCapacity(),
185 jit->generate_debug_info_,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700186 code_cache_only_for_profile_data,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000187 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800188 if (jit->GetCodeCache() == nullptr) {
189 return nullptr;
190 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000191 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000192 << PrettySize(options->GetCodeCacheInitialCapacity())
193 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000194 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100195 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100196
197
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100198 jit->CreateThreadPool();
199
200 // Notify native debugger about the classes already loaded before the creation of the jit.
201 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800202 return jit.release();
203}
204
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700205bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800206 jit_library_handle_ = dlopen(
207 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
208 if (jit_library_handle_ == nullptr) {
209 std::ostringstream oss;
210 oss << "JIT could not load libart-compiler.so: " << dlerror();
211 *error_msg = oss.str();
212 return false;
213 }
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000214 jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800215 if (jit_load_ == nullptr) {
216 dlclose(jit_library_handle_);
217 *error_msg = "JIT couldn't find jit_load entry point";
218 return false;
219 }
220 jit_unload_ = reinterpret_cast<void (*)(void*)>(
221 dlsym(jit_library_handle_, "jit_unload"));
222 if (jit_unload_ == nullptr) {
223 dlclose(jit_library_handle_);
224 *error_msg = "JIT couldn't find jit_unload entry point";
225 return false;
226 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000227 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800228 dlsym(jit_library_handle_, "jit_compile_method"));
229 if (jit_compile_method_ == nullptr) {
230 dlclose(jit_library_handle_);
231 *error_msg = "JIT couldn't find jit_compile_method entry point";
232 return false;
233 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000234 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
235 dlsym(jit_library_handle_, "jit_types_loaded"));
236 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000237 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000238 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000239 return false;
240 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700241 return true;
242}
243
244bool Jit::LoadCompiler(std::string* error_msg) {
245 if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) {
246 return false;
247 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000248 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800249 VLOG(jit) << "Calling JitLoad interpreter_only="
250 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000251 jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800252 if (jit_compiler_handle_ == nullptr) {
253 dlclose(jit_library_handle_);
254 *error_msg = "JIT couldn't load compiler";
255 return false;
256 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000257 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800258 return true;
259}
260
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000261bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100262 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800263 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000264
Alex Light0fa17862017-10-24 13:43:05 -0700265 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100266 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700267 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
268 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
269 << " due to not being safe to jit according to runtime-callbacks. For example, there"
270 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700271 return false;
272 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100273
274 // Don't compile the method if we are supposed to be deoptimized.
275 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
276 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700277 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100278 return false;
279 }
280
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000281 // If we get a request to compile a proxy method, we pass the actual Java method
282 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700283 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000284 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100285 return false;
286 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100287
288 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700289 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100290 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000291 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700292 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100293 if (!success) {
294 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700295 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100296 << " osr=" << std::boolalpha << osr;
297 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800298 if (kIsDebugBuild) {
299 if (self->IsExceptionPending()) {
300 mirror::Throwable* exception = self->GetException();
301 LOG(FATAL) << "No pending exception expected after compiling "
302 << ArtMethod::PrettyMethod(method)
303 << ": "
304 << exception->Dump();
305 }
306 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100307 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800308}
309
310void Jit::CreateThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100311 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
312 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000313
314 // We need peers as we may report the JIT thread, e.g., in the debugger.
315 constexpr bool kJitPoolNeedsPeers = true;
316 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
317
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100318 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000319 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800320}
321
322void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100323 Thread* self = Thread::Current();
324 DCHECK(Runtime::Current()->IsShuttingDown(self));
325 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700326 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100327 {
328 ScopedSuspendAll ssa(__FUNCTION__);
329 // Clear thread_pool_ field while the threads are suspended.
330 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700331 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100332 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700333
334 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Andreas Gampe8b362a82018-05-22 20:54:14 +0000335 if (!RUNNING_ON_MEMORY_TOOL) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700336 pool->StopWorkers(self);
337 pool->RemoveAllTasks(self);
338 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100339 // We could just suspend all threads, but we know those threads
340 // will finish in a short period, so it's not worth adding a suspend logic
341 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700342 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800343 }
344}
345
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000346void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800347 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100348 if (options_->GetSaveProfilingInfo()) {
349 ProfileSaver::Start(options_->GetProfileSaverOptions(),
Calin Juravle138dbff2016-06-28 19:36:58 +0100350 filename,
351 code_cache_.get(),
Calin Juravle77651c42017-03-03 18:04:02 -0800352 code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100353 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000354}
355
356void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100357 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
358 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100359 }
360}
361
Siva Chandra05d24152016-01-05 17:43:17 -0800362bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100363 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800364}
365
Nicolas Geoffray35122442016-03-02 12:05:30 +0000366bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
367 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
368}
369
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800370Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100371 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
372 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700373 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100374 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700375 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800376 DeleteThreadPool();
377 if (jit_compiler_handle_ != nullptr) {
378 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700379 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800380 }
381 if (jit_library_handle_ != nullptr) {
382 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700383 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800384 }
385}
386
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000387void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100388 if (!Runtime::Current()->UseJitCompilation()) {
389 // No need to notify if we only use the JIT to save profiles.
390 return;
391 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000392 jit::Jit* jit = Runtime::Current()->GetJit();
Calin Juravleffc87072016-04-20 14:22:09 +0100393 if (jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000394 DCHECK(jit->jit_types_loaded_ != nullptr);
395 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
396 }
397}
398
399void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
400 struct CollectClasses : public ClassVisitor {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700401 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
402 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000403 return true;
404 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800405 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000406 };
407
408 if (generate_debug_info_) {
409 ScopedObjectAccess so(Thread::Current());
410
411 CollectClasses visitor;
412 linker->VisitClasses(&visitor);
413 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000414 }
415}
416
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000417extern "C" void art_quick_osr_stub(void** stack,
418 uint32_t stack_size_in_bytes,
419 const uint8_t* native_pc,
420 JValue* result,
421 const char* shorty,
422 Thread* self);
423
424bool Jit::MaybeDoOnStackReplacement(Thread* thread,
425 ArtMethod* method,
426 uint32_t dex_pc,
427 int32_t dex_pc_offset,
428 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000429 if (!kEnableOnStackReplacement) {
430 return false;
431 }
432
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000433 Jit* jit = Runtime::Current()->GetJit();
434 if (jit == nullptr) {
435 return false;
436 }
437
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000438 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
439 // Don't attempt to do an OSR if we are close to the stack limit. Since
440 // the interpreter frames are still on stack, OSR has the potential
441 // to stack overflow even for a simple loop.
442 // b/27094810.
443 return false;
444 }
445
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000446 // Get the actual Java method if this method is from a proxy class. The compiler
447 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700448 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000449
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000450 // Cheap check if the method has been compiled already. That's an indicator that we should
451 // osr into it.
452 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
453 return false;
454 }
455
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000456 // Fetch some data before looking up for an OSR method. We don't want thread
457 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
458 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000459 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800460 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000461 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700462 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000463 void** memory = nullptr;
464 size_t frame_size = 0;
465 ShadowFrame* shadow_frame = nullptr;
466 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000467
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000468 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700469 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000470 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
471 if (osr_method == nullptr) {
472 // No osr method yet, just return to the interpreter.
473 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000474 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000475
David Srbecky052f8ca2018-04-26 15:42:54 +0100476 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000477
478 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100479 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000480 if (!stack_map.IsValid()) {
481 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
482 // hope that the next branch has one.
483 return false;
484 }
485
Alex Light21611932017-09-26 13:07:39 -0700486 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
487 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
488 // disable OSR when single stepping, but that's currently hard to know at this point.
489 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700490 return false;
491 }
492
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000493 // We found a stack map, now fill the frame with dex register values from the interpreter's
494 // shadow frame.
495 DexRegisterMap vreg_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100496 code_info.GetDexRegisterMapOf(stack_map, number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000497
498 frame_size = osr_method->GetFrameSizeInBytes();
499
500 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
501 // stack.
502 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
503 // but that is engineering complexity not worth the effort for something like OSR.
504 memory = reinterpret_cast<void**>(malloc(frame_size));
505 CHECK(memory != nullptr);
506 memset(memory, 0, frame_size);
507
508 // Art ABI: ArtMethod is at the bottom of the stack.
509 memory[0] = method;
510
511 shadow_frame = thread->PopShadowFrame();
512 if (!vreg_map.IsValid()) {
513 // If we don't have a dex register map, then there are no live dex registers at
514 // this dex pc.
515 } else {
516 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
517 DexRegisterLocation::Kind location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100518 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000519 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000520 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000521 continue;
522 }
523
524 if (location == DexRegisterLocation::Kind::kConstant) {
525 // We skip constants because the compiled code knows how to handle them.
526 continue;
527 }
528
David Srbecky7dc11782016-02-25 13:23:56 +0000529 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000530
531 int32_t vreg_value = shadow_frame->GetVReg(vreg);
532 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
533 number_of_vregs,
David Srbecky052f8ca2018-04-26 15:42:54 +0100534 code_info);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000535 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
536 DCHECK_GT(slot_offset, 0);
537 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
538 }
539 }
540
David Srbecky052f8ca2018-04-26 15:42:54 +0100541 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000542 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000543 VLOG(jit) << "Jumping to "
544 << method_name
545 << "@"
546 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000547 }
548
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000549 {
550 ManagedStack fragment;
551 thread->PushManagedStackFragment(&fragment);
552 (*art_quick_osr_stub)(memory,
553 frame_size,
554 native_pc,
555 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000556 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000557 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000558
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000559 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
560 thread->DeoptimizeWithDeoptimizationException(result);
561 }
562 thread->PopManagedStackFragment(fragment);
563 }
564 free(memory);
565 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000566 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000567 return true;
568}
569
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000570void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
571 if (bytes > 4 * MB) {
572 LOG(INFO) << "Compiler allocated "
573 << PrettySize(bytes)
574 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700575 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000576 }
577 MutexLock mu(Thread::Current(), lock_);
578 memory_use_.AddValue(bytes);
579}
580
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100581class JitCompileTask FINAL : public Task {
582 public:
583 enum TaskKind {
584 kAllocateProfile,
585 kCompile,
586 kCompileOsr
587 };
588
589 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
590 ScopedObjectAccess soa(Thread::Current());
591 // Add a global ref to the class to prevent class unloading until compilation is done.
592 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
593 CHECK(klass_ != nullptr);
594 }
595
596 ~JitCompileTask() {
597 ScopedObjectAccess soa(Thread::Current());
598 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
599 }
600
601 void Run(Thread* self) OVERRIDE {
602 ScopedObjectAccess soa(self);
603 if (kind_ == kCompile) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100604 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100605 } else if (kind_ == kCompileOsr) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100606 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100607 } else {
608 DCHECK(kind_ == kAllocateProfile);
609 if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700610 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100611 }
612 }
Calin Juravlea2638922016-04-29 16:44:11 +0100613 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100614 }
615
616 void Finalize() OVERRIDE {
617 delete this;
618 }
619
620 private:
621 ArtMethod* const method_;
622 const TaskKind kind_;
623 jobject klass_;
624
625 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
626};
627
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100628static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
629 if (method->IsClassInitializer() || !method->IsCompilable()) {
630 // We do not want to compile such methods.
631 return true;
632 }
633 if (method->IsNative()) {
634 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
635 if (klass == mirror::MethodHandle::StaticClass() || klass == mirror::VarHandle::StaticClass()) {
636 // MethodHandle and VarHandle invocation methods are required to throw an
637 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
638 // implementations that arise the exception. We need to disable JIT compilation of these JNI
639 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
640 // stubs. Since these stubs have different stack representations we can then crash in stack
641 // walking (b/78151261).
642 return true;
643 }
644 }
645 return false;
646}
647
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100648void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100649 if (thread_pool_ == nullptr) {
650 // Should only see this when shutting down.
651 DCHECK(Runtime::Current()->IsShuttingDown(self));
652 return;
653 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100654 if (IgnoreSamplesForMethod(method)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100655 return;
656 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100657 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000658 // Tests might request JIT on first use (compiled synchronously in the interpreter).
659 return;
660 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100661 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100662 DCHECK_GT(WarmMethodThreshold(), 0);
663 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
664 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
665 DCHECK_GE(PriorityThreadWeight(), 1);
666 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100667
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100668 uint16_t starting_count = method->GetCounter();
Vladimir Markoa710d912017-09-12 14:56:07 +0100669 if (Jit::ShouldUsePriorityThreadWeight(self)) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100670 count *= PriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100671 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100672 uint32_t new_count = starting_count + count;
Vladimir Marko2196c652017-11-30 16:16:07 +0000673 // Note: Native method have no "warm" state or profiling info.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100674 if (LIKELY(!method->IsNative()) && starting_count < WarmMethodThreshold()) {
675 if ((new_count >= WarmMethodThreshold()) &&
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000676 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
677 bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false);
678 if (success) {
679 VLOG(jit) << "Start profiling " << method->PrettyMethod();
680 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100681
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000682 if (thread_pool_ == nullptr) {
683 // Calling ProfilingInfo::Create might put us in a suspended state, which could
684 // lead to the thread pool being deleted when we are shutting down.
685 DCHECK(Runtime::Current()->IsShuttingDown(self));
686 return;
687 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100688
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000689 if (!success) {
690 // We failed allocating. Instead of doing the collection on the Java thread, we push
691 // an allocation to a compiler thread, that will do the collection.
692 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
693 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100694 }
695 // Avoid jumping more than one state at a time.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100696 new_count = std::min(new_count, static_cast<uint32_t>(HotMethodThreshold() - 1));
697 } else if (UseJitCompilation()) {
698 if (starting_count < HotMethodThreshold()) {
699 if ((new_count >= HotMethodThreshold()) &&
Calin Juravleffc87072016-04-20 14:22:09 +0100700 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
701 DCHECK(thread_pool_ != nullptr);
702 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
703 }
704 // Avoid jumping more than one state at a time.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100705 new_count = std::min(new_count, static_cast<uint32_t>(OSRMethodThreshold() - 1));
706 } else if (starting_count < OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100707 if (!with_backedges) {
708 // If the samples don't contain any back edge, we don't increment the hotness.
709 return;
710 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000711 DCHECK(!method->IsNative()); // No back edges reported for native methods.
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100712 if ((new_count >= OSRMethodThreshold()) && !code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100713 DCHECK(thread_pool_ != nullptr);
714 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
715 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100716 }
717 }
718 // Update hotness counter
719 method->SetCounter(new_count);
720}
721
722void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100723 Runtime* runtime = Runtime::Current();
724 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000725 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000726 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000727 if (!np_method->IsNative()) {
728 // The compiler requires a ProfilingInfo object for non-native methods.
729 ProfilingInfo::Create(thread, np_method, /* retry_allocation */ true);
730 }
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000731 JitCompileTask compile_task(method, JitCompileTask::kCompile);
732 compile_task.Run(thread);
733 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100734 return;
735 }
736
Andreas Gampe542451c2016-07-26 09:02:02 -0700737 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100738 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
739 // instead of interpreting the method.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100740 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
741 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
742 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100743 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100744 AddSamples(thread, method, 1, /* with_backedges */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100745 }
746}
747
Mathieu Chartieref41db72016-10-25 15:08:01 -0700748void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100749 ArtMethod* caller,
750 uint32_t dex_pc,
751 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700752 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100753 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700754 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100755 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100756 info->AddInvokeInfo(dex_pc, this_object->GetClass());
757 }
758}
759
760void Jit::WaitForCompilationToFinish(Thread* self) {
761 if (thread_pool_ != nullptr) {
762 thread_pool_->Wait(self, false, false);
763 }
764}
765
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000766void Jit::Stop() {
767 Thread* self = Thread::Current();
768 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
769 WaitForCompilationToFinish(self);
770 GetThreadPool()->StopWorkers(self);
771 WaitForCompilationToFinish(self);
772}
773
774void Jit::Start() {
775 GetThreadPool()->StartWorkers(Thread::Current());
776}
777
Andreas Gampef149b3f2016-11-16 14:58:24 -0800778ScopedJitSuspend::ScopedJitSuspend() {
779 jit::Jit* jit = Runtime::Current()->GetJit();
780 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
781 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000782 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800783 }
784}
785
786ScopedJitSuspend::~ScopedJitSuspend() {
787 if (was_on_) {
788 DCHECK(Runtime::Current()->GetJit() != nullptr);
789 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000790 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800791 }
792}
793
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800794} // namespace jit
795} // namespace art