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 "art_method-inl.h"
22 #include "base/enums.h"
23 #include "base/stl_util.h"
24 #include "base/systrace.h"
25 #include "base/time_utils.h"
26 #include "cha.h"
27 #include "debugger_interface.h"
28 #include "entrypoints/runtime_asm_entrypoints.h"
29 #include "gc/accounting/bitmap-inl.h"
30 #include "gc/scoped_gc_critical_section.h"
31 #include "jit/jit.h"
32 #include "jit/profiling_info.h"
33 #include "linear_alloc.h"
34 #include "mem_map.h"
35 #include "oat_file-inl.h"
36 #include "scoped_thread_state_change-inl.h"
37 #include "thread_list.h"
38 
39 namespace art {
40 namespace jit {
41 
42 static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC;
43 static constexpr int kProtData = PROT_READ | PROT_WRITE;
44 static constexpr int kProtCode = PROT_READ | PROT_EXEC;
45 
46 static constexpr size_t kCodeSizeLogThreshold = 50 * KB;
47 static constexpr size_t kStackMapSizeLogThreshold = 50 * KB;
48 
49 #define CHECKED_MPROTECT(memory, size, prot)                \
50   do {                                                      \
51     int rc = mprotect(memory, size, prot);                  \
52     if (UNLIKELY(rc != 0)) {                                \
53       errno = rc;                                           \
54       PLOG(FATAL) << "Failed to mprotect jit code cache";   \
55     }                                                       \
56   } while (false)                                           \
57 
Create(size_t initial_capacity,size_t max_capacity,bool generate_debug_info,std::string * error_msg)58 JitCodeCache* JitCodeCache::Create(size_t initial_capacity,
59                                    size_t max_capacity,
60                                    bool generate_debug_info,
61                                    std::string* error_msg) {
62   ScopedTrace trace(__PRETTY_FUNCTION__);
63   CHECK_GE(max_capacity, initial_capacity);
64 
65   // Generating debug information is mostly for using the 'perf' tool, which does
66   // not work with ashmem.
67   bool use_ashmem = !generate_debug_info;
68   // With 'perf', we want a 1-1 mapping between an address and a method.
69   bool garbage_collect_code = !generate_debug_info;
70 
71   // We need to have 32 bit offsets from method headers in code cache which point to things
72   // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
73   // Ensure we're below 1 GB to be safe.
74   if (max_capacity > 1 * GB) {
75     std::ostringstream oss;
76     oss << "Maxium code cache capacity is limited to 1 GB, "
77         << PrettySize(max_capacity) << " is too big";
78     *error_msg = oss.str();
79     return nullptr;
80   }
81 
82   std::string error_str;
83   // Map name specific for android_os_Debug.cpp accounting.
84   // Map in low 4gb to simplify accessing root tables for x86_64.
85   // We could do PC-relative addressing to avoid this problem, but that
86   // would require reserving code and data area before submitting, which
87   // means more windows for the code memory to be RWX.
88   MemMap* data_map = MemMap::MapAnonymous(
89       "data-code-cache", nullptr,
90       max_capacity,
91       kProtAll,
92       /* low_4gb */ true,
93       /* reuse */ false,
94       &error_str,
95       use_ashmem);
96   if (data_map == nullptr) {
97     std::ostringstream oss;
98     oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
99     *error_msg = oss.str();
100     return nullptr;
101   }
102 
103   // Align both capacities to page size, as that's the unit mspaces use.
104   initial_capacity = RoundDown(initial_capacity, 2 * kPageSize);
105   max_capacity = RoundDown(max_capacity, 2 * kPageSize);
106 
107   // Data cache is 1 / 2 of the map.
108   // TODO: Make this variable?
109   size_t data_size = max_capacity / 2;
110   size_t code_size = max_capacity - data_size;
111   DCHECK_EQ(code_size + data_size, max_capacity);
112   uint8_t* divider = data_map->Begin() + data_size;
113 
114   MemMap* code_map =
115       data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str, use_ashmem);
116   if (code_map == nullptr) {
117     std::ostringstream oss;
118     oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
119     *error_msg = oss.str();
120     return nullptr;
121   }
122   DCHECK_EQ(code_map->Begin(), divider);
123   data_size = initial_capacity / 2;
124   code_size = initial_capacity - data_size;
125   DCHECK_EQ(code_size + data_size, initial_capacity);
126   return new JitCodeCache(
127       code_map, data_map, code_size, data_size, max_capacity, garbage_collect_code);
128 }
129 
JitCodeCache(MemMap * code_map,MemMap * data_map,size_t initial_code_capacity,size_t initial_data_capacity,size_t max_capacity,bool garbage_collect_code)130 JitCodeCache::JitCodeCache(MemMap* code_map,
131                            MemMap* data_map,
132                            size_t initial_code_capacity,
133                            size_t initial_data_capacity,
134                            size_t max_capacity,
135                            bool garbage_collect_code)
136     : lock_("Jit code cache", kJitCodeCacheLock),
137       lock_cond_("Jit code cache condition variable", lock_),
138       collection_in_progress_(false),
139       code_map_(code_map),
140       data_map_(data_map),
141       max_capacity_(max_capacity),
142       current_capacity_(initial_code_capacity + initial_data_capacity),
143       code_end_(initial_code_capacity),
144       data_end_(initial_data_capacity),
145       last_collection_increased_code_cache_(false),
146       last_update_time_ns_(0),
147       garbage_collect_code_(garbage_collect_code),
148       used_memory_for_data_(0),
149       used_memory_for_code_(0),
150       number_of_compilations_(0),
151       number_of_osr_compilations_(0),
152       number_of_collections_(0),
153       histogram_stack_map_memory_use_("Memory used for stack maps", 16),
154       histogram_code_memory_use_("Memory used for compiled code", 16),
155       histogram_profiling_info_memory_use_("Memory used for profiling info", 16),
156       is_weak_access_enabled_(true),
157       inline_cache_cond_("Jit inline cache condition variable", lock_) {
158 
159   DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity);
160   code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/);
161   data_mspace_ = create_mspace_with_base(data_map_->Begin(), data_end_, false /*locked*/);
162 
163   if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
164     PLOG(FATAL) << "create_mspace_with_base failed";
165   }
166 
167   SetFootprintLimit(current_capacity_);
168 
169   CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
170   CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData);
171 
172   VLOG(jit) << "Created jit code cache: initial data size="
173             << PrettySize(initial_data_capacity)
174             << ", initial code size="
175             << PrettySize(initial_code_capacity);
176 }
177 
ContainsPc(const void * ptr) const178 bool JitCodeCache::ContainsPc(const void* ptr) const {
179   return code_map_->Begin() <= ptr && ptr < code_map_->End();
180 }
181 
ContainsMethod(ArtMethod * method)182 bool JitCodeCache::ContainsMethod(ArtMethod* method) {
183   MutexLock mu(Thread::Current(), lock_);
184   for (auto& it : method_code_map_) {
185     if (it.second == method) {
186       return true;
187     }
188   }
189   return false;
190 }
191 
192 class ScopedCodeCacheWrite : ScopedTrace {
193  public:
ScopedCodeCacheWrite(MemMap * code_map,bool only_for_tlb_shootdown=false)194   explicit ScopedCodeCacheWrite(MemMap* code_map, bool only_for_tlb_shootdown = false)
195       : ScopedTrace("ScopedCodeCacheWrite"),
196         code_map_(code_map),
197         only_for_tlb_shootdown_(only_for_tlb_shootdown) {
198     ScopedTrace trace("mprotect all");
199     CHECKED_MPROTECT(
200         code_map_->Begin(), only_for_tlb_shootdown_ ? kPageSize : code_map_->Size(), kProtAll);
201   }
~ScopedCodeCacheWrite()202   ~ScopedCodeCacheWrite() {
203     ScopedTrace trace("mprotect code");
204     CHECKED_MPROTECT(
205         code_map_->Begin(), only_for_tlb_shootdown_ ? kPageSize : code_map_->Size(), kProtCode);
206   }
207  private:
208   MemMap* const code_map_;
209 
210   // If we're using ScopedCacheWrite only for TLB shootdown, we limit the scope of mprotect to
211   // one page.
212   const bool only_for_tlb_shootdown_;
213 
214   DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
215 };
216 
CommitCode(Thread * self,ArtMethod * method,uint8_t * stack_map,uint8_t * method_info,uint8_t * roots_data,size_t frame_size_in_bytes,size_t core_spill_mask,size_t fp_spill_mask,const uint8_t * code,size_t code_size,size_t data_size,bool osr,Handle<mirror::ObjectArray<mirror::Object>> roots,bool has_should_deoptimize_flag,const ArenaSet<ArtMethod * > & cha_single_implementation_list)217 uint8_t* JitCodeCache::CommitCode(Thread* self,
218                                   ArtMethod* method,
219                                   uint8_t* stack_map,
220                                   uint8_t* method_info,
221                                   uint8_t* roots_data,
222                                   size_t frame_size_in_bytes,
223                                   size_t core_spill_mask,
224                                   size_t fp_spill_mask,
225                                   const uint8_t* code,
226                                   size_t code_size,
227                                   size_t data_size,
228                                   bool osr,
229                                   Handle<mirror::ObjectArray<mirror::Object>> roots,
230                                   bool has_should_deoptimize_flag,
231                                   const ArenaSet<ArtMethod*>& cha_single_implementation_list) {
232   uint8_t* result = CommitCodeInternal(self,
233                                        method,
234                                        stack_map,
235                                        method_info,
236                                        roots_data,
237                                        frame_size_in_bytes,
238                                        core_spill_mask,
239                                        fp_spill_mask,
240                                        code,
241                                        code_size,
242                                        data_size,
243                                        osr,
244                                        roots,
245                                        has_should_deoptimize_flag,
246                                        cha_single_implementation_list);
247   if (result == nullptr) {
248     // Retry.
249     GarbageCollectCache(self);
250     result = CommitCodeInternal(self,
251                                 method,
252                                 stack_map,
253                                 method_info,
254                                 roots_data,
255                                 frame_size_in_bytes,
256                                 core_spill_mask,
257                                 fp_spill_mask,
258                                 code,
259                                 code_size,
260                                 data_size,
261                                 osr,
262                                 roots,
263                                 has_should_deoptimize_flag,
264                                 cha_single_implementation_list);
265   }
266   return result;
267 }
268 
WaitForPotentialCollectionToComplete(Thread * self)269 bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
270   bool in_collection = false;
271   while (collection_in_progress_) {
272     in_collection = true;
273     lock_cond_.Wait(self);
274   }
275   return in_collection;
276 }
277 
FromCodeToAllocation(const void * code)278 static uintptr_t FromCodeToAllocation(const void* code) {
279   size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
280   return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
281 }
282 
ComputeRootTableSize(uint32_t number_of_roots)283 static uint32_t ComputeRootTableSize(uint32_t number_of_roots) {
284   return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>);
285 }
286 
GetNumberOfRoots(const uint8_t * stack_map)287 static uint32_t GetNumberOfRoots(const uint8_t* stack_map) {
288   // The length of the table is stored just before the stack map (and therefore at the end of
289   // the table itself), in order to be able to fetch it from a `stack_map` pointer.
290   return reinterpret_cast<const uint32_t*>(stack_map)[-1];
291 }
292 
FillRootTableLength(uint8_t * roots_data,uint32_t length)293 static void FillRootTableLength(uint8_t* roots_data, uint32_t length) {
294   // Store the length of the table at the end. This will allow fetching it from a `stack_map`
295   // pointer.
296   reinterpret_cast<uint32_t*>(roots_data)[length] = length;
297 }
298 
FromStackMapToRoots(const uint8_t * stack_map_data)299 static const uint8_t* FromStackMapToRoots(const uint8_t* stack_map_data) {
300   return stack_map_data - ComputeRootTableSize(GetNumberOfRoots(stack_map_data));
301 }
302 
FillRootTable(uint8_t * roots_data,Handle<mirror::ObjectArray<mirror::Object>> roots)303 static void FillRootTable(uint8_t* roots_data, Handle<mirror::ObjectArray<mirror::Object>> roots)
304     REQUIRES_SHARED(Locks::mutator_lock_) {
305   GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
306   const uint32_t length = roots->GetLength();
307   // Put all roots in `roots_data`.
308   for (uint32_t i = 0; i < length; ++i) {
309     ObjPtr<mirror::Object> object = roots->Get(i);
310     if (kIsDebugBuild) {
311       // Ensure the string is strongly interned. b/32995596
312       if (object->IsString()) {
313         ObjPtr<mirror::String> str = reinterpret_cast<mirror::String*>(object.Ptr());
314         ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
315         CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr);
316       }
317     }
318     gc_roots[i] = GcRoot<mirror::Object>(object);
319   }
320 }
321 
GetRootTable(const void * code_ptr,uint32_t * number_of_roots=nullptr)322 static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) {
323   OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
324   uint8_t* data = method_header->GetOptimizedCodeInfoPtr();
325   uint32_t roots = GetNumberOfRoots(data);
326   if (number_of_roots != nullptr) {
327     *number_of_roots = roots;
328   }
329   return data - ComputeRootTableSize(roots);
330 }
331 
332 // Use a sentinel for marking entries in the JIT table that have been cleared.
333 // This helps diagnosing in case the compiled code tries to wrongly access such
334 // entries.
335 static mirror::Class* const weak_sentinel = reinterpret_cast<mirror::Class*>(0x1);
336 
337 // Helper for the GC to process a weak class in a JIT root table.
ProcessWeakClass(GcRoot<mirror::Class> * root_ptr,IsMarkedVisitor * visitor,mirror::Class * update)338 static inline void ProcessWeakClass(GcRoot<mirror::Class>* root_ptr,
339                                     IsMarkedVisitor* visitor,
340                                     mirror::Class* update)
341     REQUIRES_SHARED(Locks::mutator_lock_) {
342   // This does not need a read barrier because this is called by GC.
343   mirror::Class* cls = root_ptr->Read<kWithoutReadBarrier>();
344   if (cls != nullptr && cls != weak_sentinel) {
345     DCHECK((cls->IsClass<kDefaultVerifyFlags, kWithoutReadBarrier>()));
346     // Look at the classloader of the class to know if it has been unloaded.
347     // This does not need a read barrier because this is called by GC.
348     mirror::Object* class_loader =
349         cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>();
350     if (class_loader == nullptr || visitor->IsMarked(class_loader) != nullptr) {
351       // The class loader is live, update the entry if the class has moved.
352       mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls));
353       // Note that new_object can be null for CMS and newly allocated objects.
354       if (new_cls != nullptr && new_cls != cls) {
355         *root_ptr = GcRoot<mirror::Class>(new_cls);
356       }
357     } else {
358       // The class loader is not live, clear the entry.
359       *root_ptr = GcRoot<mirror::Class>(update);
360     }
361   }
362 }
363 
SweepRootTables(IsMarkedVisitor * visitor)364 void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) {
365   MutexLock mu(Thread::Current(), lock_);
366   for (const auto& entry : method_code_map_) {
367     uint32_t number_of_roots = 0;
368     uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots);
369     GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
370     for (uint32_t i = 0; i < number_of_roots; ++i) {
371       // This does not need a read barrier because this is called by GC.
372       mirror::Object* object = roots[i].Read<kWithoutReadBarrier>();
373       if (object == nullptr || object == weak_sentinel) {
374         // entry got deleted in a previous sweep.
375       } else if (object->IsString<kDefaultVerifyFlags, kWithoutReadBarrier>()) {
376         mirror::Object* new_object = visitor->IsMarked(object);
377         // We know the string is marked because it's a strongly-interned string that
378         // is always alive. The IsMarked implementation of the CMS collector returns
379         // null for newly allocated objects, but we know those haven't moved. Therefore,
380         // only update the entry if we get a different non-null string.
381         // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method
382         // out of the weak access/creation pause. b/32167580
383         if (new_object != nullptr && new_object != object) {
384           DCHECK(new_object->IsString());
385           roots[i] = GcRoot<mirror::Object>(new_object);
386         }
387       } else {
388         ProcessWeakClass(
389             reinterpret_cast<GcRoot<mirror::Class>*>(&roots[i]), visitor, weak_sentinel);
390       }
391     }
392   }
393   // Walk over inline caches to clear entries containing unloaded classes.
394   for (ProfilingInfo* info : profiling_infos_) {
395     for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
396       InlineCache* cache = &info->cache_[i];
397       for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
398         ProcessWeakClass(&cache->classes_[j], visitor, nullptr);
399       }
400     }
401   }
402 }
403 
FreeCode(const void * code_ptr)404 void JitCodeCache::FreeCode(const void* code_ptr) {
405   uintptr_t allocation = FromCodeToAllocation(code_ptr);
406   // Notify native debugger that we are about to remove the code.
407   // It does nothing if we are not using native debugger.
408   DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr));
409   FreeData(GetRootTable(code_ptr));
410   FreeCode(reinterpret_cast<uint8_t*>(allocation));
411 }
412 
FreeAllMethodHeaders(const std::unordered_set<OatQuickMethodHeader * > & method_headers)413 void JitCodeCache::FreeAllMethodHeaders(
414     const std::unordered_set<OatQuickMethodHeader*>& method_headers) {
415   {
416     MutexLock mu(Thread::Current(), *Locks::cha_lock_);
417     Runtime::Current()->GetClassHierarchyAnalysis()
418         ->RemoveDependentsWithMethodHeaders(method_headers);
419   }
420 
421   // We need to remove entries in method_headers from CHA dependencies
422   // first since once we do FreeCode() below, the memory can be reused
423   // so it's possible for the same method_header to start representing
424   // different compile code.
425   MutexLock mu(Thread::Current(), lock_);
426   ScopedCodeCacheWrite scc(code_map_.get());
427   for (const OatQuickMethodHeader* method_header : method_headers) {
428     FreeCode(method_header->GetCode());
429   }
430 }
431 
RemoveMethodsIn(Thread * self,const LinearAlloc & alloc)432 void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
433   ScopedTrace trace(__PRETTY_FUNCTION__);
434   // We use a set to first collect all method_headers whose code need to be
435   // removed. We need to free the underlying code after we remove CHA dependencies
436   // for entries in this set. And it's more efficient to iterate through
437   // the CHA dependency map just once with an unordered_set.
438   std::unordered_set<OatQuickMethodHeader*> method_headers;
439   {
440     MutexLock mu(self, lock_);
441     // We do not check if a code cache GC is in progress, as this method comes
442     // with the classlinker_classes_lock_ held, and suspending ourselves could
443     // lead to a deadlock.
444     {
445       ScopedCodeCacheWrite scc(code_map_.get());
446       for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
447         if (alloc.ContainsUnsafe(it->second)) {
448           method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first));
449           it = method_code_map_.erase(it);
450         } else {
451           ++it;
452         }
453       }
454     }
455     for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
456       if (alloc.ContainsUnsafe(it->first)) {
457         // Note that the code has already been pushed to method_headers in the loop
458         // above and is going to be removed in FreeCode() below.
459         it = osr_code_map_.erase(it);
460       } else {
461         ++it;
462       }
463     }
464     for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
465       ProfilingInfo* info = *it;
466       if (alloc.ContainsUnsafe(info->GetMethod())) {
467         info->GetMethod()->SetProfilingInfo(nullptr);
468         FreeData(reinterpret_cast<uint8_t*>(info));
469         it = profiling_infos_.erase(it);
470       } else {
471         ++it;
472       }
473     }
474   }
475   FreeAllMethodHeaders(method_headers);
476 }
477 
IsWeakAccessEnabled(Thread * self) const478 bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const {
479   return kUseReadBarrier
480       ? self->GetWeakRefAccessEnabled()
481       : is_weak_access_enabled_.LoadSequentiallyConsistent();
482 }
483 
WaitUntilInlineCacheAccessible(Thread * self)484 void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) {
485   if (IsWeakAccessEnabled(self)) {
486     return;
487   }
488   ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
489   MutexLock mu(self, lock_);
490   while (!IsWeakAccessEnabled(self)) {
491     inline_cache_cond_.Wait(self);
492   }
493 }
494 
BroadcastForInlineCacheAccess()495 void JitCodeCache::BroadcastForInlineCacheAccess() {
496   Thread* self = Thread::Current();
497   MutexLock mu(self, lock_);
498   inline_cache_cond_.Broadcast(self);
499 }
500 
AllowInlineCacheAccess()501 void JitCodeCache::AllowInlineCacheAccess() {
502   DCHECK(!kUseReadBarrier);
503   is_weak_access_enabled_.StoreSequentiallyConsistent(true);
504   BroadcastForInlineCacheAccess();
505 }
506 
DisallowInlineCacheAccess()507 void JitCodeCache::DisallowInlineCacheAccess() {
508   DCHECK(!kUseReadBarrier);
509   is_weak_access_enabled_.StoreSequentiallyConsistent(false);
510 }
511 
CopyInlineCacheInto(const InlineCache & ic,Handle<mirror::ObjectArray<mirror::Class>> array)512 void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic,
513                                        Handle<mirror::ObjectArray<mirror::Class>> array) {
514   WaitUntilInlineCacheAccessible(Thread::Current());
515   // Note that we don't need to lock `lock_` here, the compiler calling
516   // this method has already ensured the inline cache will not be deleted.
517   for (size_t in_cache = 0, in_array = 0;
518        in_cache < InlineCache::kIndividualCacheSize;
519        ++in_cache) {
520     mirror::Class* object = ic.classes_[in_cache].Read();
521     if (object != nullptr) {
522       array->Set(in_array++, object);
523     }
524   }
525 }
526 
CommitCodeInternal(Thread * self,ArtMethod * method,uint8_t * stack_map,uint8_t * method_info,uint8_t * roots_data,size_t frame_size_in_bytes,size_t core_spill_mask,size_t fp_spill_mask,const uint8_t * code,size_t code_size,size_t data_size,bool osr,Handle<mirror::ObjectArray<mirror::Object>> roots,bool has_should_deoptimize_flag,const ArenaSet<ArtMethod * > & cha_single_implementation_list)527 uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
528                                           ArtMethod* method,
529                                           uint8_t* stack_map,
530                                           uint8_t* method_info,
531                                           uint8_t* roots_data,
532                                           size_t frame_size_in_bytes,
533                                           size_t core_spill_mask,
534                                           size_t fp_spill_mask,
535                                           const uint8_t* code,
536                                           size_t code_size,
537                                           size_t data_size,
538                                           bool osr,
539                                           Handle<mirror::ObjectArray<mirror::Object>> roots,
540                                           bool has_should_deoptimize_flag,
541                                           const ArenaSet<ArtMethod*>&
542                                               cha_single_implementation_list) {
543   DCHECK(stack_map != nullptr);
544   size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
545   // Ensure the header ends up at expected instruction alignment.
546   size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
547   size_t total_size = header_size + code_size;
548 
549   OatQuickMethodHeader* method_header = nullptr;
550   uint8_t* code_ptr = nullptr;
551   uint8_t* memory = nullptr;
552   {
553     ScopedThreadSuspension sts(self, kSuspended);
554     MutexLock mu(self, lock_);
555     WaitForPotentialCollectionToComplete(self);
556     {
557       ScopedCodeCacheWrite scc(code_map_.get());
558       memory = AllocateCode(total_size);
559       if (memory == nullptr) {
560         return nullptr;
561       }
562       code_ptr = memory + header_size;
563 
564       std::copy(code, code + code_size, code_ptr);
565       method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
566       new (method_header) OatQuickMethodHeader(
567           code_ptr - stack_map,
568           code_ptr - method_info,
569           frame_size_in_bytes,
570           core_spill_mask,
571           fp_spill_mask,
572           code_size);
573       // Flush caches before we remove write permission because some ARMv8 Qualcomm kernels may
574       // trigger a segfault if a page fault occurs when requesting a cache maintenance operation.
575       // This is a kernel bug that we need to work around until affected devices (e.g. Nexus 5X and
576       // 6P) stop being supported or their kernels are fixed.
577       //
578       // For reference, this behavior is caused by this commit:
579       // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c
580       FlushInstructionCache(reinterpret_cast<char*>(code_ptr),
581                             reinterpret_cast<char*>(code_ptr + code_size));
582       DCHECK(!Runtime::Current()->IsAotCompiler());
583       if (has_should_deoptimize_flag) {
584         method_header->SetHasShouldDeoptimizeFlag();
585       }
586     }
587 
588     number_of_compilations_++;
589   }
590   // We need to update the entry point in the runnable state for the instrumentation.
591   {
592     // Need cha_lock_ for checking all single-implementation flags and register
593     // dependencies.
594     MutexLock cha_mu(self, *Locks::cha_lock_);
595     bool single_impl_still_valid = true;
596     for (ArtMethod* single_impl : cha_single_implementation_list) {
597       if (!single_impl->HasSingleImplementation()) {
598         // We simply discard the compiled code. Clear the
599         // counter so that it may be recompiled later. Hopefully the
600         // class hierarchy will be more stable when compilation is retried.
601         single_impl_still_valid = false;
602         method->ClearCounter();
603         break;
604       }
605     }
606 
607     // Discard the code if any single-implementation assumptions are now invalid.
608     if (!single_impl_still_valid) {
609       VLOG(jit) << "JIT discarded jitted code due to invalid single-implementation assumptions.";
610       return nullptr;
611     }
612     DCHECK(cha_single_implementation_list.empty() || !Runtime::Current()->IsJavaDebuggable())
613         << "Should not be using cha on debuggable apps/runs!";
614 
615     for (ArtMethod* single_impl : cha_single_implementation_list) {
616       Runtime::Current()->GetClassHierarchyAnalysis()->AddDependency(
617           single_impl, method, method_header);
618     }
619 
620     // The following needs to be guarded by cha_lock_ also. Otherwise it's
621     // possible that the compiled code is considered invalidated by some class linking,
622     // but below we still make the compiled code valid for the method.
623     MutexLock mu(self, lock_);
624     // Fill the root table before updating the entry point.
625     DCHECK_EQ(FromStackMapToRoots(stack_map), roots_data);
626     DCHECK_LE(roots_data, stack_map);
627     FillRootTable(roots_data, roots);
628     {
629       // Flush data cache, as compiled code references literals in it.
630       // We also need a TLB shootdown to act as memory barrier across cores.
631       ScopedCodeCacheWrite ccw(code_map_.get(), /* only_for_tlb_shootdown */ true);
632       FlushDataCache(reinterpret_cast<char*>(roots_data),
633                      reinterpret_cast<char*>(roots_data + data_size));
634     }
635     method_code_map_.Put(code_ptr, method);
636     if (osr) {
637       number_of_osr_compilations_++;
638       osr_code_map_.Put(method, code_ptr);
639     } else {
640       Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
641           method, method_header->GetEntryPoint());
642     }
643     if (collection_in_progress_) {
644       // We need to update the live bitmap if there is a GC to ensure it sees this new
645       // code.
646       GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
647     }
648     last_update_time_ns_.StoreRelease(NanoTime());
649     VLOG(jit)
650         << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
651         << ArtMethod::PrettyMethod(method) << "@" << method
652         << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
653         << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
654         << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
655         << reinterpret_cast<const void*>(method_header->GetEntryPoint() +
656                                          method_header->GetCodeSize());
657     histogram_code_memory_use_.AddValue(code_size);
658     if (code_size > kCodeSizeLogThreshold) {
659       LOG(INFO) << "JIT allocated "
660                 << PrettySize(code_size)
661                 << " for compiled code of "
662                 << ArtMethod::PrettyMethod(method);
663     }
664   }
665 
666   return reinterpret_cast<uint8_t*>(method_header);
667 }
668 
CodeCacheSize()669 size_t JitCodeCache::CodeCacheSize() {
670   MutexLock mu(Thread::Current(), lock_);
671   return CodeCacheSizeLocked();
672 }
673 
674 // This notifies the code cache that the given method has been redefined and that it should remove
675 // any cached information it has on the method. All threads must be suspended before calling this
676 // method. The compiled code for the method (if there is any) must not be in any threads call stack.
NotifyMethodRedefined(ArtMethod * method)677 void JitCodeCache::NotifyMethodRedefined(ArtMethod* method) {
678   MutexLock mu(Thread::Current(), lock_);
679   if (method->IsNative()) {
680     return;
681   }
682   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
683   if (info != nullptr) {
684     auto profile = std::find(profiling_infos_.begin(), profiling_infos_.end(), info);
685     DCHECK(profile != profiling_infos_.end());
686     profiling_infos_.erase(profile);
687   }
688   method->SetProfilingInfo(nullptr);
689   ScopedCodeCacheWrite ccw(code_map_.get());
690   for (auto code_iter = method_code_map_.begin(); code_iter != method_code_map_.end();) {
691     if (code_iter->second == method) {
692       FreeCode(code_iter->first);
693       code_iter = method_code_map_.erase(code_iter);
694       continue;
695     }
696     ++code_iter;
697   }
698   auto code_map = osr_code_map_.find(method);
699   if (code_map != osr_code_map_.end()) {
700     osr_code_map_.erase(code_map);
701   }
702 }
703 
704 // This invalidates old_method. Once this function returns one can no longer use old_method to
705 // execute code unless it is fixed up. This fixup will happen later in the process of installing a
706 // class redefinition.
707 // TODO We should add some info to ArtMethod to note that 'old_method' has been invalidated and
708 // shouldn't be used since it is no longer logically in the jit code cache.
709 // TODO We should add DCHECKS that validate that the JIT is paused when this method is entered.
MoveObsoleteMethod(ArtMethod * old_method,ArtMethod * new_method)710 void JitCodeCache::MoveObsoleteMethod(ArtMethod* old_method, ArtMethod* new_method) {
711   // Native methods have no profiling info and need no special handling from the JIT code cache.
712   if (old_method->IsNative()) {
713     return;
714   }
715   MutexLock mu(Thread::Current(), lock_);
716   // Update ProfilingInfo to the new one and remove it from the old_method.
717   if (old_method->GetProfilingInfo(kRuntimePointerSize) != nullptr) {
718     DCHECK_EQ(old_method->GetProfilingInfo(kRuntimePointerSize)->GetMethod(), old_method);
719     ProfilingInfo* info = old_method->GetProfilingInfo(kRuntimePointerSize);
720     old_method->SetProfilingInfo(nullptr);
721     // Since the JIT should be paused and all threads suspended by the time this is called these
722     // checks should always pass.
723     DCHECK(!info->IsInUseByCompiler());
724     new_method->SetProfilingInfo(info);
725     info->method_ = new_method;
726   }
727   // Update method_code_map_ to point to the new method.
728   for (auto& it : method_code_map_) {
729     if (it.second == old_method) {
730       it.second = new_method;
731     }
732   }
733   // Update osr_code_map_ to point to the new method.
734   auto code_map = osr_code_map_.find(old_method);
735   if (code_map != osr_code_map_.end()) {
736     osr_code_map_.Put(new_method, code_map->second);
737     osr_code_map_.erase(old_method);
738   }
739 }
740 
CodeCacheSizeLocked()741 size_t JitCodeCache::CodeCacheSizeLocked() {
742   return used_memory_for_code_;
743 }
744 
DataCacheSize()745 size_t JitCodeCache::DataCacheSize() {
746   MutexLock mu(Thread::Current(), lock_);
747   return DataCacheSizeLocked();
748 }
749 
DataCacheSizeLocked()750 size_t JitCodeCache::DataCacheSizeLocked() {
751   return used_memory_for_data_;
752 }
753 
ClearData(Thread * self,uint8_t * stack_map_data,uint8_t * roots_data)754 void JitCodeCache::ClearData(Thread* self,
755                              uint8_t* stack_map_data,
756                              uint8_t* roots_data) {
757   DCHECK_EQ(FromStackMapToRoots(stack_map_data), roots_data);
758   MutexLock mu(self, lock_);
759   FreeData(reinterpret_cast<uint8_t*>(roots_data));
760 }
761 
ReserveData(Thread * self,size_t stack_map_size,size_t method_info_size,size_t number_of_roots,ArtMethod * method,uint8_t ** stack_map_data,uint8_t ** method_info_data,uint8_t ** roots_data)762 size_t JitCodeCache::ReserveData(Thread* self,
763                                  size_t stack_map_size,
764                                  size_t method_info_size,
765                                  size_t number_of_roots,
766                                  ArtMethod* method,
767                                  uint8_t** stack_map_data,
768                                  uint8_t** method_info_data,
769                                  uint8_t** roots_data) {
770   size_t table_size = ComputeRootTableSize(number_of_roots);
771   size_t size = RoundUp(stack_map_size + method_info_size + table_size, sizeof(void*));
772   uint8_t* result = nullptr;
773 
774   {
775     ScopedThreadSuspension sts(self, kSuspended);
776     MutexLock mu(self, lock_);
777     WaitForPotentialCollectionToComplete(self);
778     result = AllocateData(size);
779   }
780 
781   if (result == nullptr) {
782     // Retry.
783     GarbageCollectCache(self);
784     ScopedThreadSuspension sts(self, kSuspended);
785     MutexLock mu(self, lock_);
786     WaitForPotentialCollectionToComplete(self);
787     result = AllocateData(size);
788   }
789 
790   MutexLock mu(self, lock_);
791   histogram_stack_map_memory_use_.AddValue(size);
792   if (size > kStackMapSizeLogThreshold) {
793     LOG(INFO) << "JIT allocated "
794               << PrettySize(size)
795               << " for stack maps of "
796               << ArtMethod::PrettyMethod(method);
797   }
798   if (result != nullptr) {
799     *roots_data = result;
800     *stack_map_data = result + table_size;
801     *method_info_data = *stack_map_data + stack_map_size;
802     FillRootTableLength(*roots_data, number_of_roots);
803     return size;
804   } else {
805     *roots_data = nullptr;
806     *stack_map_data = nullptr;
807     *method_info_data = nullptr;
808     return 0;
809   }
810 }
811 
812 class MarkCodeVisitor FINAL : public StackVisitor {
813  public:
MarkCodeVisitor(Thread * thread_in,JitCodeCache * code_cache_in)814   MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
815       : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
816         code_cache_(code_cache_in),
817         bitmap_(code_cache_->GetLiveBitmap()) {}
818 
VisitFrame()819   bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
820     const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
821     if (method_header == nullptr) {
822       return true;
823     }
824     const void* code = method_header->GetCode();
825     if (code_cache_->ContainsPc(code)) {
826       // Use the atomic set version, as multiple threads are executing this code.
827       bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
828     }
829     return true;
830   }
831 
832  private:
833   JitCodeCache* const code_cache_;
834   CodeCacheBitmap* const bitmap_;
835 };
836 
837 class MarkCodeClosure FINAL : public Closure {
838  public:
MarkCodeClosure(JitCodeCache * code_cache,Barrier * barrier)839   MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
840       : code_cache_(code_cache), barrier_(barrier) {}
841 
Run(Thread * thread)842   void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
843     ScopedTrace trace(__PRETTY_FUNCTION__);
844     DCHECK(thread == Thread::Current() || thread->IsSuspended());
845     MarkCodeVisitor visitor(thread, code_cache_);
846     visitor.WalkStack();
847     if (kIsDebugBuild) {
848       // The stack walking code queries the side instrumentation stack if it
849       // sees an instrumentation exit pc, so the JIT code of methods in that stack
850       // must have been seen. We sanity check this below.
851       for (const instrumentation::InstrumentationStackFrame& frame
852               : *thread->GetInstrumentationStack()) {
853         // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
854         // its stack frame, it is not the method owning return_pc_. We just pass null to
855         // LookupMethodHeader: the method is only checked against in debug builds.
856         OatQuickMethodHeader* method_header =
857             code_cache_->LookupMethodHeader(frame.return_pc_, nullptr);
858         if (method_header != nullptr) {
859           const void* code = method_header->GetCode();
860           CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
861         }
862       }
863     }
864     barrier_->Pass(Thread::Current());
865   }
866 
867  private:
868   JitCodeCache* const code_cache_;
869   Barrier* const barrier_;
870 };
871 
NotifyCollectionDone(Thread * self)872 void JitCodeCache::NotifyCollectionDone(Thread* self) {
873   collection_in_progress_ = false;
874   lock_cond_.Broadcast(self);
875 }
876 
SetFootprintLimit(size_t new_footprint)877 void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
878   size_t per_space_footprint = new_footprint / 2;
879   DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
880   DCHECK_EQ(per_space_footprint * 2, new_footprint);
881   mspace_set_footprint_limit(data_mspace_, per_space_footprint);
882   {
883     ScopedCodeCacheWrite scc(code_map_.get());
884     mspace_set_footprint_limit(code_mspace_, per_space_footprint);
885   }
886 }
887 
IncreaseCodeCacheCapacity()888 bool JitCodeCache::IncreaseCodeCacheCapacity() {
889   if (current_capacity_ == max_capacity_) {
890     return false;
891   }
892 
893   // Double the capacity if we're below 1MB, or increase it by 1MB if
894   // we're above.
895   if (current_capacity_ < 1 * MB) {
896     current_capacity_ *= 2;
897   } else {
898     current_capacity_ += 1 * MB;
899   }
900   if (current_capacity_ > max_capacity_) {
901     current_capacity_ = max_capacity_;
902   }
903 
904   if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
905     LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
906   }
907 
908   SetFootprintLimit(current_capacity_);
909 
910   return true;
911 }
912 
MarkCompiledCodeOnThreadStacks(Thread * self)913 void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
914   Barrier barrier(0);
915   size_t threads_running_checkpoint = 0;
916   MarkCodeClosure closure(this, &barrier);
917   threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
918   // Now that we have run our checkpoint, move to a suspended state and wait
919   // for other threads to run the checkpoint.
920   ScopedThreadSuspension sts(self, kSuspended);
921   if (threads_running_checkpoint != 0) {
922     barrier.Increment(self, threads_running_checkpoint);
923   }
924 }
925 
ShouldDoFullCollection()926 bool JitCodeCache::ShouldDoFullCollection() {
927   if (current_capacity_ == max_capacity_) {
928     // Always do a full collection when the code cache is full.
929     return true;
930   } else if (current_capacity_ < kReservedCapacity) {
931     // Always do partial collection when the code cache size is below the reserved
932     // capacity.
933     return false;
934   } else if (last_collection_increased_code_cache_) {
935     // This time do a full collection.
936     return true;
937   } else {
938     // This time do a partial collection.
939     return false;
940   }
941 }
942 
GarbageCollectCache(Thread * self)943 void JitCodeCache::GarbageCollectCache(Thread* self) {
944   ScopedTrace trace(__FUNCTION__);
945   if (!garbage_collect_code_) {
946     MutexLock mu(self, lock_);
947     IncreaseCodeCacheCapacity();
948     return;
949   }
950 
951   // Wait for an existing collection, or let everyone know we are starting one.
952   {
953     ScopedThreadSuspension sts(self, kSuspended);
954     MutexLock mu(self, lock_);
955     if (WaitForPotentialCollectionToComplete(self)) {
956       return;
957     } else {
958       number_of_collections_++;
959       live_bitmap_.reset(CodeCacheBitmap::Create(
960           "code-cache-bitmap",
961           reinterpret_cast<uintptr_t>(code_map_->Begin()),
962           reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
963       collection_in_progress_ = true;
964     }
965   }
966 
967   TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit));
968   {
969     TimingLogger::ScopedTiming st("Code cache collection", &logger);
970 
971     bool do_full_collection = false;
972     {
973       MutexLock mu(self, lock_);
974       do_full_collection = ShouldDoFullCollection();
975     }
976 
977     if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
978       LOG(INFO) << "Do "
979                 << (do_full_collection ? "full" : "partial")
980                 << " code cache collection, code="
981                 << PrettySize(CodeCacheSize())
982                 << ", data=" << PrettySize(DataCacheSize());
983     }
984 
985     DoCollection(self, /* collect_profiling_info */ do_full_collection);
986 
987     if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
988       LOG(INFO) << "After code cache collection, code="
989                 << PrettySize(CodeCacheSize())
990                 << ", data=" << PrettySize(DataCacheSize());
991     }
992 
993     {
994       MutexLock mu(self, lock_);
995 
996       // Increase the code cache only when we do partial collections.
997       // TODO: base this strategy on how full the code cache is?
998       if (do_full_collection) {
999         last_collection_increased_code_cache_ = false;
1000       } else {
1001         last_collection_increased_code_cache_ = true;
1002         IncreaseCodeCacheCapacity();
1003       }
1004 
1005       bool next_collection_will_be_full = ShouldDoFullCollection();
1006 
1007       // Start polling the liveness of compiled code to prepare for the next full collection.
1008       if (next_collection_will_be_full) {
1009         // Save the entry point of methods we have compiled, and update the entry
1010         // point of those methods to the interpreter. If the method is invoked, the
1011         // interpreter will update its entry point to the compiled code and call it.
1012         for (ProfilingInfo* info : profiling_infos_) {
1013           const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1014           if (ContainsPc(entry_point)) {
1015             info->SetSavedEntryPoint(entry_point);
1016             // Don't call Instrumentation::UpdateMethods, as it can check the declaring
1017             // class of the method. We may be concurrently running a GC which makes accessing
1018             // the class unsafe. We know it is OK to bypass the instrumentation as we've just
1019             // checked that the current entry point is JIT compiled code.
1020             info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
1021           }
1022         }
1023 
1024         DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
1025       }
1026       live_bitmap_.reset(nullptr);
1027       NotifyCollectionDone(self);
1028     }
1029   }
1030   Runtime::Current()->GetJit()->AddTimingLogger(logger);
1031 }
1032 
RemoveUnmarkedCode(Thread * self)1033 void JitCodeCache::RemoveUnmarkedCode(Thread* self) {
1034   ScopedTrace trace(__FUNCTION__);
1035   std::unordered_set<OatQuickMethodHeader*> method_headers;
1036   {
1037     MutexLock mu(self, lock_);
1038     ScopedCodeCacheWrite scc(code_map_.get());
1039     // Iterate over all compiled code and remove entries that are not marked.
1040     for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
1041       const void* code_ptr = it->first;
1042       uintptr_t allocation = FromCodeToAllocation(code_ptr);
1043       if (GetLiveBitmap()->Test(allocation)) {
1044         ++it;
1045       } else {
1046         method_headers.insert(OatQuickMethodHeader::FromCodePointer(it->first));
1047         it = method_code_map_.erase(it);
1048       }
1049     }
1050   }
1051   FreeAllMethodHeaders(method_headers);
1052 }
1053 
DoCollection(Thread * self,bool collect_profiling_info)1054 void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
1055   ScopedTrace trace(__FUNCTION__);
1056   {
1057     MutexLock mu(self, lock_);
1058     if (collect_profiling_info) {
1059       // Clear the profiling info of methods that do not have compiled code as entrypoint.
1060       // Also remove the saved entry point from the ProfilingInfo objects.
1061       for (ProfilingInfo* info : profiling_infos_) {
1062         const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1063         if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) {
1064           info->GetMethod()->SetProfilingInfo(nullptr);
1065         }
1066 
1067         if (info->GetSavedEntryPoint() != nullptr) {
1068           info->SetSavedEntryPoint(nullptr);
1069           // We are going to move this method back to interpreter. Clear the counter now to
1070           // give it a chance to be hot again.
1071           info->GetMethod()->ClearCounter();
1072         }
1073       }
1074     } else if (kIsDebugBuild) {
1075       // Sanity check that the profiling infos do not have a dangling entry point.
1076       for (ProfilingInfo* info : profiling_infos_) {
1077         DCHECK(info->GetSavedEntryPoint() == nullptr);
1078       }
1079     }
1080 
1081     // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not
1082     // an entry point is either:
1083     // - an osr compiled code, that will be removed if not in a thread call stack.
1084     // - discarded compiled code, that will be removed if not in a thread call stack.
1085     for (const auto& it : method_code_map_) {
1086       ArtMethod* method = it.second;
1087       const void* code_ptr = it.first;
1088       const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1089       if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1090         GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
1091       }
1092     }
1093 
1094     // Empty osr method map, as osr compiled code will be deleted (except the ones
1095     // on thread stacks).
1096     osr_code_map_.clear();
1097   }
1098 
1099   // Run a checkpoint on all threads to mark the JIT compiled code they are running.
1100   MarkCompiledCodeOnThreadStacks(self);
1101 
1102   // At this point, mutator threads are still running, and entrypoints of methods can
1103   // change. We do know they cannot change to a code cache entry that is not marked,
1104   // therefore we can safely remove those entries.
1105   RemoveUnmarkedCode(self);
1106 
1107   if (collect_profiling_info) {
1108     ScopedThreadSuspension sts(self, kSuspended);
1109     MutexLock mu(self, lock_);
1110     // Free all profiling infos of methods not compiled nor being compiled.
1111     auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
1112       [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
1113         const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
1114         // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope
1115         // that the compiled code would not get revived. As mutator threads run concurrently,
1116         // they may have revived the compiled code, and now we are in the situation where
1117         // a method has compiled code but no ProfilingInfo.
1118         // We make sure compiled methods have a ProfilingInfo object. It is needed for
1119         // code cache collection.
1120         if (ContainsPc(ptr) &&
1121             info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
1122           info->GetMethod()->SetProfilingInfo(info);
1123         } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) {
1124           // No need for this ProfilingInfo object anymore.
1125           FreeData(reinterpret_cast<uint8_t*>(info));
1126           return true;
1127         }
1128         return false;
1129       });
1130     profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
1131     DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
1132   }
1133 }
1134 
CheckLiveCompiledCodeHasProfilingInfo()1135 bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() {
1136   ScopedTrace trace(__FUNCTION__);
1137   // Check that methods we have compiled do have a ProfilingInfo object. We would
1138   // have memory leaks of compiled code otherwise.
1139   for (const auto& it : method_code_map_) {
1140     ArtMethod* method = it.second;
1141     if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
1142       const void* code_ptr = it.first;
1143       const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1144       if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
1145         // If the code is not dead, then we have a problem. Note that this can even
1146         // happen just after a collection, as mutator threads are running in parallel
1147         // and could deoptimize an existing compiled code.
1148         return false;
1149       }
1150     }
1151   }
1152   return true;
1153 }
1154 
LookupMethodHeader(uintptr_t pc,ArtMethod * method)1155 OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
1156   static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
1157   if (kRuntimeISA == kArm) {
1158     // On Thumb-2, the pc is offset by one.
1159     --pc;
1160   }
1161   if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
1162     return nullptr;
1163   }
1164 
1165   MutexLock mu(Thread::Current(), lock_);
1166   if (method_code_map_.empty()) {
1167     return nullptr;
1168   }
1169   auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
1170   --it;
1171 
1172   const void* code_ptr = it->first;
1173   OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
1174   if (!method_header->Contains(pc)) {
1175     return nullptr;
1176   }
1177   if (kIsDebugBuild && method != nullptr) {
1178     // When we are walking the stack to redefine classes and creating obsolete methods it is
1179     // possible that we might have updated the method_code_map by making this method obsolete in a
1180     // previous frame. Therefore we should just check that the non-obsolete version of this method
1181     // is the one we expect. We change to the non-obsolete versions in the error message since the
1182     // obsolete version of the method might not be fully initialized yet. This situation can only
1183     // occur when we are in the process of allocating and setting up obsolete methods. Otherwise
1184     // method and it->second should be identical. (See runtime/openjdkjvmti/ti_redefine.cc for more
1185     // information.)
1186     DCHECK_EQ(it->second->GetNonObsoleteMethod(), method->GetNonObsoleteMethod())
1187         << ArtMethod::PrettyMethod(method->GetNonObsoleteMethod()) << " "
1188         << ArtMethod::PrettyMethod(it->second->GetNonObsoleteMethod()) << " "
1189         << std::hex << pc;
1190   }
1191   return method_header;
1192 }
1193 
LookupOsrMethodHeader(ArtMethod * method)1194 OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
1195   MutexLock mu(Thread::Current(), lock_);
1196   auto it = osr_code_map_.find(method);
1197   if (it == osr_code_map_.end()) {
1198     return nullptr;
1199   }
1200   return OatQuickMethodHeader::FromCodePointer(it->second);
1201 }
1202 
AddProfilingInfo(Thread * self,ArtMethod * method,const std::vector<uint32_t> & entries,bool retry_allocation)1203 ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
1204                                               ArtMethod* method,
1205                                               const std::vector<uint32_t>& entries,
1206                                               bool retry_allocation)
1207     // No thread safety analysis as we are using TryLock/Unlock explicitly.
1208     NO_THREAD_SAFETY_ANALYSIS {
1209   ProfilingInfo* info = nullptr;
1210   if (!retry_allocation) {
1211     // If we are allocating for the interpreter, just try to lock, to avoid
1212     // lock contention with the JIT.
1213     if (lock_.ExclusiveTryLock(self)) {
1214       info = AddProfilingInfoInternal(self, method, entries);
1215       lock_.ExclusiveUnlock(self);
1216     }
1217   } else {
1218     {
1219       MutexLock mu(self, lock_);
1220       info = AddProfilingInfoInternal(self, method, entries);
1221     }
1222 
1223     if (info == nullptr) {
1224       GarbageCollectCache(self);
1225       MutexLock mu(self, lock_);
1226       info = AddProfilingInfoInternal(self, method, entries);
1227     }
1228   }
1229   return info;
1230 }
1231 
AddProfilingInfoInternal(Thread * self ATTRIBUTE_UNUSED,ArtMethod * method,const std::vector<uint32_t> & entries)1232 ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
1233                                                       ArtMethod* method,
1234                                                       const std::vector<uint32_t>& entries) {
1235   size_t profile_info_size = RoundUp(
1236       sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
1237       sizeof(void*));
1238 
1239   // Check whether some other thread has concurrently created it.
1240   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1241   if (info != nullptr) {
1242     return info;
1243   }
1244 
1245   uint8_t* data = AllocateData(profile_info_size);
1246   if (data == nullptr) {
1247     return nullptr;
1248   }
1249   info = new (data) ProfilingInfo(method, entries);
1250 
1251   // Make sure other threads see the data in the profiling info object before the
1252   // store in the ArtMethod's ProfilingInfo pointer.
1253   QuasiAtomic::ThreadFenceRelease();
1254 
1255   method->SetProfilingInfo(info);
1256   profiling_infos_.push_back(info);
1257   histogram_profiling_info_memory_use_.AddValue(profile_info_size);
1258   return info;
1259 }
1260 
1261 // NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
1262 // is already held.
MoreCore(const void * mspace,intptr_t increment)1263 void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
1264   if (code_mspace_ == mspace) {
1265     size_t result = code_end_;
1266     code_end_ += increment;
1267     return reinterpret_cast<void*>(result + code_map_->Begin());
1268   } else {
1269     DCHECK_EQ(data_mspace_, mspace);
1270     size_t result = data_end_;
1271     data_end_ += increment;
1272     return reinterpret_cast<void*>(result + data_map_->Begin());
1273   }
1274 }
1275 
GetProfiledMethods(const std::set<std::string> & dex_base_locations,std::vector<ProfileMethodInfo> & methods)1276 void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations,
1277                                       std::vector<ProfileMethodInfo>& methods) {
1278   ScopedTrace trace(__FUNCTION__);
1279   MutexLock mu(Thread::Current(), lock_);
1280   uint16_t jit_compile_threshold = Runtime::Current()->GetJITOptions()->GetCompileThreshold();
1281   for (const ProfilingInfo* info : profiling_infos_) {
1282     ArtMethod* method = info->GetMethod();
1283     const DexFile* dex_file = method->GetDexFile();
1284     if (!ContainsElement(dex_base_locations, dex_file->GetBaseLocation())) {
1285       // Skip dex files which are not profiled.
1286       continue;
1287     }
1288     std::vector<ProfileMethodInfo::ProfileInlineCache> inline_caches;
1289 
1290     // If the method didn't reach the compilation threshold don't save the inline caches.
1291     // They might be incomplete and cause unnecessary deoptimizations.
1292     // If the inline cache is empty the compiler will generate a regular invoke virtual/interface.
1293     if (method->GetCounter() < jit_compile_threshold) {
1294       methods.emplace_back(/*ProfileMethodInfo*/
1295           dex_file, method->GetDexMethodIndex(), inline_caches);
1296       continue;
1297     }
1298 
1299     for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
1300       std::vector<ProfileMethodInfo::ProfileClassReference> profile_classes;
1301       const InlineCache& cache = info->cache_[i];
1302       ArtMethod* caller = info->GetMethod();
1303       bool is_missing_types = false;
1304       for (size_t k = 0; k < InlineCache::kIndividualCacheSize; k++) {
1305         mirror::Class* cls = cache.classes_[k].Read();
1306         if (cls == nullptr) {
1307           break;
1308         }
1309 
1310         // Check if the receiver is in the boot class path or if it's in the
1311         // same class loader as the caller. If not, skip it, as there is not
1312         // much we can do during AOT.
1313         if (!cls->IsBootStrapClassLoaded() &&
1314             caller->GetClassLoader() != cls->GetClassLoader()) {
1315           is_missing_types = true;
1316           continue;
1317         }
1318 
1319         const DexFile* class_dex_file = nullptr;
1320         dex::TypeIndex type_index;
1321 
1322         if (cls->GetDexCache() == nullptr) {
1323           DCHECK(cls->IsArrayClass()) << cls->PrettyClass();
1324           // Make a best effort to find the type index in the method's dex file.
1325           // We could search all open dex files but that might turn expensive
1326           // and probably not worth it.
1327           class_dex_file = dex_file;
1328           type_index = cls->FindTypeIndexInOtherDexFile(*dex_file);
1329         } else {
1330           class_dex_file = &(cls->GetDexFile());
1331           type_index = cls->GetDexTypeIndex();
1332         }
1333         if (!type_index.IsValid()) {
1334           // Could be a proxy class or an array for which we couldn't find the type index.
1335           is_missing_types = true;
1336           continue;
1337         }
1338         if (ContainsElement(dex_base_locations, class_dex_file->GetBaseLocation())) {
1339           // Only consider classes from the same apk (including multidex).
1340           profile_classes.emplace_back(/*ProfileMethodInfo::ProfileClassReference*/
1341               class_dex_file, type_index);
1342         } else {
1343           is_missing_types = true;
1344         }
1345       }
1346       if (!profile_classes.empty()) {
1347         inline_caches.emplace_back(/*ProfileMethodInfo::ProfileInlineCache*/
1348             cache.dex_pc_, is_missing_types, profile_classes);
1349       }
1350     }
1351     methods.emplace_back(/*ProfileMethodInfo*/
1352         dex_file, method->GetDexMethodIndex(), inline_caches);
1353   }
1354 }
1355 
GetLastUpdateTimeNs() const1356 uint64_t JitCodeCache::GetLastUpdateTimeNs() const {
1357   return last_update_time_ns_.LoadAcquire();
1358 }
1359 
IsOsrCompiled(ArtMethod * method)1360 bool JitCodeCache::IsOsrCompiled(ArtMethod* method) {
1361   MutexLock mu(Thread::Current(), lock_);
1362   return osr_code_map_.find(method) != osr_code_map_.end();
1363 }
1364 
NotifyCompilationOf(ArtMethod * method,Thread * self,bool osr)1365 bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
1366   if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
1367     return false;
1368   }
1369 
1370   MutexLock mu(self, lock_);
1371   if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
1372     return false;
1373   }
1374 
1375   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1376   if (info == nullptr) {
1377     VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled";
1378     // Because the counter is not atomic, there are some rare cases where we may not
1379     // hit the threshold for creating the ProfilingInfo. Reset the counter now to
1380     // "correct" this.
1381     method->ClearCounter();
1382     return false;
1383   }
1384 
1385   if (info->IsMethodBeingCompiled(osr)) {
1386     return false;
1387   }
1388 
1389   info->SetIsMethodBeingCompiled(true, osr);
1390   return true;
1391 }
1392 
NotifyCompilerUse(ArtMethod * method,Thread * self)1393 ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) {
1394   MutexLock mu(self, lock_);
1395   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1396   if (info != nullptr) {
1397     if (!info->IncrementInlineUse()) {
1398       // Overflow of inlining uses, just bail.
1399       return nullptr;
1400     }
1401   }
1402   return info;
1403 }
1404 
DoneCompilerUse(ArtMethod * method,Thread * self)1405 void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) {
1406   MutexLock mu(self, lock_);
1407   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1408   DCHECK(info != nullptr);
1409   info->DecrementInlineUse();
1410 }
1411 
DoneCompiling(ArtMethod * method,Thread * self ATTRIBUTE_UNUSED,bool osr)1412 void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED, bool osr) {
1413   ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
1414   DCHECK(info->IsMethodBeingCompiled(osr));
1415   info->SetIsMethodBeingCompiled(false, osr);
1416 }
1417 
GetMemorySizeOfCodePointer(const void * ptr)1418 size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
1419   MutexLock mu(Thread::Current(), lock_);
1420   return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
1421 }
1422 
InvalidateCompiledCodeFor(ArtMethod * method,const OatQuickMethodHeader * header)1423 void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
1424                                              const OatQuickMethodHeader* header) {
1425   ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
1426   if ((profiling_info != nullptr) &&
1427       (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
1428     // Prevent future uses of the compiled code.
1429     profiling_info->SetSavedEntryPoint(nullptr);
1430   }
1431 
1432   if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) {
1433     // The entrypoint is the one to invalidate, so we just update
1434     // it to the interpreter entry point and clear the counter to get the method
1435     // Jitted again.
1436     Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1437         method, GetQuickToInterpreterBridge());
1438     method->ClearCounter();
1439   } else {
1440     MutexLock mu(Thread::Current(), lock_);
1441     auto it = osr_code_map_.find(method);
1442     if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
1443       // Remove the OSR method, to avoid using it again.
1444       osr_code_map_.erase(it);
1445     }
1446   }
1447 }
1448 
AllocateCode(size_t code_size)1449 uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
1450   size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
1451   uint8_t* result = reinterpret_cast<uint8_t*>(
1452       mspace_memalign(code_mspace_, alignment, code_size));
1453   size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
1454   // Ensure the header ends up at expected instruction alignment.
1455   DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
1456   used_memory_for_code_ += mspace_usable_size(result);
1457   return result;
1458 }
1459 
FreeCode(uint8_t * code)1460 void JitCodeCache::FreeCode(uint8_t* code) {
1461   used_memory_for_code_ -= mspace_usable_size(code);
1462   mspace_free(code_mspace_, code);
1463 }
1464 
AllocateData(size_t data_size)1465 uint8_t* JitCodeCache::AllocateData(size_t data_size) {
1466   void* result = mspace_malloc(data_mspace_, data_size);
1467   used_memory_for_data_ += mspace_usable_size(result);
1468   return reinterpret_cast<uint8_t*>(result);
1469 }
1470 
FreeData(uint8_t * data)1471 void JitCodeCache::FreeData(uint8_t* data) {
1472   used_memory_for_data_ -= mspace_usable_size(data);
1473   mspace_free(data_mspace_, data);
1474 }
1475 
Dump(std::ostream & os)1476 void JitCodeCache::Dump(std::ostream& os) {
1477   MutexLock mu(Thread::Current(), lock_);
1478   os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n"
1479      << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n"
1480      << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n"
1481      << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n"
1482      << "Total number of JIT compilations: " << number_of_compilations_ << "\n"
1483      << "Total number of JIT compilations for on stack replacement: "
1484         << number_of_osr_compilations_ << "\n"
1485      << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl;
1486   histogram_stack_map_memory_use_.PrintMemoryUse(os);
1487   histogram_code_memory_use_.PrintMemoryUse(os);
1488   histogram_profiling_info_memory_use_.PrintMemoryUse(os);
1489 }
1490 
1491 }  // namespace jit
1492 }  // namespace art
1493