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