1 /*
2  * Copyright (C) 2015 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 "profile_saver.h"
18 
19 #include <fcntl.h>
20 #include <sys/resource.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include "android-base/strings.h"
26 
27 #include "art_method-inl.h"
28 #include "base/compiler_filter.h"
29 #include "base/enums.h"
30 #include "base/logging.h"  // For VLOG.
31 #include "base/scoped_arena_containers.h"
32 #include "base/stl_util.h"
33 #include "base/systrace.h"
34 #include "base/time_utils.h"
35 #include "class_table-inl.h"
36 #include "dex/dex_file_loader.h"
37 #include "dex_reference_collection.h"
38 #include "gc/collector_type.h"
39 #include "gc/gc_cause.h"
40 #include "jit/jit.h"
41 #include "jit/profiling_info.h"
42 #include "oat_file_manager.h"
43 #include "profile/profile_compilation_info.h"
44 #include "scoped_thread_state_change-inl.h"
45 
46 namespace art {
47 
48 using Hotness = ProfileCompilationInfo::MethodHotness;
49 
50 ProfileSaver* ProfileSaver::instance_ = nullptr;
51 pthread_t ProfileSaver::profiler_pthread_ = 0U;
52 
53 static_assert(ProfileCompilationInfo::kIndividualInlineCacheSize ==
54               InlineCache::kIndividualCacheSize,
55               "InlineCache and ProfileCompilationInfo do not agree on kIndividualCacheSize");
56 
57 // At what priority to schedule the saver threads. 9 is the lowest foreground priority on device.
58 static constexpr int kProfileSaverPthreadPriority = 9;
59 
SetProfileSaverThreadPriority(pthread_t thread,int priority)60 static void SetProfileSaverThreadPriority(pthread_t thread, int priority) {
61 #if defined(ART_TARGET_ANDROID)
62   int result = setpriority(PRIO_PROCESS, pthread_gettid_np(thread), priority);
63   if (result != 0) {
64     LOG(ERROR) << "Failed to setpriority to :" << priority;
65   }
66 #else
67   UNUSED(thread);
68   UNUSED(priority);
69 #endif
70 }
71 
GetDefaultThreadPriority()72 static int GetDefaultThreadPriority() {
73 #if defined(ART_TARGET_ANDROID)
74   pthread_attr_t attr;
75   sched_param param;
76   pthread_attr_init(&attr);
77   pthread_attr_getschedparam(&attr, &param);
78   return param.sched_priority;
79 #else
80   return 0;
81 #endif
82 }
83 
ProfileSaver(const ProfileSaverOptions & options,jit::JitCodeCache * jit_code_cache)84 ProfileSaver::ProfileSaver(const ProfileSaverOptions& options, jit::JitCodeCache* jit_code_cache)
85     : jit_code_cache_(jit_code_cache),
86       shutting_down_(false),
87       last_time_ns_saver_woke_up_(0),
88       jit_activity_notifications_(0),
89       wait_lock_("ProfileSaver wait lock"),
90       period_condition_("ProfileSaver period condition", wait_lock_),
91       total_bytes_written_(0),
92       total_number_of_writes_(0),
93       total_number_of_code_cache_queries_(0),
94       total_number_of_skipped_writes_(0),
95       total_number_of_failed_writes_(0),
96       total_ms_of_sleep_(0),
97       total_ns_of_work_(0),
98       total_number_of_hot_spikes_(0),
99       total_number_of_wake_ups_(0),
100       options_(options) {
101   DCHECK(options_.IsEnabled());
102 }
103 
~ProfileSaver()104 ProfileSaver::~ProfileSaver() {
105   for (auto& it : profile_cache_) {
106     delete it.second;
107   }
108 }
109 
NotifyStartupCompleted()110 void ProfileSaver::NotifyStartupCompleted() {
111   Thread* self = Thread::Current();
112   MutexLock mu(self, *Locks::profiler_lock_);
113   if (instance_ == nullptr || instance_->shutting_down_) {
114     return;
115   }
116   MutexLock mu2(self, instance_->wait_lock_);
117   instance_->period_condition_.Signal(self);
118 }
119 
Run()120 void ProfileSaver::Run() {
121   Thread* self = Thread::Current();
122 
123   // For thread annotalysis, the setup is more complicated than it should be. Run needs to start
124   // under mutex, but should drop it.
125   Locks::profiler_lock_->ExclusiveUnlock(self);
126 
127   bool check_for_first_save =
128       options_.GetMinFirstSaveMs() != ProfileSaverOptions::kMinFirstSaveMsNotSet;
129   bool force_early_first_save = check_for_first_save && IsFirstSave();
130 
131   // Fetch the resolved classes for the app images after sleeping for
132   // options_.GetSaveResolvedClassesDelayMs().
133   // TODO(calin) This only considers the case of the primary profile file.
134   // Anything that gets loaded in the same VM will not have their resolved
135   // classes save (unless they started before the initial saving was done).
136   {
137     MutexLock mu(self, wait_lock_);
138 
139     const uint64_t end_time = NanoTime() + MsToNs(force_early_first_save
140       ? options_.GetMinFirstSaveMs()
141       : options_.GetSaveResolvedClassesDelayMs());
142     while (!Runtime::Current()->GetStartupCompleted()) {
143       const uint64_t current_time = NanoTime();
144       if (current_time >= end_time) {
145         break;
146       }
147       period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0);
148     }
149     total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs();
150   }
151   // Tell the runtime that startup is completed if it has not already been notified.
152   // TODO: We should use another thread to do this in case the profile saver is not running.
153   Runtime::Current()->NotifyStartupCompleted();
154 
155   FetchAndCacheResolvedClassesAndMethods(/*startup=*/ true);
156 
157   // When we save without waiting for JIT notifications we use a simple
158   // exponential back off policy bounded by max_wait_without_jit.
159   uint32_t max_wait_without_jit = options_.GetMinSavePeriodMs() * 16;
160   uint64_t cur_wait_without_jit = options_.GetMinSavePeriodMs();
161 
162   // Loop for the profiled methods.
163   while (!ShuttingDown(self)) {
164     // Sleep only if we don't have to force an early first save configured
165     // with GetMinFirstSaveMs().
166     // If we do have to save early, move directly to the processing part
167     // since we already slept before fetching and resolving the startup
168     // classes.
169     if (!force_early_first_save) {
170       uint64_t sleep_start = NanoTime();
171       uint64_t sleep_time = 0;
172       {
173         MutexLock mu(self, wait_lock_);
174         if (options_.GetWaitForJitNotificationsToSave()) {
175           period_condition_.Wait(self);
176         } else {
177           period_condition_.TimedWait(self, cur_wait_without_jit, 0);
178           if (cur_wait_without_jit < max_wait_without_jit) {
179             cur_wait_without_jit *= 2;
180           }
181         }
182         sleep_time = NanoTime() - sleep_start;
183       }
184       // Check if the thread was woken up for shutdown.
185       if (ShuttingDown(self)) {
186         break;
187       }
188       total_number_of_wake_ups_++;
189       // We might have been woken up by a huge number of notifications to guarantee saving.
190       // If we didn't meet the minimum saving period go back to sleep (only if missed by
191       // a reasonable margin).
192       uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs());
193       while (min_save_period_ns * 0.9 > sleep_time) {
194         {
195           MutexLock mu(self, wait_lock_);
196           period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0);
197           sleep_time = NanoTime() - sleep_start;
198         }
199         // Check if the thread was woken up for shutdown.
200         if (ShuttingDown(self)) {
201           break;
202         }
203         total_number_of_wake_ups_++;
204       }
205       total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start);
206     }
207 
208     if (ShuttingDown(self)) {
209       break;
210     }
211 
212     uint16_t number_of_new_methods = 0;
213     uint64_t start_work = NanoTime();
214     // If we force an early_first_save do not run FetchAndCacheResolvedClassesAndMethods
215     // again. We just did it. So pass true to skip_class_and_method_fetching.
216     bool profile_saved_to_disk = ProcessProfilingInfo(
217         /*force_save=*/ false,
218         /*skip_class_and_method_fetching=*/ force_early_first_save,
219         &number_of_new_methods);
220 
221     // Reset the flag, so we can continue on the normal schedule.
222     force_early_first_save = false;
223 
224     // Update the notification counter based on result. Note that there might be contention on this
225     // but we don't care about to be 100% precise.
226     if (!profile_saved_to_disk) {
227       // If we didn't save to disk it may be because we didn't have enough new methods.
228       // Set the jit activity notifications to number_of_new_methods so we can wake up earlier
229       // if needed.
230       jit_activity_notifications_ = number_of_new_methods;
231     }
232     total_ns_of_work_ += NanoTime() - start_work;
233   }
234 }
235 
236 // Checks if the profile file is empty.
237 // Return true if the size of the profile file is 0 or if there were errors when
238 // trying to open the file.
IsProfileEmpty(const std::string & location)239 static bool IsProfileEmpty(const std::string& location) {
240   if (location.empty()) {
241     return true;
242   }
243 
244   struct stat stat_buffer;
245   if (stat(location.c_str(), &stat_buffer) != 0) {
246     if (VLOG_IS_ON(profiler)) {
247       PLOG(WARNING) << "Failed to stat profile location for IsFirstUse: " << location;
248     }
249     return true;
250   }
251 
252   VLOG(profiler) << "Profile " << location << " size=" << stat_buffer.st_size;
253   return stat_buffer.st_size == 0;
254 }
255 
IsFirstSave()256 bool ProfileSaver::IsFirstSave() {
257   Thread* self = Thread::Current();
258   SafeMap<std::string, std::string> tracked_locations;
259   {
260     // Make a copy so that we don't hold the lock while doing I/O.
261     MutexLock mu(self, *Locks::profiler_lock_);
262     tracked_locations = tracked_profiles_;
263   }
264 
265   for (const auto& it : tracked_locations) {
266     if (ShuttingDown(self)) {
267       return false;
268     }
269     const std::string& cur_profile = it.first;
270     const std::string& ref_profile = it.second;
271 
272     // Check if any profile is non empty. If so, then this is not the first save.
273     if (!IsProfileEmpty(cur_profile) || !IsProfileEmpty(ref_profile)) {
274       return false;
275     }
276   }
277 
278   // All locations are empty. Assume this is the first use.
279   VLOG(profiler) << "All profile locations are empty. This is considered to be first save";
280   return true;
281 }
282 
NotifyJitActivity()283 void ProfileSaver::NotifyJitActivity() {
284   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
285   if (instance_ == nullptr || instance_->shutting_down_) {
286     return;
287   }
288   instance_->NotifyJitActivityInternal();
289 }
290 
WakeUpSaver()291 void ProfileSaver::WakeUpSaver() {
292   jit_activity_notifications_ = 0;
293   last_time_ns_saver_woke_up_ = NanoTime();
294   period_condition_.Signal(Thread::Current());
295 }
296 
NotifyJitActivityInternal()297 void ProfileSaver::NotifyJitActivityInternal() {
298   // Unlikely to overflow but if it happens,
299   // we would have waken up the saver long before that.
300   jit_activity_notifications_++;
301   // Note that we are not as precise as we could be here but we don't want to wake the saver
302   // every time we see a hot method.
303   if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) {
304     MutexLock wait_mutex(Thread::Current(), wait_lock_);
305     if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) {
306       WakeUpSaver();
307     } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) {
308       // Make sure to wake up the saver if we see a spike in the number of notifications.
309       // This is a precaution to avoid losing a big number of methods in case
310       // this is a spike with no jit after.
311       total_number_of_hot_spikes_++;
312       WakeUpSaver();
313     }
314   }
315 }
316 
317 class ProfileSaver::ScopedDefaultPriority {
318  public:
ScopedDefaultPriority(pthread_t thread)319   explicit ScopedDefaultPriority(pthread_t thread) : thread_(thread) {
320     SetProfileSaverThreadPriority(thread_, GetDefaultThreadPriority());
321   }
322 
~ScopedDefaultPriority()323   ~ScopedDefaultPriority() {
324     SetProfileSaverThreadPriority(thread_, kProfileSaverPthreadPriority);
325   }
326 
327  private:
328   const pthread_t thread_;
329 };
330 
331 class ProfileSaver::GetClassesAndMethodsHelper {
332  public:
GetClassesAndMethodsHelper(bool startup,const ProfileSaverOptions & options,const ProfileCompilationInfo::ProfileSampleAnnotation & annotation)333   GetClassesAndMethodsHelper(bool startup,
334                              const ProfileSaverOptions& options,
335                              const ProfileCompilationInfo::ProfileSampleAnnotation& annotation)
336       REQUIRES_SHARED(Locks::mutator_lock_)
337       : startup_(startup),
338         profile_boot_class_path_(options.GetProfileBootClassPath()),
339         hot_method_sample_threshold_(CalculateHotMethodSampleThreshold(startup, options)),
340         extra_flags_(GetExtraMethodHotnessFlags(options)),
341         annotation_(annotation),
342         arena_stack_(Runtime::Current()->GetArenaPool()),
343         allocator_(&arena_stack_),
344         class_loaders_(std::nullopt),
345         dex_file_records_map_(allocator_.Adapter(kArenaAllocProfile)),
346         number_of_hot_methods_(0u),
347         number_of_sampled_methods_(0u) {
348     std::fill_n(max_primitive_array_dimensions_.data(), max_primitive_array_dimensions_.size(), 0u);
349   }
350 
REQUIRES_SHARED(Locks::mutator_lock_)351   ~GetClassesAndMethodsHelper() REQUIRES_SHARED(Locks::mutator_lock_) {
352     // The `class_loaders_` member destructor needs the mutator lock.
353     // We need to destroy arena-allocated dex file records.
354     for (const auto& entry : dex_file_records_map_) {
355       delete entry.second;
356     }
357   }
358 
359   void CollectClasses(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
360   void UpdateProfile(const std::set<std::string>& locations, ProfileCompilationInfo* profile_info);
361 
GetHotMethodSampleThreshold() const362   uint32_t GetHotMethodSampleThreshold() const {
363     return hot_method_sample_threshold_;
364   }
365 
GetNumberOfHotMethods() const366   size_t GetNumberOfHotMethods() const {
367     return number_of_hot_methods_;
368   }
369 
GetNumberOfSampledMethods() const370   size_t GetNumberOfSampledMethods() const {
371     return number_of_sampled_methods_;
372   }
373 
374  private:
375   // GetClassLoadersVisitor collects visited class loaders.
376   class GetClassLoadersVisitor : public ClassLoaderVisitor {
377    public:
GetClassLoadersVisitor(VariableSizedHandleScope * class_loaders)378     explicit GetClassLoadersVisitor(VariableSizedHandleScope* class_loaders)
379         : class_loaders_(class_loaders) {}
380 
Visit(ObjPtr<mirror::ClassLoader> class_loader)381     void Visit(ObjPtr<mirror::ClassLoader> class_loader)
382         REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
383       DCHECK(class_loader != nullptr);
384       class_loaders_->NewHandle(class_loader);
385     }
386 
387    private:
388     VariableSizedHandleScope* const class_loaders_;
389   };
390 
391   class CollectInternalVisitor {
392    public:
CollectInternalVisitor(GetClassesAndMethodsHelper * helper)393     explicit CollectInternalVisitor(GetClassesAndMethodsHelper* helper)
394         : helper_(helper) {}
395 
VisitRootIfNonNull(StackReference<mirror::Object> * ref)396     void VisitRootIfNonNull(StackReference<mirror::Object>* ref)
397         REQUIRES_SHARED(Locks::mutator_lock_) {
398       if (!ref->IsNull()) {
399         helper_->CollectInternal</*kBootClassLoader=*/ false>(ref->AsMirrorPtr()->AsClassLoader());
400       }
401     }
402 
403    private:
404     GetClassesAndMethodsHelper* helper_;
405   };
406 
407   struct ClassRecord {
408     dex::TypeIndex type_index;
409     uint16_t array_dimension;
410     uint32_t copied_methods_start;
411     LengthPrefixedArray<ArtMethod>* methods;
412   };
413 
414   struct DexFileRecords : public DeletableArenaObject<kArenaAllocProfile> {
DexFileRecordsart::ProfileSaver::GetClassesAndMethodsHelper::DexFileRecords415     explicit DexFileRecords(ScopedArenaAllocator* allocator)
416         : class_records(allocator->Adapter(kArenaAllocProfile)),
417           copied_methods(allocator->Adapter(kArenaAllocProfile)) {
418       class_records.reserve(kInitialClassRecordsReservation);
419     }
420 
421     static constexpr size_t kInitialClassRecordsReservation = 512;
422 
423     ScopedArenaVector<ClassRecord> class_records;
424     ScopedArenaVector<ArtMethod*> copied_methods;
425   };
426 
427   using DexFileRecordsMap = ScopedArenaHashMap<const DexFile*, DexFileRecords*>;
428 
CalculateHotMethodSampleThreshold(bool startup,const ProfileSaverOptions & options)429   static uint32_t CalculateHotMethodSampleThreshold(bool startup,
430                                                     const ProfileSaverOptions& options) {
431     Runtime* runtime = Runtime::Current();
432     if (startup) {
433       const bool is_low_ram = runtime->GetHeap()->IsLowMemoryMode();
434       return options.GetHotStartupMethodSamples(is_low_ram);
435     } else if (runtime->GetJit() != nullptr) {
436       return runtime->GetJit()->WarmMethodThreshold();
437     } else {
438       return std::numeric_limits<uint32_t>::max();
439     }
440   }
441 
ShouldCollectClasses(bool startup)442   ALWAYS_INLINE static bool ShouldCollectClasses(bool startup) {
443     // We only record classes for the startup case. This may change in the future.
444     return startup;
445   }
446 
447   // Collect classes and methods from one class loader.
448   template <bool kBootClassLoader>
449   void CollectInternal(ObjPtr<mirror::ClassLoader> class_loader) NO_INLINE
450       REQUIRES_SHARED(Locks::mutator_lock_);
451 
452   const bool startup_;
453   const bool profile_boot_class_path_;
454   const uint32_t hot_method_sample_threshold_;
455   const uint32_t extra_flags_;
456   const ProfileCompilationInfo::ProfileSampleAnnotation annotation_;
457   ArenaStack arena_stack_;
458   ScopedArenaAllocator allocator_;
459   std::optional<VariableSizedHandleScope> class_loaders_;
460   DexFileRecordsMap dex_file_records_map_;
461 
462   static_assert(Primitive::kPrimLast == Primitive::kPrimVoid);  // There are no arrays of void.
463   std::array<uint8_t, static_cast<size_t>(Primitive::kPrimLast)> max_primitive_array_dimensions_;
464 
465   size_t number_of_hot_methods_;
466   size_t number_of_sampled_methods_;
467 };
468 
469 template <bool kBootClassLoader>
CollectInternal(ObjPtr<mirror::ClassLoader> class_loader)470 void ProfileSaver::GetClassesAndMethodsHelper::CollectInternal(
471     ObjPtr<mirror::ClassLoader> class_loader) {
472   ScopedTrace trace(__PRETTY_FUNCTION__);
473   DCHECK_EQ(kBootClassLoader, class_loader == nullptr);
474 
475   // If the class loader has not loaded any classes, it may have a null table.
476   ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
477   ClassTable* const table =
478       class_linker->ClassTableForClassLoader(kBootClassLoader ? nullptr : class_loader);
479   if (table == nullptr) {
480     return;
481   }
482 
483   // Move members to local variables to allow the compiler to optimize this properly.
484   const bool startup = startup_;
485   table->Visit([&](ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
486     if (kBootClassLoader ? (!klass->IsBootStrapClassLoaded())
487                          : (klass->GetClassLoader() != class_loader)) {
488       // To avoid processing a class more than once, we process each class only
489       // when we encounter it in the defining class loader's class table.
490       // This class has a different defining class loader, skip it.
491       return true;
492     }
493 
494     uint16_t dim = 0u;
495     ObjPtr<mirror::Class> k = klass;
496     if (klass->IsArrayClass()) {
497       DCHECK_EQ(klass->NumMethods(), 0u);  // No methods to collect.
498       if (!ShouldCollectClasses(startup)) {
499         return true;
500       }
501       do {
502         DCHECK(k->IsResolved());  // Array classes are always resolved.
503         ++dim;
504         // At the time of array class creation, the element type is already either
505         // resolved or erroneous unresoved and either shall remain an invariant.
506         // Similarly, the access flag indicating a proxy class is an invariant.
507         // Read barrier is unnecessary for reading a chain of constant references
508         // in order to read primitive fields to check such invariants, or to read
509         // other constant primitive fields (dex file, primitive type) below.
510         k = k->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>();
511       } while (k->IsArrayClass());
512 
513       DCHECK(kBootClassLoader || !k->IsPrimitive());
514       if (kBootClassLoader && UNLIKELY(k->IsPrimitive())) {
515         size_t index = enum_cast<size_t>(k->GetPrimitiveType());
516         DCHECK_LT(index, max_primitive_array_dimensions_.size());
517         if (dim > max_primitive_array_dimensions_[index]) {
518           // Enforce an upper limit of 255 for primitive array dimensions.
519           max_primitive_array_dimensions_[index] =
520               std::min<size_t>(dim, std::numeric_limits<uint8_t>::max());
521         }
522         return true;
523       }
524 
525       // Attribute the array class to the defining dex file of the element class.
526       DCHECK_EQ(klass->GetCopiedMethodsStartOffset(), 0u);
527       DCHECK(klass->GetMethodsPtr() == nullptr);
528     } else {
529       // Non-array class. There is no need to collect primitive types.
530       DCHECK(kBootClassLoader || !k->IsPrimitive());
531       if (kBootClassLoader && UNLIKELY(klass->IsPrimitive())) {
532         DCHECK(profile_boot_class_path_);
533         DCHECK_EQ(klass->NumMethods(), 0u);  // No methods to collect.
534         return true;
535       }
536     }
537 
538     if (!k->IsResolved() || k->IsProxyClass()) {
539       return true;
540     }
541 
542     const DexFile& dex_file = k->GetDexFile();
543     dex::TypeIndex type_index = k->GetDexTypeIndex();
544     uint32_t copied_methods_start = klass->GetCopiedMethodsStartOffset();
545     LengthPrefixedArray<ArtMethod>* methods = klass->GetMethodsPtr();
546 
547     DexFileRecords* dex_file_records;
548     auto it = dex_file_records_map_.find(&dex_file);
549     if (it != dex_file_records_map_.end()) {
550       dex_file_records = it->second;
551     } else {
552       dex_file_records = new (&allocator_) DexFileRecords(&allocator_);
553       dex_file_records_map_.insert(std::make_pair(&dex_file, dex_file_records));
554     }
555     dex_file_records->class_records.push_back(
556         ClassRecord{type_index, dim, copied_methods_start, methods});
557     return true;
558   });
559 }
560 
CollectClasses(Thread * self)561 void ProfileSaver::GetClassesAndMethodsHelper::CollectClasses(Thread* self) {
562   ScopedTrace trace(__PRETTY_FUNCTION__);
563 
564   // Collect class loaders into a `VariableSizedHandleScope` to prevent contention
565   // problems on the class_linker_classes_lock. Hold those class loaders in
566   // a member variable to keep them alive and prevent unloading their classes,
567   // so that methods referenced in collected `DexFileRecords` remain valid.
568   class_loaders_.emplace(self);
569   {
570     GetClassLoadersVisitor class_loader_visitor(&class_loaders_.value());
571     ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
572     ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
573     class_linker->VisitClassLoaders(&class_loader_visitor);
574   }
575 
576   // Collect classes and their method array pointers.
577   if (profile_boot_class_path_) {
578     // Collect classes from the boot class loader since visit classloaders doesn't visit it.
579     CollectInternal</*kBootClassLoader=*/ true>(/*class_loader=*/ nullptr);
580   }
581   {
582     CollectInternalVisitor visitor(this);
583     class_loaders_->VisitRoots(visitor);
584   }
585 
586   // Attribute copied methods to defining dex files while holding the mutator lock.
587   for (const auto& entry : dex_file_records_map_) {
588     const DexFile* dex_file = entry.first;
589     DexFileRecords* dex_file_records = entry.second;
590 
591     for (const ClassRecord& class_record : dex_file_records->class_records) {
592       LengthPrefixedArray<ArtMethod>* methods = class_record.methods;
593       if (methods == nullptr) {
594         continue;
595       }
596       const size_t methods_size = methods->size();
597       for (size_t index = class_record.copied_methods_start; index != methods_size; ++index) {
598         // Note: Using `ArtMethod` array with implicit `kRuntimePointerSize`.
599         ArtMethod& method = methods->At(index);
600         DCHECK(method.IsCopied());
601         DCHECK(!method.IsNative());
602         if (method.IsInvokable()) {
603           const DexFile* method_dex_file = method.GetDexFile();
604           DexFileRecords* method_dex_file_records = dex_file_records;
605           if (method_dex_file != dex_file) {
606             auto it = dex_file_records_map_.find(method_dex_file);
607             if (it == dex_file_records_map_.end()) {
608               // We have not seen any class in the dex file that defines the interface with this
609               // copied method. This can happen if the interface is in the boot class path and
610               // we are not profiling boot class path; or when we first visit classes for the
611               // interface's defining class loader before it has any resolved classes and then
612               // the interface is resolved and an implementing class is defined in a child class
613               // loader before we visit that child class loader's classes.
614               continue;
615             }
616             method_dex_file_records = it->second;
617           }
618           method_dex_file_records->copied_methods.push_back(&method);
619         }
620       }
621     }
622   }
623 }
624 
UpdateProfile(const std::set<std::string> & locations,ProfileCompilationInfo * profile_info)625 void ProfileSaver::GetClassesAndMethodsHelper::UpdateProfile(const std::set<std::string>& locations,
626                                                              ProfileCompilationInfo* profile_info) {
627   // Move members to local variables to allow the compiler to optimize this properly.
628   const bool startup = startup_;
629   const uint32_t hot_method_sample_threshold = hot_method_sample_threshold_;
630   const uint32_t base_flags =
631       (startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup) | extra_flags_;
632 
633   // Collect the number of hot and sampled methods.
634   size_t number_of_hot_methods = 0u;
635   size_t number_of_sampled_methods = 0u;
636 
637   auto get_method_flags = [&](ArtMethod& method) {
638     // Mark methods as hot if they have more than hot_method_sample_threshold
639     // samples. This means they will get compiled by the compiler driver.
640     const uint16_t counter = method.GetCounter();
641     if (method.PreviouslyWarm() || counter >= hot_method_sample_threshold) {
642       ++number_of_hot_methods;
643       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(base_flags | Hotness::kFlagHot);
644     } else if (counter != 0u) {
645       ++number_of_sampled_methods;
646       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(base_flags);
647     } else {
648       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(0u);
649     }
650   };
651 
652   // Use a single string for array descriptors to avoid too many reallocations.
653   std::string array_class_descriptor;
654 
655   // Process classes and methods.
656   for (const auto& entry : dex_file_records_map_) {
657     const DexFile* dex_file = entry.first;
658     const DexFileRecords* dex_file_records = entry.second;
659 
660     // Check if this is a profiled dex file.
661     const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
662     if (locations.find(base_location) == locations.end()) {
663       continue;
664     }
665 
666     // Get the profile index.
667     ProfileCompilationInfo::ProfileIndexType profile_index =
668         profile_info->FindOrAddDexFile(*dex_file, annotation_);
669     if (profile_index == ProfileCompilationInfo::MaxProfileIndex()) {
670       // Error adding dex file to the `profile_info`.
671       continue;
672     }
673 
674     for (const ClassRecord& class_record : dex_file_records->class_records) {
675       if (class_record.array_dimension != 0u) {
676         DCHECK(ShouldCollectClasses(startup));
677         DCHECK(class_record.methods == nullptr);  // No methods to process.
678         array_class_descriptor.assign(class_record.array_dimension, '[');
679         array_class_descriptor += dex_file->StringByTypeIdx(class_record.type_index);
680         dex::TypeIndex type_index =
681             profile_info->FindOrCreateTypeIndex(*dex_file, array_class_descriptor.c_str());
682         if (type_index.IsValid()) {
683           profile_info->AddClass(profile_index, type_index);
684         }
685       } else {
686         // Non-array class.
687         if (ShouldCollectClasses(startup)) {
688           profile_info->AddClass(profile_index, class_record.type_index);
689         }
690         const size_t num_declared_methods = class_record.copied_methods_start;
691         LengthPrefixedArray<ArtMethod>* methods = class_record.methods;
692         for (size_t index = 0; index != num_declared_methods; ++index) {
693           // Note: Using `ArtMethod` array with implicit `kRuntimePointerSize`.
694           ArtMethod& method = methods->At(index);
695           DCHECK(!method.IsCopied());
696           // We do not record native methods. Once we AOT-compile the app,
697           // all native methods shall have their JNI stubs compiled.
698           if (method.IsInvokable() && !method.IsNative()) {
699             ProfileCompilationInfo::MethodHotness::Flag flags = get_method_flags(method);
700             if (flags != 0u) {
701               profile_info->AddMethod(profile_index, method.GetDexMethodIndex(), flags);
702             }
703           }
704         }
705       }
706     }
707 
708     for (ArtMethod* method : dex_file_records->copied_methods) {
709       DCHECK(method->IsCopied());
710       DCHECK(method->IsInvokable());
711       DCHECK(!method->IsNative());
712       ProfileCompilationInfo::MethodHotness::Flag flags = get_method_flags(*method);
713       if (flags != 0u) {
714         profile_info->AddMethod(profile_index, method->GetDexMethodIndex(), flags);
715       }
716     }
717   }
718 
719   if (profile_boot_class_path_) {
720     // Attribute primitive arrays to the first dex file in the boot class path (should
721     // be core-oj). We collect primitive array types to know the needed dimensions.
722     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
723     DCHECK(!class_linker->GetBootClassPath().empty());
724     const DexFile* dex_file = class_linker->GetBootClassPath().front();
725     ProfileCompilationInfo::ProfileIndexType profile_index =
726         profile_info->FindOrAddDexFile(*dex_file, annotation_);
727     if (profile_index != ProfileCompilationInfo::MaxProfileIndex()) {
728       for (size_t i = 0; i != max_primitive_array_dimensions_.size(); ++i) {
729         size_t max_dim = max_primitive_array_dimensions_[i];
730         // Insert descriptors for all dimensions up to `max_dim`.
731         for (size_t dim = 1; dim <= max_dim; ++dim) {
732           array_class_descriptor.assign(dim, '[');
733           array_class_descriptor += Primitive::Descriptor(enum_cast<Primitive::Type>(i));
734           dex::TypeIndex type_index =
735               profile_info->FindOrCreateTypeIndex(*dex_file, array_class_descriptor.c_str());
736           if (type_index.IsValid()) {
737             profile_info->AddClass(profile_index, type_index);
738           }
739         }
740       }
741     } else {
742       // Error adding dex file to the `profile_info`.
743     }
744   } else {
745     DCHECK(std::all_of(max_primitive_array_dimensions_.begin(),
746                        max_primitive_array_dimensions_.end(),
747                        [](uint8_t dim) { return dim == 0u; }));
748   }
749 
750   // Store the number of hot and sampled methods.
751   number_of_hot_methods_ = number_of_hot_methods;
752   number_of_sampled_methods_ = number_of_sampled_methods;
753 }
754 
FetchAndCacheResolvedClassesAndMethods(bool startup)755 void ProfileSaver::FetchAndCacheResolvedClassesAndMethods(bool startup) {
756   ScopedTrace trace(__PRETTY_FUNCTION__);
757   const uint64_t start_time = NanoTime();
758 
759   // Resolve any new registered locations.
760   ResolveTrackedLocations();
761 
762   Thread* const self = Thread::Current();
763   pthread_t profiler_pthread;
764   {
765     MutexLock mu(self, *Locks::profiler_lock_);
766     profiler_pthread = profiler_pthread_;
767   }
768 
769   uint32_t hot_method_sample_threshold = 0u;
770   size_t number_of_hot_methods = 0u;
771   size_t number_of_sampled_methods = 0u;
772   {
773     // Restore profile saver thread priority while holding the mutator lock. This helps
774     // prevent priority inversions blocking the GC for long periods of time.
775     // Only restore default priority if we are the profile saver thread. Other threads
776     // that call this are threads calling Stop and the signal catcher (for SIGUSR1).
777     std::optional<ScopedDefaultPriority> sdp = std::nullopt;
778     if (pthread_self() == profiler_pthread) {
779       sdp.emplace(profiler_pthread);
780     }
781 
782     ScopedObjectAccess soa(self);
783     GetClassesAndMethodsHelper helper(startup, options_, GetProfileSampleAnnotation());
784     hot_method_sample_threshold = helper.GetHotMethodSampleThreshold();
785     helper.CollectClasses(self);
786 
787     // Release the mutator lock. We shall need to re-acquire the lock for a moment to
788     // destroy the `VariableSizedHandleScope` inside the `helper` which shall be
789     // conveniently handled by destroying `sts`, then `helper` and then `soa`.
790     ScopedThreadSuspension sts(self, kNative);
791     // Get back to the previous thread priority. We shall not increase the priority
792     // for the short time we need to re-acquire mutator lock for `helper` destructor.
793     sdp.reset();
794 
795     MutexLock mu(self, *Locks::profiler_lock_);
796     for (const auto& it : tracked_dex_base_locations_) {
797       const std::string& filename = it.first;
798       auto info_it = profile_cache_.find(filename);
799       if (info_it == profile_cache_.end()) {
800         info_it = profile_cache_.Put(
801             filename,
802             new ProfileCompilationInfo(
803                 Runtime::Current()->GetArenaPool(), options_.GetProfileBootClassPath()));
804       }
805       ProfileCompilationInfo* cached_info = info_it->second;
806 
807       const std::set<std::string>& locations = it.second;
808       VLOG(profiler) << "Locations for " << it.first << " " << android::base::Join(locations, ':');
809       helper.UpdateProfile(locations, cached_info);
810 
811       // Update statistics. Note that a method shall be counted for each
812       // tracked location that covers the dex file where it is defined.
813       number_of_hot_methods += helper.GetNumberOfHotMethods();
814       number_of_sampled_methods += helper.GetNumberOfSampledMethods();
815     }
816   }
817   VLOG(profiler) << "Profile saver recorded " << number_of_hot_methods
818                  << " hot methods and " << number_of_sampled_methods
819                  << " sampled methods with threshold " << hot_method_sample_threshold
820                  << " in " << PrettyDuration(NanoTime() - start_time);
821 }
822 
ProcessProfilingInfo(bool force_save,bool skip_class_and_method_fetching,uint16_t * number_of_new_methods)823 bool ProfileSaver::ProcessProfilingInfo(
824         bool force_save,
825         bool skip_class_and_method_fetching,
826         /*out*/uint16_t* number_of_new_methods) {
827   ScopedTrace trace(__PRETTY_FUNCTION__);
828 
829   // Resolve any new registered locations.
830   ResolveTrackedLocations();
831 
832   SafeMap<std::string, std::set<std::string>> tracked_locations;
833   {
834     // Make a copy so that we don't hold the lock while doing I/O.
835     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
836     tracked_locations = tracked_dex_base_locations_;
837   }
838 
839   bool profile_file_saved = false;
840   if (number_of_new_methods != nullptr) {
841     *number_of_new_methods = 0;
842   }
843 
844   if (!skip_class_and_method_fetching) {
845     // We only need to do this once, not once per dex location.
846     // TODO: Figure out a way to only do it when stuff has changed? It takes 30-50ms.
847     FetchAndCacheResolvedClassesAndMethods(/*startup=*/ false);
848   }
849 
850   for (const auto& it : tracked_locations) {
851     if (!force_save && ShuttingDown(Thread::Current())) {
852       // The ProfileSaver is in shutdown mode, meaning a stop request was made and
853       // we need to exit cleanly (by waiting for the saver thread to finish). Unless
854       // we have a request for a forced save, do not do any processing so that we
855       // speed up the exit.
856       return true;
857     }
858     const std::string& filename = it.first;
859     const std::set<std::string>& locations = it.second;
860     VLOG(profiler) << "Tracked filename " << filename << " locations "
861                    << android::base::Join(locations, ":");
862 
863     std::vector<ProfileMethodInfo> profile_methods;
864     {
865       ScopedObjectAccess soa(Thread::Current());
866       jit_code_cache_->GetProfiledMethods(locations, profile_methods);
867       total_number_of_code_cache_queries_++;
868     }
869     {
870       ProfileCompilationInfo info(Runtime::Current()->GetArenaPool(),
871                                   /*for_boot_image=*/ options_.GetProfileBootClassPath());
872       if (!info.Load(filename, /*clear_if_invalid=*/ true)) {
873         LOG(WARNING) << "Could not forcefully load profile " << filename;
874         continue;
875       }
876       uint64_t last_save_number_of_methods = info.GetNumberOfMethods();
877       uint64_t last_save_number_of_classes = info.GetNumberOfResolvedClasses();
878       VLOG(profiler) << "last_save_number_of_methods=" << last_save_number_of_methods
879                      << " last_save_number_of_classes=" << last_save_number_of_classes
880                      << " number of profiled methods=" << profile_methods.size();
881 
882       // Try to add the method data. Note this may fail is the profile loaded from disk contains
883       // outdated data (e.g. the previous profiled dex files might have been updated).
884       // If this happens we clear the profile data and for the save to ensure the file is cleared.
885       if (!info.AddMethods(
886               profile_methods,
887               AnnotateSampleFlags(Hotness::kFlagHot | Hotness::kFlagPostStartup),
888               GetProfileSampleAnnotation())) {
889         LOG(WARNING) << "Could not add methods to the existing profiler. "
890             << "Clearing the profile data.";
891         info.ClearData();
892         force_save = true;
893       }
894 
895       {
896         MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
897         auto profile_cache_it = profile_cache_.find(filename);
898         if (profile_cache_it != profile_cache_.end()) {
899           if (!info.MergeWith(*(profile_cache_it->second))) {
900             LOG(WARNING) << "Could not merge the profile. Clearing the profile data.";
901             info.ClearData();
902             force_save = true;
903           }
904         } else if (VLOG_IS_ON(profiler)) {
905           LOG(INFO) << "Failed to find cached profile for " << filename;
906           for (auto&& pair : profile_cache_) {
907             LOG(INFO) << "Cached profile " << pair.first;
908           }
909         }
910 
911         int64_t delta_number_of_methods =
912             info.GetNumberOfMethods() - last_save_number_of_methods;
913         int64_t delta_number_of_classes =
914             info.GetNumberOfResolvedClasses() - last_save_number_of_classes;
915 
916         if (!force_save &&
917             delta_number_of_methods < options_.GetMinMethodsToSave() &&
918             delta_number_of_classes < options_.GetMinClassesToSave()) {
919           VLOG(profiler) << "Not enough information to save to: " << filename
920                         << " Number of methods: " << delta_number_of_methods
921                         << " Number of classes: " << delta_number_of_classes;
922           total_number_of_skipped_writes_++;
923           continue;
924         }
925 
926         if (number_of_new_methods != nullptr) {
927           *number_of_new_methods =
928               std::max(static_cast<uint16_t>(delta_number_of_methods),
929                       *number_of_new_methods);
930         }
931         uint64_t bytes_written;
932         // Force the save. In case the profile data is corrupted or the profile
933         // has the wrong version this will "fix" the file to the correct format.
934         if (info.Save(filename, &bytes_written)) {
935           // We managed to save the profile. Clear the cache stored during startup.
936           if (profile_cache_it != profile_cache_.end()) {
937             ProfileCompilationInfo *cached_info = profile_cache_it->second;
938             profile_cache_.erase(profile_cache_it);
939             delete cached_info;
940           }
941           if (bytes_written > 0) {
942             total_number_of_writes_++;
943             total_bytes_written_ += bytes_written;
944             profile_file_saved = true;
945           } else {
946             // At this point we could still have avoided the write.
947             // We load and merge the data from the file lazily at its first ever
948             // save attempt. So, whatever we are trying to save could already be
949             // in the file.
950             total_number_of_skipped_writes_++;
951           }
952         } else {
953           LOG(WARNING) << "Could not save profiling info to " << filename;
954           total_number_of_failed_writes_++;
955         }
956       }
957     }
958   }
959 
960   // Trim the maps to madvise the pages used for profile info.
961   // It is unlikely we will need them again in the near feature.
962   Runtime::Current()->GetArenaPool()->TrimMaps();
963 
964   return profile_file_saved;
965 }
966 
RunProfileSaverThread(void * arg)967 void* ProfileSaver::RunProfileSaverThread(void* arg) {
968   Runtime* runtime = Runtime::Current();
969 
970   bool attached = runtime->AttachCurrentThread("Profile Saver",
971                                                /*as_daemon=*/true,
972                                                runtime->GetSystemThreadGroup(),
973                                                /*create_peer=*/true);
974   if (!attached) {
975     CHECK(runtime->IsShuttingDown(Thread::Current()));
976     return nullptr;
977   }
978 
979   {
980     Locks::profiler_lock_->ExclusiveLock(Thread::Current());
981     CHECK_EQ(reinterpret_cast<ProfileSaver*>(arg), instance_);
982     instance_->Run();
983   }
984 
985   runtime->DetachCurrentThread();
986   VLOG(profiler) << "Profile saver shutdown";
987   return nullptr;
988 }
989 
ShouldProfileLocation(const std::string & location,bool profile_aot_code)990 static bool ShouldProfileLocation(const std::string& location, bool profile_aot_code) {
991   if (profile_aot_code) {
992     // If we have to profile all the code, irrespective of its compilation state, return true
993     // right away.
994     return true;
995   }
996 
997   OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
998   const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location);
999   if (oat_file == nullptr) {
1000     // This can happen if we fallback to run code directly from the APK.
1001     // Profile it with the hope that the background dexopt will get us back into
1002     // a good state.
1003     VLOG(profiler) << "Asked to profile a location without an oat file:" << location;
1004     return true;
1005   }
1006   CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
1007   if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) {
1008     VLOG(profiler)
1009         << "Skip profiling oat file because it's already speed|everything compiled: "
1010         << location << " oat location: " << oat_file->GetLocation();
1011     return false;
1012   }
1013   return true;
1014 }
1015 
Start(const ProfileSaverOptions & options,const std::string & output_filename,jit::JitCodeCache * jit_code_cache,const std::vector<std::string> & code_paths,const std::string & ref_profile_filename)1016 void  ProfileSaver::Start(const ProfileSaverOptions& options,
1017                           const std::string& output_filename,
1018                           jit::JitCodeCache* jit_code_cache,
1019                           const std::vector<std::string>& code_paths,
1020                           const std::string& ref_profile_filename) {
1021   Runtime* const runtime = Runtime::Current();
1022   DCHECK(options.IsEnabled());
1023   DCHECK(runtime->GetJit() != nullptr);
1024   DCHECK(!output_filename.empty());
1025   DCHECK(jit_code_cache != nullptr);
1026 
1027   std::vector<std::string> code_paths_to_profile;
1028   for (const std::string& location : code_paths) {
1029     if (ShouldProfileLocation(location, options.GetProfileAOTCode()))  {
1030       VLOG(profiler) << "Code path to profile " << location;
1031       code_paths_to_profile.push_back(location);
1032     }
1033   }
1034 
1035   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1036   // Support getting profile samples for the boot class path. This will be used to generate the boot
1037   // image profile. The intention is to use this code to generate to boot image but not use it in
1038   // production. b/37966211
1039   if (options.GetProfileBootClassPath()) {
1040     std::set<std::string> code_paths_keys;
1041     for (const std::string& location : code_paths) {
1042       // Use the profile base key for checking file uniqueness (as it is constructed solely based
1043       // on the location and ignores other metadata like origin package).
1044       code_paths_keys.insert(ProfileCompilationInfo::GetProfileDexFileBaseKey(location));
1045     }
1046     for (const DexFile* dex_file : runtime->GetClassLinker()->GetBootClassPath()) {
1047       // Don't check ShouldProfileLocation since the boot class path may be speed compiled.
1048       const std::string& location = dex_file->GetLocation();
1049       const std::string key = ProfileCompilationInfo::GetProfileDexFileBaseKey(location);
1050       VLOG(profiler) << "Registering boot dex file " << location;
1051       if (code_paths_keys.find(key) != code_paths_keys.end()) {
1052         LOG(WARNING) << "Boot class path location key conflicts with code path " << location;
1053       } else if (instance_ == nullptr) {
1054         // Only add the boot class path once since Start may be called multiple times for secondary
1055         // dexes.
1056         // We still do the collision check above. This handles any secondary dexes that conflict
1057         // with the boot class path dex files.
1058         code_paths_to_profile.push_back(location);
1059       }
1060     }
1061   }
1062   if (code_paths_to_profile.empty()) {
1063     VLOG(profiler) << "No code paths should be profiled.";
1064     return;
1065   }
1066 
1067   if (instance_ != nullptr) {
1068     // If we already have an instance, make sure it uses the same jit_code_cache.
1069     // This may be called multiple times via Runtime::registerAppInfo (e.g. for
1070     // apps which share the same runtime).
1071     DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache);
1072     // Add the code_paths to the tracked locations.
1073     instance_->AddTrackedLocations(output_filename, code_paths_to_profile, ref_profile_filename);
1074     return;
1075   }
1076 
1077   VLOG(profiler) << "Starting profile saver using output file: " << output_filename
1078       << ". Tracking: " << android::base::Join(code_paths_to_profile, ':')
1079       << ". With reference profile: " << ref_profile_filename;
1080 
1081   instance_ = new ProfileSaver(options, jit_code_cache);
1082   instance_->AddTrackedLocations(output_filename, code_paths_to_profile, ref_profile_filename);
1083 
1084   // Create a new thread which does the saving.
1085   CHECK_PTHREAD_CALL(
1086       pthread_create,
1087       (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
1088       "Profile saver thread");
1089 
1090   SetProfileSaverThreadPriority(profiler_pthread_, kProfileSaverPthreadPriority);
1091 }
1092 
Stop(bool dump_info)1093 void ProfileSaver::Stop(bool dump_info) {
1094   ProfileSaver* profile_saver = nullptr;
1095   pthread_t profiler_pthread = 0U;
1096 
1097   {
1098     MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
1099     VLOG(profiler) << "Stopping profile saver thread";
1100     profile_saver = instance_;
1101     profiler_pthread = profiler_pthread_;
1102     if (instance_ == nullptr) {
1103       DCHECK(false) << "Tried to stop a profile saver which was not started";
1104       return;
1105     }
1106     if (instance_->shutting_down_) {
1107       DCHECK(false) << "Tried to stop the profile saver twice";
1108       return;
1109     }
1110     instance_->shutting_down_ = true;
1111   }
1112 
1113   {
1114     // Wake up the saver thread if it is sleeping to allow for a clean exit.
1115     MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
1116     profile_saver->period_condition_.Signal(Thread::Current());
1117   }
1118 
1119   // Force save everything before destroying the thread since we want profiler_pthread_ to remain
1120   // valid.
1121   profile_saver->ProcessProfilingInfo(
1122       /*force_ save=*/ true,
1123       /*skip_class_and_method_fetching=*/ false,
1124       /*number_of_new_methods=*/ nullptr);
1125 
1126   // Wait for the saver thread to stop.
1127   CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
1128 
1129   {
1130     MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
1131     if (dump_info) {
1132       instance_->DumpInfo(LOG_STREAM(INFO));
1133     }
1134     instance_ = nullptr;
1135     profiler_pthread_ = 0U;
1136   }
1137   delete profile_saver;
1138 }
1139 
ShuttingDown(Thread * self)1140 bool ProfileSaver::ShuttingDown(Thread* self) {
1141   MutexLock mu(self, *Locks::profiler_lock_);
1142   return shutting_down_;
1143 }
1144 
IsStarted()1145 bool ProfileSaver::IsStarted() {
1146   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1147   return instance_ != nullptr;
1148 }
1149 
AddTrackedLocationsToMap(const std::string & output_filename,const std::vector<std::string> & code_paths,SafeMap<std::string,std::set<std::string>> * map)1150 static void AddTrackedLocationsToMap(const std::string& output_filename,
1151                                      const std::vector<std::string>& code_paths,
1152                                      SafeMap<std::string, std::set<std::string>>* map) {
1153   std::vector<std::string> code_paths_and_filenames;
1154   // The dex locations are sometimes set to the filename instead of the full path.
1155   // So make sure we have both "locations" when tracking what needs to be profiled.
1156   //   - apps + system server have filenames
1157   //   - boot classpath elements have full paths
1158 
1159   // TODO(calin, ngeoffray, vmarko) This is an workaround for using filanames as
1160   // dex locations - needed to prebuilt with a partial boot image
1161   // (commit: c4a924d8c74241057d957d360bf31cd5cd0e4f9c).
1162   // We should find a better way which allows us to do the tracking based on full paths.
1163   for (const std::string& path : code_paths) {
1164     size_t last_sep_index = path.find_last_of('/');
1165     if (last_sep_index == path.size() - 1) {
1166       // Should not happen, but anyone can register code paths so better be prepared and ignore
1167       // such locations.
1168       continue;
1169     }
1170     std::string filename = last_sep_index == std::string::npos
1171         ? path
1172         : path.substr(last_sep_index + 1);
1173 
1174     code_paths_and_filenames.push_back(path);
1175     code_paths_and_filenames.push_back(filename);
1176   }
1177 
1178   auto it = map->find(output_filename);
1179   if (it == map->end()) {
1180     map->Put(
1181         output_filename,
1182         std::set<std::string>(code_paths_and_filenames.begin(), code_paths_and_filenames.end()));
1183   } else {
1184     it->second.insert(code_paths_and_filenames.begin(), code_paths_and_filenames.end());
1185   }
1186 }
1187 
AddTrackedLocations(const std::string & output_filename,const std::vector<std::string> & code_paths,const std::string & ref_profile_filename)1188 void ProfileSaver::AddTrackedLocations(const std::string& output_filename,
1189                                        const std::vector<std::string>& code_paths,
1190                                        const std::string& ref_profile_filename) {
1191   // Register the output profile and its reference profile.
1192   auto it = tracked_profiles_.find(output_filename);
1193   if (it == tracked_profiles_.end()) {
1194     tracked_profiles_.Put(output_filename, ref_profile_filename);
1195   }
1196 
1197   // Add the code paths to the list of tracked location.
1198   AddTrackedLocationsToMap(output_filename, code_paths, &tracked_dex_base_locations_);
1199   // The code paths may contain symlinks which could fool the profiler.
1200   // If the dex file is compiled with an absolute location but loaded with symlink
1201   // the profiler could skip the dex due to location mismatch.
1202   // To avoid this, we add the code paths to the temporary cache of 'to_be_resolved'
1203   // locations. When the profiler thread executes we will resolve the paths to their
1204   // real paths.
1205   // Note that we delay taking the realpath to avoid spending more time than needed
1206   // when registering location (as it is done during app launch).
1207   AddTrackedLocationsToMap(output_filename,
1208                            code_paths,
1209                            &tracked_dex_base_locations_to_be_resolved_);
1210 }
1211 
DumpInstanceInfo(std::ostream & os)1212 void ProfileSaver::DumpInstanceInfo(std::ostream& os) {
1213   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1214   if (instance_ != nullptr) {
1215     instance_->DumpInfo(os);
1216   }
1217 }
1218 
DumpInfo(std::ostream & os)1219 void ProfileSaver::DumpInfo(std::ostream& os) {
1220   os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n'
1221      << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n'
1222      << "ProfileSaver total_number_of_code_cache_queries="
1223      << total_number_of_code_cache_queries_ << '\n'
1224      << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n'
1225      << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n'
1226      << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n'
1227      << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n'
1228      << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n'
1229      << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n';
1230 }
1231 
1232 
ForceProcessProfiles()1233 void ProfileSaver::ForceProcessProfiles() {
1234   ProfileSaver* saver = nullptr;
1235   {
1236     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1237     saver = instance_;
1238   }
1239   // TODO(calin): this is not actually thread safe as the instance_ may have been deleted,
1240   // but we only use this in testing when we now this won't happen.
1241   // Refactor the way we handle the instance so that we don't end up in this situation.
1242   if (saver != nullptr) {
1243     saver->ProcessProfilingInfo(
1244         /*force_save=*/ true,
1245         /*skip_class_and_method_fetching=*/ false,
1246         /*number_of_new_methods=*/ nullptr);
1247   }
1248 }
1249 
ResolveTrackedLocations()1250 void ProfileSaver::ResolveTrackedLocations() {
1251   SafeMap<std::string, std::set<std::string>> locations_to_be_resolved;
1252   {
1253     // Make a copy so that we don't hold the lock while doing I/O.
1254     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1255     locations_to_be_resolved = tracked_dex_base_locations_to_be_resolved_;
1256     tracked_dex_base_locations_to_be_resolved_.clear();
1257   }
1258 
1259   // Resolve the locations.
1260   SafeMap<std::string, std::vector<std::string>> resolved_locations_map;
1261   for (const auto& it : locations_to_be_resolved) {
1262     const std::string& filename = it.first;
1263     const std::set<std::string>& locations = it.second;
1264     auto resolved_locations_it = resolved_locations_map.Put(
1265         filename,
1266         std::vector<std::string>(locations.size()));
1267 
1268     for (const auto& location : locations) {
1269       UniqueCPtr<const char[]> location_real(realpath(location.c_str(), nullptr));
1270       // Note that it's ok if we cannot get the real path.
1271       if (location_real != nullptr) {
1272         resolved_locations_it->second.emplace_back(location_real.get());
1273       }
1274     }
1275   }
1276 
1277   // Add the resolved locations to the tracked collection.
1278   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1279   for (const auto& it : resolved_locations_map) {
1280     AddTrackedLocationsToMap(it.first, it.second, &tracked_dex_base_locations_);
1281   }
1282 }
1283 
GetProfileSampleAnnotation()1284 ProfileCompilationInfo::ProfileSampleAnnotation ProfileSaver::GetProfileSampleAnnotation() {
1285   // Ideally, this would be cached in the ProfileSaver class, when we start the thread.
1286   // However the profile is initialized before the process package name is set and fixing this
1287   // would require unnecessary complex synchronizations.
1288   std::string package_name = Runtime::Current()->GetProcessPackageName();
1289   if (package_name.empty()) {
1290     package_name = "unknown";
1291   }
1292   // We only use annotation for the boot image profiles. Regular apps do not use the extra
1293   // metadata and as such there is no need to pay the cost (storage and computational)
1294   // that comes with the annotations.
1295   return options_.GetProfileBootClassPath()
1296       ? ProfileCompilationInfo::ProfileSampleAnnotation(package_name)
1297       : ProfileCompilationInfo::ProfileSampleAnnotation::kNone;
1298 }
1299 
GetExtraMethodHotnessFlags(const ProfileSaverOptions & options)1300 uint32_t ProfileSaver::GetExtraMethodHotnessFlags(const ProfileSaverOptions& options) {
1301   // We only add the extra flags for the boot image profile because individual apps do not use
1302   // this information.
1303   if (options.GetProfileBootClassPath()) {
1304     return Is64BitInstructionSet(Runtime::Current()->GetInstructionSet())
1305         ? Hotness::kFlag64bit
1306         : Hotness::kFlag32bit;
1307   } else {
1308     return 0u;
1309   }
1310 }
1311 
AnnotateSampleFlags(uint32_t flags)1312 Hotness::Flag ProfileSaver::AnnotateSampleFlags(uint32_t flags) {
1313   uint32_t extra_flags = GetExtraMethodHotnessFlags(options_);
1314   return static_cast<Hotness::Flag>(flags | extra_flags);
1315 }
1316 
1317 }   // namespace art
1318