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