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