blob: 4a3ef0781921dfd519bb81537445c6332568822d [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "jit.h"
18
19#include <dlfcn.h>
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Andreas Gampe57943812017-12-06 21:39:13 -080023#include "base/logging.h" // For VLOG.
Andreas Gampe0897e1c2017-05-16 08:36:56 -070024#include "base/memory_tool.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080025#include "base/runtime_debug.h"
David Sehrc431b9d2018-03-02 12:01:51 -080026#include "base/utils.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010027#include "class_root.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070028#include "debugger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "entrypoints/runtime_asm_entrypoints.h"
30#include "interpreter/interpreter.h"
31#include "jit_code_cache.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010032#include "jni/java_vm_ext.h"
Orion Hodson52f5a1f2018-05-02 11:05:44 +010033#include "mirror/method_handle_impl.h"
34#include "mirror/var_handle.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010035#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000036#include "oat_quick_method_header.h"
David Sehr82d046e2018-04-23 08:14:19 -070037#include "profile/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000038#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039#include "runtime.h"
40#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070041#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000042#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070043#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010044#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045
46namespace art {
47namespace jit {
48
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000049static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000050
Andreas Gampe7897cec2017-07-19 16:28:59 -070051// Different compilation threshold constants. These can be overridden on the command line.
52static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
53static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
54static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
55
Mathieu Chartier72918ea2016-03-24 11:07:06 -070056// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080057void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070058void* Jit::jit_compiler_handle_ = nullptr;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000059void* (*Jit::jit_load_)(void) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070060void (*Jit::jit_unload_)(void*) = nullptr;
61bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
62void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000063bool (*Jit::jit_generate_debug_info_)(void*) = nullptr;
64void (*Jit::jit_update_options_)(void*) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070065
Andreas Gampe7897cec2017-07-19 16:28:59 -070066struct StressModeHelper {
67 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
68};
69DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
70
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010073 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000074
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000075 jit_options->code_cache_initial_capacity_ =
76 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
77 jit_options->code_cache_max_capacity_ =
78 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070079 jit_options->dump_info_on_shutdown_ =
80 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010081 jit_options->profile_saver_options_ =
82 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010083 jit_options->thread_pool_pthread_priority_ =
84 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000085
Andreas Gampe7897cec2017-07-19 16:28:59 -070086 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
87 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
88 } else {
89 jit_options->compile_threshold_ =
90 kIsDebugBuild
91 ? (StressModeHelper::kSlowMode
92 ? kJitSlowStressDefaultCompileThreshold
93 : kJitStressDefaultCompileThreshold)
94 : kJitDefaultCompileThreshold;
95 }
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000096 if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
97 LOG(FATAL) << "Method compilation threshold is above its internal limit.";
98 }
99
100 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
101 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
102 if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
103 LOG(FATAL) << "Method warmup threshold is above its internal limit.";
104 }
105 } else {
106 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
107 }
108
109 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
110 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
111 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
112 LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
113 }
114 } else {
115 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
116 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
117 jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
118 }
119 }
120
Calin Juravleb2771b42016-04-07 17:09:25 +0100121 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
122 jit_options->priority_thread_weight_ =
123 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
124 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
125 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
126 } else if (jit_options->priority_thread_weight_ == 0) {
127 LOG(FATAL) << "Priority thread weight cannot be 0.";
128 }
129 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100130 jit_options->priority_thread_weight_ = std::max(
131 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
132 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100133 }
134
Calin Juravle155ff3d2016-04-27 14:14:58 +0100135 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100136 jit_options->invoke_transition_weight_ =
137 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100138 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
139 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
140 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100141 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100142 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100143 } else {
144 jit_options->invoke_transition_weight_ = std::max(
145 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800146 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100147 }
148
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800149 return jit_options;
150}
151
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000152bool Jit::ShouldUsePriorityThreadWeight(Thread* self) {
153 return self->IsJitSensitiveThread() && Runtime::Current()->InJankPerceptibleProcessState();
154}
155
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000157 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700158 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000159 MutexLock mu(Thread::Current(), lock_);
160 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700161}
162
Calin Juravleb8e69992016-03-09 15:37:48 +0000163void Jit::DumpForSigQuit(std::ostream& os) {
164 DumpInfo(os);
165 ProfileSaver::DumpInstanceInfo(os);
166}
167
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700168void Jit::AddTimingLogger(const TimingLogger& logger) {
169 cumulative_timings_.AddLogger(logger);
170}
171
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100172Jit::Jit(JitCodeCache* code_cache, JitOptions* options)
173 : code_cache_(code_cache),
174 options_(options),
175 cumulative_timings_("JIT timings"),
176 memory_use_("Memory used for compilation", 16),
177 lock_("JIT memory use lock") {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100179Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) {
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000180 if (jit_load_ == nullptr) {
181 LOG(WARNING) << "Not creating JIT: library not loaded";
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 return nullptr;
183 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000184 jit_compiler_handle_ = (jit_load_)();
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000185 if (jit_compiler_handle_ == nullptr) {
186 LOG(WARNING) << "Not creating JIT: failed to allocate a compiler";
187 return nullptr;
188 }
189 std::unique_ptr<Jit> jit(new Jit(code_cache, options));
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000190
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000191 // If the code collector is enabled, check if that still holds:
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000192 // With 'perf', we want a 1-1 mapping between an address and a method.
193 // We aren't able to keep method pointers live during the instrumentation method entry trampoline
194 // so we will just disable jit-gc if we are doing that.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000195 if (code_cache->GetGarbageCollectCode()) {
196 code_cache->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
197 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
198 }
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100199
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000200 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000201 << PrettySize(options->GetCodeCacheInitialCapacity())
202 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000203 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100204 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100205
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100206 // Notify native debugger about the classes already loaded before the creation of the jit.
207 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800208 return jit.release();
209}
210
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000211template <typename T>
212bool Jit::LoadSymbol(T* address, const char* name, std::string* error_msg) {
213 *address = reinterpret_cast<T>(dlsym(jit_library_handle_, name));
214 if (*address == nullptr) {
215 *error_msg = std::string("JIT couldn't find ") + name + std::string(" entry point");
216 return false;
217 }
218 return true;
219}
220
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000221bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800222 jit_library_handle_ = dlopen(
223 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
224 if (jit_library_handle_ == nullptr) {
225 std::ostringstream oss;
226 oss << "JIT could not load libart-compiler.so: " << dlerror();
227 *error_msg = oss.str();
228 return false;
229 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000230 bool all_resolved = true;
231 all_resolved = all_resolved && LoadSymbol(&jit_load_, "jit_load", error_msg);
232 all_resolved = all_resolved && LoadSymbol(&jit_unload_, "jit_unload", error_msg);
233 all_resolved = all_resolved && LoadSymbol(&jit_compile_method_, "jit_compile_method", error_msg);
234 all_resolved = all_resolved && LoadSymbol(&jit_types_loaded_, "jit_types_loaded", error_msg);
235 all_resolved = all_resolved && LoadSymbol(&jit_update_options_, "jit_update_options", error_msg);
236 all_resolved = all_resolved &&
237 LoadSymbol(&jit_generate_debug_info_, "jit_generate_debug_info", error_msg);
238 if (!all_resolved) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800239 dlclose(jit_library_handle_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000240 return false;
241 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700242 return true;
243}
244
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000245bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100246 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800247 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000248
Alex Light0fa17862017-10-24 13:43:05 -0700249 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100250 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700251 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
252 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
253 << " due to not being safe to jit according to runtime-callbacks. For example, there"
254 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700255 return false;
256 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100257
258 // Don't compile the method if we are supposed to be deoptimized.
259 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
260 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700261 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100262 return false;
263 }
264
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000265 // If we get a request to compile a proxy method, we pass the actual Java method
266 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700267 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000268 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100269 return false;
270 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100271
272 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700273 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100274 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000275 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700276 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100277 if (!success) {
278 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700279 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100280 << " osr=" << std::boolalpha << osr;
281 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800282 if (kIsDebugBuild) {
283 if (self->IsExceptionPending()) {
284 mirror::Throwable* exception = self->GetException();
285 LOG(FATAL) << "No pending exception expected after compiling "
286 << ArtMethod::PrettyMethod(method)
287 << ": "
288 << exception->Dump();
289 }
290 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100291 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800292}
293
294void Jit::CreateThreadPool() {
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000295 if (Runtime::Current()->IsSafeMode()) {
296 // Never create the pool in safe mode.
297 return;
298 }
299 // There is a DCHECK in the 'AddSamples' method to ensure the thread pool
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100300 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000301
302 // We need peers as we may report the JIT thread, e.g., in the debugger.
303 constexpr bool kJitPoolNeedsPeers = true;
304 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
305
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100306 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000307 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800308}
309
310void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100311 Thread* self = Thread::Current();
312 DCHECK(Runtime::Current()->IsShuttingDown(self));
313 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700314 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100315 {
316 ScopedSuspendAll ssa(__FUNCTION__);
317 // Clear thread_pool_ field while the threads are suspended.
318 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700319 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100320 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700321
322 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000323 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700324 pool->StopWorkers(self);
325 pool->RemoveAllTasks(self);
326 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100327 // We could just suspend all threads, but we know those threads
328 // will finish in a short period, so it's not worth adding a suspend logic
329 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700330 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800331 }
332}
333
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000334void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800335 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100336 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100337 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100338 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000339}
340
341void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100342 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
343 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100344 }
345}
346
Siva Chandra05d24152016-01-05 17:43:17 -0800347bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100348 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800349}
350
Nicolas Geoffray35122442016-03-02 12:05:30 +0000351bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
352 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
353}
354
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800355Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100356 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
357 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700358 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100359 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700360 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800361 DeleteThreadPool();
362 if (jit_compiler_handle_ != nullptr) {
363 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700364 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800365 }
366 if (jit_library_handle_ != nullptr) {
367 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700368 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800369 }
370}
371
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000372void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100373 if (!Runtime::Current()->UseJitCompilation()) {
374 // No need to notify if we only use the JIT to save profiles.
375 return;
376 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000377 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000378 if (jit_generate_debug_info_(jit->jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000379 DCHECK(jit->jit_types_loaded_ != nullptr);
380 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
381 }
382}
383
384void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
385 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100386 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700387 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000388 return true;
389 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800390 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000391 };
392
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000393 if (jit_generate_debug_info_(jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000394 ScopedObjectAccess so(Thread::Current());
395
396 CollectClasses visitor;
397 linker->VisitClasses(&visitor);
398 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000399 }
400}
401
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000402extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700403 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000404 const uint8_t* native_pc,
405 JValue* result,
406 const char* shorty,
407 Thread* self);
408
409bool Jit::MaybeDoOnStackReplacement(Thread* thread,
410 ArtMethod* method,
411 uint32_t dex_pc,
412 int32_t dex_pc_offset,
413 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000414 if (!kEnableOnStackReplacement) {
415 return false;
416 }
417
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000418 Jit* jit = Runtime::Current()->GetJit();
419 if (jit == nullptr) {
420 return false;
421 }
422
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000423 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
424 // Don't attempt to do an OSR if we are close to the stack limit. Since
425 // the interpreter frames are still on stack, OSR has the potential
426 // to stack overflow even for a simple loop.
427 // b/27094810.
428 return false;
429 }
430
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000431 // Get the actual Java method if this method is from a proxy class. The compiler
432 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700433 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000434
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000435 // Cheap check if the method has been compiled already. That's an indicator that we should
436 // osr into it.
437 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
438 return false;
439 }
440
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000441 // Fetch some data before looking up for an OSR method. We don't want thread
442 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
443 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000444 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800445 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000446 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700447 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000448 void** memory = nullptr;
449 size_t frame_size = 0;
450 ShadowFrame* shadow_frame = nullptr;
451 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000452
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000453 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700454 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000455 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
456 if (osr_method == nullptr) {
457 // No osr method yet, just return to the interpreter.
458 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000459 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000460
David Srbecky052f8ca2018-04-26 15:42:54 +0100461 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000462
463 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100464 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000465 if (!stack_map.IsValid()) {
466 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
467 // hope that the next branch has one.
468 return false;
469 }
470
Alex Light21611932017-09-26 13:07:39 -0700471 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
472 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
473 // disable OSR when single stepping, but that's currently hard to know at this point.
474 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700475 return false;
476 }
477
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000478 // We found a stack map, now fill the frame with dex register values from the interpreter's
479 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100480 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000481
482 frame_size = osr_method->GetFrameSizeInBytes();
483
484 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
485 // stack.
486 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
487 // but that is engineering complexity not worth the effort for something like OSR.
488 memory = reinterpret_cast<void**>(malloc(frame_size));
489 CHECK(memory != nullptr);
490 memset(memory, 0, frame_size);
491
492 // Art ABI: ArtMethod is at the bottom of the stack.
493 memory[0] = method;
494
495 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100496 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000497 // If we don't have a dex register map, then there are no live dex registers at
498 // this dex pc.
499 } else {
David Srbeckyfd89b072018-06-03 12:00:22 +0100500 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000501 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100502 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000503 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000504 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000505 continue;
506 }
507
508 if (location == DexRegisterLocation::Kind::kConstant) {
509 // We skip constants because the compiled code knows how to handle them.
510 continue;
511 }
512
David Srbecky7dc11782016-02-25 13:23:56 +0000513 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000514
515 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100516 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000517 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
518 DCHECK_GT(slot_offset, 0);
519 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
520 }
521 }
522
David Srbecky052f8ca2018-04-26 15:42:54 +0100523 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000524 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000525 VLOG(jit) << "Jumping to "
526 << method_name
527 << "@"
528 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000529 }
530
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000531 {
532 ManagedStack fragment;
533 thread->PushManagedStackFragment(&fragment);
534 (*art_quick_osr_stub)(memory,
535 frame_size,
536 native_pc,
537 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000538 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000539 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000540
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000541 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
542 thread->DeoptimizeWithDeoptimizationException(result);
543 }
544 thread->PopManagedStackFragment(fragment);
545 }
546 free(memory);
547 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000548 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000549 return true;
550}
551
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000552void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
553 if (bytes > 4 * MB) {
554 LOG(INFO) << "Compiler allocated "
555 << PrettySize(bytes)
556 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700557 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000558 }
559 MutexLock mu(Thread::Current(), lock_);
560 memory_use_.AddValue(bytes);
561}
562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100564 public:
565 enum TaskKind {
566 kAllocateProfile,
567 kCompile,
568 kCompileOsr
569 };
570
571 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
572 ScopedObjectAccess soa(Thread::Current());
573 // Add a global ref to the class to prevent class unloading until compilation is done.
574 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
575 CHECK(klass_ != nullptr);
576 }
577
578 ~JitCompileTask() {
579 ScopedObjectAccess soa(Thread::Current());
580 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
581 }
582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100583 void Run(Thread* self) override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100584 ScopedObjectAccess soa(self);
585 if (kind_ == kCompile) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700586 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr= */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100587 } else if (kind_ == kCompileOsr) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700588 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr= */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100589 } else {
590 DCHECK(kind_ == kAllocateProfile);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700591 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700592 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100593 }
594 }
Calin Juravlea2638922016-04-29 16:44:11 +0100595 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100596 }
597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100598 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100599 delete this;
600 }
601
602 private:
603 ArtMethod* const method_;
604 const TaskKind kind_;
605 jobject klass_;
606
607 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
608};
609
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100610static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
611 if (method->IsClassInitializer() || !method->IsCompilable()) {
612 // We do not want to compile such methods.
613 return true;
614 }
615 if (method->IsNative()) {
616 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100617 if (klass == GetClassRoot<mirror::MethodHandle>() ||
618 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100619 // MethodHandle and VarHandle invocation methods are required to throw an
620 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
621 // implementations that arise the exception. We need to disable JIT compilation of these JNI
622 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
623 // stubs. Since these stubs have different stack representations we can then crash in stack
624 // walking (b/78151261).
625 return true;
626 }
627 }
628 return false;
629}
630
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000631void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100632 if (thread_pool_ == nullptr) {
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000633 // Should only see this when shutting down, starting up, or in zygote, which doesn't
634 // have a thread pool.
635 DCHECK(Runtime::Current()->IsShuttingDown(self) ||
636 !Runtime::Current()->IsFinishedStarting() ||
637 Runtime::Current()->IsZygote());
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000638 return;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100639 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100640 if (IgnoreSamplesForMethod(method)) {
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000641 return;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100642 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100643 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000644 // Tests might request JIT on first use (compiled synchronously in the interpreter).
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000645 return;
David Srbeckyf4886df2017-12-11 16:06:29 +0000646 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100647 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100648 DCHECK_GT(WarmMethodThreshold(), 0);
649 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
650 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
651 DCHECK_GE(PriorityThreadWeight(), 1);
652 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100653
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000654 uint16_t starting_count = method->GetCounter();
655 if (Jit::ShouldUsePriorityThreadWeight(self)) {
656 count *= PriorityThreadWeight();
657 }
658 uint32_t new_count = starting_count + count;
659 // Note: Native method have no "warm" state or profiling info.
660 if (LIKELY(!method->IsNative()) && starting_count < WarmMethodThreshold()) {
661 if ((new_count >= WarmMethodThreshold()) &&
662 (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700663 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000664 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));
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000672 return;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000673 }
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 }
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000681 // Avoid jumping more than one state at a time.
682 new_count = std::min(new_count, static_cast<uint32_t>(HotMethodThreshold() - 1));
683 } else if (UseJitCompilation()) {
684 if (starting_count < HotMethodThreshold()) {
685 if ((new_count >= HotMethodThreshold()) &&
686 !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Calin Juravleffc87072016-04-20 14:22:09 +0100687 DCHECK(thread_pool_ != nullptr);
688 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
689 }
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000690 // Avoid jumping more than one state at a time.
691 new_count = std::min(new_count, static_cast<uint32_t>(OSRMethodThreshold() - 1));
692 } else if (starting_count < OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100693 if (!with_backedges) {
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000694 // If the samples don't contain any back edge, we don't increment the hotness.
695 return;
Calin Juravleffc87072016-04-20 14:22:09 +0100696 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000697 DCHECK(!method->IsNative()); // No back edges reported for native methods.
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000698 if ((new_count >= OSRMethodThreshold()) && !code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100699 DCHECK(thread_pool_ != nullptr);
700 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
701 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100702 }
703 }
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000704 // Update hotness counter
705 method->SetCounter(new_count);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100706}
707
Alex Light59b950f2018-10-08 10:43:06 -0700708class ScopedSetRuntimeThread {
709 public:
710 explicit ScopedSetRuntimeThread(Thread* self)
711 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
712 self_->SetIsRuntimeThread(true);
713 }
714
715 ~ScopedSetRuntimeThread() {
716 self_->SetIsRuntimeThread(was_runtime_thread_);
717 }
718
719 private:
720 Thread* self_;
721 bool was_runtime_thread_;
722};
723
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100724void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100725 Runtime* runtime = Runtime::Current();
726 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000727 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000728 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000729 if (!np_method->IsNative()) {
730 // The compiler requires a ProfilingInfo object for non-native methods.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700731 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
Vladimir Markof8655b32018-03-21 17:53:56 +0000732 }
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000733 JitCompileTask compile_task(method, JitCompileTask::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -0700734 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
735 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000736 compile_task.Run(thread);
737 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100738 return;
739 }
740
Andreas Gampe542451c2016-07-26 09:02:02 -0700741 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100742 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -0700743 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
744 // must remain the instrumentation entrypoint.
745 if ((profiling_info != nullptr) &&
746 (profiling_info->GetSavedEntryPoint() != nullptr) &&
747 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100748 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
749 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100750 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700751 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100752 }
753}
754
Mathieu Chartieref41db72016-10-25 15:08:01 -0700755void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100756 ArtMethod* caller,
757 uint32_t dex_pc,
758 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700759 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100760 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700761 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100762 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100763 info->AddInvokeInfo(dex_pc, this_object->GetClass());
764 }
765}
766
767void Jit::WaitForCompilationToFinish(Thread* self) {
768 if (thread_pool_ != nullptr) {
769 thread_pool_->Wait(self, false, false);
770 }
771}
772
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000773void Jit::Stop() {
774 Thread* self = Thread::Current();
775 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
776 WaitForCompilationToFinish(self);
777 GetThreadPool()->StopWorkers(self);
778 WaitForCompilationToFinish(self);
779}
780
781void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000782 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000783}
784
Andreas Gampef149b3f2016-11-16 14:58:24 -0800785ScopedJitSuspend::ScopedJitSuspend() {
786 jit::Jit* jit = Runtime::Current()->GetJit();
787 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
788 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000789 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800790 }
791}
792
793ScopedJitSuspend::~ScopedJitSuspend() {
794 if (was_on_) {
795 DCHECK(Runtime::Current()->GetJit() != nullptr);
796 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000797 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800798 }
799}
800
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000801void Jit::PostForkChildAction() {
802 // At this point, the compiler options have been adjusted to the particular configuration
803 // of the forked child. Parse them again.
804 jit_update_options_(jit_compiler_handle_);
805
806 // Adjust the status of code cache collection: the status from zygote was to not collect.
807 code_cache_->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
808 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
809}
810
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800811} // namespace jit
812} // namespace art