blob: d44bd5988efc267f0f25732c9e08ab91d62c4574 [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"
David Srbeckye3fc2d12018-11-30 13:41:14 +000031#include "jit-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit_code_cache.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010033#include "jni/java_vm_ext.h"
Orion Hodson52f5a1f2018-05-02 11:05:44 +010034#include "mirror/method_handle_impl.h"
35#include "mirror/var_handle.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010036#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000037#include "oat_quick_method_header.h"
David Sehr82d046e2018-04-23 08:14:19 -070038#include "profile/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000039#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040#include "runtime.h"
41#include "runtime_options.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070042#include "stack.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000043#include "stack_map.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070044#include "thread-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010045#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046
47namespace art {
48namespace jit {
49
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000050static constexpr bool kEnableOnStackReplacement = true;
Nicolas Geoffraye8662132016-02-15 10:00:42 +000051
Andreas Gampe7897cec2017-07-19 16:28:59 -070052// Different compilation threshold constants. These can be overridden on the command line.
53static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default.
54static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build.
55static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build.
56
Mathieu Chartier72918ea2016-03-24 11:07:06 -070057// JIT compiler
Igor Murashkin2ffb7032017-11-08 13:35:21 -080058void* Jit::jit_library_handle_ = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070059void* Jit::jit_compiler_handle_ = nullptr;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000060void* (*Jit::jit_load_)(void) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070061void (*Jit::jit_unload_)(void*) = nullptr;
Nicolas Geoffray075456e2018-12-14 08:54:21 +000062bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool, bool) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070063void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000064bool (*Jit::jit_generate_debug_info_)(void*) = nullptr;
65void (*Jit::jit_update_options_)(void*) = nullptr;
Mathieu Chartier72918ea2016-03-24 11:07:06 -070066
Andreas Gampe7897cec2017-07-19 16:28:59 -070067struct StressModeHelper {
68 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
69};
70DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
71
David Srbeckye3fc2d12018-11-30 13:41:14 +000072uint32_t JitOptions::RoundUpThreshold(uint32_t threshold) {
73 if (threshold > kJitSamplesBatchSize) {
74 threshold = RoundUp(threshold, kJitSamplesBatchSize);
75 }
76 CHECK_LE(threshold, std::numeric_limits<uint16_t>::max());
77 return threshold;
78}
79
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010082 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000083
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000084 jit_options->code_cache_initial_capacity_ =
85 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
86 jit_options->code_cache_max_capacity_ =
87 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070088 jit_options->dump_info_on_shutdown_ =
89 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010090 jit_options->profile_saver_options_ =
91 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010092 jit_options->thread_pool_pthread_priority_ =
93 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000094
Andreas Gampe7897cec2017-07-19 16:28:59 -070095 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
96 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
97 } else {
98 jit_options->compile_threshold_ =
99 kIsDebugBuild
100 ? (StressModeHelper::kSlowMode
101 ? kJitSlowStressDefaultCompileThreshold
102 : kJitStressDefaultCompileThreshold)
103 : kJitDefaultCompileThreshold;
104 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000105 jit_options->compile_threshold_ = RoundUpThreshold(jit_options->compile_threshold_);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000106
107 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
108 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000109 } else {
110 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
111 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000112 jit_options->warmup_threshold_ = RoundUpThreshold(jit_options->warmup_threshold_);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000113
114 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
115 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000116 } else {
117 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
118 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000119 jit_options->osr_threshold_ =
120 RoundDown(std::numeric_limits<uint16_t>::max(), kJitSamplesBatchSize);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000121 }
122 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000123 jit_options->osr_threshold_ = RoundUpThreshold(jit_options->osr_threshold_);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +0000124
Calin Juravleb2771b42016-04-07 17:09:25 +0100125 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
126 jit_options->priority_thread_weight_ =
127 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
128 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
129 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
130 } else if (jit_options->priority_thread_weight_ == 0) {
131 LOG(FATAL) << "Priority thread weight cannot be 0.";
132 }
133 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100134 jit_options->priority_thread_weight_ = std::max(
135 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
136 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100137 }
138
Calin Juravle155ff3d2016-04-27 14:14:58 +0100139 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100140 jit_options->invoke_transition_weight_ =
141 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100142 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
143 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
144 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100145 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100146 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100147 } else {
148 jit_options->invoke_transition_weight_ = std::max(
149 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800150 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100151 }
152
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 return jit_options;
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 Geoffray075456e2018-12-14 08:54:21 +0000245bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool baseline, 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 Geoffray075456e2018-12-14 08:54:21 +0000275 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, baseline, 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
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800294void Jit::WaitForWorkersToBeCreated() {
295 if (thread_pool_ != nullptr) {
296 thread_pool_->WaitForWorkersToBeCreated();
297 }
298}
299
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800300void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100301 Thread* self = Thread::Current();
302 DCHECK(Runtime::Current()->IsShuttingDown(self));
303 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700304 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100305 {
306 ScopedSuspendAll ssa(__FUNCTION__);
307 // Clear thread_pool_ field while the threads are suspended.
308 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700309 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100310 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700311
312 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000313 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700314 pool->StopWorkers(self);
315 pool->RemoveAllTasks(self);
316 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100317 // We could just suspend all threads, but we know those threads
318 // will finish in a short period, so it's not worth adding a suspend logic
319 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700320 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800321 }
322}
323
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000324void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800325 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100326 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100327 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100328 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000329}
330
331void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100332 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
333 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100334 }
335}
336
Siva Chandra05d24152016-01-05 17:43:17 -0800337bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100338 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800339}
340
Nicolas Geoffray35122442016-03-02 12:05:30 +0000341bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
342 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
343}
344
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800345Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100346 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
347 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700348 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100349 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700350 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800351 DeleteThreadPool();
352 if (jit_compiler_handle_ != nullptr) {
353 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700354 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800355 }
356 if (jit_library_handle_ != nullptr) {
357 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700358 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800359 }
360}
361
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000362void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100363 if (!Runtime::Current()->UseJitCompilation()) {
364 // No need to notify if we only use the JIT to save profiles.
365 return;
366 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000367 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000368 if (jit_generate_debug_info_(jit->jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000369 DCHECK(jit->jit_types_loaded_ != nullptr);
370 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
371 }
372}
373
374void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
375 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100376 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700377 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000378 return true;
379 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800380 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000381 };
382
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000383 if (jit_generate_debug_info_(jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000384 ScopedObjectAccess so(Thread::Current());
385
386 CollectClasses visitor;
387 linker->VisitClasses(&visitor);
388 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000389 }
390}
391
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000392extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700393 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000394 const uint8_t* native_pc,
395 JValue* result,
396 const char* shorty,
397 Thread* self);
398
399bool Jit::MaybeDoOnStackReplacement(Thread* thread,
400 ArtMethod* method,
401 uint32_t dex_pc,
402 int32_t dex_pc_offset,
403 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000404 if (!kEnableOnStackReplacement) {
405 return false;
406 }
407
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000408 Jit* jit = Runtime::Current()->GetJit();
409 if (jit == nullptr) {
410 return false;
411 }
412
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000413 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
414 // Don't attempt to do an OSR if we are close to the stack limit. Since
415 // the interpreter frames are still on stack, OSR has the potential
416 // to stack overflow even for a simple loop.
417 // b/27094810.
418 return false;
419 }
420
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000421 // Get the actual Java method if this method is from a proxy class. The compiler
422 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700423 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000424
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000425 // Cheap check if the method has been compiled already. That's an indicator that we should
426 // osr into it.
427 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
428 return false;
429 }
430
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000431 // Fetch some data before looking up for an OSR method. We don't want thread
432 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
433 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000434 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800435 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000436 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700437 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000438 void** memory = nullptr;
439 size_t frame_size = 0;
440 ShadowFrame* shadow_frame = nullptr;
441 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000442
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000443 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700444 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000445 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
446 if (osr_method == nullptr) {
447 // No osr method yet, just return to the interpreter.
448 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000449 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000450
David Srbecky052f8ca2018-04-26 15:42:54 +0100451 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000452
453 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100454 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000455 if (!stack_map.IsValid()) {
456 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
457 // hope that the next branch has one.
458 return false;
459 }
460
Alex Light21611932017-09-26 13:07:39 -0700461 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
462 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
463 // disable OSR when single stepping, but that's currently hard to know at this point.
464 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700465 return false;
466 }
467
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000468 // We found a stack map, now fill the frame with dex register values from the interpreter's
469 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100470 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000471
472 frame_size = osr_method->GetFrameSizeInBytes();
473
474 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
475 // stack.
476 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
477 // but that is engineering complexity not worth the effort for something like OSR.
478 memory = reinterpret_cast<void**>(malloc(frame_size));
479 CHECK(memory != nullptr);
480 memset(memory, 0, frame_size);
481
482 // Art ABI: ArtMethod is at the bottom of the stack.
483 memory[0] = method;
484
485 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100486 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000487 // If we don't have a dex register map, then there are no live dex registers at
488 // this dex pc.
489 } else {
David Srbeckyfd89b072018-06-03 12:00:22 +0100490 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000491 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100492 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000493 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000494 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000495 continue;
496 }
497
498 if (location == DexRegisterLocation::Kind::kConstant) {
499 // We skip constants because the compiled code knows how to handle them.
500 continue;
501 }
502
David Srbecky7dc11782016-02-25 13:23:56 +0000503 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000504
505 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100506 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000507 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
508 DCHECK_GT(slot_offset, 0);
509 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
510 }
511 }
512
David Srbecky052f8ca2018-04-26 15:42:54 +0100513 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000514 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000515 VLOG(jit) << "Jumping to "
516 << method_name
517 << "@"
518 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000519 }
520
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000521 {
522 ManagedStack fragment;
523 thread->PushManagedStackFragment(&fragment);
524 (*art_quick_osr_stub)(memory,
525 frame_size,
526 native_pc,
527 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000528 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000529 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000530
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000531 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
532 thread->DeoptimizeWithDeoptimizationException(result);
533 }
534 thread->PopManagedStackFragment(fragment);
535 }
536 free(memory);
537 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000538 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000539 return true;
540}
541
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000542void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
543 if (bytes > 4 * MB) {
544 LOG(INFO) << "Compiler allocated "
545 << PrettySize(bytes)
546 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700547 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000548 }
549 MutexLock mu(Thread::Current(), lock_);
550 memory_use_.AddValue(bytes);
551}
552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100553class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100554 public:
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000555 enum class TaskKind {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100556 kAllocateProfile,
557 kCompile,
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000558 kCompileBaseline,
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000559 kCompileOsr,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100560 };
561
Andreas Gampe14bfedd2019-02-26 22:16:07 +0000562 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100563 ScopedObjectAccess soa(Thread::Current());
564 // Add a global ref to the class to prevent class unloading until compilation is done.
565 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
566 CHECK(klass_ != nullptr);
567 }
568
569 ~JitCompileTask() {
570 ScopedObjectAccess soa(Thread::Current());
571 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
572 }
573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100574 void Run(Thread* self) override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100575 ScopedObjectAccess soa(self);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000576 switch (kind_) {
577 case TaskKind::kCompile:
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000578 case TaskKind::kCompileBaseline:
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000579 case TaskKind::kCompileOsr: {
580 Runtime::Current()->GetJit()->CompileMethod(
581 method_,
582 self,
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000583 /* baseline= */ (kind_ == TaskKind::kCompileBaseline),
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000584 /* osr= */ (kind_ == TaskKind::kCompileOsr));
585 break;
586 }
587 case TaskKind::kAllocateProfile: {
588 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
589 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
590 }
591 break;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100592 }
593 }
Calin Juravlea2638922016-04-29 16:44:11 +0100594 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100595 }
596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100597 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100598 delete this;
599 }
600
601 private:
602 ArtMethod* const method_;
603 const TaskKind kind_;
604 jobject klass_;
605
606 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
607};
608
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000609void Jit::CreateThreadPool() {
610 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
611 // is not null when we instrument.
612
613 // We need peers as we may report the JIT thread, e.g., in the debugger.
614 constexpr bool kJitPoolNeedsPeers = true;
615 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
616
617 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
618 Start();
619}
620
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100621static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
622 if (method->IsClassInitializer() || !method->IsCompilable()) {
623 // We do not want to compile such methods.
624 return true;
625 }
626 if (method->IsNative()) {
627 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100628 if (klass == GetClassRoot<mirror::MethodHandle>() ||
629 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100630 // MethodHandle and VarHandle invocation methods are required to throw an
631 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
632 // implementations that arise the exception. We need to disable JIT compilation of these JNI
633 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
634 // stubs. Since these stubs have different stack representations we can then crash in stack
635 // walking (b/78151261).
636 return true;
637 }
638 }
639 return false;
640}
641
David Srbeckye3fc2d12018-11-30 13:41:14 +0000642bool Jit::MaybeCompileMethod(Thread* self,
643 ArtMethod* method,
644 uint32_t old_count,
645 uint32_t new_count,
646 bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100647 if (thread_pool_ == nullptr) {
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000648 // Should only see this when shutting down, starting up, or in safe mode.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000649 DCHECK(Runtime::Current()->IsShuttingDown(self) ||
650 !Runtime::Current()->IsFinishedStarting() ||
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000651 Runtime::Current()->IsSafeMode());
David Srbeckye3fc2d12018-11-30 13:41:14 +0000652 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100653 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100654 if (IgnoreSamplesForMethod(method)) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000655 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100656 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100657 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000658 // Tests might request JIT on first use (compiled synchronously in the interpreter).
David Srbeckye3fc2d12018-11-30 13:41:14 +0000659 return false;
David Srbeckyf4886df2017-12-11 16:06:29 +0000660 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100661 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100662 DCHECK_GT(WarmMethodThreshold(), 0);
663 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
664 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
665 DCHECK_GE(PriorityThreadWeight(), 1);
666 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100667
David Srbeckye3fc2d12018-11-30 13:41:14 +0000668 if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) {
669 // Note: Native method have no "warm" state or profiling info.
670 if (!method->IsNative() && method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700671 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000672 if (success) {
673 VLOG(jit) << "Start profiling " << method->PrettyMethod();
674 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100675
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000676 if (thread_pool_ == nullptr) {
677 // Calling ProfilingInfo::Create might put us in a suspended state, which could
678 // lead to the thread pool being deleted when we are shutting down.
679 DCHECK(Runtime::Current()->IsShuttingDown(self));
David Srbeckye3fc2d12018-11-30 13:41:14 +0000680 return false;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000681 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100682
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000683 if (!success) {
684 // We failed allocating. Instead of doing the collection on the Java thread, we push
685 // an allocation to a compiler thread, that will do the collection.
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000686 thread_pool_->AddTask(
687 self, new JitCompileTask(method, JitCompileTask::TaskKind::kAllocateProfile));
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000688 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100689 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000690 }
691 if (UseJitCompilation()) {
692 if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) {
693 if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Calin Juravleffc87072016-04-20 14:22:09 +0100694 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000695 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompile));
Calin Juravleffc87072016-04-20 14:22:09 +0100696 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000697 }
698 if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100699 if (!with_backedges) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000700 return false;
Calin Juravleffc87072016-04-20 14:22:09 +0100701 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000702 DCHECK(!method->IsNative()); // No back edges reported for native methods.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000703 if (!code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100704 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000705 thread_pool_->AddTask(
706 self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr));
Calin Juravleffc87072016-04-20 14:22:09 +0100707 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100708 }
709 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000710 return true;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100711}
712
Alex Light59b950f2018-10-08 10:43:06 -0700713class ScopedSetRuntimeThread {
714 public:
715 explicit ScopedSetRuntimeThread(Thread* self)
716 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
717 self_->SetIsRuntimeThread(true);
718 }
719
720 ~ScopedSetRuntimeThread() {
721 self_->SetIsRuntimeThread(was_runtime_thread_);
722 }
723
724 private:
725 Thread* self_;
726 bool was_runtime_thread_;
727};
728
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100729void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100730 Runtime* runtime = Runtime::Current();
731 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000732 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000733 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000734 if (!np_method->IsNative()) {
735 // The compiler requires a ProfilingInfo object for non-native methods.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700736 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
Vladimir Markof8655b32018-03-21 17:53:56 +0000737 }
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000738 JitCompileTask compile_task(method, JitCompileTask::TaskKind::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -0700739 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
740 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000741 compile_task.Run(thread);
742 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100743 return;
744 }
745
Andreas Gampe542451c2016-07-26 09:02:02 -0700746 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100747 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -0700748 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
749 // must remain the instrumentation entrypoint.
750 if ((profiling_info != nullptr) &&
751 (profiling_info->GetSavedEntryPoint() != nullptr) &&
752 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100753 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
754 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100755 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700756 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100757 }
758}
759
Mathieu Chartieref41db72016-10-25 15:08:01 -0700760void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100761 ArtMethod* caller,
762 uint32_t dex_pc,
763 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700764 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100765 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700766 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100767 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100768 info->AddInvokeInfo(dex_pc, this_object->GetClass());
769 }
770}
771
772void Jit::WaitForCompilationToFinish(Thread* self) {
773 if (thread_pool_ != nullptr) {
774 thread_pool_->Wait(self, false, false);
775 }
776}
777
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000778void Jit::Stop() {
779 Thread* self = Thread::Current();
780 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
781 WaitForCompilationToFinish(self);
782 GetThreadPool()->StopWorkers(self);
783 WaitForCompilationToFinish(self);
784}
785
786void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000787 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000788}
789
Andreas Gampef149b3f2016-11-16 14:58:24 -0800790ScopedJitSuspend::ScopedJitSuspend() {
791 jit::Jit* jit = Runtime::Current()->GetJit();
792 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
793 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000794 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800795 }
796}
797
798ScopedJitSuspend::~ScopedJitSuspend() {
799 if (was_on_) {
800 DCHECK(Runtime::Current()->GetJit() != nullptr);
801 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000802 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800803 }
804}
805
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000806void Jit::PostForkChildAction(bool is_zygote) {
807 if (is_zygote) {
808 // Don't transition if this is for a child zygote.
809 return;
810 }
811 if (Runtime::Current()->IsSafeMode()) {
812 // Delete the thread pool, we are not going to JIT.
813 thread_pool_.reset(nullptr);
814 return;
815 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000816 // At this point, the compiler options have been adjusted to the particular configuration
817 // of the forked child. Parse them again.
818 jit_update_options_(jit_compiler_handle_);
819
820 // Adjust the status of code cache collection: the status from zygote was to not collect.
821 code_cache_->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
822 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000823
824 if (thread_pool_ != nullptr) {
825 // Remove potential tasks that have been inherited from the zygote.
826 thread_pool_->RemoveAllTasks(Thread::Current());
827
828 // Resume JIT compilation.
829 thread_pool_->CreateThreads();
830 }
831}
832
833void Jit::PreZygoteFork() {
834 if (thread_pool_ == nullptr) {
835 return;
836 }
837 thread_pool_->DeleteThreads();
838}
839
840void Jit::PostZygoteFork() {
841 if (thread_pool_ == nullptr) {
842 return;
843 }
844 thread_pool_->CreateThreads();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000845}
846
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800847} // namespace jit
848} // namespace art