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