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