blob: f69115159f656cdcae865d44ebaa1d675a237f35 [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);
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);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010041 jit_options->warmup_threshold_ =
42 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070043 jit_options->dump_info_on_shutdown_ =
44 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045 return jit_options;
46}
47
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070048void Jit::DumpInfo(std::ostream& os) {
49 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
50 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010051 << " number of compiled code=" << code_cache_->NumberOfCompiledCode()
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070052 << "\n";
53 cumulative_timings_.Dump(os);
54}
55
56void Jit::AddTimingLogger(const TimingLogger& logger) {
57 cumulative_timings_.AddLogger(logger);
58}
59
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080060Jit::Jit()
61 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070062 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
63 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080064}
65
66Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
67 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070068 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069 if (!jit->LoadCompiler(error_msg)) {
70 return nullptr;
71 }
72 jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg));
73 if (jit->GetCodeCache() == nullptr) {
74 return nullptr;
75 }
76 LOG(INFO) << "JIT created with code_cache_capacity="
77 << PrettySize(options->GetCodeCacheCapacity())
78 << " compile_threshold=" << options->GetCompileThreshold();
79 return jit.release();
80}
81
82bool Jit::LoadCompiler(std::string* error_msg) {
83 jit_library_handle_ = dlopen(
84 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
85 if (jit_library_handle_ == nullptr) {
86 std::ostringstream oss;
87 oss << "JIT could not load libart-compiler.so: " << dlerror();
88 *error_msg = oss.str();
89 return false;
90 }
91 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
92 dlsym(jit_library_handle_, "jit_load"));
93 if (jit_load_ == nullptr) {
94 dlclose(jit_library_handle_);
95 *error_msg = "JIT couldn't find jit_load entry point";
96 return false;
97 }
98 jit_unload_ = reinterpret_cast<void (*)(void*)>(
99 dlsym(jit_library_handle_, "jit_unload"));
100 if (jit_unload_ == nullptr) {
101 dlclose(jit_library_handle_);
102 *error_msg = "JIT couldn't find jit_unload entry point";
103 return false;
104 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800106 dlsym(jit_library_handle_, "jit_compile_method"));
107 if (jit_compile_method_ == nullptr) {
108 dlclose(jit_library_handle_);
109 *error_msg = "JIT couldn't find jit_compile_method entry point";
110 return false;
111 }
112 CompilerCallbacks* callbacks = nullptr;
113 VLOG(jit) << "Calling JitLoad interpreter_only="
114 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
115 jit_compiler_handle_ = (jit_load_)(&callbacks);
116 if (jit_compiler_handle_ == nullptr) {
117 dlclose(jit_library_handle_);
118 *error_msg = "JIT couldn't load compiler";
119 return false;
120 }
121 if (callbacks == nullptr) {
122 dlclose(jit_library_handle_);
123 *error_msg = "JIT compiler callbacks were not set";
124 jit_compiler_handle_ = nullptr;
125 return false;
126 }
127 compiler_callbacks_ = callbacks;
128 return true;
129}
130
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800132 DCHECK(!method->IsRuntimeMethod());
Mathieu Chartierd8565452015-03-26 09:41:50 -0700133 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
134 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
135 return false;
136 }
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000137 return jit_compile_method_(jit_compiler_handle_, method, self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800138}
139
140void Jit::CreateThreadPool() {
141 CHECK(instrumentation_cache_.get() != nullptr);
142 instrumentation_cache_->CreateThreadPool();
143}
144
145void Jit::DeleteThreadPool() {
146 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000147 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 }
149}
150
151Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700152 if (dump_info_on_shutdown_) {
153 DumpInfo(LOG(INFO));
154 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 DeleteThreadPool();
156 if (jit_compiler_handle_ != nullptr) {
157 jit_unload_(jit_compiler_handle_);
158 }
159 if (jit_library_handle_ != nullptr) {
160 dlclose(jit_library_handle_);
161 }
162}
163
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100164void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800165 CHECK_GT(compile_threshold, 0U);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100166 instrumentation_cache_.reset(
167 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168}
169
170} // namespace jit
171} // namespace art