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