blob: 27a0e2d1af49ec7fbe63da85469f32b79a3d5690 [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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "runtime.h"
30#include "runtime_options.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "utils.h"
32
33namespace art {
34namespace jit {
35
36JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037 auto* jit_options = new JitOptions;
Mathieu Chartier455f67c2015-03-17 13:48:29 -070038 jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000039 jit_options->code_cache_initial_capacity_ =
40 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
41 jit_options->code_cache_max_capacity_ =
42 options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080043 jit_options->compile_threshold_ =
44 options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010045 jit_options->warmup_threshold_ =
46 options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070047 jit_options->dump_info_on_shutdown_ =
48 options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
Calin Juravle31f2c152015-10-23 17:56:15 +010049 jit_options->save_profiling_info_ =
50 options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051 return jit_options;
52}
53
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070054void Jit::DumpInfo(std::ostream& os) {
55 os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize())
56 << " data cache size=" << PrettySize(code_cache_->DataCacheSize())
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010057 << " number of compiled code=" << code_cache_->NumberOfCompiledCode()
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070058 << "\n";
59 cumulative_timings_.Dump(os);
60}
61
62void Jit::AddTimingLogger(const TimingLogger& logger) {
63 cumulative_timings_.AddLogger(logger);
64}
65
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066Jit::Jit()
67 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070068 jit_compile_method_(nullptr), dump_info_on_shutdown_(false),
69 cumulative_timings_("JIT timings") {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070}
71
72Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
73 std::unique_ptr<Jit> jit(new Jit);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070074 jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075 if (!jit->LoadCompiler(error_msg)) {
76 return nullptr;
77 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000078 jit->code_cache_.reset(JitCodeCache::Create(
79 options->GetCodeCacheInitialCapacity(), options->GetCodeCacheMaxCapacity(), error_msg));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080 if (jit->GetCodeCache() == nullptr) {
81 return nullptr;
82 }
Calin Juravle31f2c152015-10-23 17:56:15 +010083 jit->offline_profile_info_.reset(nullptr);
Calin Juravle2c6ca2b2015-11-25 19:05:42 +000084 if (options->GetSaveProfilingInfo()) {
Calin Juravle31f2c152015-10-23 17:56:15 +010085 jit->offline_profile_info_.reset(new OfflineProfilingInfo());
86 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000087 LOG(INFO) << "JIT created with initial_capacity="
88 << PrettySize(options->GetCodeCacheInitialCapacity())
89 << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
90 << ", compile_threshold=" << options->GetCompileThreshold();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091 return jit.release();
92}
93
94bool Jit::LoadCompiler(std::string* error_msg) {
95 jit_library_handle_ = dlopen(
96 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
97 if (jit_library_handle_ == nullptr) {
98 std::ostringstream oss;
99 oss << "JIT could not load libart-compiler.so: " << dlerror();
100 *error_msg = oss.str();
101 return false;
102 }
103 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
104 dlsym(jit_library_handle_, "jit_load"));
105 if (jit_load_ == nullptr) {
106 dlclose(jit_library_handle_);
107 *error_msg = "JIT couldn't find jit_load entry point";
108 return false;
109 }
110 jit_unload_ = reinterpret_cast<void (*)(void*)>(
111 dlsym(jit_library_handle_, "jit_unload"));
112 if (jit_unload_ == nullptr) {
113 dlclose(jit_library_handle_);
114 *error_msg = "JIT couldn't find jit_unload entry point";
115 return false;
116 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800118 dlsym(jit_library_handle_, "jit_compile_method"));
119 if (jit_compile_method_ == nullptr) {
120 dlclose(jit_library_handle_);
121 *error_msg = "JIT couldn't find jit_compile_method entry point";
122 return false;
123 }
124 CompilerCallbacks* callbacks = nullptr;
125 VLOG(jit) << "Calling JitLoad interpreter_only="
126 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
127 jit_compiler_handle_ = (jit_load_)(&callbacks);
128 if (jit_compiler_handle_ == nullptr) {
129 dlclose(jit_library_handle_);
130 *error_msg = "JIT couldn't load compiler";
131 return false;
132 }
133 if (callbacks == nullptr) {
134 dlclose(jit_library_handle_);
135 *error_msg = "JIT compiler callbacks were not set";
136 jit_compiler_handle_ = nullptr;
137 return false;
138 }
139 compiler_callbacks_ = callbacks;
140 return true;
141}
142
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143bool Jit::CompileMethod(ArtMethod* method, Thread* self) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144 DCHECK(!method->IsRuntimeMethod());
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 Geoffray7bf2b4f2015-07-08 10:11:59 +0000149 return jit_compile_method_(jit_compiler_handle_, method, self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800150}
151
152void Jit::CreateThreadPool() {
153 CHECK(instrumentation_cache_.get() != nullptr);
154 instrumentation_cache_->CreateThreadPool();
155}
156
157void Jit::DeleteThreadPool() {
158 if (instrumentation_cache_.get() != nullptr) {
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000159 instrumentation_cache_->DeleteThreadPool(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 }
161}
162
Calin Juravle31f2c152015-10-23 17:56:15 +0100163void Jit::SaveProfilingInfo(const std::string& filename) {
164 if (offline_profile_info_ == nullptr) {
165 return;
166 }
167 // Note that we can't check the PrimaryOatFile when constructing the offline_profilie_info_
168 // because it becomes known to the Runtime after we create and initialize the JIT.
169 const OatFile* primary_oat_file = Runtime::Current()->GetOatFileManager().GetPrimaryOatFile();
170 if (primary_oat_file == nullptr) {
171 LOG(WARNING) << "Couldn't find a primary oat file when trying to save profile info to "
172 << filename;
173 return;
174 }
175
176 uint64_t last_update_ns = code_cache_->GetLastUpdateTimeNs();
177 if (offline_profile_info_->NeedsSaving(last_update_ns)) {
178 VLOG(profiler) << "Iniate save profiling information to: " << filename;
179 std::set<ArtMethod*> methods;
180 {
181 ScopedObjectAccess soa(Thread::Current());
182 code_cache_->GetCompiledArtMethods(primary_oat_file, methods);
183 }
184 offline_profile_info_->SaveProfilingInfo(filename, last_update_ns, methods);
185 } else {
186 VLOG(profiler) << "No need to save profiling information to: " << filename;
187 }
188}
189
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800190Jit::~Jit() {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191 if (dump_info_on_shutdown_) {
192 DumpInfo(LOG(INFO));
193 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800194 DeleteThreadPool();
195 if (jit_compiler_handle_ != nullptr) {
196 jit_unload_(jit_compiler_handle_);
197 }
198 if (jit_library_handle_ != nullptr) {
199 dlclose(jit_library_handle_);
200 }
201}
202
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100203void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800204 CHECK_GT(compile_threshold, 0U);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100205 instrumentation_cache_.reset(
206 new jit::JitInstrumentationCache(compile_threshold, warmup_threshold));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800207}
208
209} // namespace jit
210} // namespace art