blob: 5c9dab2f3830fead39992fd9fe10b48a79650f34 [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 }
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000130 jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
131 dlsym(jit_library_handle_, "jit_types_loaded"));
132 if (jit_types_loaded_ == nullptr) {
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000133 dlclose(jit_library_handle_);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000134 *error_msg = "JIT couldn't find jit_types_loaded entry point";
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000135 return false;
136 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800137 CompilerCallbacks* callbacks = nullptr;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000138 bool will_generate_debug_symbols = false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139 VLOG(jit) << "Calling JitLoad interpreter_only="
140 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000141 jit_compiler_handle_ = (jit_load_)(&callbacks, &will_generate_debug_symbols);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800142 if (jit_compiler_handle_ == nullptr) {
143 dlclose(jit_library_handle_);
144 *error_msg = "JIT couldn't load compiler";
145 return false;
146 }
147 if (callbacks == nullptr) {
148 dlclose(jit_library_handle_);
149 *error_msg = "JIT compiler callbacks were not set";
150 jit_compiler_handle_ = nullptr;
151 return false;
152 }
153 compiler_callbacks_ = callbacks;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000154 generate_debug_info_ = will_generate_debug_symbols;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 return true;
156}
157
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100160 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700161 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
162 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
163 return false;
164 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100165
166 // Don't compile the method if we are supposed to be deoptimized.
167 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
168 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
169 return false;
170 }
171
172 if (!code_cache_->NotifyCompilationOf(method, self)) {
173 return false;
174 }
175 bool success = jit_compile_method_(jit_compiler_handle_, method, self);
176 code_cache_->DoneCompiling(method, self);
177 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178}
179
180void Jit::CreateThreadPool() {
181 CHECK(instrumentation_cache_.get() != nullptr);
182 instrumentation_cache_->CreateThreadPool();
183}
184
185void Jit::DeleteThreadPool() {
186 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000187 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800188 }
189}
190
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000191void Jit::StartProfileSaver(const std::string& filename,
192 const std::vector<std::string>& code_paths) {
193 if (save_profiling_info_) {
194 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100195 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000196}
197
198void Jit::StopProfileSaver() {
199 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
200 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100201 }
202}
203
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800204Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000205 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700206 if (dump_info_on_shutdown_) {
207 DumpInfo(LOG(INFO));
208 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800209 DeleteThreadPool();
210 if (jit_compiler_handle_ != nullptr) {
211 jit_unload_(jit_compiler_handle_);
212 }
213 if (jit_library_handle_ != nullptr) {
214 dlclose(jit_library_handle_);
215 }
216}
217
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100218void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219 CHECK_GT(compile_threshold, 0U);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100220 instrumentation_cache_.reset(
221 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800222}
223
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000224void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
225 jit::Jit* jit = Runtime::Current()->GetJit();
226 if (jit != nullptr && jit->generate_debug_info_) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000227 DCHECK(jit->jit_types_loaded_ != nullptr);
228 jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
229 }
230}
231
232void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
233 struct CollectClasses : public ClassVisitor {
234 bool Visit(mirror::Class* klass) override {
235 classes_.push_back(klass);
236 return true;
237 }
238 std::vector<mirror::Class*> classes_;
239 };
240
241 if (generate_debug_info_) {
242 ScopedObjectAccess so(Thread::Current());
243
244 CollectClasses visitor;
245 linker->VisitClasses(&visitor);
246 jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000247 }
248}
249
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800250} // namespace jit
251} // namespace art