blob: 7abf52ea604326515cb956c9013954a3c77b7afd [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
Calin Juravleb2771b42016-04-07 17:09:25 +0100147bool Jit::ShouldUsePriorityThreadWeight() {
Calin Juravle97cbc922016-04-15 16:16:35 +0100148 return Runtime::Current()->InJankPerceptibleProcessState()
149 && Thread::Current()->IsJitSensitiveThread();
Calin Juravleb2771b42016-04-07 17:09:25 +0100150}
151
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700152void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000153 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700154 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000155 MutexLock mu(Thread::Current(), lock_);
156 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700157}
158
Calin Juravleb8e69992016-03-09 15:37:48 +0000159void Jit::DumpForSigQuit(std::ostream& os) {
160 DumpInfo(os);
161 ProfileSaver::DumpInstanceInfo(os);
162}
163
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700164void Jit::AddTimingLogger(const TimingLogger& logger) {
165 cumulative_timings_.AddLogger(logger);
166}
167
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700168Jit::Jit() : dump_info_on_shutdown_(false),
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000169 cumulative_timings_("JIT timings"),
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000170 memory_use_("Memory used for compilation", 16),
171 lock_("JIT memory use lock"),
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000172 use_jit_compilation_(true),
173 hot_method_threshold_(0),
174 warm_method_threshold_(0),
175 osr_method_threshold_(0),
176 priority_thread_weight_(0),
177 invoke_transition_weight_(0) {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178
179Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100180 DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800181 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700182 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700183 if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800184 return nullptr;
185 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000186 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000187 options->GetCodeCacheInitialCapacity(),
188 options->GetCodeCacheMaxCapacity(),
189 jit->generate_debug_info_,
190 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800191 if (jit->GetCodeCache() == nullptr) {
192 return nullptr;
193 }
Calin Juravleffc87072016-04-20 14:22:09 +0100194 jit->use_jit_compilation_ = options->UseJitCompilation();
Calin Juravle138dbff2016-06-28 19:36:58 +0100195 jit->profile_saver_options_ = options->GetProfileSaverOptions();
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000196 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000197 << PrettySize(options->GetCodeCacheInitialCapacity())
198 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000199 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100200 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100201
202
203 jit->hot_method_threshold_ = options->GetCompileThreshold();
204 jit->warm_method_threshold_ = options->GetWarmupThreshold();
205 jit->osr_method_threshold_ = options->GetOsrThreshold();
Nicolas Geoffrayba6aae02016-04-14 14:17:29 +0100206 jit->priority_thread_weight_ = options->GetPriorityThreadWeight();
Calin Juravle155ff3d2016-04-27 14:14:58 +0100207 jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100208
209 jit->CreateThreadPool();
210
211 // Notify native debugger about the classes already loaded before the creation of the jit.
212 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800213 return jit.release();
214}
215
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700216bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 jit_library_handle_ = dlopen(
218 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
219 if (jit_library_handle_ == nullptr) {
220 std::ostringstream oss;
221 oss << "JIT could not load libart-compiler.so: " << dlerror();
222 *error_msg = oss.str();
223 return false;
224 }
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000225 jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800226 if (jit_load_ == nullptr) {
227 dlclose(jit_library_handle_);
228 *error_msg = "JIT couldn't find jit_load entry point";
229 return false;
230 }
231 jit_unload_ = reinterpret_cast<void (*)(void*)>(
232 dlsym(jit_library_handle_, "jit_unload"));
233 if (jit_unload_ == nullptr) {
234 dlclose(jit_library_handle_);
235 *error_msg = "JIT couldn't find jit_unload entry point";
236 return false;
237 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000238 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800239 dlsym(jit_library_handle_, "jit_compile_method"));
240 if (jit_compile_method_ == nullptr) {
241 dlclose(jit_library_handle_);
242 *error_msg = "JIT couldn't find jit_compile_method entry point";
243 return false;
244 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000245 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
246 dlsym(jit_library_handle_, "jit_types_loaded"));
247 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000248 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000249 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000250 return false;
251 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700252 return true;
253}
254
255bool Jit::LoadCompiler(std::string* error_msg) {
256 if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) {
257 return false;
258 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000259 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800260 VLOG(jit) << "Calling JitLoad interpreter_only="
261 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000262 jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800263 if (jit_compiler_handle_ == nullptr) {
264 dlclose(jit_library_handle_);
265 *error_msg = "JIT couldn't load compiler";
266 return false;
267 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000268 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800269 return true;
270}
271
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000272bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100273 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800274 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000275
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100276 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700277 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700278 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to breakpoint";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700279 return false;
280 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100281
282 // Don't compile the method if we are supposed to be deoptimized.
283 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
284 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700285 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100286 return false;
287 }
288
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000289 // If we get a request to compile a proxy method, we pass the actual Java method
290 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700291 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000292 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100293 return false;
294 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100295
296 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700297 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100298 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000299 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700300 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100301 if (!success) {
302 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700303 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100304 << " osr=" << std::boolalpha << osr;
305 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800306 if (kIsDebugBuild) {
307 if (self->IsExceptionPending()) {
308 mirror::Throwable* exception = self->GetException();
309 LOG(FATAL) << "No pending exception expected after compiling "
310 << ArtMethod::PrettyMethod(method)
311 << ": "
312 << exception->Dump();
313 }
314 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100315 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800316}
317
318void Jit::CreateThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100319 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
320 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000321
322 // We need peers as we may report the JIT thread, e.g., in the debugger.
323 constexpr bool kJitPoolNeedsPeers = true;
324 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
325
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100326 thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000327 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800328}
329
330void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100331 Thread* self = Thread::Current();
332 DCHECK(Runtime::Current()->IsShuttingDown(self));
333 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700334 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100335 {
336 ScopedSuspendAll ssa(__FUNCTION__);
337 // Clear thread_pool_ field while the threads are suspended.
338 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700339 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100340 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700341
342 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
343 if (!RUNNING_ON_MEMORY_TOOL) {
344 pool->StopWorkers(self);
345 pool->RemoveAllTasks(self);
346 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100347 // We could just suspend all threads, but we know those threads
348 // will finish in a short period, so it's not worth adding a suspend logic
349 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700350 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800351 }
352}
353
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000354void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800355 const std::vector<std::string>& code_paths) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100356 if (profile_saver_options_.IsEnabled()) {
357 ProfileSaver::Start(profile_saver_options_,
358 filename,
359 code_cache_.get(),
Calin Juravle77651c42017-03-03 18:04:02 -0800360 code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100361 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000362}
363
364void Jit::StopProfileSaver() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100365 if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) {
Calin Juravleb8e69992016-03-09 15:37:48 +0000366 ProfileSaver::Stop(dump_info_on_shutdown_);
Calin Juravle31f2c152015-10-23 17:56:15 +0100367 }
368}
369
Siva Chandra05d24152016-01-05 17:43:17 -0800370bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100371 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800372}
373
Nicolas Geoffray35122442016-03-02 12:05:30 +0000374bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
375 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
376}
377
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800378Jit::~Jit() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100379 DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700380 if (dump_info_on_shutdown_) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700381 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100382 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700383 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800384 DeleteThreadPool();
385 if (jit_compiler_handle_ != nullptr) {
386 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700387 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800388 }
389 if (jit_library_handle_ != nullptr) {
390 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700391 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800392 }
393}
394
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000395void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100396 if (!Runtime::Current()->UseJitCompilation()) {
397 // No need to notify if we only use the JIT to save profiles.
398 return;
399 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000400 jit::Jit* jit = Runtime::Current()->GetJit();
Calin Juravleffc87072016-04-20 14:22:09 +0100401 if (jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000402 DCHECK(jit->jit_types_loaded_ != nullptr);
403 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
404 }
405}
406
407void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
408 struct CollectClasses : public ClassVisitor {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700409 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
410 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000411 return true;
412 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800413 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000414 };
415
416 if (generate_debug_info_) {
417 ScopedObjectAccess so(Thread::Current());
418
419 CollectClasses visitor;
420 linker->VisitClasses(&visitor);
421 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000422 }
423}
424
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000425extern "C" void art_quick_osr_stub(void** stack,
426 uint32_t stack_size_in_bytes,
427 const uint8_t* native_pc,
428 JValue* result,
429 const char* shorty,
430 Thread* self);
431
432bool Jit::MaybeDoOnStackReplacement(Thread* thread,
433 ArtMethod* method,
434 uint32_t dex_pc,
435 int32_t dex_pc_offset,
436 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000437 if (!kEnableOnStackReplacement) {
438 return false;
439 }
440
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000441 Jit* jit = Runtime::Current()->GetJit();
442 if (jit == nullptr) {
443 return false;
444 }
445
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000446 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
447 // Don't attempt to do an OSR if we are close to the stack limit. Since
448 // the interpreter frames are still on stack, OSR has the potential
449 // to stack overflow even for a simple loop.
450 // b/27094810.
451 return false;
452 }
453
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000454 // Get the actual Java method if this method is from a proxy class. The compiler
455 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700456 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000457
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000458 // Cheap check if the method has been compiled already. That's an indicator that we should
459 // osr into it.
460 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
461 return false;
462 }
463
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000464 // Fetch some data before looking up for an OSR method. We don't want thread
465 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
466 // method while we are being suspended.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000467 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000468 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700469 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000470 void** memory = nullptr;
471 size_t frame_size = 0;
472 ShadowFrame* shadow_frame = nullptr;
473 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000474
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000475 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700476 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000477 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
478 if (osr_method == nullptr) {
479 // No osr method yet, just return to the interpreter.
480 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000481 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000482
483 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000484 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000485
486 // Find stack map starting at the target dex_pc.
487 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
488 if (!stack_map.IsValid()) {
489 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
490 // hope that the next branch has one.
491 return false;
492 }
493
Aart Bik29bdaee2016-05-18 15:44:07 -0700494 // Before allowing the jump, make sure the debugger is not active to avoid jumping from
495 // interpreter to OSR while e.g. single stepping. Note that we could selectively disable
496 // OSR when single stepping, but that's currently hard to know at this point.
497 if (Dbg::IsDebuggerActive()) {
498 return false;
499 }
500
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000501 // We found a stack map, now fill the frame with dex register values from the interpreter's
502 // shadow frame.
503 DexRegisterMap vreg_map =
504 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
505
506 frame_size = osr_method->GetFrameSizeInBytes();
507
508 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
509 // stack.
510 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
511 // but that is engineering complexity not worth the effort for something like OSR.
512 memory = reinterpret_cast<void**>(malloc(frame_size));
513 CHECK(memory != nullptr);
514 memset(memory, 0, frame_size);
515
516 // Art ABI: ArtMethod is at the bottom of the stack.
517 memory[0] = method;
518
519 shadow_frame = thread->PopShadowFrame();
520 if (!vreg_map.IsValid()) {
521 // If we don't have a dex register map, then there are no live dex registers at
522 // this dex pc.
523 } else {
524 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
525 DexRegisterLocation::Kind location =
526 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
527 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000528 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000529 continue;
530 }
531
532 if (location == DexRegisterLocation::Kind::kConstant) {
533 // We skip constants because the compiled code knows how to handle them.
534 continue;
535 }
536
David Srbecky7dc11782016-02-25 13:23:56 +0000537 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000538
539 int32_t vreg_value = shadow_frame->GetVReg(vreg);
540 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
541 number_of_vregs,
542 code_info,
543 encoding);
544 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
545 DCHECK_GT(slot_offset, 0);
546 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
547 }
548 }
549
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800550 native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000551 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000552 VLOG(jit) << "Jumping to "
553 << method_name
554 << "@"
555 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000556 }
557
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000558 {
559 ManagedStack fragment;
560 thread->PushManagedStackFragment(&fragment);
561 (*art_quick_osr_stub)(memory,
562 frame_size,
563 native_pc,
564 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000565 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000566 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000567
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000568 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
569 thread->DeoptimizeWithDeoptimizationException(result);
570 }
571 thread->PopManagedStackFragment(fragment);
572 }
573 free(memory);
574 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000575 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000576 return true;
577}
578
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000579void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
580 if (bytes > 4 * MB) {
581 LOG(INFO) << "Compiler allocated "
582 << PrettySize(bytes)
583 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700584 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000585 }
586 MutexLock mu(Thread::Current(), lock_);
587 memory_use_.AddValue(bytes);
588}
589
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100590class JitCompileTask FINAL : public Task {
591 public:
592 enum TaskKind {
593 kAllocateProfile,
594 kCompile,
595 kCompileOsr
596 };
597
598 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
599 ScopedObjectAccess soa(Thread::Current());
600 // Add a global ref to the class to prevent class unloading until compilation is done.
601 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
602 CHECK(klass_ != nullptr);
603 }
604
605 ~JitCompileTask() {
606 ScopedObjectAccess soa(Thread::Current());
607 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
608 }
609
610 void Run(Thread* self) OVERRIDE {
611 ScopedObjectAccess soa(self);
612 if (kind_ == kCompile) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100613 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100614 } else if (kind_ == kCompileOsr) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100615 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100616 } else {
617 DCHECK(kind_ == kAllocateProfile);
618 if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700619 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100620 }
621 }
Calin Juravlea2638922016-04-29 16:44:11 +0100622 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100623 }
624
625 void Finalize() OVERRIDE {
626 delete this;
627 }
628
629 private:
630 ArtMethod* const method_;
631 const TaskKind kind_;
632 jobject klass_;
633
634 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
635};
636
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100637void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100638 if (thread_pool_ == nullptr) {
639 // Should only see this when shutting down.
640 DCHECK(Runtime::Current()->IsShuttingDown(self));
641 return;
642 }
643
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100644 if (method->IsClassInitializer() || method->IsNative() || !method->IsCompilable()) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100645 // We do not want to compile such methods.
646 return;
647 }
648 DCHECK(thread_pool_ != nullptr);
649 DCHECK_GT(warm_method_threshold_, 0);
650 DCHECK_GT(hot_method_threshold_, warm_method_threshold_);
651 DCHECK_GT(osr_method_threshold_, hot_method_threshold_);
652 DCHECK_GE(priority_thread_weight_, 1);
653 DCHECK_LE(priority_thread_weight_, hot_method_threshold_);
654
655 int32_t starting_count = method->GetCounter();
656 if (Jit::ShouldUsePriorityThreadWeight()) {
657 count *= priority_thread_weight_;
658 }
659 int32_t new_count = starting_count + count; // int32 here to avoid wrap-around;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000660 if (starting_count < warm_method_threshold_) {
661 if ((new_count >= warm_method_threshold_) &&
662 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
663 bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false);
664 if (success) {
665 VLOG(jit) << "Start profiling " << method->PrettyMethod();
666 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100667
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000668 if (thread_pool_ == nullptr) {
669 // Calling ProfilingInfo::Create might put us in a suspended state, which could
670 // lead to the thread pool being deleted when we are shutting down.
671 DCHECK(Runtime::Current()->IsShuttingDown(self));
672 return;
673 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100674
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000675 if (!success) {
676 // We failed allocating. Instead of doing the collection on the Java thread, we push
677 // an allocation to a compiler thread, that will do the collection.
678 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
679 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100680 }
681 // Avoid jumping more than one state at a time.
682 new_count = std::min(new_count, hot_method_threshold_ - 1);
Calin Juravleffc87072016-04-20 14:22:09 +0100683 } else if (use_jit_compilation_) {
684 if (starting_count < hot_method_threshold_) {
685 if ((new_count >= hot_method_threshold_) &&
686 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
687 DCHECK(thread_pool_ != nullptr);
688 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
689 }
690 // Avoid jumping more than one state at a time.
691 new_count = std::min(new_count, osr_method_threshold_ - 1);
692 } else if (starting_count < osr_method_threshold_) {
693 if (!with_backedges) {
694 // If the samples don't contain any back edge, we don't increment the hotness.
695 return;
696 }
697 if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) {
698 DCHECK(thread_pool_ != nullptr);
699 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
700 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100701 }
702 }
703 // Update hotness counter
704 method->SetCounter(new_count);
705}
706
707void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100708 Runtime* runtime = Runtime::Current();
709 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100710 // The compiler requires a ProfilingInfo object.
711 ProfilingInfo::Create(thread, method, /* retry_allocation */ true);
712 JitCompileTask compile_task(method, JitCompileTask::kCompile);
713 compile_task.Run(thread);
714 return;
715 }
716
Andreas Gampe542451c2016-07-26 09:02:02 -0700717 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100718 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
719 // instead of interpreting the method.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100720 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
721 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
722 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100723 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100724 AddSamples(thread, method, 1, /* with_backedges */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100725 }
726}
727
Mathieu Chartieref41db72016-10-25 15:08:01 -0700728void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100729 ArtMethod* caller,
730 uint32_t dex_pc,
731 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700732 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100733 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700734 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100735 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100736 info->AddInvokeInfo(dex_pc, this_object->GetClass());
737 }
738}
739
740void Jit::WaitForCompilationToFinish(Thread* self) {
741 if (thread_pool_ != nullptr) {
742 thread_pool_->Wait(self, false, false);
743 }
744}
745
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000746void Jit::Stop() {
747 Thread* self = Thread::Current();
748 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
749 WaitForCompilationToFinish(self);
750 GetThreadPool()->StopWorkers(self);
751 WaitForCompilationToFinish(self);
752}
753
754void Jit::Start() {
755 GetThreadPool()->StartWorkers(Thread::Current());
756}
757
Andreas Gampef149b3f2016-11-16 14:58:24 -0800758ScopedJitSuspend::ScopedJitSuspend() {
759 jit::Jit* jit = Runtime::Current()->GetJit();
760 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
761 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000762 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800763 }
764}
765
766ScopedJitSuspend::~ScopedJitSuspend() {
767 if (was_on_) {
768 DCHECK(Runtime::Current()->GetJit() != nullptr);
769 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000770 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800771 }
772}
773
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800774} // namespace jit
775} // namespace art