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