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