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" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 26 | #include "cha.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 27 | #include "debugger_interface.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 28 | #include "entrypoints/runtime_asm_entrypoints.h" |
| 29 | #include "gc/accounting/bitmap-inl.h" |
Nicolas Geoffray | cf48fa0 | 2016-07-30 22:49:11 +0100 | [diff] [blame] | 30 | #include "gc/scoped_gc_critical_section.h" |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 31 | #include "jit/jit.h" |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 32 | #include "jit/profiling_info.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 33 | #include "linear_alloc.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "mem_map.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | #include "oat_file-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 36 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 37 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | namespace jit { |
| 41 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 42 | static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 43 | static constexpr int kProtData = PROT_READ | PROT_WRITE; |
| 44 | static constexpr int kProtCode = PROT_READ | PROT_EXEC; |
| 45 | |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 46 | static constexpr size_t kCodeSizeLogThreshold = 50 * KB; |
| 47 | static constexpr size_t kStackMapSizeLogThreshold = 50 * KB; |
| 48 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 49 | #define CHECKED_MPROTECT(memory, size, prot) \ |
| 50 | do { \ |
| 51 | int rc = mprotect(memory, size, prot); \ |
| 52 | if (UNLIKELY(rc != 0)) { \ |
| 53 | errno = rc; \ |
| 54 | PLOG(FATAL) << "Failed to mprotect jit code cache"; \ |
| 55 | } \ |
| 56 | } while (false) \ |
| 57 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 58 | JitCodeCache* JitCodeCache::Create(size_t initial_capacity, |
| 59 | size_t max_capacity, |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 60 | bool generate_debug_info, |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 61 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 62 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 63 | CHECK_GE(max_capacity, initial_capacity); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 64 | |
| 65 | // Generating debug information is mostly for using the 'perf' tool, which does |
| 66 | // not work with ashmem. |
| 67 | bool use_ashmem = !generate_debug_info; |
| 68 | // With 'perf', we want a 1-1 mapping between an address and a method. |
| 69 | bool garbage_collect_code = !generate_debug_info; |
| 70 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 71 | // We need to have 32 bit offsets from method headers in code cache which point to things |
| 72 | // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work. |
| 73 | // Ensure we're below 1 GB to be safe. |
| 74 | if (max_capacity > 1 * GB) { |
| 75 | std::ostringstream oss; |
| 76 | oss << "Maxium code cache capacity is limited to 1 GB, " |
| 77 | << PrettySize(max_capacity) << " is too big"; |
| 78 | *error_msg = oss.str(); |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 82 | std::string error_str; |
| 83 | // Map name specific for android_os_Debug.cpp accounting. |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 84 | // Map in low 4gb to simplify accessing root tables for x86_64. |
| 85 | // We could do PC-relative addressing to avoid this problem, but that |
| 86 | // would require reserving code and data area before submitting, which |
| 87 | // means more windows for the code memory to be RWX. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 88 | MemMap* data_map = MemMap::MapAnonymous( |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 89 | "data-code-cache", nullptr, |
| 90 | max_capacity, |
| 91 | kProtAll, |
| 92 | /* low_4gb */ true, |
| 93 | /* reuse */ false, |
| 94 | &error_str, |
| 95 | use_ashmem); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 96 | if (data_map == nullptr) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 97 | std::ostringstream oss; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 98 | 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] | 99 | *error_msg = oss.str(); |
| 100 | return nullptr; |
| 101 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 102 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 103 | // Align both capacities to page size, as that's the unit mspaces use. |
| 104 | initial_capacity = RoundDown(initial_capacity, 2 * kPageSize); |
| 105 | max_capacity = RoundDown(max_capacity, 2 * kPageSize); |
| 106 | |
Nicolas Geoffray | 4e915fb | 2015-10-28 17:39:47 +0000 | [diff] [blame] | 107 | // Data cache is 1 / 2 of the map. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 108 | // TODO: Make this variable? |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 109 | size_t data_size = max_capacity / 2; |
| 110 | size_t code_size = max_capacity - data_size; |
| 111 | DCHECK_EQ(code_size + data_size, max_capacity); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 112 | uint8_t* divider = data_map->Begin() + data_size; |
| 113 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 114 | MemMap* code_map = |
| 115 | data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str, use_ashmem); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 116 | if (code_map == nullptr) { |
| 117 | std::ostringstream oss; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 118 | 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] | 119 | *error_msg = oss.str(); |
| 120 | return nullptr; |
| 121 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 122 | DCHECK_EQ(code_map->Begin(), divider); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 123 | data_size = initial_capacity / 2; |
| 124 | code_size = initial_capacity - data_size; |
| 125 | DCHECK_EQ(code_size + data_size, initial_capacity); |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 126 | return new JitCodeCache( |
Nicolas Geoffray | c3fec4c | 2016-01-14 16:16:35 +0000 | [diff] [blame] | 127 | 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] | 128 | } |
| 129 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 130 | JitCodeCache::JitCodeCache(MemMap* code_map, |
| 131 | MemMap* data_map, |
| 132 | size_t initial_code_capacity, |
| 133 | size_t initial_data_capacity, |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 134 | size_t max_capacity, |
| 135 | bool garbage_collect_code) |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 136 | : lock_("Jit code cache", kJitCodeCacheLock), |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 137 | lock_cond_("Jit code cache condition variable", lock_), |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 138 | collection_in_progress_(false), |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 139 | code_map_(code_map), |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 140 | data_map_(data_map), |
| 141 | max_capacity_(max_capacity), |
| 142 | current_capacity_(initial_code_capacity + initial_data_capacity), |
| 143 | code_end_(initial_code_capacity), |
| 144 | data_end_(initial_data_capacity), |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 145 | last_collection_increased_code_cache_(false), |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 146 | last_update_time_ns_(0), |
Nicolas Geoffray | 0a52223 | 2016-01-19 09:34:58 +0000 | [diff] [blame] | 147 | garbage_collect_code_(garbage_collect_code), |
Nicolas Geoffray | b0d2208 | 2016-02-24 17:18:25 +0000 | [diff] [blame] | 148 | used_memory_for_data_(0), |
| 149 | used_memory_for_code_(0), |
Nicolas Geoffray | fcdd729 | 2016-02-25 13:27:47 +0000 | [diff] [blame] | 150 | number_of_compilations_(0), |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 151 | number_of_osr_compilations_(0), |
| 152 | number_of_deoptimizations_(0), |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 153 | number_of_collections_(0), |
| 154 | histogram_stack_map_memory_use_("Memory used for stack maps", 16), |
| 155 | histogram_code_memory_use_("Memory used for compiled code", 16), |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 156 | histogram_profiling_info_memory_use_("Memory used for profiling info", 16), |
| 157 | is_weak_access_enabled_(true), |
| 158 | inline_cache_cond_("Jit inline cache condition variable", lock_) { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 159 | |
Nicolas Geoffray | c3fec4c | 2016-01-14 16:16:35 +0000 | [diff] [blame] | 160 | DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 161 | code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/); |
| 162 | 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] | 163 | |
| 164 | if (code_mspace_ == nullptr || data_mspace_ == nullptr) { |
| 165 | PLOG(FATAL) << "create_mspace_with_base failed"; |
| 166 | } |
| 167 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 168 | SetFootprintLimit(current_capacity_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 169 | |
| 170 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode); |
| 171 | CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 172 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 173 | VLOG(jit) << "Created jit code cache: initial data size=" |
| 174 | << PrettySize(initial_data_capacity) |
| 175 | << ", initial code size=" |
| 176 | << PrettySize(initial_code_capacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 179 | bool JitCodeCache::ContainsPc(const void* ptr) const { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 180 | return code_map_->Begin() <= ptr && ptr < code_map_->End(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 183 | bool JitCodeCache::ContainsMethod(ArtMethod* method) { |
| 184 | MutexLock mu(Thread::Current(), lock_); |
| 185 | for (auto& it : method_code_map_) { |
| 186 | if (it.second == method) { |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Mathieu Chartier | 33fbf37 | 2016-03-07 13:48:08 -0800 | [diff] [blame] | 193 | class ScopedCodeCacheWrite : ScopedTrace { |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 194 | public: |
Mathieu Chartier | 33fbf37 | 2016-03-07 13:48:08 -0800 | [diff] [blame] | 195 | explicit ScopedCodeCacheWrite(MemMap* code_map) |
| 196 | : ScopedTrace("ScopedCodeCacheWrite"), |
| 197 | code_map_(code_map) { |
| 198 | ScopedTrace trace("mprotect all"); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 199 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 200 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 201 | ~ScopedCodeCacheWrite() { |
Mathieu Chartier | 33fbf37 | 2016-03-07 13:48:08 -0800 | [diff] [blame] | 202 | ScopedTrace trace("mprotect code"); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 203 | CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode); |
| 204 | } |
| 205 | private: |
| 206 | MemMap* const code_map_; |
| 207 | |
| 208 | DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite); |
| 209 | }; |
| 210 | |
| 211 | uint8_t* JitCodeCache::CommitCode(Thread* self, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 212 | ArtMethod* method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 213 | uint8_t* stack_map, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 214 | uint8_t* method_info, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 215 | uint8_t* roots_data, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 216 | size_t frame_size_in_bytes, |
| 217 | size_t core_spill_mask, |
| 218 | size_t fp_spill_mask, |
| 219 | const uint8_t* code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 220 | size_t code_size, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 221 | size_t data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 222 | bool osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 223 | Handle<mirror::ObjectArray<mirror::Object>> roots, |
| 224 | bool has_should_deoptimize_flag, |
| 225 | const ArenaSet<ArtMethod*>& cha_single_implementation_list) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 226 | uint8_t* result = CommitCodeInternal(self, |
| 227 | method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 228 | stack_map, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 229 | method_info, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 230 | roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 231 | frame_size_in_bytes, |
| 232 | core_spill_mask, |
| 233 | fp_spill_mask, |
| 234 | code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 235 | code_size, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 236 | data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 237 | osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 238 | roots, |
| 239 | has_should_deoptimize_flag, |
| 240 | cha_single_implementation_list); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 241 | if (result == nullptr) { |
| 242 | // Retry. |
| 243 | GarbageCollectCache(self); |
| 244 | result = CommitCodeInternal(self, |
| 245 | method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 246 | stack_map, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 247 | method_info, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 248 | roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 249 | frame_size_in_bytes, |
| 250 | core_spill_mask, |
| 251 | fp_spill_mask, |
| 252 | code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 253 | code_size, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 254 | data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 255 | osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 256 | roots, |
| 257 | has_should_deoptimize_flag, |
| 258 | cha_single_implementation_list); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 259 | } |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) { |
| 264 | bool in_collection = false; |
| 265 | while (collection_in_progress_) { |
| 266 | in_collection = true; |
| 267 | lock_cond_.Wait(self); |
| 268 | } |
| 269 | return in_collection; |
| 270 | } |
| 271 | |
| 272 | static uintptr_t FromCodeToAllocation(const void* code) { |
| 273 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 274 | return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 275 | } |
| 276 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 277 | static uint32_t ComputeRootTableSize(uint32_t number_of_roots) { |
| 278 | return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>); |
| 279 | } |
| 280 | |
| 281 | static uint32_t GetNumberOfRoots(const uint8_t* stack_map) { |
| 282 | // The length of the table is stored just before the stack map (and therefore at the end of |
| 283 | // the table itself), in order to be able to fetch it from a `stack_map` pointer. |
| 284 | return reinterpret_cast<const uint32_t*>(stack_map)[-1]; |
| 285 | } |
| 286 | |
Mathieu Chartier | 7a704be | 2016-11-22 13:24:40 -0800 | [diff] [blame] | 287 | static void FillRootTableLength(uint8_t* roots_data, uint32_t length) { |
| 288 | // Store the length of the table at the end. This will allow fetching it from a `stack_map` |
| 289 | // pointer. |
| 290 | reinterpret_cast<uint32_t*>(roots_data)[length] = length; |
| 291 | } |
| 292 | |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 293 | static const uint8_t* FromStackMapToRoots(const uint8_t* stack_map_data) { |
| 294 | return stack_map_data - ComputeRootTableSize(GetNumberOfRoots(stack_map_data)); |
| 295 | } |
| 296 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 297 | static void FillRootTable(uint8_t* roots_data, Handle<mirror::ObjectArray<mirror::Object>> roots) |
| 298 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 299 | GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
Mathieu Chartier | 7a704be | 2016-11-22 13:24:40 -0800 | [diff] [blame] | 300 | const uint32_t length = roots->GetLength(); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 301 | // Put all roots in `roots_data`. |
| 302 | for (uint32_t i = 0; i < length; ++i) { |
| 303 | ObjPtr<mirror::Object> object = roots->Get(i); |
| 304 | if (kIsDebugBuild) { |
| 305 | // Ensure the string is strongly interned. b/32995596 |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 306 | if (object->IsString()) { |
| 307 | ObjPtr<mirror::String> str = reinterpret_cast<mirror::String*>(object.Ptr()); |
| 308 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 309 | CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr); |
| 310 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 311 | } |
| 312 | gc_roots[i] = GcRoot<mirror::Object>(object); |
| 313 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) { |
| 317 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 318 | uint8_t* data = method_header->GetOptimizedCodeInfoPtr(); |
| 319 | uint32_t roots = GetNumberOfRoots(data); |
| 320 | if (number_of_roots != nullptr) { |
| 321 | *number_of_roots = roots; |
| 322 | } |
| 323 | return data - ComputeRootTableSize(roots); |
| 324 | } |
| 325 | |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 326 | // Helper for the GC to process a weak class in a JIT root table. |
| 327 | static inline void ProcessWeakClass(GcRoot<mirror::Class>* root_ptr, IsMarkedVisitor* visitor) |
| 328 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 329 | // This does not need a read barrier because this is called by GC. |
| 330 | mirror::Class* cls = root_ptr->Read<kWithoutReadBarrier>(); |
| 331 | if (cls != nullptr) { |
| 332 | DCHECK((cls->IsClass<kDefaultVerifyFlags, kWithoutReadBarrier>())); |
| 333 | // Look at the classloader of the class to know if it has been unloaded. |
| 334 | // This does not need a read barrier because this is called by GC. |
| 335 | mirror::Object* class_loader = |
| 336 | cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 337 | if (class_loader == nullptr || visitor->IsMarked(class_loader) != nullptr) { |
| 338 | // The class loader is live, update the entry if the class has moved. |
| 339 | mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls)); |
| 340 | // Note that new_object can be null for CMS and newly allocated objects. |
| 341 | if (new_cls != nullptr && new_cls != cls) { |
| 342 | *root_ptr = GcRoot<mirror::Class>(new_cls); |
| 343 | } |
| 344 | } else { |
| 345 | // The class loader is not live, clear the entry. |
| 346 | *root_ptr = GcRoot<mirror::Class>(nullptr); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 351 | void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) { |
| 352 | MutexLock mu(Thread::Current(), lock_); |
| 353 | for (const auto& entry : method_code_map_) { |
| 354 | uint32_t number_of_roots = 0; |
| 355 | uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots); |
| 356 | GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data); |
| 357 | for (uint32_t i = 0; i < number_of_roots; ++i) { |
| 358 | // This does not need a read barrier because this is called by GC. |
| 359 | mirror::Object* object = roots[i].Read<kWithoutReadBarrier>(); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 360 | if (object == nullptr) { |
| 361 | // entry got deleted in a previous sweep. |
| 362 | } else if (object->IsString<kDefaultVerifyFlags, kWithoutReadBarrier>()) { |
| 363 | mirror::Object* new_object = visitor->IsMarked(object); |
| 364 | // We know the string is marked because it's a strongly-interned string that |
| 365 | // is always alive. The IsMarked implementation of the CMS collector returns |
| 366 | // null for newly allocated objects, but we know those haven't moved. Therefore, |
| 367 | // only update the entry if we get a different non-null string. |
| 368 | // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method |
| 369 | // out of the weak access/creation pause. b/32167580 |
| 370 | if (new_object != nullptr && new_object != object) { |
| 371 | DCHECK(new_object->IsString()); |
| 372 | roots[i] = GcRoot<mirror::Object>(new_object); |
| 373 | } |
| 374 | } else { |
| 375 | ProcessWeakClass(reinterpret_cast<GcRoot<mirror::Class>*>(&roots[i]), visitor); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | } |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 379 | // Walk over inline caches to clear entries containing unloaded classes. |
| 380 | for (ProfilingInfo* info : profiling_infos_) { |
| 381 | for (size_t i = 0; i < info->number_of_inline_caches_; ++i) { |
| 382 | InlineCache* cache = &info->cache_[i]; |
| 383 | for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) { |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 384 | ProcessWeakClass(&cache->classes_[j], visitor); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 390 | void JitCodeCache::FreeCode(const void* code_ptr) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 391 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 392 | // Notify native debugger that we are about to remove the code. |
| 393 | // It does nothing if we are not using native debugger. |
| 394 | DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr)); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 395 | FreeData(GetRootTable(code_ptr)); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 396 | FreeCode(reinterpret_cast<uint8_t*>(allocation)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 399 | void JitCodeCache::FreeAllMethodHeaders( |
| 400 | const std::unordered_set<OatQuickMethodHeader*>& method_headers) { |
| 401 | { |
| 402 | MutexLock mu(Thread::Current(), *Locks::cha_lock_); |
| 403 | Runtime::Current()->GetClassHierarchyAnalysis() |
| 404 | ->RemoveDependentsWithMethodHeaders(method_headers); |
| 405 | } |
| 406 | |
| 407 | // We need to remove entries in method_headers from CHA dependencies |
| 408 | // first since once we do FreeCode() below, the memory can be reused |
| 409 | // so it's possible for the same method_header to start representing |
| 410 | // different compile code. |
| 411 | MutexLock mu(Thread::Current(), lock_); |
| 412 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 413 | for (const OatQuickMethodHeader* method_header : method_headers) { |
| 414 | FreeCode(method_header->GetCode()); |
| 415 | } |
| 416 | } |
| 417 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 418 | void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 419 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 420 | // We use a set to first collect all method_headers whose code need to be |
| 421 | // removed. We need to free the underlying code after we remove CHA dependencies |
| 422 | // for entries in this set. And it's more efficient to iterate through |
| 423 | // the CHA dependency map just once with an unordered_set. |
| 424 | std::unordered_set<OatQuickMethodHeader*> method_headers; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 425 | { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 426 | MutexLock mu(self, lock_); |
| 427 | // We do not check if a code cache GC is in progress, as this method comes |
| 428 | // with the classlinker_classes_lock_ held, and suspending ourselves could |
| 429 | // lead to a deadlock. |
| 430 | { |
| 431 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 432 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 433 | if (alloc.ContainsUnsafe(it->second)) { |
| 434 | method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first)); |
| 435 | it = method_code_map_.erase(it); |
| 436 | } else { |
| 437 | ++it; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) { |
| 442 | if (alloc.ContainsUnsafe(it->first)) { |
| 443 | // Note that the code has already been pushed to method_headers in the loop |
| 444 | // above and is going to be removed in FreeCode() below. |
| 445 | it = osr_code_map_.erase(it); |
| 446 | } else { |
| 447 | ++it; |
| 448 | } |
| 449 | } |
| 450 | for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) { |
| 451 | ProfilingInfo* info = *it; |
| 452 | if (alloc.ContainsUnsafe(info->GetMethod())) { |
| 453 | info->GetMethod()->SetProfilingInfo(nullptr); |
| 454 | FreeData(reinterpret_cast<uint8_t*>(info)); |
| 455 | it = profiling_infos_.erase(it); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 456 | } else { |
| 457 | ++it; |
| 458 | } |
| 459 | } |
| 460 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 461 | FreeAllMethodHeaders(method_headers); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 464 | bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const { |
| 465 | return kUseReadBarrier |
| 466 | ? self->GetWeakRefAccessEnabled() |
| 467 | : is_weak_access_enabled_.LoadSequentiallyConsistent(); |
| 468 | } |
| 469 | |
| 470 | void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) { |
| 471 | if (IsWeakAccessEnabled(self)) { |
| 472 | return; |
| 473 | } |
| 474 | ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 475 | MutexLock mu(self, lock_); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 476 | while (!IsWeakAccessEnabled(self)) { |
| 477 | inline_cache_cond_.Wait(self); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void JitCodeCache::BroadcastForInlineCacheAccess() { |
| 482 | Thread* self = Thread::Current(); |
| 483 | MutexLock mu(self, lock_); |
| 484 | inline_cache_cond_.Broadcast(self); |
| 485 | } |
| 486 | |
| 487 | void JitCodeCache::AllowInlineCacheAccess() { |
| 488 | DCHECK(!kUseReadBarrier); |
| 489 | is_weak_access_enabled_.StoreSequentiallyConsistent(true); |
| 490 | BroadcastForInlineCacheAccess(); |
| 491 | } |
| 492 | |
| 493 | void JitCodeCache::DisallowInlineCacheAccess() { |
| 494 | DCHECK(!kUseReadBarrier); |
| 495 | is_weak_access_enabled_.StoreSequentiallyConsistent(false); |
| 496 | } |
| 497 | |
| 498 | void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic, |
| 499 | Handle<mirror::ObjectArray<mirror::Class>> array) { |
| 500 | WaitUntilInlineCacheAccessible(Thread::Current()); |
| 501 | // Note that we don't need to lock `lock_` here, the compiler calling |
| 502 | // this method has already ensured the inline cache will not be deleted. |
| 503 | for (size_t in_cache = 0, in_array = 0; |
| 504 | in_cache < InlineCache::kIndividualCacheSize; |
| 505 | ++in_cache) { |
| 506 | mirror::Class* object = ic.classes_[in_cache].Read(); |
| 507 | if (object != nullptr) { |
| 508 | array->Set(in_array++, object); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 513 | uint8_t* JitCodeCache::CommitCodeInternal(Thread* self, |
| 514 | ArtMethod* method, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 515 | uint8_t* stack_map, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 516 | uint8_t* method_info, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 517 | uint8_t* roots_data, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 518 | size_t frame_size_in_bytes, |
| 519 | size_t core_spill_mask, |
| 520 | size_t fp_spill_mask, |
| 521 | const uint8_t* code, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 522 | size_t code_size, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 523 | size_t data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 524 | bool osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 525 | Handle<mirror::ObjectArray<mirror::Object>> roots, |
| 526 | bool has_should_deoptimize_flag, |
| 527 | const ArenaSet<ArtMethod*>& |
| 528 | cha_single_implementation_list) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 529 | DCHECK(stack_map != nullptr); |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 530 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 531 | // Ensure the header ends up at expected instruction alignment. |
| 532 | size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 533 | size_t total_size = header_size + code_size; |
| 534 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 535 | OatQuickMethodHeader* method_header = nullptr; |
Nicolas Geoffray | 1e7de6c | 2015-10-21 12:07:31 +0100 | [diff] [blame] | 536 | uint8_t* code_ptr = nullptr; |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 537 | uint8_t* memory = nullptr; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 538 | { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 539 | ScopedThreadSuspension sts(self, kSuspended); |
| 540 | MutexLock mu(self, lock_); |
| 541 | WaitForPotentialCollectionToComplete(self); |
| 542 | { |
| 543 | ScopedCodeCacheWrite scc(code_map_.get()); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 544 | memory = AllocateCode(total_size); |
| 545 | if (memory == nullptr) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 546 | return nullptr; |
| 547 | } |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 548 | code_ptr = memory + header_size; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 549 | |
| 550 | std::copy(code, code + code_size, code_ptr); |
| 551 | method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 552 | new (method_header) OatQuickMethodHeader( |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 553 | code_ptr - stack_map, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 554 | code_ptr - method_info, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 555 | frame_size_in_bytes, |
| 556 | core_spill_mask, |
| 557 | fp_spill_mask, |
| 558 | code_size); |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 559 | DCHECK_EQ(FromStackMapToRoots(stack_map), roots_data); |
| 560 | DCHECK_LE(roots_data, stack_map); |
| 561 | // Flush data cache, as compiled code references literals in it. |
| 562 | FlushDataCache(reinterpret_cast<char*>(roots_data), |
| 563 | reinterpret_cast<char*>(roots_data + data_size)); |
Kevin Brodsky | b93ce18 | 2016-12-15 14:23:09 +0000 | [diff] [blame] | 564 | // Flush caches before we remove write permission because some ARMv8 Qualcomm kernels may |
| 565 | // trigger a segfault if a page fault occurs when requesting a cache maintenance operation. |
| 566 | // This is a kernel bug that we need to work around until affected devices (e.g. Nexus 5X and |
| 567 | // 6P) stop being supported or their kernels are fixed. |
Artem Udovichenko | b18a669 | 2016-11-17 10:51:58 +0300 | [diff] [blame] | 568 | // |
Kevin Brodsky | b93ce18 | 2016-12-15 14:23:09 +0000 | [diff] [blame] | 569 | // For reference, this behavior is caused by this commit: |
| 570 | // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c |
Artem Udovichenko | b18a669 | 2016-11-17 10:51:58 +0300 | [diff] [blame] | 571 | FlushInstructionCache(reinterpret_cast<char*>(code_ptr), |
| 572 | reinterpret_cast<char*>(code_ptr + code_size)); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 573 | DCHECK(!Runtime::Current()->IsAotCompiler()); |
| 574 | if (has_should_deoptimize_flag) { |
| 575 | method_header->SetHasShouldDeoptimizeFlag(); |
| 576 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 577 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 578 | |
Nicolas Geoffray | 0a52223 | 2016-01-19 09:34:58 +0000 | [diff] [blame] | 579 | number_of_compilations_++; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 580 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 581 | // We need to update the entry point in the runnable state for the instrumentation. |
| 582 | { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 583 | // Need cha_lock_ for checking all single-implementation flags and register |
| 584 | // dependencies. |
| 585 | MutexLock cha_mu(self, *Locks::cha_lock_); |
| 586 | bool single_impl_still_valid = true; |
| 587 | for (ArtMethod* single_impl : cha_single_implementation_list) { |
| 588 | if (!single_impl->HasSingleImplementation()) { |
| 589 | // We simply discard the compiled code. Clear the |
| 590 | // counter so that it may be recompiled later. Hopefully the |
| 591 | // class hierarchy will be more stable when compilation is retried. |
| 592 | single_impl_still_valid = false; |
| 593 | method->ClearCounter(); |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // Discard the code if any single-implementation assumptions are now invalid. |
| 599 | if (!single_impl_still_valid) { |
| 600 | VLOG(jit) << "JIT discarded jitted code due to invalid single-implementation assumptions."; |
| 601 | return nullptr; |
| 602 | } |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 603 | DCHECK(cha_single_implementation_list.empty() || !Runtime::Current()->IsJavaDebuggable()) |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 604 | << "Should not be using cha on debuggable apps/runs!"; |
| 605 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 606 | for (ArtMethod* single_impl : cha_single_implementation_list) { |
| 607 | Runtime::Current()->GetClassHierarchyAnalysis()->AddDependency( |
| 608 | single_impl, method, method_header); |
| 609 | } |
| 610 | |
| 611 | // The following needs to be guarded by cha_lock_ also. Otherwise it's |
| 612 | // possible that the compiled code is considered invalidated by some class linking, |
| 613 | // but below we still make the compiled code valid for the method. |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 614 | MutexLock mu(self, lock_); |
| 615 | method_code_map_.Put(code_ptr, method); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 616 | // Fill the root table before updating the entry point. |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 617 | DCHECK_EQ(FromStackMapToRoots(stack_map), roots_data); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 618 | FillRootTable(roots_data, roots); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 619 | if (osr) { |
Nicolas Geoffray | fcdd729 | 2016-02-25 13:27:47 +0000 | [diff] [blame] | 620 | number_of_osr_compilations_++; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 621 | osr_code_map_.Put(method, code_ptr); |
Nicolas Geoffray | 480d510 | 2016-04-18 12:09:30 +0100 | [diff] [blame] | 622 | } else { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 623 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 624 | method, method_header->GetEntryPoint()); |
| 625 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 626 | if (collection_in_progress_) { |
| 627 | // We need to update the live bitmap if there is a GC to ensure it sees this new |
| 628 | // code. |
| 629 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 630 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 631 | last_update_time_ns_.StoreRelease(NanoTime()); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 632 | VLOG(jit) |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 633 | << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 634 | << ArtMethod::PrettyMethod(method) << "@" << method |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 635 | << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": " |
| 636 | << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": " |
| 637 | << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << "," |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 638 | << reinterpret_cast<const void*>(method_header->GetEntryPoint() + |
| 639 | method_header->GetCodeSize()); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 640 | histogram_code_memory_use_.AddValue(code_size); |
| 641 | if (code_size > kCodeSizeLogThreshold) { |
| 642 | LOG(INFO) << "JIT allocated " |
| 643 | << PrettySize(code_size) |
| 644 | << " for compiled code of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 645 | << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 646 | } |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 647 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 648 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 649 | return reinterpret_cast<uint8_t*>(method_header); |
| 650 | } |
| 651 | |
| 652 | size_t JitCodeCache::CodeCacheSize() { |
| 653 | MutexLock mu(Thread::Current(), lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 654 | return CodeCacheSizeLocked(); |
| 655 | } |
| 656 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 657 | // This notifies the code cache that the given method has been redefined and that it should remove |
| 658 | // any cached information it has on the method. All threads must be suspended before calling this |
| 659 | // method. The compiled code for the method (if there is any) must not be in any threads call stack. |
| 660 | void JitCodeCache::NotifyMethodRedefined(ArtMethod* method) { |
| 661 | MutexLock mu(Thread::Current(), lock_); |
| 662 | if (method->IsNative()) { |
| 663 | return; |
| 664 | } |
| 665 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
| 666 | if (info != nullptr) { |
| 667 | auto profile = std::find(profiling_infos_.begin(), profiling_infos_.end(), info); |
| 668 | DCHECK(profile != profiling_infos_.end()); |
| 669 | profiling_infos_.erase(profile); |
| 670 | } |
| 671 | method->SetProfilingInfo(nullptr); |
| 672 | ScopedCodeCacheWrite ccw(code_map_.get()); |
| 673 | for (auto code_iter = method_code_map_.begin(); |
| 674 | code_iter != method_code_map_.end(); |
| 675 | ++code_iter) { |
| 676 | if (code_iter->second == method) { |
| 677 | FreeCode(code_iter->first); |
| 678 | method_code_map_.erase(code_iter); |
| 679 | } |
| 680 | } |
| 681 | auto code_map = osr_code_map_.find(method); |
| 682 | if (code_map != osr_code_map_.end()) { |
| 683 | osr_code_map_.erase(code_map); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // This invalidates old_method. Once this function returns one can no longer use old_method to |
| 688 | // execute code unless it is fixed up. This fixup will happen later in the process of installing a |
| 689 | // class redefinition. |
| 690 | // TODO We should add some info to ArtMethod to note that 'old_method' has been invalidated and |
| 691 | // shouldn't be used since it is no longer logically in the jit code cache. |
| 692 | // TODO We should add DCHECKS that validate that the JIT is paused when this method is entered. |
| 693 | void JitCodeCache::MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) { |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 694 | // Native methods have no profiling info and need no special handling from the JIT code cache. |
| 695 | if (old_method->IsNative()) { |
| 696 | return; |
| 697 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 698 | MutexLock mu(Thread::Current(), lock_); |
| 699 | // Update ProfilingInfo to the new one and remove it from the old_method. |
| 700 | if (old_method->GetProfilingInfo(kRuntimePointerSize) != nullptr) { |
| 701 | DCHECK_EQ(old_method->GetProfilingInfo(kRuntimePointerSize)->GetMethod(), old_method); |
| 702 | ProfilingInfo* info = old_method->GetProfilingInfo(kRuntimePointerSize); |
| 703 | old_method->SetProfilingInfo(nullptr); |
| 704 | // Since the JIT should be paused and all threads suspended by the time this is called these |
| 705 | // checks should always pass. |
| 706 | DCHECK(!info->IsInUseByCompiler()); |
| 707 | new_method->SetProfilingInfo(info); |
| 708 | info->method_ = new_method; |
| 709 | } |
| 710 | // Update method_code_map_ to point to the new method. |
| 711 | for (auto& it : method_code_map_) { |
| 712 | if (it.second == old_method) { |
| 713 | it.second = new_method; |
| 714 | } |
| 715 | } |
| 716 | // Update osr_code_map_ to point to the new method. |
| 717 | auto code_map = osr_code_map_.find(old_method); |
| 718 | if (code_map != osr_code_map_.end()) { |
| 719 | osr_code_map_.Put(new_method, code_map->second); |
| 720 | osr_code_map_.erase(old_method); |
| 721 | } |
| 722 | } |
| 723 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 724 | size_t JitCodeCache::CodeCacheSizeLocked() { |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 725 | return used_memory_for_code_; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | size_t JitCodeCache::DataCacheSize() { |
| 729 | MutexLock mu(Thread::Current(), lock_); |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 730 | return DataCacheSizeLocked(); |
| 731 | } |
| 732 | |
| 733 | size_t JitCodeCache::DataCacheSizeLocked() { |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 734 | return used_memory_for_data_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Nicolas Geoffray | f46501c | 2016-11-22 13:45:36 +0000 | [diff] [blame] | 737 | void JitCodeCache::ClearData(Thread* self, |
| 738 | uint8_t* stack_map_data, |
| 739 | uint8_t* roots_data) { |
| 740 | DCHECK_EQ(FromStackMapToRoots(stack_map_data), roots_data); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 741 | MutexLock mu(self, lock_); |
Nicolas Geoffray | f46501c | 2016-11-22 13:45:36 +0000 | [diff] [blame] | 742 | FreeData(reinterpret_cast<uint8_t*>(roots_data)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 745 | size_t JitCodeCache::ReserveData(Thread* self, |
| 746 | size_t stack_map_size, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 747 | size_t method_info_size, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 748 | size_t number_of_roots, |
| 749 | ArtMethod* method, |
| 750 | uint8_t** stack_map_data, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 751 | uint8_t** method_info_data, |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 752 | uint8_t** roots_data) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 753 | size_t table_size = ComputeRootTableSize(number_of_roots); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 754 | size_t size = RoundUp(stack_map_size + method_info_size + table_size, sizeof(void*)); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 755 | uint8_t* result = nullptr; |
| 756 | |
| 757 | { |
| 758 | ScopedThreadSuspension sts(self, kSuspended); |
| 759 | MutexLock mu(self, lock_); |
| 760 | WaitForPotentialCollectionToComplete(self); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 761 | result = AllocateData(size); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | if (result == nullptr) { |
| 765 | // Retry. |
| 766 | GarbageCollectCache(self); |
| 767 | ScopedThreadSuspension sts(self, kSuspended); |
| 768 | MutexLock mu(self, lock_); |
| 769 | WaitForPotentialCollectionToComplete(self); |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 770 | result = AllocateData(size); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 773 | MutexLock mu(self, lock_); |
| 774 | histogram_stack_map_memory_use_.AddValue(size); |
| 775 | if (size > kStackMapSizeLogThreshold) { |
| 776 | LOG(INFO) << "JIT allocated " |
| 777 | << PrettySize(size) |
| 778 | << " for stack maps of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 779 | << ArtMethod::PrettyMethod(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 780 | } |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 781 | if (result != nullptr) { |
| 782 | *roots_data = result; |
| 783 | *stack_map_data = result + table_size; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 784 | *method_info_data = *stack_map_data + stack_map_size; |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 785 | FillRootTableLength(*roots_data, number_of_roots); |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 786 | return size; |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 787 | } else { |
| 788 | *roots_data = nullptr; |
| 789 | *stack_map_data = nullptr; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 790 | *method_info_data = nullptr; |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 791 | return 0; |
Nicolas Geoffray | f4b9442 | 2016-12-05 00:10:09 +0000 | [diff] [blame] | 792 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 795 | class MarkCodeVisitor FINAL : public StackVisitor { |
| 796 | public: |
| 797 | MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in) |
| 798 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames), |
| 799 | code_cache_(code_cache_in), |
| 800 | bitmap_(code_cache_->GetLiveBitmap()) {} |
| 801 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 802 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 803 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 804 | if (method_header == nullptr) { |
| 805 | return true; |
| 806 | } |
| 807 | const void* code = method_header->GetCode(); |
| 808 | if (code_cache_->ContainsPc(code)) { |
| 809 | // Use the atomic set version, as multiple threads are executing this code. |
| 810 | bitmap_->AtomicTestAndSet(FromCodeToAllocation(code)); |
| 811 | } |
| 812 | return true; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 813 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 814 | |
| 815 | private: |
| 816 | JitCodeCache* const code_cache_; |
| 817 | CodeCacheBitmap* const bitmap_; |
| 818 | }; |
| 819 | |
| 820 | class MarkCodeClosure FINAL : public Closure { |
| 821 | public: |
| 822 | MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier) |
| 823 | : code_cache_(code_cache), barrier_(barrier) {} |
| 824 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 825 | void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 826 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 827 | DCHECK(thread == Thread::Current() || thread->IsSuspended()); |
| 828 | MarkCodeVisitor visitor(thread, code_cache_); |
| 829 | visitor.WalkStack(); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 830 | if (kIsDebugBuild) { |
| 831 | // The stack walking code queries the side instrumentation stack if it |
| 832 | // sees an instrumentation exit pc, so the JIT code of methods in that stack |
| 833 | // must have been seen. We sanity check this below. |
| 834 | for (const instrumentation::InstrumentationStackFrame& frame |
| 835 | : *thread->GetInstrumentationStack()) { |
| 836 | // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in |
| 837 | // its stack frame, it is not the method owning return_pc_. We just pass null to |
| 838 | // LookupMethodHeader: the method is only checked against in debug builds. |
| 839 | OatQuickMethodHeader* method_header = |
| 840 | code_cache_->LookupMethodHeader(frame.return_pc_, nullptr); |
| 841 | if (method_header != nullptr) { |
| 842 | const void* code = method_header->GetCode(); |
| 843 | CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code))); |
| 844 | } |
| 845 | } |
| 846 | } |
Mathieu Chartier | 10d2508 | 2015-10-28 18:36:09 -0700 | [diff] [blame] | 847 | barrier_->Pass(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 848 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 849 | |
| 850 | private: |
| 851 | JitCodeCache* const code_cache_; |
| 852 | Barrier* const barrier_; |
| 853 | }; |
| 854 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 855 | void JitCodeCache::NotifyCollectionDone(Thread* self) { |
| 856 | collection_in_progress_ = false; |
| 857 | lock_cond_.Broadcast(self); |
| 858 | } |
| 859 | |
| 860 | void JitCodeCache::SetFootprintLimit(size_t new_footprint) { |
| 861 | size_t per_space_footprint = new_footprint / 2; |
| 862 | DCHECK(IsAlignedParam(per_space_footprint, kPageSize)); |
| 863 | DCHECK_EQ(per_space_footprint * 2, new_footprint); |
| 864 | mspace_set_footprint_limit(data_mspace_, per_space_footprint); |
| 865 | { |
| 866 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 867 | mspace_set_footprint_limit(code_mspace_, per_space_footprint); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | bool JitCodeCache::IncreaseCodeCacheCapacity() { |
| 872 | if (current_capacity_ == max_capacity_) { |
| 873 | return false; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 876 | // Double the capacity if we're below 1MB, or increase it by 1MB if |
| 877 | // we're above. |
| 878 | if (current_capacity_ < 1 * MB) { |
| 879 | current_capacity_ *= 2; |
| 880 | } else { |
| 881 | current_capacity_ += 1 * MB; |
| 882 | } |
| 883 | if (current_capacity_ > max_capacity_) { |
| 884 | current_capacity_ = max_capacity_; |
| 885 | } |
| 886 | |
| 887 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 888 | LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_); |
| 889 | } |
| 890 | |
| 891 | SetFootprintLimit(current_capacity_); |
| 892 | |
| 893 | return true; |
| 894 | } |
| 895 | |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 896 | void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) { |
| 897 | Barrier barrier(0); |
| 898 | size_t threads_running_checkpoint = 0; |
| 899 | MarkCodeClosure closure(this, &barrier); |
| 900 | threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure); |
| 901 | // Now that we have run our checkpoint, move to a suspended state and wait |
| 902 | // for other threads to run the checkpoint. |
| 903 | ScopedThreadSuspension sts(self, kSuspended); |
| 904 | if (threads_running_checkpoint != 0) { |
| 905 | barrier.Increment(self, threads_running_checkpoint); |
| 906 | } |
| 907 | } |
| 908 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 909 | bool JitCodeCache::ShouldDoFullCollection() { |
| 910 | if (current_capacity_ == max_capacity_) { |
| 911 | // Always do a full collection when the code cache is full. |
| 912 | return true; |
| 913 | } else if (current_capacity_ < kReservedCapacity) { |
| 914 | // Always do partial collection when the code cache size is below the reserved |
| 915 | // capacity. |
| 916 | return false; |
| 917 | } else if (last_collection_increased_code_cache_) { |
| 918 | // This time do a full collection. |
| 919 | return true; |
| 920 | } else { |
| 921 | // This time do a partial collection. |
| 922 | return false; |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 926 | void JitCodeCache::GarbageCollectCache(Thread* self) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 927 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 928 | if (!garbage_collect_code_) { |
| 929 | MutexLock mu(self, lock_); |
| 930 | IncreaseCodeCacheCapacity(); |
| 931 | return; |
| 932 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 933 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 934 | // Wait for an existing collection, or let everyone know we are starting one. |
| 935 | { |
| 936 | ScopedThreadSuspension sts(self, kSuspended); |
| 937 | MutexLock mu(self, lock_); |
| 938 | if (WaitForPotentialCollectionToComplete(self)) { |
| 939 | return; |
| 940 | } else { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 941 | number_of_collections_++; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 942 | live_bitmap_.reset(CodeCacheBitmap::Create( |
| 943 | "code-cache-bitmap", |
| 944 | reinterpret_cast<uintptr_t>(code_map_->Begin()), |
| 945 | reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2))); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 946 | collection_in_progress_ = true; |
| 947 | } |
| 948 | } |
| 949 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 950 | TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit)); |
Nicolas Geoffray | 8d37250 | 2016-02-23 13:56:43 +0000 | [diff] [blame] | 951 | { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 952 | TimingLogger::ScopedTiming st("Code cache collection", &logger); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 953 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 954 | bool do_full_collection = false; |
| 955 | { |
| 956 | MutexLock mu(self, lock_); |
| 957 | do_full_collection = ShouldDoFullCollection(); |
Nicolas Geoffray | a96917a | 2016-03-01 22:18:02 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 960 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 961 | LOG(INFO) << "Do " |
| 962 | << (do_full_collection ? "full" : "partial") |
| 963 | << " code cache collection, code=" |
| 964 | << PrettySize(CodeCacheSize()) |
| 965 | << ", data=" << PrettySize(DataCacheSize()); |
| 966 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 967 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 968 | DoCollection(self, /* collect_profiling_info */ do_full_collection); |
| 969 | |
| 970 | if (!kIsDebugBuild || VLOG_IS_ON(jit)) { |
| 971 | LOG(INFO) << "After code cache collection, code=" |
| 972 | << PrettySize(CodeCacheSize()) |
| 973 | << ", data=" << PrettySize(DataCacheSize()); |
| 974 | } |
| 975 | |
| 976 | { |
| 977 | MutexLock mu(self, lock_); |
| 978 | |
| 979 | // Increase the code cache only when we do partial collections. |
| 980 | // TODO: base this strategy on how full the code cache is? |
| 981 | if (do_full_collection) { |
| 982 | last_collection_increased_code_cache_ = false; |
| 983 | } else { |
| 984 | last_collection_increased_code_cache_ = true; |
| 985 | IncreaseCodeCacheCapacity(); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 988 | bool next_collection_will_be_full = ShouldDoFullCollection(); |
| 989 | |
| 990 | // 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] | 991 | if (next_collection_will_be_full) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 992 | // Save the entry point of methods we have compiled, and update the entry |
| 993 | // point of those methods to the interpreter. If the method is invoked, the |
| 994 | // interpreter will update its entry point to the compiled code and call it. |
| 995 | for (ProfilingInfo* info : profiling_infos_) { |
| 996 | const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
| 997 | if (ContainsPc(entry_point)) { |
| 998 | info->SetSavedEntryPoint(entry_point); |
Nicolas Geoffray | 3b1a7f4 | 2017-02-22 10:21:00 +0000 | [diff] [blame] | 999 | // Don't call Instrumentation::UpdateMethods, as it can check the declaring |
| 1000 | // class of the method. We may be concurrently running a GC which makes accessing |
| 1001 | // the class unsafe. We know it is OK to bypass the instrumentation as we've just |
| 1002 | // checked that the current entry point is JIT compiled code. |
| 1003 | info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | DCHECK(CheckLiveCompiledCodeHasProfilingInfo()); |
| 1008 | } |
| 1009 | live_bitmap_.reset(nullptr); |
| 1010 | NotifyCollectionDone(self); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1011 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1012 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1013 | Runtime::Current()->GetJit()->AddTimingLogger(logger); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1016 | void JitCodeCache::RemoveUnmarkedCode(Thread* self) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1017 | ScopedTrace trace(__FUNCTION__); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1018 | std::unordered_set<OatQuickMethodHeader*> method_headers; |
| 1019 | { |
| 1020 | MutexLock mu(self, lock_); |
| 1021 | ScopedCodeCacheWrite scc(code_map_.get()); |
| 1022 | // Iterate over all compiled code and remove entries that are not marked. |
| 1023 | for (auto it = method_code_map_.begin(); it != method_code_map_.end();) { |
| 1024 | const void* code_ptr = it->first; |
| 1025 | uintptr_t allocation = FromCodeToAllocation(code_ptr); |
| 1026 | if (GetLiveBitmap()->Test(allocation)) { |
| 1027 | ++it; |
| 1028 | } else { |
| 1029 | method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first)); |
| 1030 | it = method_code_map_.erase(it); |
| 1031 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1034 | FreeAllMethodHeaders(method_headers); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1038 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1039 | { |
| 1040 | MutexLock mu(self, lock_); |
| 1041 | if (collect_profiling_info) { |
| 1042 | // Clear the profiling info of methods that do not have compiled code as entrypoint. |
| 1043 | // Also remove the saved entry point from the ProfilingInfo objects. |
| 1044 | for (ProfilingInfo* info : profiling_infos_) { |
| 1045 | const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1046 | if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1047 | info->GetMethod()->SetProfilingInfo(nullptr); |
| 1048 | } |
Nicolas Geoffray | b9a639d | 2016-03-22 11:25:20 +0000 | [diff] [blame] | 1049 | |
| 1050 | if (info->GetSavedEntryPoint() != nullptr) { |
| 1051 | info->SetSavedEntryPoint(nullptr); |
| 1052 | // We are going to move this method back to interpreter. Clear the counter now to |
| 1053 | // give it a chance to be hot again. |
| 1054 | info->GetMethod()->ClearCounter(); |
| 1055 | } |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } else if (kIsDebugBuild) { |
| 1058 | // Sanity check that the profiling infos do not have a dangling entry point. |
| 1059 | for (ProfilingInfo* info : profiling_infos_) { |
| 1060 | DCHECK(info->GetSavedEntryPoint() == nullptr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1061 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1062 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1063 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1064 | // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not |
| 1065 | // an entry point is either: |
| 1066 | // - an osr compiled code, that will be removed if not in a thread call stack. |
| 1067 | // - discarded compiled code, that will be removed if not in a thread call stack. |
| 1068 | for (const auto& it : method_code_map_) { |
| 1069 | ArtMethod* method = it.second; |
| 1070 | const void* code_ptr = it.first; |
| 1071 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1072 | if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) { |
| 1073 | GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr)); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1077 | // 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] | 1078 | // on thread stacks). |
| 1079 | osr_code_map_.clear(); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | // 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] | 1083 | MarkCompiledCodeOnThreadStacks(self); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1084 | |
Nicolas Geoffray | 9abb297 | 2016-03-04 14:32:59 +0000 | [diff] [blame] | 1085 | // At this point, mutator threads are still running, and entrypoints of methods can |
| 1086 | // change. We do know they cannot change to a code cache entry that is not marked, |
| 1087 | // therefore we can safely remove those entries. |
| 1088 | RemoveUnmarkedCode(self); |
Nicolas Geoffray | a96917a | 2016-03-01 22:18:02 +0000 | [diff] [blame] | 1089 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1090 | if (collect_profiling_info) { |
Nicolas Geoffray | cf48fa0 | 2016-07-30 22:49:11 +0100 | [diff] [blame] | 1091 | ScopedThreadSuspension sts(self, kSuspended); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1092 | MutexLock mu(self, lock_); |
| 1093 | // Free all profiling infos of methods not compiled nor being compiled. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1094 | 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] | 1095 | [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1096 | const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode(); |
Nicolas Geoffray | 511e41b | 2016-03-02 17:09:35 +0000 | [diff] [blame] | 1097 | // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope |
| 1098 | // that the compiled code would not get revived. As mutator threads run concurrently, |
| 1099 | // they may have revived the compiled code, and now we are in the situation where |
| 1100 | // a method has compiled code but no ProfilingInfo. |
| 1101 | // We make sure compiled methods have a ProfilingInfo object. It is needed for |
| 1102 | // code cache collection. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1103 | if (ContainsPc(ptr) && |
| 1104 | info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1105 | info->GetMethod()->SetProfilingInfo(info); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1106 | } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1107 | // No need for this ProfilingInfo object anymore. |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 1108 | FreeData(reinterpret_cast<uint8_t*>(info)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1109 | return true; |
| 1110 | } |
| 1111 | return false; |
| 1112 | }); |
| 1113 | profiling_infos_.erase(profiling_kept_end, profiling_infos_.end()); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1114 | DCHECK(CheckLiveCompiledCodeHasProfilingInfo()); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1115 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1118 | bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1119 | ScopedTrace trace(__FUNCTION__); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1120 | // Check that methods we have compiled do have a ProfilingInfo object. We would |
| 1121 | // have memory leaks of compiled code otherwise. |
| 1122 | for (const auto& it : method_code_map_) { |
| 1123 | ArtMethod* method = it.second; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1124 | if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) { |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1125 | const void* code_ptr = it.first; |
| 1126 | const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1127 | if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) { |
| 1128 | // If the code is not dead, then we have a problem. Note that this can even |
| 1129 | // happen just after a collection, as mutator threads are running in parallel |
| 1130 | // and could deoptimize an existing compiled code. |
| 1131 | return false; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | return true; |
| 1136 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1137 | |
| 1138 | OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) { |
| 1139 | static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA"); |
| 1140 | if (kRuntimeISA == kArm) { |
| 1141 | // On Thumb-2, the pc is offset by one. |
| 1142 | --pc; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1143 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1144 | if (!ContainsPc(reinterpret_cast<const void*>(pc))) { |
| 1145 | return nullptr; |
| 1146 | } |
| 1147 | |
| 1148 | MutexLock mu(Thread::Current(), lock_); |
| 1149 | if (method_code_map_.empty()) { |
| 1150 | return nullptr; |
| 1151 | } |
| 1152 | auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc)); |
| 1153 | --it; |
| 1154 | |
| 1155 | const void* code_ptr = it->first; |
| 1156 | OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr); |
| 1157 | if (!method_header->Contains(pc)) { |
| 1158 | return nullptr; |
| 1159 | } |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 1160 | if (kIsDebugBuild && method != nullptr) { |
Alex Light | 1ebe4fe | 2017-01-30 14:57:11 -0800 | [diff] [blame] | 1161 | // When we are walking the stack to redefine classes and creating obsolete methods it is |
| 1162 | // possible that we might have updated the method_code_map by making this method obsolete in a |
| 1163 | // previous frame. Therefore we should just check that the non-obsolete version of this method |
| 1164 | // is the one we expect. We change to the non-obsolete versions in the error message since the |
| 1165 | // obsolete version of the method might not be fully initialized yet. This situation can only |
| 1166 | // occur when we are in the process of allocating and setting up obsolete methods. Otherwise |
| 1167 | // method and it->second should be identical. (See runtime/openjdkjvmti/ti_redefine.cc for more |
| 1168 | // information.) |
| 1169 | DCHECK_EQ(it->second->GetNonObsoleteMethod(), method->GetNonObsoleteMethod()) |
| 1170 | << ArtMethod::PrettyMethod(method->GetNonObsoleteMethod()) << " " |
| 1171 | << ArtMethod::PrettyMethod(it->second->GetNonObsoleteMethod()) << " " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1172 | << std::hex << pc; |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 1173 | } |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 1174 | return method_header; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1177 | OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) { |
| 1178 | MutexLock mu(Thread::Current(), lock_); |
| 1179 | auto it = osr_code_map_.find(method); |
| 1180 | if (it == osr_code_map_.end()) { |
| 1181 | return nullptr; |
| 1182 | } |
| 1183 | return OatQuickMethodHeader::FromCodePointer(it->second); |
| 1184 | } |
| 1185 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1186 | ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self, |
| 1187 | ArtMethod* method, |
| 1188 | const std::vector<uint32_t>& entries, |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1189 | bool retry_allocation) |
| 1190 | // No thread safety analysis as we are using TryLock/Unlock explicitly. |
| 1191 | NO_THREAD_SAFETY_ANALYSIS { |
| 1192 | ProfilingInfo* info = nullptr; |
| 1193 | if (!retry_allocation) { |
| 1194 | // If we are allocating for the interpreter, just try to lock, to avoid |
| 1195 | // lock contention with the JIT. |
| 1196 | if (lock_.ExclusiveTryLock(self)) { |
| 1197 | info = AddProfilingInfoInternal(self, method, entries); |
| 1198 | lock_.ExclusiveUnlock(self); |
| 1199 | } |
| 1200 | } else { |
| 1201 | { |
| 1202 | MutexLock mu(self, lock_); |
| 1203 | info = AddProfilingInfoInternal(self, method, entries); |
| 1204 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1205 | |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1206 | if (info == nullptr) { |
| 1207 | GarbageCollectCache(self); |
| 1208 | MutexLock mu(self, lock_); |
| 1209 | info = AddProfilingInfoInternal(self, method, entries); |
| 1210 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1211 | } |
| 1212 | return info; |
| 1213 | } |
| 1214 | |
Nicolas Geoffray | 1e7da9b | 2016-03-01 14:11:40 +0000 | [diff] [blame] | 1215 | ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED, |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1216 | ArtMethod* method, |
| 1217 | const std::vector<uint32_t>& entries) { |
| 1218 | size_t profile_info_size = RoundUp( |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1219 | sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(), |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1220 | sizeof(void*)); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Check whether some other thread has concurrently created it. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1223 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1224 | if (info != nullptr) { |
| 1225 | return info; |
| 1226 | } |
| 1227 | |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 1228 | uint8_t* data = AllocateData(profile_info_size); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1229 | if (data == nullptr) { |
| 1230 | return nullptr; |
| 1231 | } |
| 1232 | info = new (data) ProfilingInfo(method, entries); |
Nicolas Geoffray | 07f3564 | 2016-01-04 16:06:51 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Make sure other threads see the data in the profiling info object before the |
| 1235 | // store in the ArtMethod's ProfilingInfo pointer. |
| 1236 | QuasiAtomic::ThreadFenceRelease(); |
| 1237 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1238 | method->SetProfilingInfo(info); |
| 1239 | profiling_infos_.push_back(info); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1240 | histogram_profiling_info_memory_use_.AddValue(profile_info_size); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 1241 | return info; |
| 1242 | } |
| 1243 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 1244 | // NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock |
| 1245 | // is already held. |
| 1246 | void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS { |
| 1247 | if (code_mspace_ == mspace) { |
| 1248 | size_t result = code_end_; |
| 1249 | code_end_ += increment; |
| 1250 | return reinterpret_cast<void*>(result + code_map_->Begin()); |
| 1251 | } else { |
| 1252 | DCHECK_EQ(data_mspace_, mspace); |
| 1253 | size_t result = data_end_; |
| 1254 | data_end_ += increment; |
| 1255 | return reinterpret_cast<void*>(result + data_map_->Begin()); |
| 1256 | } |
| 1257 | } |
| 1258 | |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 1259 | void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1260 | std::vector<ProfileMethodInfo>& methods) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1261 | ScopedTrace trace(__FUNCTION__); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1262 | MutexLock mu(Thread::Current(), lock_); |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 1263 | for (const ProfilingInfo* info : profiling_infos_) { |
| 1264 | ArtMethod* method = info->GetMethod(); |
| 1265 | const DexFile* dex_file = method->GetDexFile(); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1266 | if (!ContainsElement(dex_base_locations, dex_file->GetBaseLocation())) { |
| 1267 | // Skip dex files which are not profiled. |
| 1268 | continue; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1269 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1270 | std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches; |
| 1271 | for (size_t i = 0; i < info->number_of_inline_caches_; ++i) { |
| 1272 | std::vector<ProfileMethodInfo::ProfileClassReference> profile_classes; |
| 1273 | const InlineCache& cache = info->cache_[i]; |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 1274 | ArtMethod* caller = info->GetMethod(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1275 | bool is_missing_types = false; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1276 | for (size_t k = 0; k < InlineCache::kIndividualCacheSize; k++) { |
| 1277 | mirror::Class* cls = cache.classes_[k].Read(); |
| 1278 | if (cls == nullptr) { |
| 1279 | break; |
| 1280 | } |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1281 | |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 1282 | // Check if the receiver is in the boot class path or if it's in the |
| 1283 | // same class loader as the caller. If not, skip it, as there is not |
| 1284 | // much we can do during AOT. |
| 1285 | if (!cls->IsBootStrapClassLoaded() && |
| 1286 | caller->GetClassLoader() != cls->GetClassLoader()) { |
| 1287 | is_missing_types = true; |
| 1288 | continue; |
| 1289 | } |
| 1290 | |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1291 | const DexFile* class_dex_file = nullptr; |
| 1292 | dex::TypeIndex type_index; |
| 1293 | |
| 1294 | if (cls->GetDexCache() == nullptr) { |
| 1295 | DCHECK(cls->IsArrayClass()) << cls->PrettyClass(); |
Calin Juravle | e21806f | 2017-02-22 11:49:43 -0800 | [diff] [blame] | 1296 | // Make a best effort to find the type index in the method's dex file. |
| 1297 | // We could search all open dex files but that might turn expensive |
| 1298 | // and probably not worth it. |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1299 | class_dex_file = dex_file; |
| 1300 | type_index = cls->FindTypeIndexInOtherDexFile(*dex_file); |
| 1301 | } else { |
| 1302 | class_dex_file = &(cls->GetDexFile()); |
| 1303 | type_index = cls->GetDexTypeIndex(); |
| 1304 | } |
| 1305 | if (!type_index.IsValid()) { |
| 1306 | // Could be a proxy class or an array for which we couldn't find the type index. |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1307 | is_missing_types = true; |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1308 | continue; |
| 1309 | } |
| 1310 | if (ContainsElement(dex_base_locations, class_dex_file->GetBaseLocation())) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1311 | // Only consider classes from the same apk (including multidex). |
| 1312 | profile_classes.emplace_back(/*ProfileMethodInfo::ProfileClassReference*/ |
Calin Juravle | 4ca70a3 | 2017-02-21 16:22:24 -0800 | [diff] [blame] | 1313 | class_dex_file, type_index); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1314 | } else { |
| 1315 | is_missing_types = true; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | if (!profile_classes.empty()) { |
| 1319 | inline_caches.emplace_back(/*ProfileMethodInfo::ProfileInlineCache*/ |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1320 | cache.dex_pc_, is_missing_types, profile_classes); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | methods.emplace_back(/*ProfileMethodInfo*/ |
| 1324 | dex_file, method->GetDexMethodIndex(), inline_caches); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 1328 | uint64_t JitCodeCache::GetLastUpdateTimeNs() const { |
| 1329 | return last_update_time_ns_.LoadAcquire(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1330 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1331 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 1332 | bool JitCodeCache::IsOsrCompiled(ArtMethod* method) { |
| 1333 | MutexLock mu(Thread::Current(), lock_); |
| 1334 | return osr_code_map_.find(method) != osr_code_map_.end(); |
| 1335 | } |
| 1336 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1337 | bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) { |
| 1338 | if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1341 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1342 | MutexLock mu(self, lock_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1343 | if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) { |
| 1344 | return false; |
| 1345 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1346 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1347 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1348 | if (info == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1349 | VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled"; |
Nicolas Geoffray | b9a639d | 2016-03-22 11:25:20 +0000 | [diff] [blame] | 1350 | // Because the counter is not atomic, there are some rare cases where we may not |
| 1351 | // hit the threshold for creating the ProfilingInfo. Reset the counter now to |
| 1352 | // "correct" this. |
| 1353 | method->ClearCounter(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1354 | return false; |
| 1355 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1356 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1357 | if (info->IsMethodBeingCompiled(osr)) { |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1358 | return false; |
| 1359 | } |
| 1360 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1361 | info->SetIsMethodBeingCompiled(true, osr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1362 | return true; |
| 1363 | } |
| 1364 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1365 | ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1366 | MutexLock mu(self, lock_); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1367 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1368 | if (info != nullptr) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame^] | 1369 | if (!info->IncrementInlineUse()) { |
| 1370 | // Overflow of inlining uses, just bail. |
| 1371 | return nullptr; |
| 1372 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1373 | } |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1374 | return info; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1377 | void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1378 | MutexLock mu(self, lock_); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1379 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 1380 | DCHECK(info != nullptr); |
| 1381 | info->DecrementInlineUse(); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1384 | void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED, bool osr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1385 | ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize); |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 1386 | DCHECK(info->IsMethodBeingCompiled(osr)); |
| 1387 | info->SetIsMethodBeingCompiled(false, osr); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 1390 | size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) { |
| 1391 | MutexLock mu(Thread::Current(), lock_); |
| 1392 | return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr))); |
| 1393 | } |
| 1394 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1395 | void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method, |
| 1396 | const OatQuickMethodHeader* header) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1397 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 1398 | if ((profiling_info != nullptr) && |
| 1399 | (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) { |
| 1400 | // Prevent future uses of the compiled code. |
| 1401 | profiling_info->SetSavedEntryPoint(nullptr); |
| 1402 | } |
| 1403 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1404 | if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) { |
| 1405 | // The entrypoint is the one to invalidate, so we just update |
| 1406 | // it to the interpreter entry point and clear the counter to get the method |
| 1407 | // Jitted again. |
| 1408 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 1409 | method, GetQuickToInterpreterBridge()); |
| 1410 | method->ClearCounter(); |
| 1411 | } else { |
| 1412 | MutexLock mu(Thread::Current(), lock_); |
| 1413 | auto it = osr_code_map_.find(method); |
| 1414 | if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) { |
| 1415 | // Remove the OSR method, to avoid using it again. |
| 1416 | osr_code_map_.erase(it); |
| 1417 | } |
| 1418 | } |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1419 | MutexLock mu(Thread::Current(), lock_); |
| 1420 | number_of_deoptimizations_++; |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Nicolas Geoffray | 38ea9bd | 2016-02-19 16:25:57 +0000 | [diff] [blame] | 1423 | uint8_t* JitCodeCache::AllocateCode(size_t code_size) { |
| 1424 | size_t alignment = GetInstructionSetAlignment(kRuntimeISA); |
| 1425 | uint8_t* result = reinterpret_cast<uint8_t*>( |
| 1426 | mspace_memalign(code_mspace_, alignment, code_size)); |
| 1427 | size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment); |
| 1428 | // Ensure the header ends up at expected instruction alignment. |
| 1429 | DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment); |
| 1430 | used_memory_for_code_ += mspace_usable_size(result); |
| 1431 | return result; |
| 1432 | } |
| 1433 | |
| 1434 | void JitCodeCache::FreeCode(uint8_t* code) { |
| 1435 | used_memory_for_code_ -= mspace_usable_size(code); |
| 1436 | mspace_free(code_mspace_, code); |
| 1437 | } |
| 1438 | |
| 1439 | uint8_t* JitCodeCache::AllocateData(size_t data_size) { |
| 1440 | void* result = mspace_malloc(data_mspace_, data_size); |
| 1441 | used_memory_for_data_ += mspace_usable_size(result); |
| 1442 | return reinterpret_cast<uint8_t*>(result); |
| 1443 | } |
| 1444 | |
| 1445 | void JitCodeCache::FreeData(uint8_t* data) { |
| 1446 | used_memory_for_data_ -= mspace_usable_size(data); |
| 1447 | mspace_free(data_mspace_, data); |
| 1448 | } |
| 1449 | |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1450 | void JitCodeCache::Dump(std::ostream& os) { |
| 1451 | MutexLock mu(Thread::Current(), lock_); |
| 1452 | os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n" |
| 1453 | << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n" |
| 1454 | << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n" |
| 1455 | << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n" |
| 1456 | << "Total number of JIT compilations: " << number_of_compilations_ << "\n" |
| 1457 | << "Total number of JIT compilations for on stack replacement: " |
| 1458 | << number_of_osr_compilations_ << "\n" |
| 1459 | << "Total number of deoptimizations: " << number_of_deoptimizations_ << "\n" |
| 1460 | << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl; |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1461 | histogram_stack_map_memory_use_.PrintMemoryUse(os); |
| 1462 | histogram_code_memory_use_.PrintMemoryUse(os); |
| 1463 | histogram_profiling_info_memory_use_.PrintMemoryUse(os); |
Nicolas Geoffray | bcd94c8 | 2016-03-03 13:23:33 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1466 | } // namespace jit |
| 1467 | } // namespace art |