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" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 22 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 23 | #include "interpreter/interpreter.h" |
| 24 | #include "jit_code_cache.h" |
| 25 | #include "jit_instrumentation.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 26 | #include "runtime.h" |
| 27 | #include "runtime_options.h" |
| 28 | #include "thread_list.h" |
| 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); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 41 | jit_options->dump_info_on_shutdown_ = |
| 42 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 43 | return jit_options; |
| 44 | } |
| 45 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 46 | void Jit::DumpInfo(std::ostream& os) { |
| 47 | os << "Code cache size=" << PrettySize(code_cache_->CodeCacheSize()) |
| 48 | << " data cache size=" << PrettySize(code_cache_->DataCacheSize()) |
| 49 | << " num methods=" << code_cache_->NumMethods() |
| 50 | << "\n"; |
| 51 | cumulative_timings_.Dump(os); |
| 52 | } |
| 53 | |
| 54 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 55 | cumulative_timings_.AddLogger(logger); |
| 56 | } |
| 57 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 58 | Jit::Jit() |
| 59 | : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr), |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 60 | jit_compile_method_(nullptr), dump_info_on_shutdown_(false), |
| 61 | cumulative_timings_("JIT timings") { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
| 65 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 66 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 67 | if (!jit->LoadCompiler(error_msg)) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg)); |
| 71 | if (jit->GetCodeCache() == nullptr) { |
| 72 | return nullptr; |
| 73 | } |
| 74 | LOG(INFO) << "JIT created with code_cache_capacity=" |
| 75 | << PrettySize(options->GetCodeCacheCapacity()) |
| 76 | << " compile_threshold=" << options->GetCompileThreshold(); |
| 77 | return jit.release(); |
| 78 | } |
| 79 | |
| 80 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 81 | jit_library_handle_ = dlopen( |
| 82 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 83 | if (jit_library_handle_ == nullptr) { |
| 84 | std::ostringstream oss; |
| 85 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 86 | *error_msg = oss.str(); |
| 87 | return false; |
| 88 | } |
| 89 | jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>( |
| 90 | dlsym(jit_library_handle_, "jit_load")); |
| 91 | if (jit_load_ == nullptr) { |
| 92 | dlclose(jit_library_handle_); |
| 93 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 94 | return false; |
| 95 | } |
| 96 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 97 | dlsym(jit_library_handle_, "jit_unload")); |
| 98 | if (jit_unload_ == nullptr) { |
| 99 | dlclose(jit_library_handle_); |
| 100 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 101 | return false; |
| 102 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 103 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 104 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 105 | if (jit_compile_method_ == nullptr) { |
| 106 | dlclose(jit_library_handle_); |
| 107 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 108 | return false; |
| 109 | } |
| 110 | CompilerCallbacks* callbacks = nullptr; |
| 111 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 112 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
| 113 | jit_compiler_handle_ = (jit_load_)(&callbacks); |
| 114 | if (jit_compiler_handle_ == nullptr) { |
| 115 | dlclose(jit_library_handle_); |
| 116 | *error_msg = "JIT couldn't load compiler"; |
| 117 | return false; |
| 118 | } |
| 119 | if (callbacks == nullptr) { |
| 120 | dlclose(jit_library_handle_); |
| 121 | *error_msg = "JIT compiler callbacks were not set"; |
| 122 | jit_compiler_handle_ = nullptr; |
| 123 | return false; |
| 124 | } |
| 125 | compiler_callbacks_ = callbacks; |
| 126 | return true; |
| 127 | } |
| 128 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 129 | bool Jit::CompileMethod(ArtMethod* method, Thread* self) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 130 | DCHECK(!method->IsRuntimeMethod()); |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 131 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 132 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 133 | return false; |
| 134 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 135 | const bool result = jit_compile_method_(jit_compiler_handle_, method, self); |
| 136 | if (result) { |
| 137 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
| 138 | } |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | void Jit::CreateThreadPool() { |
| 143 | CHECK(instrumentation_cache_.get() != nullptr); |
| 144 | instrumentation_cache_->CreateThreadPool(); |
| 145 | } |
| 146 | |
| 147 | void Jit::DeleteThreadPool() { |
| 148 | if (instrumentation_cache_.get() != nullptr) { |
| 149 | instrumentation_cache_->DeleteThreadPool(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | Jit::~Jit() { |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 154 | if (dump_info_on_shutdown_) { |
| 155 | DumpInfo(LOG(INFO)); |
| 156 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 157 | DeleteThreadPool(); |
| 158 | if (jit_compiler_handle_ != nullptr) { |
| 159 | jit_unload_(jit_compiler_handle_); |
| 160 | } |
| 161 | if (jit_library_handle_ != nullptr) { |
| 162 | dlclose(jit_library_handle_); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void Jit::CreateInstrumentationCache(size_t compile_threshold) { |
| 167 | CHECK_GT(compile_threshold, 0U); |
| 168 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | bf9fc58 | 2015-03-13 17:21:25 -0700 | [diff] [blame] | 169 | runtime->GetThreadList()->SuspendAll(__FUNCTION__); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 170 | // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile |
| 171 | // something. |
| 172 | instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold)); |
| 173 | runtime->GetInstrumentation()->AddListener( |
| 174 | new jit::JitInstrumentationListener(instrumentation_cache_.get()), |
| 175 | instrumentation::Instrumentation::kMethodEntered | |
| 176 | instrumentation::Instrumentation::kBackwardBranch); |
| 177 | runtime->GetThreadList()->ResumeAll(); |
| 178 | } |
| 179 | |
| 180 | } // namespace jit |
| 181 | } // namespace art |