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