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