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" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 27 | #include "runtime.h" |
| 28 | #include "runtime_options.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace jit { |
| 33 | |
| 34 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | auto* jit_options = new JitOptions; |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 36 | jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 37 | jit_options->code_cache_initial_capacity_ = |
| 38 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 39 | jit_options->code_cache_max_capacity_ = |
| 40 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 41 | jit_options->compile_threshold_ = |
| 42 | options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 43 | jit_options->warmup_threshold_ = |
| 44 | options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 45 | jit_options->dump_info_on_shutdown_ = |
| 46 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 47 | return jit_options; |
| 48 | } |
| 49 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 50 | void Jit::DumpInfo(std::ostream& os) { |
| 51 | os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize()) |
| 52 | << " data cache size=" << PrettySize(code_cache_->DataCacheSize()) |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 53 | << " number of compiled code=" << code_cache_->NumberOfCompiledCode() |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 54 | << "\n"; |
| 55 | cumulative_timings_.Dump(os); |
| 56 | } |
| 57 | |
| 58 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 59 | cumulative_timings_.AddLogger(logger); |
| 60 | } |
| 61 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 62 | Jit::Jit() |
| 63 | : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr), |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 64 | jit_compile_method_(nullptr), dump_info_on_shutdown_(false), |
| 65 | cumulative_timings_("JIT timings") { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 69 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 70 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 71 | if (!jit->LoadCompiler(error_msg)) { |
| 72 | return nullptr; |
| 73 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 74 | jit->code_cache_.reset(JitCodeCache::Create( |
| 75 | options->GetCodeCacheInitialCapacity(), options->GetCodeCacheMaxCapacity(), error_msg)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 76 | if (jit->GetCodeCache() == nullptr) { |
| 77 | return nullptr; |
| 78 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 79 | LOG(INFO) << "JIT created with initial_capacity=" |
| 80 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 81 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
| 82 | << ", compile_threshold=" << options->GetCompileThreshold(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 83 | return jit.release(); |
| 84 | } |
| 85 | |
| 86 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 87 | jit_library_handle_ = dlopen( |
| 88 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 89 | if (jit_library_handle_ == nullptr) { |
| 90 | std::ostringstream oss; |
| 91 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 92 | *error_msg = oss.str(); |
| 93 | return false; |
| 94 | } |
| 95 | jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>( |
| 96 | dlsym(jit_library_handle_, "jit_load")); |
| 97 | if (jit_load_ == nullptr) { |
| 98 | dlclose(jit_library_handle_); |
| 99 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 100 | return false; |
| 101 | } |
| 102 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 103 | dlsym(jit_library_handle_, "jit_unload")); |
| 104 | if (jit_unload_ == nullptr) { |
| 105 | dlclose(jit_library_handle_); |
| 106 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 107 | return false; |
| 108 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 109 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 110 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 111 | if (jit_compile_method_ == nullptr) { |
| 112 | dlclose(jit_library_handle_); |
| 113 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 114 | return false; |
| 115 | } |
| 116 | CompilerCallbacks* callbacks = nullptr; |
| 117 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 118 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
| 119 | jit_compiler_handle_ = (jit_load_)(&callbacks); |
| 120 | if (jit_compiler_handle_ == nullptr) { |
| 121 | dlclose(jit_library_handle_); |
| 122 | *error_msg = "JIT couldn't load compiler"; |
| 123 | return false; |
| 124 | } |
| 125 | if (callbacks == nullptr) { |
| 126 | dlclose(jit_library_handle_); |
| 127 | *error_msg = "JIT compiler callbacks were not set"; |
| 128 | jit_compiler_handle_ = nullptr; |
| 129 | return false; |
| 130 | } |
| 131 | compiler_callbacks_ = callbacks; |
| 132 | return true; |
| 133 | } |
| 134 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 135 | bool Jit::CompileMethod(ArtMethod* method, Thread* self) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 136 | DCHECK(!method->IsRuntimeMethod()); |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 137 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 138 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 139 | return false; |
| 140 | } |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 141 | return jit_compile_method_(jit_compiler_handle_, method, self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void Jit::CreateThreadPool() { |
| 145 | CHECK(instrumentation_cache_.get() != nullptr); |
| 146 | instrumentation_cache_->CreateThreadPool(); |
| 147 | } |
| 148 | |
| 149 | void Jit::DeleteThreadPool() { |
| 150 | if (instrumentation_cache_.get() != nullptr) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 151 | instrumentation_cache_->DeleteThreadPool(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | Jit::~Jit() { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 156 | if (dump_info_on_shutdown_) { |
| 157 | DumpInfo(LOG(INFO)); |
| 158 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 159 | DeleteThreadPool(); |
| 160 | if (jit_compiler_handle_ != nullptr) { |
| 161 | jit_unload_(jit_compiler_handle_); |
| 162 | } |
| 163 | if (jit_library_handle_ != nullptr) { |
| 164 | dlclose(jit_library_handle_); |
| 165 | } |
| 166 | } |
| 167 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 168 | void Jit::CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 169 | CHECK_GT(compile_threshold, 0U); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 170 | instrumentation_cache_.reset( |
| 171 | new jit::JitInstrumentationCache(compile_threshold, warmup_threshold)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | } // namespace jit |
| 175 | } // namespace art |