• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "jit_code_cache.h"
18 
19 #include <sstream>
20 
21 #include <android-base/logging.h>
22 
23 #include "arch/context.h"
24 #include "art_method-inl.h"
25 #include "base/enums.h"
26 #include "base/histogram-inl.h"
27 #include "base/logging.h"  // For VLOG.
28 #include "base/membarrier.h"
29 #include "base/memfd.h"
30 #include "base/mem_map.h"
31 #include "base/quasi_atomic.h"
32 #include "base/stl_util.h"
33 #include "base/systrace.h"
34 #include "base/time_utils.h"
35 #include "base/utils.h"
36 #include "cha.h"
37 #include "debugger_interface.h"
38 #include "dex/dex_file_loader.h"
39 #include "dex/method_reference.h"
40 #include "entrypoints/entrypoint_utils-inl.h"
41 #include "entrypoints/runtime_asm_entrypoints.h"
42 #include "gc/accounting/bitmap-inl.h"
43 #include "gc/allocator/dlmalloc.h"
44 #include "gc/scoped_gc_critical_section.h"
45 #include "handle.h"
46 #include "instrumentation.h"
47 #include "intern_table.h"
48 #include "jit/jit.h"
49 #include "jit/profiling_info.h"
50 #include "jit/jit_scoped_code_cache_write.h"
51 #include "linear_alloc.h"
52 #include "oat_file-inl.h"
53 #include "oat_quick_method_header.h"
54 #include "object_callbacks.h"
55 #include "profile/profile_compilation_info.h"
56 #include "scoped_thread_state_change-inl.h"
57 #include "stack.h"
58 #include "thread-current-inl.h"
59 #include "thread_list.h"
60 
61 namespace art {
62 namespace jit {
63 
64 static constexpr size_t kCodeSizeLogThreshold = 50 * KB;
65 static constexpr size_t kStackMapSizeLogThreshold = 50 * KB;
66 
67 class JitCodeCache::JniStubKey {
68  public:
REQUIRES_SHARED(Locks::mutator_lock_)69   explicit JniStubKey(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
70       : shorty_(method->GetShorty()),
71         is_static_(method->IsStatic()),
72         is_fast_native_(method->IsFastNative()),
73         is_critical_native_(method->IsCriticalNative()),
74         is_synchronized_(method->IsSynchronized()) {
75     DCHECK(!(is_fast_native_ && is_critical_native_));
76   }
77 
operator <(const JniStubKey & rhs) const78   bool operator<(const JniStubKey& rhs) const {
79     if (is_static_ != rhs.is_static_) {
80       return rhs.is_static_;
81     }
82     if (is_synchronized_ != rhs.is_synchronized_) {
83       return rhs.is_synchronized_;
84     }
85     if (is_fast_native_ != rhs.is_fast_native_) {
86       return rhs.is_fast_native_;
87     }
88     if (is_critical_native_ != rhs.is_critical_native_) {
89       return rhs.is_critical_native_;
90     }
91     return strcmp(shorty_, rhs.shorty_) < 0;
92   }
93 
94   // Update the shorty to point to another method's shorty. Call this function when removing
95   // the method that references the old shorty from JniCodeData and not removing the entire
96   // JniCodeData; the old shorty may become a dangling pointer when that method is unloaded.
UpdateShorty(ArtMethod * method) const97   void UpdateShorty(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
98     const char* shorty = method->GetShorty();
99     DCHECK_STREQ(shorty_, shorty);
100     shorty_ = shorty;
101   }
102 
103  private:
104   // The shorty points to a DexFile data and may need to change
105   // to point to the same shorty in a different DexFile.
106   mutable const char* shorty_;
107 
108   const bool is_static_;
109   const bool is_fast_native_;
110   const bool is_critical_native_;
111   const bool is_synchronized_;
112 };
113 
114 class JitCodeCache::JniStubData {
115  public:
JniStubData()116   JniStubData() : code_(nullptr), methods_() {}
117 
SetCode(const void * code)118   void SetCode(const void* code) {
119     DCHECK(code != nullptr);
120     code_ = code;
121   }
122 
UpdateEntryPoints(const void * entrypoint)123   void UpdateEntryPoints(const void* entrypoint) REQUIRES_SHARED(Locks::mutator_lock_) {
124     DCHECK(IsCompiled());
125     DCHECK(entrypoint == OatQuickMethodHeader::FromCodePointer(GetCode())->GetEntryPoint());
126     instrumentation::Instrumentation* instrum = Runtime::Current()->GetInstrumentation();
127     for (ArtMethod* m : GetMethods()) {
128       // Because `m` might be in the process of being deleted:
129       // - Call the dedicated method instead of the more generic UpdateMethodsCode
130       // - Check the class status without a full read barrier; use ReadBarrier::IsMarked().
131       bool can_set_entrypoint = true;
132       if (NeedsClinitCheckBeforeCall(m)) {
133         // To avoid resurrecting an unreachable object, we must not use a full read
134         // barrier but we do not want to miss updating an entrypoint under common
135         // circumstances, i.e. during a GC the class becomes visibly initialized,
136         // the method becomes hot, we compile the thunk and want to update the
137         // entrypoint while the method's declaring class field still points to the
138         // from-space class object with the old status. Therefore we read the
139         // declaring class without a read barrier and check if it's already marked.
140         // If yes, we check the status of the to-space class object as intended.
141         // Otherwise, there is no to-space object and the from-space class object
142         // contains the most recent value of the status field; even if this races
143         // with another thread doing a read barrier and updating the status, that's
144         // no different from a race with a thread that just updates the status.
145         // Such race can happen only for the zygote method pre-compilation, as we
146         // otherwise compile only thunks for methods of visibly initialized classes.
147         ObjPtr<mirror::Class> klass = m->GetDeclaringClass<kWithoutReadBarrier>();
148         ObjPtr<mirror::Class> marked = ReadBarrier::IsMarked(klass.Ptr());
149         ObjPtr<mirror::Class> checked_klass = (marked != nullptr) ? marked : klass;
150         can_set_entrypoint = checked_klass->IsVisiblyInitialized();
151       }
152       if (can_set_entrypoint) {
153         instrum->UpdateNativeMethodsCodeToJitCode(m, entrypoint);
154       }
155     }
156   }
157 
GetCode() const158   const void* GetCode() const {
159     return code_;
160   }
161 
IsCompiled() const162   bool IsCompiled() const {
163     return GetCode() != nullptr;
164   }
165 
AddMethod(ArtMethod * method)166   void AddMethod(ArtMethod* method) {
167     if (!ContainsElement(methods_, method)) {
168       methods_.push_back(method);
169     }
170   }
171 
GetMethods() const172   const std::vector<ArtMethod*>& GetMethods() const {
173     return methods_;
174   }
175 
RemoveMethodsIn(const LinearAlloc & alloc)176   void RemoveMethodsIn(const LinearAlloc& alloc) {
177     auto kept_end = std::remove_if(
178         methods_.begin(),
179         methods_.end(),
180         [&alloc](ArtMethod* method) { return alloc.ContainsUnsafe(method); });
181     methods_.erase(kept_end, methods_.end());
182   }
183 
RemoveMethod(ArtMethod * method)184   bool RemoveMethod(ArtMethod* method) {
185     auto it = std::find(methods_.begin(), methods_.end(), method);
186     if (it != methods_.end()) {
187       methods_.erase(it);
188       return true;
189     } else {
190       return false;
191     }
192   }
193 
MoveObsoleteMethod(ArtMethod * old_method,ArtMethod * new_method)194   void MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
195     std::replace(methods_.begin(), methods_.end(), old_method, new_method);
196   }
197 
198  private:
199   const void* code_;
200   std::vector<ArtMethod*> methods_;
201 };
202 
Create(bool used_only_for_profile_data,bool rwx_memory_allowed,bool is_zygote,std::string * error_msg)203 JitCodeCache* JitCodeCache::Create(bool used_only_for_profile_data,
204                                    bool rwx_memory_allowed,
205                                    bool is_zygote,
206                                    std::string* error_msg) {
207   // Register for membarrier expedited sync core if JIT will be generating code.
208   if (!used_only_for_profile_data) {
209     if (art::membarrier(art::MembarrierCommand::kRegisterPrivateExpeditedSyncCore) != 0) {
210       // MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE ensures that CPU instruction pipelines are
211       // flushed and it's used when adding code to the JIT. The memory used by the new code may
212       // have just been released and, in theory, the old code could still be in a pipeline.
213       VLOG(jit) << "Kernel does not support membarrier sync-core";
214     }
215   }
216 
217   size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity();
218   // Check whether the provided max capacity in options is below 1GB.
219   size_t max_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheMaxCapacity();
220   // We need to have 32 bit offsets from method headers in code cache which point to things
221   // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
222   // Ensure we're below 1 GB to be safe.
223   if (max_capacity > 1 * GB) {
224     std::ostringstream oss;
225     oss << "Maxium code cache capacity is limited to 1 GB, "
226         << PrettySize(max_capacity) << " is too big";
227     *error_msg = oss.str();
228     return nullptr;
229   }
230 
231   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
232   JitMemoryRegion region;
233   if (!region.Initialize(initial_capacity,
234                          max_capacity,
235                          rwx_memory_allowed,
236                          is_zygote,
237                          error_msg)) {
238     return nullptr;
239   }
240 
241   std::unique_ptr<JitCodeCache> jit_code_cache(new JitCodeCache());
242   if (is_zygote) {
243     // Zygote should never collect code to share the memory with the children.
244     jit_code_cache->garbage_collect_code_ = false;
245     jit_code_cache->shared_region_ = std::move(region);
246   } else {
247     jit_code_cache->private_region_ = std::move(region);
248   }
249 
250   VLOG(jit) << "Created jit code cache: initial capacity="
251             << PrettySize(initial_capacity)
252             << ", maximum capacity="
253             << PrettySize(max_capacity);
254 
255   return jit_code_cache.release();
256 }
257 
JitCodeCache()258 JitCodeCache::JitCodeCache()
259     : is_weak_access_enabled_(true),
260       inline_cache_cond_("Jit inline cache condition variable", *Locks::jit_lock_),
261       zygote_map_(&shared_region_),
262       lock_cond_("Jit code cache condition variable", *Locks::jit_lock_),
263       collection_in_progress_(false),
264       last_collection_increased_code_cache_(false),
265       garbage_collect_code_(true),
266       number_of_compilations_(0),
267       number_of_osr_compilations_(0),
268       number_of_collections_(0),
269       histogram_stack_map_memory_use_("Memory used for stack maps", 16),
270       histogram_code_memory_use_("Memory used for compiled code", 16),
271       histogram_profiling_info_memory_use_("Memory used for profiling info", 16) {
272 }
273 
~JitCodeCache()274 JitCodeCache::~JitCodeCache() {}
275 
PrivateRegionContainsPc(const void * ptr) const276 bool JitCodeCache::PrivateRegionContainsPc(const void* ptr) const {
277   return private_region_.IsInExecSpace(ptr);
278 }
279 
ContainsPc(const void * ptr) const280 bool JitCodeCache::ContainsPc(const void* ptr) const {
281   return PrivateRegionContainsPc(ptr) || shared_region_.IsInExecSpace(ptr);
282 }
283 
WillExecuteJitCode(ArtMethod * method)284 bool JitCodeCache::WillExecuteJitCode(ArtMethod* method) {
285   ScopedObjectAccess soa(art::Thread::Current());
286   ScopedAssertNoThreadSuspension sants(__FUNCTION__);
287   if (ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
288     return true;
289   } else if (method->GetEntryPointFromQuickCompiledCode() == GetQuickInstrumentationEntryPoint()) {
290     return FindCompiledCodeForInstrumentation(method) != nullptr;
291   }
292   return false;
293 }
294 
ContainsMethod(ArtMethod * method)295 bool JitCodeCache::ContainsMethod(ArtMethod* method) {
296   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
297   if (UNLIKELY(method->IsNative())) {
298     auto it = jni_stubs_map_.find(JniStubKey(method));
299     if (it != jni_stubs_map_.end() &&
300         it->second.IsCompiled() &&
301         ContainsElement(it->second.GetMethods(), method)) {
302       return true;
303     }
304   } else {
305     for (const auto& it : method_code_map_) {
306       if (it.second == method) {
307         return true;
308       }
309     }
310     if (zygote_map_.ContainsMethod(method)) {
311       return true;
312     }
313   }
314   return false;
315 }
316 
GetJniStubCode(ArtMethod * method)317 const void* JitCodeCache::GetJniStubCode(ArtMethod* method) {
318   DCHECK(method->IsNative());
319   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
320   auto it = jni_stubs_map_.find(JniStubKey(method));
321   if (it != jni_stubs_map_.end()) {
322     JniStubData& data = it->second;
323     if (data.IsCompiled() && ContainsElement(data.GetMethods(), method)) {
324       return data.GetCode();
325     }
326   }
327   return nullptr;
328 }
329 
FindCompiledCodeForInstrumentation(ArtMethod * method)330 const void* JitCodeCache::FindCompiledCodeForInstrumentation(ArtMethod* method) {
331   // If jit-gc is still on we use the SavedEntryPoint field for doing that and so cannot use it to
332   // find the instrumentation entrypoint.
333   if (LIKELY(GetGarbageCollectCode())) {
334     return nullptr;
335   }
336   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
337   if (info == nullptr) {
338     return nullptr;
339   }
340   // When GC is disabled for trampoline tracing we will use SavedEntrypoint to hold the actual
341   // jit-compiled version of the method. If jit-gc is disabled for other reasons this will just be
342   // nullptr.
343   return info->GetSavedEntryPoint();
344 }
345 
GetSavedEntryPointOfPreCompiledMethod(ArtMethod * method)346 const void* JitCodeCache::GetSavedEntryPointOfPreCompiledMethod(ArtMethod* method) {
347   if (method->IsPreCompiled()) {
348     const void* code_ptr = nullptr;
349     if (method->GetDeclaringClass()->GetClassLoader() == nullptr) {
350       code_ptr = zygote_map_.GetCodeFor(method);
351     } else {
352       MutexLock mu(Thread::Current(), *Locks::jit_lock_);
353       auto it = saved_compiled_methods_map_.find(method);
354       if (it != saved_compiled_methods_map_.end()) {
355         code_ptr = it->second;
356       }
357     }
358     if (code_ptr != nullptr) {
359       OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
360       return method_header->GetEntryPoint();
361     }
362   }
363   return nullptr;
364 }
365 
WaitForPotentialCollectionToComplete(Thread * self)366 bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
367   bool in_collection = false;
368   while (collection_in_progress_) {
369     in_collection = true;
370     lock_cond_.Wait(self);
371   }
372   return in_collection;
373 }
374 
FromCodeToAllocation(const void * code)375 static uintptr_t FromCodeToAllocation(const void* code) {
376   size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
377   return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
378 }
379 
GetNumberOfRoots(const uint8_t * stack_map)380 static uint32_t GetNumberOfRoots(const uint8_t* stack_map) {
381   // The length of the table is stored just before the stack map (and therefore at the end of
382   // the table itself), in order to be able to fetch it from a `stack_map` pointer.
383   return reinterpret_cast<const uint32_t*>(stack_map)[-1];
384 }
385 
DCheckRootsAreValid(const std::vector<Handle<mirror::Object>> & roots,bool is_shared_region)386 static void DCheckRootsAreValid(const std::vector<Handle<mirror::Object>>& roots,
387                                 bool is_shared_region)
388     REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_) {
389   if (!kIsDebugBuild) {
390     return;
391   }
392   // Put all roots in `roots_data`.
393   for (Handle<mirror::Object> object : roots) {
394     // Ensure the string is strongly interned. b/32995596
395     if (object->IsString()) {
396       ObjPtr<mirror::String> str = object->AsString();
397       ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
398       CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr);
399     }
400     // Ensure that we don't put movable objects in the shared region.
401     if (is_shared_region) {
402       CHECK(!Runtime::Current()->GetHeap()->IsMovableObject(object.Get()));
403     }
404   }
405 }
406 
GetRootTable(const void * code_ptr,uint32_t * number_of_roots=nullptr)407 static const uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) {
408   OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
409   uint8_t* data = method_header->GetOptimizedCodeInfoPtr();
410   uint32_t roots = GetNumberOfRoots(data);
411   if (number_of_roots != nullptr) {
412     *number_of_roots = roots;
413   }
414   return data - ComputeRootTableSize(roots);
415 }
416 
SweepRootTables(IsMarkedVisitor * visitor)417 void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) {
418   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
419   for (const auto& entry : method_code_map_) {
420     uint32_t number_of_roots = 0;
421     const uint8_t* root_table = GetRootTable(entry.first, &number_of_roots);
422     uint8_t* roots_data = private_region_.IsInDataSpace(root_table)
423         ? private_region_.GetWritableDataAddress(root_table)
424         : shared_region_.GetWritableDataAddress(root_table);
425     GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
426     for (uint32_t i = 0; i < number_of_roots; ++i) {
427       // This does not need a read barrier because this is called by GC.
428       mirror::Object* object = roots[i].Read<kWithoutReadBarrier>();
429       if (object == nullptr || object == Runtime::GetWeakClassSentinel()) {
430         // entry got deleted in a previous sweep.
431       } else if (object->IsString<kDefaultVerifyFlags>()) {
432         mirror::Object* new_object = visitor->IsMarked(object);
433         // We know the string is marked because it's a strongly-interned string that
434         // is always alive. The IsMarked implementation of the CMS collector returns
435         // null for newly allocated objects, but we know those haven't moved. Therefore,
436         // only update the entry if we get a different non-null string.
437         // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method
438         // out of the weak access/creation pause. b/32167580
439         if (new_object != nullptr && new_object != object) {
440           DCHECK(new_object->IsString());
441           roots[i] = GcRoot<mirror::Object>(new_object);
442         }
443       } else {
444         Runtime::ProcessWeakClass(
445             reinterpret_cast<GcRoot<mirror::Class>*>(&roots[i]),
446             visitor,
447             Runtime::GetWeakClassSentinel());
448       }
449     }
450   }
451   // Walk over inline caches to clear entries containing unloaded classes.
452   for (ProfilingInfo* info : profiling_infos_) {
453     for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
454       InlineCache* cache = &info->cache_[i];
455       for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
456         Runtime::ProcessWeakClass(&cache->classes_[j], visitor, nullptr);
457       }
458     }
459   }
460 }
461 
FreeCodeAndData(const void * code_ptr,bool free_debug_info)462 void JitCodeCache::FreeCodeAndData(const void* code_ptr, bool free_debug_info) {
463   if (IsInZygoteExecSpace(code_ptr)) {
464     // No need to free, this is shared memory.
465     return;
466   }
467   uintptr_t allocation = FromCodeToAllocation(code_ptr);
468   if (free_debug_info) {
469     // Remove compressed mini-debug info for the method.
470     // TODO: This is expensive, so we should always do it in the caller in bulk.
471     RemoveNativeDebugInfoForJit(ArrayRef<const void*>(&code_ptr, 1));
472   }
473   if (OatQuickMethodHeader::FromCodePointer(code_ptr)->IsOptimized()) {
474     private_region_.FreeData(GetRootTable(code_ptr));
475   }  // else this is a JNI stub without any data.
476 
477   private_region_.FreeCode(reinterpret_cast<uint8_t*>(allocation));
478 }
479 
FreeAllMethodHeaders(const std::unordered_set<OatQuickMethodHeader * > & method_headers)480 void JitCodeCache::FreeAllMethodHeaders(
481     const std::unordered_set<OatQuickMethodHeader*>& method_headers) {
482   // We need to remove entries in method_headers from CHA dependencies
483   // first since once we do FreeCode() below, the memory can be reused
484   // so it's possible for the same method_header to start representing
485   // different compile code.
486   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
487   {
488     MutexLock mu2(Thread::Current(), *Locks::cha_lock_);
489     Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()
490         ->RemoveDependentsWithMethodHeaders(method_headers);
491   }
492 
493   // Remove compressed mini-debug info for the methods.
494   std::vector<const void*> removed_symbols;
495   removed_symbols.reserve(method_headers.size());
496   for (const OatQuickMethodHeader* method_header : method_headers) {
497     removed_symbols.push_back(method_header->GetCode());
498   }
499   std::sort(removed_symbols.begin(), removed_symbols.end());
500   RemoveNativeDebugInfoForJit(ArrayRef<const void*>(removed_symbols));
501 
502   ScopedCodeCacheWrite scc(private_region_);
503   for (const OatQuickMethodHeader* method_header : method_headers) {
504     FreeCodeAndData(method_header->GetCode(), /*free_debug_info=*/ false);
505   }
506 }
507 
RemoveMethodsIn(Thread * self,const LinearAlloc & alloc)508 void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
509   ScopedTrace trace(__PRETTY_FUNCTION__);
510   // We use a set to first collect all method_headers whose code need to be
511   // removed. We need to free the underlying code after we remove CHA dependencies
512   // for entries in this set. And it's more efficient to iterate through
513   // the CHA dependency map just once with an unordered_set.
514   std::unordered_set<OatQuickMethodHeader*> method_headers;
515   {
516     MutexLock mu(self, *Locks::jit_lock_);
517     // We do not check if a code cache GC is in progress, as this method comes
518     // with the classlinker_classes_lock_ held, and suspending ourselves could
519     // lead to a deadlock.
520     {
521       for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) {
522         it->second.RemoveMethodsIn(alloc);
523         if (it->second.GetMethods().empty()) {
524           method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->second.GetCode()));
525           it = jni_stubs_map_.erase(it);
526         } else {
527           it->first.UpdateShorty(it->second.GetMethods().front());
528           ++it;
529         }
530       }
531       for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
532         if (alloc.ContainsUnsafe(it->second)) {
533           method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first));
534           it = method_code_map_.erase(it);
535         } else {
536           ++it;
537         }
538       }
539     }
540     for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
541       if (alloc.ContainsUnsafe(it->first)) {
542         // Note that the code has already been pushed to method_headers in the loop
543         // above and is going to be removed in FreeCode() below.
544         it = osr_code_map_.erase(it);
545       } else {
546         ++it;
547       }
548     }
549     for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
550       ProfilingInfo* info = *it;
551       if (alloc.ContainsUnsafe(info->GetMethod())) {
552         info->GetMethod()->SetProfilingInfo(nullptr);
553         private_region_.FreeWritableData(reinterpret_cast<uint8_t*>(info));
554         it = profiling_infos_.erase(it);
555       } else {
556         ++it;
557       }
558     }
559   }
560   FreeAllMethodHeaders(method_headers);
561 }
562 
IsWeakAccessEnabled(Thread * self) const563 bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const {
564   return kUseReadBarrier
565       ? self->GetWeakRefAccessEnabled()
566       : is_weak_access_enabled_.load(std::memory_order_seq_cst);
567 }
568 
WaitUntilInlineCacheAccessible(Thread * self)569 void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) {
570   if (IsWeakAccessEnabled(self)) {
571     return;
572   }
573   ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
574   MutexLock mu(self, *Locks::jit_lock_);
575   while (!IsWeakAccessEnabled(self)) {
576     inline_cache_cond_.Wait(self);
577   }
578 }
579 
BroadcastForInlineCacheAccess()580 void JitCodeCache::BroadcastForInlineCacheAccess() {
581   Thread* self = Thread::Current();
582   MutexLock mu(self, *Locks::jit_lock_);
583   inline_cache_cond_.Broadcast(self);
584 }
585 
AllowInlineCacheAccess()586 void JitCodeCache::AllowInlineCacheAccess() {
587   DCHECK(!kUseReadBarrier);
588   is_weak_access_enabled_.store(true, std::memory_order_seq_cst);
589   BroadcastForInlineCacheAccess();
590 }
591 
DisallowInlineCacheAccess()592 void JitCodeCache::DisallowInlineCacheAccess() {
593   DCHECK(!kUseReadBarrier);
594   is_weak_access_enabled_.store(false, std::memory_order_seq_cst);
595 }
596 
CopyInlineCacheInto(const InlineCache & ic,Handle<mirror::ObjectArray<mirror::Class>> array)597 void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic,
598                                        Handle<mirror::ObjectArray<mirror::Class>> array) {
599   WaitUntilInlineCacheAccessible(Thread::Current());
600   // Note that we don't need to lock `lock_` here, the compiler calling
601   // this method has already ensured the inline cache will not be deleted.
602   for (size_t in_cache = 0, in_array = 0;
603        in_cache < InlineCache::kIndividualCacheSize;
604        ++in_cache) {
605     mirror::Class* object = ic.classes_[in_cache].Read();
606     if (object != nullptr) {
607       array->Set(in_array++, object);
608     }
609   }
610 }
611 
ClearMethodCounter(ArtMethod * method,bool was_warm)612 static void ClearMethodCounter(ArtMethod* method, bool was_warm)
613     REQUIRES_SHARED(Locks::mutator_lock_) {
614   if (was_warm) {
615     method->SetPreviouslyWarm();
616   }
617   // We reset the counter to 1 so that the profile knows that the method was executed at least once.
618   // This is required for layout purposes.
619   // We also need to make sure we'll pass the warmup threshold again, so we set to 0 if
620   // the warmup threshold is 1.
621   uint16_t jit_warmup_threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
622   method->SetCounter(std::min(jit_warmup_threshold - 1, 1));
623 }
624 
WaitForPotentialCollectionToCompleteRunnable(Thread * self)625 void JitCodeCache::WaitForPotentialCollectionToCompleteRunnable(Thread* self) {
626   while (collection_in_progress_) {
627     Locks::jit_lock_->Unlock(self);
628     {
629       ScopedThreadSuspension sts(self, kSuspended);
630       MutexLock mu(self, *Locks::jit_lock_);
631       WaitForPotentialCollectionToComplete(self);
632     }
633     Locks::jit_lock_->Lock(self);
634   }
635 }
636 
Commit(Thread * self,JitMemoryRegion * region,ArtMethod * method,ArrayRef<const uint8_t> reserved_code,ArrayRef<const uint8_t> code,ArrayRef<const uint8_t> reserved_data,const std::vector<Handle<mirror::Object>> & roots,ArrayRef<const uint8_t> stack_map,bool osr,bool has_should_deoptimize_flag,const ArenaSet<ArtMethod * > & cha_single_implementation_list)637 bool JitCodeCache::Commit(Thread* self,
638                           JitMemoryRegion* region,
639                           ArtMethod* method,
640                           ArrayRef<const uint8_t> reserved_code,
641                           ArrayRef<const uint8_t> code,
642                           ArrayRef<const uint8_t> reserved_data,
643                           const std::vector<Handle<mirror::Object>>& roots,
644                           ArrayRef<const uint8_t> stack_map,
645                           bool osr,
646                           bool has_should_deoptimize_flag,
647                           const ArenaSet<ArtMethod*>& cha_single_implementation_list) {
648   DCHECK(!method->IsNative() || !osr);
649 
650   if (!method->IsNative()) {
651     // We need to do this before grabbing the lock_ because it needs to be able to see the string
652     // InternTable. Native methods do not have roots.
653     DCheckRootsAreValid(roots, IsSharedRegion(*region));
654   }
655 
656   const uint8_t* roots_data = reserved_data.data();
657   size_t root_table_size = ComputeRootTableSize(roots.size());
658   const uint8_t* stack_map_data = roots_data + root_table_size;
659 
660   MutexLock mu(self, *Locks::jit_lock_);
661   // We need to make sure that there will be no jit-gcs going on and wait for any ongoing one to
662   // finish.
663   WaitForPotentialCollectionToCompleteRunnable(self);
664   const uint8_t* code_ptr = region->CommitCode(
665       reserved_code, code, stack_map_data, has_should_deoptimize_flag);
666   if (code_ptr == nullptr) {
667     return false;
668   }
669   OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
670 
671   // Commit roots and stack maps before updating the entry point.
672   if (!region->CommitData(reserved_data, roots, stack_map)) {
673     return false;
674   }
675 
676   number_of_compilations_++;
677 
678   // We need to update the entry point in the runnable state for the instrumentation.
679   {
680     // The following needs to be guarded by cha_lock_ also. Otherwise it's possible that the
681     // compiled code is considered invalidated by some class linking, but below we still make the
682     // compiled code valid for the method.  Need cha_lock_ for checking all single-implementation
683     // flags and register dependencies.
684     MutexLock cha_mu(self, *Locks::cha_lock_);
685     bool single_impl_still_valid = true;
686     for (ArtMethod* single_impl : cha_single_implementation_list) {
687       if (!single_impl->HasSingleImplementation()) {
688         // Simply discard the compiled code. Clear the counter so that it may be recompiled later.
689         // Hopefully the class hierarchy will be more stable when compilation is retried.
690         single_impl_still_valid = false;
691         ClearMethodCounter(method, /*was_warm=*/ false);
692         break;
693       }
694     }
695 
696     // Discard the code if any single-implementation assumptions are now invalid.
697     if (UNLIKELY(!single_impl_still_valid)) {
698       VLOG(jit) << "JIT discarded jitted code due to invalid single-implementation assumptions.";
699       return false;
700     }
701     DCHECK(cha_single_implementation_list.empty() || !Runtime::Current()->IsJavaDebuggable())
702         << "Should not be using cha on debuggable apps/runs!";
703 
704     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
705     for (ArtMethod* single_impl : cha_single_implementation_list) {
706       class_linker->GetClassHierarchyAnalysis()->AddDependency(single_impl, method, method_header);
707     }
708 
709     if (UNLIKELY(method->IsNative())) {
710       auto it = jni_stubs_map_.find(JniStubKey(method));
711       DCHECK(it != jni_stubs_map_.end())
712           << "Entry inserted in NotifyCompilationOf() should be alive.";
713       JniStubData* data = &it->second;
714       DCHECK(ContainsElement(data->GetMethods(), method))
715           << "Entry inserted in NotifyCompilationOf() should contain this method.";
716       data->SetCode(code_ptr);
717       data->UpdateEntryPoints(method_header->GetEntryPoint());
718     } else {
719       if (method->IsPreCompiled() && IsSharedRegion(*region)) {
720         zygote_map_.Put(code_ptr, method);
721       } else {
722         method_code_map_.Put(code_ptr, method);
723       }
724       if (osr) {
725         number_of_osr_compilations_++;
726         osr_code_map_.Put(method, code_ptr);
727       } else if (NeedsClinitCheckBeforeCall(method) &&
728                  !method->GetDeclaringClass()->IsVisiblyInitialized()) {
729         // This situation currently only occurs in the jit-zygote mode.
730         DCHECK(!garbage_collect_code_);
731         DCHECK(method->IsPreCompiled());
732         // The shared region can easily be queried. For the private region, we
733         // use a side map.
734         if (!IsSharedRegion(*region)) {
735           saved_compiled_methods_map_.Put(method, code_ptr);
736         }
737       } else {
738         Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
739             method, method_header->GetEntryPoint());
740       }
741     }
742     if (collection_in_progress_) {
743       // We need to update the live bitmap if there is a GC to ensure it sees this new
744       // code.
745       GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
746     }
747     VLOG(jit)
748         << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
749         << ArtMethod::PrettyMethod(method) << "@" << method
750         << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
751         << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
752         << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
753         << reinterpret_cast<const void*>(method_header->GetEntryPoint() +
754                                          method_header->GetCodeSize());
755   }
756 
757   return true;
758 }
759 
CodeCacheSize()760 size_t JitCodeCache::CodeCacheSize() {
761   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
762   return CodeCacheSizeLocked();
763 }
764 
RemoveMethod(ArtMethod * method,bool release_memory)765 bool JitCodeCache::RemoveMethod(ArtMethod* method, bool release_memory) {
766   // This function is used only for testing and only with non-native methods.
767   CHECK(!method->IsNative());
768 
769   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
770 
771   bool osr = osr_code_map_.find(method) != osr_code_map_.end();
772   bool in_cache = RemoveMethodLocked(method, release_memory);
773 
774   if (!in_cache) {
775     return false;
776   }
777 
778   method->SetCounter(0);
779   Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
780       method, GetQuickToInterpreterBridge());
781   VLOG(jit)
782       << "JIT removed (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
783       << ArtMethod::PrettyMethod(method) << "@" << method
784       << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
785       << " dcache_size=" << PrettySize(DataCacheSizeLocked());
786   return true;
787 }
788 
RemoveMethodLocked(ArtMethod * method,bool release_memory)789 bool JitCodeCache::RemoveMethodLocked(ArtMethod* method, bool release_memory) {
790   if (LIKELY(!method->IsNative())) {
791     ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
792     if (info != nullptr) {
793       RemoveElement(profiling_infos_, info);
794     }
795     method->SetProfilingInfo(nullptr);
796   }
797 
798   bool in_cache = false;
799   ScopedCodeCacheWrite ccw(private_region_);
800   if (UNLIKELY(method->IsNative())) {
801     auto it = jni_stubs_map_.find(JniStubKey(method));
802     if (it != jni_stubs_map_.end() && it->second.RemoveMethod(method)) {
803       in_cache = true;
804       if (it->second.GetMethods().empty()) {
805         if (release_memory) {
806           FreeCodeAndData(it->second.GetCode());
807         }
808         jni_stubs_map_.erase(it);
809       } else {
810         it->first.UpdateShorty(it->second.GetMethods().front());
811       }
812     }
813   } else {
814     for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
815       if (it->second == method) {
816         in_cache = true;
817         if (release_memory) {
818           FreeCodeAndData(it->first);
819         }
820         it = method_code_map_.erase(it);
821       } else {
822         ++it;
823       }
824     }
825 
826     auto osr_it = osr_code_map_.find(method);
827     if (osr_it != osr_code_map_.end()) {
828       osr_code_map_.erase(osr_it);
829     }
830   }
831 
832   return in_cache;
833 }
834 
835 // This notifies the code cache that the given method has been redefined and that it should remove
836 // any cached information it has on the method. All threads must be suspended before calling this
837 // method. The compiled code for the method (if there is any) must not be in any threads call stack.
NotifyMethodRedefined(ArtMethod * method)838 void JitCodeCache::NotifyMethodRedefined(ArtMethod* method) {
839   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
840   RemoveMethodLocked(method, /* release_memory= */ true);
841 }
842 
843 // This invalidates old_method. Once this function returns one can no longer use old_method to
844 // execute code unless it is fixed up. This fixup will happen later in the process of installing a
845 // class redefinition.
846 // TODO We should add some info to ArtMethod to note that 'old_method' has been invalidated and
847 // shouldn't be used since it is no longer logically in the jit code cache.
848 // TODO We should add DCHECKS that validate that the JIT is paused when this method is entered.
MoveObsoleteMethod(ArtMethod * old_method,ArtMethod * new_method)849 void JitCodeCache::MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
850   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
851   if (old_method->IsNative()) {
852     // Update methods in jni_stubs_map_.
853     for (auto& entry : jni_stubs_map_) {
854       JniStubData& data = entry.second;
855       data.MoveObsoleteMethod(old_method, new_method);
856     }
857     return;
858   }
859   // Update ProfilingInfo to the new one and remove it from the old_method.
860   if (old_method->GetProfilingInfo(kRuntimePointerSize) != nullptr) {
861     DCHECK_EQ(old_method->GetProfilingInfo(kRuntimePointerSize)->GetMethod(), old_method);
862     ProfilingInfo* info = old_method->GetProfilingInfo(kRuntimePointerSize);
863     old_method->SetProfilingInfo(nullptr);
864     // Since the JIT should be paused and all threads suspended by the time this is called these
865     // checks should always pass.
866     DCHECK(!info->IsInUseByCompiler());
867     new_method->SetProfilingInfo(info);
868     // Get rid of the old saved entrypoint if it is there.
869     info->SetSavedEntryPoint(nullptr);
870     info->method_ = new_method;
871   }
872   // Update method_code_map_ to point to the new method.
873   for (auto& it : method_code_map_) {
874     if (it.second == old_method) {
875       it.second = new_method;
876     }
877   }
878   // Update osr_code_map_ to point to the new method.
879   auto code_map = osr_code_map_.find(old_method);
880   if (code_map != osr_code_map_.end()) {
881     osr_code_map_.Put(new_method, code_map->second);
882     osr_code_map_.erase(old_method);
883   }
884 }
885 
TransitionToDebuggable()886 void JitCodeCache::TransitionToDebuggable() {
887   // Check that none of our methods have an entrypoint in the zygote exec
888   // space (this should be taken care of by
889   // ClassLinker::UpdateEntryPointsClassVisitor.
890   {
891     MutexLock mu(Thread::Current(), *Locks::jit_lock_);
892     if (kIsDebugBuild) {
893       for (const auto& it : method_code_map_) {
894         ArtMethod* method = it.second;
895         DCHECK(!method->IsPreCompiled());
896         DCHECK(!IsInZygoteExecSpace(method->GetEntryPointFromQuickCompiledCode()));
897       }
898     }
899     // Not strictly necessary, but this map is useless now.
900     saved_compiled_methods_map_.clear();
901   }
902   if (kIsDebugBuild) {
903     for (const auto& entry : zygote_map_) {
904       ArtMethod* method = entry.method;
905       if (method != nullptr) {
906         DCHECK(!method->IsPreCompiled());
907         DCHECK(!IsInZygoteExecSpace(method->GetEntryPointFromQuickCompiledCode()));
908       }
909     }
910   }
911 }
912 
CodeCacheSizeLocked()913 size_t JitCodeCache::CodeCacheSizeLocked() {
914   return GetCurrentRegion()->GetUsedMemoryForCode();
915 }
916 
DataCacheSize()917 size_t JitCodeCache::DataCacheSize() {
918   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
919   return DataCacheSizeLocked();
920 }
921 
DataCacheSizeLocked()922 size_t JitCodeCache::DataCacheSizeLocked() {
923   return GetCurrentRegion()->GetUsedMemoryForData();
924 }
925 
Reserve(Thread * self,JitMemoryRegion * region,size_t code_size,size_t stack_map_size,size_t number_of_roots,ArtMethod * method,ArrayRef<const uint8_t> * reserved_code,ArrayRef<const uint8_t> * reserved_data)926 bool JitCodeCache::Reserve(Thread* self,
927                            JitMemoryRegion* region,
928                            size_t code_size,
929                            size_t stack_map_size,
930                            size_t number_of_roots,
931                            ArtMethod* method,
932                            /*out*/ArrayRef<const uint8_t>* reserved_code,
933                            /*out*/ArrayRef<const uint8_t>* reserved_data) {
934   code_size = OatQuickMethodHeader::InstructionAlignedSize() + code_size;
935   size_t data_size = RoundUp(ComputeRootTableSize(number_of_roots) + stack_map_size, sizeof(void*));
936 
937   const uint8_t* code;
938   const uint8_t* data;
939   // We might need to try the allocation twice (with GC in between to free up memory).
940   for (int i = 0; i < 2; i++) {
941     {
942       ScopedThreadSuspension sts(self, kSuspended);
943       MutexLock mu(self, *Locks::jit_lock_);
944       WaitForPotentialCollectionToComplete(self);
945       ScopedCodeCacheWrite ccw(*region);
946       code = region->AllocateCode(code_size);
947       data = region->AllocateData(data_size);
948     }
949     if (code == nullptr || data == nullptr) {
950       Free(self, region, code, data);
951       if (i == 0) {
952         GarbageCollectCache(self);
953         continue;  // Retry after GC.
954       } else {
955         return false;  // Fail.
956       }
957     }
958     break;  // Success.
959   }
960   *reserved_code = ArrayRef<const uint8_t>(code, code_size);
961   *reserved_data = ArrayRef<const uint8_t>(data, data_size);
962 
963   MutexLock mu(self, *Locks::jit_lock_);
964   histogram_code_memory_use_.AddValue(code_size);
965   if (code_size > kCodeSizeLogThreshold) {
966     LOG(INFO) << "JIT allocated "
967               << PrettySize(code_size)
968               << " for compiled code of "
969               << ArtMethod::PrettyMethod(method);
970   }
971   histogram_stack_map_memory_use_.AddValue(data_size);
972   if (data_size > kStackMapSizeLogThreshold) {
973     LOG(INFO) << "JIT allocated "
974               << PrettySize(data_size)
975               << " for stack maps of "
976               << ArtMethod::PrettyMethod(method);
977   }
978   return true;
979 }
980 
Free(Thread * self,JitMemoryRegion * region,const uint8_t * code,const uint8_t * data)981 void JitCodeCache::Free(Thread* self,
982                         JitMemoryRegion* region,
983                         const uint8_t* code,
984                         const uint8_t* data) {
985   MutexLock mu(self, *Locks::jit_lock_);
986   ScopedCodeCacheWrite ccw(*region);
987   if (code != nullptr) {
988     region->FreeCode(code);
989   }
990   if (data != nullptr) {
991     region->FreeData(data);
992   }
993 }
994 
995 class MarkCodeClosure final : public Closure {
996  public:
MarkCodeClosure(JitCodeCache * code_cache,CodeCacheBitmap * bitmap,Barrier * barrier)997   MarkCodeClosure(JitCodeCache* code_cache, CodeCacheBitmap* bitmap, Barrier* barrier)
998       : code_cache_(code_cache), bitmap_(bitmap), barrier_(barrier) {}
999 
Run(Thread * thread)1000   void Run(Thread* thread) override REQUIRES_SHARED(Locks::mutator_lock_) {
1001     ScopedTrace trace(__PRETTY_FUNCTION__);
1002     DCHECK(thread == Thread::Current() || thread->IsSuspended());
1003     StackVisitor::WalkStack(
1004         [&](const art::StackVisitor* stack_visitor) {
1005           const OatQuickMethodHeader* method_header =
1006               stack_visitor->GetCurrentOatQuickMethodHeader();
1007           if (method_header == nullptr) {
1008             return true;
1009           }
1010           const void* code = method_header->GetCode();
1011           if (code_cache_->ContainsPc(code) && !code_cache_->IsInZygoteExecSpace(code)) {
1012             // Use the atomic set version, as multiple threads are executing this code.
1013             bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
1014           }
1015           return true;
1016         },
1017         thread,
1018         /* context= */ nullptr,
1019         art::StackVisitor::StackWalkKind::kSkipInlinedFrames);
1020 
1021     if (kIsDebugBuild) {
1022       // The stack walking code queries the side instrumentation stack if it
1023       // sees an instrumentation exit pc, so the JIT code of methods in that stack
1024       // must have been seen. We sanity check this below.
1025       for (const auto& it : *thread->GetInstrumentationStack()) {
1026         // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
1027         // its stack frame, it is not the method owning return_pc_. We just pass null to
1028         // LookupMethodHeader: the method is only checked against in debug builds.
1029         OatQuickMethodHeader* method_header =
1030             code_cache_->LookupMethodHeader(it.second.return_pc_, /* method= */ nullptr);
1031         if (method_header != nullptr) {
1032           const void* code = method_header->GetCode();
1033           CHECK(bitmap_->Test(FromCodeToAllocation(code)));
1034         }
1035       }
1036     }
1037     barrier_->Pass(Thread::Current());
1038   }
1039 
1040  private:
1041   JitCodeCache* const code_cache_;
1042   CodeCacheBitmap* const bitmap_;
1043   Barrier* const barrier_;
1044 };
1045 
NotifyCollectionDone(Thread * self)1046 void JitCodeCache::NotifyCollectionDone(Thread* self) {
1047   collection_in_progress_ = false;
1048   lock_cond_.Broadcast(self);
1049 }
1050 
MarkCompiledCodeOnThreadStacks(Thread * self)1051 void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
1052   Barrier barrier(0);
1053   size_t threads_running_checkpoint = 0;
1054   MarkCodeClosure closure(this, GetLiveBitmap(), &barrier);
1055   threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
1056   // Now that we have run our checkpoint, move to a suspended state and wait
1057   // for other threads to run the checkpoint.
1058   ScopedThreadSuspension sts(self, kSuspended);
1059   if (threads_running_checkpoint != 0) {
1060     barrier.Increment(self, threads_running_checkpoint);
1061   }
1062 }
1063 
ShouldDoFullCollection()1064 bool JitCodeCache::ShouldDoFullCollection() {
1065   if (private_region_.GetCurrentCapacity() == private_region_.GetMaxCapacity()) {
1066     // Always do a full collection when the code cache is full.
1067     return true;
1068   } else if (private_region_.GetCurrentCapacity() < kReservedCapacity) {
1069     // Always do partial collection when the code cache size is below the reserved
1070     // capacity.
1071     return false;
1072   } else if (last_collection_increased_code_cache_) {
1073     // This time do a full collection.
1074     return true;
1075   } else {
1076     // This time do a partial collection.
1077     return false;
1078   }
1079 }
1080 
GarbageCollectCache(Thread * self)1081 void JitCodeCache::GarbageCollectCache(Thread* self) {
1082   ScopedTrace trace(__FUNCTION__);
1083   // Wait for an existing collection, or let everyone know we are starting one.
1084   {
1085     ScopedThreadSuspension sts(self, kSuspended);
1086     MutexLock mu(self, *Locks::jit_lock_);
1087     if (!garbage_collect_code_) {
1088       private_region_.IncreaseCodeCacheCapacity();
1089       return;
1090     } else if (WaitForPotentialCollectionToComplete(self)) {
1091       return;
1092     } else {
1093       number_of_collections_++;
1094       live_bitmap_.reset(CodeCacheBitmap::Create(
1095           "code-cache-bitmap",
1096           reinterpret_cast<uintptr_t>(private_region_.GetExecPages()->Begin()),
1097           reinterpret_cast<uintptr_t>(
1098               private_region_.GetExecPages()->Begin() + private_region_.GetCurrentCapacity() / 2)));
1099       collection_in_progress_ = true;
1100     }
1101   }
1102 
1103   TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit));
1104   {
1105     TimingLogger::ScopedTiming st("Code cache collection", &logger);
1106 
1107     bool do_full_collection = false;
1108     {
1109       MutexLock mu(self, *Locks::jit_lock_);
1110       do_full_collection = ShouldDoFullCollection();
1111     }
1112 
1113     VLOG(jit) << "Do "
1114               << (do_full_collection ? "full" : "partial")
1115               << " code cache collection, code="
1116               << PrettySize(CodeCacheSize())
1117               << ", data=" << PrettySize(DataCacheSize());
1118 
1119     DoCollection(self, /* collect_profiling_info= */ do_full_collection);
1120 
1121     VLOG(jit) << "After code cache collection, code="
1122               << PrettySize(CodeCacheSize())
1123               << ", data=" << PrettySize(DataCacheSize());
1124 
1125     {
1126       MutexLock mu(self, *Locks::jit_lock_);
1127 
1128       // Increase the code cache only when we do partial collections.
1129       // TODO: base this strategy on how full the code cache is?
1130       if (do_full_collection) {
1131         last_collection_increased_code_cache_ = false;
1132       } else {
1133         last_collection_increased_code_cache_ = true;
1134         private_region_.IncreaseCodeCacheCapacity();
1135       }
1136 
1137       bool next_collection_will_be_full = ShouldDoFullCollection();
1138 
1139       // Start polling the liveness of compiled code to prepare for the next full collection.
1140       if (next_collection_will_be_full) {
1141         if (Runtime::Current()->GetJITOptions()->CanCompileBaseline()) {
1142           for (ProfilingInfo* info : profiling_infos_) {
1143             info->SetBaselineHotnessCount(0);
1144           }
1145         } else {
1146           // Save the entry point of methods we have compiled, and update the entry
1147           // point of those methods to the interpreter. If the method is invoked, the
1148           // interpreter will update its entry point to the compiled code and call it.
1149           for (ProfilingInfo* info : profiling_infos_) {
1150             const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1151             if (!IsInZygoteDataSpace(info) && ContainsPc(entry_point)) {
1152               info->SetSavedEntryPoint(entry_point);
1153               // Don't call Instrumentation::UpdateMethodsCode(), as it can check the declaring
1154               // class of the method. We may be concurrently running a GC which makes accessing
1155               // the class unsafe. We know it is OK to bypass the instrumentation as we've just
1156               // checked that the current entry point is JIT compiled code.
1157               info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
1158             }
1159           }
1160         }
1161 
1162         // Change entry points of native methods back to the GenericJNI entrypoint.
1163         for (const auto& entry : jni_stubs_map_) {
1164           const JniStubData& data = entry.second;
1165           if (!data.IsCompiled() || IsInZygoteExecSpace(data.GetCode())) {
1166             continue;
1167           }
1168           // Make sure a single invocation of the GenericJNI trampoline tries to recompile.
1169           uint16_t new_counter = Runtime::Current()->GetJit()->HotMethodThreshold() - 1u;
1170           const OatQuickMethodHeader* method_header =
1171               OatQuickMethodHeader::FromCodePointer(data.GetCode());
1172           for (ArtMethod* method : data.GetMethods()) {
1173             if (method->GetEntryPointFromQuickCompiledCode() == method_header->GetEntryPoint()) {
1174               // Don't call Instrumentation::UpdateMethodsCode(), same as for normal methods above.
1175               method->SetCounter(new_counter);
1176               method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
1177             }
1178           }
1179         }
1180       }
1181       live_bitmap_.reset(nullptr);
1182       NotifyCollectionDone(self);
1183     }
1184   }
1185   Runtime::Current()->GetJit()->AddTimingLogger(logger);
1186 }
1187 
RemoveUnmarkedCode(Thread * self)1188 void JitCodeCache::RemoveUnmarkedCode(Thread* self) {
1189   ScopedTrace trace(__FUNCTION__);
1190   std::unordered_set<OatQuickMethodHeader*> method_headers;
1191   {
1192     MutexLock mu(self, *Locks::jit_lock_);
1193     // Iterate over all compiled code and remove entries that are not marked.
1194     for (auto it = jni_stubs_map_.begin(); it != jni_stubs_map_.end();) {
1195       JniStubData* data = &it->second;
1196       if (IsInZygoteExecSpace(data->GetCode()) ||
1197           !data->IsCompiled() ||
1198           GetLiveBitmap()->Test(FromCodeToAllocation(data->GetCode()))) {
1199         ++it;
1200       } else {
1201         method_headers.insert(OatQuickMethodHeader::FromCodePointer(data->GetCode()));
1202         it = jni_stubs_map_.erase(it);
1203       }
1204     }
1205     for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
1206       const void* code_ptr = it->first;
1207       uintptr_t allocation = FromCodeToAllocation(code_ptr);
1208       if (IsInZygoteExecSpace(code_ptr) || GetLiveBitmap()->Test(allocation)) {
1209         ++it;
1210       } else {
1211         OatQuickMethodHeader* header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1212         method_headers.insert(header);
1213         it = method_code_map_.erase(it);
1214       }
1215     }
1216   }
1217   FreeAllMethodHeaders(method_headers);
1218 }
1219 
GetGarbageCollectCode()1220 bool JitCodeCache::GetGarbageCollectCode() {
1221   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1222   return garbage_collect_code_;
1223 }
1224 
SetGarbageCollectCode(bool value)1225 void JitCodeCache::SetGarbageCollectCode(bool value) {
1226   Thread* self = Thread::Current();
1227   MutexLock mu(self, *Locks::jit_lock_);
1228   if (garbage_collect_code_ != value) {
1229     if (garbage_collect_code_) {
1230       // When dynamically disabling the garbage collection, we neee
1231       // to make sure that a potential current collection is finished, and also
1232       // clear the saved entry point in profiling infos to avoid dangling pointers.
1233       WaitForPotentialCollectionToComplete(self);
1234       for (ProfilingInfo* info : profiling_infos_) {
1235         info->SetSavedEntryPoint(nullptr);
1236       }
1237     }
1238     // Update the flag while holding the lock to ensure no thread will try to GC.
1239     garbage_collect_code_ = value;
1240   }
1241 }
1242 
DoCollection(Thread * self,bool collect_profiling_info)1243 void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
1244   ScopedTrace trace(__FUNCTION__);
1245   {
1246     MutexLock mu(self, *Locks::jit_lock_);
1247 
1248     if (Runtime::Current()->GetJITOptions()->CanCompileBaseline()) {
1249       // Update to interpreter the methods that have baseline entrypoints and whose baseline
1250       // hotness count is zero.
1251       // Note that these methods may be in thread stack or concurrently revived
1252       // between. That's OK, as the thread executing it will mark it.
1253       for (ProfilingInfo* info : profiling_infos_) {
1254         if (info->GetBaselineHotnessCount() == 0) {
1255           const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1256           if (ContainsPc(entry_point)) {
1257             OatQuickMethodHeader* method_header =
1258                 OatQuickMethodHeader::FromEntryPoint(entry_point);
1259             if (CodeInfo::IsBaseline(method_header->GetOptimizedCodeInfoPtr())) {
1260               info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
1261             }
1262           }
1263         }
1264       }
1265       // TODO: collect profiling info
1266       // TODO: collect optimized code?
1267     } else {
1268       if (collect_profiling_info) {
1269         // Clear the profiling info of methods that do not have compiled code as entrypoint.
1270         // Also remove the saved entry point from the ProfilingInfo objects.
1271         for (ProfilingInfo* info : profiling_infos_) {
1272           const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1273           if (!ContainsPc(ptr) && !info->IsInUseByCompiler() && !IsInZygoteDataSpace(info)) {
1274             info->GetMethod()->SetProfilingInfo(nullptr);
1275           }
1276 
1277           if (info->GetSavedEntryPoint() != nullptr) {
1278             info->SetSavedEntryPoint(nullptr);
1279             // We are going to move this method back to interpreter. Clear the counter now to
1280             // give it a chance to be hot again.
1281             ClearMethodCounter(info->GetMethod(), /*was_warm=*/ true);
1282           }
1283         }
1284       } else if (kIsDebugBuild) {
1285         // Sanity check that the profiling infos do not have a dangling entry point.
1286         for (ProfilingInfo* info : profiling_infos_) {
1287           DCHECK(!Runtime::Current()->IsZygote());
1288           const void* entry_point = info->GetSavedEntryPoint();
1289           DCHECK(entry_point == nullptr || IsInZygoteExecSpace(entry_point));
1290         }
1291       }
1292     }
1293 
1294     // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not
1295     // an entry point is either:
1296     // - an osr compiled code, that will be removed if not in a thread call stack.
1297     // - discarded compiled code, that will be removed if not in a thread call stack.
1298     for (const auto& entry : jni_stubs_map_) {
1299       const JniStubData& data = entry.second;
1300       const void* code_ptr = data.GetCode();
1301       if (IsInZygoteExecSpace(code_ptr)) {
1302         continue;
1303       }
1304       const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1305       for (ArtMethod* method : data.GetMethods()) {
1306         if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1307           GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1308           break;
1309         }
1310       }
1311     }
1312     for (const auto& it : method_code_map_) {
1313       ArtMethod* method = it.second;
1314       const void* code_ptr = it.first;
1315       if (IsInZygoteExecSpace(code_ptr)) {
1316         continue;
1317       }
1318       const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1319       if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1320         GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1321       }
1322     }
1323 
1324     // Empty osr method map, as osr compiled code will be deleted (except the ones
1325     // on thread stacks).
1326     osr_code_map_.clear();
1327   }
1328 
1329   // Run a checkpoint on all threads to mark the JIT compiled code they are running.
1330   MarkCompiledCodeOnThreadStacks(self);
1331 
1332   // At this point, mutator threads are still running, and entrypoints of methods can
1333   // change. We do know they cannot change to a code cache entry that is not marked,
1334   // therefore we can safely remove those entries.
1335   RemoveUnmarkedCode(self);
1336 
1337   if (collect_profiling_info) {
1338     MutexLock mu(self, *Locks::jit_lock_);
1339     // Free all profiling infos of methods not compiled nor being compiled.
1340     auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
1341       [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
1342         const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1343         // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope
1344         // that the compiled code would not get revived. As mutator threads run concurrently,
1345         // they may have revived the compiled code, and now we are in the situation where
1346         // a method has compiled code but no ProfilingInfo.
1347         // We make sure compiled methods have a ProfilingInfo object. It is needed for
1348         // code cache collection.
1349         if (ContainsPc(ptr) &&
1350             info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
1351           info->GetMethod()->SetProfilingInfo(info);
1352         } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) {
1353           // No need for this ProfilingInfo object anymore.
1354           private_region_.FreeWritableData(reinterpret_cast<uint8_t*>(info));
1355           return true;
1356         }
1357         return false;
1358       });
1359     profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
1360   }
1361 }
1362 
LookupMethodHeader(uintptr_t pc,ArtMethod * method)1363 OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
1364   static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
1365   if (kRuntimeISA == InstructionSet::kArm) {
1366     // On Thumb-2, the pc is offset by one.
1367     --pc;
1368   }
1369   if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
1370     return nullptr;
1371   }
1372 
1373   if (!kIsDebugBuild) {
1374     // Called with null `method` only from MarkCodeClosure::Run() in debug build.
1375     CHECK(method != nullptr);
1376   }
1377 
1378   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1379   OatQuickMethodHeader* method_header = nullptr;
1380   ArtMethod* found_method = nullptr;  // Only for DCHECK(), not for JNI stubs.
1381   if (method != nullptr && UNLIKELY(method->IsNative())) {
1382     auto it = jni_stubs_map_.find(JniStubKey(method));
1383     if (it == jni_stubs_map_.end() || !ContainsElement(it->second.GetMethods(), method)) {
1384       return nullptr;
1385     }
1386     const void* code_ptr = it->second.GetCode();
1387     method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1388     if (!method_header->Contains(pc)) {
1389       return nullptr;
1390     }
1391   } else {
1392     if (shared_region_.IsInExecSpace(reinterpret_cast<const void*>(pc))) {
1393       const void* code_ptr = zygote_map_.GetCodeFor(method, pc);
1394       if (code_ptr != nullptr) {
1395         return OatQuickMethodHeader::FromCodePointer(code_ptr);
1396       }
1397     }
1398     auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
1399     if (it != method_code_map_.begin()) {
1400       --it;
1401       const void* code_ptr = it->first;
1402       if (OatQuickMethodHeader::FromCodePointer(code_ptr)->Contains(pc)) {
1403         method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1404         found_method = it->second;
1405       }
1406     }
1407     if (method_header == nullptr && method == nullptr) {
1408       // Scan all compiled JNI stubs as well. This slow search is used only
1409       // for checks in debug build, for release builds the `method` is not null.
1410       for (auto&& entry : jni_stubs_map_) {
1411         const JniStubData& data = entry.second;
1412         if (data.IsCompiled() &&
1413             OatQuickMethodHeader::FromCodePointer(data.GetCode())->Contains(pc)) {
1414           method_header = OatQuickMethodHeader::FromCodePointer(data.GetCode());
1415         }
1416       }
1417     }
1418     if (method_header == nullptr) {
1419       return nullptr;
1420     }
1421   }
1422 
1423   if (kIsDebugBuild && method != nullptr && !method->IsNative()) {
1424     DCHECK_EQ(found_method, method)
1425         << ArtMethod::PrettyMethod(method) << " "
1426         << ArtMethod::PrettyMethod(found_method) << " "
1427         << std::hex << pc;
1428   }
1429   return method_header;
1430 }
1431 
LookupOsrMethodHeader(ArtMethod * method)1432 OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
1433   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1434   auto it = osr_code_map_.find(method);
1435   if (it == osr_code_map_.end()) {
1436     return nullptr;
1437   }
1438   return OatQuickMethodHeader::FromCodePointer(it->second);
1439 }
1440 
AddProfilingInfo(Thread * self,ArtMethod * method,const std::vector<uint32_t> & entries,bool retry_allocation)1441 ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
1442                                               ArtMethod* method,
1443                                               const std::vector<uint32_t>& entries,
1444                                               bool retry_allocation)
1445     // No thread safety analysis as we are using TryLock/Unlock explicitly.
1446     NO_THREAD_SAFETY_ANALYSIS {
1447   DCHECK(CanAllocateProfilingInfo());
1448   ProfilingInfo* info = nullptr;
1449   if (!retry_allocation) {
1450     // If we are allocating for the interpreter, just try to lock, to avoid
1451     // lock contention with the JIT.
1452     if (Locks::jit_lock_->ExclusiveTryLock(self)) {
1453       info = AddProfilingInfoInternal(self, method, entries);
1454       Locks::jit_lock_->ExclusiveUnlock(self);
1455     }
1456   } else {
1457     {
1458       MutexLock mu(self, *Locks::jit_lock_);
1459       info = AddProfilingInfoInternal(self, method, entries);
1460     }
1461 
1462     if (info == nullptr) {
1463       GarbageCollectCache(self);
1464       MutexLock mu(self, *Locks::jit_lock_);
1465       info = AddProfilingInfoInternal(self, method, entries);
1466     }
1467   }
1468   return info;
1469 }
1470 
AddProfilingInfoInternal(Thread * self ATTRIBUTE_UNUSED,ArtMethod * method,const std::vector<uint32_t> & entries)1471 ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
1472                                                       ArtMethod* method,
1473                                                       const std::vector<uint32_t>& entries) {
1474   size_t profile_info_size = RoundUp(
1475       sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
1476       sizeof(void*));
1477 
1478   // Check whether some other thread has concurrently created it.
1479   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1480   if (info != nullptr) {
1481     return info;
1482   }
1483 
1484   const uint8_t* data = private_region_.AllocateData(profile_info_size);
1485   if (data == nullptr) {
1486     return nullptr;
1487   }
1488   uint8_t* writable_data = private_region_.GetWritableDataAddress(data);
1489   info = new (writable_data) ProfilingInfo(method, entries);
1490 
1491   // Make sure other threads see the data in the profiling info object before the
1492   // store in the ArtMethod's ProfilingInfo pointer.
1493   std::atomic_thread_fence(std::memory_order_release);
1494 
1495   method->SetProfilingInfo(info);
1496   profiling_infos_.push_back(info);
1497   histogram_profiling_info_memory_use_.AddValue(profile_info_size);
1498   return info;
1499 }
1500 
MoreCore(const void * mspace,intptr_t increment)1501 void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) {
1502   return shared_region_.OwnsSpace(mspace)
1503       ? shared_region_.MoreCore(mspace, increment)
1504       : private_region_.MoreCore(mspace, increment);
1505 }
1506 
GetProfiledMethods(const std::set<std::string> & dex_base_locations,std::vector<ProfileMethodInfo> & methods)1507 void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations,
1508                                       std::vector<ProfileMethodInfo>& methods) {
1509   Thread* self = Thread::Current();
1510   WaitUntilInlineCacheAccessible(self);
1511   MutexLock mu(self, *Locks::jit_lock_);
1512   ScopedTrace trace(__FUNCTION__);
1513   uint16_t jit_compile_threshold = Runtime::Current()->GetJITOptions()->GetCompileThreshold();
1514   for (const ProfilingInfo* info : profiling_infos_) {
1515     ArtMethod* method = info->GetMethod();
1516     const DexFile* dex_file = method->GetDexFile();
1517     const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
1518     if (!ContainsElement(dex_base_locations, base_location)) {
1519       // Skip dex files which are not profiled.
1520       continue;
1521     }
1522     std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
1523 
1524     // If the method didn't reach the compilation threshold don't save the inline caches.
1525     // They might be incomplete and cause unnecessary deoptimizations.
1526     // If the inline cache is empty the compiler will generate a regular invoke virtual/interface.
1527     if (method->GetCounter() < jit_compile_threshold) {
1528       methods.emplace_back(/*ProfileMethodInfo*/
1529           MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches);
1530       continue;
1531     }
1532 
1533     for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
1534       std::vector<TypeReference> profile_classes;
1535       const InlineCache& cache = info->cache_[i];
1536       ArtMethod* caller = info->GetMethod();
1537       bool is_missing_types = false;
1538       for (size_t k = 0; k < InlineCache::kIndividualCacheSize; k++) {
1539         mirror::Class* cls = cache.classes_[k].Read();
1540         if (cls == nullptr) {
1541           break;
1542         }
1543 
1544         // Check if the receiver is in the boot class path or if it's in the
1545         // same class loader as the caller. If not, skip it, as there is not
1546         // much we can do during AOT.
1547         if (!cls->IsBootStrapClassLoaded() &&
1548             caller->GetClassLoader() != cls->GetClassLoader()) {
1549           is_missing_types = true;
1550           continue;
1551         }
1552 
1553         const DexFile* class_dex_file = nullptr;
1554         dex::TypeIndex type_index;
1555 
1556         if (cls->GetDexCache() == nullptr) {
1557           DCHECK(cls->IsArrayClass()) << cls->PrettyClass();
1558           // Make a best effort to find the type index in the method's dex file.
1559           // We could search all open dex files but that might turn expensive
1560           // and probably not worth it.
1561           class_dex_file = dex_file;
1562           type_index = cls->FindTypeIndexInOtherDexFile(*dex_file);
1563         } else {
1564           class_dex_file = &(cls->GetDexFile());
1565           type_index = cls->GetDexTypeIndex();
1566         }
1567         if (!type_index.IsValid()) {
1568           // Could be a proxy class or an array for which we couldn't find the type index.
1569           is_missing_types = true;
1570           continue;
1571         }
1572         if (ContainsElement(dex_base_locations,
1573                             DexFileLoader::GetBaseLocation(class_dex_file->GetLocation()))) {
1574           // Only consider classes from the same apk (including multidex).
1575           profile_classes.emplace_back(/*ProfileMethodInfo::ProfileClassReference*/
1576               class_dex_file, type_index);
1577         } else {
1578           is_missing_types = true;
1579         }
1580       }
1581       if (!profile_classes.empty()) {
1582         inline_caches.emplace_back(/*ProfileMethodInfo::ProfileInlineCache*/
1583             cache.dex_pc_, is_missing_types, profile_classes);
1584       }
1585     }
1586     methods.emplace_back(/*ProfileMethodInfo*/
1587         MethodReference(dex_file, method->GetDexMethodIndex()), inline_caches);
1588   }
1589 }
1590 
IsOsrCompiled(ArtMethod * method)1591 bool JitCodeCache::IsOsrCompiled(ArtMethod* method) {
1592   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1593   return osr_code_map_.find(method) != osr_code_map_.end();
1594 }
1595 
NotifyCompilationOf(ArtMethod * method,Thread * self,bool osr,bool prejit,bool baseline,JitMemoryRegion * region)1596 bool JitCodeCache::NotifyCompilationOf(ArtMethod* method,
1597                                        Thread* self,
1598                                        bool osr,
1599                                        bool prejit,
1600                                        bool baseline,
1601                                        JitMemoryRegion* region) {
1602   const void* existing_entry_point = method->GetEntryPointFromQuickCompiledCode();
1603   if (!osr && ContainsPc(existing_entry_point)) {
1604     OatQuickMethodHeader* method_header =
1605         OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
1606     if (CodeInfo::IsBaseline(method_header->GetOptimizedCodeInfoPtr()) == baseline) {
1607       VLOG(jit) << "Not compiling "
1608                 << method->PrettyMethod()
1609                 << " because it has already been compiled"
1610                 << " baseline=" << std::boolalpha << baseline;
1611       return false;
1612     }
1613   }
1614 
1615   if (NeedsClinitCheckBeforeCall(method) && !prejit) {
1616     // We do not need a synchronization barrier for checking the visibly initialized status
1617     // or checking the initialized status just for requesting visible initialization.
1618     ClassStatus status = method->GetDeclaringClass()
1619         ->GetStatus<kDefaultVerifyFlags, /*kWithSynchronizationBarrier=*/ false>();
1620     if (status != ClassStatus::kVisiblyInitialized) {
1621       // Unless we're pre-jitting, we currently don't save the JIT compiled code if we cannot
1622       // update the entrypoint due to needing an initialization check.
1623       if (status == ClassStatus::kInitialized) {
1624         // Request visible initialization but do not block to allow compiling other methods.
1625         // Hopefully, this will complete by the time the method becomes hot again.
1626         Runtime::Current()->GetClassLinker()->MakeInitializedClassesVisiblyInitialized(
1627             self, /*wait=*/ false);
1628       }
1629       VLOG(jit) << "Not compiling "
1630                 << method->PrettyMethod()
1631                 << " because it has the resolution stub";
1632       // Give it a new chance to be hot.
1633       ClearMethodCounter(method, /*was_warm=*/ false);
1634       return false;
1635     }
1636   }
1637 
1638   if (osr) {
1639     MutexLock mu(self, *Locks::jit_lock_);
1640     if (osr_code_map_.find(method) != osr_code_map_.end()) {
1641       return false;
1642     }
1643   }
1644 
1645   if (UNLIKELY(method->IsNative())) {
1646     MutexLock mu(self, *Locks::jit_lock_);
1647     JniStubKey key(method);
1648     auto it = jni_stubs_map_.find(key);
1649     bool new_compilation = false;
1650     if (it == jni_stubs_map_.end()) {
1651       // Create a new entry to mark the stub as being compiled.
1652       it = jni_stubs_map_.Put(key, JniStubData{});
1653       new_compilation = true;
1654     }
1655     JniStubData* data = &it->second;
1656     data->AddMethod(method);
1657     if (data->IsCompiled()) {
1658       OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(data->GetCode());
1659       const void* entrypoint = method_header->GetEntryPoint();
1660       // Update also entrypoints of other methods held by the JniStubData.
1661       // We could simply update the entrypoint of `method` but if the last JIT GC has
1662       // changed these entrypoints to GenericJNI in preparation for a full GC, we may
1663       // as well change them back as this stub shall not be collected anyway and this
1664       // can avoid a few expensive GenericJNI calls.
1665       data->UpdateEntryPoints(entrypoint);
1666       if (collection_in_progress_) {
1667         if (!IsInZygoteExecSpace(data->GetCode())) {
1668           GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(data->GetCode()));
1669         }
1670       }
1671     }
1672     return new_compilation;
1673   } else {
1674     ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1675     if (CanAllocateProfilingInfo() && baseline && info == nullptr) {
1676       // We can retry allocation here as we're the JIT thread.
1677       if (ProfilingInfo::Create(self, method, /* retry_allocation= */ true)) {
1678         info = method->GetProfilingInfo(kRuntimePointerSize);
1679       }
1680     }
1681     if (info == nullptr) {
1682       // When prejitting, we don't allocate a profiling info.
1683       if (!prejit && !IsSharedRegion(*region)) {
1684         VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled";
1685         // Because the counter is not atomic, there are some rare cases where we may not hit the
1686         // threshold for creating the ProfilingInfo. Reset the counter now to "correct" this.
1687         ClearMethodCounter(method, /*was_warm=*/ false);
1688         return false;
1689       }
1690     } else {
1691       MutexLock mu(self, *Locks::jit_lock_);
1692       if (info->IsMethodBeingCompiled(osr)) {
1693         return false;
1694       }
1695       info->SetIsMethodBeingCompiled(true, osr);
1696     }
1697     return true;
1698   }
1699 }
1700 
NotifyCompilerUse(ArtMethod * method,Thread * self)1701 ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) {
1702   MutexLock mu(self, *Locks::jit_lock_);
1703   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1704   if (info != nullptr) {
1705     if (!info->IncrementInlineUse()) {
1706       // Overflow of inlining uses, just bail.
1707       return nullptr;
1708     }
1709   }
1710   return info;
1711 }
1712 
DoneCompilerUse(ArtMethod * method,Thread * self)1713 void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) {
1714   MutexLock mu(self, *Locks::jit_lock_);
1715   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1716   DCHECK(info != nullptr);
1717   info->DecrementInlineUse();
1718 }
1719 
DoneCompiling(ArtMethod * method,Thread * self,bool osr)1720 void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self, bool osr) {
1721   DCHECK_EQ(Thread::Current(), self);
1722   MutexLock mu(self, *Locks::jit_lock_);
1723   if (UNLIKELY(method->IsNative())) {
1724     auto it = jni_stubs_map_.find(JniStubKey(method));
1725     DCHECK(it != jni_stubs_map_.end());
1726     JniStubData* data = &it->second;
1727     DCHECK(ContainsElement(data->GetMethods(), method));
1728     if (UNLIKELY(!data->IsCompiled())) {
1729       // Failed to compile; the JNI compiler never fails, but the cache may be full.
1730       jni_stubs_map_.erase(it);  // Remove the entry added in NotifyCompilationOf().
1731     }  // else Commit() updated entrypoints of all methods in the JniStubData.
1732   } else {
1733     ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1734     if (info != nullptr) {
1735       DCHECK(info->IsMethodBeingCompiled(osr));
1736       info->SetIsMethodBeingCompiled(false, osr);
1737     }
1738   }
1739 }
1740 
InvalidateAllCompiledCode()1741 void JitCodeCache::InvalidateAllCompiledCode() {
1742   art::MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1743   size_t cnt = profiling_infos_.size();
1744   size_t osr_size = osr_code_map_.size();
1745   for (ProfilingInfo* pi : profiling_infos_) {
1746     // NB Due to OSR we might run this on some methods multiple times but this should be fine.
1747     ArtMethod* meth = pi->GetMethod();
1748     pi->SetSavedEntryPoint(nullptr);
1749     // We had a ProfilingInfo so we must be warm.
1750     ClearMethodCounter(meth, /*was_warm=*/true);
1751     ClassLinker* linker = Runtime::Current()->GetClassLinker();
1752     if (meth->IsObsolete()) {
1753       linker->SetEntryPointsForObsoleteMethod(meth);
1754     } else {
1755       linker->SetEntryPointsToInterpreter(meth);
1756     }
1757   }
1758   osr_code_map_.clear();
1759   VLOG(jit) << "Invalidated the compiled code of " << (cnt - osr_size) << " methods and "
1760             << osr_size << " OSRs.";
1761 }
1762 
InvalidateCompiledCodeFor(ArtMethod * method,const OatQuickMethodHeader * header)1763 void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
1764                                              const OatQuickMethodHeader* header) {
1765   DCHECK(!method->IsNative());
1766   ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
1767   const void* method_entrypoint = method->GetEntryPointFromQuickCompiledCode();
1768   if ((profiling_info != nullptr) &&
1769       (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
1770     // When instrumentation is set, the actual entrypoint is the one in the profiling info.
1771     method_entrypoint = profiling_info->GetSavedEntryPoint();
1772     // Prevent future uses of the compiled code.
1773     profiling_info->SetSavedEntryPoint(nullptr);
1774   }
1775 
1776   // Clear the method counter if we are running jitted code since we might want to jit this again in
1777   // the future.
1778   if (method_entrypoint == header->GetEntryPoint()) {
1779     // The entrypoint is the one to invalidate, so we just update it to the interpreter entry point
1780     // and clear the counter to get the method Jitted again.
1781     Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1782         method, GetQuickToInterpreterBridge());
1783     ClearMethodCounter(method, /*was_warm=*/ profiling_info != nullptr);
1784   } else {
1785     MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1786     auto it = osr_code_map_.find(method);
1787     if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
1788       // Remove the OSR method, to avoid using it again.
1789       osr_code_map_.erase(it);
1790     }
1791   }
1792 
1793   // In case the method was pre-compiled, clear that information so we
1794   // can recompile it ourselves.
1795   if (method->IsPreCompiled()) {
1796     method->ClearPreCompiled();
1797   }
1798 }
1799 
Dump(std::ostream & os)1800 void JitCodeCache::Dump(std::ostream& os) {
1801   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1802   os << "Current JIT code cache size (used / resident): "
1803      << GetCurrentRegion()->GetUsedMemoryForCode() / KB << "KB / "
1804      << GetCurrentRegion()->GetResidentMemoryForCode() / KB << "KB\n"
1805      << "Current JIT data cache size (used / resident): "
1806      << GetCurrentRegion()->GetUsedMemoryForData() / KB << "KB / "
1807      << GetCurrentRegion()->GetResidentMemoryForData() / KB << "KB\n";
1808   if (!Runtime::Current()->IsZygote()) {
1809     os << "Zygote JIT code cache size (at point of fork): "
1810        << shared_region_.GetUsedMemoryForCode() / KB << "KB / "
1811        << shared_region_.GetResidentMemoryForCode() / KB << "KB\n"
1812        << "Zygote JIT data cache size (at point of fork): "
1813        << shared_region_.GetUsedMemoryForData() / KB << "KB / "
1814        << shared_region_.GetResidentMemoryForData() / KB << "KB\n";
1815   }
1816   os << "Current JIT mini-debug-info size: " << PrettySize(GetJitMiniDebugInfoMemUsage()) << "\n"
1817      << "Current JIT capacity: " << PrettySize(GetCurrentRegion()->GetCurrentCapacity()) << "\n"
1818      << "Current number of JIT JNI stub entries: " << jni_stubs_map_.size() << "\n"
1819      << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n"
1820      << "Total number of JIT compilations: " << number_of_compilations_ << "\n"
1821      << "Total number of JIT compilations for on stack replacement: "
1822         << number_of_osr_compilations_ << "\n"
1823      << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl;
1824   histogram_stack_map_memory_use_.PrintMemoryUse(os);
1825   histogram_code_memory_use_.PrintMemoryUse(os);
1826   histogram_profiling_info_memory_use_.PrintMemoryUse(os);
1827 }
1828 
PostForkChildAction(bool is_system_server,bool is_zygote)1829 void JitCodeCache::PostForkChildAction(bool is_system_server, bool is_zygote) {
1830   Thread* self = Thread::Current();
1831 
1832   // Remove potential tasks that have been inherited from the zygote.
1833   // We do this now and not in Jit::PostForkChildAction, as system server calls
1834   // JitCodeCache::PostForkChildAction first, and then does some code loading
1835   // that may result in new JIT tasks that we want to keep.
1836   ThreadPool* pool = Runtime::Current()->GetJit()->GetThreadPool();
1837   if (pool != nullptr) {
1838     pool->RemoveAllTasks(self);
1839   }
1840 
1841   MutexLock mu(self, *Locks::jit_lock_);
1842 
1843   // Reset potential writable MemMaps inherited from the zygote. We never want
1844   // to write to them.
1845   shared_region_.ResetWritableMappings();
1846 
1847   if (is_zygote || Runtime::Current()->IsSafeMode()) {
1848     // Don't create a private region for a child zygote. Regions are usually map shared
1849     // (to satisfy dual-view), and we don't want children of a child zygote to inherit it.
1850     return;
1851   }
1852 
1853   // Reset all statistics to be specific to this process.
1854   number_of_compilations_ = 0;
1855   number_of_osr_compilations_ = 0;
1856   number_of_collections_ = 0;
1857   histogram_stack_map_memory_use_.Reset();
1858   histogram_code_memory_use_.Reset();
1859   histogram_profiling_info_memory_use_.Reset();
1860 
1861   size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity();
1862   size_t max_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheMaxCapacity();
1863   std::string error_msg;
1864   if (!private_region_.Initialize(initial_capacity,
1865                                   max_capacity,
1866                                   /* rwx_memory_allowed= */ !is_system_server,
1867                                   is_zygote,
1868                                   &error_msg)) {
1869     LOG(WARNING) << "Could not create private region after zygote fork: " << error_msg;
1870   }
1871 }
1872 
GetCurrentRegion()1873 JitMemoryRegion* JitCodeCache::GetCurrentRegion() {
1874   return Runtime::Current()->IsZygote() ? &shared_region_ : &private_region_;
1875 }
1876 
Initialize(uint32_t number_of_methods)1877 void ZygoteMap::Initialize(uint32_t number_of_methods) {
1878   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
1879   // Allocate for 40-80% capacity. This will offer OK lookup times, and termination
1880   // cases.
1881   size_t capacity = RoundUpToPowerOfTwo(number_of_methods * 100 / 80);
1882   const uint8_t* memory = region_->AllocateData(
1883       capacity * sizeof(Entry) + sizeof(ZygoteCompilationState));
1884   if (memory == nullptr) {
1885     LOG(WARNING) << "Could not allocate data for the zygote map";
1886     return;
1887   }
1888   const Entry* data = reinterpret_cast<const Entry*>(memory);
1889   region_->FillData(data, capacity, Entry { nullptr, nullptr });
1890   map_ = ArrayRef(data, capacity);
1891   compilation_state_ = reinterpret_cast<const ZygoteCompilationState*>(
1892       memory + capacity * sizeof(Entry));
1893   region_->WriteData(compilation_state_, ZygoteCompilationState::kInProgress);
1894 }
1895 
GetCodeFor(ArtMethod * method,uintptr_t pc) const1896 const void* ZygoteMap::GetCodeFor(ArtMethod* method, uintptr_t pc) const {
1897   if (map_.empty()) {
1898     return nullptr;
1899   }
1900 
1901   if (method == nullptr) {
1902     // Do a linear search. This should only be used in debug builds.
1903     CHECK(kIsDebugBuild);
1904     for (const Entry& entry : map_) {
1905       const void* code_ptr = entry.code_ptr;
1906       if (code_ptr != nullptr) {
1907         OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1908         if (method_header->Contains(pc)) {
1909           return code_ptr;
1910         }
1911       }
1912     }
1913     return nullptr;
1914   }
1915 
1916   std::hash<ArtMethod*> hf;
1917   size_t index = hf(method) & (map_.size() - 1u);
1918   size_t original_index = index;
1919   // Loop over the array: we know this loop terminates as we will either
1920   // encounter the given method, or a null entry. Both terminate the loop.
1921   // Note that the zygote may concurrently write new entries to the map. That's OK as the
1922   // map is never resized.
1923   while (true) {
1924     const Entry& entry = map_[index];
1925     if (entry.method == nullptr) {
1926       // Not compiled yet.
1927       return nullptr;
1928     }
1929     if (entry.method == method) {
1930       if (entry.code_ptr == nullptr) {
1931         // This is a race with the zygote which wrote the method, but hasn't written the
1932         // code. Just bail and wait for the next time we need the method.
1933         return nullptr;
1934       }
1935       if (pc != 0 && !OatQuickMethodHeader::FromCodePointer(entry.code_ptr)->Contains(pc)) {
1936         return nullptr;
1937       }
1938       return entry.code_ptr;
1939     }
1940     index = (index + 1) & (map_.size() - 1);
1941     DCHECK_NE(original_index, index);
1942   }
1943 }
1944 
Put(const void * code,ArtMethod * method)1945 void ZygoteMap::Put(const void* code, ArtMethod* method) {
1946   if (map_.empty()) {
1947     return;
1948   }
1949   CHECK(Runtime::Current()->IsZygote());
1950   std::hash<ArtMethod*> hf;
1951   size_t index = hf(method) & (map_.size() - 1);
1952   size_t original_index = index;
1953   // Because the size of the map is bigger than the number of methods that will
1954   // be added, we are guaranteed to find a free slot in the array, and
1955   // therefore for this loop to terminate.
1956   while (true) {
1957     const Entry* entry = &map_[index];
1958     if (entry->method == nullptr) {
1959       // Note that readers can read this memory concurrently, but that's OK as
1960       // we are writing pointers.
1961       region_->WriteData(entry, Entry { method, code });
1962       break;
1963     }
1964     index = (index + 1) & (map_.size() - 1);
1965     DCHECK_NE(original_index, index);
1966   }
1967   DCHECK_EQ(GetCodeFor(method), code);
1968 }
1969 
1970 }  // namespace jit
1971 }  // namespace art
1972