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