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