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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 23 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 24 | #include "base/systrace.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 25 | #include "base/time_utils.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 26 | #include "debugger_interface.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 27 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 28 | #include "gc/accounting/bitmap-inl.h" |
Nicolas Geoffray | cf48fa0 | 2016-07-30 22:49:11 +0100 | [diff] [blame] | 29 | #include "gc/scoped_gc_critical_section.h" |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 30 | #include "jit/jit.h" |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 31 | #include "jit/profiling_info.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 32 | #include "linear_alloc.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 33 | #include "mem_map.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "oat_file-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 35 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 36 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | namespace jit { |
| 40 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 41 | static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 42 | static constexpr int kProtData = PROT_READ | PROT_WRITE; |
| 43 | static constexpr int kProtCode = PROT_READ | PROT_EXEC; |
| 44 | |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 45 | static constexpr size_t kCodeSizeLogThreshold = 50 * KB; |
| 46 | static constexpr size_t kStackMapSizeLogThreshold = 50 * KB; |
| 47 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 48 | #define CHECKED_MPROTECT(memory, size, prot) \ |
| 49 | do { \ |
| 50 | int rc = mprotect(memory, size, prot); \ |
| 51 | if (UNLIKELY(rc != 0)) { \ |
| 52 | errno = rc; \ |
| 53 | PLOG(FATAL) << "Failed to mprotect jit code cache"; \ |
| 54 | } \ |
| 55 | } while (false) \ |
| 56 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 57 | JitCodeCache* JitCodeCache::Create(size_t initial_capacity, |
| 58 | size_t max_capacity, |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 59 | bool generate_debug_info, |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 60 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 61 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 62 | CHECK_GE(max_capacity, initial_capacity); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 63 | |
| 64 | // Generating debug information is mostly for using the 'perf' tool, which does |
| 65 | // not work with ashmem. |
| 66 | bool use_ashmem = !generate_debug_info; |
| 67 | // With 'perf', we want a 1-1 mapping between an address and a method. |
| 68 | bool garbage_collect_code = !generate_debug_info; |
| 69 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 70 | // We need to have 32 bit offsets from method headers in code cache which point to things |
| 71 | // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work. |
| 72 | // Ensure we're below 1 GB to be safe. |
| 73 | if (max_capacity > 1 * GB) { |
| 74 | std::ostringstream oss; |
| 75 | oss << "Maxium code cache capacity is limited to 1 GB, " |
| 76 | << PrettySize(max_capacity) << " is too big"; |
| 77 | *error_msg = oss.str(); |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 81 | std::string error_str; |
| 82 | // Map name specific for android_os_Debug.cpp accounting. |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 83 | // Map in low 4gb to simplify accessing root tables for x86_64. |
| 84 | // We could do PC-relative addressing to avoid this problem, but that |
| 85 | // would require reserving code and data area before submitting, which |
| 86 | // means more windows for the code memory to be RWX. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 87 | MemMap* data_map = MemMap::MapAnonymous( |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 88 | "data-code-cache", nullptr, |
| 89 | max_capacity, |
| 90 | kProtAll, |
| 91 | /* low_4gb */ true, |
| 92 | /* reuse */ false, |
| 93 | &error_str, |
| 94 | use_ashmem); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 95 | if (data_map == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 96 | std::ostringstream oss; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 97 | 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] | 98 | *error_msg = oss.str(); |
| 99 | return nullptr; |
| 100 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 101 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 102 | // Align both capacities to page size, as that's the unit mspaces use. |
| 103 | initial_capacity = RoundDown(initial_capacity, 2 * kPageSize); |
| 104 | max_capacity = RoundDown(max_capacity, 2 * kPageSize); |
| 105 | |
Nicolas Geoffray | 4e915fb | 2015-10-28 17:39:47 +0000 | [diff] [blame] | 106 | // Data cache is 1 / 2 of the map. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 107 | // TODO: Make this variable? |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 108 | size_t data_size = max_capacity / 2; |
| 109 | size_t code_size = max_capacity - data_size; |
| 110 | DCHECK_EQ(code_size + data_size, max_capacity); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 111 | uint8_t* divider = data_map->Begin() + data_size; |
| 112 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 113 | MemMap* code_map = |
| 114 | data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str, use_ashmem); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 115 | if (code_map == nullptr) { |
| 116 | std::ostringstream oss; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 117 | 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] | 118 | *error_msg = oss.str(); |
| 119 | return nullptr; |
| 120 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 121 | DCHECK_EQ(code_map->Begin(), divider); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 122 | data_size = initial_capacity / 2; |
| 123 | code_size = initial_capacity - data_size; |
| 124 | DCHECK_EQ(code_size + data_size, initial_capacity); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 125 | return new JitCodeCache( |
Nicolas Geoffray | c3fec4c | 2016-01-14 16:16:35 +0000 | [diff] [blame] | 126 | 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] | 127 | } |
| 128 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 129 | JitCodeCache::JitCodeCache(MemMap* code_map, |
| 130 | MemMap* data_map, |
| 131 | size_t initial_code_capacity, |
| 132 | size_t initial_data_capacity, |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 133 | size_t max_capacity, |
| 134 | bool garbage_collect_code) |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 135 | : lock_("Jit code cache", kJitCodeCacheLock), |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame^] | 136 | lock_cond_("Jit code cache condition variable", lock_), |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 137 | collection_in_progress_(false), |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 138 | code_map_(code_map), |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 139 | data_map_(data_map), |
| 140 | max_capacity_(max_capacity), |
| 141 | current_capacity_(initial_code_capacity + initial_data_capacity), |
| 142 | code_end_(initial_code_capacity), |
| 143 | data_end_(initial_data_capacity), |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 144 | last_collection_increased_code_cache_(false), |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 145 | last_update_time_ns_(0), |
Nicolas Geoffray | 0a52223 | 2016-01-19 09:34:58 +0000 | [diff] [blame] | 146 | garbage_collect_code_(garbage_collect_code), |
Nicolas Geoffray | b0d2208 | 2016-02-24 17:18:25 +0000 | [diff] [blame] | 147 | used_memory_for_data_(0), |
| 148 | used_memory_for_code_(0), |
Nicolas Geoffray | fcdd729 | 2016-02-25 13:27:47 +0000 | [diff] [blame] | 149 | number_of_compilations_(0), |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 150 | number_of_osr_compilations_(0), |
| 151 | number_of_deoptimizations_(0), |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 152 | number_of_collections_(0), |
| 153 | histogram_stack_map_memory_use_("Memory used for stack maps", 16), |
| 154 | histogram_code_memory_use_("Memory used for compiled code", 16), |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame^] | 155 | histogram_profiling_info_memory_use_("Memory used for profiling info", 16), |
| 156 | is_weak_access_enabled_(true), |
| 157 | inline_cache_cond_("Jit inline cache condition variable", lock_) { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 158 | |
Nicolas Geoffray | c3fec4c | 2016-01-14 16:16:35 +0000 | [diff] [blame] | 159 | DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 160 | code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/); |
| 161 | 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] | 162 | |
| 163 | if (code_mspace_ == nullptr || data_mspace_ == nullptr) { |
| 164 | PLOG(FATAL) << "create_mspace_with_base failed"; |
| 165 | } |
| 166 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 167 | SetFootprintLimit(current_capacity_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 168 | |
| 169 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode); |
| 170 | CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 171 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 172 | VLOG(jit) << "Created jit code cache: initial data size=" |
| 173 | << PrettySize(initial_data_capacity) |
| 174 | << ", initial code size=" |
| 175 | << PrettySize(initial_code_capacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 178 | bool JitCodeCache::ContainsPc(const void* ptr) const { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 179 | return code_map_->Begin() <= ptr && ptr < code_map_->End(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 182 | bool JitCodeCache::ContainsMethod(ArtMethod* method) { |
| 183 | MutexLock mu(Thread::Current(), lock_); |
| 184 | for (auto& it : method_code_map_) { |
| 185 | if (it.second == method) { |
| 186 | return true; |
| 187 | } |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
Mathieu Chartier | 33fbf37 | 2016-03-07 13:48:08 -0800 | [diff] [blame] | 192 | class ScopedCodeCacheWrite : ScopedTrace { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 193 | public: |
Mathieu Chartier | 33fbf37 | 2016-03-07 13:48:08 -0800 | [diff] [blame] | 194 | explicit ScopedCodeCacheWrite(MemMap* code_map) |
| 195 | : ScopedTrace("ScopedCodeCacheWrite"), |
| 196 | code_map_(code_map) { |
| 197 | ScopedTrace trace("mprotect all"); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 198 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 199 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 200 | ~ScopedCodeCacheWrite() { |
Mathieu Chartier | 33fbf37 | 2016-03-07 13:48:08 -0800 | [diff] [blame] | 201 | ScopedTrace trace("mprotect code"); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 202 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode); |
| 203 | } |
| 204 | private: |
| 205 | MemMap* const code_map_; |
| 206 | |
| 207 | DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite); |
| 208 | }; |
| 209 | |
| 210 | uint8_t* JitCodeCache::CommitCode(Thread* self, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 211 | ArtMethod* method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 212 | uint8_t* stack_map, |
| 213 | uint8_t* roots_data, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 214 | size_t frame_size_in_bytes, |
| 215 | size_t core_spill_mask, |
| 216 | size_t fp_spill_mask, |
| 217 | const uint8_t* code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 218 | size_t code_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 219 | bool osr, |
| 220 | Handle<mirror::ObjectArray<mirror::Object>> roots) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 221 | uint8_t* result = CommitCodeInternal(self, |
| 222 | method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 223 | stack_map, |
| 224 | roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 225 | frame_size_in_bytes, |
| 226 | core_spill_mask, |
| 227 | fp_spill_mask, |
| 228 | code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 229 | code_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 230 | osr, |
| 231 | roots); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 232 | if (result == nullptr) { |
| 233 | // Retry. |
| 234 | GarbageCollectCache(self); |
| 235 | result = CommitCodeInternal(self, |
| 236 | method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 237 | stack_map, |
| 238 | roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 239 | frame_size_in_bytes, |
| 240 | core_spill_mask, |
| 241 | fp_spill_mask, |
| 242 | code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 243 | code_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 244 | osr, |
| 245 | roots); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 246 | } |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) { |
| 251 | bool in_collection = false; |
| 252 | while (collection_in_progress_) { |
| 253 | in_collection = true; |
| 254 | lock_cond_.Wait(self); |
| 255 | } |
| 256 | return in_collection; |
| 257 | } |
| 258 | |
| 259 | static uintptr_t FromCodeToAllocation(const void* code) { |
| 260 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 261 | return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 262 | } |
| 263 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 264 | static uint32_t ComputeRootTableSize(uint32_t number_of_roots) { |
| 265 | return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>); |
| 266 | } |
| 267 | |
| 268 | static uint32_t GetNumberOfRoots(const uint8_t* stack_map) { |
| 269 | // The length of the table is stored just before the stack map (and therefore at the end of |
| 270 | // the table itself), in order to be able to fetch it from a `stack_map` pointer. |
| 271 | return reinterpret_cast<const uint32_t*>(stack_map)[-1]; |
| 272 | } |
| 273 | |
| 274 | static void FillRootTable(uint8_t* roots_data, Handle<mirror::ObjectArray<mirror::Object>> roots) |
| 275 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 276 | GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
| 277 | uint32_t length = roots->GetLength(); |
| 278 | // Put all roots in `roots_data`. |
| 279 | for (uint32_t i = 0; i < length; ++i) { |
| 280 | ObjPtr<mirror::Object> object = roots->Get(i); |
| 281 | if (kIsDebugBuild) { |
| 282 | // Ensure the string is strongly interned. b/32995596 |
| 283 | CHECK(object->IsString()); |
| 284 | ObjPtr<mirror::String> str = reinterpret_cast<mirror::String*>(object.Ptr()); |
| 285 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 286 | CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr); |
| 287 | } |
| 288 | gc_roots[i] = GcRoot<mirror::Object>(object); |
| 289 | } |
| 290 | // Store the length of the table at the end. This will allow fetching it from a `stack_map` |
| 291 | // pointer. |
| 292 | reinterpret_cast<uint32_t*>(gc_roots + length)[0] = length; |
| 293 | } |
| 294 | |
| 295 | static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) { |
| 296 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 297 | uint8_t* data = method_header->GetOptimizedCodeInfoPtr(); |
| 298 | uint32_t roots = GetNumberOfRoots(data); |
| 299 | if (number_of_roots != nullptr) { |
| 300 | *number_of_roots = roots; |
| 301 | } |
| 302 | return data - ComputeRootTableSize(roots); |
| 303 | } |
| 304 | |
| 305 | void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) { |
| 306 | MutexLock mu(Thread::Current(), lock_); |
| 307 | for (const auto& entry : method_code_map_) { |
| 308 | uint32_t number_of_roots = 0; |
| 309 | uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots); |
| 310 | GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
| 311 | for (uint32_t i = 0; i < number_of_roots; ++i) { |
| 312 | // This does not need a read barrier because this is called by GC. |
| 313 | mirror::Object* object = roots[i].Read<kWithoutReadBarrier>(); |
| 314 | DCHECK(object != nullptr); |
| 315 | mirror::Object* new_object = visitor->IsMarked(object); |
| 316 | // We know the string is marked because it's a strongly-interned string that |
| 317 | // is always alive. The IsMarked implementation of the CMS collector returns |
| 318 | // null for newly allocated objects, but we know those haven't moved. Therefore, |
| 319 | // only update the entry if we get a different non-null string. |
| 320 | // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method |
| 321 | // out of the weak access/creation pause. b/32167580 |
| 322 | if (new_object != nullptr && new_object != object) { |
| 323 | DCHECK(new_object->IsString()); |
| 324 | roots[i] = GcRoot<mirror::Object>(new_object); |
| 325 | } |
| 326 | } |
| 327 | } |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame^] | 328 | // Walk over inline caches to clear entries containing unloaded classes. |
| 329 | for (ProfilingInfo* info : profiling_infos_) { |
| 330 | for (size_t i = 0; i < info->number_of_inline_caches_; ++i) { |
| 331 | InlineCache* cache = &info->cache_[i]; |
| 332 | for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) { |
| 333 | // This does not need a read barrier because this is called by GC. |
| 334 | mirror::Class* cls = cache->classes_[j].Read<kWithoutReadBarrier>(); |
| 335 | if (cls != nullptr) { |
| 336 | // Look at the classloader of the class to know if it has been |
| 337 | // unloaded. |
| 338 | // This does not need a read barrier because this is called by GC. |
| 339 | mirror::Object* class_loader = |
| 340 | cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 341 | if (visitor->IsMarked(class_loader) != nullptr) { |
| 342 | // The class loader is live, update the entry if the class has moved. |
| 343 | mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls)); |
| 344 | // Note that new_object can be null for CMS and newly allocated objects. |
| 345 | if (new_cls != nullptr && new_cls != cls) { |
| 346 | cache->classes_[j] = GcRoot<mirror::Class>(new_cls); |
| 347 | } |
| 348 | } else { |
| 349 | // The class loader is not live, clear the entry. |
| 350 | cache->classes_[j] = GcRoot<mirror::Class>(nullptr); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 358 | void JitCodeCache::FreeCode(const void* code_ptr, ArtMethod* method ATTRIBUTE_UNUSED) { |
| 359 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 360 | // Notify native debugger that we are about to remove the code. |
| 361 | // It does nothing if we are not using native debugger. |
| 362 | DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr)); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 363 | FreeData(GetRootTable(code_ptr)); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 364 | FreeCode(reinterpret_cast<uint8_t*>(allocation)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 368 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 369 | MutexLock mu(self, lock_); |
| 370 | // We do not check if a code cache GC is in progress, as this method comes |
| 371 | // with the classlinker_classes_lock_ held, and suspending ourselves could |
| 372 | // lead to a deadlock. |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 373 | { |
| 374 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 375 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 376 | if (alloc.ContainsUnsafe(it->second)) { |
| 377 | FreeCode(it->first, it->second); |
| 378 | it = method_code_map_.erase(it); |
| 379 | } else { |
| 380 | ++it; |
| 381 | } |
| 382 | } |
| 383 | } |
Nicolas Geoffray | a9b9131 | 2016-02-17 09:49:19 +0000 | [diff] [blame] | 384 | for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) { |
| 385 | if (alloc.ContainsUnsafe(it->first)) { |
| 386 | // Note that the code has already been removed in the loop above. |
| 387 | it = osr_code_map_.erase(it); |
| 388 | } else { |
| 389 | ++it; |
| 390 | } |
| 391 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 392 | for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) { |
| 393 | ProfilingInfo* info = *it; |
| 394 | if (alloc.ContainsUnsafe(info->GetMethod())) { |
| 395 | info->GetMethod()->SetProfilingInfo(nullptr); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 396 | FreeData(reinterpret_cast<uint8_t*>(info)); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 397 | it = profiling_infos_.erase(it); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 398 | } else { |
| 399 | ++it; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame^] | 404 | bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const { |
| 405 | return kUseReadBarrier |
| 406 | ? self->GetWeakRefAccessEnabled() |
| 407 | : is_weak_access_enabled_.LoadSequentiallyConsistent(); |
| 408 | } |
| 409 | |
| 410 | void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) { |
| 411 | if (IsWeakAccessEnabled(self)) { |
| 412 | return; |
| 413 | } |
| 414 | ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 415 | MutexLock mu(self, lock_); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame^] | 416 | while (!IsWeakAccessEnabled(self)) { |
| 417 | inline_cache_cond_.Wait(self); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void JitCodeCache::BroadcastForInlineCacheAccess() { |
| 422 | Thread* self = Thread::Current(); |
| 423 | MutexLock mu(self, lock_); |
| 424 | inline_cache_cond_.Broadcast(self); |
| 425 | } |
| 426 | |
| 427 | void JitCodeCache::AllowInlineCacheAccess() { |
| 428 | DCHECK(!kUseReadBarrier); |
| 429 | is_weak_access_enabled_.StoreSequentiallyConsistent(true); |
| 430 | BroadcastForInlineCacheAccess(); |
| 431 | } |
| 432 | |
| 433 | void JitCodeCache::DisallowInlineCacheAccess() { |
| 434 | DCHECK(!kUseReadBarrier); |
| 435 | is_weak_access_enabled_.StoreSequentiallyConsistent(false); |
| 436 | } |
| 437 | |
| 438 | void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic, |
| 439 | Handle<mirror::ObjectArray<mirror::Class>> array) { |
| 440 | WaitUntilInlineCacheAccessible(Thread::Current()); |
| 441 | // Note that we don't need to lock `lock_` here, the compiler calling |
| 442 | // this method has already ensured the inline cache will not be deleted. |
| 443 | for (size_t in_cache = 0, in_array = 0; |
| 444 | in_cache < InlineCache::kIndividualCacheSize; |
| 445 | ++in_cache) { |
| 446 | mirror::Class* object = ic.classes_[in_cache].Read(); |
| 447 | if (object != nullptr) { |
| 448 | array->Set(in_array++, object); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 453 | uint8_t* JitCodeCache::CommitCodeInternal(Thread* self, |
| 454 | ArtMethod* method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 455 | uint8_t* stack_map, |
| 456 | uint8_t* roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 457 | size_t frame_size_in_bytes, |
| 458 | size_t core_spill_mask, |
| 459 | size_t fp_spill_mask, |
| 460 | const uint8_t* code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 461 | size_t code_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 462 | bool osr, |
| 463 | Handle<mirror::ObjectArray<mirror::Object>> roots) { |
| 464 | DCHECK(stack_map != nullptr); |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 465 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 466 | // Ensure the header ends up at expected instruction alignment. |
| 467 | size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 468 | size_t total_size = header_size + code_size; |
| 469 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 470 | OatQuickMethodHeader* method_header = nullptr; |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 471 | uint8_t* code_ptr = nullptr; |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 472 | uint8_t* memory = nullptr; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 473 | { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 474 | ScopedThreadSuspension sts(self, kSuspended); |
| 475 | MutexLock mu(self, lock_); |
| 476 | WaitForPotentialCollectionToComplete(self); |
| 477 | { |
| 478 | ScopedCodeCacheWrite scc(code_map_.get()); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 479 | memory = AllocateCode(total_size); |
| 480 | if (memory == nullptr) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 481 | return nullptr; |
| 482 | } |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 483 | code_ptr = memory + header_size; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 484 | |
| 485 | std::copy(code, code + code_size, code_ptr); |
| 486 | method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 487 | new (method_header) OatQuickMethodHeader( |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 488 | code_ptr - stack_map, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 489 | frame_size_in_bytes, |
| 490 | core_spill_mask, |
| 491 | fp_spill_mask, |
| 492 | code_size); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 493 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 494 | |
Roland Levillain | 3243026 | 2016-02-01 15:23:20 +0000 | [diff] [blame] | 495 | FlushInstructionCache(reinterpret_cast<char*>(code_ptr), |
| 496 | reinterpret_cast<char*>(code_ptr + code_size)); |
Nicolas Geoffray | 0a52223 | 2016-01-19 09:34:58 +0000 | [diff] [blame] | 497 | number_of_compilations_++; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 498 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 499 | // We need to update the entry point in the runnable state for the instrumentation. |
| 500 | { |
| 501 | MutexLock mu(self, lock_); |
| 502 | method_code_map_.Put(code_ptr, method); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 503 | // Fill the root table before updating the entry point. |
| 504 | FillRootTable(roots_data, roots); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 505 | if (osr) { |
Nicolas Geoffray | fcdd729 | 2016-02-25 13:27:47 +0000 | [diff] [blame] | 506 | number_of_osr_compilations_++; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 507 | osr_code_map_.Put(method, code_ptr); |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 508 | } else { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 509 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 510 | method, method_header->GetEntryPoint()); |
| 511 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 512 | if (collection_in_progress_) { |
| 513 | // We need to update the live bitmap if there is a GC to ensure it sees this new |
| 514 | // code. |
| 515 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 516 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 517 | last_update_time_ns_.StoreRelease(NanoTime()); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 518 | VLOG(jit) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 519 | << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 520 | << ArtMethod::PrettyMethod(method) << "@" << method |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 521 | << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": " |
| 522 | << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": " |
| 523 | << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << "," |
| 524 | << reinterpret_cast<const void*>(method_header->GetEntryPoint() + method_header->code_size_); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 525 | histogram_code_memory_use_.AddValue(code_size); |
| 526 | if (code_size > kCodeSizeLogThreshold) { |
| 527 | LOG(INFO) << "JIT allocated " |
| 528 | << PrettySize(code_size) |
| 529 | << " for compiled code of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 530 | << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 531 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 532 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 533 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 534 | return reinterpret_cast<uint8_t*>(method_header); |
| 535 | } |
| 536 | |
| 537 | size_t JitCodeCache::CodeCacheSize() { |
| 538 | MutexLock mu(Thread::Current(), lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 539 | return CodeCacheSizeLocked(); |
| 540 | } |
| 541 | |
| 542 | size_t JitCodeCache::CodeCacheSizeLocked() { |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 543 | return used_memory_for_code_; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | size_t JitCodeCache::DataCacheSize() { |
| 547 | MutexLock mu(Thread::Current(), lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 548 | return DataCacheSizeLocked(); |
| 549 | } |
| 550 | |
| 551 | size_t JitCodeCache::DataCacheSizeLocked() { |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 552 | return used_memory_for_data_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 555 | void JitCodeCache::ClearData(Thread* self, void* data) { |
| 556 | MutexLock mu(self, lock_); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 557 | FreeData(reinterpret_cast<uint8_t*>(data)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 560 | void JitCodeCache::ReserveData(Thread* self, |
| 561 | size_t stack_map_size, |
| 562 | size_t number_of_roots, |
| 563 | ArtMethod* method, |
| 564 | uint8_t** stack_map_data, |
| 565 | uint8_t** roots_data) { |
| 566 | size_t table_size = ComputeRootTableSize(number_of_roots); |
| 567 | size_t size = RoundUp(stack_map_size + table_size, sizeof(void*)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 568 | uint8_t* result = nullptr; |
| 569 | |
| 570 | { |
| 571 | ScopedThreadSuspension sts(self, kSuspended); |
| 572 | MutexLock mu(self, lock_); |
| 573 | WaitForPotentialCollectionToComplete(self); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 574 | result = AllocateData(size); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | if (result == nullptr) { |
| 578 | // Retry. |
| 579 | GarbageCollectCache(self); |
| 580 | ScopedThreadSuspension sts(self, kSuspended); |
| 581 | MutexLock mu(self, lock_); |
| 582 | WaitForPotentialCollectionToComplete(self); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 583 | result = AllocateData(size); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 584 | } |
| 585 | |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 586 | MutexLock mu(self, lock_); |
| 587 | histogram_stack_map_memory_use_.AddValue(size); |
| 588 | if (size > kStackMapSizeLogThreshold) { |
| 589 | LOG(INFO) << "JIT allocated " |
| 590 | << PrettySize(size) |
| 591 | << " for stack maps of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 592 | << ArtMethod::PrettyMethod(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 593 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 594 | *roots_data = result; |
| 595 | *stack_map_data = result + table_size; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 598 | class MarkCodeVisitor FINAL : public StackVisitor { |
| 599 | public: |
| 600 | MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in) |
| 601 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames), |
| 602 | code_cache_(code_cache_in), |
| 603 | bitmap_(code_cache_->GetLiveBitmap()) {} |
| 604 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 605 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 606 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 607 | if (method_header == nullptr) { |
| 608 | return true; |
| 609 | } |
| 610 | const void* code = method_header->GetCode(); |
| 611 | if (code_cache_->ContainsPc(code)) { |
| 612 | // Use the atomic set version, as multiple threads are executing this code. |
| 613 | bitmap_->AtomicTestAndSet(FromCodeToAllocation(code)); |
| 614 | } |
| 615 | return true; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 616 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 617 | |
| 618 | private: |
| 619 | JitCodeCache* const code_cache_; |
| 620 | CodeCacheBitmap* const bitmap_; |
| 621 | }; |
| 622 | |
| 623 | class MarkCodeClosure FINAL : public Closure { |
| 624 | public: |
| 625 | MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier) |
| 626 | : code_cache_(code_cache), barrier_(barrier) {} |
| 627 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 628 | void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 629 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 630 | DCHECK(thread == Thread::Current() || thread->IsSuspended()); |
| 631 | MarkCodeVisitor visitor(thread, code_cache_); |
| 632 | visitor.WalkStack(); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 633 | if (kIsDebugBuild) { |
| 634 | // The stack walking code queries the side instrumentation stack if it |
| 635 | // sees an instrumentation exit pc, so the JIT code of methods in that stack |
| 636 | // must have been seen. We sanity check this below. |
| 637 | for (const instrumentation::InstrumentationStackFrame& frame |
| 638 | : *thread->GetInstrumentationStack()) { |
| 639 | // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in |
| 640 | // its stack frame, it is not the method owning return_pc_. We just pass null to |
| 641 | // LookupMethodHeader: the method is only checked against in debug builds. |
| 642 | OatQuickMethodHeader* method_header = |
| 643 | code_cache_->LookupMethodHeader(frame.return_pc_, nullptr); |
| 644 | if (method_header != nullptr) { |
| 645 | const void* code = method_header->GetCode(); |
| 646 | CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code))); |
| 647 | } |
| 648 | } |
| 649 | } |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 650 | barrier_->Pass(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 651 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 652 | |
| 653 | private: |
| 654 | JitCodeCache* const code_cache_; |
| 655 | Barrier* const barrier_; |
| 656 | }; |
| 657 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 658 | void JitCodeCache::NotifyCollectionDone(Thread* self) { |
| 659 | collection_in_progress_ = false; |
| 660 | lock_cond_.Broadcast(self); |
| 661 | } |
| 662 | |
| 663 | void JitCodeCache::SetFootprintLimit(size_t new_footprint) { |
| 664 | size_t per_space_footprint = new_footprint / 2; |
| 665 | DCHECK(IsAlignedParam(per_space_footprint, kPageSize)); |
| 666 | DCHECK_EQ(per_space_footprint * 2, new_footprint); |
| 667 | mspace_set_footprint_limit(data_mspace_, per_space_footprint); |
| 668 | { |
| 669 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 670 | mspace_set_footprint_limit(code_mspace_, per_space_footprint); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | bool JitCodeCache::IncreaseCodeCacheCapacity() { |
| 675 | if (current_capacity_ == max_capacity_) { |
| 676 | return false; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 679 | // Double the capacity if we're below 1MB, or increase it by 1MB if |
| 680 | // we're above. |
| 681 | if (current_capacity_ < 1 * MB) { |
| 682 | current_capacity_ *= 2; |
| 683 | } else { |
| 684 | current_capacity_ += 1 * MB; |
| 685 | } |
| 686 | if (current_capacity_ > max_capacity_) { |
| 687 | current_capacity_ = max_capacity_; |
| 688 | } |
| 689 | |
| 690 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 691 | LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_); |
| 692 | } |
| 693 | |
| 694 | SetFootprintLimit(current_capacity_); |
| 695 | |
| 696 | return true; |
| 697 | } |
| 698 | |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 699 | void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) { |
| 700 | Barrier barrier(0); |
| 701 | size_t threads_running_checkpoint = 0; |
| 702 | MarkCodeClosure closure(this, &barrier); |
| 703 | threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure); |
| 704 | // Now that we have run our checkpoint, move to a suspended state and wait |
| 705 | // for other threads to run the checkpoint. |
| 706 | ScopedThreadSuspension sts(self, kSuspended); |
| 707 | if (threads_running_checkpoint != 0) { |
| 708 | barrier.Increment(self, threads_running_checkpoint); |
| 709 | } |
| 710 | } |
| 711 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 712 | bool JitCodeCache::ShouldDoFullCollection() { |
| 713 | if (current_capacity_ == max_capacity_) { |
| 714 | // Always do a full collection when the code cache is full. |
| 715 | return true; |
| 716 | } else if (current_capacity_ < kReservedCapacity) { |
| 717 | // Always do partial collection when the code cache size is below the reserved |
| 718 | // capacity. |
| 719 | return false; |
| 720 | } else if (last_collection_increased_code_cache_) { |
| 721 | // This time do a full collection. |
| 722 | return true; |
| 723 | } else { |
| 724 | // This time do a partial collection. |
| 725 | return false; |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 729 | void JitCodeCache::GarbageCollectCache(Thread* self) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 730 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 731 | if (!garbage_collect_code_) { |
| 732 | MutexLock mu(self, lock_); |
| 733 | IncreaseCodeCacheCapacity(); |
| 734 | return; |
| 735 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 736 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 737 | // Wait for an existing collection, or let everyone know we are starting one. |
| 738 | { |
| 739 | ScopedThreadSuspension sts(self, kSuspended); |
| 740 | MutexLock mu(self, lock_); |
| 741 | if (WaitForPotentialCollectionToComplete(self)) { |
| 742 | return; |
| 743 | } else { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 744 | number_of_collections_++; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 745 | live_bitmap_.reset(CodeCacheBitmap::Create( |
| 746 | "code-cache-bitmap", |
| 747 | reinterpret_cast<uintptr_t>(code_map_->Begin()), |
| 748 | reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2))); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 749 | collection_in_progress_ = true; |
| 750 | } |
| 751 | } |
| 752 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 753 | TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit)); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 754 | { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 755 | TimingLogger::ScopedTiming st("Code cache collection", &logger); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 756 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 757 | bool do_full_collection = false; |
| 758 | { |
| 759 | MutexLock mu(self, lock_); |
| 760 | do_full_collection = ShouldDoFullCollection(); |
Nicolas Geoffray | a96917a | 2016-03-01 22:18:02 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 763 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 764 | LOG(INFO) << "Do " |
| 765 | << (do_full_collection ? "full" : "partial") |
| 766 | << " code cache collection, code=" |
| 767 | << PrettySize(CodeCacheSize()) |
| 768 | << ", data=" << PrettySize(DataCacheSize()); |
| 769 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 770 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 771 | DoCollection(self, /* collect_profiling_info */ do_full_collection); |
| 772 | |
| 773 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 774 | LOG(INFO) << "After code cache collection, code=" |
| 775 | << PrettySize(CodeCacheSize()) |
| 776 | << ", data=" << PrettySize(DataCacheSize()); |
| 777 | } |
| 778 | |
| 779 | { |
| 780 | MutexLock mu(self, lock_); |
| 781 | |
| 782 | // Increase the code cache only when we do partial collections. |
| 783 | // TODO: base this strategy on how full the code cache is? |
| 784 | if (do_full_collection) { |
| 785 | last_collection_increased_code_cache_ = false; |
| 786 | } else { |
| 787 | last_collection_increased_code_cache_ = true; |
| 788 | IncreaseCodeCacheCapacity(); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 791 | bool next_collection_will_be_full = ShouldDoFullCollection(); |
| 792 | |
| 793 | // Start polling the liveness of compiled code to prepare for the next full collection. |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 794 | if (next_collection_will_be_full) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 795 | // Save the entry point of methods we have compiled, and update the entry |
| 796 | // point of those methods to the interpreter. If the method is invoked, the |
| 797 | // interpreter will update its entry point to the compiled code and call it. |
| 798 | for (ProfilingInfo* info : profiling_infos_) { |
| 799 | const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
| 800 | if (ContainsPc(entry_point)) { |
| 801 | info->SetSavedEntryPoint(entry_point); |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 802 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 803 | info->GetMethod(), GetQuickToInterpreterBridge()); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
| 807 | DCHECK(CheckLiveCompiledCodeHasProfilingInfo()); |
| 808 | } |
| 809 | live_bitmap_.reset(nullptr); |
| 810 | NotifyCollectionDone(self); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 811 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 812 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 813 | Runtime::Current()->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 816 | void JitCodeCache::RemoveUnmarkedCode(Thread* self) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 817 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 818 | MutexLock mu(self, lock_); |
| 819 | ScopedCodeCacheWrite scc(code_map_.get()); |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 820 | // Iterate over all compiled code and remove entries that are not marked. |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 821 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 822 | const void* code_ptr = it->first; |
| 823 | ArtMethod* method = it->second; |
| 824 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 825 | if (GetLiveBitmap()->Test(allocation)) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 826 | ++it; |
| 827 | } else { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 828 | FreeCode(code_ptr, method); |
| 829 | it = method_code_map_.erase(it); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 835 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 836 | { |
| 837 | MutexLock mu(self, lock_); |
| 838 | if (collect_profiling_info) { |
| 839 | // Clear the profiling info of methods that do not have compiled code as entrypoint. |
| 840 | // Also remove the saved entry point from the ProfilingInfo objects. |
| 841 | for (ProfilingInfo* info : profiling_infos_) { |
| 842 | const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 843 | if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 844 | info->GetMethod()->SetProfilingInfo(nullptr); |
| 845 | } |
Nicolas Geoffray | b9a639d | 2016-03-22 11:25:20 +0000 | [diff] [blame] | 846 | |
| 847 | if (info->GetSavedEntryPoint() != nullptr) { |
| 848 | info->SetSavedEntryPoint(nullptr); |
| 849 | // We are going to move this method back to interpreter. Clear the counter now to |
| 850 | // give it a chance to be hot again. |
| 851 | info->GetMethod()->ClearCounter(); |
| 852 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 853 | } |
| 854 | } else if (kIsDebugBuild) { |
| 855 | // Sanity check that the profiling infos do not have a dangling entry point. |
| 856 | for (ProfilingInfo* info : profiling_infos_) { |
| 857 | DCHECK(info->GetSavedEntryPoint() == nullptr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 858 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 859 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 860 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 861 | // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not |
| 862 | // an entry point is either: |
| 863 | // - an osr compiled code, that will be removed if not in a thread call stack. |
| 864 | // - discarded compiled code, that will be removed if not in a thread call stack. |
| 865 | for (const auto& it : method_code_map_) { |
| 866 | ArtMethod* method = it.second; |
| 867 | const void* code_ptr = it.first; |
| 868 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 869 | if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) { |
| 870 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 871 | } |
| 872 | } |
| 873 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 874 | // 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] | 875 | // on thread stacks). |
| 876 | osr_code_map_.clear(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | // 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] | 880 | MarkCompiledCodeOnThreadStacks(self); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 881 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 882 | // At this point, mutator threads are still running, and entrypoints of methods can |
| 883 | // change. We do know they cannot change to a code cache entry that is not marked, |
| 884 | // therefore we can safely remove those entries. |
| 885 | RemoveUnmarkedCode(self); |
Nicolas Geoffray | a96917a | 2016-03-01 22:18:02 +0000 | [diff] [blame] | 886 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 887 | if (collect_profiling_info) { |
Nicolas Geoffray | cf48fa0 | 2016-07-30 22:49:11 +0100 | [diff] [blame] | 888 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 889 | MutexLock mu(self, lock_); |
| 890 | // Free all profiling infos of methods not compiled nor being compiled. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 891 | 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] | 892 | [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 893 | const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | 511e41b | 2016-03-02 17:09:35 +0000 | [diff] [blame] | 894 | // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope |
| 895 | // that the compiled code would not get revived. As mutator threads run concurrently, |
| 896 | // they may have revived the compiled code, and now we are in the situation where |
| 897 | // a method has compiled code but no ProfilingInfo. |
| 898 | // We make sure compiled methods have a ProfilingInfo object. It is needed for |
| 899 | // code cache collection. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 900 | if (ContainsPc(ptr) && |
| 901 | info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 902 | info->GetMethod()->SetProfilingInfo(info); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 903 | } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 904 | // No need for this ProfilingInfo object anymore. |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 905 | FreeData(reinterpret_cast<uint8_t*>(info)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 906 | return true; |
| 907 | } |
| 908 | return false; |
| 909 | }); |
| 910 | profiling_infos_.erase(profiling_kept_end, profiling_infos_.end()); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 911 | DCHECK(CheckLiveCompiledCodeHasProfilingInfo()); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 912 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 915 | bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 916 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 917 | // Check that methods we have compiled do have a ProfilingInfo object. We would |
| 918 | // have memory leaks of compiled code otherwise. |
| 919 | for (const auto& it : method_code_map_) { |
| 920 | ArtMethod* method = it.second; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 921 | if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 922 | const void* code_ptr = it.first; |
| 923 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 924 | if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) { |
| 925 | // If the code is not dead, then we have a problem. Note that this can even |
| 926 | // happen just after a collection, as mutator threads are running in parallel |
| 927 | // and could deoptimize an existing compiled code. |
| 928 | return false; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | return true; |
| 933 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 934 | |
| 935 | OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) { |
| 936 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 937 | if (kRuntimeISA == kArm) { |
| 938 | // On Thumb-2, the pc is offset by one. |
| 939 | --pc; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 940 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 941 | if (!ContainsPc(reinterpret_cast<const void*>(pc))) { |
| 942 | return nullptr; |
| 943 | } |
| 944 | |
| 945 | MutexLock mu(Thread::Current(), lock_); |
| 946 | if (method_code_map_.empty()) { |
| 947 | return nullptr; |
| 948 | } |
| 949 | auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc)); |
| 950 | --it; |
| 951 | |
| 952 | const void* code_ptr = it->first; |
| 953 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 954 | if (!method_header->Contains(pc)) { |
| 955 | return nullptr; |
| 956 | } |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 957 | if (kIsDebugBuild && method != nullptr) { |
| 958 | DCHECK_EQ(it->second, method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 959 | << ArtMethod::PrettyMethod(method) << " " << ArtMethod::PrettyMethod(it->second) << " " |
| 960 | << std::hex << pc; |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 961 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 962 | return method_header; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 965 | OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) { |
| 966 | MutexLock mu(Thread::Current(), lock_); |
| 967 | auto it = osr_code_map_.find(method); |
| 968 | if (it == osr_code_map_.end()) { |
| 969 | return nullptr; |
| 970 | } |
| 971 | return OatQuickMethodHeader::FromCodePointer(it->second); |
| 972 | } |
| 973 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 974 | ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self, |
| 975 | ArtMethod* method, |
| 976 | const std::vector<uint32_t>& entries, |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 977 | bool retry_allocation) |
| 978 | // No thread safety analysis as we are using TryLock/Unlock explicitly. |
| 979 | NO_THREAD_SAFETY_ANALYSIS { |
| 980 | ProfilingInfo* info = nullptr; |
| 981 | if (!retry_allocation) { |
| 982 | // If we are allocating for the interpreter, just try to lock, to avoid |
| 983 | // lock contention with the JIT. |
| 984 | if (lock_.ExclusiveTryLock(self)) { |
| 985 | info = AddProfilingInfoInternal(self, method, entries); |
| 986 | lock_.ExclusiveUnlock(self); |
| 987 | } |
| 988 | } else { |
| 989 | { |
| 990 | MutexLock mu(self, lock_); |
| 991 | info = AddProfilingInfoInternal(self, method, entries); |
| 992 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 993 | |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 994 | if (info == nullptr) { |
| 995 | GarbageCollectCache(self); |
| 996 | MutexLock mu(self, lock_); |
| 997 | info = AddProfilingInfoInternal(self, method, entries); |
| 998 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 999 | } |
| 1000 | return info; |
| 1001 | } |
| 1002 | |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1003 | ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED, |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1004 | ArtMethod* method, |
| 1005 | const std::vector<uint32_t>& entries) { |
| 1006 | size_t profile_info_size = RoundUp( |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1007 | sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(), |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1008 | sizeof(void*)); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1009 | |
| 1010 | // Check whether some other thread has concurrently created it. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1011 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1012 | if (info != nullptr) { |
| 1013 | return info; |
| 1014 | } |
| 1015 | |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 1016 | uint8_t* data = AllocateData(profile_info_size); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1017 | if (data == nullptr) { |
| 1018 | return nullptr; |
| 1019 | } |
| 1020 | info = new (data) ProfilingInfo(method, entries); |
Nicolas Geoffray | 07f3564 | 2016-01-04 16:06:51 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Make sure other threads see the data in the profiling info object before the |
| 1023 | // store in the ArtMethod's ProfilingInfo pointer. |
| 1024 | QuasiAtomic::ThreadFenceRelease(); |
| 1025 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1026 | method->SetProfilingInfo(info); |
| 1027 | profiling_infos_.push_back(info); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1028 | histogram_profiling_info_memory_use_.AddValue(profile_info_size); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1029 | return info; |
| 1030 | } |
| 1031 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1032 | // NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock |
| 1033 | // is already held. |
| 1034 | void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS { |
| 1035 | if (code_mspace_ == mspace) { |
| 1036 | size_t result = code_end_; |
| 1037 | code_end_ += increment; |
| 1038 | return reinterpret_cast<void*>(result + code_map_->Begin()); |
| 1039 | } else { |
| 1040 | DCHECK_EQ(data_mspace_, mspace); |
| 1041 | size_t result = data_end_; |
| 1042 | data_end_ += increment; |
| 1043 | return reinterpret_cast<void*>(result + data_map_->Begin()); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 1047 | void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations, |
| 1048 | std::vector<MethodReference>& methods) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1049 | ScopedTrace trace(__FUNCTION__); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1050 | MutexLock mu(Thread::Current(), lock_); |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 1051 | for (const ProfilingInfo* info : profiling_infos_) { |
| 1052 | ArtMethod* method = info->GetMethod(); |
| 1053 | const DexFile* dex_file = method->GetDexFile(); |
| 1054 | if (ContainsElement(dex_base_locations, dex_file->GetBaseLocation())) { |
| 1055 | methods.emplace_back(dex_file, method->GetDexMethodIndex()); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 1060 | uint64_t JitCodeCache::GetLastUpdateTimeNs() const { |
| 1061 | return last_update_time_ns_.LoadAcquire(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1062 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1063 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 1064 | bool JitCodeCache::IsOsrCompiled(ArtMethod* method) { |
| 1065 | MutexLock mu(Thread::Current(), lock_); |
| 1066 | return osr_code_map_.find(method) != osr_code_map_.end(); |
| 1067 | } |
| 1068 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1069 | bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) { |
| 1070 | if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1071 | return false; |
| 1072 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1073 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1074 | MutexLock mu(self, lock_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1075 | if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) { |
| 1076 | return false; |
| 1077 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1078 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1079 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1080 | if (info == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1081 | VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled"; |
Nicolas Geoffray | b9a639d | 2016-03-22 11:25:20 +0000 | [diff] [blame] | 1082 | // Because the counter is not atomic, there are some rare cases where we may not |
| 1083 | // hit the threshold for creating the ProfilingInfo. Reset the counter now to |
| 1084 | // "correct" this. |
| 1085 | method->ClearCounter(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1086 | return false; |
| 1087 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1088 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1089 | if (info->IsMethodBeingCompiled(osr)) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1090 | return false; |
| 1091 | } |
| 1092 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1093 | info->SetIsMethodBeingCompiled(true, osr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1094 | return true; |
| 1095 | } |
| 1096 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1097 | ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1098 | MutexLock mu(self, lock_); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1099 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1100 | if (info != nullptr) { |
| 1101 | info->IncrementInlineUse(); |
| 1102 | } |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1103 | return info; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1106 | void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1107 | MutexLock mu(self, lock_); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1108 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1109 | DCHECK(info != nullptr); |
| 1110 | info->DecrementInlineUse(); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1113 | void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED, bool osr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1114 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1115 | DCHECK(info->IsMethodBeingCompiled(osr)); |
| 1116 | info->SetIsMethodBeingCompiled(false, osr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 1119 | size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) { |
| 1120 | MutexLock mu(Thread::Current(), lock_); |
| 1121 | return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr))); |
| 1122 | } |
| 1123 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1124 | void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method, |
| 1125 | const OatQuickMethodHeader* header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1126 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1127 | if ((profiling_info != nullptr) && |
| 1128 | (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) { |
| 1129 | // Prevent future uses of the compiled code. |
| 1130 | profiling_info->SetSavedEntryPoint(nullptr); |
| 1131 | } |
| 1132 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1133 | if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) { |
| 1134 | // The entrypoint is the one to invalidate, so we just update |
| 1135 | // it to the interpreter entry point and clear the counter to get the method |
| 1136 | // Jitted again. |
| 1137 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 1138 | method, GetQuickToInterpreterBridge()); |
| 1139 | method->ClearCounter(); |
| 1140 | } else { |
| 1141 | MutexLock mu(Thread::Current(), lock_); |
| 1142 | auto it = osr_code_map_.find(method); |
| 1143 | if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) { |
| 1144 | // Remove the OSR method, to avoid using it again. |
| 1145 | osr_code_map_.erase(it); |
| 1146 | } |
| 1147 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1148 | MutexLock mu(Thread::Current(), lock_); |
| 1149 | number_of_deoptimizations_++; |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 1152 | uint8_t* JitCodeCache::AllocateCode(size_t code_size) { |
| 1153 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 1154 | uint8_t* result = reinterpret_cast<uint8_t*>( |
| 1155 | mspace_memalign(code_mspace_, alignment, code_size)); |
| 1156 | size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 1157 | // Ensure the header ends up at expected instruction alignment. |
| 1158 | DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment); |
| 1159 | used_memory_for_code_ += mspace_usable_size(result); |
| 1160 | return result; |
| 1161 | } |
| 1162 | |
| 1163 | void JitCodeCache::FreeCode(uint8_t* code) { |
| 1164 | used_memory_for_code_ -= mspace_usable_size(code); |
| 1165 | mspace_free(code_mspace_, code); |
| 1166 | } |
| 1167 | |
| 1168 | uint8_t* JitCodeCache::AllocateData(size_t data_size) { |
| 1169 | void* result = mspace_malloc(data_mspace_, data_size); |
| 1170 | used_memory_for_data_ += mspace_usable_size(result); |
| 1171 | return reinterpret_cast<uint8_t*>(result); |
| 1172 | } |
| 1173 | |
| 1174 | void JitCodeCache::FreeData(uint8_t* data) { |
| 1175 | used_memory_for_data_ -= mspace_usable_size(data); |
| 1176 | mspace_free(data_mspace_, data); |
| 1177 | } |
| 1178 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1179 | void JitCodeCache::Dump(std::ostream& os) { |
| 1180 | MutexLock mu(Thread::Current(), lock_); |
| 1181 | os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n" |
| 1182 | << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n" |
| 1183 | << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n" |
| 1184 | << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n" |
| 1185 | << "Total number of JIT compilations: " << number_of_compilations_ << "\n" |
| 1186 | << "Total number of JIT compilations for on stack replacement: " |
| 1187 | << number_of_osr_compilations_ << "\n" |
| 1188 | << "Total number of deoptimizations: " << number_of_deoptimizations_ << "\n" |
| 1189 | << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl; |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1190 | histogram_stack_map_memory_use_.PrintMemoryUse(os); |
| 1191 | histogram_code_memory_use_.PrintMemoryUse(os); |
| 1192 | histogram_profiling_info_memory_use_.PrintMemoryUse(os); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1195 | } // namespace jit |
| 1196 | } // namespace art |