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