blob: 33d228f2553b96fd6275f91fdcfc96c1d53e92ef [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.
Orion Hodson563ada22018-09-04 11:28:31 +010026#include "base/membarrier.h"
David Sehr79e26072018-04-06 17:58:50 -070027#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/quasi_atomic.h"
Calin Juravle66f55232015-12-08 15:09:10 +000029#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080030#include "base/systrace.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010031#include "base/time_utils.h"
Orion Hodsonf2331362018-07-11 15:14:10 +010032#include "base/utils.h"
Mingyao Yang063fc772016-08-02 11:02:54 -070033#include "cha.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000034#include "debugger_interface.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file_loader.h"
Andreas Gampef0f3c592018-06-26 13:28:00 -070036#include "dex/method_reference.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010037#include "entrypoints/runtime_asm_entrypoints.h"
38#include "gc/accounting/bitmap-inl.h"
Nicolas Geoffraycf48fa02016-07-30 22:49:11 +010039#include "gc/scoped_gc_critical_section.h"
Vladimir Markob0b68cf2017-11-14 18:11:50 +000040#include "handle.h"
Andreas Gampef0f3c592018-06-26 13:28:00 -070041#include "instrumentation.h"
Andreas Gampeb2d18fa2017-06-06 20:46:10 -070042#include "intern_table.h"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000043#include "jit/jit.h"
Nicolas Geoffray26705e22015-10-28 12:50:11 +000044#include "jit/profiling_info.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010045#include "linear_alloc.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046#include "oat_file-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070047#include "oat_quick_method_header.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070048#include "object_callbacks.h"
David Sehr82d046e2018-04-23 08:14:19 -070049#include "profile/profile_compilation_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070050#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070051#include "stack.h"
Vladimir Markob0b68cf2017-11-14 18:11:50 +000052#include "thread-current-inl.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010053#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080054
55namespace art {
56namespace jit {
57
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010058static constexpr int kProtCode = PROT_READ | PROT_EXEC;
Orion Hodsonf2331362018-07-11 15:14:10 +010059static constexpr int kProtData = PROT_READ | PROT_WRITE;
60static constexpr int kProtProfile = PROT_READ;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010061
Nicolas Geoffray933330a2016-03-16 14:20:06 +000062static constexpr size_t kCodeSizeLogThreshold = 50 * KB;
63static constexpr size_t kStackMapSizeLogThreshold = 50 * KB;
64
Vladimir Marko2196c652017-11-30 16:16:07 +000065class JitCodeCache::JniStubKey {
66 public:
67 explicit JniStubKey(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
68 : shorty_(method->GetShorty()),
69 is_static_(method->IsStatic()),
70 is_fast_native_(method->IsFastNative()),
71 is_critical_native_(method->IsCriticalNative()),
72 is_synchronized_(method->IsSynchronized()) {
73 DCHECK(!(is_fast_native_ && is_critical_native_));
74 }
75
76 bool operator<(const JniStubKey& rhs) const {
77 if (is_static_ != rhs.is_static_) {
78 return rhs.is_static_;
79 }
80 if (is_synchronized_ != rhs.is_synchronized_) {
81 return rhs.is_synchronized_;
82 }
83 if (is_fast_native_ != rhs.is_fast_native_) {
84 return rhs.is_fast_native_;
85 }
86 if (is_critical_native_ != rhs.is_critical_native_) {
87 return rhs.is_critical_native_;
88 }
89 return strcmp(shorty_, rhs.shorty_) < 0;
90 }
91
92 // Update the shorty to point to another method's shorty. Call this function when removing
93 // the method that references the old shorty from JniCodeData and not removing the entire
94 // JniCodeData; the old shorty may become a dangling pointer when that method is unloaded.
95 void UpdateShorty(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
96 const char* shorty = method->GetShorty();
97 DCHECK_STREQ(shorty_, shorty);
98 shorty_ = shorty;
99 }
100
101 private:
102 // The shorty points to a DexFile data and may need to change
103 // to point to the same shorty in a different DexFile.
104 mutable const char* shorty_;
105
106 const bool is_static_;
107 const bool is_fast_native_;
108 const bool is_critical_native_;
109 const bool is_synchronized_;
110};
111
112class JitCodeCache::JniStubData {
113 public:
114 JniStubData() : code_(nullptr), methods_() {}
115
116 void SetCode(const void* code) {
117 DCHECK(code != nullptr);
118 code_ = code;
119 }
120
121 const void* GetCode() const {
122 return code_;
123 }
124
125 bool IsCompiled() const {
126 return GetCode() != nullptr;
127 }
128
129 void AddMethod(ArtMethod* method) {
130 if (!ContainsElement(methods_, method)) {
131 methods_.push_back(method);
132 }
133 }
134
135 const std::vector<ArtMethod*>& GetMethods() const {
136 return methods_;
137 }
138
139 void RemoveMethodsIn(const LinearAlloc& alloc) {
140 auto kept_end = std::remove_if(
141 methods_.begin(),
142 methods_.end(),
143 [&alloc](ArtMethod* method) { return alloc.ContainsUnsafe(method); });
144 methods_.erase(kept_end, methods_.end());
145 }
146
147 bool RemoveMethod(ArtMethod* method) {
148 auto it = std::find(methods_.begin(), methods_.end(), method);
149 if (it != methods_.end()) {
150 methods_.erase(it);
151 return true;
152 } else {
153 return false;
154 }
155 }
156
157 void MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
158 std::replace(methods_.begin(), methods_.end(), old_method, new_method);
159 }
160
161 private:
162 const void* code_;
163 std::vector<ArtMethod*> methods_;
164};
165
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000166JitCodeCache* JitCodeCache::Create(size_t initial_capacity,
167 size_t max_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000168 bool generate_debug_info,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700169 bool used_only_for_profile_data,
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000170 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800171 ScopedTrace trace(__PRETTY_FUNCTION__);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100172 CHECK_GE(max_capacity, initial_capacity);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000173
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000174 // With 'perf', we want a 1-1 mapping between an address and a method.
Alex Light2d441b12018-06-08 15:33:21 -0700175 // We aren't able to keep method pointers live during the instrumentation method entry trampoline
176 // so we will just disable jit-gc if we are doing that.
177 bool garbage_collect_code = !generate_debug_info &&
178 !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled();
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000179
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000180 // We need to have 32 bit offsets from method headers in code cache which point to things
181 // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
182 // Ensure we're below 1 GB to be safe.
183 if (max_capacity > 1 * GB) {
184 std::ostringstream oss;
185 oss << "Maxium code cache capacity is limited to 1 GB, "
186 << PrettySize(max_capacity) << " is too big";
187 *error_msg = oss.str();
188 return nullptr;
189 }
190
Orion Hodson563ada22018-09-04 11:28:31 +0100191 // Register for membarrier expedited sync core if JIT will be generating code.
192 if (!used_only_for_profile_data) {
193 art::membarrier(art::MembarrierCommand::kRegisterPrivateExpeditedSyncCore);
194 }
195
Calin Juravle016fcbe22018-05-03 19:47:35 -0700196 // Decide how we should map the code and data sections.
197 // If we use the code cache just for profiling we do not need to map the code section as
198 // executable.
199 // NOTE 1: this is yet another workaround to bypass strict SElinux policies in order to be able
200 // to profile system server.
201 // NOTE 2: We could just not create the code section at all but we will need to
202 // special case too many cases.
Orion Hodsonf2331362018-07-11 15:14:10 +0100203 int memmap_flags_prot_code = used_only_for_profile_data ? kProtProfile : kProtCode;
Calin Juravle016fcbe22018-05-03 19:47:35 -0700204
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205 std::string error_str;
206 // Map name specific for android_os_Debug.cpp accounting.
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000207 // Map in low 4gb to simplify accessing root tables for x86_64.
208 // We could do PC-relative addressing to avoid this problem, but that
209 // would require reserving code and data area before submitting, which
210 // means more windows for the code memory to be RWX.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100211 MemMap data_map = MemMap::MapAnonymous(
212 "data-code-cache",
213 /* addr */ nullptr,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000214 max_capacity,
Andreas Gampee4deaf32017-06-09 15:27:15 -0700215 kProtData,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000216 /* low_4gb */ true,
217 /* reuse */ false,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100218 /* reservation */ nullptr,
Joel Fernandes (Google)92597a82018-08-17 16:19:19 -0700219 &error_str);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100220 if (!data_map.IsValid()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800221 std::ostringstream oss;
Andreas Gampee4deaf32017-06-09 15:27:15 -0700222 oss << "Failed to create read write cache: " << error_str << " size=" << max_capacity;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800223 *error_msg = oss.str();
224 return nullptr;
225 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100226
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100227 // Align both capacities to page size, as that's the unit mspaces use.
228 initial_capacity = RoundDown(initial_capacity, 2 * kPageSize);
229 max_capacity = RoundDown(max_capacity, 2 * kPageSize);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100230
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100231 // Data cache is 1 / 2 of the map.
232 // TODO: Make this variable?
233 size_t data_size = max_capacity / 2;
234 size_t code_size = max_capacity - data_size;
235 DCHECK_EQ(code_size + data_size, max_capacity);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100236 uint8_t* divider = data_map.Begin() + data_size;
David Sehrd1dbb742017-07-17 11:20:38 -0700237
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100238 MemMap code_map = data_map.RemapAtEnd(
Joel Fernandes (Google)92597a82018-08-17 16:19:19 -0700239 divider, "jit-code-cache", memmap_flags_prot_code | PROT_WRITE, &error_str);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100240 if (!code_map.IsValid()) {
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100241 std::ostringstream oss;
242 oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
243 *error_msg = oss.str();
David Sehrd1dbb742017-07-17 11:20:38 -0700244 return nullptr;
245 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100246 DCHECK_EQ(code_map.Begin(), divider);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000247 data_size = initial_capacity / 2;
248 code_size = initial_capacity - data_size;
249 DCHECK_EQ(code_size + data_size, initial_capacity);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100250 return new JitCodeCache(
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100251 std::move(code_map),
252 std::move(data_map),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700253 code_size,
254 data_size,
255 max_capacity,
256 garbage_collect_code,
257 memmap_flags_prot_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800258}
259
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100260JitCodeCache::JitCodeCache(MemMap&& code_map,
261 MemMap&& data_map,
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000262 size_t initial_code_capacity,
263 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000264 size_t max_capacity,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700265 bool garbage_collect_code,
266 int memmap_flags_prot_code)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100267 : lock_("Jit code cache", kJitCodeCacheLock),
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000268 lock_cond_("Jit code cache condition variable", lock_),
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100269 collection_in_progress_(false),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100270 code_map_(std::move(code_map)),
271 data_map_(std::move(data_map)),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000272 max_capacity_(max_capacity),
273 current_capacity_(initial_code_capacity + initial_data_capacity),
274 code_end_(initial_code_capacity),
275 data_end_(initial_data_capacity),
Nicolas Geoffray35122442016-03-02 12:05:30 +0000276 last_collection_increased_code_cache_(false),
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000277 garbage_collect_code_(garbage_collect_code),
Nicolas Geoffrayb0d22082016-02-24 17:18:25 +0000278 used_memory_for_data_(0),
279 used_memory_for_code_(0),
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000280 number_of_compilations_(0),
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000281 number_of_osr_compilations_(0),
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000282 number_of_collections_(0),
283 histogram_stack_map_memory_use_("Memory used for stack maps", 16),
284 histogram_code_memory_use_("Memory used for compiled code", 16),
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000285 histogram_profiling_info_memory_use_("Memory used for profiling info", 16),
286 is_weak_access_enabled_(true),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700287 inline_cache_cond_("Jit inline cache condition variable", lock_),
288 memmap_flags_prot_code_(memmap_flags_prot_code) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100289
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000290 DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100291 code_mspace_ = create_mspace_with_base(code_map_.Begin(), code_end_, false /*locked*/);
292 data_mspace_ = create_mspace_with_base(data_map_.Begin(), data_end_, false /*locked*/);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100293
294 if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
295 PLOG(FATAL) << "create_mspace_with_base failed";
296 }
297
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000298 SetFootprintLimit(current_capacity_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100299
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700300 CheckedCall(mprotect,
301 "mprotect jit code cache",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100302 code_map_.Begin(),
303 code_map_.Size(),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700304 memmap_flags_prot_code_);
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700305 CheckedCall(mprotect,
306 "mprotect jit data cache",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100307 data_map_.Begin(),
308 data_map_.Size(),
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700309 kProtData);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100310
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000311 VLOG(jit) << "Created jit code cache: initial data size="
312 << PrettySize(initial_data_capacity)
313 << ", initial code size="
314 << PrettySize(initial_code_capacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800315}
316
Vladimir Markob0b68cf2017-11-14 18:11:50 +0000317JitCodeCache::~JitCodeCache() {}
318
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100319bool JitCodeCache::ContainsPc(const void* ptr) const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100320 return code_map_.Begin() <= ptr && ptr < code_map_.End();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800321}
322
Alex Light2d441b12018-06-08 15:33:21 -0700323bool JitCodeCache::WillExecuteJitCode(ArtMethod* method) {
324 ScopedObjectAccess soa(art::Thread::Current());
325 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
326 if (ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
327 return true;
328 } else if (method->GetEntryPointFromQuickCompiledCode() == GetQuickInstrumentationEntryPoint()) {
329 return FindCompiledCodeForInstrumentation(method) != nullptr;
330 }
331 return false;
332}
333
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000334bool JitCodeCache::ContainsMethod(ArtMethod* method) {
335 MutexLock mu(Thread::Current(), lock_);
Vladimir Marko2196c652017-11-30 16:16:07 +0000336 if (UNLIKELY(method->IsNative())) {
337 auto it = jni_stubs_map_.find(JniStubKey(method));
338 if (it != jni_stubs_map_.end() &&
339 it->second.IsCompiled() &&
340 ContainsElement(it->second.GetMethods(), method)) {
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000341 return true;
342 }
Vladimir Marko2196c652017-11-30 16:16:07 +0000343 } else {
344 for (const auto& it : method_code_map_) {
345 if (it.second == method) {
346 return true;
347 }
348 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000349 }
350 return false;
351}
352
Vladimir Marko2196c652017-11-30 16:16:07 +0000353const void* JitCodeCache::GetJniStubCode(ArtMethod* method) {
354 DCHECK(method->IsNative());
355 MutexLock mu(Thread::Current(), lock_);
356 auto it = jni_stubs_map_.find(JniStubKey(method));
357 if (it != jni_stubs_map_.end()) {
358 JniStubData& data = it->second;
359 if (data.IsCompiled() && ContainsElement(data.GetMethods(), method)) {
360 return data.GetCode();
361 }
362 }
363 return nullptr;
364}
365
Alex Light2d441b12018-06-08 15:33:21 -0700366const void* JitCodeCache::FindCompiledCodeForInstrumentation(ArtMethod* method) {
Alex Light839f53a2018-07-10 15:46:14 -0700367 // If jit-gc is still on we use the SavedEntryPoint field for doing that and so cannot use it to
368 // find the instrumentation entrypoint.
369 if (LIKELY(GetGarbageCollectCode())) {
Alex Light2d441b12018-06-08 15:33:21 -0700370 return nullptr;
371 }
372 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
373 if (info == nullptr) {
374 return nullptr;
375 }
376 // When GC is disabled for trampoline tracing we will use SavedEntrypoint to hold the actual
377 // jit-compiled version of the method. If jit-gc is disabled for other reasons this will just be
378 // nullptr.
379 return info->GetSavedEntryPoint();
380}
381
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800382class ScopedCodeCacheWrite : ScopedTrace {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100383 public:
Calin Juravle016fcbe22018-05-03 19:47:35 -0700384 explicit ScopedCodeCacheWrite(const JitCodeCache* const code_cache)
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100385 : ScopedTrace("ScopedCodeCacheWrite"),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700386 code_cache_(code_cache) {
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800387 ScopedTrace trace("mprotect all");
Calin Juravle016fcbe22018-05-03 19:47:35 -0700388 CheckedCall(
389 mprotect,
390 "make code writable",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100391 code_cache_->code_map_.Begin(),
392 code_cache_->code_map_.Size(),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700393 code_cache_->memmap_flags_prot_code_ | PROT_WRITE);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800394 }
Calin Juravle016fcbe22018-05-03 19:47:35 -0700395
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100396 ~ScopedCodeCacheWrite() {
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800397 ScopedTrace trace("mprotect code");
Calin Juravle016fcbe22018-05-03 19:47:35 -0700398 CheckedCall(
399 mprotect,
400 "make code protected",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100401 code_cache_->code_map_.Begin(),
402 code_cache_->code_map_.Size(),
Calin Juravle016fcbe22018-05-03 19:47:35 -0700403 code_cache_->memmap_flags_prot_code_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100404 }
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700405
David Sehrd1dbb742017-07-17 11:20:38 -0700406 private:
Calin Juravle016fcbe22018-05-03 19:47:35 -0700407 const JitCodeCache* const code_cache_;
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100408
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100409 DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
410};
411
412uint8_t* JitCodeCache::CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100413 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000414 uint8_t* stack_map,
415 uint8_t* roots_data,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100416 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000417 size_t code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100418 size_t data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000419 bool osr,
Vladimir Markoac3ac682018-09-20 11:01:43 +0100420 const std::vector<Handle<mirror::Object>>& roots,
Mingyao Yang063fc772016-08-02 11:02:54 -0700421 bool has_should_deoptimize_flag,
422 const ArenaSet<ArtMethod*>& cha_single_implementation_list) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100423 uint8_t* result = CommitCodeInternal(self,
424 method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000425 stack_map,
426 roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100427 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000428 code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100429 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000430 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -0700431 roots,
432 has_should_deoptimize_flag,
433 cha_single_implementation_list);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100434 if (result == nullptr) {
435 // Retry.
436 GarbageCollectCache(self);
437 result = CommitCodeInternal(self,
438 method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000439 stack_map,
440 roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100441 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000442 code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100443 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000444 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -0700445 roots,
446 has_should_deoptimize_flag,
447 cha_single_implementation_list);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100448 }
449 return result;
450}
451
452bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
453 bool in_collection = false;
454 while (collection_in_progress_) {
455 in_collection = true;
456 lock_cond_.Wait(self);
457 }
458 return in_collection;
459}
460
461static uintptr_t FromCodeToAllocation(const void* code) {
462 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
463 return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
464}
465
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000466static uint32_t ComputeRootTableSize(uint32_t number_of_roots) {
467 return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>);
468}
469
470static uint32_t GetNumberOfRoots(const uint8_t* stack_map) {
471 // The length of the table is stored just before the stack map (and therefore at the end of
472 // the table itself), in order to be able to fetch it from a `stack_map` pointer.
473 return reinterpret_cast<const uint32_t*>(stack_map)[-1];
474}
475
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800476static void FillRootTableLength(uint8_t* roots_data, uint32_t length) {
477 // Store the length of the table at the end. This will allow fetching it from a `stack_map`
478 // pointer.
479 reinterpret_cast<uint32_t*>(roots_data)[length] = length;
480}
481
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +0000482static const uint8_t* FromStackMapToRoots(const uint8_t* stack_map_data) {
483 return stack_map_data - ComputeRootTableSize(GetNumberOfRoots(stack_map_data));
484}
485
Vladimir Markoac3ac682018-09-20 11:01:43 +0100486static void DCheckRootsAreValid(const std::vector<Handle<mirror::Object>>& roots)
Alex Light3e36a9c2018-06-19 09:45:05 -0700487 REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_) {
488 if (!kIsDebugBuild) {
489 return;
490 }
Alex Light3e36a9c2018-06-19 09:45:05 -0700491 // Put all roots in `roots_data`.
Vladimir Markoac3ac682018-09-20 11:01:43 +0100492 for (Handle<mirror::Object> object : roots) {
Alex Light3e36a9c2018-06-19 09:45:05 -0700493 // Ensure the string is strongly interned. b/32995596
494 if (object->IsString()) {
Vladimir Markoac3ac682018-09-20 11:01:43 +0100495 ObjPtr<mirror::String> str = object->AsString();
Alex Light3e36a9c2018-06-19 09:45:05 -0700496 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
497 CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr);
498 }
499 }
500}
501
502void JitCodeCache::FillRootTable(uint8_t* roots_data,
Vladimir Markoac3ac682018-09-20 11:01:43 +0100503 const std::vector<Handle<mirror::Object>>& roots) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000504 GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
Vladimir Markoac3ac682018-09-20 11:01:43 +0100505 const uint32_t length = roots.size();
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000506 // Put all roots in `roots_data`.
507 for (uint32_t i = 0; i < length; ++i) {
Vladimir Markoac3ac682018-09-20 11:01:43 +0100508 ObjPtr<mirror::Object> object = roots[i].Get();
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000509 gc_roots[i] = GcRoot<mirror::Object>(object);
510 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000511}
512
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100513static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000514 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
515 uint8_t* data = method_header->GetOptimizedCodeInfoPtr();
516 uint32_t roots = GetNumberOfRoots(data);
517 if (number_of_roots != nullptr) {
518 *number_of_roots = roots;
519 }
520 return data - ComputeRootTableSize(roots);
521}
522
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100523// Use a sentinel for marking entries in the JIT table that have been cleared.
524// This helps diagnosing in case the compiled code tries to wrongly access such
525// entries.
Andreas Gampe5629d2d2017-05-15 16:28:13 -0700526static mirror::Class* const weak_sentinel =
527 reinterpret_cast<mirror::Class*>(Context::kBadGprBase + 0xff);
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100528
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000529// Helper for the GC to process a weak class in a JIT root table.
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100530static inline void ProcessWeakClass(GcRoot<mirror::Class>* root_ptr,
531 IsMarkedVisitor* visitor,
532 mirror::Class* update)
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000533 REQUIRES_SHARED(Locks::mutator_lock_) {
534 // This does not need a read barrier because this is called by GC.
535 mirror::Class* cls = root_ptr->Read<kWithoutReadBarrier>();
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100536 if (cls != nullptr && cls != weak_sentinel) {
Mathieu Chartierd7a7f2f2018-09-07 11:57:18 -0700537 DCHECK((cls->IsClass<kDefaultVerifyFlags>()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000538 // Look at the classloader of the class to know if it has been unloaded.
539 // This does not need a read barrier because this is called by GC.
540 mirror::Object* class_loader =
541 cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>();
542 if (class_loader == nullptr || visitor->IsMarked(class_loader) != nullptr) {
543 // The class loader is live, update the entry if the class has moved.
544 mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls));
545 // Note that new_object can be null for CMS and newly allocated objects.
546 if (new_cls != nullptr && new_cls != cls) {
547 *root_ptr = GcRoot<mirror::Class>(new_cls);
548 }
549 } else {
550 // The class loader is not live, clear the entry.
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100551 *root_ptr = GcRoot<mirror::Class>(update);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000552 }
553 }
554}
555
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000556void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) {
557 MutexLock mu(Thread::Current(), lock_);
558 for (const auto& entry : method_code_map_) {
559 uint32_t number_of_roots = 0;
560 uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots);
561 GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
562 for (uint32_t i = 0; i < number_of_roots; ++i) {
563 // This does not need a read barrier because this is called by GC.
564 mirror::Object* object = roots[i].Read<kWithoutReadBarrier>();
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100565 if (object == nullptr || object == weak_sentinel) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000566 // entry got deleted in a previous sweep.
567 } else if (object->IsString<kDefaultVerifyFlags, kWithoutReadBarrier>()) {
568 mirror::Object* new_object = visitor->IsMarked(object);
569 // We know the string is marked because it's a strongly-interned string that
570 // is always alive. The IsMarked implementation of the CMS collector returns
571 // null for newly allocated objects, but we know those haven't moved. Therefore,
572 // only update the entry if we get a different non-null string.
573 // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method
574 // out of the weak access/creation pause. b/32167580
575 if (new_object != nullptr && new_object != object) {
576 DCHECK(new_object->IsString());
577 roots[i] = GcRoot<mirror::Object>(new_object);
578 }
579 } else {
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100580 ProcessWeakClass(
581 reinterpret_cast<GcRoot<mirror::Class>*>(&roots[i]), visitor, weak_sentinel);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000582 }
583 }
584 }
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000585 // Walk over inline caches to clear entries containing unloaded classes.
586 for (ProfilingInfo* info : profiling_infos_) {
587 for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
588 InlineCache* cache = &info->cache_[i];
589 for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
Nicolas Geoffray6ca115b2017-05-10 15:09:35 +0100590 ProcessWeakClass(&cache->classes_[j], visitor, nullptr);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000591 }
592 }
593 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000594}
595
Orion Hodson607624f2018-05-11 10:10:46 +0100596void JitCodeCache::FreeCodeAndData(const void* code_ptr) {
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100597 uintptr_t allocation = FromCodeToAllocation(code_ptr);
David Srbecky5cc349f2015-12-18 15:04:48 +0000598 // Notify native debugger that we are about to remove the code.
599 // It does nothing if we are not using native debugger.
David Srbeckyfb3de3d2018-01-29 16:11:49 +0000600 MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_);
David Srbecky440a9b32018-02-15 17:47:29 +0000601 RemoveNativeDebugInfoForJit(code_ptr);
Vladimir Marko2196c652017-11-30 16:16:07 +0000602 if (OatQuickMethodHeader::FromCodePointer(code_ptr)->IsOptimized()) {
603 FreeData(GetRootTable(code_ptr));
604 } // else this is a JNI stub without any data.
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100605 FreeCode(reinterpret_cast<uint8_t*>(allocation));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100606}
607
Mingyao Yang063fc772016-08-02 11:02:54 -0700608void JitCodeCache::FreeAllMethodHeaders(
609 const std::unordered_set<OatQuickMethodHeader*>& method_headers) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700610 // We need to remove entries in method_headers from CHA dependencies
611 // first since once we do FreeCode() below, the memory can be reused
612 // so it's possible for the same method_header to start representing
613 // different compile code.
614 MutexLock mu(Thread::Current(), lock_);
Alex Light33b7b5d2018-08-07 19:13:51 +0000615 {
616 MutexLock mu2(Thread::Current(), *Locks::cha_lock_);
617 Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()
618 ->RemoveDependentsWithMethodHeaders(method_headers);
619 }
620
Calin Juravle016fcbe22018-05-03 19:47:35 -0700621 ScopedCodeCacheWrite scc(this);
Mingyao Yang063fc772016-08-02 11:02:54 -0700622 for (const OatQuickMethodHeader* method_header : method_headers) {
Orion Hodson607624f2018-05-11 10:10:46 +0100623 FreeCodeAndData(method_header->GetCode());
Mingyao Yang063fc772016-08-02 11:02:54 -0700624 }
625}
626
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100627void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800628 ScopedTrace trace(__PRETTY_FUNCTION__);
Mingyao Yang063fc772016-08-02 11:02:54 -0700629 // We use a set to first collect all method_headers whose code need to be
630 // removed. We need to free the underlying code after we remove CHA dependencies
631 // for entries in this set. And it's more efficient to iterate through
632 // the CHA dependency map just once with an unordered_set.
633 std::unordered_set<OatQuickMethodHeader*> method_headers;
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000634 {
Mingyao Yang063fc772016-08-02 11:02:54 -0700635 MutexLock mu(self, lock_);
636 // We do not check if a code cache GC is in progress, as this method comes
637 // with the classlinker_classes_lock_ held, and suspending ourselves could
638 // lead to a deadlock.
639 {
Calin Juravle016fcbe22018-05-03 19:47:35 -0700640 ScopedCodeCacheWrite scc(this);
Vladimir Marko2196c652017-11-30 16:16:07 +0000641 for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) {
642 it->second.RemoveMethodsIn(alloc);
643 if (it->second.GetMethods().empty()) {
644 method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->second.GetCode()));
645 it = jni_stubs_map_.erase(it);
646 } else {
647 it->first.UpdateShorty(it->second.GetMethods().front());
648 ++it;
649 }
650 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700651 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
652 if (alloc.ContainsUnsafe(it->second)) {
653 method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first));
654 it = method_code_map_.erase(it);
655 } else {
656 ++it;
657 }
658 }
659 }
660 for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
661 if (alloc.ContainsUnsafe(it->first)) {
662 // Note that the code has already been pushed to method_headers in the loop
663 // above and is going to be removed in FreeCode() below.
664 it = osr_code_map_.erase(it);
665 } else {
666 ++it;
667 }
668 }
669 for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
670 ProfilingInfo* info = *it;
671 if (alloc.ContainsUnsafe(info->GetMethod())) {
672 info->GetMethod()->SetProfilingInfo(nullptr);
673 FreeData(reinterpret_cast<uint8_t*>(info));
674 it = profiling_infos_.erase(it);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000675 } else {
676 ++it;
677 }
678 }
679 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700680 FreeAllMethodHeaders(method_headers);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100681}
682
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000683bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const {
684 return kUseReadBarrier
685 ? self->GetWeakRefAccessEnabled()
Orion Hodson88591fe2018-03-06 13:35:43 +0000686 : is_weak_access_enabled_.load(std::memory_order_seq_cst);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000687}
688
689void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) {
690 if (IsWeakAccessEnabled(self)) {
691 return;
692 }
693 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000694 MutexLock mu(self, lock_);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000695 while (!IsWeakAccessEnabled(self)) {
696 inline_cache_cond_.Wait(self);
697 }
698}
699
700void JitCodeCache::BroadcastForInlineCacheAccess() {
701 Thread* self = Thread::Current();
702 MutexLock mu(self, lock_);
703 inline_cache_cond_.Broadcast(self);
704}
705
706void JitCodeCache::AllowInlineCacheAccess() {
707 DCHECK(!kUseReadBarrier);
Orion Hodson88591fe2018-03-06 13:35:43 +0000708 is_weak_access_enabled_.store(true, std::memory_order_seq_cst);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000709 BroadcastForInlineCacheAccess();
710}
711
712void JitCodeCache::DisallowInlineCacheAccess() {
713 DCHECK(!kUseReadBarrier);
Orion Hodson88591fe2018-03-06 13:35:43 +0000714 is_weak_access_enabled_.store(false, std::memory_order_seq_cst);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000715}
716
717void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic,
718 Handle<mirror::ObjectArray<mirror::Class>> array) {
719 WaitUntilInlineCacheAccessible(Thread::Current());
720 // Note that we don't need to lock `lock_` here, the compiler calling
721 // this method has already ensured the inline cache will not be deleted.
722 for (size_t in_cache = 0, in_array = 0;
723 in_cache < InlineCache::kIndividualCacheSize;
724 ++in_cache) {
725 mirror::Class* object = ic.classes_[in_cache].Read();
726 if (object != nullptr) {
727 array->Set(in_array++, object);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000728 }
729 }
730}
731
Mathieu Chartierf044c222017-05-31 15:27:54 -0700732static void ClearMethodCounter(ArtMethod* method, bool was_warm) {
733 if (was_warm) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100734 method->SetPreviouslyWarm();
Mathieu Chartierf044c222017-05-31 15:27:54 -0700735 }
736 // We reset the counter to 1 so that the profile knows that the method was executed at least once.
737 // This is required for layout purposes.
Nicolas Geoffray88f50b12017-06-09 16:08:47 +0100738 // We also need to make sure we'll pass the warmup threshold again, so we set to 0 if
739 // the warmup threshold is 1.
740 uint16_t jit_warmup_threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
741 method->SetCounter(std::min(jit_warmup_threshold - 1, 1));
Mathieu Chartierf044c222017-05-31 15:27:54 -0700742}
743
Alex Light33b7b5d2018-08-07 19:13:51 +0000744void JitCodeCache::WaitForPotentialCollectionToCompleteRunnable(Thread* self) {
745 while (collection_in_progress_) {
746 lock_.Unlock(self);
747 {
748 ScopedThreadSuspension sts(self, kSuspended);
749 MutexLock mu(self, lock_);
750 WaitForPotentialCollectionToComplete(self);
751 }
752 lock_.Lock(self);
753 }
754}
755
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100756uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
757 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000758 uint8_t* stack_map,
759 uint8_t* roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100760 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000761 size_t code_size,
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100762 size_t data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000763 bool osr,
Vladimir Markoac3ac682018-09-20 11:01:43 +0100764 const std::vector<Handle<mirror::Object>>& roots,
Mingyao Yang063fc772016-08-02 11:02:54 -0700765 bool has_should_deoptimize_flag,
766 const ArenaSet<ArtMethod*>&
767 cha_single_implementation_list) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000768 DCHECK(!method->IsNative() || !osr);
Alex Light33b7b5d2018-08-07 19:13:51 +0000769
770 if (!method->IsNative()) {
771 // We need to do this before grabbing the lock_ because it needs to be able to see the string
772 // InternTable. Native methods do not have roots.
773 DCheckRootsAreValid(roots);
774 }
775
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100776 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
777 // Ensure the header ends up at expected instruction alignment.
778 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
779 size_t total_size = header_size + code_size;
780
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100781 OatQuickMethodHeader* method_header = nullptr;
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100782 uint8_t* code_ptr = nullptr;
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000783 uint8_t* memory = nullptr;
Alex Light33b7b5d2018-08-07 19:13:51 +0000784 MutexLock mu(self, lock_);
785 // We need to make sure that there will be no jit-gcs going on and wait for any ongoing one to
786 // finish.
787 WaitForPotentialCollectionToCompleteRunnable(self);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100788 {
Alex Light33b7b5d2018-08-07 19:13:51 +0000789 ScopedCodeCacheWrite scc(this);
790 memory = AllocateCode(total_size);
791 if (memory == nullptr) {
792 return nullptr;
793 }
794 code_ptr = memory + header_size;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000795
Alex Light33b7b5d2018-08-07 19:13:51 +0000796 std::copy(code, code + code_size, code_ptr);
797 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
798 new (method_header) OatQuickMethodHeader(
799 (stack_map != nullptr) ? code_ptr - stack_map : 0u,
800 code_size);
801 // Flush caches before we remove write permission because some ARMv8 Qualcomm kernels may
802 // trigger a segfault if a page fault occurs when requesting a cache maintenance operation.
803 // This is a kernel bug that we need to work around until affected devices (e.g. Nexus 5X and
804 // 6P) stop being supported or their kernels are fixed.
805 //
806 // For reference, this behavior is caused by this commit:
807 // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c
Orion Hodson38d29fd2018-09-07 12:58:37 +0100808 FlushInstructionCache(code_ptr, code_ptr + code_size);
Orion Hodsonf2331362018-07-11 15:14:10 +0100809
810 // Ensure CPU instruction pipelines are flushed for all cores. This is necessary for
811 // correctness as code may still be in instruction pipelines despite the i-cache flush. It is
812 // not safe to assume that changing permissions with mprotect (RX->RWX->RX) will cause a TLB
813 // shootdown (incidentally invalidating the CPU pipelines by sending an IPI to all cores to
814 // notify them of the TLB invalidation). Some architectures, notably ARM and ARM64, have
815 // hardware support that broadcasts TLB invalidations and so their kernels have no software
Orion Hodson563ada22018-09-04 11:28:31 +0100816 // based TLB shootdown.
817 art::membarrier(art::MembarrierCommand::kPrivateExpeditedSyncCore);
Orion Hodson38d29fd2018-09-07 12:58:37 +0100818
Alex Light33b7b5d2018-08-07 19:13:51 +0000819 DCHECK(!Runtime::Current()->IsAotCompiler());
820 if (has_should_deoptimize_flag) {
821 method_header->SetHasShouldDeoptimizeFlag();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100822 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100823
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000824 number_of_compilations_++;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100825 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000826 // We need to update the entry point in the runnable state for the instrumentation.
827 {
Alex Light33b7b5d2018-08-07 19:13:51 +0000828 // The following needs to be guarded by cha_lock_ also. Otherwise it's possible that the
829 // compiled code is considered invalidated by some class linking, but below we still make the
830 // compiled code valid for the method. Need cha_lock_ for checking all single-implementation
831 // flags and register dependencies.
Mingyao Yang063fc772016-08-02 11:02:54 -0700832 MutexLock cha_mu(self, *Locks::cha_lock_);
833 bool single_impl_still_valid = true;
834 for (ArtMethod* single_impl : cha_single_implementation_list) {
835 if (!single_impl->HasSingleImplementation()) {
Jeff Hao00286db2017-05-30 16:53:07 -0700836 // Simply discard the compiled code. Clear the counter so that it may be recompiled later.
837 // Hopefully the class hierarchy will be more stable when compilation is retried.
Mingyao Yang063fc772016-08-02 11:02:54 -0700838 single_impl_still_valid = false;
Mathieu Chartierf044c222017-05-31 15:27:54 -0700839 ClearMethodCounter(method, /*was_warm*/ false);
Mingyao Yang063fc772016-08-02 11:02:54 -0700840 break;
841 }
842 }
843
844 // Discard the code if any single-implementation assumptions are now invalid.
845 if (!single_impl_still_valid) {
846 VLOG(jit) << "JIT discarded jitted code due to invalid single-implementation assumptions.";
847 return nullptr;
848 }
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000849 DCHECK(cha_single_implementation_list.empty() || !Runtime::Current()->IsJavaDebuggable())
Alex Lightdba61482016-12-21 08:20:29 -0800850 << "Should not be using cha on debuggable apps/runs!";
851
Mingyao Yang063fc772016-08-02 11:02:54 -0700852 for (ArtMethod* single_impl : cha_single_implementation_list) {
Andreas Gampec1ac9ee2017-07-24 22:35:49 -0700853 Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()->AddDependency(
Mingyao Yang063fc772016-08-02 11:02:54 -0700854 single_impl, method, method_header);
855 }
856
Vladimir Marko2196c652017-11-30 16:16:07 +0000857 if (UNLIKELY(method->IsNative())) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000858 auto it = jni_stubs_map_.find(JniStubKey(method));
859 DCHECK(it != jni_stubs_map_.end())
860 << "Entry inserted in NotifyCompilationOf() should be alive.";
861 JniStubData* data = &it->second;
862 DCHECK(ContainsElement(data->GetMethods(), method))
863 << "Entry inserted in NotifyCompilationOf() should contain this method.";
864 data->SetCode(code_ptr);
865 instrumentation::Instrumentation* instrum = Runtime::Current()->GetInstrumentation();
866 for (ArtMethod* m : data->GetMethods()) {
867 instrum->UpdateMethodsCode(m, method_header->GetEntryPoint());
868 }
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100869 } else {
Vladimir Marko2196c652017-11-30 16:16:07 +0000870 // Fill the root table before updating the entry point.
871 DCHECK_EQ(FromStackMapToRoots(stack_map), roots_data);
872 DCHECK_LE(roots_data, stack_map);
873 FillRootTable(roots_data, roots);
874 {
875 // Flush data cache, as compiled code references literals in it.
Orion Hodson38d29fd2018-09-07 12:58:37 +0100876 FlushDataCache(roots_data, roots_data + data_size);
Vladimir Marko2196c652017-11-30 16:16:07 +0000877 }
878 method_code_map_.Put(code_ptr, method);
879 if (osr) {
880 number_of_osr_compilations_++;
881 osr_code_map_.Put(method, code_ptr);
882 } else {
883 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
884 method, method_header->GetEntryPoint());
885 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000886 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000887 VLOG(jit)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100888 << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
David Sehr709b0702016-10-13 09:12:37 -0700889 << ArtMethod::PrettyMethod(method) << "@" << method
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000890 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
891 << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
892 << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
Mingyao Yang063fc772016-08-02 11:02:54 -0700893 << reinterpret_cast<const void*>(method_header->GetEntryPoint() +
894 method_header->GetCodeSize());
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000895 histogram_code_memory_use_.AddValue(code_size);
896 if (code_size > kCodeSizeLogThreshold) {
897 LOG(INFO) << "JIT allocated "
898 << PrettySize(code_size)
899 << " for compiled code of "
David Sehr709b0702016-10-13 09:12:37 -0700900 << ArtMethod::PrettyMethod(method);
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000901 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000902 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100903
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100904 return reinterpret_cast<uint8_t*>(method_header);
905}
906
907size_t JitCodeCache::CodeCacheSize() {
908 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000909 return CodeCacheSizeLocked();
910}
911
Orion Hodsoneced6922017-06-01 10:54:28 +0100912bool JitCodeCache::RemoveMethod(ArtMethod* method, bool release_memory) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000913 // This function is used only for testing and only with non-native methods.
914 CHECK(!method->IsNative());
915
Orion Hodsoneced6922017-06-01 10:54:28 +0100916 MutexLock mu(Thread::Current(), lock_);
Orion Hodsoneced6922017-06-01 10:54:28 +0100917
Vladimir Marko2196c652017-11-30 16:16:07 +0000918 bool osr = osr_code_map_.find(method) != osr_code_map_.end();
919 bool in_cache = RemoveMethodLocked(method, release_memory);
Orion Hodsoneced6922017-06-01 10:54:28 +0100920
921 if (!in_cache) {
922 return false;
923 }
924
Orion Hodsoneced6922017-06-01 10:54:28 +0100925 method->ClearCounter();
926 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
927 method, GetQuickToInterpreterBridge());
928 VLOG(jit)
929 << "JIT removed (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
930 << ArtMethod::PrettyMethod(method) << "@" << method
931 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
932 << " dcache_size=" << PrettySize(DataCacheSizeLocked());
933 return true;
934}
935
Vladimir Marko2196c652017-11-30 16:16:07 +0000936bool JitCodeCache::RemoveMethodLocked(ArtMethod* method, bool release_memory) {
937 if (LIKELY(!method->IsNative())) {
938 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
939 if (info != nullptr) {
940 RemoveElement(profiling_infos_, info);
941 }
942 method->SetProfilingInfo(nullptr);
943 }
944
945 bool in_cache = false;
Calin Juravle016fcbe22018-05-03 19:47:35 -0700946 ScopedCodeCacheWrite ccw(this);
Vladimir Marko2196c652017-11-30 16:16:07 +0000947 if (UNLIKELY(method->IsNative())) {
948 auto it = jni_stubs_map_.find(JniStubKey(method));
949 if (it != jni_stubs_map_.end() && it->second.RemoveMethod(method)) {
950 in_cache = true;
951 if (it->second.GetMethods().empty()) {
952 if (release_memory) {
Orion Hodson607624f2018-05-11 10:10:46 +0100953 FreeCodeAndData(it->second.GetCode());
Vladimir Marko2196c652017-11-30 16:16:07 +0000954 }
955 jni_stubs_map_.erase(it);
956 } else {
957 it->first.UpdateShorty(it->second.GetMethods().front());
958 }
959 }
960 } else {
961 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
962 if (it->second == method) {
963 in_cache = true;
964 if (release_memory) {
Orion Hodson607624f2018-05-11 10:10:46 +0100965 FreeCodeAndData(it->first);
Vladimir Marko2196c652017-11-30 16:16:07 +0000966 }
967 it = method_code_map_.erase(it);
968 } else {
969 ++it;
970 }
971 }
972
973 auto osr_it = osr_code_map_.find(method);
974 if (osr_it != osr_code_map_.end()) {
975 osr_code_map_.erase(osr_it);
976 }
977 }
978
979 return in_cache;
980}
981
Alex Lightdba61482016-12-21 08:20:29 -0800982// This notifies the code cache that the given method has been redefined and that it should remove
983// any cached information it has on the method. All threads must be suspended before calling this
984// method. The compiled code for the method (if there is any) must not be in any threads call stack.
985void JitCodeCache::NotifyMethodRedefined(ArtMethod* method) {
986 MutexLock mu(Thread::Current(), lock_);
Vladimir Marko2196c652017-11-30 16:16:07 +0000987 RemoveMethodLocked(method, /* release_memory */ true);
Alex Lightdba61482016-12-21 08:20:29 -0800988}
989
990// This invalidates old_method. Once this function returns one can no longer use old_method to
991// execute code unless it is fixed up. This fixup will happen later in the process of installing a
992// class redefinition.
993// TODO We should add some info to ArtMethod to note that 'old_method' has been invalidated and
994// shouldn't be used since it is no longer logically in the jit code cache.
995// TODO We should add DCHECKS that validate that the JIT is paused when this method is entered.
996void JitCodeCache::MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000997 MutexLock mu(Thread::Current(), lock_);
Alex Lighteee0bd42017-02-14 15:31:45 +0000998 if (old_method->IsNative()) {
Vladimir Marko2196c652017-11-30 16:16:07 +0000999 // Update methods in jni_stubs_map_.
1000 for (auto& entry : jni_stubs_map_) {
1001 JniStubData& data = entry.second;
1002 data.MoveObsoleteMethod(old_method, new_method);
1003 }
Alex Lighteee0bd42017-02-14 15:31:45 +00001004 return;
1005 }
Alex Lightdba61482016-12-21 08:20:29 -08001006 // Update ProfilingInfo to the new one and remove it from the old_method.
1007 if (old_method->GetProfilingInfo(kRuntimePointerSize) != nullptr) {
1008 DCHECK_EQ(old_method->GetProfilingInfo(kRuntimePointerSize)->GetMethod(), old_method);
1009 ProfilingInfo* info = old_method->GetProfilingInfo(kRuntimePointerSize);
1010 old_method->SetProfilingInfo(nullptr);
1011 // Since the JIT should be paused and all threads suspended by the time this is called these
1012 // checks should always pass.
1013 DCHECK(!info->IsInUseByCompiler());
1014 new_method->SetProfilingInfo(info);
Alex Light2d441b12018-06-08 15:33:21 -07001015 // Get rid of the old saved entrypoint if it is there.
1016 info->SetSavedEntryPoint(nullptr);
Alex Lightdba61482016-12-21 08:20:29 -08001017 info->method_ = new_method;
1018 }
1019 // Update method_code_map_ to point to the new method.
1020 for (auto& it : method_code_map_) {
1021 if (it.second == old_method) {
1022 it.second = new_method;
1023 }
1024 }
1025 // Update osr_code_map_ to point to the new method.
1026 auto code_map = osr_code_map_.find(old_method);
1027 if (code_map != osr_code_map_.end()) {
1028 osr_code_map_.Put(new_method, code_map->second);
1029 osr_code_map_.erase(old_method);
1030 }
1031}
1032
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00001033size_t JitCodeCache::CodeCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001034 return used_memory_for_code_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +01001035}
1036
1037size_t JitCodeCache::DataCacheSize() {
1038 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00001039 return DataCacheSizeLocked();
1040}
1041
1042size_t JitCodeCache::DataCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001043 return used_memory_for_data_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001044}
1045
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001046void JitCodeCache::ClearData(Thread* self,
1047 uint8_t* stack_map_data,
1048 uint8_t* roots_data) {
1049 DCHECK_EQ(FromStackMapToRoots(stack_map_data), roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001050 MutexLock mu(self, lock_);
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001051 FreeData(reinterpret_cast<uint8_t*>(roots_data));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001052}
1053
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001054size_t JitCodeCache::ReserveData(Thread* self,
1055 size_t stack_map_size,
1056 size_t number_of_roots,
1057 ArtMethod* method,
1058 uint8_t** stack_map_data,
1059 uint8_t** roots_data) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001060 size_t table_size = ComputeRootTableSize(number_of_roots);
David Srbecky8cd54542018-07-15 23:58:44 +01001061 size_t size = RoundUp(stack_map_size + table_size, sizeof(void*));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001062 uint8_t* result = nullptr;
1063
1064 {
1065 ScopedThreadSuspension sts(self, kSuspended);
1066 MutexLock mu(self, lock_);
1067 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001068 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001069 }
1070
1071 if (result == nullptr) {
1072 // Retry.
1073 GarbageCollectCache(self);
1074 ScopedThreadSuspension sts(self, kSuspended);
1075 MutexLock mu(self, lock_);
1076 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001077 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001078 }
1079
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001080 MutexLock mu(self, lock_);
1081 histogram_stack_map_memory_use_.AddValue(size);
1082 if (size > kStackMapSizeLogThreshold) {
1083 LOG(INFO) << "JIT allocated "
1084 << PrettySize(size)
1085 << " for stack maps of "
David Sehr709b0702016-10-13 09:12:37 -07001086 << ArtMethod::PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001087 }
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001088 if (result != nullptr) {
1089 *roots_data = result;
1090 *stack_map_data = result + table_size;
1091 FillRootTableLength(*roots_data, number_of_roots);
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001092 return size;
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001093 } else {
1094 *roots_data = nullptr;
1095 *stack_map_data = nullptr;
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001096 return 0;
Nicolas Geoffrayf4b94422016-12-05 00:10:09 +00001097 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001098}
1099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001100class MarkCodeVisitor final : public StackVisitor {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001101 public:
1102 MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
1103 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
1104 code_cache_(code_cache_in),
1105 bitmap_(code_cache_->GetLiveBitmap()) {}
1106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001107 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001108 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
1109 if (method_header == nullptr) {
1110 return true;
1111 }
1112 const void* code = method_header->GetCode();
1113 if (code_cache_->ContainsPc(code)) {
1114 // Use the atomic set version, as multiple threads are executing this code.
1115 bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
1116 }
1117 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001118 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001119
1120 private:
1121 JitCodeCache* const code_cache_;
1122 CodeCacheBitmap* const bitmap_;
1123};
1124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001125class MarkCodeClosure final : public Closure {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001126 public:
1127 MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
1128 : code_cache_(code_cache), barrier_(barrier) {}
1129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001130 void Run(Thread* thread) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001131 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001132 DCHECK(thread == Thread::Current() || thread->IsSuspended());
1133 MarkCodeVisitor visitor(thread, code_cache_);
1134 visitor.WalkStack();
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +00001135 if (kIsDebugBuild) {
1136 // The stack walking code queries the side instrumentation stack if it
1137 // sees an instrumentation exit pc, so the JIT code of methods in that stack
1138 // must have been seen. We sanity check this below.
1139 for (const instrumentation::InstrumentationStackFrame& frame
1140 : *thread->GetInstrumentationStack()) {
1141 // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
1142 // its stack frame, it is not the method owning return_pc_. We just pass null to
1143 // LookupMethodHeader: the method is only checked against in debug builds.
1144 OatQuickMethodHeader* method_header =
Vladimir Marko2196c652017-11-30 16:16:07 +00001145 code_cache_->LookupMethodHeader(frame.return_pc_, /* method */ nullptr);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +00001146 if (method_header != nullptr) {
1147 const void* code = method_header->GetCode();
1148 CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
1149 }
1150 }
1151 }
Mathieu Chartier10d25082015-10-28 18:36:09 -07001152 barrier_->Pass(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001153 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001154
1155 private:
1156 JitCodeCache* const code_cache_;
1157 Barrier* const barrier_;
1158};
1159
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001160void JitCodeCache::NotifyCollectionDone(Thread* self) {
1161 collection_in_progress_ = false;
1162 lock_cond_.Broadcast(self);
1163}
1164
1165void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
1166 size_t per_space_footprint = new_footprint / 2;
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001167 DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001168 DCHECK_EQ(per_space_footprint * 2, new_footprint);
1169 mspace_set_footprint_limit(data_mspace_, per_space_footprint);
1170 {
Calin Juravle016fcbe22018-05-03 19:47:35 -07001171 ScopedCodeCacheWrite scc(this);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001172 mspace_set_footprint_limit(code_mspace_, per_space_footprint);
1173 }
1174}
1175
1176bool JitCodeCache::IncreaseCodeCacheCapacity() {
1177 if (current_capacity_ == max_capacity_) {
1178 return false;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001179 }
1180
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001181 // Double the capacity if we're below 1MB, or increase it by 1MB if
1182 // we're above.
1183 if (current_capacity_ < 1 * MB) {
1184 current_capacity_ *= 2;
1185 } else {
1186 current_capacity_ += 1 * MB;
1187 }
1188 if (current_capacity_ > max_capacity_) {
1189 current_capacity_ = max_capacity_;
1190 }
1191
Nicolas Geoffray646d6382017-08-09 10:50:00 +01001192 VLOG(jit) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001193
1194 SetFootprintLimit(current_capacity_);
1195
1196 return true;
1197}
1198
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001199void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
1200 Barrier barrier(0);
1201 size_t threads_running_checkpoint = 0;
1202 MarkCodeClosure closure(this, &barrier);
1203 threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
1204 // Now that we have run our checkpoint, move to a suspended state and wait
1205 // for other threads to run the checkpoint.
1206 ScopedThreadSuspension sts(self, kSuspended);
1207 if (threads_running_checkpoint != 0) {
1208 barrier.Increment(self, threads_running_checkpoint);
1209 }
1210}
1211
Nicolas Geoffray35122442016-03-02 12:05:30 +00001212bool JitCodeCache::ShouldDoFullCollection() {
1213 if (current_capacity_ == max_capacity_) {
1214 // Always do a full collection when the code cache is full.
1215 return true;
1216 } else if (current_capacity_ < kReservedCapacity) {
1217 // Always do partial collection when the code cache size is below the reserved
1218 // capacity.
1219 return false;
1220 } else if (last_collection_increased_code_cache_) {
1221 // This time do a full collection.
1222 return true;
1223 } else {
1224 // This time do a partial collection.
1225 return false;
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001226 }
1227}
1228
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001229void JitCodeCache::GarbageCollectCache(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001230 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001231 if (!garbage_collect_code_) {
1232 MutexLock mu(self, lock_);
1233 IncreaseCodeCacheCapacity();
1234 return;
1235 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001236
Nicolas Geoffraya5891e82015-11-06 14:18:27 +00001237 // Wait for an existing collection, or let everyone know we are starting one.
1238 {
1239 ScopedThreadSuspension sts(self, kSuspended);
1240 MutexLock mu(self, lock_);
1241 if (WaitForPotentialCollectionToComplete(self)) {
1242 return;
1243 } else {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001244 number_of_collections_++;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001245 live_bitmap_.reset(CodeCacheBitmap::Create(
1246 "code-cache-bitmap",
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001247 reinterpret_cast<uintptr_t>(code_map_.Begin()),
1248 reinterpret_cast<uintptr_t>(code_map_.Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001249 collection_in_progress_ = true;
1250 }
1251 }
1252
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001253 TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit));
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001254 {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001255 TimingLogger::ScopedTiming st("Code cache collection", &logger);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001256
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001257 bool do_full_collection = false;
1258 {
1259 MutexLock mu(self, lock_);
1260 do_full_collection = ShouldDoFullCollection();
Nicolas Geoffraya96917a2016-03-01 22:18:02 +00001261 }
1262
Nicolas Geoffray646d6382017-08-09 10:50:00 +01001263 VLOG(jit) << "Do "
1264 << (do_full_collection ? "full" : "partial")
1265 << " code cache collection, code="
1266 << PrettySize(CodeCacheSize())
1267 << ", data=" << PrettySize(DataCacheSize());
Nicolas Geoffray35122442016-03-02 12:05:30 +00001268
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001269 DoCollection(self, /* collect_profiling_info */ do_full_collection);
1270
Nicolas Geoffray646d6382017-08-09 10:50:00 +01001271 VLOG(jit) << "After code cache collection, code="
1272 << PrettySize(CodeCacheSize())
1273 << ", data=" << PrettySize(DataCacheSize());
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001274
1275 {
1276 MutexLock mu(self, lock_);
1277
1278 // Increase the code cache only when we do partial collections.
1279 // TODO: base this strategy on how full the code cache is?
1280 if (do_full_collection) {
1281 last_collection_increased_code_cache_ = false;
1282 } else {
1283 last_collection_increased_code_cache_ = true;
1284 IncreaseCodeCacheCapacity();
Nicolas Geoffray35122442016-03-02 12:05:30 +00001285 }
1286
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001287 bool next_collection_will_be_full = ShouldDoFullCollection();
1288
1289 // Start polling the liveness of compiled code to prepare for the next full collection.
Nicolas Geoffray480d5102016-04-18 12:09:30 +01001290 if (next_collection_will_be_full) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001291 // Save the entry point of methods we have compiled, and update the entry
1292 // point of those methods to the interpreter. If the method is invoked, the
1293 // interpreter will update its entry point to the compiled code and call it.
1294 for (ProfilingInfo* info : profiling_infos_) {
1295 const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1296 if (ContainsPc(entry_point)) {
1297 info->SetSavedEntryPoint(entry_point);
Vladimir Marko2196c652017-11-30 16:16:07 +00001298 // Don't call Instrumentation::UpdateMethodsCode(), as it can check the declaring
Nicolas Geoffray3b1a7f42017-02-22 10:21:00 +00001299 // class of the method. We may be concurrently running a GC which makes accessing
1300 // the class unsafe. We know it is OK to bypass the instrumentation as we've just
1301 // checked that the current entry point is JIT compiled code.
1302 info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001303 }
1304 }
1305
1306 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
Vladimir Marko2196c652017-11-30 16:16:07 +00001307
1308 // Change entry points of native methods back to the GenericJNI entrypoint.
1309 for (const auto& entry : jni_stubs_map_) {
1310 const JniStubData& data = entry.second;
1311 if (!data.IsCompiled()) {
1312 continue;
1313 }
1314 // Make sure a single invocation of the GenericJNI trampoline tries to recompile.
1315 uint16_t new_counter = Runtime::Current()->GetJit()->HotMethodThreshold() - 1u;
1316 const OatQuickMethodHeader* method_header =
1317 OatQuickMethodHeader::FromCodePointer(data.GetCode());
1318 for (ArtMethod* method : data.GetMethods()) {
1319 if (method->GetEntryPointFromQuickCompiledCode() == method_header->GetEntryPoint()) {
1320 // Don't call Instrumentation::UpdateMethodsCode(), same as for normal methods above.
1321 method->SetCounter(new_counter);
1322 method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
1323 }
1324 }
1325 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001326 }
1327 live_bitmap_.reset(nullptr);
1328 NotifyCollectionDone(self);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001329 }
Nicolas Geoffray35122442016-03-02 12:05:30 +00001330 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001331 Runtime::Current()->GetJit()->AddTimingLogger(logger);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001332}
1333
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001334void JitCodeCache::RemoveUnmarkedCode(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001335 ScopedTrace trace(__FUNCTION__);
Mingyao Yang063fc772016-08-02 11:02:54 -07001336 std::unordered_set<OatQuickMethodHeader*> method_headers;
1337 {
1338 MutexLock mu(self, lock_);
Calin Juravle016fcbe22018-05-03 19:47:35 -07001339 ScopedCodeCacheWrite scc(this);
Mingyao Yang063fc772016-08-02 11:02:54 -07001340 // Iterate over all compiled code and remove entries that are not marked.
Vladimir Marko2196c652017-11-30 16:16:07 +00001341 for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) {
1342 JniStubData* data = &it->second;
1343 if (!data->IsCompiled() || GetLiveBitmap()->Test(FromCodeToAllocation(data->GetCode()))) {
1344 ++it;
1345 } else {
1346 method_headers.insert(OatQuickMethodHeader::FromCodePointer(data->GetCode()));
1347 it = jni_stubs_map_.erase(it);
1348 }
1349 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001350 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
1351 const void* code_ptr = it->first;
1352 uintptr_t allocation = FromCodeToAllocation(code_ptr);
1353 if (GetLiveBitmap()->Test(allocation)) {
1354 ++it;
1355 } else {
Alex Light2d441b12018-06-08 15:33:21 -07001356 OatQuickMethodHeader* header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1357 method_headers.insert(header);
Mingyao Yang063fc772016-08-02 11:02:54 -07001358 it = method_code_map_.erase(it);
1359 }
Nicolas Geoffray35122442016-03-02 12:05:30 +00001360 }
1361 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001362 FreeAllMethodHeaders(method_headers);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001363}
1364
1365void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001366 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001367 {
1368 MutexLock mu(self, lock_);
1369 if (collect_profiling_info) {
1370 // Clear the profiling info of methods that do not have compiled code as entrypoint.
1371 // Also remove the saved entry point from the ProfilingInfo objects.
1372 for (ProfilingInfo* info : profiling_infos_) {
1373 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001374 if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001375 info->GetMethod()->SetProfilingInfo(nullptr);
1376 }
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +00001377
1378 if (info->GetSavedEntryPoint() != nullptr) {
1379 info->SetSavedEntryPoint(nullptr);
1380 // We are going to move this method back to interpreter. Clear the counter now to
Mathieu Chartierf044c222017-05-31 15:27:54 -07001381 // give it a chance to be hot again.
1382 ClearMethodCounter(info->GetMethod(), /*was_warm*/ true);
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +00001383 }
Nicolas Geoffray35122442016-03-02 12:05:30 +00001384 }
1385 } else if (kIsDebugBuild) {
1386 // Sanity check that the profiling infos do not have a dangling entry point.
1387 for (ProfilingInfo* info : profiling_infos_) {
1388 DCHECK(info->GetSavedEntryPoint() == nullptr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001389 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001390 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001391
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001392 // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not
1393 // an entry point is either:
1394 // - an osr compiled code, that will be removed if not in a thread call stack.
1395 // - discarded compiled code, that will be removed if not in a thread call stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00001396 for (const auto& entry : jni_stubs_map_) {
1397 const JniStubData& data = entry.second;
1398 const void* code_ptr = data.GetCode();
1399 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1400 for (ArtMethod* method : data.GetMethods()) {
1401 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1402 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1403 break;
1404 }
1405 }
1406 }
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001407 for (const auto& it : method_code_map_) {
1408 ArtMethod* method = it.second;
1409 const void* code_ptr = it.first;
1410 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1411 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1412 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1413 }
1414 }
1415
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001416 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001417 // on thread stacks).
1418 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001419 }
1420
1421 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +00001422 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001423
Nicolas Geoffray9abb2972016-03-04 14:32:59 +00001424 // At this point, mutator threads are still running, and entrypoints of methods can
1425 // change. We do know they cannot change to a code cache entry that is not marked,
1426 // therefore we can safely remove those entries.
1427 RemoveUnmarkedCode(self);
Nicolas Geoffraya96917a2016-03-01 22:18:02 +00001428
Nicolas Geoffray35122442016-03-02 12:05:30 +00001429 if (collect_profiling_info) {
1430 MutexLock mu(self, lock_);
1431 // Free all profiling infos of methods not compiled nor being compiled.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001432 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001433 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001434 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray511e41b2016-03-02 17:09:35 +00001435 // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope
1436 // that the compiled code would not get revived. As mutator threads run concurrently,
1437 // they may have revived the compiled code, and now we are in the situation where
1438 // a method has compiled code but no ProfilingInfo.
1439 // We make sure compiled methods have a ProfilingInfo object. It is needed for
1440 // code cache collection.
Andreas Gampe542451c2016-07-26 09:02:02 -07001441 if (ContainsPc(ptr) &&
1442 info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001443 info->GetMethod()->SetProfilingInfo(info);
Andreas Gampe542451c2016-07-26 09:02:02 -07001444 } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001445 // No need for this ProfilingInfo object anymore.
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001446 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001447 return true;
1448 }
1449 return false;
1450 });
1451 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray35122442016-03-02 12:05:30 +00001452 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001453 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001454}
1455
Nicolas Geoffray35122442016-03-02 12:05:30 +00001456bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001457 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001458 // Check that methods we have compiled do have a ProfilingInfo object. We would
1459 // have memory leaks of compiled code otherwise.
1460 for (const auto& it : method_code_map_) {
1461 ArtMethod* method = it.second;
Andreas Gampe542451c2016-07-26 09:02:02 -07001462 if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +00001463 const void* code_ptr = it.first;
1464 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1465 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1466 // If the code is not dead, then we have a problem. Note that this can even
1467 // happen just after a collection, as mutator threads are running in parallel
1468 // and could deoptimize an existing compiled code.
1469 return false;
1470 }
1471 }
1472 }
1473 return true;
1474}
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001475
1476OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001477 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
1478 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001479 // On Thumb-2, the pc is offset by one.
1480 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001481 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001482 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
1483 return nullptr;
1484 }
1485
Vladimir Marko2196c652017-11-30 16:16:07 +00001486 if (!kIsDebugBuild) {
1487 // Called with null `method` only from MarkCodeClosure::Run() in debug build.
1488 CHECK(method != nullptr);
Vladimir Marko47d31852017-11-28 18:36:12 +00001489 }
Vladimir Markoe7441632017-11-29 13:00:56 +00001490
Vladimir Marko2196c652017-11-30 16:16:07 +00001491 MutexLock mu(Thread::Current(), lock_);
1492 OatQuickMethodHeader* method_header = nullptr;
1493 ArtMethod* found_method = nullptr; // Only for DCHECK(), not for JNI stubs.
1494 if (method != nullptr && UNLIKELY(method->IsNative())) {
1495 auto it = jni_stubs_map_.find(JniStubKey(method));
1496 if (it == jni_stubs_map_.end() || !ContainsElement(it->second.GetMethods(), method)) {
1497 return nullptr;
1498 }
1499 const void* code_ptr = it->second.GetCode();
1500 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1501 if (!method_header->Contains(pc)) {
1502 return nullptr;
1503 }
1504 } else {
1505 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
1506 if (it != method_code_map_.begin()) {
1507 --it;
1508 const void* code_ptr = it->first;
1509 if (OatQuickMethodHeader::FromCodePointer(code_ptr)->Contains(pc)) {
1510 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1511 found_method = it->second;
1512 }
1513 }
1514 if (method_header == nullptr && method == nullptr) {
1515 // Scan all compiled JNI stubs as well. This slow search is used only
1516 // for checks in debug build, for release builds the `method` is not null.
1517 for (auto&& entry : jni_stubs_map_) {
1518 const JniStubData& data = entry.second;
1519 if (data.IsCompiled() &&
1520 OatQuickMethodHeader::FromCodePointer(data.GetCode())->Contains(pc)) {
1521 method_header = OatQuickMethodHeader::FromCodePointer(data.GetCode());
1522 }
1523 }
1524 }
1525 if (method_header == nullptr) {
1526 return nullptr;
1527 }
Nicolas Geoffray056d7752017-11-30 09:12:13 +00001528 }
Vladimir Marko2196c652017-11-30 16:16:07 +00001529
1530 if (kIsDebugBuild && method != nullptr && !method->IsNative()) {
Alex Light1ebe4fe2017-01-30 14:57:11 -08001531 // When we are walking the stack to redefine classes and creating obsolete methods it is
1532 // possible that we might have updated the method_code_map by making this method obsolete in a
1533 // previous frame. Therefore we should just check that the non-obsolete version of this method
1534 // is the one we expect. We change to the non-obsolete versions in the error message since the
1535 // obsolete version of the method might not be fully initialized yet. This situation can only
1536 // occur when we are in the process of allocating and setting up obsolete methods. Otherwise
Andreas Gampe06c42a52017-07-26 14:17:14 -07001537 // method and it->second should be identical. (See openjdkjvmti/ti_redefine.cc for more
Alex Light1ebe4fe2017-01-30 14:57:11 -08001538 // information.)
Vladimir Marko2196c652017-11-30 16:16:07 +00001539 DCHECK_EQ(found_method->GetNonObsoleteMethod(), method->GetNonObsoleteMethod())
Alex Light1ebe4fe2017-01-30 14:57:11 -08001540 << ArtMethod::PrettyMethod(method->GetNonObsoleteMethod()) << " "
Vladimir Marko2196c652017-11-30 16:16:07 +00001541 << ArtMethod::PrettyMethod(found_method->GetNonObsoleteMethod()) << " "
David Sehr709b0702016-10-13 09:12:37 -07001542 << std::hex << pc;
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +00001543 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +01001544 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001545}
1546
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001547OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
1548 MutexLock mu(Thread::Current(), lock_);
1549 auto it = osr_code_map_.find(method);
1550 if (it == osr_code_map_.end()) {
1551 return nullptr;
1552 }
1553 return OatQuickMethodHeader::FromCodePointer(it->second);
1554}
1555
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001556ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
1557 ArtMethod* method,
1558 const std::vector<uint32_t>& entries,
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001559 bool retry_allocation)
1560 // No thread safety analysis as we are using TryLock/Unlock explicitly.
1561 NO_THREAD_SAFETY_ANALYSIS {
1562 ProfilingInfo* info = nullptr;
1563 if (!retry_allocation) {
1564 // If we are allocating for the interpreter, just try to lock, to avoid
1565 // lock contention with the JIT.
1566 if (lock_.ExclusiveTryLock(self)) {
1567 info = AddProfilingInfoInternal(self, method, entries);
1568 lock_.ExclusiveUnlock(self);
1569 }
1570 } else {
1571 {
1572 MutexLock mu(self, lock_);
1573 info = AddProfilingInfoInternal(self, method, entries);
1574 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001575
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001576 if (info == nullptr) {
1577 GarbageCollectCache(self);
1578 MutexLock mu(self, lock_);
1579 info = AddProfilingInfoInternal(self, method, entries);
1580 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001581 }
1582 return info;
1583}
1584
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001585ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001586 ArtMethod* method,
1587 const std::vector<uint32_t>& entries) {
1588 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001589 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001590 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001591
1592 // Check whether some other thread has concurrently created it.
Andreas Gampe542451c2016-07-26 09:02:02 -07001593 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001594 if (info != nullptr) {
1595 return info;
1596 }
1597
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001598 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001599 if (data == nullptr) {
1600 return nullptr;
1601 }
1602 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +00001603
1604 // Make sure other threads see the data in the profiling info object before the
1605 // store in the ArtMethod's ProfilingInfo pointer.
Orion Hodson27b96762018-03-13 16:06:57 +00001606 std::atomic_thread_fence(std::memory_order_release);
Nicolas Geoffray07f35642016-01-04 16:06:51 +00001607
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001608 method->SetProfilingInfo(info);
1609 profiling_infos_.push_back(info);
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001610 histogram_profiling_info_memory_use_.AddValue(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001611 return info;
1612}
1613
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001614// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
1615// is already held.
1616void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
1617 if (code_mspace_ == mspace) {
1618 size_t result = code_end_;
1619 code_end_ += increment;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001620 return reinterpret_cast<void*>(result + code_map_.Begin());
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001621 } else {
1622 DCHECK_EQ(data_mspace_, mspace);
1623 size_t result = data_end_;
1624 data_end_ += increment;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001625 return reinterpret_cast<void*>(result + data_map_.Begin());
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001626 }
1627}
1628
Calin Juravle99629622016-04-19 16:33:46 +01001629void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle940eb0c2017-01-30 19:30:44 -08001630 std::vector<ProfileMethodInfo>& methods) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001631 ScopedTrace trace(__FUNCTION__);
Calin Juravle31f2c152015-10-23 17:56:15 +01001632 MutexLock mu(Thread::Current(), lock_);
Calin Juravlea39fd982017-05-18 10:15:52 -07001633 uint16_t jit_compile_threshold = Runtime::Current()->GetJITOptions()->GetCompileThreshold();
Calin Juravle99629622016-04-19 16:33:46 +01001634 for (const ProfilingInfo* info : profiling_infos_) {
1635 ArtMethod* method = info->GetMethod();
1636 const DexFile* dex_file = method->GetDexFile();
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001637 const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
1638 if (!ContainsElement(dex_base_locations, base_location)) {
Calin Juravle940eb0c2017-01-30 19:30:44 -08001639 // Skip dex files which are not profiled.
1640 continue;
Calin Juravle31f2c152015-10-23 17:56:15 +01001641 }
Calin Juravle940eb0c2017-01-30 19:30:44 -08001642 std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
Calin Juravlea39fd982017-05-18 10:15:52 -07001643
1644 // If the method didn't reach the compilation threshold don't save the inline caches.
1645 // They might be incomplete and cause unnecessary deoptimizations.
1646 // If the inline cache is empty the compiler will generate a regular invoke virtual/interface.
1647 if (method->GetCounter() < jit_compile_threshold) {
1648 methods.emplace_back(/*ProfileMethodInfo*/
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001649 MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches);
Calin Juravlea39fd982017-05-18 10:15:52 -07001650 continue;
1651 }
1652
Calin Juravle940eb0c2017-01-30 19:30:44 -08001653 for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
Mathieu Chartierdbddc222017-05-24 12:04:13 -07001654 std::vector<TypeReference> profile_classes;
Calin Juravle940eb0c2017-01-30 19:30:44 -08001655 const InlineCache& cache = info->cache_[i];
Calin Juravle13439f02017-02-21 01:17:21 -08001656 ArtMethod* caller = info->GetMethod();
Calin Juravle589e71e2017-03-03 16:05:05 -08001657 bool is_missing_types = false;
Calin Juravle940eb0c2017-01-30 19:30:44 -08001658 for (size_t k = 0; k < InlineCache::kIndividualCacheSize; k++) {
1659 mirror::Class* cls = cache.classes_[k].Read();
1660 if (cls == nullptr) {
1661 break;
1662 }
Calin Juravle4ca70a32017-02-21 16:22:24 -08001663
Calin Juravle13439f02017-02-21 01:17:21 -08001664 // Check if the receiver is in the boot class path or if it's in the
1665 // same class loader as the caller. If not, skip it, as there is not
1666 // much we can do during AOT.
1667 if (!cls->IsBootStrapClassLoaded() &&
1668 caller->GetClassLoader() != cls->GetClassLoader()) {
1669 is_missing_types = true;
1670 continue;
1671 }
1672
Calin Juravle4ca70a32017-02-21 16:22:24 -08001673 const DexFile* class_dex_file = nullptr;
1674 dex::TypeIndex type_index;
1675
1676 if (cls->GetDexCache() == nullptr) {
1677 DCHECK(cls->IsArrayClass()) << cls->PrettyClass();
Calin Juravlee21806f2017-02-22 11:49:43 -08001678 // Make a best effort to find the type index in the method's dex file.
1679 // We could search all open dex files but that might turn expensive
1680 // and probably not worth it.
Calin Juravle4ca70a32017-02-21 16:22:24 -08001681 class_dex_file = dex_file;
1682 type_index = cls->FindTypeIndexInOtherDexFile(*dex_file);
1683 } else {
1684 class_dex_file = &(cls->GetDexFile());
1685 type_index = cls->GetDexTypeIndex();
1686 }
1687 if (!type_index.IsValid()) {
1688 // Could be a proxy class or an array for which we couldn't find the type index.
Calin Juravle589e71e2017-03-03 16:05:05 -08001689 is_missing_types = true;
Calin Juravle4ca70a32017-02-21 16:22:24 -08001690 continue;
1691 }
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001692 if (ContainsElement(dex_base_locations,
1693 DexFileLoader::GetBaseLocation(class_dex_file->GetLocation()))) {
Calin Juravle940eb0c2017-01-30 19:30:44 -08001694 // Only consider classes from the same apk (including multidex).
1695 profile_classes.emplace_back(/*ProfileMethodInfo::ProfileClassReference*/
Calin Juravle4ca70a32017-02-21 16:22:24 -08001696 class_dex_file, type_index);
Calin Juravle589e71e2017-03-03 16:05:05 -08001697 } else {
1698 is_missing_types = true;
Calin Juravle940eb0c2017-01-30 19:30:44 -08001699 }
1700 }
1701 if (!profile_classes.empty()) {
1702 inline_caches.emplace_back(/*ProfileMethodInfo::ProfileInlineCache*/
Calin Juravle589e71e2017-03-03 16:05:05 -08001703 cache.dex_pc_, is_missing_types, profile_classes);
Calin Juravle940eb0c2017-01-30 19:30:44 -08001704 }
1705 }
1706 methods.emplace_back(/*ProfileMethodInfo*/
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -07001707 MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches);
Calin Juravle31f2c152015-10-23 17:56:15 +01001708 }
1709}
1710
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +01001711bool JitCodeCache::IsOsrCompiled(ArtMethod* method) {
1712 MutexLock mu(Thread::Current(), lock_);
1713 return osr_code_map_.find(method) != osr_code_map_.end();
1714}
1715
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001716bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
1717 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001718 return false;
1719 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001720
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001721 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001722 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
1723 return false;
1724 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001725
Vladimir Marko2196c652017-11-30 16:16:07 +00001726 if (UNLIKELY(method->IsNative())) {
1727 JniStubKey key(method);
1728 auto it = jni_stubs_map_.find(key);
1729 bool new_compilation = false;
1730 if (it == jni_stubs_map_.end()) {
1731 // Create a new entry to mark the stub as being compiled.
1732 it = jni_stubs_map_.Put(key, JniStubData{});
1733 new_compilation = true;
1734 }
1735 JniStubData* data = &it->second;
1736 data->AddMethod(method);
1737 if (data->IsCompiled()) {
1738 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(data->GetCode());
1739 const void* entrypoint = method_header->GetEntryPoint();
1740 // Update also entrypoints of other methods held by the JniStubData.
1741 // We could simply update the entrypoint of `method` but if the last JIT GC has
1742 // changed these entrypoints to GenericJNI in preparation for a full GC, we may
1743 // as well change them back as this stub shall not be collected anyway and this
1744 // can avoid a few expensive GenericJNI calls.
1745 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1746 for (ArtMethod* m : data->GetMethods()) {
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +00001747 // Call the dedicated method instead of the more generic UpdateMethodsCode, because
1748 // `m` might be in the process of being deleted.
1749 instrumentation->UpdateNativeMethodsCodeToJitCode(m, entrypoint);
Vladimir Marko2196c652017-11-30 16:16:07 +00001750 }
1751 if (collection_in_progress_) {
1752 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(data->GetCode()));
1753 }
1754 }
1755 return new_compilation;
1756 } else {
1757 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1758 if (info == nullptr) {
1759 VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled";
1760 // Because the counter is not atomic, there are some rare cases where we may not hit the
1761 // threshold for creating the ProfilingInfo. Reset the counter now to "correct" this.
1762 ClearMethodCounter(method, /*was_warm*/ false);
1763 return false;
1764 }
Nicolas Geoffray056d7752017-11-30 09:12:13 +00001765
Vladimir Marko2196c652017-11-30 16:16:07 +00001766 if (info->IsMethodBeingCompiled(osr)) {
1767 return false;
1768 }
Nicolas Geoffray056d7752017-11-30 09:12:13 +00001769
Vladimir Marko2196c652017-11-30 16:16:07 +00001770 info->SetIsMethodBeingCompiled(true, osr);
1771 return true;
1772 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001773}
1774
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001775ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001776 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001777 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001778 if (info != nullptr) {
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +00001779 if (!info->IncrementInlineUse()) {
1780 // Overflow of inlining uses, just bail.
1781 return nullptr;
1782 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001783 }
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001784 return info;
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001785}
1786
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001787void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001788 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001789 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001790 DCHECK(info != nullptr);
1791 info->DecrementInlineUse();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001792}
1793
Vladimir Marko2196c652017-11-30 16:16:07 +00001794void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self, bool osr) {
1795 DCHECK_EQ(Thread::Current(), self);
1796 MutexLock mu(self, lock_);
1797 if (UNLIKELY(method->IsNative())) {
1798 auto it = jni_stubs_map_.find(JniStubKey(method));
1799 DCHECK(it != jni_stubs_map_.end());
1800 JniStubData* data = &it->second;
1801 DCHECK(ContainsElement(data->GetMethods(), method));
1802 if (UNLIKELY(!data->IsCompiled())) {
1803 // Failed to compile; the JNI compiler never fails, but the cache may be full.
1804 jni_stubs_map_.erase(it); // Remove the entry added in NotifyCompilationOf().
1805 } // else CommitCodeInternal() updated entrypoints of all methods in the JniStubData.
1806 } else {
1807 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1808 DCHECK(info->IsMethodBeingCompiled(osr));
1809 info->SetIsMethodBeingCompiled(false, osr);
1810 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001811}
1812
Nicolas Geoffraya25dce92016-01-12 16:41:10 +00001813size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
1814 MutexLock mu(Thread::Current(), lock_);
1815 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
1816}
1817
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001818void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
1819 const OatQuickMethodHeader* header) {
Vladimir Marko2196c652017-11-30 16:16:07 +00001820 DCHECK(!method->IsNative());
Andreas Gampe542451c2016-07-26 09:02:02 -07001821 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Alex Light2d441b12018-06-08 15:33:21 -07001822 const void* method_entrypoint = method->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray35122442016-03-02 12:05:30 +00001823 if ((profiling_info != nullptr) &&
1824 (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
Alex Light2d441b12018-06-08 15:33:21 -07001825 // When instrumentation is set, the actual entrypoint is the one in the profiling info.
1826 method_entrypoint = profiling_info->GetSavedEntryPoint();
Nicolas Geoffray35122442016-03-02 12:05:30 +00001827 // Prevent future uses of the compiled code.
1828 profiling_info->SetSavedEntryPoint(nullptr);
1829 }
1830
Alex Light2d441b12018-06-08 15:33:21 -07001831 // Clear the method counter if we are running jitted code since we might want to jit this again in
1832 // the future.
1833 if (method_entrypoint == header->GetEntryPoint()) {
Jeff Hao00286db2017-05-30 16:53:07 -07001834 // The entrypoint is the one to invalidate, so we just update it to the interpreter entry point
Mathieu Chartierf044c222017-05-31 15:27:54 -07001835 // and clear the counter to get the method Jitted again.
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001836 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1837 method, GetQuickToInterpreterBridge());
Mathieu Chartierf044c222017-05-31 15:27:54 -07001838 ClearMethodCounter(method, /*was_warm*/ profiling_info != nullptr);
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001839 } else {
1840 MutexLock mu(Thread::Current(), lock_);
1841 auto it = osr_code_map_.find(method);
1842 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
1843 // Remove the OSR method, to avoid using it again.
1844 osr_code_map_.erase(it);
1845 }
1846 }
1847}
1848
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001849uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
1850 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
1851 uint8_t* result = reinterpret_cast<uint8_t*>(
1852 mspace_memalign(code_mspace_, alignment, code_size));
1853 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
1854 // Ensure the header ends up at expected instruction alignment.
1855 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
1856 used_memory_for_code_ += mspace_usable_size(result);
1857 return result;
1858}
1859
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001860void JitCodeCache::FreeCode(uint8_t* code) {
1861 used_memory_for_code_ -= mspace_usable_size(code);
1862 mspace_free(code_mspace_, code);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001863}
1864
1865uint8_t* JitCodeCache::AllocateData(size_t data_size) {
1866 void* result = mspace_malloc(data_mspace_, data_size);
1867 used_memory_for_data_ += mspace_usable_size(result);
1868 return reinterpret_cast<uint8_t*>(result);
1869}
1870
1871void JitCodeCache::FreeData(uint8_t* data) {
1872 used_memory_for_data_ -= mspace_usable_size(data);
1873 mspace_free(data_mspace_, data);
1874}
1875
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001876void JitCodeCache::Dump(std::ostream& os) {
1877 MutexLock mu(Thread::Current(), lock_);
David Srbeckyfb3de3d2018-01-29 16:11:49 +00001878 MutexLock mu2(Thread::Current(), *Locks::native_debug_interface_lock_);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001879 os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n"
1880 << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n"
David Srbecky440a9b32018-02-15 17:47:29 +00001881 << "Current JIT mini-debug-info size: " << PrettySize(GetJitNativeDebugInfoMemUsage()) << "\n"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001882 << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n"
Vladimir Marko2196c652017-11-30 16:16:07 +00001883 << "Current number of JIT JNI stub entries: " << jni_stubs_map_.size() << "\n"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001884 << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n"
1885 << "Total number of JIT compilations: " << number_of_compilations_ << "\n"
1886 << "Total number of JIT compilations for on stack replacement: "
1887 << number_of_osr_compilations_ << "\n"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001888 << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl;
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001889 histogram_stack_map_memory_use_.PrintMemoryUse(os);
1890 histogram_code_memory_use_.PrintMemoryUse(os);
1891 histogram_profiling_info_memory_use_.PrintMemoryUse(os);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001892}
1893
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001894} // namespace jit
1895} // namespace art