blob: 82b7ba7f3092d5ba88e5e14f44202850d4fa6e7b [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
21#include "entrypoints/runtime_asm_entrypoints.h"
22#include "interpreter/interpreter.h"
23#include "jit_code_cache.h"
24#include "jit_instrumentation.h"
25#include "mirror/art_method-inl.h"
26#include "runtime.h"
27#include "runtime_options.h"
28#include "thread_list.h"
29#include "utils.h"
30
31namespace art {
32namespace jit {
33
34JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartierbf9026d2015-03-13 11:34:02 -070035 if (kRuntimeISA == kArm64) {
36 return nullptr;
37 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038 if (!options.GetOrDefault(RuntimeArgumentMap::UseJIT)) {
39 return nullptr;
40 }
41 auto* jit_options = new JitOptions;
42 jit_options->code_cache_capacity_ =
43 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheCapacity);
44 jit_options->compile_threshold_ =
45 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070046 jit_options->dump_info_on_shutdown_ =
47 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080048 return jit_options;
49}
50
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070051void Jit::DumpInfo(std::ostream& os) {
52 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
53 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
54 << " num methods=" << code_cache_->NumMethods()
55 << "\n";
56 cumulative_timings_.Dump(os);
57}
58
59void Jit::AddTimingLogger(const TimingLogger& logger) {
60 cumulative_timings_.AddLogger(logger);
61}
62
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063Jit::Jit()
64 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070065 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
66 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067}
68
69Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
70 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070071 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072 if (!jit->LoadCompiler(error_msg)) {
73 return nullptr;
74 }
75 jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg));
76 if (jit->GetCodeCache() == nullptr) {
77 return nullptr;
78 }
79 LOG(INFO) << "JIT created with code_cache_capacity="
80 << PrettySize(options->GetCodeCacheCapacity())
81 << " compile_threshold=" << options->GetCompileThreshold();
82 return jit.release();
83}
84
85bool Jit::LoadCompiler(std::string* error_msg) {
86 jit_library_handle_ = dlopen(
87 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
88 if (jit_library_handle_ == nullptr) {
89 std::ostringstream oss;
90 oss << "JIT could not load libart-compiler.so: " << dlerror();
91 *error_msg = oss.str();
92 return false;
93 }
94 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
95 dlsym(jit_library_handle_, "jit_load"));
96 if (jit_load_ == nullptr) {
97 dlclose(jit_library_handle_);
98 *error_msg = "JIT couldn't find jit_load entry point";
99 return false;
100 }
101 jit_unload_ = reinterpret_cast<void (*)(void*)>(
102 dlsym(jit_library_handle_, "jit_unload"));
103 if (jit_unload_ == nullptr) {
104 dlclose(jit_library_handle_);
105 *error_msg = "JIT couldn't find jit_unload entry point";
106 return false;
107 }
108 jit_compile_method_ = reinterpret_cast<bool (*)(void*, mirror::ArtMethod*, Thread*)>(
109 dlsym(jit_library_handle_, "jit_compile_method"));
110 if (jit_compile_method_ == nullptr) {
111 dlclose(jit_library_handle_);
112 *error_msg = "JIT couldn't find jit_compile_method entry point";
113 return false;
114 }
115 CompilerCallbacks* callbacks = nullptr;
116 VLOG(jit) << "Calling JitLoad interpreter_only="
117 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
118 jit_compiler_handle_ = (jit_load_)(&callbacks);
119 if (jit_compiler_handle_ == nullptr) {
120 dlclose(jit_library_handle_);
121 *error_msg = "JIT couldn't load compiler";
122 return false;
123 }
124 if (callbacks == nullptr) {
125 dlclose(jit_library_handle_);
126 *error_msg = "JIT compiler callbacks were not set";
127 jit_compiler_handle_ = nullptr;
128 return false;
129 }
130 compiler_callbacks_ = callbacks;
131 return true;
132}
133
134bool Jit::CompileMethod(mirror::ArtMethod* method, Thread* self) {
135 DCHECK(!method->IsRuntimeMethod());
136 const bool result = jit_compile_method_(jit_compiler_handle_, method, self);
137 if (result) {
138 method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge);
139 }
140 return result;
141}
142
143void Jit::CreateThreadPool() {
144 CHECK(instrumentation_cache_.get() != nullptr);
145 instrumentation_cache_->CreateThreadPool();
146}
147
148void Jit::DeleteThreadPool() {
149 if (instrumentation_cache_.get() != nullptr) {
150 instrumentation_cache_->DeleteThreadPool();
151 }
152}
153
154Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700155 if (dump_info_on_shutdown_) {
156 DumpInfo(LOG(INFO));
157 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 DeleteThreadPool();
159 if (jit_compiler_handle_ != nullptr) {
160 jit_unload_(jit_compiler_handle_);
161 }
162 if (jit_library_handle_ != nullptr) {
163 dlclose(jit_library_handle_);
164 }
165}
166
167void Jit::CreateInstrumentationCache(size_t compile_threshold) {
168 CHECK_GT(compile_threshold, 0U);
169 Runtime* const runtime = Runtime::Current();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700170 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile
172 // something.
173 instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold));
174 runtime->GetInstrumentation()->AddListener(
175 new jit::JitInstrumentationListener(instrumentation_cache_.get()),
176 instrumentation::Instrumentation::kMethodEntered |
177 instrumentation::Instrumentation::kBackwardBranch);
178 runtime->GetThreadList()->ResumeAll();
179}
180
181} // namespace jit
182} // namespace art