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