blob: 0d95bc6e64d70c7dc33178a591ed26331cfcd034 [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
Igor Murashkin2ffb7032017-11-08 13:35:21 -080055void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070056void* 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
Alex Light0fa17862017-10-24 13:43:05 -0700275 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100276 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700277 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
278 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
279 << " due to not being safe to jit according to runtime-callbacks. For example, there"
280 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700281 return false;
282 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100283
284 // Don't compile the method if we are supposed to be deoptimized.
285 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
286 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700287 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100288 return false;
289 }
290
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000291 // If we get a request to compile a proxy method, we pass the actual Java method
292 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700293 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000294 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100295 return false;
296 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100297
298 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700299 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100300 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000301 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700302 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100303 if (!success) {
304 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700305 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100306 << " osr=" << std::boolalpha << osr;
307 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800308 if (kIsDebugBuild) {
309 if (self->IsExceptionPending()) {
310 mirror::Throwable* exception = self->GetException();
311 LOG(FATAL) << "No pending exception expected after compiling "
312 << ArtMethod::PrettyMethod(method)
313 << ": "
314 << exception->Dump();
315 }
316 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100317 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800318}
319
320void Jit::CreateThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100321 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
322 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000323
324 // We need peers as we may report the JIT thread, e.g., in the debugger.
325 constexpr bool kJitPoolNeedsPeers = true;
326 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
327
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100328 thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000329 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800330}
331
332void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100333 Thread* self = Thread::Current();
334 DCHECK(Runtime::Current()->IsShuttingDown(self));
335 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700336 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100337 {
338 ScopedSuspendAll ssa(__FUNCTION__);
339 // Clear thread_pool_ field while the threads are suspended.
340 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700341 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100342 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700343
344 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
345 if (!RUNNING_ON_MEMORY_TOOL) {
346 pool->StopWorkers(self);
347 pool->RemoveAllTasks(self);
348 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100349 // We could just suspend all threads, but we know those threads
350 // will finish in a short period, so it's not worth adding a suspend logic
351 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700352 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800353 }
354}
355
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000356void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800357 const std::vector<std::string>& code_paths) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100358 if (profile_saver_options_.IsEnabled()) {
359 ProfileSaver::Start(profile_saver_options_,
360 filename,
361 code_cache_.get(),
Calin Juravle77651c42017-03-03 18:04:02 -0800362 code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100363 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000364}
365
366void Jit::StopProfileSaver() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100367 if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) {
Calin Juravleb8e69992016-03-09 15:37:48 +0000368 ProfileSaver::Stop(dump_info_on_shutdown_);
Calin Juravle31f2c152015-10-23 17:56:15 +0100369 }
370}
371
Siva Chandra05d24152016-01-05 17:43:17 -0800372bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100373 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800374}
375
Nicolas Geoffray35122442016-03-02 12:05:30 +0000376bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
377 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
378}
379
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800380Jit::~Jit() {
Calin Juravle138dbff2016-06-28 19:36:58 +0100381 DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700382 if (dump_info_on_shutdown_) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700383 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100384 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700385 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800386 DeleteThreadPool();
387 if (jit_compiler_handle_ != nullptr) {
388 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700389 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800390 }
391 if (jit_library_handle_ != nullptr) {
392 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700393 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800394 }
395}
396
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000397void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100398 if (!Runtime::Current()->UseJitCompilation()) {
399 // No need to notify if we only use the JIT to save profiles.
400 return;
401 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000402 jit::Jit* jit = Runtime::Current()->GetJit();
Calin Juravleffc87072016-04-20 14:22:09 +0100403 if (jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000404 DCHECK(jit->jit_types_loaded_ != nullptr);
405 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
406 }
407}
408
409void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
410 struct CollectClasses : public ClassVisitor {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700411 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
412 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000413 return true;
414 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800415 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000416 };
417
418 if (generate_debug_info_) {
419 ScopedObjectAccess so(Thread::Current());
420
421 CollectClasses visitor;
422 linker->VisitClasses(&visitor);
423 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000424 }
425}
426
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000427extern "C" void art_quick_osr_stub(void** stack,
428 uint32_t stack_size_in_bytes,
429 const uint8_t* native_pc,
430 JValue* result,
431 const char* shorty,
432 Thread* self);
433
434bool Jit::MaybeDoOnStackReplacement(Thread* thread,
435 ArtMethod* method,
436 uint32_t dex_pc,
437 int32_t dex_pc_offset,
438 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000439 if (!kEnableOnStackReplacement) {
440 return false;
441 }
442
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000443 Jit* jit = Runtime::Current()->GetJit();
444 if (jit == nullptr) {
445 return false;
446 }
447
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000448 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
449 // Don't attempt to do an OSR if we are close to the stack limit. Since
450 // the interpreter frames are still on stack, OSR has the potential
451 // to stack overflow even for a simple loop.
452 // b/27094810.
453 return false;
454 }
455
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000456 // Get the actual Java method if this method is from a proxy class. The compiler
457 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700458 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000459
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000460 // Cheap check if the method has been compiled already. That's an indicator that we should
461 // osr into it.
462 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
463 return false;
464 }
465
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000466 // Fetch some data before looking up for an OSR method. We don't want thread
467 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
468 // method while we are being suspended.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000469 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000470 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700471 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000472 void** memory = nullptr;
473 size_t frame_size = 0;
474 ShadowFrame* shadow_frame = nullptr;
475 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000476
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000477 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700478 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000479 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
480 if (osr_method == nullptr) {
481 // No osr method yet, just return to the interpreter.
482 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000483 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000484
485 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000486 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000487
488 // Find stack map starting at the target dex_pc.
489 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
490 if (!stack_map.IsValid()) {
491 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
492 // hope that the next branch has one.
493 return false;
494 }
495
Alex Light21611932017-09-26 13:07:39 -0700496 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
497 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
498 // disable OSR when single stepping, but that's currently hard to know at this point.
499 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700500 return false;
501 }
502
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000503 // We found a stack map, now fill the frame with dex register values from the interpreter's
504 // shadow frame.
505 DexRegisterMap vreg_map =
506 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
507
508 frame_size = osr_method->GetFrameSizeInBytes();
509
510 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
511 // stack.
512 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
513 // but that is engineering complexity not worth the effort for something like OSR.
514 memory = reinterpret_cast<void**>(malloc(frame_size));
515 CHECK(memory != nullptr);
516 memset(memory, 0, frame_size);
517
518 // Art ABI: ArtMethod is at the bottom of the stack.
519 memory[0] = method;
520
521 shadow_frame = thread->PopShadowFrame();
522 if (!vreg_map.IsValid()) {
523 // If we don't have a dex register map, then there are no live dex registers at
524 // this dex pc.
525 } else {
526 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
527 DexRegisterLocation::Kind location =
528 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
529 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000530 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000531 continue;
532 }
533
534 if (location == DexRegisterLocation::Kind::kConstant) {
535 // We skip constants because the compiled code knows how to handle them.
536 continue;
537 }
538
David Srbecky7dc11782016-02-25 13:23:56 +0000539 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000540
541 int32_t vreg_value = shadow_frame->GetVReg(vreg);
542 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
543 number_of_vregs,
544 code_info,
545 encoding);
546 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
547 DCHECK_GT(slot_offset, 0);
548 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
549 }
550 }
551
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800552 native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000553 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000554 VLOG(jit) << "Jumping to "
555 << method_name
556 << "@"
557 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000558 }
559
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000560 {
561 ManagedStack fragment;
562 thread->PushManagedStackFragment(&fragment);
563 (*art_quick_osr_stub)(memory,
564 frame_size,
565 native_pc,
566 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000567 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000568 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000569
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000570 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
571 thread->DeoptimizeWithDeoptimizationException(result);
572 }
573 thread->PopManagedStackFragment(fragment);
574 }
575 free(memory);
576 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000577 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000578 return true;
579}
580
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000581void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
582 if (bytes > 4 * MB) {
583 LOG(INFO) << "Compiler allocated "
584 << PrettySize(bytes)
585 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700586 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000587 }
588 MutexLock mu(Thread::Current(), lock_);
589 memory_use_.AddValue(bytes);
590}
591
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100592class JitCompileTask FINAL : public Task {
593 public:
594 enum TaskKind {
595 kAllocateProfile,
596 kCompile,
597 kCompileOsr
598 };
599
600 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
601 ScopedObjectAccess soa(Thread::Current());
602 // Add a global ref to the class to prevent class unloading until compilation is done.
603 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
604 CHECK(klass_ != nullptr);
605 }
606
607 ~JitCompileTask() {
608 ScopedObjectAccess soa(Thread::Current());
609 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
610 }
611
612 void Run(Thread* self) OVERRIDE {
613 ScopedObjectAccess soa(self);
614 if (kind_ == kCompile) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100615 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100616 } else if (kind_ == kCompileOsr) {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100617 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100618 } else {
619 DCHECK(kind_ == kAllocateProfile);
620 if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700621 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100622 }
623 }
Calin Juravlea2638922016-04-29 16:44:11 +0100624 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100625 }
626
627 void Finalize() OVERRIDE {
628 delete this;
629 }
630
631 private:
632 ArtMethod* const method_;
633 const TaskKind kind_;
634 jobject klass_;
635
636 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
637};
638
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100639void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100640 if (thread_pool_ == nullptr) {
641 // Should only see this when shutting down.
642 DCHECK(Runtime::Current()->IsShuttingDown(self));
643 return;
644 }
645
Vladimir Marko2196c652017-11-30 16:16:07 +0000646 if (method->IsClassInitializer() || !method->IsCompilable()) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100647 // We do not want to compile such methods.
648 return;
649 }
650 DCHECK(thread_pool_ != nullptr);
651 DCHECK_GT(warm_method_threshold_, 0);
652 DCHECK_GT(hot_method_threshold_, warm_method_threshold_);
653 DCHECK_GT(osr_method_threshold_, hot_method_threshold_);
654 DCHECK_GE(priority_thread_weight_, 1);
655 DCHECK_LE(priority_thread_weight_, hot_method_threshold_);
656
657 int32_t starting_count = method->GetCounter();
Vladimir Markoa710d912017-09-12 14:56:07 +0100658 if (Jit::ShouldUsePriorityThreadWeight(self)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100659 count *= priority_thread_weight_;
660 }
661 int32_t new_count = starting_count + count; // int32 here to avoid wrap-around;
Vladimir Marko2196c652017-11-30 16:16:07 +0000662 // Note: Native method have no "warm" state or profiling info.
663 if (LIKELY(!method->IsNative()) && starting_count < warm_method_threshold_) {
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000664 if ((new_count >= warm_method_threshold_) &&
665 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
666 bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false);
667 if (success) {
668 VLOG(jit) << "Start profiling " << method->PrettyMethod();
669 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100670
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000671 if (thread_pool_ == nullptr) {
672 // Calling ProfilingInfo::Create might put us in a suspended state, which could
673 // lead to the thread pool being deleted when we are shutting down.
674 DCHECK(Runtime::Current()->IsShuttingDown(self));
675 return;
676 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100677
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000678 if (!success) {
679 // We failed allocating. Instead of doing the collection on the Java thread, we push
680 // an allocation to a compiler thread, that will do the collection.
681 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
682 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100683 }
684 // Avoid jumping more than one state at a time.
685 new_count = std::min(new_count, hot_method_threshold_ - 1);
Calin Juravleffc87072016-04-20 14:22:09 +0100686 } else if (use_jit_compilation_) {
687 if (starting_count < hot_method_threshold_) {
688 if ((new_count >= hot_method_threshold_) &&
689 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
690 DCHECK(thread_pool_ != nullptr);
691 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
692 }
693 // Avoid jumping more than one state at a time.
694 new_count = std::min(new_count, osr_method_threshold_ - 1);
695 } else if (starting_count < osr_method_threshold_) {
696 if (!with_backedges) {
697 // If the samples don't contain any back edge, we don't increment the hotness.
698 return;
699 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000700 DCHECK(!method->IsNative()); // No back edges reported for native methods.
Calin Juravleffc87072016-04-20 14:22:09 +0100701 if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) {
702 DCHECK(thread_pool_ != nullptr);
703 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
704 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100705 }
706 }
707 // Update hotness counter
708 method->SetCounter(new_count);
709}
710
711void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100712 Runtime* runtime = Runtime::Current();
713 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100714 // The compiler requires a ProfilingInfo object.
715 ProfilingInfo::Create(thread, method, /* retry_allocation */ true);
716 JitCompileTask compile_task(method, JitCompileTask::kCompile);
717 compile_task.Run(thread);
718 return;
719 }
720
Andreas Gampe542451c2016-07-26 09:02:02 -0700721 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100722 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
723 // instead of interpreting the method.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100724 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
725 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
726 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100727 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100728 AddSamples(thread, method, 1, /* with_backedges */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100729 }
730}
731
Mathieu Chartieref41db72016-10-25 15:08:01 -0700732void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100733 ArtMethod* caller,
734 uint32_t dex_pc,
735 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700736 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100737 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700738 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100739 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100740 info->AddInvokeInfo(dex_pc, this_object->GetClass());
741 }
742}
743
744void Jit::WaitForCompilationToFinish(Thread* self) {
745 if (thread_pool_ != nullptr) {
746 thread_pool_->Wait(self, false, false);
747 }
748}
749
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000750void Jit::Stop() {
751 Thread* self = Thread::Current();
752 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
753 WaitForCompilationToFinish(self);
754 GetThreadPool()->StopWorkers(self);
755 WaitForCompilationToFinish(self);
756}
757
758void Jit::Start() {
759 GetThreadPool()->StartWorkers(Thread::Current());
760}
761
Andreas Gampef149b3f2016-11-16 14:58:24 -0800762ScopedJitSuspend::ScopedJitSuspend() {
763 jit::Jit* jit = Runtime::Current()->GetJit();
764 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
765 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000766 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800767 }
768}
769
770ScopedJitSuspend::~ScopedJitSuspend() {
771 if (was_on_) {
772 DCHECK(Runtime::Current()->GetJit() != nullptr);
773 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000774 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800775 }
776}
777
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800778} // namespace jit
779} // namespace art