blob: 26a4fe49f19d0f066c991a0f7e5bcb64788abd79 [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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "runtime.h"
28#include "runtime_options.h"
29#include "thread_list.h"
30#include "utils.h"
31
32namespace art {
33namespace jit {
34
35JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070037 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038 jit_options->code_cache_capacity_ =
39 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheCapacity);
40 jit_options->compile_threshold_ =
41 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070042 jit_options->dump_info_on_shutdown_ =
43 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044 return jit_options;
45}
46
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070047void Jit::DumpInfo(std::ostream& os) {
48 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
49 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
50 << " num methods=" << code_cache_->NumMethods()
51 << "\n";
52 cumulative_timings_.Dump(os);
53}
54
55void Jit::AddTimingLogger(const TimingLogger& logger) {
56 cumulative_timings_.AddLogger(logger);
57}
58
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059Jit::Jit()
60 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070061 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
62 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063}
64
65Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
66 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070067 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080068 if (!jit->LoadCompiler(error_msg)) {
69 return nullptr;
70 }
71 jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg));
72 if (jit->GetCodeCache() == nullptr) {
73 return nullptr;
74 }
75 LOG(INFO) << "JIT created with code_cache_capacity="
76 << PrettySize(options->GetCodeCacheCapacity())
77 << " compile_threshold=" << options->GetCompileThreshold();
78 return jit.release();
79}
80
81bool Jit::LoadCompiler(std::string* error_msg) {
82 jit_library_handle_ = dlopen(
83 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
84 if (jit_library_handle_ == nullptr) {
85 std::ostringstream oss;
86 oss << "JIT could not load libart-compiler.so: " << dlerror();
87 *error_msg = oss.str();
88 return false;
89 }
90 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
91 dlsym(jit_library_handle_, "jit_load"));
92 if (jit_load_ == nullptr) {
93 dlclose(jit_library_handle_);
94 *error_msg = "JIT couldn't find jit_load entry point";
95 return false;
96 }
97 jit_unload_ = reinterpret_cast<void (*)(void*)>(
98 dlsym(jit_library_handle_, "jit_unload"));
99 if (jit_unload_ == nullptr) {
100 dlclose(jit_library_handle_);
101 *error_msg = "JIT couldn't find jit_unload entry point";
102 return false;
103 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800105 dlsym(jit_library_handle_, "jit_compile_method"));
106 if (jit_compile_method_ == nullptr) {
107 dlclose(jit_library_handle_);
108 *error_msg = "JIT couldn't find jit_compile_method entry point";
109 return false;
110 }
111 CompilerCallbacks* callbacks = nullptr;
112 VLOG(jit) << "Calling JitLoad interpreter_only="
113 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
114 jit_compiler_handle_ = (jit_load_)(&callbacks);
115 if (jit_compiler_handle_ == nullptr) {
116 dlclose(jit_library_handle_);
117 *error_msg = "JIT couldn't load compiler";
118 return false;
119 }
120 if (callbacks == nullptr) {
121 dlclose(jit_library_handle_);
122 *error_msg = "JIT compiler callbacks were not set";
123 jit_compiler_handle_ = nullptr;
124 return false;
125 }
126 compiler_callbacks_ = callbacks;
127 return true;
128}
129
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800131 DCHECK(!method->IsRuntimeMethod());
Mathieu Chartierd8565452015-03-26 09:41:50 -0700132 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
133 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
134 return false;
135 }
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000136 return jit_compile_method_(jit_compiler_handle_, method, self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800137}
138
139void Jit::CreateThreadPool() {
140 CHECK(instrumentation_cache_.get() != nullptr);
141 instrumentation_cache_->CreateThreadPool();
142}
143
144void Jit::DeleteThreadPool() {
145 if (instrumentation_cache_.get() != nullptr) {
146 instrumentation_cache_->DeleteThreadPool();
147 }
148}
149
150Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700151 if (dump_info_on_shutdown_) {
152 DumpInfo(LOG(INFO));
153 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 DeleteThreadPool();
155 if (jit_compiler_handle_ != nullptr) {
156 jit_unload_(jit_compiler_handle_);
157 }
158 if (jit_library_handle_ != nullptr) {
159 dlclose(jit_library_handle_);
160 }
161}
162
163void Jit::CreateInstrumentationCache(size_t compile_threshold) {
164 CHECK_GT(compile_threshold, 0U);
165 Runtime* const runtime = Runtime::Current();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700166 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800167 // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile
168 // something.
169 instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold));
170 runtime->GetInstrumentation()->AddListener(
171 new jit::JitInstrumentationListener(instrumentation_cache_.get()),
172 instrumentation::Instrumentation::kMethodEntered |
173 instrumentation::Instrumentation::kBackwardBranch);
174 runtime->GetThreadList()->ResumeAll();
175}
176
177} // namespace jit
178} // namespace art