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