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