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