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 | |
| 21 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 22 | #include "interpreter/interpreter.h" |
| 23 | #include "jit_code_cache.h" |
| 24 | #include "jit_instrumentation.h" |
| 25 | #include "mirror/art_method-inl.h" |
| 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) { |
| 35 | if (!options.GetOrDefault(RuntimeArgumentMap::UseJIT)) { |
| 36 | return nullptr; |
| 37 | } |
| 38 | auto* jit_options = new JitOptions; |
| 39 | jit_options->code_cache_capacity_ = |
| 40 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheCapacity); |
| 41 | jit_options->compile_threshold_ = |
| 42 | options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
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()) |
| 51 | << " num methods=" << code_cache_->NumMethods() |
| 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 | } |
| 105 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, mirror::ArtMethod*, Thread*)>( |
| 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 | |
| 131 | bool Jit::CompileMethod(mirror::ArtMethod* method, Thread* self) { |
| 132 | DCHECK(!method->IsRuntimeMethod()); |
| 133 | const bool result = jit_compile_method_(jit_compiler_handle_, method, self); |
| 134 | if (result) { |
| 135 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
| 136 | } |
| 137 | return result; |
| 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) { |
| 147 | instrumentation_cache_->DeleteThreadPool(); |
| 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 | |
| 164 | void Jit::CreateInstrumentationCache(size_t compile_threshold) { |
| 165 | CHECK_GT(compile_threshold, 0U); |
| 166 | Runtime* const runtime = Runtime::Current(); |
| 167 | runtime->GetThreadList()->SuspendAll(); |
| 168 | // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile |
| 169 | // something. |
| 170 | instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold)); |
| 171 | runtime->GetInstrumentation()->AddListener( |
| 172 | new jit::JitInstrumentationListener(instrumentation_cache_.get()), |
| 173 | instrumentation::Instrumentation::kMethodEntered | |
| 174 | instrumentation::Instrumentation::kBackwardBranch); |
| 175 | runtime->GetThreadList()->ResumeAll(); |
| 176 | } |
| 177 | |
| 178 | } // namespace jit |
| 179 | } // namespace art |