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" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 29 | #include "runtime.h" |
| 30 | #include "runtime_options.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace jit { |
| 35 | |
| 36 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | auto* jit_options = new JitOptions; |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 38 | jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 39 | jit_options->code_cache_initial_capacity_ = |
| 40 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 41 | jit_options->code_cache_max_capacity_ = |
| 42 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 43 | jit_options->compile_threshold_ = |
| 44 | options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 45 | jit_options->warmup_threshold_ = |
| 46 | options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 47 | jit_options->dump_info_on_shutdown_ = |
| 48 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 49 | jit_options->save_profiling_info_ = |
| 50 | options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 51 | return jit_options; |
| 52 | } |
| 53 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 54 | void Jit::DumpInfo(std::ostream& os) { |
| 55 | os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize()) |
| 56 | << " data cache size=" << PrettySize(code_cache_->DataCacheSize()) |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 57 | << " number of compiled code=" << code_cache_->NumberOfCompiledCode() |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 58 | << "\n"; |
| 59 | cumulative_timings_.Dump(os); |
| 60 | } |
| 61 | |
| 62 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 63 | cumulative_timings_.AddLogger(logger); |
| 64 | } |
| 65 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 66 | Jit::Jit() |
| 67 | : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr), |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 68 | jit_compile_method_(nullptr), dump_info_on_shutdown_(false), |
| 69 | cumulative_timings_("JIT timings") { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 73 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 74 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 75 | if (!jit->LoadCompiler(error_msg)) { |
| 76 | return nullptr; |
| 77 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 78 | jit->code_cache_.reset(JitCodeCache::Create( |
| 79 | options->GetCodeCacheInitialCapacity(), options->GetCodeCacheMaxCapacity(), error_msg)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 80 | if (jit->GetCodeCache() == nullptr) { |
| 81 | return nullptr; |
| 82 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 83 | jit->offline_profile_info_.reset(nullptr); |
Calin Juravle | 2c6ca2b | 2015-11-25 19:05:42 +0000 | [diff] [blame] | 84 | if (options->GetSaveProfilingInfo()) { |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 85 | jit->offline_profile_info_.reset(new OfflineProfilingInfo()); |
| 86 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 87 | LOG(INFO) << "JIT created with initial_capacity=" |
| 88 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 89 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
| 90 | << ", compile_threshold=" << options->GetCompileThreshold(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 91 | return jit.release(); |
| 92 | } |
| 93 | |
| 94 | bool 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 117 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 118 | 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 143 | bool Jit::CompileMethod(ArtMethod* method, Thread* self) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 144 | DCHECK(!method->IsRuntimeMethod()); |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 145 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 146 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 147 | return false; |
| 148 | } |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 149 | return jit_compile_method_(jit_compiler_handle_, method, self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void Jit::CreateThreadPool() { |
| 153 | CHECK(instrumentation_cache_.get() != nullptr); |
| 154 | instrumentation_cache_->CreateThreadPool(); |
| 155 | } |
| 156 | |
| 157 | void Jit::DeleteThreadPool() { |
| 158 | if (instrumentation_cache_.get() != nullptr) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 159 | instrumentation_cache_->DeleteThreadPool(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 163 | void 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 190 | Jit::~Jit() { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 191 | if (dump_info_on_shutdown_) { |
| 192 | DumpInfo(LOG(INFO)); |
| 193 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 194 | 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 Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 203 | void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 204 | CHECK_GT(compile_threshold, 0U); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 205 | instrumentation_cache_.reset( |
| 206 | new jit::JitInstrumentationCache(compile_threshold, warmup_threshold)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | } // namespace jit |
| 210 | } // namespace art |