blob: e38a6848249fbeeb49d71a8a3b18793595ffd88d [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 Gampe2a5c4682015-08-14 08:22:54 -070022#include "debugger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080023#include "entrypoints/runtime_asm_entrypoints.h"
24#include "interpreter/interpreter.h"
25#include "jit_code_cache.h"
26#include "jit_instrumentation.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010027#include "oat_file_manager.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000028#include "oat_quick_method_header.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010029#include "offline_profiling_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000030#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "runtime.h"
32#include "runtime_options.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000033#include "stack_map.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "utils.h"
35
36namespace art {
37namespace jit {
38
Nicolas Geoffray4d9b10a2016-02-07 13:13:33 +000039static constexpr bool kEnableOnStackReplacement = false;
40
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070043 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000044 jit_options->code_cache_initial_capacity_ =
45 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
46 jit_options->code_cache_max_capacity_ =
47 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080048 jit_options->compile_threshold_ =
49 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000050 // TODO(ngeoffray): Make this a proper option.
51 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010052 jit_options->warmup_threshold_ =
53 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070054 jit_options->dump_info_on_shutdown_ =
55 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle31f2c152015-10-23 17:56:15 +010056 jit_options->save_profiling_info_ =
57 options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 return jit_options;
59}
60
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070061void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000062 os << "JIT code cache size=" << PrettySize(code_cache_->CodeCacheSize()) << "\n"
63 << "JIT data cache size=" << PrettySize(code_cache_->DataCacheSize()) << "\n"
64 << "JIT current capacity=" << PrettySize(code_cache_->GetCurrentCapacity()) << "\n"
Nicolas Geoffray0a522232016-01-19 09:34:58 +000065 << "JIT number of compiled code=" << code_cache_->NumberOfCompiledCode() << "\n"
66 << "JIT total number of compilations=" << code_cache_->NumberOfCompilations() << "\n";
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070067 cumulative_timings_.Dump(os);
68}
69
70void Jit::AddTimingLogger(const TimingLogger& logger) {
71 cumulative_timings_.AddLogger(logger);
72}
73
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000074Jit::Jit() : jit_library_handle_(nullptr),
75 jit_compiler_handle_(nullptr),
76 jit_load_(nullptr),
77 jit_compile_method_(nullptr),
78 dump_info_on_shutdown_(false),
79 cumulative_timings_("JIT timings"),
80 save_profiling_info_(false),
81 generate_debug_info_(false) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080082}
83
84Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
85 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070086 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080087 if (!jit->LoadCompiler(error_msg)) {
88 return nullptr;
89 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000090 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000091 options->GetCodeCacheInitialCapacity(),
92 options->GetCodeCacheMaxCapacity(),
93 jit->generate_debug_info_,
94 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095 if (jit->GetCodeCache() == nullptr) {
96 return nullptr;
97 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000098 jit->save_profiling_info_ = options->GetSaveProfilingInfo();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000099 LOG(INFO) << "JIT created with initial_capacity="
100 << PrettySize(options->GetCodeCacheInitialCapacity())
101 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000102 << ", compile_threshold=" << options->GetCompileThreshold()
103 << ", save_profiling_info=" << options->GetSaveProfilingInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800104 return jit.release();
105}
106
107bool Jit::LoadCompiler(std::string* error_msg) {
108 jit_library_handle_ = dlopen(
109 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
110 if (jit_library_handle_ == nullptr) {
111 std::ostringstream oss;
112 oss << "JIT could not load libart-compiler.so: " << dlerror();
113 *error_msg = oss.str();
114 return false;
115 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000116 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**, bool*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117 dlsym(jit_library_handle_, "jit_load"));
118 if (jit_load_ == nullptr) {
119 dlclose(jit_library_handle_);
120 *error_msg = "JIT couldn't find jit_load entry point";
121 return false;
122 }
123 jit_unload_ = reinterpret_cast<void (*)(void*)>(
124 dlsym(jit_library_handle_, "jit_unload"));
125 if (jit_unload_ == nullptr) {
126 dlclose(jit_library_handle_);
127 *error_msg = "JIT couldn't find jit_unload entry point";
128 return false;
129 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000130 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800131 dlsym(jit_library_handle_, "jit_compile_method"));
132 if (jit_compile_method_ == nullptr) {
133 dlclose(jit_library_handle_);
134 *error_msg = "JIT couldn't find jit_compile_method entry point";
135 return false;
136 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000137 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
138 dlsym(jit_library_handle_, "jit_types_loaded"));
139 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000140 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000141 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000142 return false;
143 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144 CompilerCallbacks* callbacks = nullptr;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000145 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800146 VLOG(jit) << "Calling JitLoad interpreter_only="
147 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000148 jit_compiler_handle_ = (jit_load_)(&callbacks, &will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800149 if (jit_compiler_handle_ == nullptr) {
150 dlclose(jit_library_handle_);
151 *error_msg = "JIT couldn't load compiler";
152 return false;
153 }
154 if (callbacks == nullptr) {
155 dlclose(jit_library_handle_);
156 *error_msg = "JIT compiler callbacks were not set";
157 jit_compiler_handle_ = nullptr;
158 return false;
159 }
160 compiler_callbacks_ = callbacks;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000161 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800162 return true;
163}
164
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000165bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100167 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700168 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
169 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
170 return false;
171 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100172
173 // Don't compile the method if we are supposed to be deoptimized.
174 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
175 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000176 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100177 return false;
178 }
179
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000180 if (!code_cache_->NotifyCompilationOf(method, self, osr)) {
181 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to code cache";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100182 return false;
183 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000184 bool success = jit_compile_method_(jit_compiler_handle_, method, self, osr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100185 code_cache_->DoneCompiling(method, self);
186 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187}
188
189void Jit::CreateThreadPool() {
190 CHECK(instrumentation_cache_.get() != nullptr);
191 instrumentation_cache_->CreateThreadPool();
192}
193
194void Jit::DeleteThreadPool() {
195 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000196 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800197 }
198}
199
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000200void Jit::StartProfileSaver(const std::string& filename,
201 const std::vector<std::string>& code_paths) {
202 if (save_profiling_info_) {
203 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100204 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000205}
206
207void Jit::StopProfileSaver() {
208 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
209 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100210 }
211}
212
Siva Chandra05d24152016-01-05 17:43:17 -0800213bool Jit::JitAtFirstUse() {
214 if (instrumentation_cache_ != nullptr) {
215 return instrumentation_cache_->HotMethodThreshold() == 0;
216 }
217 return false;
218}
219
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800220Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000221 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700222 if (dump_info_on_shutdown_) {
223 DumpInfo(LOG(INFO));
224 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800225 DeleteThreadPool();
226 if (jit_compiler_handle_ != nullptr) {
227 jit_unload_(jit_compiler_handle_);
228 }
229 if (jit_library_handle_ != nullptr) {
230 dlclose(jit_library_handle_);
231 }
232}
233
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000234void Jit::CreateInstrumentationCache(size_t compile_threshold,
235 size_t warmup_threshold,
236 size_t osr_threshold) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100237 instrumentation_cache_.reset(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000238 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold, osr_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800239}
240
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000241void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
242 jit::Jit* jit = Runtime::Current()->GetJit();
243 if (jit != nullptr && jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000244 DCHECK(jit->jit_types_loaded_ != nullptr);
245 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
246 }
247}
248
249void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
250 struct CollectClasses : public ClassVisitor {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800251 bool operator()(mirror::Class* klass) override {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000252 classes_.push_back(klass);
253 return true;
254 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800255 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000256 };
257
258 if (generate_debug_info_) {
259 ScopedObjectAccess so(Thread::Current());
260
261 CollectClasses visitor;
262 linker->VisitClasses(&visitor);
263 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000264 }
265}
266
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000267extern "C" void art_quick_osr_stub(void** stack,
268 uint32_t stack_size_in_bytes,
269 const uint8_t* native_pc,
270 JValue* result,
271 const char* shorty,
272 Thread* self);
273
274bool Jit::MaybeDoOnStackReplacement(Thread* thread,
275 ArtMethod* method,
276 uint32_t dex_pc,
277 int32_t dex_pc_offset,
278 JValue* result) {
Nicolas Geoffray4d9b10a2016-02-07 13:13:33 +0000279 if (!kEnableOnStackReplacement) {
280 return false;
281 }
282
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000283 Jit* jit = Runtime::Current()->GetJit();
284 if (jit == nullptr) {
285 return false;
286 }
287
288 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
289 VLOG(jit) << "OSR not supported on this platform";
290 return false;
291 }
292
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000293 // Get the actual Java method if this method is from a proxy class. The compiler
294 // and the JIT code cache do not expect methods from proxy classes.
295 method = method->GetInterfaceMethodIfProxy(sizeof(void*));
296
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000297 // Cheap check if the method has been compiled already. That's an indicator that we should
298 // osr into it.
299 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
300 return false;
301 }
302
303 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
304 if (osr_method == nullptr) {
305 // No osr method yet, just return to the interpreter.
306 return false;
307 }
308
309 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
310 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
311 StackMapEncoding encoding = code_info.ExtractEncoding();
312
313 // Find stack map starting at the target dex_pc.
314 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
315 if (!stack_map.IsValid()) {
316 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
317 // hope that the next branch has one.
318 return false;
319 }
320
321 // We found a stack map, now fill the frame with dex register values from the interpreter's
322 // shadow frame.
323 DexRegisterMap vreg_map =
324 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
325
326 ShadowFrame* shadow_frame = thread->PopShadowFrame();
327
328 size_t frame_size = osr_method->GetFrameSizeInBytes();
329 void** memory = reinterpret_cast<void**>(malloc(frame_size));
330 memset(memory, 0, frame_size);
331
332 // Art ABI: ArtMethod is at the bottom of the stack.
333 memory[0] = method;
334
335 if (!vreg_map.IsValid()) {
336 // If we don't have a dex register map, then there are no live dex registers at
337 // this dex pc.
338 } else {
339 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
340 DexRegisterLocation::Kind location =
341 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
342 if (location == DexRegisterLocation::Kind::kNone) {
343 // Dex register is dead or unitialized.
344 continue;
345 }
346
347 if (location == DexRegisterLocation::Kind::kConstant) {
348 // We skip constants because the compiled code knows how to handle them.
349 continue;
350 }
351
352 DCHECK(location == DexRegisterLocation::Kind::kInStack);
353
354 int32_t vreg_value = shadow_frame->GetVReg(vreg);
355 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
356 number_of_vregs,
357 code_info,
358 encoding);
359 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
360 DCHECK_GT(slot_offset, 0);
361 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
362 }
363 }
364
365 const uint8_t* native_pc = stack_map.GetNativePcOffset(encoding) + osr_method->GetEntryPoint();
366 VLOG(jit) << "Jumping to "
367 << PrettyMethod(method)
368 << "@"
369 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
370 {
371 ManagedStack fragment;
372 thread->PushManagedStackFragment(&fragment);
373 (*art_quick_osr_stub)(memory,
374 frame_size,
375 native_pc,
376 result,
377 method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(),
378 thread);
379 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
380 thread->DeoptimizeWithDeoptimizationException(result);
381 }
382 thread->PopManagedStackFragment(fragment);
383 }
384 free(memory);
385 thread->PushShadowFrame(shadow_frame);
386 VLOG(jit) << "Done running OSR code for " << PrettyMethod(method);
387 return true;
388}
389
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800390} // namespace jit
391} // namespace art