blob: 05668a97b30fe64b0a5f205f8a6385903409a930 [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"
28#include "offline_profiling_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000029#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "runtime.h"
31#include "runtime_options.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "utils.h"
33
34namespace art {
35namespace jit {
36
37JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070039 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000040 jit_options->code_cache_initial_capacity_ =
41 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
42 jit_options->code_cache_max_capacity_ =
43 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044 jit_options->compile_threshold_ =
45 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010046 jit_options->warmup_threshold_ =
47 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070048 jit_options->dump_info_on_shutdown_ =
49 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle31f2c152015-10-23 17:56:15 +010050 jit_options->save_profiling_info_ =
51 options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052 return jit_options;
53}
54
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070055void Jit::DumpInfo(std::ostream& os) {
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000056 os << "JIT code cache size=" << PrettySize(code_cache_->CodeCacheSize()) << "\n"
57 << "JIT data cache size=" << PrettySize(code_cache_->DataCacheSize()) << "\n"
58 << "JIT current capacity=" << PrettySize(code_cache_->GetCurrentCapacity()) << "\n"
59 << "JIT number of compiled code=" << code_cache_->NumberOfCompiledCode() << "\n";
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070060 cumulative_timings_.Dump(os);
61}
62
63void Jit::AddTimingLogger(const TimingLogger& logger) {
64 cumulative_timings_.AddLogger(logger);
65}
66
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000067Jit::Jit() : jit_library_handle_(nullptr),
68 jit_compiler_handle_(nullptr),
69 jit_load_(nullptr),
70 jit_compile_method_(nullptr),
71 dump_info_on_shutdown_(false),
72 cumulative_timings_("JIT timings"),
73 save_profiling_info_(false),
74 generate_debug_info_(false) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075}
76
77Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
78 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070079 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080 if (!jit->LoadCompiler(error_msg)) {
81 return nullptr;
82 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000083 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000084 options->GetCodeCacheInitialCapacity(),
85 options->GetCodeCacheMaxCapacity(),
86 jit->generate_debug_info_,
87 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080088 if (jit->GetCodeCache() == nullptr) {
89 return nullptr;
90 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000091 jit->save_profiling_info_ = options->GetSaveProfilingInfo();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000092 LOG(INFO) << "JIT created with initial_capacity="
93 << PrettySize(options->GetCodeCacheInitialCapacity())
94 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +000095 << ", compile_threshold=" << options->GetCompileThreshold()
96 << ", save_profiling_info=" << options->GetSaveProfilingInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080097 return jit.release();
98}
99
100bool Jit::LoadCompiler(std::string* error_msg) {
101 jit_library_handle_ = dlopen(
102 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
103 if (jit_library_handle_ == nullptr) {
104 std::ostringstream oss;
105 oss << "JIT could not load libart-compiler.so: " << dlerror();
106 *error_msg = oss.str();
107 return false;
108 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000109 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**, bool*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110 dlsym(jit_library_handle_, "jit_load"));
111 if (jit_load_ == nullptr) {
112 dlclose(jit_library_handle_);
113 *error_msg = "JIT couldn't find jit_load entry point";
114 return false;
115 }
116 jit_unload_ = reinterpret_cast<void (*)(void*)>(
117 dlsym(jit_library_handle_, "jit_unload"));
118 if (jit_unload_ == nullptr) {
119 dlclose(jit_library_handle_);
120 *error_msg = "JIT couldn't find jit_unload entry point";
121 return false;
122 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 dlsym(jit_library_handle_, "jit_compile_method"));
125 if (jit_compile_method_ == nullptr) {
126 dlclose(jit_library_handle_);
127 *error_msg = "JIT couldn't find jit_compile_method entry point";
128 return false;
129 }
130 CompilerCallbacks* callbacks = nullptr;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000131 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800132 VLOG(jit) << "Calling JitLoad interpreter_only="
133 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000134 jit_compiler_handle_ = (jit_load_)(&callbacks, &will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800135 if (jit_compiler_handle_ == nullptr) {
136 dlclose(jit_library_handle_);
137 *error_msg = "JIT couldn't load compiler";
138 return false;
139 }
140 if (callbacks == nullptr) {
141 dlclose(jit_library_handle_);
142 *error_msg = "JIT compiler callbacks were not set";
143 jit_compiler_handle_ = nullptr;
144 return false;
145 }
146 compiler_callbacks_ = callbacks;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000147 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 return true;
149}
150
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800152 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100153 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700154 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
155 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
156 return false;
157 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100158
159 // Don't compile the method if we are supposed to be deoptimized.
160 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
161 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
162 return false;
163 }
164
165 if (!code_cache_->NotifyCompilationOf(method, self)) {
166 return false;
167 }
168 bool success = jit_compile_method_(jit_compiler_handle_, method, self);
169 code_cache_->DoneCompiling(method, self);
170 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171}
172
173void Jit::CreateThreadPool() {
174 CHECK(instrumentation_cache_.get() != nullptr);
175 instrumentation_cache_->CreateThreadPool();
176}
177
178void Jit::DeleteThreadPool() {
179 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000180 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800181 }
182}
183
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000184void Jit::StartProfileSaver(const std::string& filename,
185 const std::vector<std::string>& code_paths) {
186 if (save_profiling_info_) {
187 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100188 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000189}
190
191void Jit::StopProfileSaver() {
192 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
193 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100194 }
195}
196
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800197Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000198 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700199 if (dump_info_on_shutdown_) {
200 DumpInfo(LOG(INFO));
201 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800202 DeleteThreadPool();
203 if (jit_compiler_handle_ != nullptr) {
204 jit_unload_(jit_compiler_handle_);
205 }
206 if (jit_library_handle_ != nullptr) {
207 dlclose(jit_library_handle_);
208 }
209}
210
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100211void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800212 CHECK_GT(compile_threshold, 0U);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100213 instrumentation_cache_.reset(
214 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800215}
216
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217} // namespace jit
218} // namespace art