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