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