blob: 54b65e6674818469228c003acbdc8245b19492c0 [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
39JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070041 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000042 jit_options->code_cache_initial_capacity_ =
43 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
44 jit_options->code_cache_max_capacity_ =
45 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046 jit_options->compile_threshold_ =
47 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000048 // TODO(ngeoffray): Make this a proper option.
49 jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010050 jit_options->warmup_threshold_ =
51 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070052 jit_options->dump_info_on_shutdown_ =
53 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle31f2c152015-10-23 17:56:15 +010054 jit_options->save_profiling_info_ =
55 options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056 return jit_options;
57}
58
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070059void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000060 os << "JIT code cache size=" << PrettySize(code_cache_->CodeCacheSize()) << "\n"
61 << "JIT data cache size=" << PrettySize(code_cache_->DataCacheSize()) << "\n"
62 << "JIT current capacity=" << PrettySize(code_cache_->GetCurrentCapacity()) << "\n"
Nicolas Geoffray0a522232016-01-19 09:34:58 +000063 << "JIT number of compiled code=" << code_cache_->NumberOfCompiledCode() << "\n"
64 << "JIT total number of compilations=" << code_cache_->NumberOfCompilations() << "\n";
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070065 cumulative_timings_.Dump(os);
66}
67
68void Jit::AddTimingLogger(const TimingLogger& logger) {
69 cumulative_timings_.AddLogger(logger);
70}
71
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000072Jit::Jit() : jit_library_handle_(nullptr),
73 jit_compiler_handle_(nullptr),
74 jit_load_(nullptr),
75 jit_compile_method_(nullptr),
76 dump_info_on_shutdown_(false),
77 cumulative_timings_("JIT timings"),
78 save_profiling_info_(false),
79 generate_debug_info_(false) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080}
81
82Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
83 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070084 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085 if (!jit->LoadCompiler(error_msg)) {
86 return nullptr;
87 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000088 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000089 options->GetCodeCacheInitialCapacity(),
90 options->GetCodeCacheMaxCapacity(),
91 jit->generate_debug_info_,
92 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093 if (jit->GetCodeCache() == nullptr) {
94 return nullptr;
95 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000096 jit->save_profiling_info_ = options->GetSaveProfilingInfo();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000097 LOG(INFO) << "JIT created with initial_capacity="
98 << PrettySize(options->GetCodeCacheInitialCapacity())
99 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000100 << ", compile_threshold=" << options->GetCompileThreshold()
101 << ", save_profiling_info=" << options->GetSaveProfilingInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800102 return jit.release();
103}
104
105bool Jit::LoadCompiler(std::string* error_msg) {
106 jit_library_handle_ = dlopen(
107 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
108 if (jit_library_handle_ == nullptr) {
109 std::ostringstream oss;
110 oss << "JIT could not load libart-compiler.so: " << dlerror();
111 *error_msg = oss.str();
112 return false;
113 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000114 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**, bool*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115 dlsym(jit_library_handle_, "jit_load"));
116 if (jit_load_ == nullptr) {
117 dlclose(jit_library_handle_);
118 *error_msg = "JIT couldn't find jit_load entry point";
119 return false;
120 }
121 jit_unload_ = reinterpret_cast<void (*)(void*)>(
122 dlsym(jit_library_handle_, "jit_unload"));
123 if (jit_unload_ == nullptr) {
124 dlclose(jit_library_handle_);
125 *error_msg = "JIT couldn't find jit_unload entry point";
126 return false;
127 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000128 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800129 dlsym(jit_library_handle_, "jit_compile_method"));
130 if (jit_compile_method_ == nullptr) {
131 dlclose(jit_library_handle_);
132 *error_msg = "JIT couldn't find jit_compile_method entry point";
133 return false;
134 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000135 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
136 dlsym(jit_library_handle_, "jit_types_loaded"));
137 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000138 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000139 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000140 return false;
141 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800142 CompilerCallbacks* callbacks = nullptr;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000143 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144 VLOG(jit) << "Calling JitLoad interpreter_only="
145 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000146 jit_compiler_handle_ = (jit_load_)(&callbacks, &will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800147 if (jit_compiler_handle_ == nullptr) {
148 dlclose(jit_library_handle_);
149 *error_msg = "JIT couldn't load compiler";
150 return false;
151 }
152 if (callbacks == nullptr) {
153 dlclose(jit_library_handle_);
154 *error_msg = "JIT compiler callbacks were not set";
155 jit_compiler_handle_ = nullptr;
156 return false;
157 }
158 compiler_callbacks_ = callbacks;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000159 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 return true;
161}
162
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000163bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800164 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100165 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700166 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
167 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
168 return false;
169 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100170
171 // Don't compile the method if we are supposed to be deoptimized.
172 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
173 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000174 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100175 return false;
176 }
177
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000178 if (!code_cache_->NotifyCompilationOf(method, self, osr)) {
179 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to code cache";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100180 return false;
181 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000182 bool success = jit_compile_method_(jit_compiler_handle_, method, self, osr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100183 code_cache_->DoneCompiling(method, self);
184 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800185}
186
187void Jit::CreateThreadPool() {
188 CHECK(instrumentation_cache_.get() != nullptr);
189 instrumentation_cache_->CreateThreadPool();
190}
191
192void Jit::DeleteThreadPool() {
193 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000194 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 }
196}
197
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000198void Jit::StartProfileSaver(const std::string& filename,
199 const std::vector<std::string>& code_paths) {
200 if (save_profiling_info_) {
201 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100202 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000203}
204
205void Jit::StopProfileSaver() {
206 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
207 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100208 }
209}
210
Siva Chandra05d24152016-01-05 17:43:17 -0800211bool Jit::JitAtFirstUse() {
212 if (instrumentation_cache_ != nullptr) {
213 return instrumentation_cache_->HotMethodThreshold() == 0;
214 }
215 return false;
216}
217
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800218Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000219 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700220 if (dump_info_on_shutdown_) {
221 DumpInfo(LOG(INFO));
222 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800223 DeleteThreadPool();
224 if (jit_compiler_handle_ != nullptr) {
225 jit_unload_(jit_compiler_handle_);
226 }
227 if (jit_library_handle_ != nullptr) {
228 dlclose(jit_library_handle_);
229 }
230}
231
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000232void Jit::CreateInstrumentationCache(size_t compile_threshold,
233 size_t warmup_threshold,
234 size_t osr_threshold) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100235 instrumentation_cache_.reset(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000236 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold, osr_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800237}
238
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000239void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
240 jit::Jit* jit = Runtime::Current()->GetJit();
241 if (jit != nullptr && jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000242 DCHECK(jit->jit_types_loaded_ != nullptr);
243 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
244 }
245}
246
247void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
248 struct CollectClasses : public ClassVisitor {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800249 bool operator()(mirror::Class* klass) override {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000250 classes_.push_back(klass);
251 return true;
252 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800253 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000254 };
255
256 if (generate_debug_info_) {
257 ScopedObjectAccess so(Thread::Current());
258
259 CollectClasses visitor;
260 linker->VisitClasses(&visitor);
261 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000262 }
263}
264
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000265extern "C" void art_quick_osr_stub(void** stack,
266 uint32_t stack_size_in_bytes,
267 const uint8_t* native_pc,
268 JValue* result,
269 const char* shorty,
270 Thread* self);
271
272bool Jit::MaybeDoOnStackReplacement(Thread* thread,
273 ArtMethod* method,
274 uint32_t dex_pc,
275 int32_t dex_pc_offset,
276 JValue* result) {
277 Jit* jit = Runtime::Current()->GetJit();
278 if (jit == nullptr) {
279 return false;
280 }
281
282 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
283 VLOG(jit) << "OSR not supported on this platform";
284 return false;
285 }
286
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000287 // Get the actual Java method if this method is from a proxy class. The compiler
288 // and the JIT code cache do not expect methods from proxy classes.
289 method = method->GetInterfaceMethodIfProxy(sizeof(void*));
290
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000291 // Cheap check if the method has been compiled already. That's an indicator that we should
292 // osr into it.
293 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
294 return false;
295 }
296
297 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
298 if (osr_method == nullptr) {
299 // No osr method yet, just return to the interpreter.
300 return false;
301 }
302
303 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
304 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
305 StackMapEncoding encoding = code_info.ExtractEncoding();
306
307 // Find stack map starting at the target dex_pc.
308 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
309 if (!stack_map.IsValid()) {
310 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
311 // hope that the next branch has one.
312 return false;
313 }
314
315 // We found a stack map, now fill the frame with dex register values from the interpreter's
316 // shadow frame.
317 DexRegisterMap vreg_map =
318 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
319
320 ShadowFrame* shadow_frame = thread->PopShadowFrame();
321
322 size_t frame_size = osr_method->GetFrameSizeInBytes();
323 void** memory = reinterpret_cast<void**>(malloc(frame_size));
324 memset(memory, 0, frame_size);
325
326 // Art ABI: ArtMethod is at the bottom of the stack.
327 memory[0] = method;
328
329 if (!vreg_map.IsValid()) {
330 // If we don't have a dex register map, then there are no live dex registers at
331 // this dex pc.
332 } else {
333 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
334 DexRegisterLocation::Kind location =
335 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
336 if (location == DexRegisterLocation::Kind::kNone) {
337 // Dex register is dead or unitialized.
338 continue;
339 }
340
341 if (location == DexRegisterLocation::Kind::kConstant) {
342 // We skip constants because the compiled code knows how to handle them.
343 continue;
344 }
345
346 DCHECK(location == DexRegisterLocation::Kind::kInStack);
347
348 int32_t vreg_value = shadow_frame->GetVReg(vreg);
349 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
350 number_of_vregs,
351 code_info,
352 encoding);
353 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
354 DCHECK_GT(slot_offset, 0);
355 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
356 }
357 }
358
359 const uint8_t* native_pc = stack_map.GetNativePcOffset(encoding) + osr_method->GetEntryPoint();
360 VLOG(jit) << "Jumping to "
361 << PrettyMethod(method)
362 << "@"
363 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
364 {
365 ManagedStack fragment;
366 thread->PushManagedStackFragment(&fragment);
367 (*art_quick_osr_stub)(memory,
368 frame_size,
369 native_pc,
370 result,
371 method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(),
372 thread);
373 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
374 thread->DeoptimizeWithDeoptimizationException(result);
375 }
376 thread->PopManagedStackFragment(fragment);
377 }
378 free(memory);
379 thread->PushShadowFrame(shadow_frame);
380 VLOG(jit) << "Done running OSR code for " << PrettyMethod(method);
381 return true;
382}
383
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800384} // namespace jit
385} // namespace art