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