blob: 2fc418b7d863ba20dff280d5f49fdd02274017b4 [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 Srbeckydb94f2b2018-11-09 12:58:28 +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
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080073 auto* jit_options = new JitOptions;
Calin Juravleffc87072016-04-20 14:22:09 +010074 jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000075
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000076 jit_options->code_cache_initial_capacity_ =
77 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
78 jit_options->code_cache_max_capacity_ =
79 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070080 jit_options->dump_info_on_shutdown_ =
81 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle138dbff2016-06-28 19:36:58 +010082 jit_options->profile_saver_options_ =
83 options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
Nicolas Geoffray47b95802018-05-16 15:42:17 +010084 jit_options->thread_pool_pthread_priority_ =
85 options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000086
Andreas Gampe7897cec2017-07-19 16:28:59 -070087 if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
88 jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
89 } else {
90 jit_options->compile_threshold_ =
91 kIsDebugBuild
92 ? (StressModeHelper::kSlowMode
93 ? kJitSlowStressDefaultCompileThreshold
94 : kJitStressDefaultCompileThreshold)
95 : kJitDefaultCompileThreshold;
96 }
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000097 if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
98 LOG(FATAL) << "Method compilation threshold is above its internal limit.";
99 }
100
101 if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
102 jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
103 if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
104 LOG(FATAL) << "Method warmup threshold is above its internal limit.";
105 }
106 } else {
107 jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
108 }
109
110 if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
111 jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
112 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
113 LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
114 }
115 } else {
116 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
117 if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
118 jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
119 }
120 }
121
Calin Juravleb2771b42016-04-07 17:09:25 +0100122 if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
123 jit_options->priority_thread_weight_ =
124 *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
125 if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
126 LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
127 } else if (jit_options->priority_thread_weight_ == 0) {
128 LOG(FATAL) << "Priority thread weight cannot be 0.";
129 }
130 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100131 jit_options->priority_thread_weight_ = std::max(
132 jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
133 static_cast<size_t>(1));
Calin Juravleb2771b42016-04-07 17:09:25 +0100134 }
135
Calin Juravle155ff3d2016-04-27 14:14:58 +0100136 if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100137 jit_options->invoke_transition_weight_ =
138 *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
Calin Juravle155ff3d2016-04-27 14:14:58 +0100139 if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
140 LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
141 } else if (jit_options->invoke_transition_weight_ == 0) {
Nicolas Geoffray7c9f3ba2016-05-06 16:52:36 +0100142 LOG(FATAL) << "Invoke transition weight cannot be 0.";
Calin Juravle155ff3d2016-04-27 14:14:58 +0100143 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100144 } else {
145 jit_options->invoke_transition_weight_ = std::max(
146 jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
Mathieu Chartier6beced42016-11-15 15:51:31 -0800147 static_cast<size_t>(1));
Calin Juravle155ff3d2016-04-27 14:14:58 +0100148 }
149
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800150 return jit_options;
151}
152
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700153void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000154 code_cache_->Dump(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700155 cumulative_timings_.Dump(os);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000156 MutexLock mu(Thread::Current(), lock_);
157 memory_use_.PrintMemoryUse(os);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700158}
159
Calin Juravleb8e69992016-03-09 15:37:48 +0000160void Jit::DumpForSigQuit(std::ostream& os) {
161 DumpInfo(os);
162 ProfileSaver::DumpInstanceInfo(os);
163}
164
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700165void Jit::AddTimingLogger(const TimingLogger& logger) {
166 cumulative_timings_.AddLogger(logger);
167}
168
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100169Jit::Jit(JitCodeCache* code_cache, JitOptions* options)
170 : code_cache_(code_cache),
171 options_(options),
172 cumulative_timings_("JIT timings"),
173 memory_use_("Memory used for compilation", 16),
174 lock_("JIT memory use lock") {}
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100176Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) {
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000177 if (jit_load_ == nullptr) {
178 LOG(WARNING) << "Not creating JIT: library not loaded";
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179 return nullptr;
180 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000181 jit_compiler_handle_ = (jit_load_)();
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000182 if (jit_compiler_handle_ == nullptr) {
183 LOG(WARNING) << "Not creating JIT: failed to allocate a compiler";
184 return nullptr;
185 }
186 std::unique_ptr<Jit> jit(new Jit(code_cache, options));
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000187
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000188 // If the code collector is enabled, check if that still holds:
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000189 // With 'perf', we want a 1-1 mapping between an address and a method.
190 // We aren't able to keep method pointers live during the instrumentation method entry trampoline
191 // so we will just disable jit-gc if we are doing that.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000192 if (code_cache->GetGarbageCollectCode()) {
193 code_cache->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
194 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
195 }
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100196
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000197 VLOG(jit) << "JIT created with initial_capacity="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000198 << PrettySize(options->GetCodeCacheInitialCapacity())
199 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000200 << ", compile_threshold=" << options->GetCompileThreshold()
Calin Juravle138dbff2016-06-28 19:36:58 +0100201 << ", profile_saver_options=" << options->GetProfileSaverOptions();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100202
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100203 // Notify native debugger about the classes already loaded before the creation of the jit.
204 jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205 return jit.release();
206}
207
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000208template <typename T>
209bool Jit::LoadSymbol(T* address, const char* name, std::string* error_msg) {
210 *address = reinterpret_cast<T>(dlsym(jit_library_handle_, name));
211 if (*address == nullptr) {
212 *error_msg = std::string("JIT couldn't find ") + name + std::string(" entry point");
213 return false;
214 }
215 return true;
216}
217
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000218bool Jit::LoadCompilerLibrary(std::string* error_msg) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219 jit_library_handle_ = dlopen(
220 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
221 if (jit_library_handle_ == nullptr) {
222 std::ostringstream oss;
223 oss << "JIT could not load libart-compiler.so: " << dlerror();
224 *error_msg = oss.str();
225 return false;
226 }
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000227 bool all_resolved = true;
228 all_resolved = all_resolved && LoadSymbol(&jit_load_, "jit_load", error_msg);
229 all_resolved = all_resolved && LoadSymbol(&jit_unload_, "jit_unload", error_msg);
230 all_resolved = all_resolved && LoadSymbol(&jit_compile_method_, "jit_compile_method", error_msg);
231 all_resolved = all_resolved && LoadSymbol(&jit_types_loaded_, "jit_types_loaded", error_msg);
232 all_resolved = all_resolved && LoadSymbol(&jit_update_options_, "jit_update_options", error_msg);
233 all_resolved = all_resolved &&
234 LoadSymbol(&jit_generate_debug_info_, "jit_generate_debug_info", error_msg);
235 if (!all_resolved) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800236 dlclose(jit_library_handle_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000237 return false;
238 }
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700239 return true;
240}
241
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000242bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Calin Juravleffc87072016-04-20 14:22:09 +0100243 DCHECK(Runtime::Current()->UseJitCompilation());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800244 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000245
Alex Light0fa17862017-10-24 13:43:05 -0700246 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100247 // Don't compile the method if it has breakpoints.
Alex Light0fa17862017-10-24 13:43:05 -0700248 if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
249 VLOG(jit) << "JIT not compiling " << method->PrettyMethod()
250 << " due to not being safe to jit according to runtime-callbacks. For example, there"
251 << " could be breakpoints in this method.";
Mathieu Chartierd8565452015-03-26 09:41:50 -0700252 return false;
253 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100254
255 // Don't compile the method if we are supposed to be deoptimized.
256 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
257 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
David Sehr709b0702016-10-13 09:12:37 -0700258 VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100259 return false;
260 }
261
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000262 // If we get a request to compile a proxy method, we pass the actual Java method
263 // of that proxy method, as the compiler does not expect a proxy method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700264 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000265 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100266 return false;
267 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100268
269 VLOG(jit) << "Compiling method "
David Sehr709b0702016-10-13 09:12:37 -0700270 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100271 << " osr=" << std::boolalpha << osr;
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000272 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
buzbee454b3b62016-04-07 14:42:47 -0700273 code_cache_->DoneCompiling(method_to_compile, self, osr);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100274 if (!success) {
275 VLOG(jit) << "Failed to compile method "
David Sehr709b0702016-10-13 09:12:37 -0700276 << ArtMethod::PrettyMethod(method_to_compile)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100277 << " osr=" << std::boolalpha << osr;
278 }
Andreas Gampe320ba912016-11-18 17:39:45 -0800279 if (kIsDebugBuild) {
280 if (self->IsExceptionPending()) {
281 mirror::Throwable* exception = self->GetException();
282 LOG(FATAL) << "No pending exception expected after compiling "
283 << ArtMethod::PrettyMethod(method)
284 << ": "
285 << exception->Dump();
286 }
287 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100288 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800289}
290
291void Jit::CreateThreadPool() {
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000292 if (Runtime::Current()->IsSafeMode()) {
293 // Never create the pool in safe mode.
294 return;
295 }
296 // There is a DCHECK in the 'AddSamples' method to ensure the thread pool
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100297 // is not null when we instrument.
Andreas Gampe4471e4f2017-01-30 16:40:49 +0000298
299 // We need peers as we may report the JIT thread, e.g., in the debugger.
300 constexpr bool kJitPoolNeedsPeers = true;
301 thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
302
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100303 thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000304 Start();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800305}
306
307void Jit::DeleteThreadPool() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100308 Thread* self = Thread::Current();
309 DCHECK(Runtime::Current()->IsShuttingDown(self));
310 if (thread_pool_ != nullptr) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700311 std::unique_ptr<ThreadPool> pool;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100312 {
313 ScopedSuspendAll ssa(__FUNCTION__);
314 // Clear thread_pool_ field while the threads are suspended.
315 // A mutator in the 'AddSamples' method will check against it.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700316 pool = std::move(thread_pool_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100317 }
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700318
319 // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
Roland Levillain05e34f42018-05-24 13:19:05 +0000320 if (!kRunningOnMemoryTool) {
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700321 pool->StopWorkers(self);
322 pool->RemoveAllTasks(self);
323 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100324 // We could just suspend all threads, but we know those threads
325 // will finish in a short period, so it's not worth adding a suspend logic
326 // here. Besides, this is only done for shutdown.
Andreas Gampe0897e1c2017-05-16 08:36:56 -0700327 pool->Wait(self, false, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800328 }
329}
330
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000331void Jit::StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800332 const std::vector<std::string>& code_paths) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100333 if (options_->GetSaveProfilingInfo()) {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100334 ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100335 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000336}
337
338void Jit::StopProfileSaver() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100339 if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) {
340 ProfileSaver::Stop(options_->DumpJitInfoOnShutdown());
Calin Juravle31f2c152015-10-23 17:56:15 +0100341 }
342}
343
Siva Chandra05d24152016-01-05 17:43:17 -0800344bool Jit::JitAtFirstUse() {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100345 return HotMethodThreshold() == 0;
Siva Chandra05d24152016-01-05 17:43:17 -0800346}
347
Nicolas Geoffray35122442016-03-02 12:05:30 +0000348bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
349 return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
350}
351
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800352Jit::~Jit() {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100353 DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted());
354 if (options_->DumpJitInfoOnShutdown()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700355 DumpInfo(LOG_STREAM(INFO));
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100356 Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700357 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800358 DeleteThreadPool();
359 if (jit_compiler_handle_ != nullptr) {
360 jit_unload_(jit_compiler_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700361 jit_compiler_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800362 }
363 if (jit_library_handle_ != nullptr) {
364 dlclose(jit_library_handle_);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700365 jit_library_handle_ = nullptr;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800366 }
367}
368
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000369void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
Calin Juravleffc87072016-04-20 14:22:09 +0100370 if (!Runtime::Current()->UseJitCompilation()) {
371 // No need to notify if we only use the JIT to save profiles.
372 return;
373 }
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000374 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000375 if (jit_generate_debug_info_(jit->jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000376 DCHECK(jit->jit_types_loaded_ != nullptr);
377 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
378 }
379}
380
381void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
382 struct CollectClasses : public ClassVisitor {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100383 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700384 classes_.push_back(klass.Ptr());
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000385 return true;
386 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800387 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000388 };
389
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000390 if (jit_generate_debug_info_(jit_compiler_handle_)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000391 ScopedObjectAccess so(Thread::Current());
392
393 CollectClasses visitor;
394 linker->VisitClasses(&visitor);
395 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000396 }
397}
398
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000399extern "C" void art_quick_osr_stub(void** stack,
Evgenii Stepanov6d90fde2018-09-04 17:50:38 -0700400 size_t stack_size_in_bytes,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000401 const uint8_t* native_pc,
402 JValue* result,
403 const char* shorty,
404 Thread* self);
405
406bool Jit::MaybeDoOnStackReplacement(Thread* thread,
407 ArtMethod* method,
408 uint32_t dex_pc,
409 int32_t dex_pc_offset,
410 JValue* result) {
Nicolas Geoffraye8662132016-02-15 10:00:42 +0000411 if (!kEnableOnStackReplacement) {
412 return false;
413 }
414
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000415 Jit* jit = Runtime::Current()->GetJit();
416 if (jit == nullptr) {
417 return false;
418 }
419
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000420 if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
421 // Don't attempt to do an OSR if we are close to the stack limit. Since
422 // the interpreter frames are still on stack, OSR has the potential
423 // to stack overflow even for a simple loop.
424 // b/27094810.
425 return false;
426 }
427
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000428 // Get the actual Java method if this method is from a proxy class. The compiler
429 // and the JIT code cache do not expect methods from proxy classes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700430 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000431
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000432 // Cheap check if the method has been compiled already. That's an indicator that we should
433 // osr into it.
434 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
435 return false;
436 }
437
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000438 // Fetch some data before looking up for an OSR method. We don't want thread
439 // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
440 // method while we are being suspended.
David Sehr0225f8e2018-01-31 08:52:24 +0000441 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800442 const size_t number_of_vregs = accessor.RegistersSize();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000443 const char* shorty = method->GetShorty();
David Sehr709b0702016-10-13 09:12:37 -0700444 std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000445 void** memory = nullptr;
446 size_t frame_size = 0;
447 ShadowFrame* shadow_frame = nullptr;
448 const uint8_t* native_pc = nullptr;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000449
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000450 {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700451 ScopedAssertNoThreadSuspension sts("Holding OSR method");
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000452 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
453 if (osr_method == nullptr) {
454 // No osr method yet, just return to the interpreter.
455 return false;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000456 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000457
David Srbecky052f8ca2018-04-26 15:42:54 +0100458 CodeInfo code_info(osr_method);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000459
460 // Find stack map starting at the target dex_pc.
David Srbecky052f8ca2018-04-26 15:42:54 +0100461 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000462 if (!stack_map.IsValid()) {
463 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
464 // hope that the next branch has one.
465 return false;
466 }
467
Alex Light21611932017-09-26 13:07:39 -0700468 // Before allowing the jump, make sure no code is actively inspecting the method to avoid
469 // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively
470 // disable OSR when single stepping, but that's currently hard to know at this point.
471 if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) {
Aart Bik29bdaee2016-05-18 15:44:07 -0700472 return false;
473 }
474
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000475 // We found a stack map, now fill the frame with dex register values from the interpreter's
476 // shadow frame.
David Srbeckyfd89b072018-06-03 12:00:22 +0100477 DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000478
479 frame_size = osr_method->GetFrameSizeInBytes();
480
481 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
482 // stack.
483 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
484 // but that is engineering complexity not worth the effort for something like OSR.
485 memory = reinterpret_cast<void**>(malloc(frame_size));
486 CHECK(memory != nullptr);
487 memset(memory, 0, frame_size);
488
489 // Art ABI: ArtMethod is at the bottom of the stack.
490 memory[0] = method;
491
492 shadow_frame = thread->PopShadowFrame();
David Srbeckyfd89b072018-06-03 12:00:22 +0100493 if (vreg_map.empty()) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000494 // If we don't have a dex register map, then there are no live dex registers at
495 // this dex pc.
496 } else {
David Srbeckyfd89b072018-06-03 12:00:22 +0100497 DCHECK_EQ(vreg_map.size(), number_of_vregs);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000498 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
David Srbeckye1402122018-06-13 18:20:45 +0100499 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000500 if (location == DexRegisterLocation::Kind::kNone) {
Nicolas Geoffrayc0b27962016-02-16 12:06:05 +0000501 // Dex register is dead or uninitialized.
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000502 continue;
503 }
504
505 if (location == DexRegisterLocation::Kind::kConstant) {
506 // We skip constants because the compiled code knows how to handle them.
507 continue;
508 }
509
David Srbecky7dc11782016-02-25 13:23:56 +0000510 DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000511
512 int32_t vreg_value = shadow_frame->GetVReg(vreg);
David Srbeckye1402122018-06-13 18:20:45 +0100513 int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000514 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
515 DCHECK_GT(slot_offset, 0);
516 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
517 }
518 }
519
David Srbecky052f8ca2018-04-26 15:42:54 +0100520 native_pc = stack_map.GetNativePcOffset(kRuntimeISA) +
David Srbecky09ed0982016-02-12 21:58:43 +0000521 osr_method->GetEntryPoint();
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000522 VLOG(jit) << "Jumping to "
523 << method_name
524 << "@"
525 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000526 }
527
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000528 {
529 ManagedStack fragment;
530 thread->PushManagedStackFragment(&fragment);
531 (*art_quick_osr_stub)(memory,
532 frame_size,
533 native_pc,
534 result,
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000535 shorty,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000536 thread);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000537
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000538 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
539 thread->DeoptimizeWithDeoptimizationException(result);
540 }
541 thread->PopManagedStackFragment(fragment);
542 }
543 free(memory);
544 thread->PushShadowFrame(shadow_frame);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000545 VLOG(jit) << "Done running OSR code for " << method_name;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000546 return true;
547}
548
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000549void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
550 if (bytes > 4 * MB) {
551 LOG(INFO) << "Compiler allocated "
552 << PrettySize(bytes)
553 << " to compile "
David Sehr709b0702016-10-13 09:12:37 -0700554 << ArtMethod::PrettyMethod(method);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000555 }
556 MutexLock mu(Thread::Current(), lock_);
557 memory_use_.AddValue(bytes);
558}
559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100560class JitCompileTask final : public Task {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100561 public:
562 enum TaskKind {
563 kAllocateProfile,
564 kCompile,
565 kCompileOsr
566 };
567
568 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
569 ScopedObjectAccess soa(Thread::Current());
570 // Add a global ref to the class to prevent class unloading until compilation is done.
571 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
572 CHECK(klass_ != nullptr);
573 }
574
575 ~JitCompileTask() {
576 ScopedObjectAccess soa(Thread::Current());
577 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
578 }
579
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100580 void Run(Thread* self) override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100581 ScopedObjectAccess soa(self);
582 if (kind_ == kCompile) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700583 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr= */ false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100584 } else if (kind_ == kCompileOsr) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700585 Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr= */ true);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100586 } else {
587 DCHECK(kind_ == kAllocateProfile);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700588 if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) {
David Sehr709b0702016-10-13 09:12:37 -0700589 VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100590 }
591 }
Calin Juravlea2638922016-04-29 16:44:11 +0100592 ProfileSaver::NotifyJitActivity();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100593 }
594
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100595 void Finalize() override {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100596 delete this;
597 }
598
599 private:
600 ArtMethod* const method_;
601 const TaskKind kind_;
602 jobject klass_;
603
604 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
605};
606
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100607static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
608 if (method->IsClassInitializer() || !method->IsCompilable()) {
609 // We do not want to compile such methods.
610 return true;
611 }
612 if (method->IsNative()) {
613 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100614 if (klass == GetClassRoot<mirror::MethodHandle>() ||
615 klass == GetClassRoot<mirror::VarHandle>()) {
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100616 // MethodHandle and VarHandle invocation methods are required to throw an
617 // UnsupportedOperationException if invoked reflectively. We achieve this by having native
618 // implementations that arise the exception. We need to disable JIT compilation of these JNI
619 // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI
620 // stubs. Since these stubs have different stack representations we can then crash in stack
621 // walking (b/78151261).
622 return true;
623 }
624 }
625 return false;
626}
627
David Srbeckydb94f2b2018-11-09 12:58:28 +0000628bool Jit::MaybeCompileMethod(Thread* self,
629 ArtMethod* method,
630 uint32_t old_count,
631 uint32_t new_count,
632 bool with_backedges) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100633 if (thread_pool_ == nullptr) {
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000634 // Should only see this when shutting down, starting up, or in zygote, which doesn't
635 // have a thread pool.
636 DCHECK(Runtime::Current()->IsShuttingDown(self) ||
637 !Runtime::Current()->IsFinishedStarting() ||
638 Runtime::Current()->IsZygote());
David Srbeckydb94f2b2018-11-09 12:58:28 +0000639 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100640 }
Orion Hodson52f5a1f2018-05-02 11:05:44 +0100641 if (IgnoreSamplesForMethod(method)) {
David Srbeckydb94f2b2018-11-09 12:58:28 +0000642 return false;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100643 }
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100644 if (HotMethodThreshold() == 0) {
David Srbeckyf4886df2017-12-11 16:06:29 +0000645 // Tests might request JIT on first use (compiled synchronously in the interpreter).
David Srbeckydb94f2b2018-11-09 12:58:28 +0000646 return false;
David Srbeckyf4886df2017-12-11 16:06:29 +0000647 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100648 DCHECK(thread_pool_ != nullptr);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100649 DCHECK_GT(WarmMethodThreshold(), 0);
650 DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold());
651 DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold());
652 DCHECK_GE(PriorityThreadWeight(), 1);
653 DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100654
David Srbeckydb94f2b2018-11-09 12:58:28 +0000655 if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) {
656 // Note: Native method have no "warm" state or profiling info.
657 if (!method->IsNative() && method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700658 bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false);
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000659 if (success) {
660 VLOG(jit) << "Start profiling " << method->PrettyMethod();
661 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100662
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000663 if (thread_pool_ == nullptr) {
664 // Calling ProfilingInfo::Create might put us in a suspended state, which could
665 // lead to the thread pool being deleted when we are shutting down.
666 DCHECK(Runtime::Current()->IsShuttingDown(self));
David Srbeckydb94f2b2018-11-09 12:58:28 +0000667 return false;
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000668 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100669
Nicolas Geoffray941c6ec2017-06-09 11:53:23 +0000670 if (!success) {
671 // We failed allocating. Instead of doing the collection on the Java thread, we push
672 // an allocation to a compiler thread, that will do the collection.
673 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
674 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100675 }
David Srbeckydb94f2b2018-11-09 12:58:28 +0000676 }
677 if (UseJitCompilation()) {
678 if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) {
679 if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Calin Juravleffc87072016-04-20 14:22:09 +0100680 DCHECK(thread_pool_ != nullptr);
681 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
682 }
David Srbeckydb94f2b2018-11-09 12:58:28 +0000683 }
684 if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100685 if (!with_backedges) {
David Srbeckydb94f2b2018-11-09 12:58:28 +0000686 return false;
Calin Juravleffc87072016-04-20 14:22:09 +0100687 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000688 DCHECK(!method->IsNative()); // No back edges reported for native methods.
David Srbeckydb94f2b2018-11-09 12:58:28 +0000689 if (!code_cache_->IsOsrCompiled(method)) {
Calin Juravleffc87072016-04-20 14:22:09 +0100690 DCHECK(thread_pool_ != nullptr);
691 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
692 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100693 }
694 }
David Srbeckydb94f2b2018-11-09 12:58:28 +0000695 return true;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100696}
697
Alex Light59b950f2018-10-08 10:43:06 -0700698class ScopedSetRuntimeThread {
699 public:
700 explicit ScopedSetRuntimeThread(Thread* self)
701 : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) {
702 self_->SetIsRuntimeThread(true);
703 }
704
705 ~ScopedSetRuntimeThread() {
706 self_->SetIsRuntimeThread(was_runtime_thread_);
707 }
708
709 private:
710 Thread* self_;
711 bool was_runtime_thread_;
712};
713
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100714void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
Calin Juravleffc87072016-04-20 14:22:09 +0100715 Runtime* runtime = Runtime::Current();
716 if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000717 ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000718 if (np_method->IsCompilable()) {
Vladimir Markof8655b32018-03-21 17:53:56 +0000719 if (!np_method->IsNative()) {
720 // The compiler requires a ProfilingInfo object for non-native methods.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700721 ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true);
Vladimir Markof8655b32018-03-21 17:53:56 +0000722 }
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000723 JitCompileTask compile_task(method, JitCompileTask::kCompile);
Alex Light59b950f2018-10-08 10:43:06 -0700724 // Fake being in a runtime thread so that class-load behavior will be the same as normal jit.
725 ScopedSetRuntimeThread ssrt(thread);
Vladimir Markobe0c7cf2018-03-19 13:40:56 +0000726 compile_task.Run(thread);
727 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100728 return;
729 }
730
Andreas Gampe542451c2016-07-26 09:02:02 -0700731 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100732 // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
Alex Light2d441b12018-06-08 15:33:21 -0700733 // instead of interpreting the method. We don't update it for instrumentation as the entrypoint
734 // must remain the instrumentation entrypoint.
735 if ((profiling_info != nullptr) &&
736 (profiling_info->GetSavedEntryPoint() != nullptr) &&
737 (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) {
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100738 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
739 method, profiling_info->GetSavedEntryPoint());
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100740 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700741 AddSamples(thread, method, 1, /* with_backedges= */false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100742 }
743}
744
Mathieu Chartieref41db72016-10-25 15:08:01 -0700745void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100746 ArtMethod* caller,
747 uint32_t dex_pc,
748 ArtMethod* callee ATTRIBUTE_UNUSED) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700749 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100750 DCHECK(this_object != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700751 ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100752 if (info != nullptr) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100753 info->AddInvokeInfo(dex_pc, this_object->GetClass());
754 }
755}
756
757void Jit::WaitForCompilationToFinish(Thread* self) {
758 if (thread_pool_ != nullptr) {
759 thread_pool_->Wait(self, false, false);
760 }
761}
762
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000763void Jit::Stop() {
764 Thread* self = Thread::Current();
765 // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
766 WaitForCompilationToFinish(self);
767 GetThreadPool()->StopWorkers(self);
768 WaitForCompilationToFinish(self);
769}
770
771void Jit::Start() {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000772 GetThreadPool()->StartWorkers(Thread::Current());
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000773}
774
Andreas Gampef149b3f2016-11-16 14:58:24 -0800775ScopedJitSuspend::ScopedJitSuspend() {
776 jit::Jit* jit = Runtime::Current()->GetJit();
777 was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
778 if (was_on_) {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000779 jit->Stop();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800780 }
781}
782
783ScopedJitSuspend::~ScopedJitSuspend() {
784 if (was_on_) {
785 DCHECK(Runtime::Current()->GetJit() != nullptr);
786 DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000787 Runtime::Current()->GetJit()->Start();
Andreas Gampef149b3f2016-11-16 14:58:24 -0800788 }
789}
790
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000791void Jit::PostForkChildAction() {
792 // At this point, the compiler options have been adjusted to the particular configuration
793 // of the forked child. Parse them again.
794 jit_update_options_(jit_compiler_handle_);
795
796 // Adjust the status of code cache collection: the status from zygote was to not collect.
797 code_cache_->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) &&
798 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled());
799}
800
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800801} // namespace jit
802} // namespace art