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