blob: 70a717154b483cc1b706ef5272da3ff0f461bda3 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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 Gampe5629d2d2017-05-15 16:28:13 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Andreas Gampef0f3c592018-06-26 13:28:00 -070024#include "base/histogram-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080025#include "base/logging.h" // For VLOG.
David Sehr79e26072018-04-06 17:58:50 -070026#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/quasi_atomic.h"
Calin Juravle66f55232015-12-08 15:09:10 +000028#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080029#include "base/systrace.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010030#include "base/time_utils.h"
Mingyao Yang063fc772016-08-02 11:02:54 -070031#include "cha.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000032#include "debugger_interface.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file_loader.h"
Andreas Gampef0f3c592018-06-26 13:28:00 -070034#include "dex/method_reference.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010035#include "entrypoints/runtime_asm_entrypoints.h"
36#include "gc/accounting/bitmap-inl.h"
Nicolas Geoffraycf48fa02016-07-30 22:49:11 +010037#include "gc/scoped_gc_critical_section.h"
Vladimir Markob0b68cf2017-11-14 18:11:50 +000038#include "handle.h"
Andreas Gampef0f3c592018-06-26 13:28:00 -070039#include "instrumentation.h"
Andreas Gampeb2d18fa2017-06-06 20:46:10 -070040#include "intern_table.h"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000041#include "jit/jit.h"
Nicolas Geoffray26705e22015-10-28 12:50:11 +000042#include "jit/profiling_info.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010043#include "linear_alloc.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044#include "oat_file-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070045#include "oat_quick_method_header.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070046#include "object_callbacks.h"
David Sehr82d046e2018-04-23 08:14:19 -070047#include "profile/profile_compilation_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070048#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070049#include "stack.h"
Vladimir Markob0b68cf2017-11-14 18:11:50 +000050#include "thread-current-inl.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010051#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052
53namespace art {
54namespace jit {
55
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010056static constexpr int kProtData = PROT_READ | PROT_WRITE;
57static constexpr int kProtCode = PROT_READ | PROT_EXEC;
58
Nicolas Geoffray933330a2016-03-16 14:20:06 +000059static constexpr size_t kCodeSizeLogThreshold = 50 * KB;
60static constexpr size_t kStackMapSizeLogThreshold = 50 * KB;
61
Vladimir Marko2196c652017-11-30 16:16:07 +000062class JitCodeCache::JniStubKey {
63 public:
64 explicit JniStubKey(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
65 : shorty_(method->GetShorty()),
66 is_static_(method->IsStatic()),
67 is_fast_native_(method->IsFastNative()),
68 is_critical_native_(method->IsCriticalNative()),
69 is_synchronized_(method->IsSynchronized()) {
70 DCHECK(!(is_fast_native_ && is_critical_native_));
71 }
72
73 bool operator<(const JniStubKey& rhs) const {
74 if (is_static_ != rhs.is_static_) {
75 return rhs.is_static_;
76 }
77 if (is_synchronized_ != rhs.is_synchronized_) {
78 return rhs.is_synchronized_;
79 }
80 if (is_fast_native_ != rhs.is_fast_native_) {
81 return rhs.is_fast_native_;
82 }
83 if (is_critical_native_ != rhs.is_critical_native_) {
84 return rhs.is_critical_native_;
85 }
86 return strcmp(shorty_, rhs.shorty_) < 0;
87 }
88
89 // Update the shorty to point to another method's shorty. Call this function when removing
90 // the method that references the old shorty from JniCodeData and not removing the entire
91 // JniCodeData; the old shorty may become a dangling pointer when that method is unloaded.
92 void UpdateShorty(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
93 const char* shorty = method->GetShorty();
94 DCHECK_STREQ(shorty_, shorty);
95 shorty_ = shorty;
96 }
97
98 private:
99 // The shorty points to a DexFile data and may need to change
100 // to point to the same shorty in a different DexFile.
101 mutable const char* shorty_;
102
103 const bool is_static_;
104 const bool is_fast_native_;
105 const bool is_critical_native_;
106 const bool is_synchronized_;
107};
108
109class JitCodeCache::JniStubData {
110 public:
111 JniStubData() : code_(nullptr), methods_() {}
112
113 void SetCode(const void* code) {
114 DCHECK(code != nullptr);
115 code_ = code;
116 }
117
118 const void* GetCode() const {
119 return code_;
120 }
121
122 bool IsCompiled() const {
123 return GetCode() != nullptr;
124 }
125
126 void AddMethod(ArtMethod* method) {
127 if (!ContainsElement(methods_, method)) {
128 methods_.push_back(method);
129 }
130 }
131
132 const std::vector<ArtMethod*>& GetMethods() const {
133 return methods_;
134 }
135
136 void RemoveMethodsIn(const LinearAlloc& alloc) {
137 auto kept_end = std::remove_if(
138 methods_.begin(),
139 methods_.end(),
140 [&alloc](ArtMethod* method) { return alloc.ContainsUnsafe(method); });
141 methods_.erase(kept_end, methods_.end());
142 }
143
144 bool RemoveMethod(ArtMethod* method) {
145 auto it = std::find(methods_.begin(), methods_.end(), method);
146 if (it != methods_.end()) {
147 methods_.erase(it);
148 return true;
149 } else {
150 return false;
151 }
152 }
153
154 void MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
155 std::replace(methods_.begin(), methods_.end(), old_method, new_method);
156 }
157
158 private:
159 const void* code_;
160 std::vector<ArtMethod*> methods_;
161};
162
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000163JitCodeCache* JitCodeCache::Create(size_t initial_capacity,
164 size_t max_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000165 bool generate_debug_info,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700166 bool used_only_for_profile_data,
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000167 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800168 ScopedTrace trace(__PRETTY_FUNCTION__);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100169 CHECK_GE(max_capacity, initial_capacity);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000170
David Sehrd1dbb742017-07-17 11:20:38 -0700171 // Generating debug information is for using the Linux perf tool on
172 // host which does not work with ashmem.
Steve Austin882ed6b2018-06-08 11:40:38 -0700173 // Also, targets linux and fuchsia do not support ashmem.
174 bool use_ashmem = !generate_debug_info && !kIsTargetLinux && !kIsTargetFuchsia;
David Sehrd1dbb742017-07-17 11:20:38 -0700175
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000176 // With 'perf', we want a 1-1 mapping between an address and a method.
Alex Light2d441b12018-06-08 15:33:21 -0700177 // We aren't able to keep method pointers live during the instrumentation method entry trampoline
178 // so we will just disable jit-gc if we are doing that.
179 bool garbage_collect_code = !generate_debug_info &&
180 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000181
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000182 // We need to have 32 bit offsets from method headers in code cache which point to things
183 // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
184 // Ensure we're below 1 GB to be safe.
185 if (max_capacity > 1 * GB) {
186 std::ostringstream oss;
187 oss << "Maxium code cache capacity is limited to 1 GB, "
188 << PrettySize(max_capacity) << " is too big";
189 *error_msg = oss.str();
190 return nullptr;
191 }
192
Calin Juravle016fcbe22018-05-03 19:47:35 -0700193 // Decide how we should map the code and data sections.
194 // If we use the code cache just for profiling we do not need to map the code section as
195 // executable.
196 // NOTE 1: this is yet another workaround to bypass strict SElinux policies in order to be able
197 // to profile system server.
198 // NOTE 2: We could just not create the code section at all but we will need to
199 // special case too many cases.
200 int memmap_flags_prot_code = used_only_for_profile_data ? (kProtCode & ~PROT_EXEC) : kProtCode;
201
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800202 std::string error_str;
203 // Map name specific for android_os_Debug.cpp accounting.
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000204 // Map in low 4gb to simplify accessing root tables for x86_64.
205 // We could do PC-relative addressing to avoid this problem, but that
206 // would require reserving code and data area before submitting, which
207 // means more windows for the code memory to be RWX.
Andreas Gampee4deaf32017-06-09 15:27:15 -0700208 std::unique_ptr<MemMap> data_map(MemMap::MapAnonymous(
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000209 "data-code-cache", nullptr,
210 max_capacity,
Andreas Gampee4deaf32017-06-09 15:27:15 -0700211 kProtData,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000212 /* low_4gb */ true,
213 /* reuse */ false,
214 &error_str,
Andreas Gampee4deaf32017-06-09 15:27:15 -0700215 use_ashmem));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100216 if (data_map == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 std::ostringstream oss;
Andreas Gampee4deaf32017-06-09 15:27:15 -0700218 oss << "Failed to create read write cache: " << error_str << " size=" << max_capacity;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219 *error_msg = oss.str();
220 return nullptr;
221 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100222
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100223 // Align both capacities to page size, as that's the unit mspaces use.
224 initial_capacity = RoundDown(initial_capacity, 2 * kPageSize);
225 max_capacity = RoundDown(max_capacity, 2 * kPageSize);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100226
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100227 // Data cache is 1 / 2 of the map.
228 // TODO: Make this variable?
229 size_t data_size = max_capacity / 2;
230 size_t code_size = max_capacity - data_size;
231 DCHECK_EQ(code_size + data_size, max_capacity);
232 uint8_t* divider = data_map->Begin() + data_size;
David Sehrd1dbb742017-07-17 11:20:38 -0700233
Calin Juravle016fcbe22018-05-03 19:47:35 -0700234 MemMap* code_map = data_map->RemapAtEnd(
235 divider,
236 "jit-code-cache",
237 memmap_flags_prot_code | PROT_WRITE,
238 &error_str, use_ashmem);
David Sehrd1dbb742017-07-17 11:20:38 -0700239 if (code_map == nullptr) {
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100240 std::ostringstream oss;
241 oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
242 *error_msg = oss.str();
David Sehrd1dbb742017-07-17 11:20:38 -0700243 return nullptr;
244 }
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100245 DCHECK_EQ(code_map->Begin(), divider);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000246 data_size = initial_capacity / 2;
247 code_size = initial_capacity - data_size;
248 DCHECK_EQ(code_size + data_size, initial_capacity);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100249 return new JitCodeCache(
Calin Juravle016fcbe22018-05-03 19:47:35 -0700250 code_map,
251 data_map.release(),
252 code_size,
253 data_size,
254 max_capacity,
255 garbage_collect_code,
256 memmap_flags_prot_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257}
258
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100259JitCodeCache::JitCodeCache(MemMap* code_map,
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000260 MemMap* data_map,
261 size_t initial_code_capacity,
262 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000263 size_t max_capacity,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700264 bool garbage_collect_code,
265 int memmap_flags_prot_code)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100266 : lock_("Jit code cache", kJitCodeCacheLock),
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000267 lock_cond_("Jit code cache condition variable", lock_),
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100268 collection_in_progress_(false),
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100269 code_map_(code_map),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000270 data_map_(data_map),
271 max_capacity_(max_capacity),
272 current_capacity_(initial_code_capacity + initial_data_capacity),
273 code_end_(initial_code_capacity),
274 data_end_(initial_data_capacity),
Nicolas Geoffray35122442016-03-02 12:05:30 +0000275 last_collection_increased_code_cache_(false),
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000276 garbage_collect_code_(garbage_collect_code),
Nicolas Geoffrayb0d22082016-02-24 17:18:25 +0000277 used_memory_for_data_(0),
278 used_memory_for_code_(0),
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000279 number_of_compilations_(0),
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000280 number_of_osr_compilations_(0),
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000281 number_of_collections_(0),
282 histogram_stack_map_memory_use_("Memory used for stack maps", 16),
283 histogram_code_memory_use_("Memory used for compiled code", 16),
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000284 histogram_profiling_info_memory_use_("Memory used for profiling info", 16),
285 is_weak_access_enabled_(true),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700286 inline_cache_cond_("Jit inline cache condition variable", lock_),
287 memmap_flags_prot_code_(memmap_flags_prot_code) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100288
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000289 DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100290 code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000291 data_mspace_ = create_mspace_with_base(data_map_->Begin(), data_end_, false /*locked*/);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100292
293 if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
294 PLOG(FATAL) << "create_mspace_with_base failed";
295 }
296
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000297 SetFootprintLimit(current_capacity_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100298
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700299 CheckedCall(mprotect,
300 "mprotect jit code cache",
301 code_map_->Begin(),
302 code_map_->Size(),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700303 memmap_flags_prot_code_);
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700304 CheckedCall(mprotect,
305 "mprotect jit data cache",
306 data_map_->Begin(),
307 data_map_->Size(),
308 kProtData);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100309
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000310 VLOG(jit) << "Created jit code cache: initial data size="
311 << PrettySize(initial_data_capacity)
312 << ", initial code size="
313 << PrettySize(initial_code_capacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800314}
315
Vladimir Markob0b68cf2017-11-14 18:11:50 +0000316JitCodeCache::~JitCodeCache() {}
317
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100318bool JitCodeCache::ContainsPc(const void* ptr) const {
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100319 return code_map_->Begin() <= ptr && ptr < code_map_->End();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800320}
321
Alex Light2d441b12018-06-08 15:33:21 -0700322bool JitCodeCache::WillExecuteJitCode(ArtMethod* method) {
323 ScopedObjectAccess soa(art::Thread::Current());
324 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
325 if (ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
326 return true;
327 } else if (method->GetEntryPointFromQuickCompiledCode() == GetQuickInstrumentationEntryPoint()) {
328 return FindCompiledCodeForInstrumentation(method) != nullptr;
329 }
330 return false;
331}
332
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000333bool JitCodeCache::ContainsMethod(ArtMethod* method) {
334 MutexLock mu(Thread::Current(), lock_);
Vladimir Marko2196c652017-11-30 16:16:07 +0000335 if (UNLIKELY(method->IsNative())) {
336 auto it = jni_stubs_map_.find(JniStubKey(method));
337 if (it != jni_stubs_map_.end() &&
338 it->second.IsCompiled() &&
339 ContainsElement(it->second.GetMethods(), method)) {
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000340 return true;
341 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000342 } else {
343 for (const auto& it : method_code_map_) {
344 if (it.second == method) {
345 return true;
346 }
347 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000348 }
349 return false;
350}
351
Vladimir Marko2196c652017-11-30 16:16:07 +0000352const void* JitCodeCache::GetJniStubCode(ArtMethod* method) {
353 DCHECK(method->IsNative());
354 MutexLock mu(Thread::Current(), lock_);
355 auto it = jni_stubs_map_.find(JniStubKey(method));
356 if (it != jni_stubs_map_.end()) {
357 JniStubData& data = it->second;
358 if (data.IsCompiled() && ContainsElement(data.GetMethods(), method)) {
359 return data.GetCode();
360 }
361 }
362 return nullptr;
363}
364
Alex Light25bf4462018-06-11 10:28:06 -0700365void JitCodeCache::ClearAllCompiledDexCode() {
366 MutexLock mu(Thread::Current(), lock_);
367 // Get rid of OSR code waiting to be put on a thread.
368 osr_code_map_.clear();
369
370 // We don't clear out or even touch method_code_map_ since that is what we use to go the other
371 // way, move from code currently-running to the method it's from. Getting rid of it would break
372 // the jit-gc, stack-walking and signal handling. Since we never look through it to go the other
373 // way (from method -> code) everything is fine.
374
375 for (ProfilingInfo* p : profiling_infos_) {
376 p->SetSavedEntryPoint(nullptr);
377 }
378}
379
Alex Light2d441b12018-06-08 15:33:21 -0700380const void* JitCodeCache::FindCompiledCodeForInstrumentation(ArtMethod* method) {
Alex Light839f53a2018-07-10 15:46:14 -0700381 // If jit-gc is still on we use the SavedEntryPoint field for doing that and so cannot use it to
382 // find the instrumentation entrypoint.
383 if (LIKELY(GetGarbageCollectCode())) {
Alex Light2d441b12018-06-08 15:33:21 -0700384 return nullptr;
385 }
386 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
387 if (info == nullptr) {
388 return nullptr;
389 }
390 // When GC is disabled for trampoline tracing we will use SavedEntrypoint to hold the actual
391 // jit-compiled version of the method. If jit-gc is disabled for other reasons this will just be
392 // nullptr.
393 return info->GetSavedEntryPoint();
394}
395
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800396class ScopedCodeCacheWrite : ScopedTrace {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100397 public:
Calin Juravle016fcbe22018-05-03 19:47:35 -0700398 explicit ScopedCodeCacheWrite(const JitCodeCache* const code_cache)
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100399 : ScopedTrace("ScopedCodeCacheWrite"),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700400 code_cache_(code_cache) {
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800401 ScopedTrace trace("mprotect all");
Calin Juravle016fcbe22018-05-03 19:47:35 -0700402 CheckedCall(
403 mprotect,
404 "make code writable",
405 code_cache_->code_map_->Begin(),
406 code_cache_->code_map_->Size(),
407 code_cache_->memmap_flags_prot_code_ | PROT_WRITE);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800408 }
Calin Juravle016fcbe22018-05-03 19:47:35 -0700409
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100410 ~ScopedCodeCacheWrite() {
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800411 ScopedTrace trace("mprotect code");
Calin Juravle016fcbe22018-05-03 19:47:35 -0700412 CheckedCall(
413 mprotect,
414 "make code protected",
415 code_cache_->code_map_->Begin(),
416 code_cache_->code_map_->Size(),
417 code_cache_->memmap_flags_prot_code_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100418 }
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700419
David Sehrd1dbb742017-07-17 11:20:38 -0700420 private:
Calin Juravle016fcbe22018-05-03 19:47:35 -0700421 const JitCodeCache* const code_cache_;
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100422
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100423 DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
424};
425
426uint8_t* JitCodeCache::CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100427 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000428 uint8_t* stack_map,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700429 uint8_t* method_info,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000430 uint8_t* roots_data,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100431 size_t frame_size_in_bytes,
432 size_t core_spill_mask,
433 size_t fp_spill_mask,
434 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000435 size_t code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100436 size_t data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000437 bool osr,
Mingyao Yang063fc772016-08-02 11:02:54 -0700438 Handle<mirror::ObjectArray<mirror::Object>> roots,
439 bool has_should_deoptimize_flag,
440 const ArenaSet<ArtMethod*>& cha_single_implementation_list) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100441 uint8_t* result = CommitCodeInternal(self,
442 method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000443 stack_map,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700444 method_info,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000445 roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100446 frame_size_in_bytes,
447 core_spill_mask,
448 fp_spill_mask,
449 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000450 code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100451 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000452 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -0700453 roots,
454 has_should_deoptimize_flag,
455 cha_single_implementation_list);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100456 if (result == nullptr) {
457 // Retry.
458 GarbageCollectCache(self);
459 result = CommitCodeInternal(self,
460 method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000461 stack_map,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700462 method_info,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000463 roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100464 frame_size_in_bytes,
465 core_spill_mask,
466 fp_spill_mask,
467 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000468 code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100469 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000470 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -0700471 roots,
472 has_should_deoptimize_flag,
473 cha_single_implementation_list);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100474 }
475 return result;
476}
477
478bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
479 bool in_collection = false;
480 while (collection_in_progress_) {
481 in_collection = true;
482 lock_cond_.Wait(self);
483 }
484 return in_collection;
485}
486
487static uintptr_t FromCodeToAllocation(const void* code) {
488 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
489 return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
490}
491
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000492static uint32_t ComputeRootTableSize(uint32_t number_of_roots) {
493 return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>);
494}
495
496static uint32_t GetNumberOfRoots(const uint8_t* stack_map) {
497 // The length of the table is stored just before the stack map (and therefore at the end of
498 // the table itself), in order to be able to fetch it from a `stack_map` pointer.
499 return reinterpret_cast<const uint32_t*>(stack_map)[-1];
500}
501
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800502static void FillRootTableLength(uint8_t* roots_data, uint32_t length) {
503 // Store the length of the table at the end. This will allow fetching it from a `stack_map`
504 // pointer.
505 reinterpret_cast<uint32_t*>(roots_data)[length] = length;
506}
507
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +0000508static const uint8_t* FromStackMapToRoots(const uint8_t* stack_map_data) {
509 return stack_map_data - ComputeRootTableSize(GetNumberOfRoots(stack_map_data));
510}
511
Alex Light3e36a9c2018-06-19 09:45:05 -0700512static void DCheckRootsAreValid(Handle<mirror::ObjectArray<mirror::Object>> roots)
513 REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_) {
514 if (!kIsDebugBuild) {
515 return;
516 }
517 const uint32_t length = roots->GetLength();
518 // Put all roots in `roots_data`.
519 for (uint32_t i = 0; i < length; ++i) {
520 ObjPtr<mirror::Object> object = roots->Get(i);
521 // Ensure the string is strongly interned. b/32995596
522 if (object->IsString()) {
523 ObjPtr<mirror::String> str = ObjPtr<mirror::String>::DownCast(object);
524 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
525 CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr);
526 }
527 }
528}
529
530void JitCodeCache::FillRootTable(uint8_t* roots_data,
531 Handle<mirror::ObjectArray<mirror::Object>> roots) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000532 GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800533 const uint32_t length = roots->GetLength();
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000534 // Put all roots in `roots_data`.
535 for (uint32_t i = 0; i < length; ++i) {
536 ObjPtr<mirror::Object> object = roots->Get(i);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000537 gc_roots[i] = GcRoot<mirror::Object>(object);
538 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000539}
540
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100541static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000542 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
543 uint8_t* data = method_header->GetOptimizedCodeInfoPtr();
544 uint32_t roots = GetNumberOfRoots(data);
545 if (number_of_roots != nullptr) {
546 *number_of_roots = roots;
547 }
548 return data - ComputeRootTableSize(roots);
549}
550
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100551// Use a sentinel for marking entries in the JIT table that have been cleared.
552// This helps diagnosing in case the compiled code tries to wrongly access such
553// entries.
Andreas Gampe5629d2d2017-05-15 16:28:13 -0700554static mirror::Class* const weak_sentinel =
555 reinterpret_cast<mirror::Class*>(Context::kBadGprBase + 0xff);
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100556
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000557// Helper for the GC to process a weak class in a JIT root table.
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100558static inline void ProcessWeakClass(GcRoot<mirror::Class>* root_ptr,
559 IsMarkedVisitor* visitor,
560 mirror::Class* update)
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000561 REQUIRES_SHARED(Locks::mutator_lock_) {
562 // This does not need a read barrier because this is called by GC.
563 mirror::Class* cls = root_ptr->Read<kWithoutReadBarrier>();
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100564 if (cls != nullptr && cls != weak_sentinel) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000565 DCHECK((cls->IsClass<kDefaultVerifyFlags, kWithoutReadBarrier>()));
566 // Look at the classloader of the class to know if it has been unloaded.
567 // This does not need a read barrier because this is called by GC.
568 mirror::Object* class_loader =
569 cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>();
570 if (class_loader == nullptr || visitor->IsMarked(class_loader) != nullptr) {
571 // The class loader is live, update the entry if the class has moved.
572 mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls));
573 // Note that new_object can be null for CMS and newly allocated objects.
574 if (new_cls != nullptr && new_cls != cls) {
575 *root_ptr = GcRoot<mirror::Class>(new_cls);
576 }
577 } else {
578 // The class loader is not live, clear the entry.
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100579 *root_ptr = GcRoot<mirror::Class>(update);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000580 }
581 }
582}
583
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000584void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) {
585 MutexLock mu(Thread::Current(), lock_);
586 for (const auto& entry : method_code_map_) {
587 uint32_t number_of_roots = 0;
588 uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots);
589 GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
590 for (uint32_t i = 0; i < number_of_roots; ++i) {
591 // This does not need a read barrier because this is called by GC.
592 mirror::Object* object = roots[i].Read<kWithoutReadBarrier>();
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100593 if (object == nullptr || object == weak_sentinel) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000594 // entry got deleted in a previous sweep.
595 } else if (object->IsString<kDefaultVerifyFlags, kWithoutReadBarrier>()) {
596 mirror::Object* new_object = visitor->IsMarked(object);
597 // We know the string is marked because it's a strongly-interned string that
598 // is always alive. The IsMarked implementation of the CMS collector returns
599 // null for newly allocated objects, but we know those haven't moved. Therefore,
600 // only update the entry if we get a different non-null string.
601 // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method
602 // out of the weak access/creation pause. b/32167580
603 if (new_object != nullptr && new_object != object) {
604 DCHECK(new_object->IsString());
605 roots[i] = GcRoot<mirror::Object>(new_object);
606 }
607 } else {
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100608 ProcessWeakClass(
609 reinterpret_cast<GcRoot<mirror::Class>*>(&roots[i]), visitor, weak_sentinel);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000610 }
611 }
612 }
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000613 // Walk over inline caches to clear entries containing unloaded classes.
614 for (ProfilingInfo* info : profiling_infos_) {
615 for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
616 InlineCache* cache = &info->cache_[i];
617 for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100618 ProcessWeakClass(&cache->classes_[j], visitor, nullptr);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000619 }
620 }
621 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000622}
623
Orion Hodson607624f2018-05-11 10:10:46 +0100624void JitCodeCache::FreeCodeAndData(const void* code_ptr) {
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100625 uintptr_t allocation = FromCodeToAllocation(code_ptr);
David Srbecky5cc349f2015-12-18 15:04:48 +0000626 // Notify native debugger that we are about to remove the code.
627 // It does nothing if we are not using native debugger.
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000628 MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_);
David Srbecky440a9b32018-02-15 17:47:29 +0000629 RemoveNativeDebugInfoForJit(code_ptr);
Vladimir Marko2196c652017-11-30 16:16:07 +0000630 if (OatQuickMethodHeader::FromCodePointer(code_ptr)->IsOptimized()) {
631 FreeData(GetRootTable(code_ptr));
632 } // else this is a JNI stub without any data.
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100633 FreeCode(reinterpret_cast<uint8_t*>(allocation));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100634}
635
Mingyao Yang063fc772016-08-02 11:02:54 -0700636void JitCodeCache::FreeAllMethodHeaders(
637 const std::unordered_set<OatQuickMethodHeader*>& method_headers) {
638 {
639 MutexLock mu(Thread::Current(), *Locks::cha_lock_);
Andreas Gampec1ac9ee2017-07-24 22:35:49 -0700640 Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()
Mingyao Yang063fc772016-08-02 11:02:54 -0700641 ->RemoveDependentsWithMethodHeaders(method_headers);
642 }
643
644 // We need to remove entries in method_headers from CHA dependencies
645 // first since once we do FreeCode() below, the memory can be reused
646 // so it's possible for the same method_header to start representing
647 // different compile code.
648 MutexLock mu(Thread::Current(), lock_);
Calin Juravle016fcbe22018-05-03 19:47:35 -0700649 ScopedCodeCacheWrite scc(this);
Mingyao Yang063fc772016-08-02 11:02:54 -0700650 for (const OatQuickMethodHeader* method_header : method_headers) {
Orion Hodson607624f2018-05-11 10:10:46 +0100651 FreeCodeAndData(method_header->GetCode());
Mingyao Yang063fc772016-08-02 11:02:54 -0700652 }
653}
654
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100655void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800656 ScopedTrace trace(__PRETTY_FUNCTION__);
Mingyao Yang063fc772016-08-02 11:02:54 -0700657 // We use a set to first collect all method_headers whose code need to be
658 // removed. We need to free the underlying code after we remove CHA dependencies
659 // for entries in this set. And it's more efficient to iterate through
660 // the CHA dependency map just once with an unordered_set.
661 std::unordered_set<OatQuickMethodHeader*> method_headers;
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000662 {
Mingyao Yang063fc772016-08-02 11:02:54 -0700663 MutexLock mu(self, lock_);
664 // We do not check if a code cache GC is in progress, as this method comes
665 // with the classlinker_classes_lock_ held, and suspending ourselves could
666 // lead to a deadlock.
667 {
Calin Juravle016fcbe22018-05-03 19:47:35 -0700668 ScopedCodeCacheWrite scc(this);
Vladimir Marko2196c652017-11-30 16:16:07 +0000669 for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) {
670 it->second.RemoveMethodsIn(alloc);
671 if (it->second.GetMethods().empty()) {
672 method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->second.GetCode()));
673 it = jni_stubs_map_.erase(it);
674 } else {
675 it->first.UpdateShorty(it->second.GetMethods().front());
676 ++it;
677 }
678 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700679 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
680 if (alloc.ContainsUnsafe(it->second)) {
681 method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first));
682 it = method_code_map_.erase(it);
683 } else {
684 ++it;
685 }
686 }
687 }
688 for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
689 if (alloc.ContainsUnsafe(it->first)) {
690 // Note that the code has already been pushed to method_headers in the loop
691 // above and is going to be removed in FreeCode() below.
692 it = osr_code_map_.erase(it);
693 } else {
694 ++it;
695 }
696 }
697 for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
698 ProfilingInfo* info = *it;
699 if (alloc.ContainsUnsafe(info->GetMethod())) {
700 info->GetMethod()->SetProfilingInfo(nullptr);
701 FreeData(reinterpret_cast<uint8_t*>(info));
702 it = profiling_infos_.erase(it);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000703 } else {
704 ++it;
705 }
706 }
707 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700708 FreeAllMethodHeaders(method_headers);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100709}
710
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000711bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const {
712 return kUseReadBarrier
713 ? self->GetWeakRefAccessEnabled()
Orion Hodson88591fe2018-03-06 13:35:43 +0000714 : is_weak_access_enabled_.load(std::memory_order_seq_cst);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000715}
716
717void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) {
718 if (IsWeakAccessEnabled(self)) {
719 return;
720 }
721 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000722 MutexLock mu(self, lock_);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000723 while (!IsWeakAccessEnabled(self)) {
724 inline_cache_cond_.Wait(self);
725 }
726}
727
728void JitCodeCache::BroadcastForInlineCacheAccess() {
729 Thread* self = Thread::Current();
730 MutexLock mu(self, lock_);
731 inline_cache_cond_.Broadcast(self);
732}
733
734void JitCodeCache::AllowInlineCacheAccess() {
735 DCHECK(!kUseReadBarrier);
Orion Hodson88591fe2018-03-06 13:35:43 +0000736 is_weak_access_enabled_.store(true, std::memory_order_seq_cst);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000737 BroadcastForInlineCacheAccess();
738}
739
740void JitCodeCache::DisallowInlineCacheAccess() {
741 DCHECK(!kUseReadBarrier);
Orion Hodson88591fe2018-03-06 13:35:43 +0000742 is_weak_access_enabled_.store(false, std::memory_order_seq_cst);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000743}
744
745void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic,
746 Handle<mirror::ObjectArray<mirror::Class>> array) {
747 WaitUntilInlineCacheAccessible(Thread::Current());
748 // Note that we don't need to lock `lock_` here, the compiler calling
749 // this method has already ensured the inline cache will not be deleted.
750 for (size_t in_cache = 0, in_array = 0;
751 in_cache < InlineCache::kIndividualCacheSize;
752 ++in_cache) {
753 mirror::Class* object = ic.classes_[in_cache].Read();
754 if (object != nullptr) {
755 array->Set(in_array++, object);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000756 }
757 }
758}
759
Mathieu Chartierf044c222017-05-31 15:27:54 -0700760static void ClearMethodCounter(ArtMethod* method, bool was_warm) {
761 if (was_warm) {
Nicolas Geoffray34088e12018-03-08 10:56:09 +0000762 // Don't do any read barrier, as the declaring class of `method` may
763 // be in the process of being GC'ed (reading the declaring class is done
764 // when DCHECKing the declaring class is resolved, which we know it is
765 // at this point).
766 method->SetPreviouslyWarm<kWithoutReadBarrier>();
Mathieu Chartierf044c222017-05-31 15:27:54 -0700767 }
768 // We reset the counter to 1 so that the profile knows that the method was executed at least once.
769 // This is required for layout purposes.
Nicolas Geoffray88f50b12017-06-09 16:08:47 +0100770 // We also need to make sure we'll pass the warmup threshold again, so we set to 0 if
771 // the warmup threshold is 1.
772 uint16_t jit_warmup_threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
773 method->SetCounter(std::min(jit_warmup_threshold - 1, 1));
Mathieu Chartierf044c222017-05-31 15:27:54 -0700774}
775
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100776uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
777 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000778 uint8_t* stack_map,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700779 uint8_t* method_info,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000780 uint8_t* roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100781 size_t frame_size_in_bytes,
782 size_t core_spill_mask,
783 size_t fp_spill_mask,
784 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000785 size_t code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100786 size_t data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000787 bool osr,
Mingyao Yang063fc772016-08-02 11:02:54 -0700788 Handle<mirror::ObjectArray<mirror::Object>> roots,
789 bool has_should_deoptimize_flag,
790 const ArenaSet<ArtMethod*>&
791 cha_single_implementation_list) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000792 DCHECK(!method->IsNative() || !osr);
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100793 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
794 // Ensure the header ends up at expected instruction alignment.
795 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
796 size_t total_size = header_size + code_size;
797
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100798 OatQuickMethodHeader* method_header = nullptr;
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100799 uint8_t* code_ptr = nullptr;
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000800 uint8_t* memory = nullptr;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100801 {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000802 ScopedThreadSuspension sts(self, kSuspended);
803 MutexLock mu(self, lock_);
804 WaitForPotentialCollectionToComplete(self);
805 {
Calin Juravle016fcbe22018-05-03 19:47:35 -0700806 ScopedCodeCacheWrite scc(this);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000807 memory = AllocateCode(total_size);
808 if (memory == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000809 return nullptr;
810 }
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100811 code_ptr = memory + header_size;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000812
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100813 std::copy(code, code + code_size, code_ptr);
814 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
815 new (method_header) OatQuickMethodHeader(
Vladimir Marko2196c652017-11-30 16:16:07 +0000816 (stack_map != nullptr) ? code_ptr - stack_map : 0u,
817 (method_info != nullptr) ? code_ptr - method_info : 0u,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000818 frame_size_in_bytes,
819 core_spill_mask,
820 fp_spill_mask,
821 code_size);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100822 // Flush caches before we remove write permission because some ARMv8 Qualcomm kernels may
823 // trigger a segfault if a page fault occurs when requesting a cache maintenance operation.
824 // This is a kernel bug that we need to work around until affected devices (e.g. Nexus 5X and
825 // 6P) stop being supported or their kernels are fixed.
826 //
827 // For reference, this behavior is caused by this commit:
828 // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c
829 FlushInstructionCache(reinterpret_cast<char*>(code_ptr),
830 reinterpret_cast<char*>(code_ptr + code_size));
Mingyao Yang063fc772016-08-02 11:02:54 -0700831 DCHECK(!Runtime::Current()->IsAotCompiler());
832 if (has_should_deoptimize_flag) {
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100833 method_header->SetHasShouldDeoptimizeFlag();
Mingyao Yang063fc772016-08-02 11:02:54 -0700834 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100835 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100836
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000837 number_of_compilations_++;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100838 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000839 // We need to update the entry point in the runnable state for the instrumentation.
840 {
Mingyao Yang063fc772016-08-02 11:02:54 -0700841 // Need cha_lock_ for checking all single-implementation flags and register
842 // dependencies.
843 MutexLock cha_mu(self, *Locks::cha_lock_);
844 bool single_impl_still_valid = true;
845 for (ArtMethod* single_impl : cha_single_implementation_list) {
846 if (!single_impl->HasSingleImplementation()) {
Jeff Hao00286db2017-05-30 16:53:07 -0700847 // Simply discard the compiled code. Clear the counter so that it may be recompiled later.
848 // Hopefully the class hierarchy will be more stable when compilation is retried.
Mingyao Yang063fc772016-08-02 11:02:54 -0700849 single_impl_still_valid = false;
Mathieu Chartierf044c222017-05-31 15:27:54 -0700850 ClearMethodCounter(method, /*was_warm*/ false);
Mingyao Yang063fc772016-08-02 11:02:54 -0700851 break;
852 }
853 }
854
855 // Discard the code if any single-implementation assumptions are now invalid.
856 if (!single_impl_still_valid) {
857 VLOG(jit) << "JIT discarded jitted code due to invalid single-implementation assumptions.";
858 return nullptr;
859 }
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000860 DCHECK(cha_single_implementation_list.empty() || !Runtime::Current()->IsJavaDebuggable())
Alex Lightdba61482016-12-21 08:20:29 -0800861 << "Should not be using cha on debuggable apps/runs!";
862
Mingyao Yang063fc772016-08-02 11:02:54 -0700863 for (ArtMethod* single_impl : cha_single_implementation_list) {
Andreas Gampec1ac9ee2017-07-24 22:35:49 -0700864 Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()->AddDependency(
Mingyao Yang063fc772016-08-02 11:02:54 -0700865 single_impl, method, method_header);
866 }
867
Alex Light3e36a9c2018-06-19 09:45:05 -0700868 if (!method->IsNative()) {
869 // We need to do this before grabbing the lock_ because it needs to be able to see the string
870 // InternTable. Native methods do not have roots.
871 DCheckRootsAreValid(roots);
872 }
873
Mingyao Yang063fc772016-08-02 11:02:54 -0700874 // The following needs to be guarded by cha_lock_ also. Otherwise it's
875 // possible that the compiled code is considered invalidated by some class linking,
876 // but below we still make the compiled code valid for the method.
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000877 MutexLock mu(self, lock_);
Vladimir Marko2196c652017-11-30 16:16:07 +0000878 if (UNLIKELY(method->IsNative())) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000879 auto it = jni_stubs_map_.find(JniStubKey(method));
880 DCHECK(it != jni_stubs_map_.end())
881 << "Entry inserted in NotifyCompilationOf() should be alive.";
882 JniStubData* data = &it->second;
883 DCHECK(ContainsElement(data->GetMethods(), method))
884 << "Entry inserted in NotifyCompilationOf() should contain this method.";
885 data->SetCode(code_ptr);
886 instrumentation::Instrumentation* instrum = Runtime::Current()->GetInstrumentation();
887 for (ArtMethod* m : data->GetMethods()) {
888 instrum->UpdateMethodsCode(m, method_header->GetEntryPoint());
889 }
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100890 } else {
Vladimir Marko2196c652017-11-30 16:16:07 +0000891 // Fill the root table before updating the entry point.
892 DCHECK_EQ(FromStackMapToRoots(stack_map), roots_data);
893 DCHECK_LE(roots_data, stack_map);
894 FillRootTable(roots_data, roots);
895 {
896 // Flush data cache, as compiled code references literals in it.
Vladimir Marko2196c652017-11-30 16:16:07 +0000897 FlushDataCache(reinterpret_cast<char*>(roots_data),
898 reinterpret_cast<char*>(roots_data + data_size));
899 }
900 method_code_map_.Put(code_ptr, method);
901 if (osr) {
902 number_of_osr_compilations_++;
903 osr_code_map_.Put(method, code_ptr);
904 } else {
905 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
906 method, method_header->GetEntryPoint());
907 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000908 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000909 if (collection_in_progress_) {
910 // We need to update the live bitmap if there is a GC to ensure it sees this new
911 // code.
912 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
913 }
914 VLOG(jit)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100915 << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
David Sehr709b0702016-10-13 09:12:37 -0700916 << ArtMethod::PrettyMethod(method) << "@" << method
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000917 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
918 << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
919 << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
Mingyao Yang063fc772016-08-02 11:02:54 -0700920 << reinterpret_cast<const void*>(method_header->GetEntryPoint() +
921 method_header->GetCodeSize());
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000922 histogram_code_memory_use_.AddValue(code_size);
923 if (code_size > kCodeSizeLogThreshold) {
924 LOG(INFO) << "JIT allocated "
925 << PrettySize(code_size)
926 << " for compiled code of "
David Sehr709b0702016-10-13 09:12:37 -0700927 << ArtMethod::PrettyMethod(method);
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000928 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000929 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100930
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100931 return reinterpret_cast<uint8_t*>(method_header);
932}
933
934size_t JitCodeCache::CodeCacheSize() {
935 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000936 return CodeCacheSizeLocked();
937}
938
Orion Hodsoneced6922017-06-01 10:54:28 +0100939bool JitCodeCache::RemoveMethod(ArtMethod* method, bool release_memory) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000940 // This function is used only for testing and only with non-native methods.
941 CHECK(!method->IsNative());
942
Orion Hodsoneced6922017-06-01 10:54:28 +0100943 MutexLock mu(Thread::Current(), lock_);
Orion Hodsoneced6922017-06-01 10:54:28 +0100944
Vladimir Marko2196c652017-11-30 16:16:07 +0000945 bool osr = osr_code_map_.find(method) != osr_code_map_.end();
946 bool in_cache = RemoveMethodLocked(method, release_memory);
Orion Hodsoneced6922017-06-01 10:54:28 +0100947
948 if (!in_cache) {
949 return false;
950 }
951
Orion Hodsoneced6922017-06-01 10:54:28 +0100952 method->ClearCounter();
953 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
954 method, GetQuickToInterpreterBridge());
955 VLOG(jit)
956 << "JIT removed (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
957 << ArtMethod::PrettyMethod(method) << "@" << method
958 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
959 << " dcache_size=" << PrettySize(DataCacheSizeLocked());
960 return true;
961}
962
Vladimir Marko2196c652017-11-30 16:16:07 +0000963bool JitCodeCache::RemoveMethodLocked(ArtMethod* method, bool release_memory) {
964 if (LIKELY(!method->IsNative())) {
965 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
966 if (info != nullptr) {
967 RemoveElement(profiling_infos_, info);
968 }
969 method->SetProfilingInfo(nullptr);
970 }
971
972 bool in_cache = false;
Calin Juravle016fcbe22018-05-03 19:47:35 -0700973 ScopedCodeCacheWrite ccw(this);
Vladimir Marko2196c652017-11-30 16:16:07 +0000974 if (UNLIKELY(method->IsNative())) {
975 auto it = jni_stubs_map_.find(JniStubKey(method));
976 if (it != jni_stubs_map_.end() && it->second.RemoveMethod(method)) {
977 in_cache = true;
978 if (it->second.GetMethods().empty()) {
979 if (release_memory) {
Orion Hodson607624f2018-05-11 10:10:46 +0100980 FreeCodeAndData(it->second.GetCode());
Vladimir Marko2196c652017-11-30 16:16:07 +0000981 }
982 jni_stubs_map_.erase(it);
983 } else {
984 it->first.UpdateShorty(it->second.GetMethods().front());
985 }
986 }
987 } else {
988 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
989 if (it->second == method) {
990 in_cache = true;
991 if (release_memory) {
Orion Hodson607624f2018-05-11 10:10:46 +0100992 FreeCodeAndData(it->first);
Vladimir Marko2196c652017-11-30 16:16:07 +0000993 }
994 it = method_code_map_.erase(it);
995 } else {
996 ++it;
997 }
998 }
999
1000 auto osr_it = osr_code_map_.find(method);
1001 if (osr_it != osr_code_map_.end()) {
1002 osr_code_map_.erase(osr_it);
1003 }
1004 }
1005
1006 return in_cache;
1007}
1008
Alex Lightdba61482016-12-21 08:20:29 -08001009// This notifies the code cache that the given method has been redefined and that it should remove
1010// any cached information it has on the method. All threads must be suspended before calling this
1011// method. The compiled code for the method (if there is any) must not be in any threads call stack.
1012void JitCodeCache::NotifyMethodRedefined(ArtMethod* method) {
1013 MutexLock mu(Thread::Current(), lock_);
Vladimir Marko2196c652017-11-30 16:16:07 +00001014 RemoveMethodLocked(method, /* release_memory */ true);
Alex Lightdba61482016-12-21 08:20:29 -08001015}
1016
1017// This invalidates old_method. Once this function returns one can no longer use old_method to
1018// execute code unless it is fixed up. This fixup will happen later in the process of installing a
1019// class redefinition.
1020// TODO We should add some info to ArtMethod to note that 'old_method' has been invalidated and
1021// shouldn't be used since it is no longer logically in the jit code cache.
1022// TODO We should add DCHECKS that validate that the JIT is paused when this method is entered.
1023void JitCodeCache::MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
Vladimir Marko2196c652017-11-30 16:16:07 +00001024 MutexLock mu(Thread::Current(), lock_);
Alex Lighteee0bd42017-02-14 15:31:45 +00001025 if (old_method->IsNative()) {
Vladimir Marko2196c652017-11-30 16:16:07 +00001026 // Update methods in jni_stubs_map_.
1027 for (auto& entry : jni_stubs_map_) {
1028 JniStubData& data = entry.second;
1029 data.MoveObsoleteMethod(old_method, new_method);
1030 }
Alex Lighteee0bd42017-02-14 15:31:45 +00001031 return;
1032 }
Alex Lightdba61482016-12-21 08:20:29 -08001033 // Update ProfilingInfo to the new one and remove it from the old_method.
1034 if (old_method->GetProfilingInfo(kRuntimePointerSize) != nullptr) {
1035 DCHECK_EQ(old_method->GetProfilingInfo(kRuntimePointerSize)->GetMethod(), old_method);
1036 ProfilingInfo* info = old_method->GetProfilingInfo(kRuntimePointerSize);
1037 old_method->SetProfilingInfo(nullptr);
1038 // Since the JIT should be paused and all threads suspended by the time this is called these
1039 // checks should always pass.
1040 DCHECK(!info->IsInUseByCompiler());
1041 new_method->SetProfilingInfo(info);
Alex Light2d441b12018-06-08 15:33:21 -07001042 // Get rid of the old saved entrypoint if it is there.
1043 info->SetSavedEntryPoint(nullptr);
Alex Lightdba61482016-12-21 08:20:29 -08001044 info->method_ = new_method;
1045 }
1046 // Update method_code_map_ to point to the new method.
1047 for (auto& it : method_code_map_) {
1048 if (it.second == old_method) {
1049 it.second = new_method;
1050 }
1051 }
1052 // Update osr_code_map_ to point to the new method.
1053 auto code_map = osr_code_map_.find(old_method);
1054 if (code_map != osr_code_map_.end()) {
1055 osr_code_map_.Put(new_method, code_map->second);
1056 osr_code_map_.erase(old_method);
1057 }
1058}
1059
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00001060size_t JitCodeCache::CodeCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001061 return used_memory_for_code_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +01001062}
1063
1064size_t JitCodeCache::DataCacheSize() {
1065 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00001066 return DataCacheSizeLocked();
1067}
1068
1069size_t JitCodeCache::DataCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001070 return used_memory_for_data_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001071}
1072
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001073void JitCodeCache::ClearData(Thread* self,
1074 uint8_t* stack_map_data,
1075 uint8_t* roots_data) {
1076 DCHECK_EQ(FromStackMapToRoots(stack_map_data), roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001077 MutexLock mu(self, lock_);
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001078 FreeData(reinterpret_cast<uint8_t*>(roots_data));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001079}
1080
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001081size_t JitCodeCache::ReserveData(Thread* self,
1082 size_t stack_map_size,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001083 size_t method_info_size,
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001084 size_t number_of_roots,
1085 ArtMethod* method,
1086 uint8_t** stack_map_data,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001087 uint8_t** method_info_data,
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001088 uint8_t** roots_data) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001089 size_t table_size = ComputeRootTableSize(number_of_roots);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001090 size_t size = RoundUp(stack_map_size + method_info_size + table_size, sizeof(void*));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001091 uint8_t* result = nullptr;
1092
1093 {
1094 ScopedThreadSuspension sts(self, kSuspended);
1095 MutexLock mu(self, lock_);
1096 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001097 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001098 }
1099
1100 if (result == nullptr) {
1101 // Retry.
1102 GarbageCollectCache(self);
1103 ScopedThreadSuspension sts(self, kSuspended);
1104 MutexLock mu(self, lock_);
1105 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001106 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001107 }
1108
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001109 MutexLock mu(self, lock_);
1110 histogram_stack_map_memory_use_.AddValue(size);
1111 if (size > kStackMapSizeLogThreshold) {
1112 LOG(INFO) << "JIT allocated "
1113 << PrettySize(size)
1114 << " for stack maps of "
David Sehr709b0702016-10-13 09:12:37 -07001115 << ArtMethod::PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001116 }
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001117 if (result != nullptr) {
1118 *roots_data = result;
1119 *stack_map_data = result + table_size;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001120 *method_info_data = *stack_map_data + stack_map_size;
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001121 FillRootTableLength(*roots_data, number_of_roots);
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001122 return size;
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001123 } else {
1124 *roots_data = nullptr;
1125 *stack_map_data = nullptr;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001126 *method_info_data = nullptr;
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001127 return 0;
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001128 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001129}
1130
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001131class MarkCodeVisitor FINAL : public StackVisitor {
1132 public:
1133 MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
1134 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
1135 code_cache_(code_cache_in),
1136 bitmap_(code_cache_->GetLiveBitmap()) {}
1137
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001138 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001139 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
1140 if (method_header == nullptr) {
1141 return true;
1142 }
1143 const void* code = method_header->GetCode();
1144 if (code_cache_->ContainsPc(code)) {
1145 // Use the atomic set version, as multiple threads are executing this code.
1146 bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
1147 }
1148 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001149 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001150
1151 private:
1152 JitCodeCache* const code_cache_;
1153 CodeCacheBitmap* const bitmap_;
1154};
1155
1156class MarkCodeClosure FINAL : public Closure {
1157 public:
1158 MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
1159 : code_cache_(code_cache), barrier_(barrier) {}
1160
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001161 void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001162 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001163 DCHECK(thread == Thread::Current() || thread->IsSuspended());
1164 MarkCodeVisitor visitor(thread, code_cache_);
1165 visitor.WalkStack();
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +00001166 if (kIsDebugBuild) {
1167 // The stack walking code queries the side instrumentation stack if it
1168 // sees an instrumentation exit pc, so the JIT code of methods in that stack
1169 // must have been seen. We sanity check this below.
1170 for (const instrumentation::InstrumentationStackFrame& frame
1171 : *thread->GetInstrumentationStack()) {
1172 // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
1173 // its stack frame, it is not the method owning return_pc_. We just pass null to
1174 // LookupMethodHeader: the method is only checked against in debug builds.
1175 OatQuickMethodHeader* method_header =
Vladimir Marko2196c652017-11-30 16:16:07 +00001176 code_cache_->LookupMethodHeader(frame.return_pc_, /* method */ nullptr);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +00001177 if (method_header != nullptr) {
1178 const void* code = method_header->GetCode();
1179 CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
1180 }
1181 }
1182 }
Mathieu Chartier10d25082015-10-28 18:36:09 -07001183 barrier_->Pass(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001184 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001185
1186 private:
1187 JitCodeCache* const code_cache_;
1188 Barrier* const barrier_;
1189};
1190
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001191void JitCodeCache::NotifyCollectionDone(Thread* self) {
1192 collection_in_progress_ = false;
1193 lock_cond_.Broadcast(self);
1194}
1195
1196void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
1197 size_t per_space_footprint = new_footprint / 2;
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001198 DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001199 DCHECK_EQ(per_space_footprint * 2, new_footprint);
1200 mspace_set_footprint_limit(data_mspace_, per_space_footprint);
1201 {
Calin Juravle016fcbe22018-05-03 19:47:35 -07001202 ScopedCodeCacheWrite scc(this);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001203 mspace_set_footprint_limit(code_mspace_, per_space_footprint);
1204 }
1205}
1206
1207bool JitCodeCache::IncreaseCodeCacheCapacity() {
1208 if (current_capacity_ == max_capacity_) {
1209 return false;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001210 }
1211
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001212 // Double the capacity if we're below 1MB, or increase it by 1MB if
1213 // we're above.
1214 if (current_capacity_ < 1 * MB) {
1215 current_capacity_ *= 2;
1216 } else {
1217 current_capacity_ += 1 * MB;
1218 }
1219 if (current_capacity_ > max_capacity_) {
1220 current_capacity_ = max_capacity_;
1221 }
1222
Nicolas Geoffray646d6382017-08-09 10:50:00 +01001223 VLOG(jit) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001224
1225 SetFootprintLimit(current_capacity_);
1226
1227 return true;
1228}
1229
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001230void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
1231 Barrier barrier(0);
1232 size_t threads_running_checkpoint = 0;
1233 MarkCodeClosure closure(this, &barrier);
1234 threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
1235 // Now that we have run our checkpoint, move to a suspended state and wait
1236 // for other threads to run the checkpoint.
1237 ScopedThreadSuspension sts(self, kSuspended);
1238 if (threads_running_checkpoint != 0) {
1239 barrier.Increment(self, threads_running_checkpoint);
1240 }
1241}
1242
Nicolas Geoffray35122442016-03-02 12:05:30 +00001243bool JitCodeCache::ShouldDoFullCollection() {
1244 if (current_capacity_ == max_capacity_) {
1245 // Always do a full collection when the code cache is full.
1246 return true;
1247 } else if (current_capacity_ < kReservedCapacity) {
1248 // Always do partial collection when the code cache size is below the reserved
1249 // capacity.
1250 return false;
1251 } else if (last_collection_increased_code_cache_) {
1252 // This time do a full collection.
1253 return true;
1254 } else {
1255 // This time do a partial collection.
1256 return false;
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001257 }
1258}
1259
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001260void JitCodeCache::GarbageCollectCache(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001261 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001262 if (!garbage_collect_code_) {
1263 MutexLock mu(self, lock_);
1264 IncreaseCodeCacheCapacity();
1265 return;
1266 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001267
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00001268 // Wait for an existing collection, or let everyone know we are starting one.
1269 {
1270 ScopedThreadSuspension sts(self, kSuspended);
1271 MutexLock mu(self, lock_);
1272 if (WaitForPotentialCollectionToComplete(self)) {
1273 return;
1274 } else {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001275 number_of_collections_++;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001276 live_bitmap_.reset(CodeCacheBitmap::Create(
1277 "code-cache-bitmap",
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001278 reinterpret_cast<uintptr_t>(code_map_->Begin()),
1279 reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001280 collection_in_progress_ = true;
1281 }
1282 }
1283
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001284 TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit));
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001285 {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001286 TimingLogger::ScopedTiming st("Code cache collection", &logger);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001287
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001288 bool do_full_collection = false;
1289 {
1290 MutexLock mu(self, lock_);
1291 do_full_collection = ShouldDoFullCollection();
Nicolas Geoffraya96917a2016-03-01 22:18:02 +00001292 }
1293
Nicolas Geoffray646d6382017-08-09 10:50:00 +01001294 VLOG(jit) << "Do "
1295 << (do_full_collection ? "full" : "partial")
1296 << " code cache collection, code="
1297 << PrettySize(CodeCacheSize())
1298 << ", data=" << PrettySize(DataCacheSize());
Nicolas Geoffray35122442016-03-02 12:05:30 +00001299
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001300 DoCollection(self, /* collect_profiling_info */ do_full_collection);
1301
Nicolas Geoffray646d6382017-08-09 10:50:00 +01001302 VLOG(jit) << "After code cache collection, code="
1303 << PrettySize(CodeCacheSize())
1304 << ", data=" << PrettySize(DataCacheSize());
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001305
1306 {
1307 MutexLock mu(self, lock_);
1308
1309 // Increase the code cache only when we do partial collections.
1310 // TODO: base this strategy on how full the code cache is?
1311 if (do_full_collection) {
1312 last_collection_increased_code_cache_ = false;
1313 } else {
1314 last_collection_increased_code_cache_ = true;
1315 IncreaseCodeCacheCapacity();
Nicolas Geoffray35122442016-03-02 12:05:30 +00001316 }
1317
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001318 bool next_collection_will_be_full = ShouldDoFullCollection();
1319
1320 // Start polling the liveness of compiled code to prepare for the next full collection.
Nicolas Geoffray480d5102016-04-18 12:09:30 +01001321 if (next_collection_will_be_full) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001322 // Save the entry point of methods we have compiled, and update the entry
1323 // point of those methods to the interpreter. If the method is invoked, the
1324 // interpreter will update its entry point to the compiled code and call it.
1325 for (ProfilingInfo* info : profiling_infos_) {
1326 const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1327 if (ContainsPc(entry_point)) {
1328 info->SetSavedEntryPoint(entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +00001329 // Don't call Instrumentation::UpdateMethodsCode(), as it can check the declaring
Nicolas Geoffray3b1a7f42017-02-22 10:21:00 +00001330 // class of the method. We may be concurrently running a GC which makes accessing
1331 // the class unsafe. We know it is OK to bypass the instrumentation as we've just
1332 // checked that the current entry point is JIT compiled code.
1333 info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001334 }
1335 }
1336
1337 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
Vladimir Marko2196c652017-11-30 16:16:07 +00001338
1339 // Change entry points of native methods back to the GenericJNI entrypoint.
1340 for (const auto& entry : jni_stubs_map_) {
1341 const JniStubData& data = entry.second;
1342 if (!data.IsCompiled()) {
1343 continue;
1344 }
1345 // Make sure a single invocation of the GenericJNI trampoline tries to recompile.
1346 uint16_t new_counter = Runtime::Current()->GetJit()->HotMethodThreshold() - 1u;
1347 const OatQuickMethodHeader* method_header =
1348 OatQuickMethodHeader::FromCodePointer(data.GetCode());
1349 for (ArtMethod* method : data.GetMethods()) {
1350 if (method->GetEntryPointFromQuickCompiledCode() == method_header->GetEntryPoint()) {
1351 // Don't call Instrumentation::UpdateMethodsCode(), same as for normal methods above.
1352 method->SetCounter(new_counter);
1353 method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
1354 }
1355 }
1356 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001357 }
1358 live_bitmap_.reset(nullptr);
1359 NotifyCollectionDone(self);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001360 }
Nicolas Geoffray35122442016-03-02 12:05:30 +00001361 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001362 Runtime::Current()->GetJit()->AddTimingLogger(logger);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001363}
1364
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001365void JitCodeCache::RemoveUnmarkedCode(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001366 ScopedTrace trace(__FUNCTION__);
Mingyao Yang063fc772016-08-02 11:02:54 -07001367 std::unordered_set<OatQuickMethodHeader*> method_headers;
1368 {
1369 MutexLock mu(self, lock_);
Calin Juravle016fcbe22018-05-03 19:47:35 -07001370 ScopedCodeCacheWrite scc(this);
Mingyao Yang063fc772016-08-02 11:02:54 -07001371 // Iterate over all compiled code and remove entries that are not marked.
Vladimir Marko2196c652017-11-30 16:16:07 +00001372 for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) {
1373 JniStubData* data = &it->second;
1374 if (!data->IsCompiled() || GetLiveBitmap()->Test(FromCodeToAllocation(data->GetCode()))) {
1375 ++it;
1376 } else {
1377 method_headers.insert(OatQuickMethodHeader::FromCodePointer(data->GetCode()));
1378 it = jni_stubs_map_.erase(it);
1379 }
1380 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001381 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
1382 const void* code_ptr = it->first;
1383 uintptr_t allocation = FromCodeToAllocation(code_ptr);
1384 if (GetLiveBitmap()->Test(allocation)) {
1385 ++it;
1386 } else {
Alex Light2d441b12018-06-08 15:33:21 -07001387 OatQuickMethodHeader* header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1388 method_headers.insert(header);
Mingyao Yang063fc772016-08-02 11:02:54 -07001389 it = method_code_map_.erase(it);
1390 }
Nicolas Geoffray35122442016-03-02 12:05:30 +00001391 }
1392 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001393 FreeAllMethodHeaders(method_headers);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001394}
1395
1396void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001397 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001398 {
1399 MutexLock mu(self, lock_);
1400 if (collect_profiling_info) {
1401 // Clear the profiling info of methods that do not have compiled code as entrypoint.
1402 // Also remove the saved entry point from the ProfilingInfo objects.
1403 for (ProfilingInfo* info : profiling_infos_) {
1404 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001405 if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001406 info->GetMethod()->SetProfilingInfo(nullptr);
1407 }
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +00001408
1409 if (info->GetSavedEntryPoint() != nullptr) {
1410 info->SetSavedEntryPoint(nullptr);
1411 // We are going to move this method back to interpreter. Clear the counter now to
Mathieu Chartierf044c222017-05-31 15:27:54 -07001412 // give it a chance to be hot again.
1413 ClearMethodCounter(info->GetMethod(), /*was_warm*/ true);
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +00001414 }
Nicolas Geoffray35122442016-03-02 12:05:30 +00001415 }
1416 } else if (kIsDebugBuild) {
1417 // Sanity check that the profiling infos do not have a dangling entry point.
1418 for (ProfilingInfo* info : profiling_infos_) {
1419 DCHECK(info->GetSavedEntryPoint() == nullptr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001420 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001421 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001422
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001423 // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not
1424 // an entry point is either:
1425 // - an osr compiled code, that will be removed if not in a thread call stack.
1426 // - discarded compiled code, that will be removed if not in a thread call stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00001427 for (const auto& entry : jni_stubs_map_) {
1428 const JniStubData& data = entry.second;
1429 const void* code_ptr = data.GetCode();
1430 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1431 for (ArtMethod* method : data.GetMethods()) {
1432 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1433 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1434 break;
1435 }
1436 }
1437 }
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001438 for (const auto& it : method_code_map_) {
1439 ArtMethod* method = it.second;
1440 const void* code_ptr = it.first;
1441 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1442 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1443 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1444 }
1445 }
1446
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001447 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001448 // on thread stacks).
1449 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001450 }
1451
1452 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001453 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001454
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001455 // At this point, mutator threads are still running, and entrypoints of methods can
1456 // change. We do know they cannot change to a code cache entry that is not marked,
1457 // therefore we can safely remove those entries.
1458 RemoveUnmarkedCode(self);
Nicolas Geoffraya96917a2016-03-01 22:18:02 +00001459
Nicolas Geoffray35122442016-03-02 12:05:30 +00001460 if (collect_profiling_info) {
1461 MutexLock mu(self, lock_);
1462 // Free all profiling infos of methods not compiled nor being compiled.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001463 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001464 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001465 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray511e41b2016-03-02 17:09:35 +00001466 // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope
1467 // that the compiled code would not get revived. As mutator threads run concurrently,
1468 // they may have revived the compiled code, and now we are in the situation where
1469 // a method has compiled code but no ProfilingInfo.
1470 // We make sure compiled methods have a ProfilingInfo object. It is needed for
1471 // code cache collection.
Andreas Gampe542451c2016-07-26 09:02:02 -07001472 if (ContainsPc(ptr) &&
1473 info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001474 info->GetMethod()->SetProfilingInfo(info);
Andreas Gampe542451c2016-07-26 09:02:02 -07001475 } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001476 // No need for this ProfilingInfo object anymore.
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001477 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001478 return true;
1479 }
1480 return false;
1481 });
1482 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray35122442016-03-02 12:05:30 +00001483 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001484 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001485}
1486
Nicolas Geoffray35122442016-03-02 12:05:30 +00001487bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001488 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001489 // Check that methods we have compiled do have a ProfilingInfo object. We would
1490 // have memory leaks of compiled code otherwise.
1491 for (const auto& it : method_code_map_) {
1492 ArtMethod* method = it.second;
Andreas Gampe542451c2016-07-26 09:02:02 -07001493 if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001494 const void* code_ptr = it.first;
1495 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1496 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1497 // If the code is not dead, then we have a problem. Note that this can even
1498 // happen just after a collection, as mutator threads are running in parallel
1499 // and could deoptimize an existing compiled code.
1500 return false;
1501 }
1502 }
1503 }
1504 return true;
1505}
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001506
1507OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001508 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
1509 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001510 // On Thumb-2, the pc is offset by one.
1511 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001512 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001513 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
1514 return nullptr;
1515 }
1516
Vladimir Marko2196c652017-11-30 16:16:07 +00001517 if (!kIsDebugBuild) {
1518 // Called with null `method` only from MarkCodeClosure::Run() in debug build.
1519 CHECK(method != nullptr);
Vladimir Marko47d31852017-11-28 18:36:12 +00001520 }
Vladimir Markoe7441632017-11-29 13:00:56 +00001521
Vladimir Marko2196c652017-11-30 16:16:07 +00001522 MutexLock mu(Thread::Current(), lock_);
1523 OatQuickMethodHeader* method_header = nullptr;
1524 ArtMethod* found_method = nullptr; // Only for DCHECK(), not for JNI stubs.
1525 if (method != nullptr && UNLIKELY(method->IsNative())) {
1526 auto it = jni_stubs_map_.find(JniStubKey(method));
1527 if (it == jni_stubs_map_.end() || !ContainsElement(it->second.GetMethods(), method)) {
1528 return nullptr;
1529 }
1530 const void* code_ptr = it->second.GetCode();
1531 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1532 if (!method_header->Contains(pc)) {
1533 return nullptr;
1534 }
1535 } else {
1536 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
1537 if (it != method_code_map_.begin()) {
1538 --it;
1539 const void* code_ptr = it->first;
1540 if (OatQuickMethodHeader::FromCodePointer(code_ptr)->Contains(pc)) {
1541 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1542 found_method = it->second;
1543 }
1544 }
1545 if (method_header == nullptr && method == nullptr) {
1546 // Scan all compiled JNI stubs as well. This slow search is used only
1547 // for checks in debug build, for release builds the `method` is not null.
1548 for (auto&& entry : jni_stubs_map_) {
1549 const JniStubData& data = entry.second;
1550 if (data.IsCompiled() &&
1551 OatQuickMethodHeader::FromCodePointer(data.GetCode())->Contains(pc)) {
1552 method_header = OatQuickMethodHeader::FromCodePointer(data.GetCode());
1553 }
1554 }
1555 }
1556 if (method_header == nullptr) {
1557 return nullptr;
1558 }
Nicolas Geoffray056d7752017-11-30 09:12:13 +00001559 }
Vladimir Marko2196c652017-11-30 16:16:07 +00001560
1561 if (kIsDebugBuild && method != nullptr && !method->IsNative()) {
Alex Light1ebe4fe2017-01-30 14:57:11 -08001562 // When we are walking the stack to redefine classes and creating obsolete methods it is
1563 // possible that we might have updated the method_code_map by making this method obsolete in a
1564 // previous frame. Therefore we should just check that the non-obsolete version of this method
1565 // is the one we expect. We change to the non-obsolete versions in the error message since the
1566 // obsolete version of the method might not be fully initialized yet. This situation can only
1567 // occur when we are in the process of allocating and setting up obsolete methods. Otherwise
Andreas Gampe06c42a52017-07-26 14:17:14 -07001568 // method and it->second should be identical. (See openjdkjvmti/ti_redefine.cc for more
Alex Light1ebe4fe2017-01-30 14:57:11 -08001569 // information.)
Vladimir Marko2196c652017-11-30 16:16:07 +00001570 DCHECK_EQ(found_method->GetNonObsoleteMethod(), method->GetNonObsoleteMethod())
Alex Light1ebe4fe2017-01-30 14:57:11 -08001571 << ArtMethod::PrettyMethod(method->GetNonObsoleteMethod()) << " "
Vladimir Marko2196c652017-11-30 16:16:07 +00001572 << ArtMethod::PrettyMethod(found_method->GetNonObsoleteMethod()) << " "
David Sehr709b0702016-10-13 09:12:37 -07001573 << std::hex << pc;
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +00001574 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001575 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001576}
1577
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001578OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
1579 MutexLock mu(Thread::Current(), lock_);
1580 auto it = osr_code_map_.find(method);
1581 if (it == osr_code_map_.end()) {
1582 return nullptr;
1583 }
1584 return OatQuickMethodHeader::FromCodePointer(it->second);
1585}
1586
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001587ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
1588 ArtMethod* method,
1589 const std::vector<uint32_t>& entries,
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001590 bool retry_allocation)
1591 // No thread safety analysis as we are using TryLock/Unlock explicitly.
1592 NO_THREAD_SAFETY_ANALYSIS {
1593 ProfilingInfo* info = nullptr;
1594 if (!retry_allocation) {
1595 // If we are allocating for the interpreter, just try to lock, to avoid
1596 // lock contention with the JIT.
1597 if (lock_.ExclusiveTryLock(self)) {
1598 info = AddProfilingInfoInternal(self, method, entries);
1599 lock_.ExclusiveUnlock(self);
1600 }
1601 } else {
1602 {
1603 MutexLock mu(self, lock_);
1604 info = AddProfilingInfoInternal(self, method, entries);
1605 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001606
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001607 if (info == nullptr) {
1608 GarbageCollectCache(self);
1609 MutexLock mu(self, lock_);
1610 info = AddProfilingInfoInternal(self, method, entries);
1611 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001612 }
1613 return info;
1614}
1615
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001616ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001617 ArtMethod* method,
1618 const std::vector<uint32_t>& entries) {
1619 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001620 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001621 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001622
1623 // Check whether some other thread has concurrently created it.
Andreas Gampe542451c2016-07-26 09:02:02 -07001624 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001625 if (info != nullptr) {
1626 return info;
1627 }
1628
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001629 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001630 if (data == nullptr) {
1631 return nullptr;
1632 }
1633 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +00001634
1635 // Make sure other threads see the data in the profiling info object before the
1636 // store in the ArtMethod's ProfilingInfo pointer.
Orion Hodson27b96762018-03-13 16:06:57 +00001637 std::atomic_thread_fence(std::memory_order_release);
Nicolas Geoffray07f35642016-01-04 16:06:51 +00001638
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001639 method->SetProfilingInfo(info);
1640 profiling_infos_.push_back(info);
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001641 histogram_profiling_info_memory_use_.AddValue(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001642 return info;
1643}
1644
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001645// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
1646// is already held.
1647void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
1648 if (code_mspace_ == mspace) {
1649 size_t result = code_end_;
1650 code_end_ += increment;
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001651 return reinterpret_cast<void*>(result + code_map_->Begin());
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001652 } else {
1653 DCHECK_EQ(data_mspace_, mspace);
1654 size_t result = data_end_;
1655 data_end_ += increment;
1656 return reinterpret_cast<void*>(result + data_map_->Begin());
1657 }
1658}
1659
Calin Juravle99629622016-04-19 16:33:46 +01001660void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle940eb0c2017-01-30 19:30:44 -08001661 std::vector<ProfileMethodInfo>& methods) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001662 ScopedTrace trace(__FUNCTION__);
Calin Juravle31f2c152015-10-23 17:56:15 +01001663 MutexLock mu(Thread::Current(), lock_);
Calin Juravlea39fd982017-05-18 10:15:52 -07001664 uint16_t jit_compile_threshold = Runtime::Current()->GetJITOptions()->GetCompileThreshold();
Calin Juravle99629622016-04-19 16:33:46 +01001665 for (const ProfilingInfo* info : profiling_infos_) {
1666 ArtMethod* method = info->GetMethod();
1667 const DexFile* dex_file = method->GetDexFile();
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001668 const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
1669 if (!ContainsElement(dex_base_locations, base_location)) {
Calin Juravle940eb0c2017-01-30 19:30:44 -08001670 // Skip dex files which are not profiled.
1671 continue;
Calin Juravle31f2c152015-10-23 17:56:15 +01001672 }
Calin Juravle940eb0c2017-01-30 19:30:44 -08001673 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
Calin Juravlea39fd982017-05-18 10:15:52 -07001674
1675 // If the method didn't reach the compilation threshold don't save the inline caches.
1676 // They might be incomplete and cause unnecessary deoptimizations.
1677 // If the inline cache is empty the compiler will generate a regular invoke virtual/interface.
1678 if (method->GetCounter() < jit_compile_threshold) {
1679 methods.emplace_back(/*ProfileMethodInfo*/
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001680 MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches);
Calin Juravlea39fd982017-05-18 10:15:52 -07001681 continue;
1682 }
1683
Calin Juravle940eb0c2017-01-30 19:30:44 -08001684 for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
Mathieu Chartierdbddc222017-05-24 12:04:13 -07001685 std::vector<TypeReference> profile_classes;
Calin Juravle940eb0c2017-01-30 19:30:44 -08001686 const InlineCache& cache = info->cache_[i];
Calin Juravle13439f02017-02-21 01:17:21 -08001687 ArtMethod* caller = info->GetMethod();
Calin Juravle589e71e2017-03-03 16:05:05 -08001688 bool is_missing_types = false;
Calin Juravle940eb0c2017-01-30 19:30:44 -08001689 for (size_t k = 0; k < InlineCache::kIndividualCacheSize; k++) {
1690 mirror::Class* cls = cache.classes_[k].Read();
1691 if (cls == nullptr) {
1692 break;
1693 }
Calin Juravle4ca70a32017-02-21 16:22:24 -08001694
Calin Juravle13439f02017-02-21 01:17:21 -08001695 // Check if the receiver is in the boot class path or if it's in the
1696 // same class loader as the caller. If not, skip it, as there is not
1697 // much we can do during AOT.
1698 if (!cls->IsBootStrapClassLoaded() &&
1699 caller->GetClassLoader() != cls->GetClassLoader()) {
1700 is_missing_types = true;
1701 continue;
1702 }
1703
Calin Juravle4ca70a32017-02-21 16:22:24 -08001704 const DexFile* class_dex_file = nullptr;
1705 dex::TypeIndex type_index;
1706
1707 if (cls->GetDexCache() == nullptr) {
1708 DCHECK(cls->IsArrayClass()) << cls->PrettyClass();
Calin Juravlee21806f2017-02-22 11:49:43 -08001709 // Make a best effort to find the type index in the method's dex file.
1710 // We could search all open dex files but that might turn expensive
1711 // and probably not worth it.
Calin Juravle4ca70a32017-02-21 16:22:24 -08001712 class_dex_file = dex_file;
1713 type_index = cls->FindTypeIndexInOtherDexFile(*dex_file);
1714 } else {
1715 class_dex_file = &(cls->GetDexFile());
1716 type_index = cls->GetDexTypeIndex();
1717 }
1718 if (!type_index.IsValid()) {
1719 // Could be a proxy class or an array for which we couldn't find the type index.
Calin Juravle589e71e2017-03-03 16:05:05 -08001720 is_missing_types = true;
Calin Juravle4ca70a32017-02-21 16:22:24 -08001721 continue;
1722 }
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001723 if (ContainsElement(dex_base_locations,
1724 DexFileLoader::GetBaseLocation(class_dex_file->GetLocation()))) {
Calin Juravle940eb0c2017-01-30 19:30:44 -08001725 // Only consider classes from the same apk (including multidex).
1726 profile_classes.emplace_back(/*ProfileMethodInfo::ProfileClassReference*/
Calin Juravle4ca70a32017-02-21 16:22:24 -08001727 class_dex_file, type_index);
Calin Juravle589e71e2017-03-03 16:05:05 -08001728 } else {
1729 is_missing_types = true;
Calin Juravle940eb0c2017-01-30 19:30:44 -08001730 }
1731 }
1732 if (!profile_classes.empty()) {
1733 inline_caches.emplace_back(/*ProfileMethodInfo::ProfileInlineCache*/
Calin Juravle589e71e2017-03-03 16:05:05 -08001734 cache.dex_pc_, is_missing_types, profile_classes);
Calin Juravle940eb0c2017-01-30 19:30:44 -08001735 }
1736 }
1737 methods.emplace_back(/*ProfileMethodInfo*/
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001738 MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches);
Calin Juravle31f2c152015-10-23 17:56:15 +01001739 }
1740}
1741
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +01001742bool JitCodeCache::IsOsrCompiled(ArtMethod* method) {
1743 MutexLock mu(Thread::Current(), lock_);
1744 return osr_code_map_.find(method) != osr_code_map_.end();
1745}
1746
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001747bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
1748 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001749 return false;
1750 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001751
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001752 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001753 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
1754 return false;
1755 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001756
Vladimir Marko2196c652017-11-30 16:16:07 +00001757 if (UNLIKELY(method->IsNative())) {
1758 JniStubKey key(method);
1759 auto it = jni_stubs_map_.find(key);
1760 bool new_compilation = false;
1761 if (it == jni_stubs_map_.end()) {
1762 // Create a new entry to mark the stub as being compiled.
1763 it = jni_stubs_map_.Put(key, JniStubData{});
1764 new_compilation = true;
1765 }
1766 JniStubData* data = &it->second;
1767 data->AddMethod(method);
1768 if (data->IsCompiled()) {
1769 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(data->GetCode());
1770 const void* entrypoint = method_header->GetEntryPoint();
1771 // Update also entrypoints of other methods held by the JniStubData.
1772 // We could simply update the entrypoint of `method` but if the last JIT GC has
1773 // changed these entrypoints to GenericJNI in preparation for a full GC, we may
1774 // as well change them back as this stub shall not be collected anyway and this
1775 // can avoid a few expensive GenericJNI calls.
1776 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1777 for (ArtMethod* m : data->GetMethods()) {
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +00001778 // Call the dedicated method instead of the more generic UpdateMethodsCode, because
1779 // `m` might be in the process of being deleted.
1780 instrumentation->UpdateNativeMethodsCodeToJitCode(m, entrypoint);
Vladimir Marko2196c652017-11-30 16:16:07 +00001781 }
1782 if (collection_in_progress_) {
1783 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(data->GetCode()));
1784 }
1785 }
1786 return new_compilation;
1787 } else {
1788 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1789 if (info == nullptr) {
1790 VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled";
1791 // Because the counter is not atomic, there are some rare cases where we may not hit the
1792 // threshold for creating the ProfilingInfo. Reset the counter now to "correct" this.
1793 ClearMethodCounter(method, /*was_warm*/ false);
1794 return false;
1795 }
Nicolas Geoffray056d7752017-11-30 09:12:13 +00001796
Vladimir Marko2196c652017-11-30 16:16:07 +00001797 if (info->IsMethodBeingCompiled(osr)) {
1798 return false;
1799 }
Nicolas Geoffray056d7752017-11-30 09:12:13 +00001800
Vladimir Marko2196c652017-11-30 16:16:07 +00001801 info->SetIsMethodBeingCompiled(true, osr);
1802 return true;
1803 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001804}
1805
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001806ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001807 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001808 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001809 if (info != nullptr) {
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +00001810 if (!info->IncrementInlineUse()) {
1811 // Overflow of inlining uses, just bail.
1812 return nullptr;
1813 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001814 }
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001815 return info;
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001816}
1817
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001818void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001819 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001820 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001821 DCHECK(info != nullptr);
1822 info->DecrementInlineUse();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001823}
1824
Vladimir Marko2196c652017-11-30 16:16:07 +00001825void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self, bool osr) {
1826 DCHECK_EQ(Thread::Current(), self);
1827 MutexLock mu(self, lock_);
1828 if (UNLIKELY(method->IsNative())) {
1829 auto it = jni_stubs_map_.find(JniStubKey(method));
1830 DCHECK(it != jni_stubs_map_.end());
1831 JniStubData* data = &it->second;
1832 DCHECK(ContainsElement(data->GetMethods(), method));
1833 if (UNLIKELY(!data->IsCompiled())) {
1834 // Failed to compile; the JNI compiler never fails, but the cache may be full.
1835 jni_stubs_map_.erase(it); // Remove the entry added in NotifyCompilationOf().
1836 } // else CommitCodeInternal() updated entrypoints of all methods in the JniStubData.
1837 } else {
1838 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1839 DCHECK(info->IsMethodBeingCompiled(osr));
1840 info->SetIsMethodBeingCompiled(false, osr);
1841 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001842}
1843
Nicolas Geoffraya25dce92016-01-12 16:41:10 +00001844size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
1845 MutexLock mu(Thread::Current(), lock_);
1846 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
1847}
1848
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001849void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
1850 const OatQuickMethodHeader* header) {
Vladimir Marko2196c652017-11-30 16:16:07 +00001851 DCHECK(!method->IsNative());
Andreas Gampe542451c2016-07-26 09:02:02 -07001852 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Alex Light2d441b12018-06-08 15:33:21 -07001853 const void* method_entrypoint = method->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray35122442016-03-02 12:05:30 +00001854 if ((profiling_info != nullptr) &&
1855 (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
Alex Light2d441b12018-06-08 15:33:21 -07001856 // When instrumentation is set, the actual entrypoint is the one in the profiling info.
1857 method_entrypoint = profiling_info->GetSavedEntryPoint();
Nicolas Geoffray35122442016-03-02 12:05:30 +00001858 // Prevent future uses of the compiled code.
1859 profiling_info->SetSavedEntryPoint(nullptr);
1860 }
1861
Alex Light2d441b12018-06-08 15:33:21 -07001862 // Clear the method counter if we are running jitted code since we might want to jit this again in
1863 // the future.
1864 if (method_entrypoint == header->GetEntryPoint()) {
Jeff Hao00286db2017-05-30 16:53:07 -07001865 // The entrypoint is the one to invalidate, so we just update it to the interpreter entry point
Mathieu Chartierf044c222017-05-31 15:27:54 -07001866 // and clear the counter to get the method Jitted again.
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001867 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1868 method, GetQuickToInterpreterBridge());
Mathieu Chartierf044c222017-05-31 15:27:54 -07001869 ClearMethodCounter(method, /*was_warm*/ profiling_info != nullptr);
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001870 } else {
1871 MutexLock mu(Thread::Current(), lock_);
1872 auto it = osr_code_map_.find(method);
1873 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
1874 // Remove the OSR method, to avoid using it again.
1875 osr_code_map_.erase(it);
1876 }
1877 }
1878}
1879
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001880uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
1881 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
1882 uint8_t* result = reinterpret_cast<uint8_t*>(
1883 mspace_memalign(code_mspace_, alignment, code_size));
1884 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
1885 // Ensure the header ends up at expected instruction alignment.
1886 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
1887 used_memory_for_code_ += mspace_usable_size(result);
1888 return result;
1889}
1890
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001891void JitCodeCache::FreeCode(uint8_t* code) {
1892 used_memory_for_code_ -= mspace_usable_size(code);
1893 mspace_free(code_mspace_, code);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001894}
1895
1896uint8_t* JitCodeCache::AllocateData(size_t data_size) {
1897 void* result = mspace_malloc(data_mspace_, data_size);
1898 used_memory_for_data_ += mspace_usable_size(result);
1899 return reinterpret_cast<uint8_t*>(result);
1900}
1901
1902void JitCodeCache::FreeData(uint8_t* data) {
1903 used_memory_for_data_ -= mspace_usable_size(data);
1904 mspace_free(data_mspace_, data);
1905}
1906
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001907void JitCodeCache::Dump(std::ostream& os) {
1908 MutexLock mu(Thread::Current(), lock_);
David Srbeckyfb3de3d2018-01-29 16:11:49 +00001909 MutexLock mu2(Thread::Current(), *Locks::native_debug_interface_lock_);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001910 os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n"
1911 << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n"
David Srbecky440a9b32018-02-15 17:47:29 +00001912 << "Current JIT mini-debug-info size: " << PrettySize(GetJitNativeDebugInfoMemUsage()) << "\n"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001913 << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n"
Vladimir Marko2196c652017-11-30 16:16:07 +00001914 << "Current number of JIT JNI stub entries: " << jni_stubs_map_.size() << "\n"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001915 << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n"
1916 << "Total number of JIT compilations: " << number_of_compilations_ << "\n"
1917 << "Total number of JIT compilations for on stack replacement: "
1918 << number_of_osr_compilations_ << "\n"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001919 << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl;
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001920 histogram_stack_map_memory_use_.PrintMemoryUse(os);
1921 histogram_code_memory_use_.PrintMemoryUse(os);
1922 histogram_profiling_info_memory_use_.PrintMemoryUse(os);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001923}
1924
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001925} // namespace jit
1926} // namespace art