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