blob: f241308101c38467779ffa49858c80893ea2e2d0 [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"
Calin Juravle31f2c152015-10-23 17:56:15 +010027#include "oat_file_manager.h"
28#include "offline_profiling_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000029#include "profile_saver.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "runtime.h"
31#include "runtime_options.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "utils.h"
33
34namespace art {
35namespace jit {
36
37JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070039 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000040 jit_options->code_cache_initial_capacity_ =
41 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
42 jit_options->code_cache_max_capacity_ =
43 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044 jit_options->compile_threshold_ =
45 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010046 jit_options->warmup_threshold_ =
47 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070048 jit_options->dump_info_on_shutdown_ =
49 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle31f2c152015-10-23 17:56:15 +010050 jit_options->save_profiling_info_ =
51 options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052 return jit_options;
53}
54
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070055void Jit::DumpInfo(std::ostream& os) {
56 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
57 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010058 << " number of compiled code=" << code_cache_->NumberOfCompiledCode()
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070059 << "\n";
60 cumulative_timings_.Dump(os);
61}
62
63void Jit::AddTimingLogger(const TimingLogger& logger) {
64 cumulative_timings_.AddLogger(logger);
65}
66
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067Jit::Jit()
68 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070069 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000070 cumulative_timings_("JIT timings"), save_profiling_info_(false) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071}
72
73Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
74 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070075 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 if (!jit->LoadCompiler(error_msg)) {
77 return nullptr;
78 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000079 jit->code_cache_.reset(JitCodeCache::Create(
80 options->GetCodeCacheInitialCapacity(), options->GetCodeCacheMaxCapacity(), error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 if (jit->GetCodeCache() == nullptr) {
82 return nullptr;
83 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000084 jit->save_profiling_info_ = options->GetSaveProfilingInfo();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000085 LOG(INFO) << "JIT created with initial_capacity="
86 << PrettySize(options->GetCodeCacheInitialCapacity())
87 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
Calin Juravle4d77b6a2015-12-01 18:38:09 +000088 << ", compile_threshold=" << options->GetCompileThreshold()
89 << ", save_profiling_info=" << options->GetSaveProfilingInfo();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080090 return jit.release();
91}
92
93bool Jit::LoadCompiler(std::string* error_msg) {
94 jit_library_handle_ = dlopen(
95 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
96 if (jit_library_handle_ == nullptr) {
97 std::ostringstream oss;
98 oss << "JIT could not load libart-compiler.so: " << dlerror();
99 *error_msg = oss.str();
100 return false;
101 }
102 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
103 dlsym(jit_library_handle_, "jit_load"));
104 if (jit_load_ == nullptr) {
105 dlclose(jit_library_handle_);
106 *error_msg = "JIT couldn't find jit_load entry point";
107 return false;
108 }
109 jit_unload_ = reinterpret_cast<void (*)(void*)>(
110 dlsym(jit_library_handle_, "jit_unload"));
111 if (jit_unload_ == nullptr) {
112 dlclose(jit_library_handle_);
113 *error_msg = "JIT couldn't find jit_unload entry point";
114 return false;
115 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117 dlsym(jit_library_handle_, "jit_compile_method"));
118 if (jit_compile_method_ == nullptr) {
119 dlclose(jit_library_handle_);
120 *error_msg = "JIT couldn't find jit_compile_method entry point";
121 return false;
122 }
123 CompilerCallbacks* callbacks = nullptr;
124 VLOG(jit) << "Calling JitLoad interpreter_only="
125 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
126 jit_compiler_handle_ = (jit_load_)(&callbacks);
127 if (jit_compiler_handle_ == nullptr) {
128 dlclose(jit_library_handle_);
129 *error_msg = "JIT couldn't load compiler";
130 return false;
131 }
132 if (callbacks == nullptr) {
133 dlclose(jit_library_handle_);
134 *error_msg = "JIT compiler callbacks were not set";
135 jit_compiler_handle_ = nullptr;
136 return false;
137 }
138 compiler_callbacks_ = callbacks;
139 return true;
140}
141
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800143 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100144 // Don't compile the method if it has breakpoints.
Mathieu Chartierd8565452015-03-26 09:41:50 -0700145 if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
146 VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint";
147 return false;
148 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100149
150 // Don't compile the method if we are supposed to be deoptimized.
151 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
152 if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
153 return false;
154 }
155
156 if (!code_cache_->NotifyCompilationOf(method, self)) {
157 return false;
158 }
159 bool success = jit_compile_method_(jit_compiler_handle_, method, self);
160 code_cache_->DoneCompiling(method, self);
161 return success;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800162}
163
164void Jit::CreateThreadPool() {
165 CHECK(instrumentation_cache_.get() != nullptr);
166 instrumentation_cache_->CreateThreadPool();
167}
168
169void Jit::DeleteThreadPool() {
170 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000171 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172 }
173}
174
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000175void Jit::StartProfileSaver(const std::string& filename,
176 const std::vector<std::string>& code_paths) {
177 if (save_profiling_info_) {
178 ProfileSaver::Start(filename, code_cache_.get(), code_paths);
Calin Juravle31f2c152015-10-23 17:56:15 +0100179 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000180}
181
182void Jit::StopProfileSaver() {
183 if (save_profiling_info_ && ProfileSaver::IsStarted()) {
184 ProfileSaver::Stop();
Calin Juravle31f2c152015-10-23 17:56:15 +0100185 }
186}
187
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800188Jit::~Jit() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000189 DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted());
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700190 if (dump_info_on_shutdown_) {
191 DumpInfo(LOG(INFO));
192 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800193 DeleteThreadPool();
194 if (jit_compiler_handle_ != nullptr) {
195 jit_unload_(jit_compiler_handle_);
196 }
197 if (jit_library_handle_ != nullptr) {
198 dlclose(jit_library_handle_);
199 }
200}
201
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100202void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800203 CHECK_GT(compile_threshold, 0U);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100204 instrumentation_cache_.reset(
205 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800206}
207
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800208} // namespace jit
209} // namespace art