blob: 8d542d3554659f3a21ab4edded866c205a465bd1 [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;
62bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
63void (*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 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
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800294void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100295 Thread* self = Thread::Current();
296 DCHECK(Runtime::Current()->IsShuttingDown(self));
297 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700298 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100299 {
300 ScopedSuspendAll ssa(__FUNCTION__);
301 // Clear thread_pool_ field while the threads are suspended.
302 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700303 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100304 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700305
306 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000307 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700308 pool->StopWorkers(self);
309 pool->RemoveAllTasks(self);
310 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100311 // We could just suspend all threads, but we know those threads
312 // will finish in a short period, so it's not worth adding a suspend logic
313 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700314 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800315 }
316}
317
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000318void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800319 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100320 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100321 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100322 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000323}
324
325void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100326 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
327 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100328 }
329}
330
Siva Chandra05d24152016-01-05 17:43:17 -0800331bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100332 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800333}
334
Nicolas Geoffray35122442016-03-02 12:05:30 +0000335bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
336 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
337}
338
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800339Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100340 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
341 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700342 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100343 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700344 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800345 DeleteThreadPool();
346 if (jit_compiler_handle_ != nullptr) {
347 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700348 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800349 }
350 if (jit_library_handle_ != nullptr) {
351 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700352 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800353 }
354}
355
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000356void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100357 if (!Runtime::Current()->UseJitCompilation()) {
358 // No need to notify if we only use the JIT to save profiles.
359 return;
360 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000361 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000362 if (jit_generate_debug_info_(jit->jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000363 DCHECK(jit->jit_types_loaded_ != nullptr);
364 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
365 }
366}
367
368void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
369 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100370 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700371 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000372 return true;
373 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800374 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000375 };
376
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000377 if (jit_generate_debug_info_(jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000378 ScopedObjectAccess so(Thread::Current());
379
380 CollectClasses visitor;
381 linker->VisitClasses(&visitor);
382 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000383 }
384}
385
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000386extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700387 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000388 const uint8_t* native_pc,
389 JValue* result,
390 const char* shorty,
391 Thread* self);
392
393bool Jit::MaybeDoOnStackReplacement(Thread* thread,
394 ArtMethod* method,
395 uint32_t dex_pc,
396 int32_t dex_pc_offset,
397 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000398 if (!kEnableOnStackReplacement) {
399 return false;
400 }
401
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000402 Jit* jit = Runtime::Current()->GetJit();
403 if (jit == nullptr) {
404 return false;
405 }
406
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000407 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
408 // Don't attempt to do an OSR if we are close to the stack limit. Since
409 // the interpreter frames are still on stack, OSR has the potential
410 // to stack overflow even for a simple loop.
411 // b/27094810.
412 return false;
413 }
414
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000415 // Get the actual Java method if this method is from a proxy class. The compiler
416 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700417 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000418
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000419 // Cheap check if the method has been compiled already. That's an indicator that we should
420 // osr into it.
421 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
422 return false;
423 }
424
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000425 // Fetch some data before looking up for an OSR method. We don't want thread
426 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
427 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000428 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800429 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000430 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700431 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000432 void** memory = nullptr;
433 size_t frame_size = 0;
434 ShadowFrame* shadow_frame = nullptr;
435 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000436
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000437 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700438 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000439 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
440 if (osr_method == nullptr) {
441 // No osr method yet, just return to the interpreter.
442 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000443 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000444
David Srbecky052f8ca2018-04-26 15:42:54 +0100445 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000446
447 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100448 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000449 if (!stack_map.IsValid()) {
450 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
451 // hope that the next branch has one.
452 return false;
453 }
454
Alex Light21611932017-09-26 13:07:39 -0700455 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
456 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
457 // disable OSR when single stepping, but that's currently hard to know at this point.
458 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700459 return false;
460 }
461
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000462 // We found a stack map, now fill the frame with dex register values from the interpreter's
463 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100464 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000465
466 frame_size = osr_method->GetFrameSizeInBytes();
467
468 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
469 // stack.
470 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
471 // but that is engineering complexity not worth the effort for something like OSR.
472 memory = reinterpret_cast<void**>(malloc(frame_size));
473 CHECK(memory != nullptr);
474 memset(memory, 0, frame_size);
475
476 // Art ABI: ArtMethod is at the bottom of the stack.
477 memory[0] = method;
478
479 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100480 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000481 // If we don't have a dex register map, then there are no live dex registers at
482 // this dex pc.
483 } else {
David Srbeckyfd89b072018-06-03 12:00:22 +0100484 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000485 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100486 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000487 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000488 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000489 continue;
490 }
491
492 if (location == DexRegisterLocation::Kind::kConstant) {
493 // We skip constants because the compiled code knows how to handle them.
494 continue;
495 }
496
David Srbecky7dc11782016-02-25 13:23:56 +0000497 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000498
499 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100500 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000501 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
502 DCHECK_GT(slot_offset, 0);
503 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
504 }
505 }
506
David Srbecky052f8ca2018-04-26 15:42:54 +0100507 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000508 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000509 VLOG(jit) << "Jumping to "
510 << method_name
511 << "@"
512 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000513 }
514
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000515 {
516 ManagedStack fragment;
517 thread->PushManagedStackFragment(&fragment);
518 (*art_quick_osr_stub)(memory,
519 frame_size,
520 native_pc,
521 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000522 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000523 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000524
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000525 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
526 thread->DeoptimizeWithDeoptimizationException(result);
527 }
528 thread->PopManagedStackFragment(fragment);
529 }
530 free(memory);
531 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000532 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000533 return true;
534}
535
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000536void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
537 if (bytes > 4 * MB) {
538 LOG(INFO) << "Compiler allocated "
539 << PrettySize(bytes)
540 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700541 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000542 }
543 MutexLock mu(Thread::Current(), lock_);
544 memory_use_.AddValue(bytes);
545}
546
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100547class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100548 public:
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000549 enum class TaskKind {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100550 kAllocateProfile,
551 kCompile,
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000552 kCompileOsr,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100553 };
554
555 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
556 ScopedObjectAccess soa(Thread::Current());
557 // Add a global ref to the class to prevent class unloading until compilation is done.
558 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
559 CHECK(klass_ != nullptr);
560 }
561
562 ~JitCompileTask() {
563 ScopedObjectAccess soa(Thread::Current());
564 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
565 }
566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100567 void Run(Thread* self) override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100568 ScopedObjectAccess soa(self);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000569 switch (kind_) {
570 case TaskKind::kCompile:
571 case TaskKind::kCompileOsr: {
572 Runtime::Current()->GetJit()->CompileMethod(
573 method_,
574 self,
575 /* osr= */ (kind_ == TaskKind::kCompileOsr));
576 break;
577 }
578 case TaskKind::kAllocateProfile: {
579 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
580 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
581 }
582 break;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100583 }
584 }
Calin Juravlea2638922016-04-29 16:44:11 +0100585 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100586 }
587
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100588 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100589 delete this;
590 }
591
592 private:
593 ArtMethod* const method_;
594 const TaskKind kind_;
595 jobject klass_;
596
597 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
598};
599
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000600void Jit::CreateThreadPool() {
601 // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
602 // is not null when we instrument.
603
604 // We need peers as we may report the JIT thread, e.g., in the debugger.
605 constexpr bool kJitPoolNeedsPeers = true;
606 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
607
608 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
609 Start();
610}
611
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100612static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
613 if (method->IsClassInitializer() || !method->IsCompilable()) {
614 // We do not want to compile such methods.
615 return true;
616 }
617 if (method->IsNative()) {
618 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100619 if (klass == GetClassRoot<mirror::MethodHandle>() ||
620 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100621 // MethodHandle and VarHandle invocation methods are required to throw an
622 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
623 // implementations that arise the exception. We need to disable JIT compilation of these JNI
624 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
625 // stubs. Since these stubs have different stack representations we can then crash in stack
626 // walking (b/78151261).
627 return true;
628 }
629 }
630 return false;
631}
632
David Srbeckye3fc2d12018-11-30 13:41:14 +0000633bool Jit::MaybeCompileMethod(Thread* self,
634 ArtMethod* method,
635 uint32_t old_count,
636 uint32_t new_count,
637 bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100638 if (thread_pool_ == nullptr) {
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000639 // Should only see this when shutting down, starting up, or in safe mode.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000640 DCHECK(Runtime::Current()->IsShuttingDown(self) ||
641 !Runtime::Current()->IsFinishedStarting() ||
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000642 Runtime::Current()->IsSafeMode());
David Srbeckye3fc2d12018-11-30 13:41:14 +0000643 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100644 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100645 if (IgnoreSamplesForMethod(method)) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000646 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100647 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100648 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000649 // Tests might request JIT on first use (compiled synchronously in the interpreter).
David Srbeckye3fc2d12018-11-30 13:41:14 +0000650 return false;
David Srbeckyf4886df2017-12-11 16:06:29 +0000651 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100652 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100653 DCHECK_GT(WarmMethodThreshold(), 0);
654 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
655 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
656 DCHECK_GE(PriorityThreadWeight(), 1);
657 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100658
David Srbeckye3fc2d12018-11-30 13:41:14 +0000659 if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) {
660 // Note: Native method have no "warm" state or profiling info.
661 if (!method->IsNative() && method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700662 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000663 if (success) {
664 VLOG(jit) << "Start profiling " << method->PrettyMethod();
665 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100666
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000667 if (thread_pool_ == nullptr) {
668 // Calling ProfilingInfo::Create might put us in a suspended state, which could
669 // lead to the thread pool being deleted when we are shutting down.
670 DCHECK(Runtime::Current()->IsShuttingDown(self));
David Srbeckye3fc2d12018-11-30 13:41:14 +0000671 return false;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000672 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100673
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000674 if (!success) {
675 // We failed allocating. Instead of doing the collection on the Java thread, we push
676 // an allocation to a compiler thread, that will do the collection.
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000677 thread_pool_->AddTask(
678 self, new JitCompileTask(method, JitCompileTask::TaskKind::kAllocateProfile));
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000679 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100680 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000681 }
682 if (UseJitCompilation()) {
683 if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) {
684 if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Calin Juravleffc87072016-04-20 14:22:09 +0100685 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000686 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompile));
Calin Juravleffc87072016-04-20 14:22:09 +0100687 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000688 }
689 if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100690 if (!with_backedges) {
David Srbeckye3fc2d12018-11-30 13:41:14 +0000691 return false;
Calin Juravleffc87072016-04-20 14:22:09 +0100692 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000693 DCHECK(!method->IsNative()); // No back edges reported for native methods.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000694 if (!code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100695 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000696 thread_pool_->AddTask(
697 self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr));
Calin Juravleffc87072016-04-20 14:22:09 +0100698 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100699 }
700 }
David Srbeckye3fc2d12018-11-30 13:41:14 +0000701 return true;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100702}
703
Alex Light59b950f2018-10-08 10:43:06 -0700704class ScopedSetRuntimeThread {
705 public:
706 explicit ScopedSetRuntimeThread(Thread* self)
707 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
708 self_->SetIsRuntimeThread(true);
709 }
710
711 ~ScopedSetRuntimeThread() {
712 self_->SetIsRuntimeThread(was_runtime_thread_);
713 }
714
715 private:
716 Thread* self_;
717 bool was_runtime_thread_;
718};
719
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100720void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100721 Runtime* runtime = Runtime::Current();
722 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000723 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000724 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000725 if (!np_method->IsNative()) {
726 // The compiler requires a ProfilingInfo object for non-native methods.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700727 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
Vladimir Markof8655b32018-03-21 17:53:56 +0000728 }
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000729 JitCompileTask compile_task(method, JitCompileTask::TaskKind::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -0700730 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
731 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000732 compile_task.Run(thread);
733 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100734 return;
735 }
736
Andreas Gampe542451c2016-07-26 09:02:02 -0700737 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100738 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -0700739 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
740 // must remain the instrumentation entrypoint.
741 if ((profiling_info != nullptr) &&
742 (profiling_info->GetSavedEntryPoint() != nullptr) &&
743 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100744 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
745 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100746 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700747 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100748 }
749}
750
Mathieu Chartieref41db72016-10-25 15:08:01 -0700751void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100752 ArtMethod* caller,
753 uint32_t dex_pc,
754 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700755 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100756 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700757 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100758 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100759 info->AddInvokeInfo(dex_pc, this_object->GetClass());
760 }
761}
762
763void Jit::WaitForCompilationToFinish(Thread* self) {
764 if (thread_pool_ != nullptr) {
765 thread_pool_->Wait(self, false, false);
766 }
767}
768
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000769void Jit::Stop() {
770 Thread* self = Thread::Current();
771 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
772 WaitForCompilationToFinish(self);
773 GetThreadPool()->StopWorkers(self);
774 WaitForCompilationToFinish(self);
775}
776
777void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000778 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000779}
780
Andreas Gampef149b3f2016-11-16 14:58:24 -0800781ScopedJitSuspend::ScopedJitSuspend() {
782 jit::Jit* jit = Runtime::Current()->GetJit();
783 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
784 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000785 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800786 }
787}
788
789ScopedJitSuspend::~ScopedJitSuspend() {
790 if (was_on_) {
791 DCHECK(Runtime::Current()->GetJit() != nullptr);
792 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000793 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800794 }
795}
796
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000797void Jit::PostForkChildAction(bool is_zygote) {
798 if (is_zygote) {
799 // Don't transition if this is for a child zygote.
800 return;
801 }
802 if (Runtime::Current()->IsSafeMode()) {
803 // Delete the thread pool, we are not going to JIT.
804 thread_pool_.reset(nullptr);
805 return;
806 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000807 // At this point, the compiler options have been adjusted to the particular configuration
808 // of the forked child. Parse them again.
809 jit_update_options_(jit_compiler_handle_);
810
811 // Adjust the status of code cache collection: the status from zygote was to not collect.
812 code_cache_->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
813 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000814
815 if (thread_pool_ != nullptr) {
816 // Remove potential tasks that have been inherited from the zygote.
817 thread_pool_->RemoveAllTasks(Thread::Current());
818
819 // Resume JIT compilation.
820 thread_pool_->CreateThreads();
821 }
822}
823
824void Jit::PreZygoteFork() {
825 if (thread_pool_ == nullptr) {
826 return;
827 }
828 thread_pool_->DeleteThreads();
829}
830
831void Jit::PostZygoteFork() {
832 if (thread_pool_ == nullptr) {
833 return;
834 }
835 thread_pool_->CreateThreads();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000836}
837
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800838} // namespace jit
839} // namespace art