blob: fcfa457bf72db73d1be0cd5bc7e307f4a6932e43 [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 Geoffrayd9994f02016-02-11 17:35:55 +0000165
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100166 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700167 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
168 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
169 return false;
170 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100171
172 // Don't compile the method if we are supposed to be deoptimized.
173 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
174 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000175 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to deoptimization";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100176 return false;
177 }
178
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000179 // If we get a request to compile a proxy method, we pass the actual Java method
180 // of that proxy method, as the compiler does not expect a proxy method.
181 ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*));
182 if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000183 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to code cache";
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100184 return false;
185 }
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000186 bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
187 code_cache_->DoneCompiling(method_to_compile, self);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100188 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800189}
190
191void Jit::CreateThreadPool() {
192 CHECK(instrumentation_cache_.get() != nullptr);
193 instrumentation_cache_->CreateThreadPool();
194}
195
196void Jit::DeleteThreadPool() {
197 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000198 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199 }
200}
201
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000202void Jit::StartProfileSaver(const std::string& filename,
203 const std::vector<std::string>& code_paths) {
204 if (save_profiling_info_) {
205 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100206 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000207}
208
209void Jit::StopProfileSaver() {
210 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
211 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100212 }
213}
214
Siva Chandra05d24152016-01-05 17:43:17 -0800215bool Jit::JitAtFirstUse() {
216 if (instrumentation_cache_ != nullptr) {
217 return instrumentation_cache_->HotMethodThreshold() == 0;
218 }
219 return false;
220}
221
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800222Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000223 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700224 if (dump_info_on_shutdown_) {
225 DumpInfo(LOG(INFO));
226 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800227 DeleteThreadPool();
228 if (jit_compiler_handle_ != nullptr) {
229 jit_unload_(jit_compiler_handle_);
230 }
231 if (jit_library_handle_ != nullptr) {
232 dlclose(jit_library_handle_);
233 }
234}
235
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000236void Jit::CreateInstrumentationCache(size_t compile_threshold,
237 size_t warmup_threshold,
238 size_t osr_threshold) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100239 instrumentation_cache_.reset(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000240 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold, osr_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800241}
242
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000243void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
244 jit::Jit* jit = Runtime::Current()->GetJit();
245 if (jit != nullptr && jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000246 DCHECK(jit->jit_types_loaded_ != nullptr);
247 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
248 }
249}
250
251void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
252 struct CollectClasses : public ClassVisitor {
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -0800253 bool operator()(mirror::Class* klass) override {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000254 classes_.push_back(klass);
255 return true;
256 }
Mathieu Chartier9b1c9b72016-02-02 10:09:58 -0800257 std::vector<mirror::Class*> classes_;
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000258 };
259
260 if (generate_debug_info_) {
261 ScopedObjectAccess so(Thread::Current());
262
263 CollectClasses visitor;
264 linker->VisitClasses(&visitor);
265 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000266 }
267}
268
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000269extern "C" void art_quick_osr_stub(void** stack,
270 uint32_t stack_size_in_bytes,
271 const uint8_t* native_pc,
272 JValue* result,
273 const char* shorty,
274 Thread* self);
275
276bool Jit::MaybeDoOnStackReplacement(Thread* thread,
277 ArtMethod* method,
278 uint32_t dex_pc,
279 int32_t dex_pc_offset,
280 JValue* result) {
281 Jit* jit = Runtime::Current()->GetJit();
282 if (jit == nullptr) {
283 return false;
284 }
285
286 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
287 VLOG(jit) << "OSR not supported on this platform";
288 return false;
289 }
290
Nicolas Geoffrayd9bc4332016-02-05 23:32:25 +0000291 // Get the actual Java method if this method is from a proxy class. The compiler
292 // and the JIT code cache do not expect methods from proxy classes.
293 method = method->GetInterfaceMethodIfProxy(sizeof(void*));
294
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000295 // Cheap check if the method has been compiled already. That's an indicator that we should
296 // osr into it.
297 if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
298 return false;
299 }
300
301 const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
302 if (osr_method == nullptr) {
303 // No osr method yet, just return to the interpreter.
304 return false;
305 }
306
307 const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
308 CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
309 StackMapEncoding encoding = code_info.ExtractEncoding();
310
311 // Find stack map starting at the target dex_pc.
312 StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
313 if (!stack_map.IsValid()) {
314 // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
315 // hope that the next branch has one.
316 return false;
317 }
318
319 // We found a stack map, now fill the frame with dex register values from the interpreter's
320 // shadow frame.
321 DexRegisterMap vreg_map =
322 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
323
324 ShadowFrame* shadow_frame = thread->PopShadowFrame();
325
326 size_t frame_size = osr_method->GetFrameSizeInBytes();
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000327
328 // Allocate memory to put shadow frame values. The osr stub will copy that memory to
329 // stack.
330 // Note that we could pass the shadow frame to the stub, and let it copy the values there,
331 // but that is engineering complexity not worth the effort for something like OSR.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000332 void** memory = reinterpret_cast<void**>(malloc(frame_size));
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000333 CHECK(memory != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000334 memset(memory, 0, frame_size);
335
336 // Art ABI: ArtMethod is at the bottom of the stack.
337 memory[0] = method;
338
339 if (!vreg_map.IsValid()) {
340 // If we don't have a dex register map, then there are no live dex registers at
341 // this dex pc.
342 } else {
343 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
344 DexRegisterLocation::Kind location =
345 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
346 if (location == DexRegisterLocation::Kind::kNone) {
347 // Dex register is dead or unitialized.
348 continue;
349 }
350
351 if (location == DexRegisterLocation::Kind::kConstant) {
352 // We skip constants because the compiled code knows how to handle them.
353 continue;
354 }
355
356 DCHECK(location == DexRegisterLocation::Kind::kInStack);
357
358 int32_t vreg_value = shadow_frame->GetVReg(vreg);
359 int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
360 number_of_vregs,
361 code_info,
362 encoding);
363 DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
364 DCHECK_GT(slot_offset, 0);
365 (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
366 }
367 }
368
369 const uint8_t* native_pc = stack_map.GetNativePcOffset(encoding) + osr_method->GetEntryPoint();
370 VLOG(jit) << "Jumping to "
371 << PrettyMethod(method)
372 << "@"
373 << std::hex << reinterpret_cast<uintptr_t>(native_pc);
374 {
375 ManagedStack fragment;
376 thread->PushManagedStackFragment(&fragment);
377 (*art_quick_osr_stub)(memory,
378 frame_size,
379 native_pc,
380 result,
381 method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(),
382 thread);
383 if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
384 thread->DeoptimizeWithDeoptimizationException(result);
385 }
386 thread->PopManagedStackFragment(fragment);
387 }
388 free(memory);
389 thread->PushShadowFrame(shadow_frame);
390 VLOG(jit) << "Done running OSR code for " << PrettyMethod(method);
391 return true;
392}
393
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800394} // namespace jit
395} // namespace art