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_code_cache.h" |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 22 | |
Andreas Gampe | 5629d2d | 2017-05-15 16:28:13 -0700 | [diff] [blame] | 23 | #include "arch/context.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.h" |
Andreas Gampe | f0f3c59 | 2018-06-26 13:28:00 -0700 | [diff] [blame] | 26 | #include "base/histogram-inl.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 27 | #include "base/logging.h" // For VLOG. |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 28 | #include "base/membarrier.h" |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 29 | #include "base/memfd.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 30 | #include "base/mem_map.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 31 | #include "base/quasi_atomic.h" |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 32 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 33 | #include "base/systrace.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 34 | #include "base/time_utils.h" |
Orion Hodson | f233136 | 2018-07-11 15:14:10 +0100 | [diff] [blame] | 35 | #include "base/utils.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 36 | #include "cha.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 37 | #include "debugger_interface.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 38 | #include "dex/dex_file_loader.h" |
Andreas Gampe | f0f3c59 | 2018-06-26 13:28:00 -0700 | [diff] [blame] | 39 | #include "dex/method_reference.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 40 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 41 | #include "gc/accounting/bitmap-inl.h" |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 42 | #include "gc/allocator/dlmalloc.h" |
Nicolas Geoffray | cf48fa0 | 2016-07-30 22:49:11 +0100 | [diff] [blame] | 43 | #include "gc/scoped_gc_critical_section.h" |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 44 | #include "handle.h" |
Andreas Gampe | f0f3c59 | 2018-06-26 13:28:00 -0700 | [diff] [blame] | 45 | #include "instrumentation.h" |
Andreas Gampe | b2d18fa | 2017-06-06 20:46:10 -0700 | [diff] [blame] | 46 | #include "intern_table.h" |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 47 | #include "jit/jit.h" |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 48 | #include "jit/profiling_info.h" |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 49 | #include "jit/jit_scoped_code_cache_write.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 50 | #include "linear_alloc.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 51 | #include "oat_file-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 52 | #include "oat_quick_method_header.h" |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 53 | #include "object_callbacks.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 54 | #include "profile/profile_compilation_info.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 55 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 56 | #include "stack.h" |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 57 | #include "thread-current-inl.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 58 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 59 | |
| 60 | namespace art { |
| 61 | namespace jit { |
| 62 | |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 63 | static constexpr size_t kCodeSizeLogThreshold = 50 * KB; |
| 64 | static constexpr size_t kStackMapSizeLogThreshold = 50 * KB; |
| 65 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 66 | class JitCodeCache::JniStubKey { |
| 67 | public: |
| 68 | explicit JniStubKey(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) |
| 69 | : shorty_(method->GetShorty()), |
| 70 | is_static_(method->IsStatic()), |
| 71 | is_fast_native_(method->IsFastNative()), |
| 72 | is_critical_native_(method->IsCriticalNative()), |
| 73 | is_synchronized_(method->IsSynchronized()) { |
| 74 | DCHECK(!(is_fast_native_ && is_critical_native_)); |
| 75 | } |
| 76 | |
| 77 | bool operator<(const JniStubKey& rhs) const { |
| 78 | if (is_static_ != rhs.is_static_) { |
| 79 | return rhs.is_static_; |
| 80 | } |
| 81 | if (is_synchronized_ != rhs.is_synchronized_) { |
| 82 | return rhs.is_synchronized_; |
| 83 | } |
| 84 | if (is_fast_native_ != rhs.is_fast_native_) { |
| 85 | return rhs.is_fast_native_; |
| 86 | } |
| 87 | if (is_critical_native_ != rhs.is_critical_native_) { |
| 88 | return rhs.is_critical_native_; |
| 89 | } |
| 90 | return strcmp(shorty_, rhs.shorty_) < 0; |
| 91 | } |
| 92 | |
| 93 | // Update the shorty to point to another method's shorty. Call this function when removing |
| 94 | // the method that references the old shorty from JniCodeData and not removing the entire |
| 95 | // JniCodeData; the old shorty may become a dangling pointer when that method is unloaded. |
| 96 | void UpdateShorty(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 97 | const char* shorty = method->GetShorty(); |
| 98 | DCHECK_STREQ(shorty_, shorty); |
| 99 | shorty_ = shorty; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | // The shorty points to a DexFile data and may need to change |
| 104 | // to point to the same shorty in a different DexFile. |
| 105 | mutable const char* shorty_; |
| 106 | |
| 107 | const bool is_static_; |
| 108 | const bool is_fast_native_; |
| 109 | const bool is_critical_native_; |
| 110 | const bool is_synchronized_; |
| 111 | }; |
| 112 | |
| 113 | class JitCodeCache::JniStubData { |
| 114 | public: |
| 115 | JniStubData() : code_(nullptr), methods_() {} |
| 116 | |
| 117 | void SetCode(const void* code) { |
| 118 | DCHECK(code != nullptr); |
| 119 | code_ = code; |
| 120 | } |
| 121 | |
| 122 | const void* GetCode() const { |
| 123 | return code_; |
| 124 | } |
| 125 | |
| 126 | bool IsCompiled() const { |
| 127 | return GetCode() != nullptr; |
| 128 | } |
| 129 | |
| 130 | void AddMethod(ArtMethod* method) { |
| 131 | if (!ContainsElement(methods_, method)) { |
| 132 | methods_.push_back(method); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const std::vector<ArtMethod*>& GetMethods() const { |
| 137 | return methods_; |
| 138 | } |
| 139 | |
| 140 | void RemoveMethodsIn(const LinearAlloc& alloc) { |
| 141 | auto kept_end = std::remove_if( |
| 142 | methods_.begin(), |
| 143 | methods_.end(), |
| 144 | [&alloc](ArtMethod* method) { return alloc.ContainsUnsafe(method); }); |
| 145 | methods_.erase(kept_end, methods_.end()); |
| 146 | } |
| 147 | |
| 148 | bool RemoveMethod(ArtMethod* method) { |
| 149 | auto it = std::find(methods_.begin(), methods_.end(), method); |
| 150 | if (it != methods_.end()) { |
| 151 | methods_.erase(it); |
| 152 | return true; |
| 153 | } else { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) { |
| 159 | std::replace(methods_.begin(), methods_.end(), old_method, new_method); |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | const void* code_; |
| 164 | std::vector<ArtMethod*> methods_; |
| 165 | }; |
| 166 | |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 167 | JitCodeCache* JitCodeCache::Create(bool used_only_for_profile_data, |
| 168 | bool rwx_memory_allowed, |
| 169 | bool is_zygote, |
| 170 | std::string* error_msg) { |
| 171 | // Register for membarrier expedited sync core if JIT will be generating code. |
| 172 | if (!used_only_for_profile_data) { |
| 173 | if (art::membarrier(art::MembarrierCommand::kRegisterPrivateExpeditedSyncCore) != 0) { |
| 174 | // MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE ensures that CPU instruction pipelines are |
| 175 | // flushed and it's used when adding code to the JIT. The memory used by the new code may |
| 176 | // have just been released and, in theory, the old code could still be in a pipeline. |
| 177 | VLOG(jit) << "Kernel does not support membarrier sync-core"; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Check whether the provided max capacity in options is below 1GB. |
| 182 | size_t max_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheMaxCapacity(); |
| 183 | // We need to have 32 bit offsets from method headers in code cache which point to things |
| 184 | // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work. |
| 185 | // Ensure we're below 1 GB to be safe. |
| 186 | if (max_capacity > 1 * GB) { |
| 187 | std::ostringstream oss; |
| 188 | oss << "Maxium code cache capacity is limited to 1 GB, " |
| 189 | << PrettySize(max_capacity) << " is too big"; |
| 190 | *error_msg = oss.str(); |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
| 194 | size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity(); |
| 195 | |
| 196 | std::unique_ptr<JitCodeCache> jit_code_cache(new JitCodeCache()); |
| 197 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 198 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
| 199 | jit_code_cache->private_region_.InitializeState(initial_capacity, max_capacity); |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 200 | |
| 201 | // Zygote should never collect code to share the memory with the children. |
| 202 | if (is_zygote) { |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 203 | jit_code_cache->garbage_collect_code_ = false; |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 206 | if (!jit_code_cache->private_region_.InitializeMappings( |
| 207 | rwx_memory_allowed, is_zygote, error_msg)) { |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 208 | return nullptr; |
| 209 | } |
| 210 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 211 | jit_code_cache->private_region_.InitializeSpaces(); |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 212 | |
| 213 | VLOG(jit) << "Created jit code cache: initial capacity=" |
| 214 | << PrettySize(initial_capacity) |
| 215 | << ", maximum capacity=" |
| 216 | << PrettySize(max_capacity); |
| 217 | |
| 218 | return jit_code_cache.release(); |
| 219 | } |
| 220 | |
| 221 | JitCodeCache::JitCodeCache() |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 222 | : is_weak_access_enabled_(true), |
| 223 | inline_cache_cond_("Jit inline cache condition variable", *Locks::jit_lock_), |
| 224 | lock_cond_("Jit code cache condition variable", *Locks::jit_lock_), |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 225 | collection_in_progress_(false), |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 226 | last_collection_increased_code_cache_(false), |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 227 | garbage_collect_code_(true), |
Nicolas Geoffray | fcdd729 | 2016-02-25 13:27:47 +0000 | [diff] [blame] | 228 | number_of_compilations_(0), |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 229 | number_of_osr_compilations_(0), |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 230 | number_of_collections_(0), |
| 231 | histogram_stack_map_memory_use_("Memory used for stack maps", 16), |
| 232 | histogram_code_memory_use_("Memory used for compiled code", 16), |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 233 | histogram_profiling_info_memory_use_("Memory used for profiling info", 16) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 236 | JitCodeCache::~JitCodeCache() {} |
| 237 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 238 | bool JitCodeCache::ContainsPc(const void* ptr) const { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 239 | return private_region_.IsInExecSpace(ptr) || shared_region_.IsInExecSpace(ptr); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 242 | bool JitCodeCache::WillExecuteJitCode(ArtMethod* method) { |
| 243 | ScopedObjectAccess soa(art::Thread::Current()); |
| 244 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
| 245 | if (ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
| 246 | return true; |
| 247 | } else if (method->GetEntryPointFromQuickCompiledCode() == GetQuickInstrumentationEntryPoint()) { |
| 248 | return FindCompiledCodeForInstrumentation(method) != nullptr; |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 253 | bool JitCodeCache::ContainsMethod(ArtMethod* method) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 254 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 255 | if (UNLIKELY(method->IsNative())) { |
| 256 | auto it = jni_stubs_map_.find(JniStubKey(method)); |
| 257 | if (it != jni_stubs_map_.end() && |
| 258 | it->second.IsCompiled() && |
| 259 | ContainsElement(it->second.GetMethods(), method)) { |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 262 | } else { |
| 263 | for (const auto& it : method_code_map_) { |
| 264 | if (it.second == method) { |
| 265 | return true; |
| 266 | } |
| 267 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 272 | const void* JitCodeCache::GetJniStubCode(ArtMethod* method) { |
| 273 | DCHECK(method->IsNative()); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 274 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 275 | auto it = jni_stubs_map_.find(JniStubKey(method)); |
| 276 | if (it != jni_stubs_map_.end()) { |
| 277 | JniStubData& data = it->second; |
| 278 | if (data.IsCompiled() && ContainsElement(data.GetMethods(), method)) { |
| 279 | return data.GetCode(); |
| 280 | } |
| 281 | } |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 285 | const void* JitCodeCache::FindCompiledCodeForInstrumentation(ArtMethod* method) { |
Alex Light | 839f53a | 2018-07-10 15:46:14 -0700 | [diff] [blame] | 286 | // If jit-gc is still on we use the SavedEntryPoint field for doing that and so cannot use it to |
| 287 | // find the instrumentation entrypoint. |
| 288 | if (LIKELY(GetGarbageCollectCode())) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 289 | return nullptr; |
| 290 | } |
| 291 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
| 292 | if (info == nullptr) { |
| 293 | return nullptr; |
| 294 | } |
| 295 | // When GC is disabled for trampoline tracing we will use SavedEntrypoint to hold the actual |
| 296 | // jit-compiled version of the method. If jit-gc is disabled for other reasons this will just be |
| 297 | // nullptr. |
| 298 | return info->GetSavedEntryPoint(); |
| 299 | } |
| 300 | |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 301 | const void* JitCodeCache::GetZygoteSavedEntryPoint(ArtMethod* method) { |
David Srbecky | 3db3d37 | 2019-04-17 18:19:17 +0100 | [diff] [blame] | 302 | if (Runtime::Current()->IsUsingApexBootImageLocation() && |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 303 | // Currently only applies to boot classpath |
| 304 | method->GetDeclaringClass()->GetClassLoader() == nullptr) { |
| 305 | const void* entry_point = nullptr; |
| 306 | if (method->IsNative()) { |
| 307 | const void* code_ptr = GetJniStubCode(method); |
| 308 | if (code_ptr != nullptr) { |
| 309 | entry_point = OatQuickMethodHeader::FromCodePointer(code_ptr)->GetEntryPoint(); |
| 310 | } |
Nicolas Geoffray | a3b31ba | 2019-04-14 20:10:16 +0100 | [diff] [blame] | 311 | } else { |
| 312 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
| 313 | if (profiling_info != nullptr) { |
| 314 | entry_point = profiling_info->GetSavedEntryPoint(); |
| 315 | } |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 316 | } |
| 317 | if (Runtime::Current()->IsZygote() || IsInZygoteExecSpace(entry_point)) { |
| 318 | return entry_point; |
| 319 | } |
| 320 | } |
| 321 | return nullptr; |
| 322 | } |
| 323 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 324 | uint8_t* JitCodeCache::CommitCode(Thread* self, |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 325 | JitMemoryRegion* region, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 326 | ArtMethod* method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 327 | uint8_t* stack_map, |
| 328 | uint8_t* roots_data, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 329 | const uint8_t* code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 330 | size_t code_size, |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 331 | size_t data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 332 | bool osr, |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 333 | const std::vector<Handle<mirror::Object>>& roots, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 334 | bool has_should_deoptimize_flag, |
| 335 | const ArenaSet<ArtMethod*>& cha_single_implementation_list) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 336 | uint8_t* result = CommitCodeInternal(self, |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 337 | region, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 338 | method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 339 | stack_map, |
| 340 | roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 341 | code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 342 | code_size, |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 343 | data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 344 | osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 345 | roots, |
| 346 | has_should_deoptimize_flag, |
| 347 | cha_single_implementation_list); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 348 | if (result == nullptr) { |
| 349 | // Retry. |
| 350 | GarbageCollectCache(self); |
| 351 | result = CommitCodeInternal(self, |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 352 | region, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 353 | method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 354 | stack_map, |
| 355 | roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 356 | code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 357 | code_size, |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 358 | data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 359 | osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 360 | roots, |
| 361 | has_should_deoptimize_flag, |
| 362 | cha_single_implementation_list); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 363 | } |
| 364 | return result; |
| 365 | } |
| 366 | |
| 367 | bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) { |
| 368 | bool in_collection = false; |
| 369 | while (collection_in_progress_) { |
| 370 | in_collection = true; |
| 371 | lock_cond_.Wait(self); |
| 372 | } |
| 373 | return in_collection; |
| 374 | } |
| 375 | |
| 376 | static uintptr_t FromCodeToAllocation(const void* code) { |
| 377 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 378 | return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 379 | } |
| 380 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 381 | static uint32_t ComputeRootTableSize(uint32_t number_of_roots) { |
| 382 | return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>); |
| 383 | } |
| 384 | |
| 385 | static uint32_t GetNumberOfRoots(const uint8_t* stack_map) { |
| 386 | // The length of the table is stored just before the stack map (and therefore at the end of |
| 387 | // the table itself), in order to be able to fetch it from a `stack_map` pointer. |
| 388 | return reinterpret_cast<const uint32_t*>(stack_map)[-1]; |
| 389 | } |
| 390 | |
Mathieu Chartier | 7a704be | 2016-11-22 13:24:40 -0800 | [diff] [blame] | 391 | static void FillRootTableLength(uint8_t* roots_data, uint32_t length) { |
| 392 | // Store the length of the table at the end. This will allow fetching it from a `stack_map` |
| 393 | // pointer. |
| 394 | reinterpret_cast<uint32_t*>(roots_data)[length] = length; |
| 395 | } |
| 396 | |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 397 | static const uint8_t* FromStackMapToRoots(const uint8_t* stack_map_data) { |
| 398 | return stack_map_data - ComputeRootTableSize(GetNumberOfRoots(stack_map_data)); |
| 399 | } |
| 400 | |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 401 | static void DCheckRootsAreValid(const std::vector<Handle<mirror::Object>>& roots) |
Alex Light | 3e36a9c | 2018-06-19 09:45:05 -0700 | [diff] [blame] | 402 | REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 403 | if (!kIsDebugBuild) { |
| 404 | return; |
| 405 | } |
Alex Light | 3e36a9c | 2018-06-19 09:45:05 -0700 | [diff] [blame] | 406 | // Put all roots in `roots_data`. |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 407 | for (Handle<mirror::Object> object : roots) { |
Alex Light | 3e36a9c | 2018-06-19 09:45:05 -0700 | [diff] [blame] | 408 | // Ensure the string is strongly interned. b/32995596 |
| 409 | if (object->IsString()) { |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 410 | ObjPtr<mirror::String> str = object->AsString(); |
Alex Light | 3e36a9c | 2018-06-19 09:45:05 -0700 | [diff] [blame] | 411 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 412 | CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void JitCodeCache::FillRootTable(uint8_t* roots_data, |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 418 | const std::vector<Handle<mirror::Object>>& roots) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 419 | GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 420 | const uint32_t length = roots.size(); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 421 | // Put all roots in `roots_data`. |
| 422 | for (uint32_t i = 0; i < length; ++i) { |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 423 | ObjPtr<mirror::Object> object = roots[i].Get(); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 424 | gc_roots[i] = GcRoot<mirror::Object>(object); |
| 425 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 428 | static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 429 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 430 | uint8_t* data = method_header->GetOptimizedCodeInfoPtr(); |
| 431 | uint32_t roots = GetNumberOfRoots(data); |
| 432 | if (number_of_roots != nullptr) { |
| 433 | *number_of_roots = roots; |
| 434 | } |
| 435 | return data - ComputeRootTableSize(roots); |
| 436 | } |
| 437 | |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 438 | // Use a sentinel for marking entries in the JIT table that have been cleared. |
| 439 | // This helps diagnosing in case the compiled code tries to wrongly access such |
| 440 | // entries. |
Andreas Gampe | 5629d2d | 2017-05-15 16:28:13 -0700 | [diff] [blame] | 441 | static mirror::Class* const weak_sentinel = |
| 442 | reinterpret_cast<mirror::Class*>(Context::kBadGprBase + 0xff); |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 443 | |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 444 | // Helper for the GC to process a weak class in a JIT root table. |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 445 | static inline void ProcessWeakClass(GcRoot<mirror::Class>* root_ptr, |
| 446 | IsMarkedVisitor* visitor, |
| 447 | mirror::Class* update) |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 448 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 449 | // This does not need a read barrier because this is called by GC. |
| 450 | mirror::Class* cls = root_ptr->Read<kWithoutReadBarrier>(); |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 451 | if (cls != nullptr && cls != weak_sentinel) { |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 452 | DCHECK((cls->IsClass<kDefaultVerifyFlags>())); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 453 | // Look at the classloader of the class to know if it has been unloaded. |
| 454 | // This does not need a read barrier because this is called by GC. |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 455 | ObjPtr<mirror::Object> class_loader = |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 456 | cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 457 | if (class_loader == nullptr || visitor->IsMarked(class_loader.Ptr()) != nullptr) { |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 458 | // The class loader is live, update the entry if the class has moved. |
| 459 | mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls)); |
| 460 | // Note that new_object can be null for CMS and newly allocated objects. |
| 461 | if (new_cls != nullptr && new_cls != cls) { |
| 462 | *root_ptr = GcRoot<mirror::Class>(new_cls); |
| 463 | } |
| 464 | } else { |
| 465 | // The class loader is not live, clear the entry. |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 466 | *root_ptr = GcRoot<mirror::Class>(update); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 471 | void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 472 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 473 | for (const auto& entry : method_code_map_) { |
| 474 | uint32_t number_of_roots = 0; |
| 475 | uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots); |
| 476 | GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
| 477 | for (uint32_t i = 0; i < number_of_roots; ++i) { |
| 478 | // This does not need a read barrier because this is called by GC. |
| 479 | mirror::Object* object = roots[i].Read<kWithoutReadBarrier>(); |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 480 | if (object == nullptr || object == weak_sentinel) { |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 481 | // entry got deleted in a previous sweep. |
Vladimir Marko | d355acf | 2019-03-21 17:09:40 +0000 | [diff] [blame] | 482 | } else if (object->IsString<kDefaultVerifyFlags>()) { |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 483 | mirror::Object* new_object = visitor->IsMarked(object); |
| 484 | // We know the string is marked because it's a strongly-interned string that |
| 485 | // is always alive. The IsMarked implementation of the CMS collector returns |
| 486 | // null for newly allocated objects, but we know those haven't moved. Therefore, |
| 487 | // only update the entry if we get a different non-null string. |
| 488 | // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method |
| 489 | // out of the weak access/creation pause. b/32167580 |
| 490 | if (new_object != nullptr && new_object != object) { |
| 491 | DCHECK(new_object->IsString()); |
| 492 | roots[i] = GcRoot<mirror::Object>(new_object); |
| 493 | } |
| 494 | } else { |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 495 | ProcessWeakClass( |
| 496 | reinterpret_cast<GcRoot<mirror::Class>*>(&roots[i]), visitor, weak_sentinel); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | } |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 500 | // Walk over inline caches to clear entries containing unloaded classes. |
| 501 | for (ProfilingInfo* info : profiling_infos_) { |
| 502 | for (size_t i = 0; i < info->number_of_inline_caches_; ++i) { |
| 503 | InlineCache* cache = &info->cache_[i]; |
| 504 | for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) { |
Nicolas Geoffray | 6ca115b | 2017-05-10 15:09:35 +0100 | [diff] [blame] | 505 | ProcessWeakClass(&cache->classes_[j], visitor, nullptr); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Orion Hodson | 607624f | 2018-05-11 10:10:46 +0100 | [diff] [blame] | 511 | void JitCodeCache::FreeCodeAndData(const void* code_ptr) { |
Nicolas Geoffray | ae982f9 | 2018-12-08 12:31:10 +0000 | [diff] [blame] | 512 | if (IsInZygoteExecSpace(code_ptr)) { |
| 513 | // No need to free, this is shared memory. |
| 514 | return; |
| 515 | } |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 516 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 517 | // Notify native debugger that we are about to remove the code. |
| 518 | // It does nothing if we are not using native debugger. |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 519 | RemoveNativeDebugInfoForJit(Thread::Current(), code_ptr); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 520 | if (OatQuickMethodHeader::FromCodePointer(code_ptr)->IsOptimized()) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 521 | private_region_.FreeData(GetRootTable(code_ptr)); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 522 | } // else this is a JNI stub without any data. |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 523 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 524 | private_region_.FreeCode(reinterpret_cast<uint8_t*>(allocation)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 527 | void JitCodeCache::FreeAllMethodHeaders( |
| 528 | const std::unordered_set<OatQuickMethodHeader*>& method_headers) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 529 | // We need to remove entries in method_headers from CHA dependencies |
| 530 | // first since once we do FreeCode() below, the memory can be reused |
| 531 | // so it's possible for the same method_header to start representing |
| 532 | // different compile code. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 533 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 534 | { |
| 535 | MutexLock mu2(Thread::Current(), *Locks::cha_lock_); |
| 536 | Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis() |
| 537 | ->RemoveDependentsWithMethodHeaders(method_headers); |
| 538 | } |
| 539 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 540 | ScopedCodeCacheWrite scc(private_region_); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 541 | for (const OatQuickMethodHeader* method_header : method_headers) { |
Orion Hodson | 607624f | 2018-05-11 10:10:46 +0100 | [diff] [blame] | 542 | FreeCodeAndData(method_header->GetCode()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 546 | void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 547 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 548 | // We use a set to first collect all method_headers whose code need to be |
| 549 | // removed. We need to free the underlying code after we remove CHA dependencies |
| 550 | // for entries in this set. And it's more efficient to iterate through |
| 551 | // the CHA dependency map just once with an unordered_set. |
| 552 | std::unordered_set<OatQuickMethodHeader*> method_headers; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 553 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 554 | MutexLock mu(self, *Locks::jit_lock_); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 555 | // We do not check if a code cache GC is in progress, as this method comes |
| 556 | // with the classlinker_classes_lock_ held, and suspending ourselves could |
| 557 | // lead to a deadlock. |
| 558 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 559 | ScopedCodeCacheWrite scc(private_region_); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 560 | for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) { |
| 561 | it->second.RemoveMethodsIn(alloc); |
| 562 | if (it->second.GetMethods().empty()) { |
| 563 | method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->second.GetCode())); |
| 564 | it = jni_stubs_map_.erase(it); |
| 565 | } else { |
| 566 | it->first.UpdateShorty(it->second.GetMethods().front()); |
| 567 | ++it; |
| 568 | } |
| 569 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 570 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 571 | if (alloc.ContainsUnsafe(it->second)) { |
| 572 | method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first)); |
| 573 | it = method_code_map_.erase(it); |
| 574 | } else { |
| 575 | ++it; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) { |
| 580 | if (alloc.ContainsUnsafe(it->first)) { |
| 581 | // Note that the code has already been pushed to method_headers in the loop |
| 582 | // above and is going to be removed in FreeCode() below. |
| 583 | it = osr_code_map_.erase(it); |
| 584 | } else { |
| 585 | ++it; |
| 586 | } |
| 587 | } |
| 588 | for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) { |
| 589 | ProfilingInfo* info = *it; |
| 590 | if (alloc.ContainsUnsafe(info->GetMethod())) { |
| 591 | info->GetMethod()->SetProfilingInfo(nullptr); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 592 | private_region_.FreeData(reinterpret_cast<uint8_t*>(info)); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 593 | it = profiling_infos_.erase(it); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 594 | } else { |
| 595 | ++it; |
| 596 | } |
| 597 | } |
| 598 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 599 | FreeAllMethodHeaders(method_headers); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 602 | bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const { |
| 603 | return kUseReadBarrier |
| 604 | ? self->GetWeakRefAccessEnabled() |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 605 | : is_weak_access_enabled_.load(std::memory_order_seq_cst); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) { |
| 609 | if (IsWeakAccessEnabled(self)) { |
| 610 | return; |
| 611 | } |
| 612 | ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 613 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 614 | while (!IsWeakAccessEnabled(self)) { |
| 615 | inline_cache_cond_.Wait(self); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void JitCodeCache::BroadcastForInlineCacheAccess() { |
| 620 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 621 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 622 | inline_cache_cond_.Broadcast(self); |
| 623 | } |
| 624 | |
| 625 | void JitCodeCache::AllowInlineCacheAccess() { |
| 626 | DCHECK(!kUseReadBarrier); |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 627 | is_weak_access_enabled_.store(true, std::memory_order_seq_cst); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 628 | BroadcastForInlineCacheAccess(); |
| 629 | } |
| 630 | |
| 631 | void JitCodeCache::DisallowInlineCacheAccess() { |
| 632 | DCHECK(!kUseReadBarrier); |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 633 | is_weak_access_enabled_.store(false, std::memory_order_seq_cst); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic, |
| 637 | Handle<mirror::ObjectArray<mirror::Class>> array) { |
| 638 | WaitUntilInlineCacheAccessible(Thread::Current()); |
| 639 | // Note that we don't need to lock `lock_` here, the compiler calling |
| 640 | // this method has already ensured the inline cache will not be deleted. |
| 641 | for (size_t in_cache = 0, in_array = 0; |
| 642 | in_cache < InlineCache::kIndividualCacheSize; |
| 643 | ++in_cache) { |
| 644 | mirror::Class* object = ic.classes_[in_cache].Read(); |
| 645 | if (object != nullptr) { |
| 646 | array->Set(in_array++, object); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 651 | static void ClearMethodCounter(ArtMethod* method, bool was_warm) |
| 652 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | f044c22 | 2017-05-31 15:27:54 -0700 | [diff] [blame] | 653 | if (was_warm) { |
Vladimir Marko | c945e0d | 2018-07-18 17:26:45 +0100 | [diff] [blame] | 654 | method->SetPreviouslyWarm(); |
Mathieu Chartier | f044c22 | 2017-05-31 15:27:54 -0700 | [diff] [blame] | 655 | } |
| 656 | // We reset the counter to 1 so that the profile knows that the method was executed at least once. |
| 657 | // This is required for layout purposes. |
Nicolas Geoffray | 88f50b1 | 2017-06-09 16:08:47 +0100 | [diff] [blame] | 658 | // We also need to make sure we'll pass the warmup threshold again, so we set to 0 if |
| 659 | // the warmup threshold is 1. |
| 660 | uint16_t jit_warmup_threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold(); |
| 661 | method->SetCounter(std::min(jit_warmup_threshold - 1, 1)); |
Mathieu Chartier | f044c22 | 2017-05-31 15:27:54 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 664 | void JitCodeCache::WaitForPotentialCollectionToCompleteRunnable(Thread* self) { |
| 665 | while (collection_in_progress_) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 666 | Locks::jit_lock_->Unlock(self); |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 667 | { |
| 668 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 669 | MutexLock mu(self, *Locks::jit_lock_); |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 670 | WaitForPotentialCollectionToComplete(self); |
| 671 | } |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 672 | Locks::jit_lock_->Lock(self); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 676 | uint8_t* JitCodeCache::CommitCodeInternal(Thread* self, |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 677 | JitMemoryRegion* region, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 678 | ArtMethod* method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 679 | uint8_t* stack_map, |
| 680 | uint8_t* roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 681 | const uint8_t* code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 682 | size_t code_size, |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 683 | size_t data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 684 | bool osr, |
Vladimir Marko | ac3ac68 | 2018-09-20 11:01:43 +0100 | [diff] [blame] | 685 | const std::vector<Handle<mirror::Object>>& roots, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 686 | bool has_should_deoptimize_flag, |
| 687 | const ArenaSet<ArtMethod*>& |
| 688 | cha_single_implementation_list) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 689 | DCHECK(!method->IsNative() || !osr); |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 690 | |
| 691 | if (!method->IsNative()) { |
| 692 | // We need to do this before grabbing the lock_ because it needs to be able to see the string |
| 693 | // InternTable. Native methods do not have roots. |
| 694 | DCheckRootsAreValid(roots); |
| 695 | } |
| 696 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 697 | OatQuickMethodHeader* method_header = nullptr; |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 698 | uint8_t* code_ptr = nullptr; |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 699 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 700 | MutexLock mu(self, *Locks::jit_lock_); |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 701 | // We need to make sure that there will be no jit-gcs going on and wait for any ongoing one to |
| 702 | // finish. |
| 703 | WaitForPotentialCollectionToCompleteRunnable(self); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 704 | { |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 705 | ScopedCodeCacheWrite scc(*region); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 706 | |
| 707 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 708 | // Ensure the header ends up at expected instruction alignment. |
| 709 | size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 710 | size_t total_size = header_size + code_size; |
| 711 | |
| 712 | // AllocateCode allocates memory in non-executable region for alignment header and code. The |
| 713 | // header size may include alignment padding. |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 714 | uint8_t* nox_memory = region->AllocateCode(total_size); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 715 | if (nox_memory == nullptr) { |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 716 | return nullptr; |
| 717 | } |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 718 | |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 719 | // code_ptr points to non-executable code. |
| 720 | code_ptr = nox_memory + header_size; |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 721 | std::copy(code, code + code_size, code_ptr); |
| 722 | method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 723 | |
| 724 | // From here code_ptr points to executable code. |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 725 | code_ptr = region->GetExecutableAddress(code_ptr); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 726 | |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 727 | new (method_header) OatQuickMethodHeader( |
Nicolas Geoffray | 5708376 | 2019-03-05 09:24:45 +0000 | [diff] [blame] | 728 | (stack_map != nullptr) ? code_ptr - stack_map : 0u, |
| 729 | code_size); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 730 | |
| 731 | DCHECK(!Runtime::Current()->IsAotCompiler()); |
| 732 | if (has_should_deoptimize_flag) { |
| 733 | method_header->SetHasShouldDeoptimizeFlag(); |
| 734 | } |
| 735 | |
| 736 | // Update method_header pointer to executable code region. |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 737 | method_header = region->GetExecutableAddress(method_header); |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 738 | |
| 739 | // Both instruction and data caches need flushing to the point of unification where both share |
| 740 | // a common view of memory. Flushing the data cache ensures the dirty cachelines from the |
| 741 | // newly added code are written out to the point of unification. Flushing the instruction |
| 742 | // cache ensures the newly written code will be fetched from the point of unification before |
| 743 | // use. Memory in the code cache is re-cycled as code is added and removed. The flushes |
| 744 | // prevent stale code from residing in the instruction cache. |
| 745 | // |
| 746 | // Caches are flushed before write permission is removed because some ARMv8 Qualcomm kernels |
| 747 | // may trigger a segfault if a page fault occurs when requesting a cache maintenance |
| 748 | // operation. This is a kernel bug that we need to work around until affected devices |
| 749 | // (e.g. Nexus 5X and 6P) stop being supported or their kernels are fixed. |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 750 | // |
| 751 | // For reference, this behavior is caused by this commit: |
| 752 | // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 753 | // |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 754 | if (region->HasDualCodeMapping()) { |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 755 | // Flush the data cache lines associated with the non-executable copy of the code just added. |
| 756 | FlushDataCache(nox_memory, nox_memory + total_size); |
| 757 | } |
| 758 | // FlushInstructionCache() flushes both data and instruction caches lines. The cacheline range |
| 759 | // flushed is for the executable mapping of the code just added. |
Orion Hodson | df1ab20 | 2019-06-02 16:45:03 +0100 | [diff] [blame] | 760 | uint8_t* x_memory = reinterpret_cast<uint8_t*>(method_header); |
| 761 | FlushInstructionCache(x_memory, x_memory + total_size); |
Orion Hodson | f233136 | 2018-07-11 15:14:10 +0100 | [diff] [blame] | 762 | |
| 763 | // Ensure CPU instruction pipelines are flushed for all cores. This is necessary for |
| 764 | // correctness as code may still be in instruction pipelines despite the i-cache flush. It is |
| 765 | // not safe to assume that changing permissions with mprotect (RX->RWX->RX) will cause a TLB |
| 766 | // shootdown (incidentally invalidating the CPU pipelines by sending an IPI to all cores to |
| 767 | // notify them of the TLB invalidation). Some architectures, notably ARM and ARM64, have |
| 768 | // hardware support that broadcasts TLB invalidations and so their kernels have no software |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 769 | // based TLB shootdown. The sync-core flavor of membarrier was introduced in Linux 4.16 to |
| 770 | // address this (see mbarrier(2)). The membarrier here will fail on prior kernels and on |
| 771 | // platforms lacking the appropriate support. |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 772 | art::membarrier(art::MembarrierCommand::kPrivateExpeditedSyncCore); |
Orion Hodson | 38d29fd | 2018-09-07 12:58:37 +0100 | [diff] [blame] | 773 | |
Nicolas Geoffray | 0a52223 | 2016-01-19 09:34:58 +0000 | [diff] [blame] | 774 | number_of_compilations_++; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 775 | } |
Orion Hodson | 1d3fd08 | 2018-09-28 09:38:35 +0100 | [diff] [blame] | 776 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 777 | // We need to update the entry point in the runnable state for the instrumentation. |
| 778 | { |
Alex Light | 33b7b5d | 2018-08-07 19:13:51 +0000 | [diff] [blame] | 779 | // The following needs to be guarded by cha_lock_ also. Otherwise it's possible that the |
| 780 | // compiled code is considered invalidated by some class linking, but below we still make the |
| 781 | // compiled code valid for the method. Need cha_lock_ for checking all single-implementation |
| 782 | // flags and register dependencies. |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 783 | MutexLock cha_mu(self, *Locks::cha_lock_); |
| 784 | bool single_impl_still_valid = true; |
| 785 | for (ArtMethod* single_impl : cha_single_implementation_list) { |
| 786 | if (!single_impl->HasSingleImplementation()) { |
Jeff Hao | 00286db | 2017-05-30 16:53:07 -0700 | [diff] [blame] | 787 | // Simply discard the compiled code. Clear the counter so that it may be recompiled later. |
| 788 | // Hopefully the class hierarchy will be more stable when compilation is retried. |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 789 | single_impl_still_valid = false; |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 790 | ClearMethodCounter(method, /*was_warm=*/ false); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Discard the code if any single-implementation assumptions are now invalid. |
| 796 | if (!single_impl_still_valid) { |
| 797 | VLOG(jit) << "JIT discarded jitted code due to invalid single-implementation assumptions."; |
| 798 | return nullptr; |
| 799 | } |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 800 | DCHECK(cha_single_implementation_list.empty() || !Runtime::Current()->IsJavaDebuggable()) |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 801 | << "Should not be using cha on debuggable apps/runs!"; |
| 802 | |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 803 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 804 | for (ArtMethod* single_impl : cha_single_implementation_list) { |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 805 | class_linker->GetClassHierarchyAnalysis()->AddDependency(single_impl, method, method_header); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 808 | if (UNLIKELY(method->IsNative())) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 809 | auto it = jni_stubs_map_.find(JniStubKey(method)); |
| 810 | DCHECK(it != jni_stubs_map_.end()) |
| 811 | << "Entry inserted in NotifyCompilationOf() should be alive."; |
| 812 | JniStubData* data = &it->second; |
| 813 | DCHECK(ContainsElement(data->GetMethods(), method)) |
| 814 | << "Entry inserted in NotifyCompilationOf() should contain this method."; |
| 815 | data->SetCode(code_ptr); |
| 816 | instrumentation::Instrumentation* instrum = Runtime::Current()->GetInstrumentation(); |
| 817 | for (ArtMethod* m : data->GetMethods()) { |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 818 | if (!class_linker->IsQuickResolutionStub(m->GetEntryPointFromQuickCompiledCode())) { |
| 819 | instrum->UpdateMethodsCode(m, method_header->GetEntryPoint()); |
| 820 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 821 | } |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 822 | } else { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 823 | // Fill the root table before updating the entry point. |
| 824 | DCHECK_EQ(FromStackMapToRoots(stack_map), roots_data); |
| 825 | DCHECK_LE(roots_data, stack_map); |
| 826 | FillRootTable(roots_data, roots); |
| 827 | { |
| 828 | // Flush data cache, as compiled code references literals in it. |
Orion Hodson | 38d29fd | 2018-09-07 12:58:37 +0100 | [diff] [blame] | 829 | FlushDataCache(roots_data, roots_data + data_size); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 830 | } |
| 831 | method_code_map_.Put(code_ptr, method); |
| 832 | if (osr) { |
| 833 | number_of_osr_compilations_++; |
| 834 | osr_code_map_.Put(method, code_ptr); |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 835 | } else if (class_linker->IsQuickResolutionStub( |
| 836 | method->GetEntryPointFromQuickCompiledCode())) { |
| 837 | // This situation currently only occurs in the jit-zygote mode. |
| 838 | DCHECK(Runtime::Current()->IsZygote()); |
David Srbecky | 3db3d37 | 2019-04-17 18:19:17 +0100 | [diff] [blame] | 839 | DCHECK(Runtime::Current()->IsUsingApexBootImageLocation()); |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 840 | DCHECK(method->GetDeclaringClass()->GetClassLoader() == nullptr); |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 841 | // TODO(ngeoffray): In most cases, the zygote will not have a profiling |
| 842 | // info for a compiled method. Use a map instead. |
| 843 | if (method->GetProfilingInfo(kRuntimePointerSize) != nullptr) { |
| 844 | // Save the entrypoint, so it can be fetched later once the class is |
| 845 | // initialized. |
| 846 | method->GetProfilingInfo(kRuntimePointerSize)->SetSavedEntryPoint( |
| 847 | method_header->GetEntryPoint()); |
| 848 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 849 | } else { |
| 850 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 851 | method, method_header->GetEntryPoint()); |
| 852 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 853 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 854 | VLOG(jit) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 855 | << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 856 | << ArtMethod::PrettyMethod(method) << "@" << method |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 857 | << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": " |
| 858 | << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": " |
| 859 | << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << "," |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 860 | << reinterpret_cast<const void*>(method_header->GetEntryPoint() + |
| 861 | method_header->GetCodeSize()); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 862 | histogram_code_memory_use_.AddValue(code_size); |
| 863 | if (code_size > kCodeSizeLogThreshold) { |
| 864 | LOG(INFO) << "JIT allocated " |
| 865 | << PrettySize(code_size) |
| 866 | << " for compiled code of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 867 | << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 868 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 869 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 870 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 871 | return reinterpret_cast<uint8_t*>(method_header); |
| 872 | } |
| 873 | |
| 874 | size_t JitCodeCache::CodeCacheSize() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 875 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 876 | return CodeCacheSizeLocked(); |
| 877 | } |
| 878 | |
Orion Hodson | eced692 | 2017-06-01 10:54:28 +0100 | [diff] [blame] | 879 | bool JitCodeCache::RemoveMethod(ArtMethod* method, bool release_memory) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 880 | // This function is used only for testing and only with non-native methods. |
| 881 | CHECK(!method->IsNative()); |
| 882 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 883 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Orion Hodson | eced692 | 2017-06-01 10:54:28 +0100 | [diff] [blame] | 884 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 885 | bool osr = osr_code_map_.find(method) != osr_code_map_.end(); |
| 886 | bool in_cache = RemoveMethodLocked(method, release_memory); |
Orion Hodson | eced692 | 2017-06-01 10:54:28 +0100 | [diff] [blame] | 887 | |
| 888 | if (!in_cache) { |
| 889 | return false; |
| 890 | } |
| 891 | |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 892 | method->SetCounter(0); |
Orion Hodson | eced692 | 2017-06-01 10:54:28 +0100 | [diff] [blame] | 893 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 894 | method, GetQuickToInterpreterBridge()); |
| 895 | VLOG(jit) |
| 896 | << "JIT removed (osr=" << std::boolalpha << osr << std::noboolalpha << ") " |
| 897 | << ArtMethod::PrettyMethod(method) << "@" << method |
| 898 | << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": " |
| 899 | << " dcache_size=" << PrettySize(DataCacheSizeLocked()); |
| 900 | return true; |
| 901 | } |
| 902 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 903 | bool JitCodeCache::RemoveMethodLocked(ArtMethod* method, bool release_memory) { |
| 904 | if (LIKELY(!method->IsNative())) { |
| 905 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
| 906 | if (info != nullptr) { |
| 907 | RemoveElement(profiling_infos_, info); |
| 908 | } |
| 909 | method->SetProfilingInfo(nullptr); |
| 910 | } |
| 911 | |
| 912 | bool in_cache = false; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 913 | ScopedCodeCacheWrite ccw(private_region_); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 914 | if (UNLIKELY(method->IsNative())) { |
| 915 | auto it = jni_stubs_map_.find(JniStubKey(method)); |
| 916 | if (it != jni_stubs_map_.end() && it->second.RemoveMethod(method)) { |
| 917 | in_cache = true; |
| 918 | if (it->second.GetMethods().empty()) { |
| 919 | if (release_memory) { |
Orion Hodson | 607624f | 2018-05-11 10:10:46 +0100 | [diff] [blame] | 920 | FreeCodeAndData(it->second.GetCode()); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 921 | } |
| 922 | jni_stubs_map_.erase(it); |
| 923 | } else { |
| 924 | it->first.UpdateShorty(it->second.GetMethods().front()); |
| 925 | } |
| 926 | } |
| 927 | } else { |
| 928 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 929 | if (it->second == method) { |
| 930 | in_cache = true; |
| 931 | if (release_memory) { |
Orion Hodson | 607624f | 2018-05-11 10:10:46 +0100 | [diff] [blame] | 932 | FreeCodeAndData(it->first); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 933 | } |
| 934 | it = method_code_map_.erase(it); |
| 935 | } else { |
| 936 | ++it; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | auto osr_it = osr_code_map_.find(method); |
| 941 | if (osr_it != osr_code_map_.end()) { |
| 942 | osr_code_map_.erase(osr_it); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return in_cache; |
| 947 | } |
| 948 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 949 | // This notifies the code cache that the given method has been redefined and that it should remove |
| 950 | // any cached information it has on the method. All threads must be suspended before calling this |
| 951 | // method. The compiled code for the method (if there is any) must not be in any threads call stack. |
| 952 | void JitCodeCache::NotifyMethodRedefined(ArtMethod* method) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 953 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 954 | RemoveMethodLocked(method, /* release_memory= */ true); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | // This invalidates old_method. Once this function returns one can no longer use old_method to |
| 958 | // execute code unless it is fixed up. This fixup will happen later in the process of installing a |
| 959 | // class redefinition. |
| 960 | // TODO We should add some info to ArtMethod to note that 'old_method' has been invalidated and |
| 961 | // shouldn't be used since it is no longer logically in the jit code cache. |
| 962 | // TODO We should add DCHECKS that validate that the JIT is paused when this method is entered. |
| 963 | void JitCodeCache::MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 964 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 965 | if (old_method->IsNative()) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 966 | // Update methods in jni_stubs_map_. |
| 967 | for (auto& entry : jni_stubs_map_) { |
| 968 | JniStubData& data = entry.second; |
| 969 | data.MoveObsoleteMethod(old_method, new_method); |
| 970 | } |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 971 | return; |
| 972 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 973 | // Update ProfilingInfo to the new one and remove it from the old_method. |
| 974 | if (old_method->GetProfilingInfo(kRuntimePointerSize) != nullptr) { |
| 975 | DCHECK_EQ(old_method->GetProfilingInfo(kRuntimePointerSize)->GetMethod(), old_method); |
| 976 | ProfilingInfo* info = old_method->GetProfilingInfo(kRuntimePointerSize); |
| 977 | old_method->SetProfilingInfo(nullptr); |
| 978 | // Since the JIT should be paused and all threads suspended by the time this is called these |
| 979 | // checks should always pass. |
| 980 | DCHECK(!info->IsInUseByCompiler()); |
| 981 | new_method->SetProfilingInfo(info); |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 982 | // Get rid of the old saved entrypoint if it is there. |
| 983 | info->SetSavedEntryPoint(nullptr); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 984 | info->method_ = new_method; |
| 985 | } |
| 986 | // Update method_code_map_ to point to the new method. |
| 987 | for (auto& it : method_code_map_) { |
| 988 | if (it.second == old_method) { |
| 989 | it.second = new_method; |
| 990 | } |
| 991 | } |
| 992 | // Update osr_code_map_ to point to the new method. |
| 993 | auto code_map = osr_code_map_.find(old_method); |
| 994 | if (code_map != osr_code_map_.end()) { |
| 995 | osr_code_map_.Put(new_method, code_map->second); |
| 996 | osr_code_map_.erase(old_method); |
| 997 | } |
| 998 | } |
| 999 | |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1000 | void JitCodeCache::ClearEntryPointsInZygoteExecSpace() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1001 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1002 | // Iterate over profiling infos to know which methods may have been JITted. Note that |
| 1003 | // to be JITted, a method must have a profiling info. |
| 1004 | for (ProfilingInfo* info : profiling_infos_) { |
| 1005 | ArtMethod* method = info->GetMethod(); |
| 1006 | if (IsInZygoteExecSpace(method->GetEntryPointFromQuickCompiledCode())) { |
| 1007 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 1008 | } |
| 1009 | // If zygote does method tracing, or in some configuration where |
| 1010 | // the JIT zygote does GC, we also need to clear the saved entry point |
| 1011 | // in the profiling info. |
| 1012 | if (IsInZygoteExecSpace(info->GetSavedEntryPoint())) { |
| 1013 | info->SetSavedEntryPoint(nullptr); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 1018 | size_t JitCodeCache::CodeCacheSizeLocked() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1019 | return private_region_.GetUsedMemoryForCode(); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | size_t JitCodeCache::DataCacheSize() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1023 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 1024 | return DataCacheSizeLocked(); |
| 1025 | } |
| 1026 | |
| 1027 | size_t JitCodeCache::DataCacheSizeLocked() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1028 | return private_region_.GetUsedMemoryForData(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Nicolas Geoffray | f46501c | 2016-11-22 13:45:36 +0000 | [diff] [blame] | 1031 | void JitCodeCache::ClearData(Thread* self, |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 1032 | JitMemoryRegion* region, |
Nicolas Geoffray | f46501c | 2016-11-22 13:45:36 +0000 | [diff] [blame] | 1033 | uint8_t* stack_map_data, |
| 1034 | uint8_t* roots_data) { |
| 1035 | DCHECK_EQ(FromStackMapToRoots(stack_map_data), roots_data); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1036 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 1037 | region->FreeData(reinterpret_cast<uint8_t*>(roots_data)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 1040 | size_t JitCodeCache::ReserveData(Thread* self, |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 1041 | JitMemoryRegion* region, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 1042 | size_t stack_map_size, |
| 1043 | size_t number_of_roots, |
| 1044 | ArtMethod* method, |
| 1045 | uint8_t** stack_map_data, |
| 1046 | uint8_t** roots_data) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1047 | size_t table_size = ComputeRootTableSize(number_of_roots); |
David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 1048 | size_t size = RoundUp(stack_map_size + table_size, sizeof(void*)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1049 | uint8_t* result = nullptr; |
| 1050 | |
| 1051 | { |
| 1052 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1053 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1054 | WaitForPotentialCollectionToComplete(self); |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 1055 | result = region->AllocateData(size); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | if (result == nullptr) { |
| 1059 | // Retry. |
| 1060 | GarbageCollectCache(self); |
| 1061 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1062 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1063 | WaitForPotentialCollectionToComplete(self); |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame^] | 1064 | result = region->AllocateData(size); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1067 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1068 | histogram_stack_map_memory_use_.AddValue(size); |
| 1069 | if (size > kStackMapSizeLogThreshold) { |
| 1070 | LOG(INFO) << "JIT allocated " |
| 1071 | << PrettySize(size) |
| 1072 | << " for stack maps of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1073 | << ArtMethod::PrettyMethod(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1074 | } |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 1075 | if (result != nullptr) { |
| 1076 | *roots_data = result; |
| 1077 | *stack_map_data = result + table_size; |
| 1078 | FillRootTableLength(*roots_data, number_of_roots); |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 1079 | return size; |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 1080 | } else { |
| 1081 | *roots_data = nullptr; |
| 1082 | *stack_map_data = nullptr; |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 1083 | return 0; |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 1084 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1087 | class MarkCodeClosure final : public Closure { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1088 | public: |
Nicolas Geoffray | b9f1af5 | 2018-11-16 10:30:29 +0000 | [diff] [blame] | 1089 | MarkCodeClosure(JitCodeCache* code_cache, CodeCacheBitmap* bitmap, Barrier* barrier) |
| 1090 | : code_cache_(code_cache), bitmap_(bitmap), barrier_(barrier) {} |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1091 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1092 | void Run(Thread* thread) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1093 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1094 | DCHECK(thread == Thread::Current() || thread->IsSuspended()); |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 1095 | StackVisitor::WalkStack( |
| 1096 | [&](const art::StackVisitor* stack_visitor) { |
| 1097 | const OatQuickMethodHeader* method_header = |
| 1098 | stack_visitor->GetCurrentOatQuickMethodHeader(); |
| 1099 | if (method_header == nullptr) { |
| 1100 | return true; |
| 1101 | } |
| 1102 | const void* code = method_header->GetCode(); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1103 | if (code_cache_->ContainsPc(code) && !code_cache_->IsInZygoteExecSpace(code)) { |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 1104 | // Use the atomic set version, as multiple threads are executing this code. |
| 1105 | bitmap_->AtomicTestAndSet(FromCodeToAllocation(code)); |
| 1106 | } |
| 1107 | return true; |
| 1108 | }, |
| 1109 | thread, |
| 1110 | /* context= */ nullptr, |
| 1111 | art::StackVisitor::StackWalkKind::kSkipInlinedFrames); |
| 1112 | |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 1113 | if (kIsDebugBuild) { |
| 1114 | // The stack walking code queries the side instrumentation stack if it |
| 1115 | // sees an instrumentation exit pc, so the JIT code of methods in that stack |
| 1116 | // must have been seen. We sanity check this below. |
| 1117 | for (const instrumentation::InstrumentationStackFrame& frame |
| 1118 | : *thread->GetInstrumentationStack()) { |
| 1119 | // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in |
| 1120 | // its stack frame, it is not the method owning return_pc_. We just pass null to |
| 1121 | // LookupMethodHeader: the method is only checked against in debug builds. |
| 1122 | OatQuickMethodHeader* method_header = |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1123 | code_cache_->LookupMethodHeader(frame.return_pc_, /* method= */ nullptr); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 1124 | if (method_header != nullptr) { |
| 1125 | const void* code = method_header->GetCode(); |
Nicolas Geoffray | b9f1af5 | 2018-11-16 10:30:29 +0000 | [diff] [blame] | 1126 | CHECK(bitmap_->Test(FromCodeToAllocation(code))); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 1130 | barrier_->Pass(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1131 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1132 | |
| 1133 | private: |
| 1134 | JitCodeCache* const code_cache_; |
Nicolas Geoffray | b9f1af5 | 2018-11-16 10:30:29 +0000 | [diff] [blame] | 1135 | CodeCacheBitmap* const bitmap_; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1136 | Barrier* const barrier_; |
| 1137 | }; |
| 1138 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1139 | void JitCodeCache::NotifyCollectionDone(Thread* self) { |
| 1140 | collection_in_progress_ = false; |
| 1141 | lock_cond_.Broadcast(self); |
| 1142 | } |
| 1143 | |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 1144 | void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) { |
| 1145 | Barrier barrier(0); |
| 1146 | size_t threads_running_checkpoint = 0; |
Nicolas Geoffray | b9f1af5 | 2018-11-16 10:30:29 +0000 | [diff] [blame] | 1147 | MarkCodeClosure closure(this, GetLiveBitmap(), &barrier); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 1148 | threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure); |
| 1149 | // Now that we have run our checkpoint, move to a suspended state and wait |
| 1150 | // for other threads to run the checkpoint. |
| 1151 | ScopedThreadSuspension sts(self, kSuspended); |
| 1152 | if (threads_running_checkpoint != 0) { |
| 1153 | barrier.Increment(self, threads_running_checkpoint); |
| 1154 | } |
| 1155 | } |
| 1156 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1157 | bool JitCodeCache::ShouldDoFullCollection() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1158 | if (private_region_.GetCurrentCapacity() == private_region_.GetMaxCapacity()) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1159 | // Always do a full collection when the code cache is full. |
| 1160 | return true; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1161 | } else if (private_region_.GetCurrentCapacity() < kReservedCapacity) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1162 | // Always do partial collection when the code cache size is below the reserved |
| 1163 | // capacity. |
| 1164 | return false; |
| 1165 | } else if (last_collection_increased_code_cache_) { |
| 1166 | // This time do a full collection. |
| 1167 | return true; |
| 1168 | } else { |
| 1169 | // This time do a partial collection. |
| 1170 | return false; |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1174 | void JitCodeCache::GarbageCollectCache(Thread* self) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1175 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 1176 | // Wait for an existing collection, or let everyone know we are starting one. |
| 1177 | { |
| 1178 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1179 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1180 | if (!garbage_collect_code_) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1181 | private_region_.IncreaseCodeCacheCapacity(); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1182 | return; |
| 1183 | } else if (WaitForPotentialCollectionToComplete(self)) { |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } else { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1186 | number_of_collections_++; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1187 | live_bitmap_.reset(CodeCacheBitmap::Create( |
| 1188 | "code-cache-bitmap", |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1189 | reinterpret_cast<uintptr_t>(private_region_.GetExecPages()->Begin()), |
| 1190 | reinterpret_cast<uintptr_t>( |
| 1191 | private_region_.GetExecPages()->Begin() + private_region_.GetCurrentCapacity() / 2))); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 1192 | collection_in_progress_ = true; |
| 1193 | } |
| 1194 | } |
| 1195 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1196 | TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit)); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 1197 | { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1198 | TimingLogger::ScopedTiming st("Code cache collection", &logger); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1199 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1200 | bool do_full_collection = false; |
| 1201 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1202 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1203 | do_full_collection = ShouldDoFullCollection(); |
Nicolas Geoffray | a96917a | 2016-03-01 22:18:02 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Nicolas Geoffray | 646d638 | 2017-08-09 10:50:00 +0100 | [diff] [blame] | 1206 | VLOG(jit) << "Do " |
| 1207 | << (do_full_collection ? "full" : "partial") |
| 1208 | << " code cache collection, code=" |
| 1209 | << PrettySize(CodeCacheSize()) |
| 1210 | << ", data=" << PrettySize(DataCacheSize()); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1211 | |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1212 | DoCollection(self, /* collect_profiling_info= */ do_full_collection); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1213 | |
Nicolas Geoffray | 646d638 | 2017-08-09 10:50:00 +0100 | [diff] [blame] | 1214 | VLOG(jit) << "After code cache collection, code=" |
| 1215 | << PrettySize(CodeCacheSize()) |
| 1216 | << ", data=" << PrettySize(DataCacheSize()); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1217 | |
| 1218 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1219 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Increase the code cache only when we do partial collections. |
| 1222 | // TODO: base this strategy on how full the code cache is? |
| 1223 | if (do_full_collection) { |
| 1224 | last_collection_increased_code_cache_ = false; |
| 1225 | } else { |
| 1226 | last_collection_increased_code_cache_ = true; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1227 | private_region_.IncreaseCodeCacheCapacity(); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1230 | bool next_collection_will_be_full = ShouldDoFullCollection(); |
| 1231 | |
| 1232 | // Start polling the liveness of compiled code to prepare for the next full collection. |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 1233 | if (next_collection_will_be_full) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1234 | // Save the entry point of methods we have compiled, and update the entry |
| 1235 | // point of those methods to the interpreter. If the method is invoked, the |
| 1236 | // interpreter will update its entry point to the compiled code and call it. |
| 1237 | for (ProfilingInfo* info : profiling_infos_) { |
| 1238 | const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1239 | if (!IsInZygoteDataSpace(info) && ContainsPc(entry_point)) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1240 | info->SetSavedEntryPoint(entry_point); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1241 | // Don't call Instrumentation::UpdateMethodsCode(), as it can check the declaring |
Nicolas Geoffray | 3b1a7f4 | 2017-02-22 10:21:00 +0000 | [diff] [blame] | 1242 | // class of the method. We may be concurrently running a GC which makes accessing |
| 1243 | // the class unsafe. We know it is OK to bypass the instrumentation as we've just |
| 1244 | // checked that the current entry point is JIT compiled code. |
| 1245 | info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1249 | // Change entry points of native methods back to the GenericJNI entrypoint. |
| 1250 | for (const auto& entry : jni_stubs_map_) { |
| 1251 | const JniStubData& data = entry.second; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1252 | if (!data.IsCompiled() || IsInZygoteExecSpace(data.GetCode())) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1253 | continue; |
| 1254 | } |
| 1255 | // Make sure a single invocation of the GenericJNI trampoline tries to recompile. |
| 1256 | uint16_t new_counter = Runtime::Current()->GetJit()->HotMethodThreshold() - 1u; |
| 1257 | const OatQuickMethodHeader* method_header = |
| 1258 | OatQuickMethodHeader::FromCodePointer(data.GetCode()); |
| 1259 | for (ArtMethod* method : data.GetMethods()) { |
| 1260 | if (method->GetEntryPointFromQuickCompiledCode() == method_header->GetEntryPoint()) { |
| 1261 | // Don't call Instrumentation::UpdateMethodsCode(), same as for normal methods above. |
| 1262 | method->SetCounter(new_counter); |
| 1263 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
| 1264 | } |
| 1265 | } |
| 1266 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1267 | } |
| 1268 | live_bitmap_.reset(nullptr); |
| 1269 | NotifyCollectionDone(self); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1270 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1271 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1272 | Runtime::Current()->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1275 | void JitCodeCache::RemoveUnmarkedCode(Thread* self) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1276 | ScopedTrace trace(__FUNCTION__); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1277 | std::unordered_set<OatQuickMethodHeader*> method_headers; |
| 1278 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1279 | MutexLock mu(self, *Locks::jit_lock_); |
| 1280 | ScopedCodeCacheWrite scc(private_region_); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1281 | // Iterate over all compiled code and remove entries that are not marked. |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1282 | for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) { |
| 1283 | JniStubData* data = &it->second; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1284 | if (IsInZygoteExecSpace(data->GetCode()) || |
| 1285 | !data->IsCompiled() || |
| 1286 | GetLiveBitmap()->Test(FromCodeToAllocation(data->GetCode()))) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1287 | ++it; |
| 1288 | } else { |
| 1289 | method_headers.insert(OatQuickMethodHeader::FromCodePointer(data->GetCode())); |
| 1290 | it = jni_stubs_map_.erase(it); |
| 1291 | } |
| 1292 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1293 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 1294 | const void* code_ptr = it->first; |
| 1295 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1296 | if (IsInZygoteExecSpace(code_ptr) || GetLiveBitmap()->Test(allocation)) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1297 | ++it; |
| 1298 | } else { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 1299 | OatQuickMethodHeader* header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1300 | method_headers.insert(header); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1301 | it = method_code_map_.erase(it); |
| 1302 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1305 | FreeAllMethodHeaders(method_headers); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1308 | bool JitCodeCache::GetGarbageCollectCode() { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1309 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1310 | return garbage_collect_code_; |
| 1311 | } |
| 1312 | |
| 1313 | void JitCodeCache::SetGarbageCollectCode(bool value) { |
| 1314 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1315 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 1316 | if (garbage_collect_code_ != value) { |
| 1317 | if (garbage_collect_code_) { |
| 1318 | // When dynamically disabling the garbage collection, we neee |
| 1319 | // to make sure that a potential current collection is finished, and also |
| 1320 | // clear the saved entry point in profiling infos to avoid dangling pointers. |
| 1321 | WaitForPotentialCollectionToComplete(self); |
| 1322 | for (ProfilingInfo* info : profiling_infos_) { |
| 1323 | info->SetSavedEntryPoint(nullptr); |
| 1324 | } |
| 1325 | } |
| 1326 | // Update the flag while holding the lock to ensure no thread will try to GC. |
| 1327 | garbage_collect_code_ = value; |
| 1328 | } |
| 1329 | } |
| 1330 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1331 | void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1332 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1333 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1334 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1335 | if (collect_profiling_info) { |
| 1336 | // Clear the profiling info of methods that do not have compiled code as entrypoint. |
| 1337 | // Also remove the saved entry point from the ProfilingInfo objects. |
| 1338 | for (ProfilingInfo* info : profiling_infos_) { |
| 1339 | const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1340 | if (!ContainsPc(ptr) && !info->IsInUseByCompiler() && !IsInZygoteDataSpace(info)) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1341 | info->GetMethod()->SetProfilingInfo(nullptr); |
| 1342 | } |
Nicolas Geoffray | b9a639d | 2016-03-22 11:25:20 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (info->GetSavedEntryPoint() != nullptr) { |
| 1345 | info->SetSavedEntryPoint(nullptr); |
| 1346 | // We are going to move this method back to interpreter. Clear the counter now to |
Mathieu Chartier | f044c22 | 2017-05-31 15:27:54 -0700 | [diff] [blame] | 1347 | // give it a chance to be hot again. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1348 | ClearMethodCounter(info->GetMethod(), /*was_warm=*/ true); |
Nicolas Geoffray | b9a639d | 2016-03-22 11:25:20 +0000 | [diff] [blame] | 1349 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1350 | } |
| 1351 | } else if (kIsDebugBuild) { |
| 1352 | // Sanity check that the profiling infos do not have a dangling entry point. |
| 1353 | for (ProfilingInfo* info : profiling_infos_) { |
David Srbecky | 605a5fe | 2019-04-24 14:05:21 +0100 | [diff] [blame] | 1354 | DCHECK(!Runtime::Current()->IsZygote()); |
| 1355 | const void* entry_point = info->GetSavedEntryPoint(); |
| 1356 | DCHECK(entry_point == nullptr || IsInZygoteExecSpace(entry_point)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1357 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1358 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1359 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1360 | // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not |
| 1361 | // an entry point is either: |
| 1362 | // - an osr compiled code, that will be removed if not in a thread call stack. |
| 1363 | // - discarded compiled code, that will be removed if not in a thread call stack. |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1364 | for (const auto& entry : jni_stubs_map_) { |
| 1365 | const JniStubData& data = entry.second; |
| 1366 | const void* code_ptr = data.GetCode(); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1367 | if (IsInZygoteExecSpace(code_ptr)) { |
| 1368 | continue; |
| 1369 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1370 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1371 | for (ArtMethod* method : data.GetMethods()) { |
| 1372 | if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) { |
| 1373 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1378 | for (const auto& it : method_code_map_) { |
| 1379 | ArtMethod* method = it.second; |
| 1380 | const void* code_ptr = it.first; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1381 | if (IsInZygoteExecSpace(code_ptr)) { |
| 1382 | continue; |
| 1383 | } |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1384 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1385 | if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) { |
| 1386 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 1387 | } |
| 1388 | } |
| 1389 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1390 | // Empty osr method map, as osr compiled code will be deleted (except the ones |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1391 | // on thread stacks). |
| 1392 | osr_code_map_.clear(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | // Run a checkpoint on all threads to mark the JIT compiled code they are running. |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 1396 | MarkCompiledCodeOnThreadStacks(self); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1397 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1398 | // At this point, mutator threads are still running, and entrypoints of methods can |
| 1399 | // change. We do know they cannot change to a code cache entry that is not marked, |
| 1400 | // therefore we can safely remove those entries. |
| 1401 | RemoveUnmarkedCode(self); |
Nicolas Geoffray | a96917a | 2016-03-01 22:18:02 +0000 | [diff] [blame] | 1402 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1403 | if (collect_profiling_info) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1404 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1405 | // Free all profiling infos of methods not compiled nor being compiled. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1406 | auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(), |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 1407 | [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1408 | const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | 511e41b | 2016-03-02 17:09:35 +0000 | [diff] [blame] | 1409 | // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope |
| 1410 | // that the compiled code would not get revived. As mutator threads run concurrently, |
| 1411 | // they may have revived the compiled code, and now we are in the situation where |
| 1412 | // a method has compiled code but no ProfilingInfo. |
| 1413 | // We make sure compiled methods have a ProfilingInfo object. It is needed for |
| 1414 | // code cache collection. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1415 | if (ContainsPc(ptr) && |
| 1416 | info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1417 | info->GetMethod()->SetProfilingInfo(info); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1418 | } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1419 | // No need for this ProfilingInfo object anymore. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1420 | private_region_.FreeData(reinterpret_cast<uint8_t*>(info)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1421 | return true; |
| 1422 | } |
| 1423 | return false; |
| 1424 | }); |
| 1425 | profiling_infos_.erase(profiling_kept_end, profiling_infos_.end()); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1426 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1429 | OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 1430 | static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 1431 | if (kRuntimeISA == InstructionSet::kArm) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1432 | // On Thumb-2, the pc is offset by one. |
| 1433 | --pc; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1434 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1435 | if (!ContainsPc(reinterpret_cast<const void*>(pc))) { |
| 1436 | return nullptr; |
| 1437 | } |
| 1438 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1439 | if (!kIsDebugBuild) { |
| 1440 | // Called with null `method` only from MarkCodeClosure::Run() in debug build. |
| 1441 | CHECK(method != nullptr); |
Vladimir Marko | 47d3185 | 2017-11-28 18:36:12 +0000 | [diff] [blame] | 1442 | } |
Vladimir Marko | e744163 | 2017-11-29 13:00:56 +0000 | [diff] [blame] | 1443 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1444 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1445 | OatQuickMethodHeader* method_header = nullptr; |
| 1446 | ArtMethod* found_method = nullptr; // Only for DCHECK(), not for JNI stubs. |
| 1447 | if (method != nullptr && UNLIKELY(method->IsNative())) { |
| 1448 | auto it = jni_stubs_map_.find(JniStubKey(method)); |
| 1449 | if (it == jni_stubs_map_.end() || !ContainsElement(it->second.GetMethods(), method)) { |
| 1450 | return nullptr; |
| 1451 | } |
| 1452 | const void* code_ptr = it->second.GetCode(); |
| 1453 | method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1454 | if (!method_header->Contains(pc)) { |
| 1455 | return nullptr; |
| 1456 | } |
| 1457 | } else { |
| 1458 | auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc)); |
| 1459 | if (it != method_code_map_.begin()) { |
| 1460 | --it; |
| 1461 | const void* code_ptr = it->first; |
| 1462 | if (OatQuickMethodHeader::FromCodePointer(code_ptr)->Contains(pc)) { |
| 1463 | method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1464 | found_method = it->second; |
| 1465 | } |
| 1466 | } |
| 1467 | if (method_header == nullptr && method == nullptr) { |
| 1468 | // Scan all compiled JNI stubs as well. This slow search is used only |
| 1469 | // for checks in debug build, for release builds the `method` is not null. |
| 1470 | for (auto&& entry : jni_stubs_map_) { |
| 1471 | const JniStubData& data = entry.second; |
| 1472 | if (data.IsCompiled() && |
| 1473 | OatQuickMethodHeader::FromCodePointer(data.GetCode())->Contains(pc)) { |
| 1474 | method_header = OatQuickMethodHeader::FromCodePointer(data.GetCode()); |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | if (method_header == nullptr) { |
| 1479 | return nullptr; |
| 1480 | } |
Nicolas Geoffray | 056d775 | 2017-11-30 09:12:13 +0000 | [diff] [blame] | 1481 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (kIsDebugBuild && method != nullptr && !method->IsNative()) { |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 1484 | DCHECK_EQ(found_method, method) |
| 1485 | << ArtMethod::PrettyMethod(method) << " " |
| 1486 | << ArtMethod::PrettyMethod(found_method) << " " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1487 | << std::hex << pc; |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 1488 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1489 | return method_header; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1492 | OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1493 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1494 | auto it = osr_code_map_.find(method); |
| 1495 | if (it == osr_code_map_.end()) { |
| 1496 | return nullptr; |
| 1497 | } |
| 1498 | return OatQuickMethodHeader::FromCodePointer(it->second); |
| 1499 | } |
| 1500 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1501 | ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self, |
| 1502 | ArtMethod* method, |
| 1503 | const std::vector<uint32_t>& entries, |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1504 | bool retry_allocation) |
| 1505 | // No thread safety analysis as we are using TryLock/Unlock explicitly. |
| 1506 | NO_THREAD_SAFETY_ANALYSIS { |
| 1507 | ProfilingInfo* info = nullptr; |
| 1508 | if (!retry_allocation) { |
| 1509 | // If we are allocating for the interpreter, just try to lock, to avoid |
| 1510 | // lock contention with the JIT. |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1511 | if (Locks::jit_lock_->ExclusiveTryLock(self)) { |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1512 | info = AddProfilingInfoInternal(self, method, entries); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1513 | Locks::jit_lock_->ExclusiveUnlock(self); |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } else { |
| 1516 | { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1517 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1518 | info = AddProfilingInfoInternal(self, method, entries); |
| 1519 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1520 | |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1521 | if (info == nullptr) { |
| 1522 | GarbageCollectCache(self); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1523 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1524 | info = AddProfilingInfoInternal(self, method, entries); |
| 1525 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | return info; |
| 1528 | } |
| 1529 | |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1530 | ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED, |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1531 | ArtMethod* method, |
| 1532 | const std::vector<uint32_t>& entries) { |
| 1533 | size_t profile_info_size = RoundUp( |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1534 | sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(), |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1535 | sizeof(void*)); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1536 | |
| 1537 | // Check whether some other thread has concurrently created it. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1538 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1539 | if (info != nullptr) { |
| 1540 | return info; |
| 1541 | } |
| 1542 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1543 | uint8_t* data = private_region_.AllocateData(profile_info_size); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1544 | if (data == nullptr) { |
| 1545 | return nullptr; |
| 1546 | } |
| 1547 | info = new (data) ProfilingInfo(method, entries); |
Nicolas Geoffray | 07f3564 | 2016-01-04 16:06:51 +0000 | [diff] [blame] | 1548 | |
| 1549 | // Make sure other threads see the data in the profiling info object before the |
| 1550 | // store in the ArtMethod's ProfilingInfo pointer. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 1551 | std::atomic_thread_fence(std::memory_order_release); |
Nicolas Geoffray | 07f3564 | 2016-01-04 16:06:51 +0000 | [diff] [blame] | 1552 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1553 | method->SetProfilingInfo(info); |
| 1554 | profiling_infos_.push_back(info); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1555 | histogram_profiling_info_memory_use_.AddValue(profile_info_size); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1556 | return info; |
| 1557 | } |
| 1558 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1559 | void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) { |
| 1560 | return private_region_.MoreCore(mspace, increment); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 1563 | void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1564 | std::vector<ProfileMethodInfo>& methods) { |
Nicolas Geoffray | 1afdfe6 | 2018-11-21 09:38:10 +0000 | [diff] [blame] | 1565 | Thread* self = Thread::Current(); |
| 1566 | WaitUntilInlineCacheAccessible(self); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1567 | MutexLock mu(self, *Locks::jit_lock_); |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1568 | ScopedTrace trace(__FUNCTION__); |
Calin Juravle | a39fd98 | 2017-05-18 10:15:52 -0700 | [diff] [blame] | 1569 | uint16_t jit_compile_threshold = Runtime::Current()->GetJITOptions()->GetCompileThreshold(); |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 1570 | for (const ProfilingInfo* info : profiling_infos_) { |
| 1571 | ArtMethod* method = info->GetMethod(); |
| 1572 | const DexFile* dex_file = method->GetDexFile(); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 1573 | const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation()); |
| 1574 | if (!ContainsElement(dex_base_locations, base_location)) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1575 | // Skip dex files which are not profiled. |
| 1576 | continue; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1577 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1578 | std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches; |
Calin Juravle | a39fd98 | 2017-05-18 10:15:52 -0700 | [diff] [blame] | 1579 | |
| 1580 | // If the method didn't reach the compilation threshold don't save the inline caches. |
| 1581 | // They might be incomplete and cause unnecessary deoptimizations. |
| 1582 | // If the inline cache is empty the compiler will generate a regular invoke virtual/interface. |
| 1583 | if (method->GetCounter() < jit_compile_threshold) { |
| 1584 | methods.emplace_back(/*ProfileMethodInfo*/ |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1585 | MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches); |
Calin Juravle | a39fd98 | 2017-05-18 10:15:52 -0700 | [diff] [blame] | 1586 | continue; |
| 1587 | } |
| 1588 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1589 | for (size_t i = 0; i < info->number_of_inline_caches_; ++i) { |
Mathieu Chartier | dbddc22 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1590 | std::vector<TypeReference> profile_classes; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1591 | const InlineCache& cache = info->cache_[i]; |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 1592 | ArtMethod* caller = info->GetMethod(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1593 | bool is_missing_types = false; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1594 | for (size_t k = 0; k < InlineCache::kIndividualCacheSize; k++) { |
| 1595 | mirror::Class* cls = cache.classes_[k].Read(); |
| 1596 | if (cls == nullptr) { |
| 1597 | break; |
| 1598 | } |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1599 | |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 1600 | // Check if the receiver is in the boot class path or if it's in the |
| 1601 | // same class loader as the caller. If not, skip it, as there is not |
| 1602 | // much we can do during AOT. |
| 1603 | if (!cls->IsBootStrapClassLoaded() && |
| 1604 | caller->GetClassLoader() != cls->GetClassLoader()) { |
| 1605 | is_missing_types = true; |
| 1606 | continue; |
| 1607 | } |
| 1608 | |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1609 | const DexFile* class_dex_file = nullptr; |
| 1610 | dex::TypeIndex type_index; |
| 1611 | |
| 1612 | if (cls->GetDexCache() == nullptr) { |
| 1613 | DCHECK(cls->IsArrayClass()) << cls->PrettyClass(); |
Calin Juravle | e21806f | 2017-02-22 11:49:43 -0800 | [diff] [blame] | 1614 | // Make a best effort to find the type index in the method's dex file. |
| 1615 | // We could search all open dex files but that might turn expensive |
| 1616 | // and probably not worth it. |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1617 | class_dex_file = dex_file; |
| 1618 | type_index = cls->FindTypeIndexInOtherDexFile(*dex_file); |
| 1619 | } else { |
| 1620 | class_dex_file = &(cls->GetDexFile()); |
| 1621 | type_index = cls->GetDexTypeIndex(); |
| 1622 | } |
| 1623 | if (!type_index.IsValid()) { |
| 1624 | // Could be a proxy class or an array for which we couldn't find the type index. |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1625 | is_missing_types = true; |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1626 | continue; |
| 1627 | } |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 1628 | if (ContainsElement(dex_base_locations, |
| 1629 | DexFileLoader::GetBaseLocation(class_dex_file->GetLocation()))) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1630 | // Only consider classes from the same apk (including multidex). |
| 1631 | profile_classes.emplace_back(/*ProfileMethodInfo::ProfileClassReference*/ |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1632 | class_dex_file, type_index); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1633 | } else { |
| 1634 | is_missing_types = true; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | if (!profile_classes.empty()) { |
| 1638 | inline_caches.emplace_back(/*ProfileMethodInfo::ProfileInlineCache*/ |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1639 | cache.dex_pc_, is_missing_types, profile_classes); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | methods.emplace_back(/*ProfileMethodInfo*/ |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1643 | MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 1647 | bool JitCodeCache::IsOsrCompiled(ArtMethod* method) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1648 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 1649 | return osr_code_map_.find(method) != osr_code_map_.end(); |
| 1650 | } |
| 1651 | |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 1652 | bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr, bool prejit) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1653 | if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1654 | return false; |
| 1655 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1656 | |
Nicolas Geoffray | d03e8dd | 2019-04-10 23:13:20 +0100 | [diff] [blame] | 1657 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1658 | if (class_linker->IsQuickResolutionStub(method->GetEntryPointFromQuickCompiledCode())) { |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 1659 | if (!prejit) { |
| 1660 | // Unless we're pre-jitting, we currently don't save the JIT compiled code if we cannot |
| 1661 | // update the entrypoint due to having the resolution stub. |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 1662 | VLOG(jit) << "Not compiling " |
| 1663 | << method->PrettyMethod() |
| 1664 | << " because it has the resolution stub"; |
| 1665 | // Give it a new chance to be hot. |
| 1666 | ClearMethodCounter(method, /*was_warm=*/ false); |
| 1667 | return false; |
| 1668 | } |
Nicolas Geoffray | d03e8dd | 2019-04-10 23:13:20 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1671 | MutexLock mu(self, *Locks::jit_lock_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1672 | if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) { |
| 1673 | return false; |
| 1674 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1675 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1676 | if (UNLIKELY(method->IsNative())) { |
| 1677 | JniStubKey key(method); |
| 1678 | auto it = jni_stubs_map_.find(key); |
| 1679 | bool new_compilation = false; |
| 1680 | if (it == jni_stubs_map_.end()) { |
| 1681 | // Create a new entry to mark the stub as being compiled. |
| 1682 | it = jni_stubs_map_.Put(key, JniStubData{}); |
| 1683 | new_compilation = true; |
| 1684 | } |
| 1685 | JniStubData* data = &it->second; |
| 1686 | data->AddMethod(method); |
| 1687 | if (data->IsCompiled()) { |
| 1688 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(data->GetCode()); |
| 1689 | const void* entrypoint = method_header->GetEntryPoint(); |
| 1690 | // Update also entrypoints of other methods held by the JniStubData. |
| 1691 | // We could simply update the entrypoint of `method` but if the last JIT GC has |
| 1692 | // changed these entrypoints to GenericJNI in preparation for a full GC, we may |
| 1693 | // as well change them back as this stub shall not be collected anyway and this |
| 1694 | // can avoid a few expensive GenericJNI calls. |
| 1695 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 1696 | for (ArtMethod* m : data->GetMethods()) { |
Nicolas Geoffray | a6e0e7d | 2018-01-26 13:16:50 +0000 | [diff] [blame] | 1697 | // Call the dedicated method instead of the more generic UpdateMethodsCode, because |
| 1698 | // `m` might be in the process of being deleted. |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 1699 | if (!class_linker->IsQuickResolutionStub(m->GetEntryPointFromQuickCompiledCode())) { |
| 1700 | instrumentation->UpdateNativeMethodsCodeToJitCode(m, entrypoint); |
| 1701 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1702 | } |
| 1703 | if (collection_in_progress_) { |
David Srbecky | c45b589 | 2019-04-24 10:32:04 +0100 | [diff] [blame] | 1704 | if (!IsInZygoteExecSpace(data->GetCode())) { |
| 1705 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(data->GetCode())); |
| 1706 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | return new_compilation; |
| 1710 | } else { |
| 1711 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
| 1712 | if (info == nullptr) { |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 1713 | // When prejitting, we don't allocate a profiling info. |
| 1714 | if (!prejit) { |
| 1715 | VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled"; |
| 1716 | // Because the counter is not atomic, there are some rare cases where we may not hit the |
| 1717 | // threshold for creating the ProfilingInfo. Reset the counter now to "correct" this. |
| 1718 | ClearMethodCounter(method, /*was_warm=*/ false); |
| 1719 | return false; |
| 1720 | } |
| 1721 | } else { |
| 1722 | if (info->IsMethodBeingCompiled(osr)) { |
| 1723 | return false; |
| 1724 | } |
| 1725 | info->SetIsMethodBeingCompiled(true, osr); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1726 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1727 | return true; |
| 1728 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1729 | } |
| 1730 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1731 | ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1732 | MutexLock mu(self, *Locks::jit_lock_); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1733 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1734 | if (info != nullptr) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1735 | if (!info->IncrementInlineUse()) { |
| 1736 | // Overflow of inlining uses, just bail. |
| 1737 | return nullptr; |
| 1738 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1739 | } |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1740 | return info; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1743 | void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1744 | MutexLock mu(self, *Locks::jit_lock_); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1745 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1746 | DCHECK(info != nullptr); |
| 1747 | info->DecrementInlineUse(); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1750 | void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self, bool osr) { |
| 1751 | DCHECK_EQ(Thread::Current(), self); |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1752 | MutexLock mu(self, *Locks::jit_lock_); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1753 | if (UNLIKELY(method->IsNative())) { |
| 1754 | auto it = jni_stubs_map_.find(JniStubKey(method)); |
| 1755 | DCHECK(it != jni_stubs_map_.end()); |
| 1756 | JniStubData* data = &it->second; |
| 1757 | DCHECK(ContainsElement(data->GetMethods(), method)); |
| 1758 | if (UNLIKELY(!data->IsCompiled())) { |
| 1759 | // Failed to compile; the JNI compiler never fails, but the cache may be full. |
| 1760 | jni_stubs_map_.erase(it); // Remove the entry added in NotifyCompilationOf(). |
| 1761 | } // else CommitCodeInternal() updated entrypoints of all methods in the JniStubData. |
| 1762 | } else { |
| 1763 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 1764 | if (info != nullptr) { |
| 1765 | DCHECK(info->IsMethodBeingCompiled(osr)); |
| 1766 | info->SetIsMethodBeingCompiled(false, osr); |
| 1767 | } |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1768 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1771 | void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method, |
| 1772 | const OatQuickMethodHeader* header) { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1773 | DCHECK(!method->IsNative()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1774 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 1775 | const void* method_entrypoint = method->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1776 | if ((profiling_info != nullptr) && |
| 1777 | (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 1778 | // When instrumentation is set, the actual entrypoint is the one in the profiling info. |
| 1779 | method_entrypoint = profiling_info->GetSavedEntryPoint(); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1780 | // Prevent future uses of the compiled code. |
| 1781 | profiling_info->SetSavedEntryPoint(nullptr); |
| 1782 | } |
| 1783 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 1784 | // Clear the method counter if we are running jitted code since we might want to jit this again in |
| 1785 | // the future. |
| 1786 | if (method_entrypoint == header->GetEntryPoint()) { |
Jeff Hao | 00286db | 2017-05-30 16:53:07 -0700 | [diff] [blame] | 1787 | // The entrypoint is the one to invalidate, so we just update it to the interpreter entry point |
Mathieu Chartier | f044c22 | 2017-05-31 15:27:54 -0700 | [diff] [blame] | 1788 | // and clear the counter to get the method Jitted again. |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1789 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 1790 | method, GetQuickToInterpreterBridge()); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1791 | ClearMethodCounter(method, /*was_warm=*/ profiling_info != nullptr); |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1792 | } else { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1793 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1794 | auto it = osr_code_map_.find(method); |
| 1795 | if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) { |
| 1796 | // Remove the OSR method, to avoid using it again. |
| 1797 | osr_code_map_.erase(it); |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1802 | void JitCodeCache::Dump(std::ostream& os) { |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1803 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
| 1804 | os << "Current JIT code cache size: " << PrettySize(private_region_.GetUsedMemoryForCode()) |
| 1805 | << "\n" |
| 1806 | << "Current JIT data cache size: " << PrettySize(private_region_.GetUsedMemoryForData()) |
| 1807 | << "\n" |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 1808 | << "Current JIT mini-debug-info size: " << PrettySize(GetJitMiniDebugInfoMemUsage()) << "\n" |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1809 | << "Current JIT capacity: " << PrettySize(private_region_.GetCurrentCapacity()) << "\n" |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 1810 | << "Current number of JIT JNI stub entries: " << jni_stubs_map_.size() << "\n" |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1811 | << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n" |
| 1812 | << "Total number of JIT compilations: " << number_of_compilations_ << "\n" |
| 1813 | << "Total number of JIT compilations for on stack replacement: " |
| 1814 | << number_of_osr_compilations_ << "\n" |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1815 | << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl; |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1816 | histogram_stack_map_memory_use_.PrintMemoryUse(os); |
| 1817 | histogram_code_memory_use_.PrintMemoryUse(os); |
| 1818 | histogram_profiling_info_memory_use_.PrintMemoryUse(os); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 1821 | void JitCodeCache::PostForkChildAction(bool is_system_server, bool is_zygote) { |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 1822 | if (is_zygote) { |
| 1823 | // Don't transition if this is for a child zygote. |
| 1824 | return; |
| 1825 | } |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1826 | MutexLock mu(Thread::Current(), *Locks::jit_lock_); |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 1827 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1828 | shared_region_ = std::move(private_region_); |
| 1829 | |
| 1830 | // Reset all statistics to be specific to this process. |
| 1831 | number_of_compilations_ = 0; |
| 1832 | number_of_osr_compilations_ = 0; |
| 1833 | number_of_collections_ = 0; |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 1834 | |
| 1835 | size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity(); |
| 1836 | size_t max_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheMaxCapacity(); |
| 1837 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1838 | private_region_.InitializeState(initial_capacity, max_capacity); |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 1839 | |
| 1840 | std::string error_msg; |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1841 | if (!private_region_.InitializeMappings( |
| 1842 | /* rwx_memory_allowed= */ !is_system_server, is_zygote, &error_msg)) { |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 1843 | LOG(WARNING) << "Could not reset JIT state after zygote fork: " << error_msg; |
| 1844 | return; |
| 1845 | } |
| 1846 | |
Nicolas Geoffray | 2a905b2 | 2019-06-06 09:04:07 +0100 | [diff] [blame] | 1847 | private_region_.InitializeSpaces(); |
Nicolas Geoffray | 7a2c7c2 | 2018-11-20 10:03:13 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1850 | } // namespace jit |
| 1851 | } // namespace art |