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