blob: 8c27bfe85e2bb378914df2def80846c18623e8b3 [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 Gampe7897cec2017-07-19 16:28:59 -070023#include "base/logging.h"
Andreas Gampe0897e1c2017-05-16 08:36:56 -070024#include "base/memory_tool.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070025#include "debugger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080026#include "entrypoints/runtime_asm_entrypoints.h"
27#include "interpreter/interpreter.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070028#include "java_vm_ext.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "jit_code_cache.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010030#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000031#include "oat_quick_method_header.h"
Calin Juravle33083d62017-01-18 15:29:12 -080032#include "profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000033#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "runtime.h"
35#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070036#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000037#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070038#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010039#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040#include "utils.h"
41
42namespace art {
43namespace jit {
44
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000045static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010046// At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
47static constexpr int kJitPoolThreadPthreadPriority = 9;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000048
Andreas Gampe7897cec2017-07-19 16:28:59 -070049// Different compilation threshold constants. These can be overridden on the command line.
50static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
51static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
52static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
53
Mathieu Chartier72918ea2016-03-24 11:07:06 -070054// JIT compiler
55void* Jit::jit_library_handle_= nullptr;
56void* Jit::jit_compiler_handle_ = nullptr;
57void* (*Jit::jit_load_)(bool*) = nullptr;
58void (*Jit::jit_unload_)(void*) = nullptr;
59bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
60void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
61bool Jit::generate_debug_info_ = false;
62
Andreas Gampe7897cec2017-07-19 16:28:59 -070063struct StressModeHelper {
64 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
65};
66DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
67
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080068JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010070 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000071
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000072 jit_options->code_cache_initial_capacity_ =
73 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
74 jit_options->code_cache_max_capacity_ =
75 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070076 jit_options->dump_info_on_shutdown_ =
77 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010078 jit_options->profile_saver_options_ =
79 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000080
Andreas Gampe7897cec2017-07-19 16:28:59 -070081 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
82 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
83 } else {
84 jit_options->compile_threshold_ =
85 kIsDebugBuild
86 ? (StressModeHelper::kSlowMode
87 ? kJitSlowStressDefaultCompileThreshold
88 : kJitStressDefaultCompileThreshold)
89 : kJitDefaultCompileThreshold;
90 }
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000091 if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
92 LOG(FATAL) << "Method compilation threshold is above its internal limit.";
93 }
94
95 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
96 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
97 if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
98 LOG(FATAL) << "Method warmup threshold is above its internal limit.";
99 }
100 } else {
101 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
102 }
103
104 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
105 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
106 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
107 LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
108 }
109 } else {
110 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
111 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
112 jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
113 }
114 }
115
Calin Juravleb2771b42016-04-07 17:09:25 +0100116 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
117 jit_options->priority_thread_weight_ =
118 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
119 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
120 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
121 } else if (jit_options->priority_thread_weight_ == 0) {
122 LOG(FATAL) << "Priority thread weight cannot be 0.";
123 }
124 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100125 jit_options->priority_thread_weight_ = std::max(
126 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
127 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100128 }
129
Calin Juravle155ff3d2016-04-27 14:14:58 +0100130 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100131 jit_options->invoke_transition_weight_ =
132 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100133 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
134 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
135 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100136 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100137 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100138 } else {
139 jit_options->invoke_transition_weight_ = std::max(
140 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800141 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100142 }
143
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144 return jit_options;
145}
146
Vladimir Markoa710d912017-09-12 14:56:07 +0100147bool Jit::ShouldUsePriorityThreadWeight(Thread* self) {
148 return self->IsJitSensitiveThread() && Runtime::Current()->InJankPerceptibleProcessState();
Calin Juravleb2771b42016-04-07 17:09:25 +0100149}
150
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700151void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000152 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700153 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000154 MutexLock mu(Thread::Current(), lock_);
155 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156}
157
Calin Juravleb8e69992016-03-09 15:37:48 +0000158void Jit::DumpForSigQuit(std::ostream& os) {
159 DumpInfo(os);
160 ProfileSaver::DumpInstanceInfo(os);
161}
162
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700163void Jit::AddTimingLogger(const TimingLogger& logger) {
164 cumulative_timings_.AddLogger(logger);
165}
166
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700167Jit::Jit() : dump_info_on_shutdown_(false),
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000168 cumulative_timings_("JIT timings"),
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000169 memory_use_("Memory used for compilation", 16),
170 lock_("JIT memory use lock"),
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000171 use_jit_compilation_(true),
172 hot_method_threshold_(0),
173 warm_method_threshold_(0),
174 osr_method_threshold_(0),
175 priority_thread_weight_(0),
176 invoke_transition_weight_(0) {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177
178Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100179 DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700181 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700182 if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800183 return nullptr;
184 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000185 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000186 options->GetCodeCacheInitialCapacity(),
187 options->GetCodeCacheMaxCapacity(),
188 jit->generate_debug_info_,
189 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800190 if (jit->GetCodeCache() == nullptr) {
191 return nullptr;
192 }
Calin Juravleffc87072016-04-20 14:22:09 +0100193 jit->use_jit_compilation_ = options->UseJitCompilation();
Calin Juravle138dbff2016-06-28 19:36:58 +0100194 jit->profile_saver_options_ = options->GetProfileSaverOptions();
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000195 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000196 << PrettySize(options->GetCodeCacheInitialCapacity())
197 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000198 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100199 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100200
201
202 jit->hot_method_threshold_ = options->GetCompileThreshold();
203 jit->warm_method_threshold_ = options->GetWarmupThreshold();
204 jit->osr_method_threshold_ = options->GetOsrThreshold();
Nicolas Geoffrayba6aae02016-04-14 14:17:29 +0100205 jit->priority_thread_weight_ = options->GetPriorityThreadWeight();
Calin Juravle155ff3d2016-04-27 14:14:58 +0100206 jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100207
208 jit->CreateThreadPool();
209
210 // Notify native debugger about the classes already loaded before the creation of the jit.
211 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800212 return jit.release();
213}
214
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700215bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216 jit_library_handle_ = dlopen(
217 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
218 if (jit_library_handle_ == nullptr) {
219 std::ostringstream oss;
220 oss << "JIT could not load libart-compiler.so: " << dlerror();
221 *error_msg = oss.str();
222 return false;
223 }
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000224 jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800225 if (jit_load_ == nullptr) {
226 dlclose(jit_library_handle_);
227 *error_msg = "JIT couldn't find jit_load entry point";
228 return false;
229 }
230 jit_unload_ = reinterpret_cast<void (*)(void*)>(
231 dlsym(jit_library_handle_, "jit_unload"));
232 if (jit_unload_ == nullptr) {
233 dlclose(jit_library_handle_);
234 *error_msg = "JIT couldn't find jit_unload entry point";
235 return false;
236 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000237 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800238 dlsym(jit_library_handle_, "jit_compile_method"));
239 if (jit_compile_method_ == nullptr) {
240 dlclose(jit_library_handle_);
241 *error_msg = "JIT couldn't find jit_compile_method entry point";
242 return false;
243 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000244 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
245 dlsym(jit_library_handle_, "jit_types_loaded"));
246 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000247 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000248 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000249 return false;
250 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700251 return true;
252}
253
254bool Jit::LoadCompiler(std::string* error_msg) {
255 if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) {
256 return false;
257 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000258 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800259 VLOG(jit) << "Calling JitLoad interpreter_only="
260 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000261 jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800262 if (jit_compiler_handle_ == nullptr) {
263 dlclose(jit_library_handle_);
264 *error_msg = "JIT couldn't load compiler";
265 return false;
266 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000267 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800268 return true;
269}
270
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000271bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100272 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800273 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000274
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100275 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700276 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700277 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to breakpoint";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700278 return false;
279 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100280
281 // Don't compile the method if we are supposed to be deoptimized.
282 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
283 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700284 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100285 return false;
286 }
287
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000288 // If we get a request to compile a proxy method, we pass the actual Java method
289 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700290 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000291 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100292 return false;
293 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100294
295 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700296 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100297 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000298 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700299 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100300 if (!success) {
301 VLOG(jit) << "Failed to compile 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;
304 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800305 if (kIsDebugBuild) {
306 if (self->IsExceptionPending()) {
307 mirror::Throwable* exception = self->GetException();
308 LOG(FATAL) << "No pending exception expected after compiling "
309 << ArtMethod::PrettyMethod(method)
310 << ": "
311 << exception->Dump();
312 }
313 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100314 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800315}
316
317void Jit::CreateThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100318 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
319 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000320
321 // We need peers as we may report the JIT thread, e.g., in the debugger.
322 constexpr bool kJitPoolNeedsPeers = true;
323 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
324
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100325 thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000326 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800327}
328
329void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100330 Thread* self = Thread::Current();
331 DCHECK(Runtime::Current()->IsShuttingDown(self));
332 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700333 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100334 {
335 ScopedSuspendAll ssa(__FUNCTION__);
336 // Clear thread_pool_ field while the threads are suspended.
337 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700338 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100339 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700340
341 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
342 if (!RUNNING_ON_MEMORY_TOOL) {
343 pool->StopWorkers(self);
344 pool->RemoveAllTasks(self);
345 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100346 // We could just suspend all threads, but we know those threads
347 // will finish in a short period, so it's not worth adding a suspend logic
348 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700349 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800350 }
351}
352
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000353void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800354 const std::vector<std::string>& code_paths) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100355 if (profile_saver_options_.IsEnabled()) {
356 ProfileSaver::Start(profile_saver_options_,
357 filename,
358 code_cache_.get(),
Calin Juravle77651c42017-03-03 18:04:02 -0800359 code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100360 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000361}
362
363void Jit::StopProfileSaver() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100364 if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) {
Calin Juravleb8e69992016-03-09 15:37:48 +0000365 ProfileSaver::Stop(dump_info_on_shutdown_);
Calin Juravle31f2c152015-10-23 17:56:15 +0100366 }
367}
368
Siva Chandra05d24152016-01-05 17:43:17 -0800369bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100370 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800371}
372
Nicolas Geoffray35122442016-03-02 12:05:30 +0000373bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
374 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
375}
376
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800377Jit::~Jit() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100378 DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700379 if (dump_info_on_shutdown_) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700380 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100381 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700382 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800383 DeleteThreadPool();
384 if (jit_compiler_handle_ != nullptr) {
385 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700386 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800387 }
388 if (jit_library_handle_ != nullptr) {
389 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700390 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800391 }
392}
393
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000394void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100395 if (!Runtime::Current()->UseJitCompilation()) {
396 // No need to notify if we only use the JIT to save profiles.
397 return;
398 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000399 jit::Jit* jit = Runtime::Current()->GetJit();
Calin Juravleffc87072016-04-20 14:22:09 +0100400 if (jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000401 DCHECK(jit->jit_types_loaded_ != nullptr);
402 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
403 }
404}
405
406void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
407 struct CollectClasses : public ClassVisitor {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700408 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
409 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000410 return true;
411 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800412 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000413 };
414
415 if (generate_debug_info_) {
416 ScopedObjectAccess so(Thread::Current());
417
418 CollectClasses visitor;
419 linker->VisitClasses(&visitor);
420 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000421 }
422}
423
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000424extern "C" void art_quick_osr_stub(void** stack,
425 uint32_t stack_size_in_bytes,
426 const uint8_t* native_pc,
427 JValue* result,
428 const char* shorty,
429 Thread* self);
430
431bool Jit::MaybeDoOnStackReplacement(Thread* thread,
432 ArtMethod* method,
433 uint32_t dex_pc,
434 int32_t dex_pc_offset,
435 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000436 if (!kEnableOnStackReplacement) {
437 return false;
438 }
439
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000440 Jit* jit = Runtime::Current()->GetJit();
441 if (jit == nullptr) {
442 return false;
443 }
444
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000445 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
446 // Don't attempt to do an OSR if we are close to the stack limit. Since
447 // the interpreter frames are still on stack, OSR has the potential
448 // to stack overflow even for a simple loop.
449 // b/27094810.
450 return false;
451 }
452
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000453 // Get the actual Java method if this method is from a proxy class. The compiler
454 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700455 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000456
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000457 // Cheap check if the method has been compiled already. That's an indicator that we should
458 // osr into it.
459 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
460 return false;
461 }
462
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000463 // Fetch some data before looking up for an OSR method. We don't want thread
464 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
465 // method while we are being suspended.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000466 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000467 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700468 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000469 void** memory = nullptr;
470 size_t frame_size = 0;
471 ShadowFrame* shadow_frame = nullptr;
472 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000473
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000474 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700475 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000476 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
477 if (osr_method == nullptr) {
478 // No osr method yet, just return to the interpreter.
479 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000480 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000481
482 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000483 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000484
485 // Find stack map starting at the target dex_pc.
486 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
487 if (!stack_map.IsValid()) {
488 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
489 // hope that the next branch has one.
490 return false;
491 }
492
Aart Bik29bdaee2016-05-18 15:44:07 -0700493 // Before allowing the jump, make sure the debugger is not active to avoid jumping from
494 // interpreter to OSR while e.g. single stepping. Note that we could selectively disable
495 // OSR when single stepping, but that's currently hard to know at this point.
496 if (Dbg::IsDebuggerActive()) {
497 return false;
498 }
499
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000500 // We found a stack map, now fill the frame with dex register values from the interpreter's
501 // shadow frame.
502 DexRegisterMap vreg_map =
503 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
504
505 frame_size = osr_method->GetFrameSizeInBytes();
506
507 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
508 // stack.
509 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
510 // but that is engineering complexity not worth the effort for something like OSR.
511 memory = reinterpret_cast<void**>(malloc(frame_size));
512 CHECK(memory != nullptr);
513 memset(memory, 0, frame_size);
514
515 // Art ABI: ArtMethod is at the bottom of the stack.
516 memory[0] = method;
517
518 shadow_frame = thread->PopShadowFrame();
519 if (!vreg_map.IsValid()) {
520 // If we don't have a dex register map, then there are no live dex registers at
521 // this dex pc.
522 } else {
523 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
524 DexRegisterLocation::Kind location =
525 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
526 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000527 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000528 continue;
529 }
530
531 if (location == DexRegisterLocation::Kind::kConstant) {
532 // We skip constants because the compiled code knows how to handle them.
533 continue;
534 }
535
David Srbecky7dc11782016-02-25 13:23:56 +0000536 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000537
538 int32_t vreg_value = shadow_frame->GetVReg(vreg);
539 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
540 number_of_vregs,
541 code_info,
542 encoding);
543 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
544 DCHECK_GT(slot_offset, 0);
545 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
546 }
547 }
548
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800549 native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000550 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000551 VLOG(jit) << "Jumping to "
552 << method_name
553 << "@"
554 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000555 }
556
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000557 {
558 ManagedStack fragment;
559 thread->PushManagedStackFragment(&fragment);
560 (*art_quick_osr_stub)(memory,
561 frame_size,
562 native_pc,
563 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000564 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000565 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000566
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000567 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
568 thread->DeoptimizeWithDeoptimizationException(result);
569 }
570 thread->PopManagedStackFragment(fragment);
571 }
572 free(memory);
573 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000574 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000575 return true;
576}
577
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000578void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
579 if (bytes > 4 * MB) {
580 LOG(INFO) << "Compiler allocated "
581 << PrettySize(bytes)
582 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700583 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000584 }
585 MutexLock mu(Thread::Current(), lock_);
586 memory_use_.AddValue(bytes);
587}
588
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100589class JitCompileTask FINAL : public Task {
590 public:
591 enum TaskKind {
592 kAllocateProfile,
593 kCompile,
594 kCompileOsr
595 };
596
597 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
598 ScopedObjectAccess soa(Thread::Current());
599 // Add a global ref to the class to prevent class unloading until compilation is done.
600 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
601 CHECK(klass_ != nullptr);
602 }
603
604 ~JitCompileTask() {
605 ScopedObjectAccess soa(Thread::Current());
606 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
607 }
608
609 void Run(Thread* self) OVERRIDE {
610 ScopedObjectAccess soa(self);
611 if (kind_ == kCompile) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100612 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100613 } else if (kind_ == kCompileOsr) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100614 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100615 } else {
616 DCHECK(kind_ == kAllocateProfile);
617 if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700618 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100619 }
620 }
Calin Juravlea2638922016-04-29 16:44:11 +0100621 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100622 }
623
624 void Finalize() OVERRIDE {
625 delete this;
626 }
627
628 private:
629 ArtMethod* const method_;
630 const TaskKind kind_;
631 jobject klass_;
632
633 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
634};
635
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100636void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100637 if (thread_pool_ == nullptr) {
638 // Should only see this when shutting down.
639 DCHECK(Runtime::Current()->IsShuttingDown(self));
640 return;
641 }
642
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100643 if (method->IsClassInitializer() || method->IsNative() || !method->IsCompilable()) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100644 // We do not want to compile such methods.
645 return;
646 }
647 DCHECK(thread_pool_ != nullptr);
648 DCHECK_GT(warm_method_threshold_, 0);
649 DCHECK_GT(hot_method_threshold_, warm_method_threshold_);
650 DCHECK_GT(osr_method_threshold_, hot_method_threshold_);
651 DCHECK_GE(priority_thread_weight_, 1);
652 DCHECK_LE(priority_thread_weight_, hot_method_threshold_);
653
654 int32_t starting_count = method->GetCounter();
Vladimir Markoa710d912017-09-12 14:56:07 +0100655 if (Jit::ShouldUsePriorityThreadWeight(self)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100656 count *= priority_thread_weight_;
657 }
658 int32_t new_count = starting_count + count; // int32 here to avoid wrap-around;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000659 if (starting_count < warm_method_threshold_) {
660 if ((new_count >= warm_method_threshold_) &&
661 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
662 bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false);
663 if (success) {
664 VLOG(jit) << "Start profiling " << method->PrettyMethod();
665 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100666
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000667 if (thread_pool_ == nullptr) {
668 // Calling ProfilingInfo::Create might put us in a suspended state, which could
669 // lead to the thread pool being deleted when we are shutting down.
670 DCHECK(Runtime::Current()->IsShuttingDown(self));
671 return;
672 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100673
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000674 if (!success) {
675 // We failed allocating. Instead of doing the collection on the Java thread, we push
676 // an allocation to a compiler thread, that will do the collection.
677 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
678 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100679 }
680 // Avoid jumping more than one state at a time.
681 new_count = std::min(new_count, hot_method_threshold_ - 1);
Calin Juravleffc87072016-04-20 14:22:09 +0100682 } else if (use_jit_compilation_) {
683 if (starting_count < hot_method_threshold_) {
684 if ((new_count >= hot_method_threshold_) &&
685 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
686 DCHECK(thread_pool_ != nullptr);
687 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
688 }
689 // Avoid jumping more than one state at a time.
690 new_count = std::min(new_count, osr_method_threshold_ - 1);
691 } else if (starting_count < osr_method_threshold_) {
692 if (!with_backedges) {
693 // If the samples don't contain any back edge, we don't increment the hotness.
694 return;
695 }
696 if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) {
697 DCHECK(thread_pool_ != nullptr);
698 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
699 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100700 }
701 }
702 // Update hotness counter
703 method->SetCounter(new_count);
704}
705
706void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100707 Runtime* runtime = Runtime::Current();
708 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100709 // The compiler requires a ProfilingInfo object.
710 ProfilingInfo::Create(thread, method, /* retry_allocation */ true);
711 JitCompileTask compile_task(method, JitCompileTask::kCompile);
712 compile_task.Run(thread);
713 return;
714 }
715
Andreas Gampe542451c2016-07-26 09:02:02 -0700716 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100717 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
718 // instead of interpreting the method.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100719 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
720 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
721 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100722 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100723 AddSamples(thread, method, 1, /* with_backedges */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100724 }
725}
726
Mathieu Chartieref41db72016-10-25 15:08:01 -0700727void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100728 ArtMethod* caller,
729 uint32_t dex_pc,
730 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700731 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100732 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700733 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100734 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100735 info->AddInvokeInfo(dex_pc, this_object->GetClass());
736 }
737}
738
739void Jit::WaitForCompilationToFinish(Thread* self) {
740 if (thread_pool_ != nullptr) {
741 thread_pool_->Wait(self, false, false);
742 }
743}
744
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000745void Jit::Stop() {
746 Thread* self = Thread::Current();
747 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
748 WaitForCompilationToFinish(self);
749 GetThreadPool()->StopWorkers(self);
750 WaitForCompilationToFinish(self);
751}
752
753void Jit::Start() {
754 GetThreadPool()->StartWorkers(Thread::Current());
755}
756
Andreas Gampef149b3f2016-11-16 14:58:24 -0800757ScopedJitSuspend::ScopedJitSuspend() {
758 jit::Jit* jit = Runtime::Current()->GetJit();
759 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
760 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000761 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800762 }
763}
764
765ScopedJitSuspend::~ScopedJitSuspend() {
766 if (was_on_) {
767 DCHECK(Runtime::Current()->GetJit() != nullptr);
768 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000769 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800770 }
771}
772
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800773} // namespace jit
774} // namespace art