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