blob: 2c0fbadc1d91725fb73c22ed2496ede89d51e9ad [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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070030#include "java_vm_ext.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "jit_code_cache.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 Geoffray274fe4a2016-04-12 16:33:24 +010049// At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
50static constexpr int kJitPoolThreadPthreadPriority = 9;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000051
Andreas Gampe7897cec2017-07-19 16:28:59 -070052// Different compilation threshold constants. These can be overridden on the command line.
53static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
54static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
55static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
56
Mathieu Chartier72918ea2016-03-24 11:07:06 -070057// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080058void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070059void* Jit::jit_compiler_handle_ = nullptr;
60void* (*Jit::jit_load_)(bool*) = nullptr;
61void (*Jit::jit_unload_)(void*) = nullptr;
62bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
63void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
64bool Jit::generate_debug_info_ = false;
65
Andreas Gampe7897cec2017-07-19 16:28:59 -070066struct StressModeHelper {
67 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
68};
69DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
70
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010073 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000074
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000075 jit_options->code_cache_initial_capacity_ =
76 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
77 jit_options->code_cache_max_capacity_ =
78 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070079 jit_options->dump_info_on_shutdown_ =
80 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010081 jit_options->profile_saver_options_ =
82 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
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
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700170Jit::Jit() : dump_info_on_shutdown_(false),
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000171 cumulative_timings_("JIT timings"),
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000172 memory_use_("Memory used for compilation", 16),
173 lock_("JIT memory use lock"),
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000174 use_jit_compilation_(true),
175 hot_method_threshold_(0),
176 warm_method_threshold_(0),
177 osr_method_threshold_(0),
178 priority_thread_weight_(0),
179 invoke_transition_weight_(0) {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180
181Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100182 DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800183 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700184 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700185 if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186 return nullptr;
187 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000188 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000189 options->GetCodeCacheInitialCapacity(),
190 options->GetCodeCacheMaxCapacity(),
191 jit->generate_debug_info_,
192 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800193 if (jit->GetCodeCache() == nullptr) {
194 return nullptr;
195 }
Calin Juravleffc87072016-04-20 14:22:09 +0100196 jit->use_jit_compilation_ = options->UseJitCompilation();
Calin Juravle138dbff2016-06-28 19:36:58 +0100197 jit->profile_saver_options_ = options->GetProfileSaverOptions();
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000198 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000199 << PrettySize(options->GetCodeCacheInitialCapacity())
200 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000201 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100202 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100203
204
205 jit->hot_method_threshold_ = options->GetCompileThreshold();
206 jit->warm_method_threshold_ = options->GetWarmupThreshold();
207 jit->osr_method_threshold_ = options->GetOsrThreshold();
Nicolas Geoffrayba6aae02016-04-14 14:17:29 +0100208 jit->priority_thread_weight_ = options->GetPriorityThreadWeight();
Calin Juravle155ff3d2016-04-27 14:14:58 +0100209 jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100210
211 jit->CreateThreadPool();
212
213 // Notify native debugger about the classes already loaded before the creation of the jit.
214 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800215 return jit.release();
216}
217
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700218bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219 jit_library_handle_ = dlopen(
220 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
221 if (jit_library_handle_ == nullptr) {
222 std::ostringstream oss;
223 oss << "JIT could not load libart-compiler.so: " << dlerror();
224 *error_msg = oss.str();
225 return false;
226 }
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000227 jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800228 if (jit_load_ == nullptr) {
229 dlclose(jit_library_handle_);
230 *error_msg = "JIT couldn't find jit_load entry point";
231 return false;
232 }
233 jit_unload_ = reinterpret_cast<void (*)(void*)>(
234 dlsym(jit_library_handle_, "jit_unload"));
235 if (jit_unload_ == nullptr) {
236 dlclose(jit_library_handle_);
237 *error_msg = "JIT couldn't find jit_unload entry point";
238 return false;
239 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000240 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800241 dlsym(jit_library_handle_, "jit_compile_method"));
242 if (jit_compile_method_ == nullptr) {
243 dlclose(jit_library_handle_);
244 *error_msg = "JIT couldn't find jit_compile_method entry point";
245 return false;
246 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000247 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
248 dlsym(jit_library_handle_, "jit_types_loaded"));
249 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000250 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000251 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000252 return false;
253 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700254 return true;
255}
256
257bool Jit::LoadCompiler(std::string* error_msg) {
258 if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) {
259 return false;
260 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000261 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800262 VLOG(jit) << "Calling JitLoad interpreter_only="
263 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000264 jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800265 if (jit_compiler_handle_ == nullptr) {
266 dlclose(jit_library_handle_);
267 *error_msg = "JIT couldn't load compiler";
268 return false;
269 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000270 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800271 return true;
272}
273
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000274bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100275 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800276 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000277
Alex Light0fa17862017-10-24 13:43:05 -0700278 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100279 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700280 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
281 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
282 << " due to not being safe to jit according to runtime-callbacks. For example, there"
283 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700284 return false;
285 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100286
287 // Don't compile the method if we are supposed to be deoptimized.
288 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
289 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700290 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100291 return false;
292 }
293
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000294 // If we get a request to compile a proxy method, we pass the actual Java method
295 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700296 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000297 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100298 return false;
299 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100300
301 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700302 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100303 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000304 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700305 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100306 if (!success) {
307 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700308 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100309 << " osr=" << std::boolalpha << osr;
310 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800311 if (kIsDebugBuild) {
312 if (self->IsExceptionPending()) {
313 mirror::Throwable* exception = self->GetException();
314 LOG(FATAL) << "No pending exception expected after compiling "
315 << ArtMethod::PrettyMethod(method)
316 << ": "
317 << exception->Dump();
318 }
319 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100320 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800321}
322
323void Jit::CreateThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100324 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
325 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000326
327 // We need peers as we may report the JIT thread, e.g., in the debugger.
328 constexpr bool kJitPoolNeedsPeers = true;
329 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
330
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100331 thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000332 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800333}
334
335void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100336 Thread* self = Thread::Current();
337 DCHECK(Runtime::Current()->IsShuttingDown(self));
338 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700339 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100340 {
341 ScopedSuspendAll ssa(__FUNCTION__);
342 // Clear thread_pool_ field while the threads are suspended.
343 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700344 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100345 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700346
347 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
348 if (!RUNNING_ON_MEMORY_TOOL) {
349 pool->StopWorkers(self);
350 pool->RemoveAllTasks(self);
351 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100352 // We could just suspend all threads, but we know those threads
353 // will finish in a short period, so it's not worth adding a suspend logic
354 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700355 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800356 }
357}
358
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000359void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800360 const std::vector<std::string>& code_paths) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100361 if (profile_saver_options_.IsEnabled()) {
362 ProfileSaver::Start(profile_saver_options_,
363 filename,
364 code_cache_.get(),
Calin Juravle77651c42017-03-03 18:04:02 -0800365 code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100366 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000367}
368
369void Jit::StopProfileSaver() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100370 if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) {
Calin Juravleb8e69992016-03-09 15:37:48 +0000371 ProfileSaver::Stop(dump_info_on_shutdown_);
Calin Juravle31f2c152015-10-23 17:56:15 +0100372 }
373}
374
Siva Chandra05d24152016-01-05 17:43:17 -0800375bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100376 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800377}
378
Nicolas Geoffray35122442016-03-02 12:05:30 +0000379bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
380 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
381}
382
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800383Jit::~Jit() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100384 DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700385 if (dump_info_on_shutdown_) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700386 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100387 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700388 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800389 DeleteThreadPool();
390 if (jit_compiler_handle_ != nullptr) {
391 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700392 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800393 }
394 if (jit_library_handle_ != nullptr) {
395 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700396 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800397 }
398}
399
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000400void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100401 if (!Runtime::Current()->UseJitCompilation()) {
402 // No need to notify if we only use the JIT to save profiles.
403 return;
404 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000405 jit::Jit* jit = Runtime::Current()->GetJit();
Calin Juravleffc87072016-04-20 14:22:09 +0100406 if (jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000407 DCHECK(jit->jit_types_loaded_ != nullptr);
408 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
409 }
410}
411
412void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
413 struct CollectClasses : public ClassVisitor {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700414 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
415 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000416 return true;
417 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800418 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000419 };
420
421 if (generate_debug_info_) {
422 ScopedObjectAccess so(Thread::Current());
423
424 CollectClasses visitor;
425 linker->VisitClasses(&visitor);
426 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000427 }
428}
429
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000430extern "C" void art_quick_osr_stub(void** stack,
431 uint32_t stack_size_in_bytes,
432 const uint8_t* native_pc,
433 JValue* result,
434 const char* shorty,
435 Thread* self);
436
437bool Jit::MaybeDoOnStackReplacement(Thread* thread,
438 ArtMethod* method,
439 uint32_t dex_pc,
440 int32_t dex_pc_offset,
441 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000442 if (!kEnableOnStackReplacement) {
443 return false;
444 }
445
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000446 Jit* jit = Runtime::Current()->GetJit();
447 if (jit == nullptr) {
448 return false;
449 }
450
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000451 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
452 // Don't attempt to do an OSR if we are close to the stack limit. Since
453 // the interpreter frames are still on stack, OSR has the potential
454 // to stack overflow even for a simple loop.
455 // b/27094810.
456 return false;
457 }
458
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000459 // Get the actual Java method if this method is from a proxy class. The compiler
460 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700461 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000462
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000463 // Cheap check if the method has been compiled already. That's an indicator that we should
464 // osr into it.
465 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
466 return false;
467 }
468
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000469 // Fetch some data before looking up for an OSR method. We don't want thread
470 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
471 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000472 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800473 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000474 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700475 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000476 void** memory = nullptr;
477 size_t frame_size = 0;
478 ShadowFrame* shadow_frame = nullptr;
479 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000480
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000481 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700482 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000483 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
484 if (osr_method == nullptr) {
485 // No osr method yet, just return to the interpreter.
486 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000487 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000488
489 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000490 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000491
492 // Find stack map starting at the target dex_pc.
493 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
494 if (!stack_map.IsValid()) {
495 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
496 // hope that the next branch has one.
497 return false;
498 }
499
Alex Light21611932017-09-26 13:07:39 -0700500 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
501 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
502 // disable OSR when single stepping, but that's currently hard to know at this point.
503 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700504 return false;
505 }
506
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000507 // We found a stack map, now fill the frame with dex register values from the interpreter's
508 // shadow frame.
509 DexRegisterMap vreg_map =
510 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
511
512 frame_size = osr_method->GetFrameSizeInBytes();
513
514 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
515 // stack.
516 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
517 // but that is engineering complexity not worth the effort for something like OSR.
518 memory = reinterpret_cast<void**>(malloc(frame_size));
519 CHECK(memory != nullptr);
520 memset(memory, 0, frame_size);
521
522 // Art ABI: ArtMethod is at the bottom of the stack.
523 memory[0] = method;
524
525 shadow_frame = thread->PopShadowFrame();
526 if (!vreg_map.IsValid()) {
527 // If we don't have a dex register map, then there are no live dex registers at
528 // this dex pc.
529 } else {
530 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
531 DexRegisterLocation::Kind location =
532 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
533 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000534 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000535 continue;
536 }
537
538 if (location == DexRegisterLocation::Kind::kConstant) {
539 // We skip constants because the compiled code knows how to handle them.
540 continue;
541 }
542
David Srbecky7dc11782016-02-25 13:23:56 +0000543 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000544
545 int32_t vreg_value = shadow_frame->GetVReg(vreg);
546 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
547 number_of_vregs,
548 code_info,
549 encoding);
550 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
551 DCHECK_GT(slot_offset, 0);
552 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
553 }
554 }
555
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800556 native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000557 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000558 VLOG(jit) << "Jumping to "
559 << method_name
560 << "@"
561 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000562 }
563
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000564 {
565 ManagedStack fragment;
566 thread->PushManagedStackFragment(&fragment);
567 (*art_quick_osr_stub)(memory,
568 frame_size,
569 native_pc,
570 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000571 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000572 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000573
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000574 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
575 thread->DeoptimizeWithDeoptimizationException(result);
576 }
577 thread->PopManagedStackFragment(fragment);
578 }
579 free(memory);
580 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000581 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000582 return true;
583}
584
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000585void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
586 if (bytes > 4 * MB) {
587 LOG(INFO) << "Compiler allocated "
588 << PrettySize(bytes)
589 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700590 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000591 }
592 MutexLock mu(Thread::Current(), lock_);
593 memory_use_.AddValue(bytes);
594}
595
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100596class JitCompileTask FINAL : public Task {
597 public:
598 enum TaskKind {
599 kAllocateProfile,
600 kCompile,
601 kCompileOsr
602 };
603
604 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
605 ScopedObjectAccess soa(Thread::Current());
606 // Add a global ref to the class to prevent class unloading until compilation is done.
607 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
608 CHECK(klass_ != nullptr);
609 }
610
611 ~JitCompileTask() {
612 ScopedObjectAccess soa(Thread::Current());
613 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
614 }
615
616 void Run(Thread* self) OVERRIDE {
617 ScopedObjectAccess soa(self);
618 if (kind_ == kCompile) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100619 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100620 } else if (kind_ == kCompileOsr) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100621 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100622 } else {
623 DCHECK(kind_ == kAllocateProfile);
624 if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700625 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100626 }
627 }
Calin Juravlea2638922016-04-29 16:44:11 +0100628 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100629 }
630
631 void Finalize() OVERRIDE {
632 delete this;
633 }
634
635 private:
636 ArtMethod* const method_;
637 const TaskKind kind_;
638 jobject klass_;
639
640 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
641};
642
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100643static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
644 if (method->IsClassInitializer() || !method->IsCompilable()) {
645 // We do not want to compile such methods.
646 return true;
647 }
648 if (method->IsNative()) {
649 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
650 if (klass == mirror::MethodHandle::StaticClass() || klass == mirror::VarHandle::StaticClass()) {
651 // MethodHandle and VarHandle invocation methods are required to throw an
652 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
653 // implementations that arise the exception. We need to disable JIT compilation of these JNI
654 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
655 // stubs. Since these stubs have different stack representations we can then crash in stack
656 // walking (b/78151261).
657 return true;
658 }
659 }
660 return false;
661}
662
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100663void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100664 if (thread_pool_ == nullptr) {
665 // Should only see this when shutting down.
666 DCHECK(Runtime::Current()->IsShuttingDown(self));
667 return;
668 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100669 if (IgnoreSamplesForMethod(method)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100670 return;
671 }
David Srbeckyf4886df2017-12-11 16:06:29 +0000672 if (hot_method_threshold_ == 0) {
673 // Tests might request JIT on first use (compiled synchronously in the interpreter).
674 return;
675 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100676 DCHECK(thread_pool_ != nullptr);
677 DCHECK_GT(warm_method_threshold_, 0);
678 DCHECK_GT(hot_method_threshold_, warm_method_threshold_);
679 DCHECK_GT(osr_method_threshold_, hot_method_threshold_);
680 DCHECK_GE(priority_thread_weight_, 1);
681 DCHECK_LE(priority_thread_weight_, hot_method_threshold_);
682
683 int32_t starting_count = method->GetCounter();
Vladimir Markoa710d912017-09-12 14:56:07 +0100684 if (Jit::ShouldUsePriorityThreadWeight(self)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100685 count *= priority_thread_weight_;
686 }
687 int32_t new_count = starting_count + count; // int32 here to avoid wrap-around;
Vladimir Marko2196c652017-11-30 16:16:07 +0000688 // Note: Native method have no "warm" state or profiling info.
689 if (LIKELY(!method->IsNative()) && starting_count < warm_method_threshold_) {
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000690 if ((new_count >= warm_method_threshold_) &&
691 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
692 bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false);
693 if (success) {
694 VLOG(jit) << "Start profiling " << method->PrettyMethod();
695 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100696
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000697 if (thread_pool_ == nullptr) {
698 // Calling ProfilingInfo::Create might put us in a suspended state, which could
699 // lead to the thread pool being deleted when we are shutting down.
700 DCHECK(Runtime::Current()->IsShuttingDown(self));
701 return;
702 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100703
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000704 if (!success) {
705 // We failed allocating. Instead of doing the collection on the Java thread, we push
706 // an allocation to a compiler thread, that will do the collection.
707 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
708 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100709 }
710 // Avoid jumping more than one state at a time.
711 new_count = std::min(new_count, hot_method_threshold_ - 1);
Calin Juravleffc87072016-04-20 14:22:09 +0100712 } else if (use_jit_compilation_) {
713 if (starting_count < hot_method_threshold_) {
714 if ((new_count >= hot_method_threshold_) &&
715 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
716 DCHECK(thread_pool_ != nullptr);
717 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
718 }
719 // Avoid jumping more than one state at a time.
720 new_count = std::min(new_count, osr_method_threshold_ - 1);
721 } else if (starting_count < osr_method_threshold_) {
722 if (!with_backedges) {
723 // If the samples don't contain any back edge, we don't increment the hotness.
724 return;
725 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000726 DCHECK(!method->IsNative()); // No back edges reported for native methods.
Calin Juravleffc87072016-04-20 14:22:09 +0100727 if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) {
728 DCHECK(thread_pool_ != nullptr);
729 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
730 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100731 }
732 }
733 // Update hotness counter
734 method->SetCounter(new_count);
735}
736
737void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100738 Runtime* runtime = Runtime::Current();
739 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000740 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000741 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000742 if (!np_method->IsNative()) {
743 // The compiler requires a ProfilingInfo object for non-native methods.
744 ProfilingInfo::Create(thread, np_method, /* retry_allocation */ true);
745 }
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000746 JitCompileTask compile_task(method, JitCompileTask::kCompile);
747 compile_task.Run(thread);
748 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100749 return;
750 }
751
Andreas Gampe542451c2016-07-26 09:02:02 -0700752 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100753 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
754 // instead of interpreting the method.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100755 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
756 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
757 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100758 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100759 AddSamples(thread, method, 1, /* with_backedges */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100760 }
761}
762
Mathieu Chartieref41db72016-10-25 15:08:01 -0700763void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100764 ArtMethod* caller,
765 uint32_t dex_pc,
766 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700767 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100768 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700769 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100770 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100771 info->AddInvokeInfo(dex_pc, this_object->GetClass());
772 }
773}
774
775void Jit::WaitForCompilationToFinish(Thread* self) {
776 if (thread_pool_ != nullptr) {
777 thread_pool_->Wait(self, false, false);
778 }
779}
780
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000781void Jit::Stop() {
782 Thread* self = Thread::Current();
783 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
784 WaitForCompilationToFinish(self);
785 GetThreadPool()->StopWorkers(self);
786 WaitForCompilationToFinish(self);
787}
788
789void Jit::Start() {
790 GetThreadPool()->StartWorkers(Thread::Current());
791}
792
Andreas Gampef149b3f2016-11-16 14:58:24 -0800793ScopedJitSuspend::ScopedJitSuspend() {
794 jit::Jit* jit = Runtime::Current()->GetJit();
795 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
796 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000797 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800798 }
799}
800
801ScopedJitSuspend::~ScopedJitSuspend() {
802 if (was_on_) {
803 DCHECK(Runtime::Current()->GetJit() != nullptr);
804 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000805 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800806 }
807}
808
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800809} // namespace jit
810} // namespace art