blob: ecbf13c4b11ab99a3a1cf70abf409783c1cbfa2c [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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#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);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000037 jit_options->code_cache_initial_capacity_ =
38 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
39 jit_options->code_cache_max_capacity_ =
40 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041 jit_options->compile_threshold_ =
42 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010043 jit_options->warmup_threshold_ =
44 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070045 jit_options->dump_info_on_shutdown_ =
46 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047 return jit_options;
48}
49
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070050void Jit::DumpInfo(std::ostream& os) {
51 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
52 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010053 << " number of compiled code=" << code_cache_->NumberOfCompiledCode()
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070054 << "\n";
55 cumulative_timings_.Dump(os);
56}
57
58void Jit::AddTimingLogger(const TimingLogger& logger) {
59 cumulative_timings_.AddLogger(logger);
60}
61
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062Jit::Jit()
63 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070064 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
65 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066}
67
68Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
69 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070070 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071 if (!jit->LoadCompiler(error_msg)) {
72 return nullptr;
73 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000074 jit->code_cache_.reset(JitCodeCache::Create(
75 options->GetCodeCacheInitialCapacity(), options->GetCodeCacheMaxCapacity(), error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 if (jit->GetCodeCache() == nullptr) {
77 return nullptr;
78 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000079 LOG(INFO) << "JIT created with initial_capacity="
80 << PrettySize(options->GetCodeCacheInitialCapacity())
81 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
82 << ", compile_threshold=" << options->GetCompileThreshold();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080083 return jit.release();
84}
85
86bool Jit::LoadCompiler(std::string* error_msg) {
87 jit_library_handle_ = dlopen(
88 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
89 if (jit_library_handle_ == nullptr) {
90 std::ostringstream oss;
91 oss << "JIT could not load libart-compiler.so: " << dlerror();
92 *error_msg = oss.str();
93 return false;
94 }
95 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
96 dlsym(jit_library_handle_, "jit_load"));
97 if (jit_load_ == nullptr) {
98 dlclose(jit_library_handle_);
99 *error_msg = "JIT couldn't find jit_load entry point";
100 return false;
101 }
102 jit_unload_ = reinterpret_cast<void (*)(void*)>(
103 dlsym(jit_library_handle_, "jit_unload"));
104 if (jit_unload_ == nullptr) {
105 dlclose(jit_library_handle_);
106 *error_msg = "JIT couldn't find jit_unload entry point";
107 return false;
108 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700109 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110 dlsym(jit_library_handle_, "jit_compile_method"));
111 if (jit_compile_method_ == nullptr) {
112 dlclose(jit_library_handle_);
113 *error_msg = "JIT couldn't find jit_compile_method entry point";
114 return false;
115 }
116 CompilerCallbacks* callbacks = nullptr;
117 VLOG(jit) << "Calling JitLoad interpreter_only="
118 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
119 jit_compiler_handle_ = (jit_load_)(&callbacks);
120 if (jit_compiler_handle_ == nullptr) {
121 dlclose(jit_library_handle_);
122 *error_msg = "JIT couldn't load compiler";
123 return false;
124 }
125 if (callbacks == nullptr) {
126 dlclose(jit_library_handle_);
127 *error_msg = "JIT compiler callbacks were not set";
128 jit_compiler_handle_ = nullptr;
129 return false;
130 }
131 compiler_callbacks_ = callbacks;
132 return true;
133}
134
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800136 DCHECK(!method->IsRuntimeMethod());
Mathieu Chartierd8565452015-03-26 09:41:50 -0700137 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
138 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
139 return false;
140 }
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000141 return jit_compile_method_(jit_compiler_handle_, method, self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800142}
143
144void Jit::CreateThreadPool() {
145 CHECK(instrumentation_cache_.get() != nullptr);
146 instrumentation_cache_->CreateThreadPool();
147}
148
149void Jit::DeleteThreadPool() {
150 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000151 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800152 }
153}
154
155Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156 if (dump_info_on_shutdown_) {
157 DumpInfo(LOG(INFO));
158 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159 DeleteThreadPool();
160 if (jit_compiler_handle_ != nullptr) {
161 jit_unload_(jit_compiler_handle_);
162 }
163 if (jit_library_handle_ != nullptr) {
164 dlclose(jit_library_handle_);
165 }
166}
167
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100168void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800169 CHECK_GT(compile_threshold, 0U);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100170 instrumentation_cache_.reset(
171 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172}
173
174} // namespace jit
175} // namespace art