blob: 13c1f813bdf80954b77d8936e565564807757b16 [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 Chartiere5f13e52015-02-24 09:37:21 -080035 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070036 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037 jit_options->code_cache_capacity_ =
38 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheCapacity);
39 jit_options->compile_threshold_ =
40 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070041 jit_options->dump_info_on_shutdown_ =
42 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080043 return jit_options;
44}
45
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070046void Jit::DumpInfo(std::ostream& os) {
47 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
48 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
49 << " num methods=" << code_cache_->NumMethods()
50 << "\n";
51 cumulative_timings_.Dump(os);
52}
53
54void Jit::AddTimingLogger(const TimingLogger& logger) {
55 cumulative_timings_.AddLogger(logger);
56}
57
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058Jit::Jit()
59 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070060 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
61 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062}
63
64Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
65 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070066 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067 if (!jit->LoadCompiler(error_msg)) {
68 return nullptr;
69 }
70 jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg));
71 if (jit->GetCodeCache() == nullptr) {
72 return nullptr;
73 }
74 LOG(INFO) << "JIT created with code_cache_capacity="
75 << PrettySize(options->GetCodeCacheCapacity())
76 << " compile_threshold=" << options->GetCompileThreshold();
77 return jit.release();
78}
79
80bool Jit::LoadCompiler(std::string* error_msg) {
81 jit_library_handle_ = dlopen(
82 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
83 if (jit_library_handle_ == nullptr) {
84 std::ostringstream oss;
85 oss << "JIT could not load libart-compiler.so: " << dlerror();
86 *error_msg = oss.str();
87 return false;
88 }
89 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
90 dlsym(jit_library_handle_, "jit_load"));
91 if (jit_load_ == nullptr) {
92 dlclose(jit_library_handle_);
93 *error_msg = "JIT couldn't find jit_load entry point";
94 return false;
95 }
96 jit_unload_ = reinterpret_cast<void (*)(void*)>(
97 dlsym(jit_library_handle_, "jit_unload"));
98 if (jit_unload_ == nullptr) {
99 dlclose(jit_library_handle_);
100 *error_msg = "JIT couldn't find jit_unload entry point";
101 return false;
102 }
103 jit_compile_method_ = reinterpret_cast<bool (*)(void*, mirror::ArtMethod*, Thread*)>(
104 dlsym(jit_library_handle_, "jit_compile_method"));
105 if (jit_compile_method_ == nullptr) {
106 dlclose(jit_library_handle_);
107 *error_msg = "JIT couldn't find jit_compile_method entry point";
108 return false;
109 }
110 CompilerCallbacks* callbacks = nullptr;
111 VLOG(jit) << "Calling JitLoad interpreter_only="
112 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
113 jit_compiler_handle_ = (jit_load_)(&callbacks);
114 if (jit_compiler_handle_ == nullptr) {
115 dlclose(jit_library_handle_);
116 *error_msg = "JIT couldn't load compiler";
117 return false;
118 }
119 if (callbacks == nullptr) {
120 dlclose(jit_library_handle_);
121 *error_msg = "JIT compiler callbacks were not set";
122 jit_compiler_handle_ = nullptr;
123 return false;
124 }
125 compiler_callbacks_ = callbacks;
126 return true;
127}
128
129bool Jit::CompileMethod(mirror::ArtMethod* method, Thread* self) {
130 DCHECK(!method->IsRuntimeMethod());
131 const bool result = jit_compile_method_(jit_compiler_handle_, method, self);
132 if (result) {
133 method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge);
134 }
135 return result;
136}
137
138void Jit::CreateThreadPool() {
139 CHECK(instrumentation_cache_.get() != nullptr);
140 instrumentation_cache_->CreateThreadPool();
141}
142
143void Jit::DeleteThreadPool() {
144 if (instrumentation_cache_.get() != nullptr) {
145 instrumentation_cache_->DeleteThreadPool();
146 }
147}
148
149Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700150 if (dump_info_on_shutdown_) {
151 DumpInfo(LOG(INFO));
152 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 DeleteThreadPool();
154 if (jit_compiler_handle_ != nullptr) {
155 jit_unload_(jit_compiler_handle_);
156 }
157 if (jit_library_handle_ != nullptr) {
158 dlclose(jit_library_handle_);
159 }
160}
161
162void Jit::CreateInstrumentationCache(size_t compile_threshold) {
163 CHECK_GT(compile_threshold, 0U);
164 Runtime* const runtime = Runtime::Current();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700165 runtime->GetThreadList()->SuspendAll(__FUNCTION__);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile
167 // something.
168 instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold));
169 runtime->GetInstrumentation()->AddListener(
170 new jit::JitInstrumentationListener(instrumentation_cache_.get()),
171 instrumentation::Instrumentation::kMethodEntered |
172 instrumentation::Instrumentation::kBackwardBranch);
173 runtime->GetThreadList()->ResumeAll();
174}
175
176} // namespace jit
177} // namespace art