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