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