blob: 539c1819520dcdb4501897e647a2345a6d45dcac [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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
31namespace art {
32namespace jit {
33
34JitOptions* 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);
43 return jit_options;
44}
45
46Jit::Jit()
47 : jit_library_handle_(nullptr), jit_compiler_handle_(nullptr), jit_load_(nullptr),
48 jit_compile_method_(nullptr) {
49}
50
51Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
52 std::unique_ptr<Jit> jit(new Jit);
53 if (!jit->LoadCompiler(error_msg)) {
54 return nullptr;
55 }
56 jit->code_cache_.reset(JitCodeCache::Create(options->GetCodeCacheCapacity(), error_msg));
57 if (jit->GetCodeCache() == nullptr) {
58 return nullptr;
59 }
60 LOG(INFO) << "JIT created with code_cache_capacity="
61 << PrettySize(options->GetCodeCacheCapacity())
62 << " compile_threshold=" << options->GetCompileThreshold();
63 return jit.release();
64}
65
66bool Jit::LoadCompiler(std::string* error_msg) {
67 jit_library_handle_ = dlopen(
68 kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
69 if (jit_library_handle_ == nullptr) {
70 std::ostringstream oss;
71 oss << "JIT could not load libart-compiler.so: " << dlerror();
72 *error_msg = oss.str();
73 return false;
74 }
75 jit_load_ = reinterpret_cast<void* (*)(CompilerCallbacks**)>(
76 dlsym(jit_library_handle_, "jit_load"));
77 if (jit_load_ == nullptr) {
78 dlclose(jit_library_handle_);
79 *error_msg = "JIT couldn't find jit_load entry point";
80 return false;
81 }
82 jit_unload_ = reinterpret_cast<void (*)(void*)>(
83 dlsym(jit_library_handle_, "jit_unload"));
84 if (jit_unload_ == nullptr) {
85 dlclose(jit_library_handle_);
86 *error_msg = "JIT couldn't find jit_unload entry point";
87 return false;
88 }
89 jit_compile_method_ = reinterpret_cast<bool (*)(void*, mirror::ArtMethod*, Thread*)>(
90 dlsym(jit_library_handle_, "jit_compile_method"));
91 if (jit_compile_method_ == nullptr) {
92 dlclose(jit_library_handle_);
93 *error_msg = "JIT couldn't find jit_compile_method entry point";
94 return false;
95 }
96 CompilerCallbacks* callbacks = nullptr;
97 VLOG(jit) << "Calling JitLoad interpreter_only="
98 << Runtime::Current()->GetInstrumentation()->InterpretOnly();
99 jit_compiler_handle_ = (jit_load_)(&callbacks);
100 if (jit_compiler_handle_ == nullptr) {
101 dlclose(jit_library_handle_);
102 *error_msg = "JIT couldn't load compiler";
103 return false;
104 }
105 if (callbacks == nullptr) {
106 dlclose(jit_library_handle_);
107 *error_msg = "JIT compiler callbacks were not set";
108 jit_compiler_handle_ = nullptr;
109 return false;
110 }
111 compiler_callbacks_ = callbacks;
112 return true;
113}
114
115bool Jit::CompileMethod(mirror::ArtMethod* method, Thread* self) {
116 DCHECK(!method->IsRuntimeMethod());
117 const bool result = jit_compile_method_(jit_compiler_handle_, method, self);
118 if (result) {
119 method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge);
120 }
121 return result;
122}
123
124void Jit::CreateThreadPool() {
125 CHECK(instrumentation_cache_.get() != nullptr);
126 instrumentation_cache_->CreateThreadPool();
127}
128
129void Jit::DeleteThreadPool() {
130 if (instrumentation_cache_.get() != nullptr) {
131 instrumentation_cache_->DeleteThreadPool();
132 }
133}
134
135Jit::~Jit() {
136 DeleteThreadPool();
137 if (jit_compiler_handle_ != nullptr) {
138 jit_unload_(jit_compiler_handle_);
139 }
140 if (jit_library_handle_ != nullptr) {
141 dlclose(jit_library_handle_);
142 }
143}
144
145void Jit::CreateInstrumentationCache(size_t compile_threshold) {
146 CHECK_GT(compile_threshold, 0U);
147 Runtime* const runtime = Runtime::Current();
148 runtime->GetThreadList()->SuspendAll();
149 // Add Jit interpreter instrumentation, tells the interpreter when to notify the jit to compile
150 // something.
151 instrumentation_cache_.reset(new jit::JitInstrumentationCache(compile_threshold));
152 runtime->GetInstrumentation()->AddListener(
153 new jit::JitInstrumentationListener(instrumentation_cache_.get()),
154 instrumentation::Instrumentation::kMethodEntered |
155 instrumentation::Instrumentation::kBackwardBranch);
156 runtime->GetThreadList()->ResumeAll();
157}
158
159} // namespace jit
160} // namespace art