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