blob: 06074934204ded4d61ba02a748a526d1bf18f6db [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);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010042 jit_options->warmup_threshold_ =
43 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070044 jit_options->dump_info_on_shutdown_ =
45 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046 return jit_options;
47}
48
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070049void Jit::DumpInfo(std::ostream& os) {
50 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
51 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
52 << " num methods=" << code_cache_->NumMethods()
53 << "\n";
54 cumulative_timings_.Dump(os);
55}
56
57void Jit::AddTimingLogger(const TimingLogger& logger) {
58 cumulative_timings_.AddLogger(logger);
59}
60
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061Jit::Jit()
62 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070063 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
64 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080065}
66
67Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
68 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070069 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070 if (!jit->LoadCompiler(error_msg)) {
71 return nullptr;
72 }
73 jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg));
74 if (jit->GetCodeCache() == nullptr) {
75 return nullptr;
76 }
77 LOG(INFO) << "JIT created with code_cache_capacity="
78 << PrettySize(options->GetCodeCacheCapacity())
79 << " compile_threshold=" << options->GetCompileThreshold();
80 return jit.release();
81}
82
83bool Jit::LoadCompiler(std::string* error_msg) {
84 jit_library_handle_ = dlopen(
85 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
86 if (jit_library_handle_ == nullptr) {
87 std::ostringstream oss;
88 oss << "JIT could not load libart-compiler.so: " << dlerror();
89 *error_msg = oss.str();
90 return false;
91 }
92 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
93 dlsym(jit_library_handle_, "jit_load"));
94 if (jit_load_ == nullptr) {
95 dlclose(jit_library_handle_);
96 *error_msg = "JIT couldn't find jit_load entry point";
97 return false;
98 }
99 jit_unload_ = reinterpret_cast<void (*)(void*)>(
100 dlsym(jit_library_handle_, "jit_unload"));
101 if (jit_unload_ == nullptr) {
102 dlclose(jit_library_handle_);
103 *error_msg = "JIT couldn't find jit_unload entry point";
104 return false;
105 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 dlsym(jit_library_handle_, "jit_compile_method"));
108 if (jit_compile_method_ == nullptr) {
109 dlclose(jit_library_handle_);
110 *error_msg = "JIT couldn't find jit_compile_method entry point";
111 return false;
112 }
113 CompilerCallbacks* callbacks = nullptr;
114 VLOG(jit) << "Calling JitLoad interpreter_only="
115 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
116 jit_compiler_handle_ = (jit_load_)(&callbacks);
117 if (jit_compiler_handle_ == nullptr) {
118 dlclose(jit_library_handle_);
119 *error_msg = "JIT couldn't load compiler";
120 return false;
121 }
122 if (callbacks == nullptr) {
123 dlclose(jit_library_handle_);
124 *error_msg = "JIT compiler callbacks were not set";
125 jit_compiler_handle_ = nullptr;
126 return false;
127 }
128 compiler_callbacks_ = callbacks;
129 return true;
130}
131
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133 DCHECK(!method->IsRuntimeMethod());
Mathieu Chartierd8565452015-03-26 09:41:50 -0700134 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
135 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
136 return false;
137 }
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000138 return jit_compile_method_(jit_compiler_handle_, method, self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139}
140
141void Jit::CreateThreadPool() {
142 CHECK(instrumentation_cache_.get() != nullptr);
143 instrumentation_cache_->CreateThreadPool();
144}
145
146void Jit::DeleteThreadPool() {
147 if (instrumentation_cache_.get() != nullptr) {
148 instrumentation_cache_->DeleteThreadPool();
149 }
150}
151
152Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700153 if (dump_info_on_shutdown_) {
154 DumpInfo(LOG(INFO));
155 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800156 DeleteThreadPool();
157 if (jit_compiler_handle_ != nullptr) {
158 jit_unload_(jit_compiler_handle_);
159 }
160 if (jit_library_handle_ != nullptr) {
161 dlclose(jit_library_handle_);
162 }
163}
164
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100165void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 CHECK_GT(compile_threshold, 0U);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700167 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168 // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile
169 // something.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100170 instrumentation_cache_.reset(
171 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700172 Runtime::Current()->GetInstrumentation()->AddListener(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800173 new jit::JitInstrumentationListener(instrumentation_cache_.get()),
174 instrumentation::Instrumentation::kMethodEntered |
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100175 instrumentation::Instrumentation::kBackwardBranch |
176 instrumentation::Instrumentation::kInvokeVirtualOrInterface);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177}
178
179} // namespace jit
180} // namespace art