blob: f540814fbe2515e141c8c955ea674aac868db9a5 [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"
Nicolas Geoffray0a522232016-01-19 09:34:58 +000059 << "JIT number of compiled code=" << code_cache_->NumberOfCompiledCode() << "\n"
60 << "JIT total number of compilations=" << code_cache_->NumberOfCompilations() << "\n";
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070061 cumulative_timings_.Dump(os);
62}
63
64void Jit::AddTimingLogger(const TimingLogger& logger) {
65 cumulative_timings_.AddLogger(logger);
66}
67
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000068Jit::Jit() : jit_library_handle_(nullptr),
69 jit_compiler_handle_(nullptr),
70 jit_load_(nullptr),
71 jit_compile_method_(nullptr),
72 dump_info_on_shutdown_(false),
73 cumulative_timings_("JIT timings"),
74 save_profiling_info_(false),
75 generate_debug_info_(false) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076}
77
78Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
79 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070080 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 if (!jit->LoadCompiler(error_msg)) {
82 return nullptr;
83 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000084 jit->code_cache_.reset(JitCodeCache::Create(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000085 options->GetCodeCacheInitialCapacity(),
86 options->GetCodeCacheMaxCapacity(),
87 jit->generate_debug_info_,
88 error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080089 if (jit->GetCodeCache() == nullptr) {
90 return nullptr;
91 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000092 jit->save_profiling_info_ = options->GetSaveProfilingInfo();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000093 LOG(INFO) << "JIT created with initial_capacity="
94 << PrettySize(options->GetCodeCacheInitialCapacity())
95 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +000096 << ", compile_threshold=" << options->GetCompileThreshold()
97 << ", save_profiling_info=" << options->GetSaveProfilingInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080098 return jit.release();
99}
100
101bool Jit::LoadCompiler(std::string* error_msg) {
102 jit_library_handle_ = dlopen(
103 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
104 if (jit_library_handle_ == nullptr) {
105 std::ostringstream oss;
106 oss << "JIT could not load libart-compiler.so: " << dlerror();
107 *error_msg = oss.str();
108 return false;
109 }
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000110 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**, bool*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800111 dlsym(jit_library_handle_, "jit_load"));
112 if (jit_load_ == nullptr) {
113 dlclose(jit_library_handle_);
114 *error_msg = "JIT couldn't find jit_load entry point";
115 return false;
116 }
117 jit_unload_ = reinterpret_cast<void (*)(void*)>(
118 dlsym(jit_library_handle_, "jit_unload"));
119 if (jit_unload_ == nullptr) {
120 dlclose(jit_library_handle_);
121 *error_msg = "JIT couldn't find jit_unload entry point";
122 return false;
123 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 dlsym(jit_library_handle_, "jit_compile_method"));
126 if (jit_compile_method_ == nullptr) {
127 dlclose(jit_library_handle_);
128 *error_msg = "JIT couldn't find jit_compile_method entry point";
129 return false;
130 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000131 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
132 dlsym(jit_library_handle_, "jit_types_loaded"));
133 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000134 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000135 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000136 return false;
137 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800138 CompilerCallbacks* callbacks = nullptr;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000139 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800140 VLOG(jit) << "Calling JitLoad interpreter_only="
141 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000142 jit_compiler_handle_ = (jit_load_)(&callbacks, &will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800143 if (jit_compiler_handle_ == nullptr) {
144 dlclose(jit_library_handle_);
145 *error_msg = "JIT couldn't load compiler";
146 return false;
147 }
148 if (callbacks == nullptr) {
149 dlclose(jit_library_handle_);
150 *error_msg = "JIT compiler callbacks were not set";
151 jit_compiler_handle_ = nullptr;
152 return false;
153 }
154 compiler_callbacks_ = callbacks;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000155 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800156 return true;
157}
158
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100161 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700162 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
163 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
164 return false;
165 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100166
167 // Don't compile the method if we are supposed to be deoptimized.
168 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
169 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
170 return false;
171 }
172
173 if (!code_cache_->NotifyCompilationOf(method, self)) {
174 return false;
175 }
176 bool success = jit_compile_method_(jit_compiler_handle_, method, self);
177 code_cache_->DoneCompiling(method, self);
178 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179}
180
181void Jit::CreateThreadPool() {
182 CHECK(instrumentation_cache_.get() != nullptr);
183 instrumentation_cache_->CreateThreadPool();
184}
185
186void Jit::DeleteThreadPool() {
187 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000188 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800189 }
190}
191
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000192void Jit::StartProfileSaver(const std::string& filename,
193 const std::vector<std::string>& code_paths) {
194 if (save_profiling_info_) {
195 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100196 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000197}
198
199void Jit::StopProfileSaver() {
200 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
201 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100202 }
203}
204
Siva Chandra05d24152016-01-05 17:43:17 -0800205bool Jit::JitAtFirstUse() {
206 if (instrumentation_cache_ != nullptr) {
207 return instrumentation_cache_->HotMethodThreshold() == 0;
208 }
209 return false;
210}
211
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800212Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000213 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700214 if (dump_info_on_shutdown_) {
215 DumpInfo(LOG(INFO));
216 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 DeleteThreadPool();
218 if (jit_compiler_handle_ != nullptr) {
219 jit_unload_(jit_compiler_handle_);
220 }
221 if (jit_library_handle_ != nullptr) {
222 dlclose(jit_library_handle_);
223 }
224}
225
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100226void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100227 instrumentation_cache_.reset(
228 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800229}
230
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000231void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
232 jit::Jit* jit = Runtime::Current()->GetJit();
233 if (jit != nullptr && jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000234 DCHECK(jit->jit_types_loaded_ != nullptr);
235 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
236 }
237}
238
239void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
240 struct CollectClasses : public ClassVisitor {
241 bool Visit(mirror::Class* klass) override {
242 classes_.push_back(klass);
243 return true;
244 }
245 std::vector<mirror::Class*> classes_;
246 };
247
248 if (generate_debug_info_) {
249 ScopedObjectAccess so(Thread::Current());
250
251 CollectClasses visitor;
252 linker->VisitClasses(&visitor);
253 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000254 }
255}
256
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257} // namespace jit
258} // namespace art