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" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 26 | #include "oat_file_manager.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 27 | #include "oat_quick_method_header.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 28 | #include "offline_profiling_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 29 | #include "profile_saver.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
| 31 | #include "runtime_options.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 32 | #include "stack_map.h" |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 33 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "utils.h" |
| 35 | |
| 36 | namespace art { |
| 37 | namespace jit { |
| 38 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 39 | static constexpr bool kEnableOnStackReplacement = true; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 40 | // At what priority to schedule jit threads. 9 is the lowest foreground priority on device. |
| 41 | static constexpr int kJitPoolThreadPthreadPriority = 9; |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 42 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 43 | // JIT compiler |
| 44 | void* Jit::jit_library_handle_= nullptr; |
| 45 | void* Jit::jit_compiler_handle_ = nullptr; |
| 46 | void* (*Jit::jit_load_)(bool*) = nullptr; |
| 47 | void (*Jit::jit_unload_)(void*) = nullptr; |
| 48 | bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr; |
| 49 | void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr; |
| 50 | bool Jit::generate_debug_info_ = false; |
| 51 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 52 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 53 | auto* jit_options = new JitOptions; |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 54 | jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 55 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 56 | jit_options->code_cache_initial_capacity_ = |
| 57 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 58 | jit_options->code_cache_max_capacity_ = |
| 59 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 60 | jit_options->dump_info_on_shutdown_ = |
| 61 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 62 | jit_options->save_profiling_info_ = |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 63 | options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo); |
| 64 | |
| 65 | jit_options->compile_threshold_ = options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); |
| 66 | if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 67 | LOG(FATAL) << "Method compilation threshold is above its internal limit."; |
| 68 | } |
| 69 | |
| 70 | if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) { |
| 71 | jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold); |
| 72 | if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 73 | LOG(FATAL) << "Method warmup threshold is above its internal limit."; |
| 74 | } |
| 75 | } else { |
| 76 | jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2; |
| 77 | } |
| 78 | |
| 79 | if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) { |
| 80 | jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold); |
| 81 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 82 | LOG(FATAL) << "Method on stack replacement threshold is above its internal limit."; |
| 83 | } |
| 84 | } else { |
| 85 | jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2; |
| 86 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
| 87 | jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max(); |
| 88 | } |
| 89 | } |
| 90 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 91 | if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) { |
| 92 | jit_options->priority_thread_weight_ = |
| 93 | *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight); |
| 94 | if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) { |
| 95 | LOG(FATAL) << "Priority thread weight is above the warmup threshold."; |
| 96 | } else if (jit_options->priority_thread_weight_ == 0) { |
| 97 | LOG(FATAL) << "Priority thread weight cannot be 0."; |
| 98 | } |
| 99 | } else { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 100 | jit_options->priority_thread_weight_ = std::max( |
| 101 | jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio, |
| 102 | static_cast<size_t>(1)); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 105 | if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 106 | jit_options->invoke_transition_weight_ = |
| 107 | *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 108 | if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) { |
| 109 | LOG(FATAL) << "Invoke transition weight is above the warmup threshold."; |
| 110 | } else if (jit_options->invoke_transition_weight_ == 0) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 111 | LOG(FATAL) << "Invoke transition weight cannot be 0."; |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 112 | } |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 113 | } else { |
| 114 | jit_options->invoke_transition_weight_ = std::max( |
| 115 | jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio, |
| 116 | static_cast<size_t>(1));; |
| 117 | } |
| 118 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 119 | return jit_options; |
| 120 | } |
| 121 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 122 | bool Jit::ShouldUsePriorityThreadWeight() { |
Calin Juravle | 97cbc92 | 2016-04-15 16:16:35 +0100 | [diff] [blame] | 123 | return Runtime::Current()->InJankPerceptibleProcessState() |
| 124 | && Thread::Current()->IsJitSensitiveThread(); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 127 | void Jit::DumpInfo(std::ostream& os) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 128 | code_cache_->Dump(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 129 | cumulative_timings_.Dump(os); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 130 | MutexLock mu(Thread::Current(), lock_); |
| 131 | memory_use_.PrintMemoryUse(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 134 | void Jit::DumpForSigQuit(std::ostream& os) { |
| 135 | DumpInfo(os); |
| 136 | ProfileSaver::DumpInstanceInfo(os); |
| 137 | } |
| 138 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 139 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 140 | cumulative_timings_.AddLogger(logger); |
| 141 | } |
| 142 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 143 | Jit::Jit() : dump_info_on_shutdown_(false), |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 144 | cumulative_timings_("JIT timings"), |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 145 | memory_use_("Memory used for compilation", 16), |
| 146 | lock_("JIT memory use lock"), |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 147 | use_jit_compilation_(true), |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 148 | save_profiling_info_(false) {} |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 149 | |
| 150 | Jit* Jit::Create(JitOptions* options, std::string* error_msg) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 151 | DCHECK(options->UseJitCompilation() || options->GetSaveProfilingInfo()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 152 | std::unique_ptr<Jit> jit(new Jit); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 153 | jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 154 | if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 155 | return nullptr; |
| 156 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 157 | jit->code_cache_.reset(JitCodeCache::Create( |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 158 | options->GetCodeCacheInitialCapacity(), |
| 159 | options->GetCodeCacheMaxCapacity(), |
| 160 | jit->generate_debug_info_, |
| 161 | error_msg)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 162 | if (jit->GetCodeCache() == nullptr) { |
| 163 | return nullptr; |
| 164 | } |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 165 | jit->use_jit_compilation_ = options->UseJitCompilation(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 166 | jit->save_profiling_info_ = options->GetSaveProfilingInfo(); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 167 | VLOG(jit) << "JIT created with initial_capacity=" |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 168 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 169 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 170 | << ", compile_threshold=" << options->GetCompileThreshold() |
| 171 | << ", save_profiling_info=" << options->GetSaveProfilingInfo(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 172 | |
| 173 | |
| 174 | jit->hot_method_threshold_ = options->GetCompileThreshold(); |
| 175 | jit->warm_method_threshold_ = options->GetWarmupThreshold(); |
| 176 | jit->osr_method_threshold_ = options->GetOsrThreshold(); |
Nicolas Geoffray | ba6aae0 | 2016-04-14 14:17:29 +0100 | [diff] [blame] | 177 | jit->priority_thread_weight_ = options->GetPriorityThreadWeight(); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 178 | jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 179 | |
| 180 | jit->CreateThreadPool(); |
| 181 | |
| 182 | // Notify native debugger about the classes already loaded before the creation of the jit. |
| 183 | jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 184 | return jit.release(); |
| 185 | } |
| 186 | |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 187 | bool Jit::LoadCompilerLibrary(std::string* error_msg) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 188 | jit_library_handle_ = dlopen( |
| 189 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 190 | if (jit_library_handle_ == nullptr) { |
| 191 | std::ostringstream oss; |
| 192 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 193 | *error_msg = oss.str(); |
| 194 | return false; |
| 195 | } |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 196 | jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load")); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 197 | if (jit_load_ == nullptr) { |
| 198 | dlclose(jit_library_handle_); |
| 199 | *error_msg = "JIT couldn't find jit_load entry point"; |
| 200 | return false; |
| 201 | } |
| 202 | jit_unload_ = reinterpret_cast<void (*)(void*)>( |
| 203 | dlsym(jit_library_handle_, "jit_unload")); |
| 204 | if (jit_unload_ == nullptr) { |
| 205 | dlclose(jit_library_handle_); |
| 206 | *error_msg = "JIT couldn't find jit_unload entry point"; |
| 207 | return false; |
| 208 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 209 | jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 210 | dlsym(jit_library_handle_, "jit_compile_method")); |
| 211 | if (jit_compile_method_ == nullptr) { |
| 212 | dlclose(jit_library_handle_); |
| 213 | *error_msg = "JIT couldn't find jit_compile_method entry point"; |
| 214 | return false; |
| 215 | } |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 216 | jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>( |
| 217 | dlsym(jit_library_handle_, "jit_types_loaded")); |
| 218 | if (jit_types_loaded_ == nullptr) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 219 | dlclose(jit_library_handle_); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 220 | *error_msg = "JIT couldn't find jit_types_loaded entry point"; |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
| 226 | bool Jit::LoadCompiler(std::string* error_msg) { |
| 227 | if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) { |
| 228 | return false; |
| 229 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 230 | bool will_generate_debug_symbols = false; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 231 | VLOG(jit) << "Calling JitLoad interpreter_only=" |
| 232 | << Runtime::Current()->GetInstrumentation()->InterpretOnly(); |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 233 | jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 234 | if (jit_compiler_handle_ == nullptr) { |
| 235 | dlclose(jit_library_handle_); |
| 236 | *error_msg = "JIT couldn't load compiler"; |
| 237 | return false; |
| 238 | } |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 239 | generate_debug_info_ = will_generate_debug_symbols; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 243 | bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 244 | DCHECK(Runtime::Current()->UseJitCompilation()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 245 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 246 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 247 | // Don't compile the method if it has breakpoints. |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 248 | if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { |
| 249 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to breakpoint"; |
| 250 | return false; |
| 251 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 252 | |
| 253 | // Don't compile the method if we are supposed to be deoptimized. |
| 254 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 255 | if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 256 | VLOG(jit) << "JIT not compiling " << PrettyMethod(method) << " due to deoptimization"; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 260 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 261 | // of that proxy method, as the compiler does not expect a proxy method. |
| 262 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(sizeof(void*)); |
| 263 | if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 264 | return false; |
| 265 | } |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 266 | |
| 267 | VLOG(jit) << "Compiling method " |
| 268 | << PrettyMethod(method_to_compile) |
| 269 | << " osr=" << std::boolalpha << osr; |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 270 | bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 271 | code_cache_->DoneCompiling(method_to_compile, self, osr); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 272 | if (!success) { |
| 273 | VLOG(jit) << "Failed to compile method " |
| 274 | << PrettyMethod(method_to_compile) |
| 275 | << " osr=" << std::boolalpha << osr; |
| 276 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 277 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void Jit::CreateThreadPool() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 281 | // There is a DCHECK in the 'AddSamples' method to ensure the tread pool |
| 282 | // is not null when we instrument. |
| 283 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1)); |
| 284 | thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority); |
| 285 | thread_pool_->StartWorkers(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void Jit::DeleteThreadPool() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 289 | Thread* self = Thread::Current(); |
| 290 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 291 | if (thread_pool_ != nullptr) { |
| 292 | ThreadPool* cache = nullptr; |
| 293 | { |
| 294 | ScopedSuspendAll ssa(__FUNCTION__); |
| 295 | // Clear thread_pool_ field while the threads are suspended. |
| 296 | // A mutator in the 'AddSamples' method will check against it. |
| 297 | cache = thread_pool_.release(); |
| 298 | } |
| 299 | cache->StopWorkers(self); |
| 300 | cache->RemoveAllTasks(self); |
| 301 | // We could just suspend all threads, but we know those threads |
| 302 | // will finish in a short period, so it's not worth adding a suspend logic |
| 303 | // here. Besides, this is only done for shutdown. |
| 304 | cache->Wait(self, false, false); |
| 305 | delete cache; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 309 | void Jit::StartProfileSaver(const std::string& filename, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 310 | const std::vector<std::string>& code_paths, |
| 311 | const std::string& foreign_dex_profile_path, |
| 312 | const std::string& app_dir) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 313 | if (save_profiling_info_) { |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 314 | ProfileSaver::Start(filename, code_cache_.get(), code_paths, foreign_dex_profile_path, app_dir); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 315 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void Jit::StopProfileSaver() { |
| 319 | if (save_profiling_info_ && ProfileSaver::IsStarted()) { |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 320 | ProfileSaver::Stop(dump_info_on_shutdown_); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 324 | bool Jit::JitAtFirstUse() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 325 | return HotMethodThreshold() == 0; |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 328 | bool Jit::CanInvokeCompiledCode(ArtMethod* method) { |
| 329 | return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode()); |
| 330 | } |
| 331 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 332 | Jit::~Jit() { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 333 | DCHECK(!save_profiling_info_ || !ProfileSaver::IsStarted()); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 334 | if (dump_info_on_shutdown_) { |
| 335 | DumpInfo(LOG(INFO)); |
| 336 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 337 | DeleteThreadPool(); |
| 338 | if (jit_compiler_handle_ != nullptr) { |
| 339 | jit_unload_(jit_compiler_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 340 | jit_compiler_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 341 | } |
| 342 | if (jit_library_handle_ != nullptr) { |
| 343 | dlclose(jit_library_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 344 | jit_library_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 348 | void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 349 | if (!Runtime::Current()->UseJitCompilation()) { |
| 350 | // No need to notify if we only use the JIT to save profiles. |
| 351 | return; |
| 352 | } |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 353 | jit::Jit* jit = Runtime::Current()->GetJit(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 354 | if (jit->generate_debug_info_) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 355 | DCHECK(jit->jit_types_loaded_ != nullptr); |
| 356 | jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { |
| 361 | struct CollectClasses : public ClassVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 362 | bool operator()(mirror::Class* klass) override { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 363 | classes_.push_back(klass); |
| 364 | return true; |
| 365 | } |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 366 | std::vector<mirror::Class*> classes_; |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | if (generate_debug_info_) { |
| 370 | ScopedObjectAccess so(Thread::Current()); |
| 371 | |
| 372 | CollectClasses visitor; |
| 373 | linker->VisitClasses(&visitor); |
| 374 | jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size()); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 378 | extern "C" void art_quick_osr_stub(void** stack, |
| 379 | uint32_t stack_size_in_bytes, |
| 380 | const uint8_t* native_pc, |
| 381 | JValue* result, |
| 382 | const char* shorty, |
| 383 | Thread* self); |
| 384 | |
| 385 | bool Jit::MaybeDoOnStackReplacement(Thread* thread, |
| 386 | ArtMethod* method, |
| 387 | uint32_t dex_pc, |
| 388 | int32_t dex_pc_offset, |
| 389 | JValue* result) { |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 390 | if (!kEnableOnStackReplacement) { |
| 391 | return false; |
| 392 | } |
| 393 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 394 | Jit* jit = Runtime::Current()->GetJit(); |
| 395 | if (jit == nullptr) { |
| 396 | return false; |
| 397 | } |
| 398 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 399 | if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) { |
| 400 | // Don't attempt to do an OSR if we are close to the stack limit. Since |
| 401 | // the interpreter frames are still on stack, OSR has the potential |
| 402 | // to stack overflow even for a simple loop. |
| 403 | // b/27094810. |
| 404 | return false; |
| 405 | } |
| 406 | |
Nicolas Geoffray | d9bc433 | 2016-02-05 23:32:25 +0000 | [diff] [blame] | 407 | // Get the actual Java method if this method is from a proxy class. The compiler |
| 408 | // and the JIT code cache do not expect methods from proxy classes. |
| 409 | method = method->GetInterfaceMethodIfProxy(sizeof(void*)); |
| 410 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 411 | // Cheap check if the method has been compiled already. That's an indicator that we should |
| 412 | // osr into it. |
| 413 | if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 414 | return false; |
| 415 | } |
| 416 | |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 417 | // Fetch some data before looking up for an OSR method. We don't want thread |
| 418 | // suspension once we hold an OSR method, as the JIT code cache could delete the OSR |
| 419 | // method while we are being suspended. |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 420 | const size_t number_of_vregs = method->GetCodeItem()->registers_size_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 421 | const char* shorty = method->GetShorty(); |
| 422 | std::string method_name(VLOG_IS_ON(jit) ? PrettyMethod(method) : ""); |
| 423 | void** memory = nullptr; |
| 424 | size_t frame_size = 0; |
| 425 | ShadowFrame* shadow_frame = nullptr; |
| 426 | const uint8_t* native_pc = nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 427 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 428 | { |
| 429 | ScopedAssertNoThreadSuspension sts(thread, "Holding OSR method"); |
| 430 | const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method); |
| 431 | if (osr_method == nullptr) { |
| 432 | // No osr method yet, just return to the interpreter. |
| 433 | return false; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 434 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 435 | |
| 436 | CodeInfo code_info = osr_method->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 437 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 438 | |
| 439 | // Find stack map starting at the target dex_pc. |
| 440 | StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding); |
| 441 | if (!stack_map.IsValid()) { |
| 442 | // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the |
| 443 | // hope that the next branch has one. |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | // We found a stack map, now fill the frame with dex register values from the interpreter's |
| 448 | // shadow frame. |
| 449 | DexRegisterMap vreg_map = |
| 450 | code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs); |
| 451 | |
| 452 | frame_size = osr_method->GetFrameSizeInBytes(); |
| 453 | |
| 454 | // Allocate memory to put shadow frame values. The osr stub will copy that memory to |
| 455 | // stack. |
| 456 | // Note that we could pass the shadow frame to the stub, and let it copy the values there, |
| 457 | // but that is engineering complexity not worth the effort for something like OSR. |
| 458 | memory = reinterpret_cast<void**>(malloc(frame_size)); |
| 459 | CHECK(memory != nullptr); |
| 460 | memset(memory, 0, frame_size); |
| 461 | |
| 462 | // Art ABI: ArtMethod is at the bottom of the stack. |
| 463 | memory[0] = method; |
| 464 | |
| 465 | shadow_frame = thread->PopShadowFrame(); |
| 466 | if (!vreg_map.IsValid()) { |
| 467 | // If we don't have a dex register map, then there are no live dex registers at |
| 468 | // this dex pc. |
| 469 | } else { |
| 470 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
| 471 | DexRegisterLocation::Kind location = |
| 472 | vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); |
| 473 | if (location == DexRegisterLocation::Kind::kNone) { |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 474 | // Dex register is dead or uninitialized. |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 475 | continue; |
| 476 | } |
| 477 | |
| 478 | if (location == DexRegisterLocation::Kind::kConstant) { |
| 479 | // We skip constants because the compiled code knows how to handle them. |
| 480 | continue; |
| 481 | } |
| 482 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 483 | DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 484 | |
| 485 | int32_t vreg_value = shadow_frame->GetVReg(vreg); |
| 486 | int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg, |
| 487 | number_of_vregs, |
| 488 | code_info, |
| 489 | encoding); |
| 490 | DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size)); |
| 491 | DCHECK_GT(slot_offset, 0); |
| 492 | (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value; |
| 493 | } |
| 494 | } |
| 495 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 496 | native_pc = stack_map.GetNativePcOffset(encoding.stack_map_encoding) + |
| 497 | osr_method->GetEntryPoint(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 498 | VLOG(jit) << "Jumping to " |
| 499 | << method_name |
| 500 | << "@" |
| 501 | << std::hex << reinterpret_cast<uintptr_t>(native_pc); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 504 | { |
| 505 | ManagedStack fragment; |
| 506 | thread->PushManagedStackFragment(&fragment); |
| 507 | (*art_quick_osr_stub)(memory, |
| 508 | frame_size, |
| 509 | native_pc, |
| 510 | result, |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 511 | shorty, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 512 | thread); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 513 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 514 | if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) { |
| 515 | thread->DeoptimizeWithDeoptimizationException(result); |
| 516 | } |
| 517 | thread->PopManagedStackFragment(fragment); |
| 518 | } |
| 519 | free(memory); |
| 520 | thread->PushShadowFrame(shadow_frame); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 521 | VLOG(jit) << "Done running OSR code for " << method_name; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 525 | void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) { |
| 526 | if (bytes > 4 * MB) { |
| 527 | LOG(INFO) << "Compiler allocated " |
| 528 | << PrettySize(bytes) |
| 529 | << " to compile " |
| 530 | << PrettyMethod(method); |
| 531 | } |
| 532 | MutexLock mu(Thread::Current(), lock_); |
| 533 | memory_use_.AddValue(bytes); |
| 534 | } |
| 535 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 536 | class JitCompileTask FINAL : public Task { |
| 537 | public: |
| 538 | enum TaskKind { |
| 539 | kAllocateProfile, |
| 540 | kCompile, |
| 541 | kCompileOsr |
| 542 | }; |
| 543 | |
| 544 | JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) { |
| 545 | ScopedObjectAccess soa(Thread::Current()); |
| 546 | // Add a global ref to the class to prevent class unloading until compilation is done. |
| 547 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 548 | CHECK(klass_ != nullptr); |
| 549 | } |
| 550 | |
| 551 | ~JitCompileTask() { |
| 552 | ScopedObjectAccess soa(Thread::Current()); |
| 553 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 554 | } |
| 555 | |
| 556 | void Run(Thread* self) OVERRIDE { |
| 557 | ScopedObjectAccess soa(self); |
| 558 | if (kind_ == kCompile) { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 559 | Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 560 | } else if (kind_ == kCompileOsr) { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 561 | Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 562 | } else { |
| 563 | DCHECK(kind_ == kAllocateProfile); |
| 564 | if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) { |
| 565 | VLOG(jit) << "Start profiling " << PrettyMethod(method_); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void Finalize() OVERRIDE { |
| 571 | delete this; |
| 572 | } |
| 573 | |
| 574 | private: |
| 575 | ArtMethod* const method_; |
| 576 | const TaskKind kind_; |
| 577 | jobject klass_; |
| 578 | |
| 579 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
| 580 | }; |
| 581 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 582 | void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 583 | if (thread_pool_ == nullptr) { |
| 584 | // Should only see this when shutting down. |
| 585 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 586 | return; |
| 587 | } |
| 588 | |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 589 | if (method->IsClassInitializer() || method->IsNative() || !method->IsCompilable()) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 590 | // We do not want to compile such methods. |
| 591 | return; |
| 592 | } |
| 593 | DCHECK(thread_pool_ != nullptr); |
| 594 | DCHECK_GT(warm_method_threshold_, 0); |
| 595 | DCHECK_GT(hot_method_threshold_, warm_method_threshold_); |
| 596 | DCHECK_GT(osr_method_threshold_, hot_method_threshold_); |
| 597 | DCHECK_GE(priority_thread_weight_, 1); |
| 598 | DCHECK_LE(priority_thread_weight_, hot_method_threshold_); |
| 599 | |
| 600 | int32_t starting_count = method->GetCounter(); |
| 601 | if (Jit::ShouldUsePriorityThreadWeight()) { |
| 602 | count *= priority_thread_weight_; |
| 603 | } |
| 604 | int32_t new_count = starting_count + count; // int32 here to avoid wrap-around; |
| 605 | if (starting_count < warm_method_threshold_) { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 606 | if ((new_count >= warm_method_threshold_) && |
| 607 | (method->GetProfilingInfo(sizeof(void*)) == nullptr)) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 608 | bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false); |
| 609 | if (success) { |
| 610 | VLOG(jit) << "Start profiling " << PrettyMethod(method); |
| 611 | } |
| 612 | |
| 613 | if (thread_pool_ == nullptr) { |
| 614 | // Calling ProfilingInfo::Create might put us in a suspended state, which could |
| 615 | // lead to the thread pool being deleted when we are shutting down. |
| 616 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | if (!success) { |
| 621 | // We failed allocating. Instead of doing the collection on the Java thread, we push |
| 622 | // an allocation to a compiler thread, that will do the collection. |
| 623 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile)); |
| 624 | } |
| 625 | } |
| 626 | // Avoid jumping more than one state at a time. |
| 627 | new_count = std::min(new_count, hot_method_threshold_ - 1); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 628 | } else if (use_jit_compilation_) { |
| 629 | if (starting_count < hot_method_threshold_) { |
| 630 | if ((new_count >= hot_method_threshold_) && |
| 631 | !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 632 | DCHECK(thread_pool_ != nullptr); |
| 633 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile)); |
| 634 | } |
| 635 | // Avoid jumping more than one state at a time. |
| 636 | new_count = std::min(new_count, osr_method_threshold_ - 1); |
| 637 | } else if (starting_count < osr_method_threshold_) { |
| 638 | if (!with_backedges) { |
| 639 | // If the samples don't contain any back edge, we don't increment the hotness. |
| 640 | return; |
| 641 | } |
| 642 | if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) { |
| 643 | DCHECK(thread_pool_ != nullptr); |
| 644 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr)); |
| 645 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | // Update hotness counter |
| 649 | method->SetCounter(new_count); |
| 650 | } |
| 651 | |
| 652 | void Jit::MethodEntered(Thread* thread, ArtMethod* method) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 653 | Runtime* runtime = Runtime::Current(); |
| 654 | if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 655 | // The compiler requires a ProfilingInfo object. |
| 656 | ProfilingInfo::Create(thread, method, /* retry_allocation */ true); |
| 657 | JitCompileTask compile_task(method, JitCompileTask::kCompile); |
| 658 | compile_task.Run(thread); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | ProfilingInfo* profiling_info = method->GetProfilingInfo(sizeof(void*)); |
| 663 | // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it |
| 664 | // instead of interpreting the method. |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 665 | if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) { |
| 666 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 667 | method, profiling_info->GetSavedEntryPoint()); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 668 | } else { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 669 | AddSamples(thread, method, 1, /* with_backedges */false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | void Jit::InvokeVirtualOrInterface(Thread* thread, |
| 674 | mirror::Object* this_object, |
| 675 | ArtMethod* caller, |
| 676 | uint32_t dex_pc, |
| 677 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
| 678 | ScopedAssertNoThreadSuspension ants(thread, __FUNCTION__); |
| 679 | DCHECK(this_object != nullptr); |
| 680 | ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*)); |
| 681 | if (info != nullptr) { |
| 682 | // Since the instrumentation is marked from the declaring class we need to mark the card so |
| 683 | // that mod-union tables and card rescanning know about the update. |
| 684 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(caller->GetDeclaringClass()); |
| 685 | info->AddInvokeInfo(dex_pc, this_object->GetClass()); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | void Jit::WaitForCompilationToFinish(Thread* self) { |
| 690 | if (thread_pool_ != nullptr) { |
| 691 | thread_pool_->Wait(self, false, false); |
| 692 | } |
| 693 | } |
| 694 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 695 | } // namespace jit |
| 696 | } // namespace art |