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" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 22 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 23 | #include "gc/accounting/bitmap-inl.h" |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 24 | #include "jit/profiling_info.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 25 | #include "linear_alloc.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 26 | #include "mem_map.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 27 | #include "oat_file-inl.h" |
Nicolas Geoffray | 6262340 | 2015-10-28 19:15:05 +0000 | [diff] [blame] | 28 | #include "scoped_thread_state_change.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 29 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace jit { |
| 33 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 34 | static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 35 | static constexpr int kProtData = PROT_READ | PROT_WRITE; |
| 36 | static constexpr int kProtCode = PROT_READ | PROT_EXEC; |
| 37 | |
| 38 | #define CHECKED_MPROTECT(memory, size, prot) \ |
| 39 | do { \ |
| 40 | int rc = mprotect(memory, size, prot); \ |
| 41 | if (UNLIKELY(rc != 0)) { \ |
| 42 | errno = rc; \ |
| 43 | PLOG(FATAL) << "Failed to mprotect jit code cache"; \ |
| 44 | } \ |
| 45 | } while (false) \ |
| 46 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 47 | JitCodeCache* JitCodeCache::Create(size_t initial_capacity, |
| 48 | size_t max_capacity, |
| 49 | std::string* error_msg) { |
| 50 | CHECK_GE(max_capacity, initial_capacity); |
| 51 | // We need to have 32 bit offsets from method headers in code cache which point to things |
| 52 | // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work. |
| 53 | // Ensure we're below 1 GB to be safe. |
| 54 | if (max_capacity > 1 * GB) { |
| 55 | std::ostringstream oss; |
| 56 | oss << "Maxium code cache capacity is limited to 1 GB, " |
| 57 | << PrettySize(max_capacity) << " is too big"; |
| 58 | *error_msg = oss.str(); |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 62 | std::string error_str; |
| 63 | // Map name specific for android_os_Debug.cpp accounting. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 64 | MemMap* data_map = MemMap::MapAnonymous( |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 65 | "data-code-cache", nullptr, max_capacity, kProtAll, false, false, &error_str); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 66 | if (data_map == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 67 | std::ostringstream oss; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 68 | 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] | 69 | *error_msg = oss.str(); |
| 70 | return nullptr; |
| 71 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 72 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 73 | // Align both capacities to page size, as that's the unit mspaces use. |
| 74 | initial_capacity = RoundDown(initial_capacity, 2 * kPageSize); |
| 75 | max_capacity = RoundDown(max_capacity, 2 * kPageSize); |
| 76 | |
Nicolas Geoffray | 4e915fb | 2015-10-28 17:39:47 +0000 | [diff] [blame] | 77 | // Data cache is 1 / 2 of the map. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 78 | // TODO: Make this variable? |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 79 | size_t data_size = max_capacity / 2; |
| 80 | size_t code_size = max_capacity - data_size; |
| 81 | DCHECK_EQ(code_size + data_size, max_capacity); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 82 | uint8_t* divider = data_map->Begin() + data_size; |
| 83 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 84 | MemMap* code_map = data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str); |
| 85 | if (code_map == nullptr) { |
| 86 | std::ostringstream oss; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 87 | oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 88 | *error_msg = oss.str(); |
| 89 | return nullptr; |
| 90 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 91 | DCHECK_EQ(code_map->Begin(), divider); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 92 | data_size = initial_capacity / 2; |
| 93 | code_size = initial_capacity - data_size; |
| 94 | DCHECK_EQ(code_size + data_size, initial_capacity); |
| 95 | return new JitCodeCache(code_map, data_map, code_size, data_size, max_capacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 98 | JitCodeCache::JitCodeCache(MemMap* code_map, |
| 99 | MemMap* data_map, |
| 100 | size_t initial_code_capacity, |
| 101 | size_t initial_data_capacity, |
| 102 | size_t max_capacity) |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 103 | : lock_("Jit code cache", kJitCodeCacheLock), |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 104 | lock_cond_("Jit code cache variable", lock_), |
| 105 | collection_in_progress_(false), |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 106 | code_map_(code_map), |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 107 | data_map_(data_map), |
| 108 | max_capacity_(max_capacity), |
| 109 | current_capacity_(initial_code_capacity + initial_data_capacity), |
| 110 | code_end_(initial_code_capacity), |
| 111 | data_end_(initial_data_capacity), |
| 112 | has_done_one_collection_(false) { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 113 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 114 | code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/); |
| 115 | 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] | 116 | |
| 117 | if (code_mspace_ == nullptr || data_mspace_ == nullptr) { |
| 118 | PLOG(FATAL) << "create_mspace_with_base failed"; |
| 119 | } |
| 120 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 121 | SetFootprintLimit(current_capacity_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 122 | |
| 123 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode); |
| 124 | CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 125 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 126 | VLOG(jit) << "Created jit code cache: initial data size=" |
| 127 | << PrettySize(initial_data_capacity) |
| 128 | << ", initial code size=" |
| 129 | << PrettySize(initial_code_capacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 132 | bool JitCodeCache::ContainsPc(const void* ptr) const { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 133 | return code_map_->Begin() <= ptr && ptr < code_map_->End(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 136 | bool JitCodeCache::ContainsMethod(ArtMethod* method) { |
| 137 | MutexLock mu(Thread::Current(), lock_); |
| 138 | for (auto& it : method_code_map_) { |
| 139 | if (it.second == method) { |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 146 | class ScopedCodeCacheWrite { |
| 147 | public: |
| 148 | explicit ScopedCodeCacheWrite(MemMap* code_map) : code_map_(code_map) { |
| 149 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 150 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 151 | ~ScopedCodeCacheWrite() { |
| 152 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode); |
| 153 | } |
| 154 | private: |
| 155 | MemMap* const code_map_; |
| 156 | |
| 157 | DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite); |
| 158 | }; |
| 159 | |
| 160 | uint8_t* JitCodeCache::CommitCode(Thread* self, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 161 | ArtMethod* method, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 162 | const uint8_t* mapping_table, |
| 163 | const uint8_t* vmap_table, |
| 164 | const uint8_t* gc_map, |
| 165 | size_t frame_size_in_bytes, |
| 166 | size_t core_spill_mask, |
| 167 | size_t fp_spill_mask, |
| 168 | const uint8_t* code, |
| 169 | size_t code_size) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 170 | uint8_t* result = CommitCodeInternal(self, |
| 171 | method, |
| 172 | mapping_table, |
| 173 | vmap_table, |
| 174 | gc_map, |
| 175 | frame_size_in_bytes, |
| 176 | core_spill_mask, |
| 177 | fp_spill_mask, |
| 178 | code, |
| 179 | code_size); |
| 180 | if (result == nullptr) { |
| 181 | // Retry. |
| 182 | GarbageCollectCache(self); |
| 183 | result = CommitCodeInternal(self, |
| 184 | method, |
| 185 | mapping_table, |
| 186 | vmap_table, |
| 187 | gc_map, |
| 188 | frame_size_in_bytes, |
| 189 | core_spill_mask, |
| 190 | fp_spill_mask, |
| 191 | code, |
| 192 | code_size); |
| 193 | } |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) { |
| 198 | bool in_collection = false; |
| 199 | while (collection_in_progress_) { |
| 200 | in_collection = true; |
| 201 | lock_cond_.Wait(self); |
| 202 | } |
| 203 | return in_collection; |
| 204 | } |
| 205 | |
| 206 | static uintptr_t FromCodeToAllocation(const void* code) { |
| 207 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 208 | return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 209 | } |
| 210 | |
| 211 | void JitCodeCache::FreeCode(const void* code_ptr, ArtMethod* method ATTRIBUTE_UNUSED) { |
| 212 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
| 213 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 214 | const uint8_t* data = method_header->GetNativeGcMap(); |
| 215 | if (data != nullptr) { |
| 216 | mspace_free(data_mspace_, const_cast<uint8_t*>(data)); |
| 217 | } |
| 218 | data = method_header->GetMappingTable(); |
| 219 | if (data != nullptr) { |
| 220 | mspace_free(data_mspace_, const_cast<uint8_t*>(data)); |
| 221 | } |
| 222 | // Use the offset directly to prevent sanity check that the method is |
| 223 | // compiled with optimizing. |
| 224 | // TODO(ngeoffray): Clean up. |
| 225 | if (method_header->vmap_table_offset_ != 0) { |
| 226 | data = method_header->code_ - method_header->vmap_table_offset_; |
| 227 | mspace_free(data_mspace_, const_cast<uint8_t*>(data)); |
| 228 | } |
| 229 | mspace_free(code_mspace_, reinterpret_cast<uint8_t*>(allocation)); |
| 230 | } |
| 231 | |
| 232 | void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) { |
| 233 | MutexLock mu(self, lock_); |
| 234 | // We do not check if a code cache GC is in progress, as this method comes |
| 235 | // with the classlinker_classes_lock_ held, and suspending ourselves could |
| 236 | // lead to a deadlock. |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 237 | { |
| 238 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 239 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 240 | if (alloc.ContainsUnsafe(it->second)) { |
| 241 | FreeCode(it->first, it->second); |
| 242 | it = method_code_map_.erase(it); |
| 243 | } else { |
| 244 | ++it; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) { |
| 249 | ProfilingInfo* info = *it; |
| 250 | if (alloc.ContainsUnsafe(info->GetMethod())) { |
| 251 | info->GetMethod()->SetProfilingInfo(nullptr); |
| 252 | mspace_free(data_mspace_, reinterpret_cast<uint8_t*>(info)); |
| 253 | it = profiling_infos_.erase(it); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 254 | } else { |
| 255 | ++it; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | uint8_t* JitCodeCache::CommitCodeInternal(Thread* self, |
| 261 | ArtMethod* method, |
| 262 | const uint8_t* mapping_table, |
| 263 | const uint8_t* vmap_table, |
| 264 | const uint8_t* gc_map, |
| 265 | size_t frame_size_in_bytes, |
| 266 | size_t core_spill_mask, |
| 267 | size_t fp_spill_mask, |
| 268 | const uint8_t* code, |
| 269 | size_t code_size) { |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 270 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 271 | // Ensure the header ends up at expected instruction alignment. |
| 272 | size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 273 | size_t total_size = header_size + code_size; |
| 274 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 275 | OatQuickMethodHeader* method_header = nullptr; |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 276 | uint8_t* code_ptr = nullptr; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 277 | { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 278 | ScopedThreadSuspension sts(self, kSuspended); |
| 279 | MutexLock mu(self, lock_); |
| 280 | WaitForPotentialCollectionToComplete(self); |
| 281 | { |
| 282 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 283 | uint8_t* result = reinterpret_cast<uint8_t*>( |
| 284 | mspace_memalign(code_mspace_, alignment, total_size)); |
| 285 | if (result == nullptr) { |
| 286 | return nullptr; |
| 287 | } |
| 288 | code_ptr = result + header_size; |
| 289 | DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(code_ptr), alignment); |
| 290 | |
| 291 | std::copy(code, code + code_size, code_ptr); |
| 292 | method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 293 | new (method_header) OatQuickMethodHeader( |
| 294 | (mapping_table == nullptr) ? 0 : code_ptr - mapping_table, |
| 295 | (vmap_table == nullptr) ? 0 : code_ptr - vmap_table, |
| 296 | (gc_map == nullptr) ? 0 : code_ptr - gc_map, |
| 297 | frame_size_in_bytes, |
| 298 | core_spill_mask, |
| 299 | fp_spill_mask, |
| 300 | code_size); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 301 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 302 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 303 | __builtin___clear_cache(reinterpret_cast<char*>(code_ptr), |
| 304 | reinterpret_cast<char*>(code_ptr + code_size)); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 305 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 306 | // We need to update the entry point in the runnable state for the instrumentation. |
| 307 | { |
| 308 | MutexLock mu(self, lock_); |
| 309 | method_code_map_.Put(code_ptr, method); |
| 310 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 311 | method, method_header->GetEntryPoint()); |
| 312 | if (collection_in_progress_) { |
| 313 | // We need to update the live bitmap if there is a GC to ensure it sees this new |
| 314 | // code. |
| 315 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 316 | } |
| 317 | VLOG(jit) |
| 318 | << "JIT added " |
| 319 | << PrettyMethod(method) << "@" << method |
| 320 | << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": " |
| 321 | << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": " |
| 322 | << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << "," |
| 323 | << reinterpret_cast<const void*>(method_header->GetEntryPoint() + method_header->code_size_); |
| 324 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 325 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 326 | return reinterpret_cast<uint8_t*>(method_header); |
| 327 | } |
| 328 | |
| 329 | size_t JitCodeCache::CodeCacheSize() { |
| 330 | MutexLock mu(Thread::Current(), lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 331 | return CodeCacheSizeLocked(); |
| 332 | } |
| 333 | |
| 334 | size_t JitCodeCache::CodeCacheSizeLocked() { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 335 | size_t bytes_allocated = 0; |
| 336 | mspace_inspect_all(code_mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated); |
| 337 | return bytes_allocated; |
| 338 | } |
| 339 | |
| 340 | size_t JitCodeCache::DataCacheSize() { |
| 341 | MutexLock mu(Thread::Current(), lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 342 | return DataCacheSizeLocked(); |
| 343 | } |
| 344 | |
| 345 | size_t JitCodeCache::DataCacheSizeLocked() { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 346 | size_t bytes_allocated = 0; |
| 347 | mspace_inspect_all(data_mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated); |
| 348 | return bytes_allocated; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 351 | size_t JitCodeCache::NumberOfCompiledCode() { |
| 352 | MutexLock mu(Thread::Current(), lock_); |
| 353 | return method_code_map_.size(); |
| 354 | } |
| 355 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 356 | void JitCodeCache::ClearData(Thread* self, void* data) { |
| 357 | MutexLock mu(self, lock_); |
| 358 | mspace_free(data_mspace_, data); |
| 359 | } |
| 360 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 361 | uint8_t* JitCodeCache::ReserveData(Thread* self, size_t size) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 362 | size = RoundUp(size, sizeof(void*)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 363 | uint8_t* result = nullptr; |
| 364 | |
| 365 | { |
| 366 | ScopedThreadSuspension sts(self, kSuspended); |
| 367 | MutexLock mu(self, lock_); |
| 368 | WaitForPotentialCollectionToComplete(self); |
| 369 | result = reinterpret_cast<uint8_t*>(mspace_malloc(data_mspace_, size)); |
| 370 | } |
| 371 | |
| 372 | if (result == nullptr) { |
| 373 | // Retry. |
| 374 | GarbageCollectCache(self); |
| 375 | ScopedThreadSuspension sts(self, kSuspended); |
| 376 | MutexLock mu(self, lock_); |
| 377 | WaitForPotentialCollectionToComplete(self); |
| 378 | result = reinterpret_cast<uint8_t*>(mspace_malloc(data_mspace_, size)); |
| 379 | } |
| 380 | |
| 381 | return result; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 384 | 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] | 385 | uint8_t* result = ReserveData(self, end - begin); |
| 386 | if (result == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 387 | return nullptr; // Out of space in the data cache. |
| 388 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 389 | std::copy(begin, end, result); |
| 390 | return result; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 393 | class MarkCodeVisitor FINAL : public StackVisitor { |
| 394 | public: |
| 395 | MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in) |
| 396 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames), |
| 397 | code_cache_(code_cache_in), |
| 398 | bitmap_(code_cache_->GetLiveBitmap()) {} |
| 399 | |
| 400 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 401 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 402 | if (method_header == nullptr) { |
| 403 | return true; |
| 404 | } |
| 405 | const void* code = method_header->GetCode(); |
| 406 | if (code_cache_->ContainsPc(code)) { |
| 407 | // Use the atomic set version, as multiple threads are executing this code. |
| 408 | bitmap_->AtomicTestAndSet(FromCodeToAllocation(code)); |
| 409 | } |
| 410 | return true; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 411 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 412 | |
| 413 | private: |
| 414 | JitCodeCache* const code_cache_; |
| 415 | CodeCacheBitmap* const bitmap_; |
| 416 | }; |
| 417 | |
| 418 | class MarkCodeClosure FINAL : public Closure { |
| 419 | public: |
| 420 | MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier) |
| 421 | : code_cache_(code_cache), barrier_(barrier) {} |
| 422 | |
| 423 | void Run(Thread* thread) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 424 | DCHECK(thread == Thread::Current() || thread->IsSuspended()); |
| 425 | MarkCodeVisitor visitor(thread, code_cache_); |
| 426 | visitor.WalkStack(); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 427 | if (kIsDebugBuild) { |
| 428 | // The stack walking code queries the side instrumentation stack if it |
| 429 | // sees an instrumentation exit pc, so the JIT code of methods in that stack |
| 430 | // must have been seen. We sanity check this below. |
| 431 | for (const instrumentation::InstrumentationStackFrame& frame |
| 432 | : *thread->GetInstrumentationStack()) { |
| 433 | // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in |
| 434 | // its stack frame, it is not the method owning return_pc_. We just pass null to |
| 435 | // LookupMethodHeader: the method is only checked against in debug builds. |
| 436 | OatQuickMethodHeader* method_header = |
| 437 | code_cache_->LookupMethodHeader(frame.return_pc_, nullptr); |
| 438 | if (method_header != nullptr) { |
| 439 | const void* code = method_header->GetCode(); |
| 440 | CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code))); |
| 441 | } |
| 442 | } |
| 443 | } |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 444 | barrier_->Pass(Thread::Current()); |
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 | Barrier* const barrier_; |
| 450 | }; |
| 451 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 452 | void JitCodeCache::NotifyCollectionDone(Thread* self) { |
| 453 | collection_in_progress_ = false; |
| 454 | lock_cond_.Broadcast(self); |
| 455 | } |
| 456 | |
| 457 | void JitCodeCache::SetFootprintLimit(size_t new_footprint) { |
| 458 | size_t per_space_footprint = new_footprint / 2; |
| 459 | DCHECK(IsAlignedParam(per_space_footprint, kPageSize)); |
| 460 | DCHECK_EQ(per_space_footprint * 2, new_footprint); |
| 461 | mspace_set_footprint_limit(data_mspace_, per_space_footprint); |
| 462 | { |
| 463 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 464 | mspace_set_footprint_limit(code_mspace_, per_space_footprint); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | bool JitCodeCache::IncreaseCodeCacheCapacity() { |
| 469 | if (current_capacity_ == max_capacity_) { |
| 470 | return false; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 473 | // Double the capacity if we're below 1MB, or increase it by 1MB if |
| 474 | // we're above. |
| 475 | if (current_capacity_ < 1 * MB) { |
| 476 | current_capacity_ *= 2; |
| 477 | } else { |
| 478 | current_capacity_ += 1 * MB; |
| 479 | } |
| 480 | if (current_capacity_ > max_capacity_) { |
| 481 | current_capacity_ = max_capacity_; |
| 482 | } |
| 483 | |
| 484 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 485 | LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_); |
| 486 | } |
| 487 | |
| 488 | SetFootprintLimit(current_capacity_); |
| 489 | |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | void JitCodeCache::GarbageCollectCache(Thread* self) { |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 494 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 495 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 496 | // Wait for an existing collection, or let everyone know we are starting one. |
| 497 | { |
| 498 | ScopedThreadSuspension sts(self, kSuspended); |
| 499 | MutexLock mu(self, lock_); |
| 500 | if (WaitForPotentialCollectionToComplete(self)) { |
| 501 | return; |
| 502 | } else { |
| 503 | collection_in_progress_ = true; |
| 504 | } |
| 505 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 506 | |
| 507 | // Check if we just need to grow the capacity. If we don't, allocate the bitmap while |
| 508 | // we hold the lock. |
| 509 | { |
| 510 | MutexLock mu(self, lock_); |
| 511 | if (has_done_one_collection_ && IncreaseCodeCacheCapacity()) { |
| 512 | has_done_one_collection_ = false; |
| 513 | NotifyCollectionDone(self); |
| 514 | return; |
| 515 | } else { |
| 516 | live_bitmap_.reset(CodeCacheBitmap::Create( |
| 517 | "code-cache-bitmap", |
| 518 | reinterpret_cast<uintptr_t>(code_map_->Begin()), |
| 519 | reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2))); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 524 | LOG(INFO) << "Clearing code cache, code=" |
| 525 | << PrettySize(CodeCacheSize()) |
| 526 | << ", data=" << PrettySize(DataCacheSize()); |
| 527 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 528 | // Walk over all compiled methods and set the entry points of these |
| 529 | // methods to interpreter. |
| 530 | { |
| 531 | MutexLock mu(self, lock_); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 532 | for (auto& it : method_code_map_) { |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 533 | instrumentation->UpdateMethodsCode(it.second, GetQuickToInterpreterBridge()); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 534 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 535 | for (ProfilingInfo* info : profiling_infos_) { |
| 536 | info->GetMethod()->SetProfilingInfo(nullptr); |
| 537 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Run a checkpoint on all threads to mark the JIT compiled code they are running. |
| 541 | { |
| 542 | Barrier barrier(0); |
Nicolas Geoffray | 6262340 | 2015-10-28 19:15:05 +0000 | [diff] [blame] | 543 | size_t threads_running_checkpoint = 0; |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 544 | MarkCodeClosure closure(this, &barrier); |
| 545 | threads_running_checkpoint = |
| 546 | Runtime::Current()->GetThreadList()->RunCheckpoint(&closure); |
| 547 | // Now that we have run our checkpoint, move to a suspended state and wait |
| 548 | // for other threads to run the checkpoint. |
| 549 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 550 | if (threads_running_checkpoint != 0) { |
| 551 | barrier.Increment(self, threads_running_checkpoint); |
| 552 | } |
| 553 | } |
| 554 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 555 | { |
| 556 | MutexLock mu(self, lock_); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 557 | // Free unused compiled code, and restore the entry point of used compiled code. |
| 558 | { |
| 559 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 560 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 561 | const void* code_ptr = it->first; |
| 562 | ArtMethod* method = it->second; |
| 563 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
| 564 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 565 | if (GetLiveBitmap()->Test(allocation)) { |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 566 | instrumentation->UpdateMethodsCode(method, method_header->GetEntryPoint()); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 567 | ++it; |
| 568 | } else { |
| 569 | method->ClearCounter(); |
| 570 | DCHECK_NE(method->GetEntryPointFromQuickCompiledCode(), method_header->GetEntryPoint()); |
| 571 | FreeCode(code_ptr, method); |
| 572 | it = method_code_map_.erase(it); |
| 573 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 574 | } |
| 575 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 576 | |
| 577 | // Free all profiling info. |
| 578 | for (ProfilingInfo* info : profiling_infos_) { |
| 579 | DCHECK(info->GetMethod()->GetProfilingInfo(sizeof(void*)) == nullptr); |
| 580 | mspace_free(data_mspace_, reinterpret_cast<uint8_t*>(info)); |
| 581 | } |
| 582 | profiling_infos_.clear(); |
| 583 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 584 | live_bitmap_.reset(nullptr); |
| 585 | has_done_one_collection_ = true; |
| 586 | NotifyCollectionDone(self); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 590 | LOG(INFO) << "After clearing code cache, code=" |
| 591 | << PrettySize(CodeCacheSize()) |
| 592 | << ", data=" << PrettySize(DataCacheSize()); |
| 593 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 596 | |
| 597 | OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) { |
| 598 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 599 | if (kRuntimeISA == kArm) { |
| 600 | // On Thumb-2, the pc is offset by one. |
| 601 | --pc; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 602 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 603 | if (!ContainsPc(reinterpret_cast<const void*>(pc))) { |
| 604 | return nullptr; |
| 605 | } |
| 606 | |
| 607 | MutexLock mu(Thread::Current(), lock_); |
| 608 | if (method_code_map_.empty()) { |
| 609 | return nullptr; |
| 610 | } |
| 611 | auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc)); |
| 612 | --it; |
| 613 | |
| 614 | const void* code_ptr = it->first; |
| 615 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 616 | if (!method_header->Contains(pc)) { |
| 617 | return nullptr; |
| 618 | } |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 619 | if (kIsDebugBuild && method != nullptr) { |
| 620 | DCHECK_EQ(it->second, method) |
| 621 | << PrettyMethod(method) << " " << PrettyMethod(it->second) << " " << std::hex << pc; |
| 622 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 623 | return method_header; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 626 | ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self, |
| 627 | ArtMethod* method, |
| 628 | const std::vector<uint32_t>& entries, |
| 629 | bool retry_allocation) { |
| 630 | ProfilingInfo* info = AddProfilingInfoInternal(self, method, entries); |
| 631 | |
| 632 | if (info == nullptr && retry_allocation) { |
| 633 | GarbageCollectCache(self); |
| 634 | info = AddProfilingInfoInternal(self, method, entries); |
| 635 | } |
| 636 | return info; |
| 637 | } |
| 638 | |
| 639 | ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self, |
| 640 | ArtMethod* method, |
| 641 | const std::vector<uint32_t>& entries) { |
| 642 | size_t profile_info_size = RoundUp( |
| 643 | sizeof(ProfilingInfo) + sizeof(ProfilingInfo::InlineCache) * entries.size(), |
| 644 | sizeof(void*)); |
| 645 | ScopedThreadSuspension sts(self, kSuspended); |
| 646 | MutexLock mu(self, lock_); |
| 647 | WaitForPotentialCollectionToComplete(self); |
| 648 | |
| 649 | // Check whether some other thread has concurrently created it. |
| 650 | ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*)); |
| 651 | if (info != nullptr) { |
| 652 | return info; |
| 653 | } |
| 654 | |
| 655 | uint8_t* data = reinterpret_cast<uint8_t*>(mspace_malloc(data_mspace_, profile_info_size)); |
| 656 | if (data == nullptr) { |
| 657 | return nullptr; |
| 658 | } |
| 659 | info = new (data) ProfilingInfo(method, entries); |
| 660 | method->SetProfilingInfo(info); |
| 661 | profiling_infos_.push_back(info); |
| 662 | return info; |
| 663 | } |
| 664 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame^] | 665 | // NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock |
| 666 | // is already held. |
| 667 | void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS { |
| 668 | if (code_mspace_ == mspace) { |
| 669 | size_t result = code_end_; |
| 670 | code_end_ += increment; |
| 671 | return reinterpret_cast<void*>(result + code_map_->Begin()); |
| 672 | } else { |
| 673 | DCHECK_EQ(data_mspace_, mspace); |
| 674 | size_t result = data_end_; |
| 675 | data_end_ += increment; |
| 676 | return reinterpret_cast<void*>(result + data_map_->Begin()); |
| 677 | } |
| 678 | } |
| 679 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 680 | } // namespace jit |
| 681 | } // namespace art |