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 | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 23 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include "base/logging.h" // For VLOG. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 25 | #include "base/memory_tool.h" |
Andreas Gampe | dcc528d | 2017-12-07 13:37:10 -0800 | [diff] [blame] | 26 | #include "base/runtime_debug.h" |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 27 | #include "base/scoped_flock.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/utils.h" |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 29 | #include "class_root.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 30 | #include "debugger.h" |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 31 | #include "dex/type_lookup_table.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 32 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 33 | #include "interpreter/interpreter.h" |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 34 | #include "jit-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | #include "jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 36 | #include "jni/java_vm_ext.h" |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 37 | #include "mirror/method_handle_impl.h" |
| 38 | #include "mirror/var_handle.h" |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 39 | #include "oat_file.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 40 | #include "oat_file_manager.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 41 | #include "oat_quick_method_header.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 42 | #include "profile/profile_compilation_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 43 | #include "profile_saver.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 44 | #include "runtime.h" |
| 45 | #include "runtime_options.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 46 | #include "stack.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 47 | #include "stack_map.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 48 | #include "thread-inl.h" |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 49 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 50 | |
| 51 | namespace art { |
| 52 | namespace jit { |
| 53 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 54 | static constexpr bool kEnableOnStackReplacement = true; |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 55 | |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 56 | // Different compilation threshold constants. These can be overridden on the command line. |
| 57 | static constexpr size_t kJitDefaultCompileThreshold = 10000; // Non-debug default. |
| 58 | static constexpr size_t kJitStressDefaultCompileThreshold = 100; // Fast-debug build. |
| 59 | static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2; // Slow-debug build. |
| 60 | |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 61 | // JIT compiler |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 62 | void* Jit::jit_library_handle_ = nullptr; |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 63 | void* Jit::jit_compiler_handle_ = nullptr; |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 64 | void* (*Jit::jit_load_)(void) = nullptr; |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 65 | void (*Jit::jit_unload_)(void*) = nullptr; |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame] | 66 | bool (*Jit::jit_compile_method_)(void*, JitMemoryRegion*, ArtMethod*, Thread*, bool, bool) |
| 67 | = nullptr; |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 68 | void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr; |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 69 | bool (*Jit::jit_generate_debug_info_)(void*) = nullptr; |
| 70 | void (*Jit::jit_update_options_)(void*) = nullptr; |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 71 | |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 72 | struct StressModeHelper { |
| 73 | DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode); |
| 74 | }; |
| 75 | DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode); |
| 76 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 77 | uint32_t JitOptions::RoundUpThreshold(uint32_t threshold) { |
| 78 | if (threshold > kJitSamplesBatchSize) { |
| 79 | threshold = RoundUp(threshold, kJitSamplesBatchSize); |
| 80 | } |
| 81 | CHECK_LE(threshold, std::numeric_limits<uint16_t>::max()); |
| 82 | return threshold; |
| 83 | } |
| 84 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 85 | JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 86 | auto* jit_options = new JitOptions; |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 87 | jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 88 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 89 | jit_options->code_cache_initial_capacity_ = |
| 90 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); |
| 91 | jit_options->code_cache_max_capacity_ = |
| 92 | options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 93 | jit_options->dump_info_on_shutdown_ = |
| 94 | options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 95 | jit_options->profile_saver_options_ = |
| 96 | options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts); |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 97 | jit_options->thread_pool_pthread_priority_ = |
| 98 | options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 99 | |
Andreas Gampe | 7897cec | 2017-07-19 16:28:59 -0700 | [diff] [blame] | 100 | if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) { |
| 101 | jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold); |
| 102 | } else { |
| 103 | jit_options->compile_threshold_ = |
| 104 | kIsDebugBuild |
| 105 | ? (StressModeHelper::kSlowMode |
| 106 | ? kJitSlowStressDefaultCompileThreshold |
| 107 | : kJitStressDefaultCompileThreshold) |
| 108 | : kJitDefaultCompileThreshold; |
| 109 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 110 | jit_options->compile_threshold_ = RoundUpThreshold(jit_options->compile_threshold_); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 111 | |
| 112 | if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) { |
| 113 | jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 114 | } else { |
| 115 | jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2; |
| 116 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 117 | jit_options->warmup_threshold_ = RoundUpThreshold(jit_options->warmup_threshold_); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 118 | |
| 119 | if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) { |
| 120 | jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 121 | } else { |
| 122 | jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2; |
| 123 | if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 124 | jit_options->osr_threshold_ = |
| 125 | RoundDown(std::numeric_limits<uint16_t>::max(), kJitSamplesBatchSize); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 128 | jit_options->osr_threshold_ = RoundUpThreshold(jit_options->osr_threshold_); |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 129 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 130 | if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) { |
| 131 | jit_options->priority_thread_weight_ = |
| 132 | *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight); |
| 133 | if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) { |
| 134 | LOG(FATAL) << "Priority thread weight is above the warmup threshold."; |
| 135 | } else if (jit_options->priority_thread_weight_ == 0) { |
| 136 | LOG(FATAL) << "Priority thread weight cannot be 0."; |
| 137 | } |
| 138 | } else { |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 139 | jit_options->priority_thread_weight_ = std::max( |
| 140 | jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio, |
| 141 | static_cast<size_t>(1)); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 144 | if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 145 | jit_options->invoke_transition_weight_ = |
| 146 | *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 147 | if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) { |
| 148 | LOG(FATAL) << "Invoke transition weight is above the warmup threshold."; |
| 149 | } else if (jit_options->invoke_transition_weight_ == 0) { |
Nicolas Geoffray | 7c9f3ba | 2016-05-06 16:52:36 +0100 | [diff] [blame] | 150 | LOG(FATAL) << "Invoke transition weight cannot be 0."; |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 151 | } |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 152 | } else { |
| 153 | jit_options->invoke_transition_weight_ = std::max( |
| 154 | jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 155 | static_cast<size_t>(1)); |
Calin Juravle | 155ff3d | 2016-04-27 14:14:58 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 158 | return jit_options; |
| 159 | } |
| 160 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 161 | void Jit::DumpInfo(std::ostream& os) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 162 | code_cache_->Dump(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 163 | cumulative_timings_.Dump(os); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 164 | MutexLock mu(Thread::Current(), lock_); |
| 165 | memory_use_.PrintMemoryUse(os); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 168 | void Jit::DumpForSigQuit(std::ostream& os) { |
| 169 | DumpInfo(os); |
| 170 | ProfileSaver::DumpInstanceInfo(os); |
| 171 | } |
| 172 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 173 | void Jit::AddTimingLogger(const TimingLogger& logger) { |
| 174 | cumulative_timings_.AddLogger(logger); |
| 175 | } |
| 176 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 177 | Jit::Jit(JitCodeCache* code_cache, JitOptions* options) |
| 178 | : code_cache_(code_cache), |
| 179 | options_(options), |
| 180 | cumulative_timings_("JIT timings"), |
| 181 | memory_use_("Memory used for compilation", 16), |
| 182 | lock_("JIT memory use lock") {} |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 183 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 184 | Jit* Jit::Create(JitCodeCache* code_cache, JitOptions* options) { |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 185 | if (jit_load_ == nullptr) { |
| 186 | LOG(WARNING) << "Not creating JIT: library not loaded"; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 187 | return nullptr; |
| 188 | } |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 189 | jit_compiler_handle_ = (jit_load_)(); |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 190 | if (jit_compiler_handle_ == nullptr) { |
| 191 | LOG(WARNING) << "Not creating JIT: failed to allocate a compiler"; |
| 192 | return nullptr; |
| 193 | } |
| 194 | std::unique_ptr<Jit> jit(new Jit(code_cache, options)); |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 195 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 196 | // If the code collector is enabled, check if that still holds: |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 197 | // With 'perf', we want a 1-1 mapping between an address and a method. |
| 198 | // We aren't able to keep method pointers live during the instrumentation method entry trampoline |
| 199 | // so we will just disable jit-gc if we are doing that. |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 200 | if (code_cache->GetGarbageCollectCode()) { |
| 201 | code_cache->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) && |
| 202 | !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()); |
| 203 | } |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 204 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 205 | VLOG(jit) << "JIT created with initial_capacity=" |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 206 | << PrettySize(options->GetCodeCacheInitialCapacity()) |
| 207 | << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 208 | << ", compile_threshold=" << options->GetCompileThreshold() |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 209 | << ", profile_saver_options=" << options->GetProfileSaverOptions(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 210 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 211 | // Notify native debugger about the classes already loaded before the creation of the jit. |
| 212 | jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 213 | return jit.release(); |
| 214 | } |
| 215 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 216 | template <typename T> |
| 217 | bool Jit::LoadSymbol(T* address, const char* name, std::string* error_msg) { |
| 218 | *address = reinterpret_cast<T>(dlsym(jit_library_handle_, name)); |
| 219 | if (*address == nullptr) { |
| 220 | *error_msg = std::string("JIT couldn't find ") + name + std::string(" entry point"); |
| 221 | return false; |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 226 | bool Jit::LoadCompilerLibrary(std::string* error_msg) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 227 | jit_library_handle_ = dlopen( |
| 228 | kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); |
| 229 | if (jit_library_handle_ == nullptr) { |
| 230 | std::ostringstream oss; |
| 231 | oss << "JIT could not load libart-compiler.so: " << dlerror(); |
| 232 | *error_msg = oss.str(); |
| 233 | return false; |
| 234 | } |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 235 | bool all_resolved = true; |
| 236 | all_resolved = all_resolved && LoadSymbol(&jit_load_, "jit_load", error_msg); |
| 237 | all_resolved = all_resolved && LoadSymbol(&jit_unload_, "jit_unload", error_msg); |
| 238 | all_resolved = all_resolved && LoadSymbol(&jit_compile_method_, "jit_compile_method", error_msg); |
| 239 | all_resolved = all_resolved && LoadSymbol(&jit_types_loaded_, "jit_types_loaded", error_msg); |
| 240 | all_resolved = all_resolved && LoadSymbol(&jit_update_options_, "jit_update_options", error_msg); |
| 241 | all_resolved = all_resolved && |
| 242 | LoadSymbol(&jit_generate_debug_info_, "jit_generate_debug_info", error_msg); |
| 243 | if (!all_resolved) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 244 | dlclose(jit_library_handle_); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 245 | return false; |
| 246 | } |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 250 | bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool baseline, bool osr, bool prejit) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 251 | DCHECK(Runtime::Current()->UseJitCompilation()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 252 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 253 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 254 | RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 255 | // Don't compile the method if it has breakpoints. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 256 | if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) { |
| 257 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() |
| 258 | << " due to not being safe to jit according to runtime-callbacks. For example, there" |
| 259 | << " could be breakpoints in this method."; |
Mathieu Chartier | d856545 | 2015-03-26 09:41:50 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 262 | |
| 263 | // Don't compile the method if we are supposed to be deoptimized. |
| 264 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 265 | if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 266 | VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization"; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 270 | JitMemoryRegion* region = GetCodeCache()->GetCurrentRegion(); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 271 | if (osr && GetCodeCache()->IsSharedRegion(*region)) { |
| 272 | VLOG(jit) << "JIT not osr compiling " |
| 273 | << method->PrettyMethod() |
| 274 | << " due to using shared region"; |
| 275 | return false; |
| 276 | } |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 277 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 278 | // If we get a request to compile a proxy method, we pass the actual Java method |
| 279 | // of that proxy method, as the compiler does not expect a proxy method. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 280 | ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 281 | if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr, prejit, region)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 282 | return false; |
| 283 | } |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 284 | |
| 285 | VLOG(jit) << "Compiling method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 286 | << ArtMethod::PrettyMethod(method_to_compile) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 287 | << " osr=" << std::boolalpha << osr; |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame] | 288 | bool success = jit_compile_method_( |
| 289 | jit_compiler_handle_, region, method_to_compile, self, baseline, osr); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 290 | code_cache_->DoneCompiling(method_to_compile, self, osr); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 291 | if (!success) { |
| 292 | VLOG(jit) << "Failed to compile method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 293 | << ArtMethod::PrettyMethod(method_to_compile) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 294 | << " osr=" << std::boolalpha << osr; |
| 295 | } |
Andreas Gampe | 320ba91 | 2016-11-18 17:39:45 -0800 | [diff] [blame] | 296 | if (kIsDebugBuild) { |
| 297 | if (self->IsExceptionPending()) { |
| 298 | mirror::Throwable* exception = self->GetException(); |
| 299 | LOG(FATAL) << "No pending exception expected after compiling " |
| 300 | << ArtMethod::PrettyMethod(method) |
| 301 | << ": " |
| 302 | << exception->Dump(); |
| 303 | } |
| 304 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 305 | return success; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 308 | void Jit::WaitForWorkersToBeCreated() { |
| 309 | if (thread_pool_ != nullptr) { |
| 310 | thread_pool_->WaitForWorkersToBeCreated(); |
| 311 | } |
| 312 | } |
| 313 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 314 | void Jit::DeleteThreadPool() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 315 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 316 | if (thread_pool_ != nullptr) { |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 317 | std::unique_ptr<ThreadPool> pool; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 318 | { |
| 319 | ScopedSuspendAll ssa(__FUNCTION__); |
| 320 | // Clear thread_pool_ field while the threads are suspended. |
| 321 | // A mutator in the 'AddSamples' method will check against it. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 322 | pool = std::move(thread_pool_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 323 | } |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 324 | |
| 325 | // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue. |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 326 | if (!kRunningOnMemoryTool) { |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 327 | pool->StopWorkers(self); |
| 328 | pool->RemoveAllTasks(self); |
| 329 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 330 | // We could just suspend all threads, but we know those threads |
| 331 | // will finish in a short period, so it's not worth adding a suspend logic |
| 332 | // here. Besides, this is only done for shutdown. |
Andreas Gampe | 0897e1c | 2017-05-16 08:36:56 -0700 | [diff] [blame] | 333 | pool->Wait(self, false, false); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 337 | void Jit::StartProfileSaver(const std::string& filename, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 338 | const std::vector<std::string>& code_paths) { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 339 | if (options_->GetSaveProfilingInfo()) { |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 340 | ProfileSaver::Start(options_->GetProfileSaverOptions(), filename, code_cache_, code_paths); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 341 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void Jit::StopProfileSaver() { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 345 | if (options_->GetSaveProfilingInfo() && ProfileSaver::IsStarted()) { |
| 346 | ProfileSaver::Stop(options_->DumpJitInfoOnShutdown()); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 350 | bool Jit::JitAtFirstUse() { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 351 | return HotMethodThreshold() == 0; |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 354 | bool Jit::CanInvokeCompiledCode(ArtMethod* method) { |
| 355 | return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode()); |
| 356 | } |
| 357 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 358 | Jit::~Jit() { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 359 | DCHECK(!options_->GetSaveProfilingInfo() || !ProfileSaver::IsStarted()); |
| 360 | if (options_->DumpJitInfoOnShutdown()) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 361 | DumpInfo(LOG_STREAM(INFO)); |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 362 | Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO)); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 363 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 364 | DeleteThreadPool(); |
| 365 | if (jit_compiler_handle_ != nullptr) { |
| 366 | jit_unload_(jit_compiler_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 367 | jit_compiler_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 368 | } |
| 369 | if (jit_library_handle_ != nullptr) { |
| 370 | dlclose(jit_library_handle_); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 371 | jit_library_handle_ = nullptr; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 375 | void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 376 | if (!Runtime::Current()->UseJitCompilation()) { |
| 377 | // No need to notify if we only use the JIT to save profiles. |
| 378 | return; |
| 379 | } |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 380 | jit::Jit* jit = Runtime::Current()->GetJit(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 381 | if (jit_generate_debug_info_(jit->jit_compiler_handle_)) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 382 | DCHECK(jit->jit_types_loaded_ != nullptr); |
| 383 | jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { |
| 388 | struct CollectClasses : public ClassVisitor { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 389 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 390 | classes_.push_back(klass.Ptr()); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 391 | return true; |
| 392 | } |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 393 | std::vector<mirror::Class*> classes_; |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 396 | if (jit_generate_debug_info_(jit_compiler_handle_)) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 397 | ScopedObjectAccess so(Thread::Current()); |
| 398 | |
| 399 | CollectClasses visitor; |
| 400 | linker->VisitClasses(&visitor); |
| 401 | jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size()); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 405 | extern "C" void art_quick_osr_stub(void** stack, |
Evgenii Stepanov | 6d90fde | 2018-09-04 17:50:38 -0700 | [diff] [blame] | 406 | size_t stack_size_in_bytes, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 407 | const uint8_t* native_pc, |
| 408 | JValue* result, |
| 409 | const char* shorty, |
| 410 | Thread* self); |
| 411 | |
| 412 | bool Jit::MaybeDoOnStackReplacement(Thread* thread, |
| 413 | ArtMethod* method, |
| 414 | uint32_t dex_pc, |
| 415 | int32_t dex_pc_offset, |
| 416 | JValue* result) { |
Nicolas Geoffray | e866213 | 2016-02-15 10:00:42 +0000 | [diff] [blame] | 417 | if (!kEnableOnStackReplacement) { |
| 418 | return false; |
| 419 | } |
| 420 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 421 | Jit* jit = Runtime::Current()->GetJit(); |
| 422 | if (jit == nullptr) { |
| 423 | return false; |
| 424 | } |
| 425 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 426 | if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) { |
| 427 | // Don't attempt to do an OSR if we are close to the stack limit. Since |
| 428 | // the interpreter frames are still on stack, OSR has the potential |
| 429 | // to stack overflow even for a simple loop. |
| 430 | // b/27094810. |
| 431 | return false; |
| 432 | } |
| 433 | |
Nicolas Geoffray | d9bc433 | 2016-02-05 23:32:25 +0000 | [diff] [blame] | 434 | // Get the actual Java method if this method is from a proxy class. The compiler |
| 435 | // and the JIT code cache do not expect methods from proxy classes. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 436 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Nicolas Geoffray | d9bc433 | 2016-02-05 23:32:25 +0000 | [diff] [blame] | 437 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 438 | // Cheap check if the method has been compiled already. That's an indicator that we should |
| 439 | // osr into it. |
| 440 | if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 441 | return false; |
| 442 | } |
| 443 | |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 444 | // Fetch some data before looking up for an OSR method. We don't want thread |
| 445 | // suspension once we hold an OSR method, as the JIT code cache could delete the OSR |
| 446 | // method while we are being suspended. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 447 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 448 | const size_t number_of_vregs = accessor.RegistersSize(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 449 | const char* shorty = method->GetShorty(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 450 | std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : ""); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 451 | void** memory = nullptr; |
| 452 | size_t frame_size = 0; |
| 453 | ShadowFrame* shadow_frame = nullptr; |
| 454 | const uint8_t* native_pc = nullptr; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 455 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 456 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 457 | ScopedAssertNoThreadSuspension sts("Holding OSR method"); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 458 | const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method); |
| 459 | if (osr_method == nullptr) { |
| 460 | // No osr method yet, just return to the interpreter. |
| 461 | return false; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 462 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 463 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 464 | CodeInfo code_info(osr_method); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 465 | |
| 466 | // Find stack map starting at the target dex_pc. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 467 | StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 468 | if (!stack_map.IsValid()) { |
| 469 | // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the |
| 470 | // hope that the next branch has one. |
| 471 | return false; |
| 472 | } |
| 473 | |
Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 474 | // Before allowing the jump, make sure no code is actively inspecting the method to avoid |
| 475 | // jumping from interpreter to OSR while e.g. single stepping. Note that we could selectively |
| 476 | // disable OSR when single stepping, but that's currently hard to know at this point. |
| 477 | if (Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method)) { |
Aart Bik | 29bdaee | 2016-05-18 15:44:07 -0700 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 481 | // We found a stack map, now fill the frame with dex register values from the interpreter's |
| 482 | // shadow frame. |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 483 | DexRegisterMap vreg_map = code_info.GetDexRegisterMapOf(stack_map); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 484 | |
| 485 | frame_size = osr_method->GetFrameSizeInBytes(); |
| 486 | |
| 487 | // Allocate memory to put shadow frame values. The osr stub will copy that memory to |
| 488 | // stack. |
| 489 | // Note that we could pass the shadow frame to the stub, and let it copy the values there, |
| 490 | // but that is engineering complexity not worth the effort for something like OSR. |
| 491 | memory = reinterpret_cast<void**>(malloc(frame_size)); |
| 492 | CHECK(memory != nullptr); |
| 493 | memset(memory, 0, frame_size); |
| 494 | |
| 495 | // Art ABI: ArtMethod is at the bottom of the stack. |
| 496 | memory[0] = method; |
| 497 | |
| 498 | shadow_frame = thread->PopShadowFrame(); |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 499 | if (vreg_map.empty()) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 500 | // If we don't have a dex register map, then there are no live dex registers at |
| 501 | // this dex pc. |
| 502 | } else { |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 503 | DCHECK_EQ(vreg_map.size(), number_of_vregs); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 504 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 505 | DexRegisterLocation::Kind location = vreg_map[vreg].GetKind(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 506 | if (location == DexRegisterLocation::Kind::kNone) { |
Nicolas Geoffray | c0b2796 | 2016-02-16 12:06:05 +0000 | [diff] [blame] | 507 | // Dex register is dead or uninitialized. |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 508 | continue; |
| 509 | } |
| 510 | |
| 511 | if (location == DexRegisterLocation::Kind::kConstant) { |
| 512 | // We skip constants because the compiled code knows how to handle them. |
| 513 | continue; |
| 514 | } |
| 515 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 516 | DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 517 | |
| 518 | int32_t vreg_value = shadow_frame->GetVReg(vreg); |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 519 | int32_t slot_offset = vreg_map[vreg].GetStackOffsetInBytes(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 520 | DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size)); |
| 521 | DCHECK_GT(slot_offset, 0); |
| 522 | (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value; |
| 523 | } |
| 524 | } |
| 525 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 526 | native_pc = stack_map.GetNativePcOffset(kRuntimeISA) + |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 527 | osr_method->GetEntryPoint(); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 528 | VLOG(jit) << "Jumping to " |
| 529 | << method_name |
| 530 | << "@" |
| 531 | << std::hex << reinterpret_cast<uintptr_t>(native_pc); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 534 | { |
| 535 | ManagedStack fragment; |
| 536 | thread->PushManagedStackFragment(&fragment); |
| 537 | (*art_quick_osr_stub)(memory, |
| 538 | frame_size, |
| 539 | native_pc, |
| 540 | result, |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 541 | shorty, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 542 | thread); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 543 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 544 | if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) { |
| 545 | thread->DeoptimizeWithDeoptimizationException(result); |
| 546 | } |
| 547 | thread->PopManagedStackFragment(fragment); |
| 548 | } |
| 549 | free(memory); |
| 550 | thread->PushShadowFrame(shadow_frame); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 551 | VLOG(jit) << "Done running OSR code for " << method_name; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 555 | void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) { |
| 556 | if (bytes > 4 * MB) { |
| 557 | LOG(INFO) << "Compiler allocated " |
| 558 | << PrettySize(bytes) |
| 559 | << " to compile " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 560 | << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 561 | } |
| 562 | MutexLock mu(Thread::Current(), lock_); |
| 563 | memory_use_.AddValue(bytes); |
| 564 | } |
| 565 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 566 | class JitCompileTask final : public Task { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 567 | public: |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 568 | enum class TaskKind { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 569 | kAllocateProfile, |
| 570 | kCompile, |
Nicolas Geoffray | 075456e | 2018-12-14 08:54:21 +0000 | [diff] [blame] | 571 | kCompileBaseline, |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 572 | kCompileOsr, |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 573 | kPreCompile, |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 574 | }; |
| 575 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 576 | JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind), klass_(nullptr) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 577 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 578 | // For a non-bootclasspath class, add a global ref to the class to prevent class unloading |
| 579 | // until compilation is done. |
| 580 | if (method->GetDeclaringClass()->GetClassLoader() != nullptr) { |
| 581 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 582 | CHECK(klass_ != nullptr); |
| 583 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | ~JitCompileTask() { |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 587 | if (klass_ != nullptr) { |
| 588 | ScopedObjectAccess soa(Thread::Current()); |
| 589 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 590 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 593 | void Run(Thread* self) override { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 594 | ScopedObjectAccess soa(self); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 595 | switch (kind_) { |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 596 | case TaskKind::kPreCompile: |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 597 | case TaskKind::kCompile: |
Nicolas Geoffray | 075456e | 2018-12-14 08:54:21 +0000 | [diff] [blame] | 598 | case TaskKind::kCompileBaseline: |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 599 | case TaskKind::kCompileOsr: { |
| 600 | Runtime::Current()->GetJit()->CompileMethod( |
| 601 | method_, |
| 602 | self, |
Nicolas Geoffray | 075456e | 2018-12-14 08:54:21 +0000 | [diff] [blame] | 603 | /* baseline= */ (kind_ == TaskKind::kCompileBaseline), |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 604 | /* osr= */ (kind_ == TaskKind::kCompileOsr), |
| 605 | /* prejit= */ (kind_ == TaskKind::kPreCompile)); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | case TaskKind::kAllocateProfile: { |
| 609 | if (ProfilingInfo::Create(self, method_, /* retry_allocation= */ true)) { |
| 610 | VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_); |
| 611 | } |
| 612 | break; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 613 | } |
| 614 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 615 | ProfileSaver::NotifyJitActivity(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 618 | void Finalize() override { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 619 | delete this; |
| 620 | } |
| 621 | |
| 622 | private: |
| 623 | ArtMethod* const method_; |
| 624 | const TaskKind kind_; |
| 625 | jobject klass_; |
| 626 | |
| 627 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
| 628 | }; |
| 629 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 630 | class ZygoteTask final : public Task { |
| 631 | public: |
| 632 | ZygoteTask() {} |
| 633 | |
| 634 | void Run(Thread* self) override { |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 635 | Runtime* runtime = Runtime::Current(); |
| 636 | std::string profile_file; |
| 637 | for (const std::string& option : runtime->GetImageCompilerOptions()) { |
| 638 | if (android::base::StartsWith(option, "--profile-file=")) { |
| 639 | profile_file = option.substr(strlen("--profile-file=")); |
| 640 | break; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | const std::vector<const DexFile*>& boot_class_path = |
| 645 | runtime->GetClassLinker()->GetBootClassPath(); |
| 646 | ScopedNullHandle<mirror::ClassLoader> null_handle; |
| 647 | // We add to the queue for zygote so that we can fork processes in-between |
| 648 | // compilations. |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 649 | uint32_t added_to_queue = runtime->GetJit()->CompileMethodsFromProfile( |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 650 | self, boot_class_path, profile_file, null_handle, /* add_to_queue= */ true); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 651 | |
| 652 | JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 653 | code_cache->GetZygoteMap()->Initialize(added_to_queue); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void Finalize() override { |
| 657 | delete this; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | private: |
| 661 | DISALLOW_COPY_AND_ASSIGN(ZygoteTask); |
| 662 | }; |
| 663 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 664 | static std::string GetProfileFile(const std::string& dex_location) { |
| 665 | // Hardcoded assumption where the profile file is. |
| 666 | // TODO(ngeoffray): this is brittle and we would need to change change if we |
| 667 | // wanted to do more eager JITting of methods in a profile. This is |
| 668 | // currently only for system server. |
| 669 | return dex_location + ".prof"; |
| 670 | } |
| 671 | |
| 672 | class JitProfileTask final : public Task { |
| 673 | public: |
| 674 | JitProfileTask(const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 675 | jobject class_loader) { |
| 676 | ScopedObjectAccess soa(Thread::Current()); |
| 677 | StackHandleScope<1> hs(soa.Self()); |
| 678 | Handle<mirror::ClassLoader> h_loader(hs.NewHandle( |
| 679 | soa.Decode<mirror::ClassLoader>(class_loader))); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 680 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 681 | for (const auto& dex_file : dex_files) { |
| 682 | dex_files_.push_back(dex_file.get()); |
| 683 | // Register the dex file so that we can guarantee it doesn't get deleted |
| 684 | // while reading it during the task. |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 685 | class_linker->RegisterDexFile(*dex_file.get(), h_loader.Get()); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 686 | } |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 687 | // We also create our own global ref to use this class loader later. |
| 688 | class_loader_ = soa.Vm()->AddGlobalRef(soa.Self(), h_loader.Get()); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void Run(Thread* self) override { |
| 692 | ScopedObjectAccess soa(self); |
| 693 | StackHandleScope<1> hs(self); |
| 694 | Handle<mirror::ClassLoader> loader = hs.NewHandle<mirror::ClassLoader>( |
| 695 | soa.Decode<mirror::ClassLoader>(class_loader_)); |
| 696 | Runtime::Current()->GetJit()->CompileMethodsFromProfile( |
| 697 | self, |
| 698 | dex_files_, |
| 699 | GetProfileFile(dex_files_[0]->GetLocation()), |
| 700 | loader, |
| 701 | /* add_to_queue= */ false); |
| 702 | } |
| 703 | |
| 704 | void Finalize() override { |
| 705 | delete this; |
| 706 | } |
| 707 | |
Nicolas Geoffray | f5a07ae | 2019-06-20 14:59:07 +0100 | [diff] [blame] | 708 | ~JitProfileTask() { |
| 709 | ScopedObjectAccess soa(Thread::Current()); |
| 710 | soa.Vm()->DeleteGlobalRef(soa.Self(), class_loader_); |
| 711 | } |
| 712 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 713 | private: |
| 714 | std::vector<const DexFile*> dex_files_; |
| 715 | jobject class_loader_; |
| 716 | |
| 717 | DISALLOW_COPY_AND_ASSIGN(JitProfileTask); |
| 718 | }; |
| 719 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 720 | void Jit::CreateThreadPool() { |
| 721 | // There is a DCHECK in the 'AddSamples' method to ensure the tread pool |
| 722 | // is not null when we instrument. |
| 723 | |
| 724 | // We need peers as we may report the JIT thread, e.g., in the debugger. |
| 725 | constexpr bool kJitPoolNeedsPeers = true; |
| 726 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers)); |
| 727 | |
| 728 | thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority()); |
| 729 | Start(); |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 730 | |
| 731 | // If we're not using the default boot image location, request a JIT task to |
| 732 | // compile all methods in the boot image profile. |
| 733 | Runtime* runtime = Runtime::Current(); |
David Srbecky | 3db3d37 | 2019-04-17 18:19:17 +0100 | [diff] [blame] | 734 | if (runtime->IsZygote() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) { |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 735 | thread_pool_->AddTask(Thread::Current(), new ZygoteTask()); |
| 736 | } |
| 737 | } |
| 738 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 739 | void Jit::RegisterDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 740 | jobject class_loader) { |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 741 | if (dex_files.empty()) { |
| 742 | return; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 743 | } |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 744 | Runtime* runtime = Runtime::Current(); |
| 745 | if (runtime->IsSystemServer() && runtime->IsUsingApexBootImageLocation() && UseJitCompilation()) { |
| 746 | thread_pool_->AddTask(Thread::Current(), new JitProfileTask(dex_files, class_loader)); |
| 747 | } |
| 748 | } |
| 749 | |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 750 | uint32_t Jit::CompileMethodsFromProfile( |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 751 | Thread* self, |
| 752 | const std::vector<const DexFile*>& dex_files, |
| 753 | const std::string& profile_file, |
| 754 | Handle<mirror::ClassLoader> class_loader, |
| 755 | bool add_to_queue) { |
| 756 | |
| 757 | if (profile_file.empty()) { |
| 758 | LOG(WARNING) << "Expected a profile file in JIT zygote mode"; |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 759 | return 0u; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | std::string error_msg; |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 763 | ScopedFlock profile = LockedFile::Open( |
| 764 | profile_file.c_str(), O_RDONLY, /* block= */ false, &error_msg); |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 765 | |
| 766 | // Return early if we're unable to obtain a lock on the profile. |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 767 | if (profile.get() == nullptr) { |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 768 | LOG(ERROR) << "Cannot lock profile: " << error_msg; |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 769 | return 0u; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | ProfileCompilationInfo profile_info; |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 773 | if (!profile_info.Load(profile->Fd())) { |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 774 | LOG(ERROR) << "Could not load profile file"; |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 775 | return 0u; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 776 | } |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 777 | ScopedObjectAccess soa(self); |
| 778 | StackHandleScope<1> hs(self); |
| 779 | MutableHandle<mirror::DexCache> dex_cache = hs.NewHandle<mirror::DexCache>(nullptr); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 780 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 781 | uint32_t added_to_queue = 0u; |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 782 | for (const DexFile* dex_file : dex_files) { |
Nicolas Geoffray | f59bc11 | 2019-04-03 10:29:29 +0100 | [diff] [blame] | 783 | if (LocationIsOnRuntimeModule(dex_file->GetLocation().c_str())) { |
| 784 | // The runtime module jars are already preopted. |
| 785 | continue; |
| 786 | } |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 787 | // To speed up class lookups, generate a type lookup table for |
| 788 | // the dex file. |
Nicolas Geoffray | c5e3a52 | 2019-04-30 09:47:55 +0100 | [diff] [blame] | 789 | if (dex_file->GetOatDexFile() == nullptr) { |
| 790 | TypeLookupTable type_lookup_table = TypeLookupTable::Create(*dex_file); |
| 791 | type_lookup_tables_.push_back( |
| 792 | std::make_unique<art::OatDexFile>(std::move(type_lookup_table))); |
| 793 | dex_file->SetOatDexFile(type_lookup_tables_.back().get()); |
| 794 | } |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 795 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 796 | std::set<dex::TypeIndex> class_types; |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 797 | std::set<uint16_t> all_methods; |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 798 | if (!profile_info.GetClassesAndMethods(*dex_file, |
| 799 | &class_types, |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 800 | &all_methods, |
| 801 | &all_methods, |
| 802 | &all_methods)) { |
Nicolas Geoffray | f59bc11 | 2019-04-03 10:29:29 +0100 | [diff] [blame] | 803 | // This means the profile file did not reference the dex file, which is the case |
| 804 | // if there's no classes and methods of that dex file in the profile. |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 805 | continue; |
| 806 | } |
| 807 | dex_cache.Assign(class_linker->FindDexCache(self, *dex_file)); |
| 808 | CHECK(dex_cache != nullptr) << "Could not find dex cache for " << dex_file->GetLocation(); |
Nicolas Geoffray | 1d077ac | 2019-02-27 15:53:28 +0000 | [diff] [blame] | 809 | |
| 810 | for (uint16_t method_idx : all_methods) { |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 811 | ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType( |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 812 | method_idx, dex_cache, class_loader); |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 813 | if (method == nullptr) { |
| 814 | self->ClearException(); |
| 815 | continue; |
| 816 | } |
| 817 | if (!method->IsCompilable() || !method->IsInvokable()) { |
| 818 | continue; |
| 819 | } |
| 820 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 821 | if (class_linker->IsQuickToInterpreterBridge(entry_point) || |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 822 | class_linker->IsQuickGenericJniStub(entry_point) || |
| 823 | class_linker->IsQuickResolutionStub(entry_point)) { |
Nicolas Geoffray | b10f028 | 2019-04-12 17:30:52 +0100 | [diff] [blame] | 824 | // Special case ZygoteServer class so that it gets compiled before the |
| 825 | // zygote enters it. This avoids needing to do OSR during app startup. |
| 826 | // TODO: have a profile instead. |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 827 | if (!add_to_queue || method->GetDeclaringClass()->DescriptorEquals( |
Nicolas Geoffray | b10f028 | 2019-04-12 17:30:52 +0100 | [diff] [blame] | 828 | "Lcom/android/internal/os/ZygoteServer;")) { |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 829 | CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ true); |
Nicolas Geoffray | b10f028 | 2019-04-12 17:30:52 +0100 | [diff] [blame] | 830 | } else { |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 831 | ++added_to_queue; |
Nicolas Geoffray | b10f028 | 2019-04-12 17:30:52 +0100 | [diff] [blame] | 832 | thread_pool_->AddTask(self, |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 833 | new JitCompileTask(method, JitCompileTask::TaskKind::kPreCompile)); |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 834 | if (Runtime::Current()->IsZygote()) { |
| 835 | method->SetZygoteCompiled(); |
| 836 | } |
Nicolas Geoffray | b10f028 | 2019-04-12 17:30:52 +0100 | [diff] [blame] | 837 | } |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 841 | return added_to_queue; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 844 | static bool IgnoreSamplesForMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 845 | if (method->IsClassInitializer() || !method->IsCompilable() || method->IsZygoteCompiled()) { |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 846 | // We do not want to compile such methods. |
| 847 | return true; |
| 848 | } |
| 849 | if (method->IsNative()) { |
| 850 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 851 | if (klass == GetClassRoot<mirror::MethodHandle>() || |
| 852 | klass == GetClassRoot<mirror::VarHandle>()) { |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 853 | // MethodHandle and VarHandle invocation methods are required to throw an |
| 854 | // UnsupportedOperationException if invoked reflectively. We achieve this by having native |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 855 | // implementations that raise the exception. We need to disable JIT compilation of these JNI |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 856 | // methods as it can lead to transitioning between JIT compiled JNI stubs and generic JNI |
| 857 | // stubs. Since these stubs have different stack representations we can then crash in stack |
| 858 | // walking (b/78151261). |
| 859 | return true; |
| 860 | } |
| 861 | } |
| 862 | return false; |
| 863 | } |
| 864 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 865 | bool Jit::MaybeCompileMethod(Thread* self, |
| 866 | ArtMethod* method, |
| 867 | uint32_t old_count, |
| 868 | uint32_t new_count, |
| 869 | bool with_backedges) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 870 | if (thread_pool_ == nullptr) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 871 | return false; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 872 | } |
Orion Hodson | 52f5a1f | 2018-05-02 11:05:44 +0100 | [diff] [blame] | 873 | if (IgnoreSamplesForMethod(method)) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 874 | return false; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 875 | } |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 876 | if (HotMethodThreshold() == 0) { |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 877 | // Tests might request JIT on first use (compiled synchronously in the interpreter). |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 878 | return false; |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 879 | } |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 880 | DCHECK_GT(WarmMethodThreshold(), 0); |
| 881 | DCHECK_GT(HotMethodThreshold(), WarmMethodThreshold()); |
| 882 | DCHECK_GT(OSRMethodThreshold(), HotMethodThreshold()); |
| 883 | DCHECK_GE(PriorityThreadWeight(), 1); |
| 884 | DCHECK_LE(PriorityThreadWeight(), HotMethodThreshold()); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 885 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 886 | if (old_count < WarmMethodThreshold() && new_count >= WarmMethodThreshold()) { |
| 887 | // Note: Native method have no "warm" state or profiling info. |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 888 | if (!method->IsNative() && |
| 889 | (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) && |
| 890 | code_cache_->CanAllocateProfilingInfo()) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 891 | bool success = ProfilingInfo::Create(self, method, /* retry_allocation= */ false); |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 892 | if (success) { |
| 893 | VLOG(jit) << "Start profiling " << method->PrettyMethod(); |
| 894 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 895 | |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 896 | if (thread_pool_ == nullptr) { |
| 897 | // Calling ProfilingInfo::Create might put us in a suspended state, which could |
| 898 | // lead to the thread pool being deleted when we are shutting down. |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 899 | return false; |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 900 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 901 | |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 902 | if (!success) { |
| 903 | // We failed allocating. Instead of doing the collection on the Java thread, we push |
| 904 | // an allocation to a compiler thread, that will do the collection. |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 905 | thread_pool_->AddTask( |
| 906 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kAllocateProfile)); |
Nicolas Geoffray | 941c6ec | 2017-06-09 11:53:23 +0000 | [diff] [blame] | 907 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 908 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 909 | } |
| 910 | if (UseJitCompilation()) { |
David Srbecky | c45b589 | 2019-04-24 10:32:04 +0100 | [diff] [blame] | 911 | if (old_count == 0 && |
| 912 | method->IsNative() && |
| 913 | Runtime::Current()->IsUsingApexBootImageLocation()) { |
| 914 | // jitzygote: Compile JNI stub on first use to avoid the expensive generic stub. |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 915 | CompileMethod(method, self, /* baseline= */ false, /* osr= */ false, /* prejit= */ false); |
David Srbecky | c45b589 | 2019-04-24 10:32:04 +0100 | [diff] [blame] | 916 | return true; |
| 917 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 918 | if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) { |
| 919 | if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 920 | DCHECK(thread_pool_ != nullptr); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 921 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompile)); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 922 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 923 | } |
| 924 | if (old_count < OSRMethodThreshold() && new_count >= OSRMethodThreshold()) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 925 | if (!with_backedges) { |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 926 | return false; |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 927 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 928 | DCHECK(!method->IsNative()); // No back edges reported for native methods. |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 929 | if (!code_cache_->IsOsrCompiled(method)) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 930 | DCHECK(thread_pool_ != nullptr); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 931 | thread_pool_->AddTask( |
| 932 | self, new JitCompileTask(method, JitCompileTask::TaskKind::kCompileOsr)); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 933 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 934 | } |
| 935 | } |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 936 | return true; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Alex Light | 59b950f | 2018-10-08 10:43:06 -0700 | [diff] [blame] | 939 | class ScopedSetRuntimeThread { |
| 940 | public: |
| 941 | explicit ScopedSetRuntimeThread(Thread* self) |
| 942 | : self_(self), was_runtime_thread_(self_->IsRuntimeThread()) { |
| 943 | self_->SetIsRuntimeThread(true); |
| 944 | } |
| 945 | |
| 946 | ~ScopedSetRuntimeThread() { |
| 947 | self_->SetIsRuntimeThread(was_runtime_thread_); |
| 948 | } |
| 949 | |
| 950 | private: |
| 951 | Thread* self_; |
| 952 | bool was_runtime_thread_; |
| 953 | }; |
| 954 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 955 | void Jit::MethodEntered(Thread* thread, ArtMethod* method) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 956 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 5e33ccd | 2019-07-03 10:53:08 +0100 | [diff] [blame] | 957 | if (UNLIKELY(runtime->UseJitCompilation() && JitAtFirstUse())) { |
Vladimir Marko | be0c7cf | 2018-03-19 13:40:56 +0000 | [diff] [blame] | 958 | ArtMethod* np_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Vladimir Marko | be0c7cf | 2018-03-19 13:40:56 +0000 | [diff] [blame] | 959 | if (np_method->IsCompilable()) { |
Nicolas Geoffray | 5e33ccd | 2019-07-03 10:53:08 +0100 | [diff] [blame] | 960 | if (!np_method->IsNative() && GetCodeCache()->CanAllocateProfilingInfo()) { |
Nicolas Geoffray | 29bb803 | 2019-06-06 10:32:24 +0100 | [diff] [blame] | 961 | // The compiler requires a ProfilingInfo object for non-native methods. |
| 962 | ProfilingInfo::Create(thread, np_method, /* retry_allocation= */ true); |
| 963 | } |
| 964 | // TODO(ngeoffray): For JIT at first use, use kPreCompile. Currently we don't due to |
| 965 | // conflicts with jitzygote optimizations. |
| 966 | JitCompileTask compile_task(method, JitCompileTask::TaskKind::kCompile); |
Alex Light | 59b950f | 2018-10-08 10:43:06 -0700 | [diff] [blame] | 967 | // Fake being in a runtime thread so that class-load behavior will be the same as normal jit. |
| 968 | ScopedSetRuntimeThread ssrt(thread); |
Vladimir Marko | be0c7cf | 2018-03-19 13:40:56 +0000 | [diff] [blame] | 969 | compile_task.Run(thread); |
| 970 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 971 | return; |
| 972 | } |
| 973 | |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 974 | if (UNLIKELY(method->IsZygoteCompiled())) { |
| 975 | const void* code_ptr = code_cache_->GetZygoteMap()->GetCodeFor(method); |
| 976 | if (code_ptr != nullptr) { |
| 977 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(method, code_ptr); |
| 978 | return; |
| 979 | } |
| 980 | } |
| 981 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 982 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 983 | // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 984 | // instead of interpreting the method. We don't update it for instrumentation as the entrypoint |
| 985 | // must remain the instrumentation entrypoint. |
| 986 | if ((profiling_info != nullptr) && |
| 987 | (profiling_info->GetSavedEntryPoint() != nullptr) && |
| 988 | (method->GetEntryPointFromQuickCompiledCode() != GetQuickInstrumentationEntryPoint())) { |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 989 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 990 | method, profiling_info->GetSavedEntryPoint()); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 991 | } else { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 992 | AddSamples(thread, method, 1, /* with_backedges= */false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 996 | void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object, |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 997 | ArtMethod* caller, |
| 998 | uint32_t dex_pc, |
| 999 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1000 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1001 | DCHECK(this_object != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1002 | ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1003 | if (info != nullptr) { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 1004 | info->AddInvokeInfo(dex_pc, this_object->GetClass()); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | void Jit::WaitForCompilationToFinish(Thread* self) { |
| 1009 | if (thread_pool_ != nullptr) { |
| 1010 | thread_pool_->Wait(self, false, false); |
| 1011 | } |
| 1012 | } |
| 1013 | |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1014 | void Jit::Stop() { |
| 1015 | Thread* self = Thread::Current(); |
| 1016 | // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice. |
| 1017 | WaitForCompilationToFinish(self); |
| 1018 | GetThreadPool()->StopWorkers(self); |
| 1019 | WaitForCompilationToFinish(self); |
| 1020 | } |
| 1021 | |
| 1022 | void Jit::Start() { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 1023 | GetThreadPool()->StartWorkers(Thread::Current()); |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 1026 | ScopedJitSuspend::ScopedJitSuspend() { |
| 1027 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 1028 | was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr); |
| 1029 | if (was_on_) { |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1030 | jit->Stop(); |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | ScopedJitSuspend::~ScopedJitSuspend() { |
| 1035 | if (was_on_) { |
| 1036 | DCHECK(Runtime::Current()->GetJit() != nullptr); |
| 1037 | DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr); |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 1038 | Runtime::Current()->GetJit()->Start(); |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Nicolas Geoffray | 0d54cfb | 2019-05-03 09:13:52 +0100 | [diff] [blame] | 1042 | void Jit::PostForkChildAction(bool is_system_server, bool is_zygote) { |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1043 | if (is_zygote || Runtime::Current()->IsSafeMode()) { |
| 1044 | // Remove potential tasks that have been inherited from the zygote. |
Nicolas Geoffray | 953da54 | 2019-03-04 10:09:11 +0000 | [diff] [blame] | 1045 | thread_pool_->RemoveAllTasks(Thread::Current()); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1046 | // Delete the thread pool, we are not going to JIT. |
| 1047 | thread_pool_.reset(nullptr); |
| 1048 | return; |
| 1049 | } |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 1050 | // At this point, the compiler options have been adjusted to the particular configuration |
| 1051 | // of the forked child. Parse them again. |
| 1052 | jit_update_options_(jit_compiler_handle_); |
| 1053 | |
| 1054 | // Adjust the status of code cache collection: the status from zygote was to not collect. |
| 1055 | code_cache_->SetGarbageCollectCode(!jit_generate_debug_info_(jit_compiler_handle_) && |
| 1056 | !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (thread_pool_ != nullptr) { |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame^] | 1059 | // Remove potential tasks that have been inherited from the zygote. |
| 1060 | thread_pool_->RemoveAllTasks(Thread::Current()); |
| 1061 | if (is_system_server && |
| 1062 | Runtime::Current()->IsUsingApexBootImageLocation() && |
| 1063 | UseJitCompilation()) { |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 1064 | // Disable garbage collection: we don't want it to delete methods we're compiling |
| 1065 | // through boot and system server profiles. |
| 1066 | // TODO(ngeoffray): Fix this so we still collect deoptimized and unused code. |
| 1067 | code_cache_->SetGarbageCollectCode(false); |
Nicolas Geoffray | 0d54cfb | 2019-05-03 09:13:52 +0100 | [diff] [blame] | 1068 | } |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1069 | |
| 1070 | // Resume JIT compilation. |
| 1071 | thread_pool_->CreateThreads(); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | void Jit::PreZygoteFork() { |
| 1076 | if (thread_pool_ == nullptr) { |
| 1077 | return; |
| 1078 | } |
| 1079 | thread_pool_->DeleteThreads(); |
| 1080 | } |
| 1081 | |
| 1082 | void Jit::PostZygoteFork() { |
| 1083 | if (thread_pool_ == nullptr) { |
| 1084 | return; |
| 1085 | } |
| 1086 | thread_pool_->CreateThreads(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 1089 | bool Jit::CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const { |
| 1090 | return !is_for_shared_region || |
| 1091 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(method->GetDeclaringClass()); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | bool Jit::CanEncodeClass(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const { |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 1095 | return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | bool Jit::CanEncodeString(ObjPtr<mirror::String> string, bool is_for_shared_region) const { |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 1099 | return !is_for_shared_region || Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 1102 | bool Jit::CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const { |
| 1103 | if (!is_for_shared_region) { |
| 1104 | return cls->IsInitialized(); |
| 1105 | } else { |
| 1106 | // Look up the class status in the oat file. |
| 1107 | const DexFile& dex_file = *cls->GetDexCache()->GetDexFile(); |
| 1108 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 1109 | // In case we run without an image there won't be a backing oat file. |
| 1110 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
| 1111 | return false; |
| 1112 | } |
| 1113 | uint16_t class_def_index = cls->GetDexClassDefIndex(); |
| 1114 | return oat_dex_file->GetOatClass(class_def_index).GetStatus() >= ClassStatus::kInitialized; |
| 1115 | } |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1118 | } // namespace jit |
| 1119 | } // namespace art |