Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 22 | #include "debugger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 23 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 24 | #include "interpreter/interpreter.h" |
| 25 | #include "jit_code_cache.h" |
| 26 | #include "jit_instrumentation.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 27 | #include "oat_file_manager.h" |
| 28 | #include "offline_profiling_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 29 | #include "profile_saver.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
| 31 | #include "runtime_options.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 32 | #include "utils.h" |
| 33 | |
| 34 | namespace art { |
| 35 | namespace jit { |
| 36 | |
| 37 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 38 | auto* jit_options = new JitOptions; |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 39 | jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 40 | jit_options->code_cache_initial_capacity_ = |
| 41 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 42 | jit_options->code_cache_max_capacity_ = |
| 43 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 44 | jit_options->compile_threshold_ = |
| 45 | options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 46 | jit_options->warmup_threshold_ = |
| 47 | options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 48 | jit_options->dump_info_on_shutdown_ = |
| 49 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 50 | jit_options->save_profiling_info_ = |
| 51 | options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 52 | return jit_options; |
| 53 | } |
| 54 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 55 | void Jit::DumpInfo(std::ostream& os) { |
Nicolas Geoffray | aee2156 | 2015-12-15 16:39:44 +0000 | [diff] [blame] | 56 | os << "JIT code cache size=" << PrettySize(code_cache_->CodeCacheSize()) << "\n" |
| 57 | << "JIT data cache size=" << PrettySize(code_cache_->DataCacheSize()) << "\n" |
| 58 | << "JIT current capacity=" << PrettySize(code_cache_->GetCurrentCapacity()) << "\n" |
Nicolas Geoffray | 0a52223 | 2016-01-19 09:34:58 +0000 | [diff] [blame] | 59 | << "JIT number of compiled code=" << code_cache_->NumberOfCompiledCode() << "\n" |
| 60 | << "JIT total number of compilations=" << code_cache_->NumberOfCompilations() << "\n"; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 61 | cumulative_timings_.Dump(os); |
| 62 | } |
| 63 | |
| 64 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 65 | cumulative_timings_.AddLogger(logger); |
| 66 | } |
| 67 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 68 | Jit::Jit() : jit_library_handle_(nullptr), |
| 69 | jit_compiler_handle_(nullptr), |
| 70 | jit_load_(nullptr), |
| 71 | jit_compile_method_(nullptr), |
| 72 | dump_info_on_shutdown_(false), |
| 73 | cumulative_timings_("JIT timings"), |
| 74 | save_profiling_info_(false), |
| 75 | generate_debug_info_(false) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 79 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 80 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 81 | if (!jit->LoadCompiler(error_msg)) { |
| 82 | return nullptr; |
| 83 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 84 | jit->code_cache_.reset(JitCodeCache::Create( |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 85 | options->GetCodeCacheInitialCapacity(), |
| 86 | options->GetCodeCacheMaxCapacity(), |
| 87 | jit->generate_debug_info_, |
| 88 | error_msg)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 89 | if (jit->GetCodeCache() == nullptr) { |
| 90 | return nullptr; |
| 91 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 92 | jit->save_profiling_info_ = options->GetSaveProfilingInfo(); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 93 | LOG(INFO) << "JIT created with initial_capacity=" |
| 94 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 95 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 96 | << ", compile_threshold=" << options->GetCompileThreshold() |
| 97 | << ", save_profiling_info=" << options->GetSaveProfilingInfo(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 98 | return jit.release(); |
| 99 | } |
| 100 | |
| 101 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 102 | jit_library_handle_ = dlopen( |
| 103 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 104 | if (jit_library_handle_ == nullptr) { |
| 105 | std::ostringstream oss; |
| 106 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 107 | *error_msg = oss.str(); |
| 108 | return false; |
| 109 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 110 | jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**, bool*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 111 | dlsym(jit_library_handle_, "jit_load")); |
| 112 | if (jit_load_ == nullptr) { |
| 113 | dlclose(jit_library_handle_); |
| 114 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 115 | return false; |
| 116 | } |
| 117 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 118 | dlsym(jit_library_handle_, "jit_unload")); |
| 119 | if (jit_unload_ == nullptr) { |
| 120 | dlclose(jit_library_handle_); |
| 121 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 122 | return false; |
| 123 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 125 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 126 | if (jit_compile_method_ == nullptr) { |
| 127 | dlclose(jit_library_handle_); |
| 128 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 129 | return false; |
| 130 | } |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 131 | jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>( |
| 132 | dlsym(jit_library_handle_, "jit_types_loaded")); |
| 133 | if (jit_types_loaded_ == nullptr) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 134 | dlclose(jit_library_handle_); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 135 | *error_msg = "JIT couldn't find jit_types_loaded entry point"; |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 138 | CompilerCallbacks* callbacks = nullptr; |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 139 | bool will_generate_debug_symbols = false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 140 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 141 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 142 | jit_compiler_handle_ = (jit_load_)(&callbacks, &will_generate_debug_symbols); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 143 | if (jit_compiler_handle_ == nullptr) { |
| 144 | dlclose(jit_library_handle_); |
| 145 | *error_msg = "JIT couldn't load compiler"; |
| 146 | return false; |
| 147 | } |
| 148 | if (callbacks == nullptr) { |
| 149 | dlclose(jit_library_handle_); |
| 150 | *error_msg = "JIT compiler callbacks were not set"; |
| 151 | jit_compiler_handle_ = nullptr; |
| 152 | return false; |
| 153 | } |
| 154 | compiler_callbacks_ = callbacks; |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 155 | generate_debug_info_ = will_generate_debug_symbols; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 159 | bool Jit::CompileMethod(ArtMethod* method, Thread* self) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 160 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 161 | // Don't compile the method if it has breakpoints. |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 162 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 163 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 164 | return false; |
| 165 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 166 | |
| 167 | // Don't compile the method if we are supposed to be deoptimized. |
| 168 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 169 | if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | if (!code_cache_->NotifyCompilationOf(method, self)) { |
| 174 | return false; |
| 175 | } |
| 176 | bool success = jit_compile_method_(jit_compiler_handle_, method, self); |
| 177 | code_cache_->DoneCompiling(method, self); |
| 178 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void Jit::CreateThreadPool() { |
| 182 | CHECK(instrumentation_cache_.get() != nullptr); |
| 183 | instrumentation_cache_->CreateThreadPool(); |
| 184 | } |
| 185 | |
| 186 | void Jit::DeleteThreadPool() { |
| 187 | if (instrumentation_cache_.get() != nullptr) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 188 | instrumentation_cache_->DeleteThreadPool(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 192 | void Jit::StartProfileSaver(const std::string& filename, |
| 193 | const std::vector<std::string>& code_paths) { |
| 194 | if (save_profiling_info_) { |
| 195 | ProfileSaver::Start(filename, code_cache_.get(), code_paths); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 196 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void Jit::StopProfileSaver() { |
| 200 | if (save_profiling_info_ && ProfileSaver::IsStarted()) { |
| 201 | ProfileSaver::Stop(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame^] | 205 | bool Jit::JitAtFirstUse() { |
| 206 | if (instrumentation_cache_ != nullptr) { |
| 207 | return instrumentation_cache_->HotMethodThreshold() == 0; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 212 | Jit::~Jit() { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 213 | DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted()); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 214 | if (dump_info_on_shutdown_) { |
| 215 | DumpInfo(LOG(INFO)); |
| 216 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 217 | DeleteThreadPool(); |
| 218 | if (jit_compiler_handle_ != nullptr) { |
| 219 | jit_unload_(jit_compiler_handle_); |
| 220 | } |
| 221 | if (jit_library_handle_ != nullptr) { |
| 222 | dlclose(jit_library_handle_); |
| 223 | } |
| 224 | } |
| 225 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 226 | void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 227 | instrumentation_cache_.reset( |
| 228 | new jit::JitInstrumentationCache(compile_threshold, warmup_threshold)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 231 | void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { |
| 232 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 233 | if (jit != nullptr && jit->generate_debug_info_) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 234 | DCHECK(jit->jit_types_loaded_ != nullptr); |
| 235 | jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { |
| 240 | struct CollectClasses : public ClassVisitor { |
| 241 | bool Visit(mirror::Class* klass) override { |
| 242 | classes_.push_back(klass); |
| 243 | return true; |
| 244 | } |
| 245 | std::vector<mirror::Class*> classes_; |
| 246 | }; |
| 247 | |
| 248 | if (generate_debug_info_) { |
| 249 | ScopedObjectAccess so(Thread::Current()); |
| 250 | |
| 251 | CollectClasses visitor; |
| 252 | linker->VisitClasses(&visitor); |
| 253 | jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size()); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 257 | } // namespace jit |
| 258 | } // namespace art |