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