• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   uint16_t initial_value = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
638   auto get_method_flags = [&](ArtMethod& method) {
639     // Mark methods as hot if they have more than hot_method_sample_threshold
640     // samples. This means they will get compiled by the compiler driver.
641     if (method.PreviouslyWarm() ||
642         method.CounterHasReached(hot_method_sample_threshold, initial_value)) {
643       ++number_of_hot_methods;
644       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(base_flags | Hotness::kFlagHot);
645     } else if (method.CounterHasChanged(initial_value)) {
646       ++number_of_sampled_methods;
647       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(base_flags);
648     } else {
649       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(0u);
650     }
651   };
652 
653   // Use a single string for array descriptors to avoid too many reallocations.
654   std::string array_class_descriptor;
655 
656   // Process classes and methods.
657   for (const auto& entry : dex_file_records_map_) {
658     const DexFile* dex_file = entry.first;
659     const DexFileRecords* dex_file_records = entry.second;
660 
661     // Check if this is a profiled dex file.
662     const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
663     if (locations.find(base_location) == locations.end()) {
664       continue;
665     }
666 
667     // Get the profile index.
668     ProfileCompilationInfo::ProfileIndexType profile_index =
669         profile_info->FindOrAddDexFile(*dex_file, annotation_);
670     if (profile_index == ProfileCompilationInfo::MaxProfileIndex()) {
671       // Error adding dex file to the `profile_info`.
672       continue;
673     }
674 
675     for (const ClassRecord& class_record : dex_file_records->class_records) {
676       if (class_record.array_dimension != 0u) {
677         DCHECK(ShouldCollectClasses(startup));
678         DCHECK(class_record.methods == nullptr);  // No methods to process.
679         array_class_descriptor.assign(class_record.array_dimension, '[');
680         array_class_descriptor += dex_file->StringByTypeIdx(class_record.type_index);
681         dex::TypeIndex type_index =
682             profile_info->FindOrCreateTypeIndex(*dex_file, array_class_descriptor.c_str());
683         if (type_index.IsValid()) {
684           profile_info->AddClass(profile_index, type_index);
685         }
686       } else {
687         // Non-array class.
688         if (ShouldCollectClasses(startup)) {
689           profile_info->AddClass(profile_index, class_record.type_index);
690         }
691         const size_t num_declared_methods = class_record.copied_methods_start;
692         LengthPrefixedArray<ArtMethod>* methods = class_record.methods;
693         for (size_t index = 0; index != num_declared_methods; ++index) {
694           // Note: Using `ArtMethod` array with implicit `kRuntimePointerSize`.
695           ArtMethod& method = methods->At(index);
696           DCHECK(!method.IsCopied());
697           // We do not record native methods. Once we AOT-compile the app,
698           // all native methods shall have their JNI stubs compiled.
699           if (method.IsInvokable() && !method.IsNative()) {
700             ProfileCompilationInfo::MethodHotness::Flag flags = get_method_flags(method);
701             if (flags != 0u) {
702               profile_info->AddMethod(profile_index, method.GetDexMethodIndex(), flags);
703             }
704           }
705         }
706       }
707     }
708 
709     for (ArtMethod* method : dex_file_records->copied_methods) {
710       DCHECK(method->IsCopied());
711       DCHECK(method->IsInvokable());
712       DCHECK(!method->IsNative());
713       ProfileCompilationInfo::MethodHotness::Flag flags = get_method_flags(*method);
714       if (flags != 0u) {
715         profile_info->AddMethod(profile_index, method->GetDexMethodIndex(), flags);
716       }
717     }
718   }
719 
720   if (profile_boot_class_path_) {
721     // Attribute primitive arrays to the first dex file in the boot class path (should
722     // be core-oj). We collect primitive array types to know the needed dimensions.
723     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
724     DCHECK(!class_linker->GetBootClassPath().empty());
725     const DexFile* dex_file = class_linker->GetBootClassPath().front();
726     ProfileCompilationInfo::ProfileIndexType profile_index =
727         profile_info->FindOrAddDexFile(*dex_file, annotation_);
728     if (profile_index != ProfileCompilationInfo::MaxProfileIndex()) {
729       for (size_t i = 0; i != max_primitive_array_dimensions_.size(); ++i) {
730         size_t max_dim = max_primitive_array_dimensions_[i];
731         // Insert descriptors for all dimensions up to `max_dim`.
732         for (size_t dim = 1; dim <= max_dim; ++dim) {
733           array_class_descriptor.assign(dim, '[');
734           array_class_descriptor += Primitive::Descriptor(enum_cast<Primitive::Type>(i));
735           dex::TypeIndex type_index =
736               profile_info->FindOrCreateTypeIndex(*dex_file, array_class_descriptor.c_str());
737           if (type_index.IsValid()) {
738             profile_info->AddClass(profile_index, type_index);
739           }
740         }
741       }
742     } else {
743       // Error adding dex file to the `profile_info`.
744     }
745   } else {
746     DCHECK(std::all_of(max_primitive_array_dimensions_.begin(),
747                        max_primitive_array_dimensions_.end(),
748                        [](uint8_t dim) { return dim == 0u; }));
749   }
750 
751   // Store the number of hot and sampled methods.
752   number_of_hot_methods_ = number_of_hot_methods;
753   number_of_sampled_methods_ = number_of_sampled_methods;
754 }
755 
FetchAndCacheResolvedClassesAndMethods(bool startup)756 void ProfileSaver::FetchAndCacheResolvedClassesAndMethods(bool startup) {
757   ScopedTrace trace(__PRETTY_FUNCTION__);
758   const uint64_t start_time = NanoTime();
759 
760   // Resolve any new registered locations.
761   ResolveTrackedLocations();
762 
763   Thread* const self = Thread::Current();
764   pthread_t profiler_pthread;
765   {
766     MutexLock mu(self, *Locks::profiler_lock_);
767     profiler_pthread = profiler_pthread_;
768   }
769 
770   uint32_t hot_method_sample_threshold = 0u;
771   size_t number_of_hot_methods = 0u;
772   size_t number_of_sampled_methods = 0u;
773   {
774     // Restore profile saver thread priority while holding the mutator lock. This helps
775     // prevent priority inversions blocking the GC for long periods of time.
776     // Only restore default priority if we are the profile saver thread. Other threads
777     // that call this are threads calling Stop and the signal catcher (for SIGUSR1).
778     std::optional<ScopedDefaultPriority> sdp = std::nullopt;
779     if (pthread_self() == profiler_pthread) {
780       sdp.emplace(profiler_pthread);
781     }
782 
783     ScopedObjectAccess soa(self);
784     GetClassesAndMethodsHelper helper(startup, options_, GetProfileSampleAnnotation());
785     hot_method_sample_threshold = helper.GetHotMethodSampleThreshold();
786     helper.CollectClasses(self);
787 
788     // Release the mutator lock. We shall need to re-acquire the lock for a moment to
789     // destroy the `VariableSizedHandleScope` inside the `helper` which shall be
790     // conveniently handled by destroying `sts`, then `helper` and then `soa`.
791     ScopedThreadSuspension sts(self, ThreadState::kNative);
792     // Get back to the previous thread priority. We shall not increase the priority
793     // for the short time we need to re-acquire mutator lock for `helper` destructor.
794     sdp.reset();
795 
796     MutexLock mu(self, *Locks::profiler_lock_);
797     for (const auto& it : tracked_dex_base_locations_) {
798       const std::string& filename = it.first;
799       auto info_it = profile_cache_.find(filename);
800       if (info_it == profile_cache_.end()) {
801         info_it = profile_cache_.Put(
802             filename,
803             new ProfileCompilationInfo(
804                 Runtime::Current()->GetArenaPool(), options_.GetProfileBootClassPath()));
805       }
806       ProfileCompilationInfo* cached_info = info_it->second;
807 
808       const std::set<std::string>& locations = it.second;
809       VLOG(profiler) << "Locations for " << it.first << " " << android::base::Join(locations, ':');
810       helper.UpdateProfile(locations, cached_info);
811 
812       // Update statistics. Note that a method shall be counted for each
813       // tracked location that covers the dex file where it is defined.
814       number_of_hot_methods += helper.GetNumberOfHotMethods();
815       number_of_sampled_methods += helper.GetNumberOfSampledMethods();
816     }
817   }
818   VLOG(profiler) << "Profile saver recorded " << number_of_hot_methods
819                  << " hot methods and " << number_of_sampled_methods
820                  << " sampled methods with threshold " << hot_method_sample_threshold
821                  << " in " << PrettyDuration(NanoTime() - start_time);
822 }
823 
ProcessProfilingInfo(bool force_save,bool skip_class_and_method_fetching,uint16_t * number_of_new_methods)824 bool ProfileSaver::ProcessProfilingInfo(
825         bool force_save,
826         bool skip_class_and_method_fetching,
827         /*out*/uint16_t* number_of_new_methods) {
828   ScopedTrace trace(__PRETTY_FUNCTION__);
829 
830   // Resolve any new registered locations.
831   ResolveTrackedLocations();
832 
833   SafeMap<std::string, std::set<std::string>> tracked_locations;
834   {
835     // Make a copy so that we don't hold the lock while doing I/O.
836     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
837     tracked_locations = tracked_dex_base_locations_;
838   }
839 
840   bool profile_file_saved = false;
841   if (number_of_new_methods != nullptr) {
842     *number_of_new_methods = 0;
843   }
844 
845   if (!skip_class_and_method_fetching) {
846     // We only need to do this once, not once per dex location.
847     // TODO: Figure out a way to only do it when stuff has changed? It takes 30-50ms.
848     FetchAndCacheResolvedClassesAndMethods(/*startup=*/ false);
849   }
850 
851   for (const auto& it : tracked_locations) {
852     if (!force_save && ShuttingDown(Thread::Current())) {
853       // The ProfileSaver is in shutdown mode, meaning a stop request was made and
854       // we need to exit cleanly (by waiting for the saver thread to finish). Unless
855       // we have a request for a forced save, do not do any processing so that we
856       // speed up the exit.
857       return true;
858     }
859     const std::string& filename = it.first;
860     const std::set<std::string>& locations = it.second;
861     VLOG(profiler) << "Tracked filename " << filename << " locations "
862                    << android::base::Join(locations, ":");
863 
864     std::vector<ProfileMethodInfo> profile_methods;
865     {
866       ScopedObjectAccess soa(Thread::Current());
867       jit_code_cache_->GetProfiledMethods(locations, profile_methods);
868       total_number_of_code_cache_queries_++;
869     }
870     {
871       ProfileCompilationInfo info(Runtime::Current()->GetArenaPool(),
872                                   /*for_boot_image=*/ options_.GetProfileBootClassPath());
873       if (!info.Load(filename, /*clear_if_invalid=*/ true)) {
874         LOG(WARNING) << "Could not forcefully load profile " << filename;
875         continue;
876       }
877       uint64_t last_save_number_of_methods = info.GetNumberOfMethods();
878       uint64_t last_save_number_of_classes = info.GetNumberOfResolvedClasses();
879       VLOG(profiler) << "last_save_number_of_methods=" << last_save_number_of_methods
880                      << " last_save_number_of_classes=" << last_save_number_of_classes
881                      << " number of profiled methods=" << profile_methods.size();
882 
883       // Try to add the method data. Note this may fail is the profile loaded from disk contains
884       // outdated data (e.g. the previous profiled dex files might have been updated).
885       // If this happens we clear the profile data and for the save to ensure the file is cleared.
886       if (!info.AddMethods(
887               profile_methods,
888               AnnotateSampleFlags(Hotness::kFlagHot | Hotness::kFlagPostStartup),
889               GetProfileSampleAnnotation())) {
890         LOG(WARNING) << "Could not add methods to the existing profiler. "
891             << "Clearing the profile data.";
892         info.ClearData();
893         force_save = true;
894       }
895 
896       {
897         MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
898         auto profile_cache_it = profile_cache_.find(filename);
899         if (profile_cache_it != profile_cache_.end()) {
900           if (!info.MergeWith(*(profile_cache_it->second))) {
901             LOG(WARNING) << "Could not merge the profile. Clearing the profile data.";
902             info.ClearData();
903             force_save = true;
904           }
905         } else if (VLOG_IS_ON(profiler)) {
906           LOG(INFO) << "Failed to find cached profile for " << filename;
907           for (auto&& pair : profile_cache_) {
908             LOG(INFO) << "Cached profile " << pair.first;
909           }
910         }
911 
912         int64_t delta_number_of_methods =
913             info.GetNumberOfMethods() - last_save_number_of_methods;
914         int64_t delta_number_of_classes =
915             info.GetNumberOfResolvedClasses() - last_save_number_of_classes;
916 
917         if (!force_save &&
918             delta_number_of_methods < options_.GetMinMethodsToSave() &&
919             delta_number_of_classes < options_.GetMinClassesToSave()) {
920           VLOG(profiler) << "Not enough information to save to: " << filename
921                         << " Number of methods: " << delta_number_of_methods
922                         << " Number of classes: " << delta_number_of_classes;
923           total_number_of_skipped_writes_++;
924           continue;
925         }
926 
927         if (number_of_new_methods != nullptr) {
928           *number_of_new_methods =
929               std::max(static_cast<uint16_t>(delta_number_of_methods),
930                       *number_of_new_methods);
931         }
932         uint64_t bytes_written;
933         // Force the save. In case the profile data is corrupted or the profile
934         // has the wrong version this will "fix" the file to the correct format.
935         if (info.Save(filename, &bytes_written)) {
936           // We managed to save the profile. Clear the cache stored during startup.
937           if (profile_cache_it != profile_cache_.end()) {
938             ProfileCompilationInfo *cached_info = profile_cache_it->second;
939             profile_cache_.erase(profile_cache_it);
940             delete cached_info;
941           }
942           if (bytes_written > 0) {
943             total_number_of_writes_++;
944             total_bytes_written_ += bytes_written;
945             profile_file_saved = true;
946           } else {
947             // At this point we could still have avoided the write.
948             // We load and merge the data from the file lazily at its first ever
949             // save attempt. So, whatever we are trying to save could already be
950             // in the file.
951             total_number_of_skipped_writes_++;
952           }
953         } else {
954           LOG(WARNING) << "Could not save profiling info to " << filename;
955           total_number_of_failed_writes_++;
956         }
957       }
958     }
959   }
960 
961   // Trim the maps to madvise the pages used for profile info.
962   // It is unlikely we will need them again in the near feature.
963   Runtime::Current()->GetArenaPool()->TrimMaps();
964 
965   return profile_file_saved;
966 }
967 
RunProfileSaverThread(void * arg)968 void* ProfileSaver::RunProfileSaverThread(void* arg) {
969   Runtime* runtime = Runtime::Current();
970 
971   bool attached = runtime->AttachCurrentThread("Profile Saver",
972                                                /*as_daemon=*/true,
973                                                runtime->GetSystemThreadGroup(),
974                                                /*create_peer=*/true);
975   if (!attached) {
976     CHECK(runtime->IsShuttingDown(Thread::Current()));
977     return nullptr;
978   }
979 
980   {
981     Locks::profiler_lock_->ExclusiveLock(Thread::Current());
982     CHECK_EQ(reinterpret_cast<ProfileSaver*>(arg), instance_);
983     instance_->Run();
984   }
985 
986   runtime->DetachCurrentThread();
987   VLOG(profiler) << "Profile saver shutdown";
988   return nullptr;
989 }
990 
ShouldProfileLocation(const std::string & location,bool profile_aot_code)991 static bool ShouldProfileLocation(const std::string& location, bool profile_aot_code) {
992   if (profile_aot_code) {
993     // If we have to profile all the code, irrespective of its compilation state, return true
994     // right away.
995     return true;
996   }
997 
998   OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
999   const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location);
1000   if (oat_file == nullptr) {
1001     // This can happen if we fallback to run code directly from the APK.
1002     // Profile it with the hope that the background dexopt will get us back into
1003     // a good state.
1004     VLOG(profiler) << "Asked to profile a location without an oat file:" << location;
1005     return true;
1006   }
1007   CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
1008   if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) {
1009     VLOG(profiler)
1010         << "Skip profiling oat file because it's already speed|everything compiled: "
1011         << location << " oat location: " << oat_file->GetLocation();
1012     return false;
1013   }
1014   return true;
1015 }
1016 
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)1017 void  ProfileSaver::Start(const ProfileSaverOptions& options,
1018                           const std::string& output_filename,
1019                           jit::JitCodeCache* jit_code_cache,
1020                           const std::vector<std::string>& code_paths,
1021                           const std::string& ref_profile_filename) {
1022   Runtime* const runtime = Runtime::Current();
1023   DCHECK(options.IsEnabled());
1024   DCHECK(runtime->GetJit() != nullptr);
1025   DCHECK(!output_filename.empty());
1026   DCHECK(jit_code_cache != nullptr);
1027 
1028   std::vector<std::string> code_paths_to_profile;
1029   for (const std::string& location : code_paths) {
1030     if (ShouldProfileLocation(location, options.GetProfileAOTCode()))  {
1031       VLOG(profiler) << "Code path to profile " << location;
1032       code_paths_to_profile.push_back(location);
1033     }
1034   }
1035 
1036   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1037   // Support getting profile samples for the boot class path. This will be used to generate the boot
1038   // image profile. The intention is to use this code to generate to boot image but not use it in
1039   // production. b/37966211
1040   if (options.GetProfileBootClassPath()) {
1041     std::set<std::string> code_paths_keys;
1042     for (const std::string& location : code_paths) {
1043       // Use the profile base key for checking file uniqueness (as it is constructed solely based
1044       // on the location and ignores other metadata like origin package).
1045       code_paths_keys.insert(ProfileCompilationInfo::GetProfileDexFileBaseKey(location));
1046     }
1047     for (const DexFile* dex_file : runtime->GetClassLinker()->GetBootClassPath()) {
1048       // Don't check ShouldProfileLocation since the boot class path may be speed compiled.
1049       const std::string& location = dex_file->GetLocation();
1050       const std::string key = ProfileCompilationInfo::GetProfileDexFileBaseKey(location);
1051       VLOG(profiler) << "Registering boot dex file " << location;
1052       if (code_paths_keys.find(key) != code_paths_keys.end()) {
1053         LOG(WARNING) << "Boot class path location key conflicts with code path " << location;
1054       } else if (instance_ == nullptr) {
1055         // Only add the boot class path once since Start may be called multiple times for secondary
1056         // dexes.
1057         // We still do the collision check above. This handles any secondary dexes that conflict
1058         // with the boot class path dex files.
1059         code_paths_to_profile.push_back(location);
1060       }
1061     }
1062   }
1063   if (code_paths_to_profile.empty()) {
1064     VLOG(profiler) << "No code paths should be profiled.";
1065     return;
1066   }
1067 
1068   if (instance_ != nullptr) {
1069     // If we already have an instance, make sure it uses the same jit_code_cache.
1070     // This may be called multiple times via Runtime::registerAppInfo (e.g. for
1071     // apps which share the same runtime).
1072     DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache);
1073     // Add the code_paths to the tracked locations.
1074     instance_->AddTrackedLocations(output_filename, code_paths_to_profile, ref_profile_filename);
1075     return;
1076   }
1077 
1078   VLOG(profiler) << "Starting profile saver using output file: " << output_filename
1079       << ". Tracking: " << android::base::Join(code_paths_to_profile, ':')
1080       << ". With reference profile: " << ref_profile_filename;
1081 
1082   instance_ = new ProfileSaver(options, jit_code_cache);
1083   instance_->AddTrackedLocations(output_filename, code_paths_to_profile, ref_profile_filename);
1084 
1085   // Create a new thread which does the saving.
1086   CHECK_PTHREAD_CALL(
1087       pthread_create,
1088       (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
1089       "Profile saver thread");
1090 
1091   SetProfileSaverThreadPriority(profiler_pthread_, kProfileSaverPthreadPriority);
1092 }
1093 
Stop(bool dump_info)1094 void ProfileSaver::Stop(bool dump_info) {
1095   ProfileSaver* profile_saver = nullptr;
1096   pthread_t profiler_pthread = 0U;
1097 
1098   {
1099     MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
1100     VLOG(profiler) << "Stopping profile saver thread";
1101     profile_saver = instance_;
1102     profiler_pthread = profiler_pthread_;
1103     if (instance_ == nullptr) {
1104       DCHECK(false) << "Tried to stop a profile saver which was not started";
1105       return;
1106     }
1107     if (instance_->shutting_down_) {
1108       DCHECK(false) << "Tried to stop the profile saver twice";
1109       return;
1110     }
1111     instance_->shutting_down_ = true;
1112   }
1113 
1114   {
1115     // Wake up the saver thread if it is sleeping to allow for a clean exit.
1116     MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
1117     profile_saver->period_condition_.Signal(Thread::Current());
1118   }
1119 
1120   // Force save everything before destroying the thread since we want profiler_pthread_ to remain
1121   // valid.
1122   profile_saver->ProcessProfilingInfo(
1123       /*force_ save=*/ true,
1124       /*skip_class_and_method_fetching=*/ false,
1125       /*number_of_new_methods=*/ nullptr);
1126 
1127   // Wait for the saver thread to stop.
1128   CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
1129 
1130   {
1131     MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
1132     if (dump_info) {
1133       instance_->DumpInfo(LOG_STREAM(INFO));
1134     }
1135     instance_ = nullptr;
1136     profiler_pthread_ = 0U;
1137   }
1138   delete profile_saver;
1139 }
1140 
ShuttingDown(Thread * self)1141 bool ProfileSaver::ShuttingDown(Thread* self) {
1142   MutexLock mu(self, *Locks::profiler_lock_);
1143   return shutting_down_;
1144 }
1145 
IsStarted()1146 bool ProfileSaver::IsStarted() {
1147   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1148   return instance_ != nullptr;
1149 }
1150 
AddTrackedLocationsToMap(const std::string & output_filename,const std::vector<std::string> & code_paths,SafeMap<std::string,std::set<std::string>> * map)1151 static void AddTrackedLocationsToMap(const std::string& output_filename,
1152                                      const std::vector<std::string>& code_paths,
1153                                      SafeMap<std::string, std::set<std::string>>* map) {
1154   std::vector<std::string> code_paths_and_filenames;
1155   // The dex locations are sometimes set to the filename instead of the full path.
1156   // So make sure we have both "locations" when tracking what needs to be profiled.
1157   //   - apps + system server have filenames
1158   //   - boot classpath elements have full paths
1159 
1160   // TODO(calin, ngeoffray, vmarko) This is an workaround for using filanames as
1161   // dex locations - needed to prebuilt with a partial boot image
1162   // (commit: c4a924d8c74241057d957d360bf31cd5cd0e4f9c).
1163   // We should find a better way which allows us to do the tracking based on full paths.
1164   for (const std::string& path : code_paths) {
1165     size_t last_sep_index = path.find_last_of('/');
1166     if (last_sep_index == path.size() - 1) {
1167       // Should not happen, but anyone can register code paths so better be prepared and ignore
1168       // such locations.
1169       continue;
1170     }
1171     std::string filename = last_sep_index == std::string::npos
1172         ? path
1173         : path.substr(last_sep_index + 1);
1174 
1175     code_paths_and_filenames.push_back(path);
1176     code_paths_and_filenames.push_back(filename);
1177   }
1178 
1179   auto it = map->find(output_filename);
1180   if (it == map->end()) {
1181     map->Put(
1182         output_filename,
1183         std::set<std::string>(code_paths_and_filenames.begin(), code_paths_and_filenames.end()));
1184   } else {
1185     it->second.insert(code_paths_and_filenames.begin(), code_paths_and_filenames.end());
1186   }
1187 }
1188 
AddTrackedLocations(const std::string & output_filename,const std::vector<std::string> & code_paths,const std::string & ref_profile_filename)1189 void ProfileSaver::AddTrackedLocations(const std::string& output_filename,
1190                                        const std::vector<std::string>& code_paths,
1191                                        const std::string& ref_profile_filename) {
1192   // Register the output profile and its reference profile.
1193   auto it = tracked_profiles_.find(output_filename);
1194   if (it == tracked_profiles_.end()) {
1195     tracked_profiles_.Put(output_filename, ref_profile_filename);
1196   }
1197 
1198   // Add the code paths to the list of tracked location.
1199   AddTrackedLocationsToMap(output_filename, code_paths, &tracked_dex_base_locations_);
1200   // The code paths may contain symlinks which could fool the profiler.
1201   // If the dex file is compiled with an absolute location but loaded with symlink
1202   // the profiler could skip the dex due to location mismatch.
1203   // To avoid this, we add the code paths to the temporary cache of 'to_be_resolved'
1204   // locations. When the profiler thread executes we will resolve the paths to their
1205   // real paths.
1206   // Note that we delay taking the realpath to avoid spending more time than needed
1207   // when registering location (as it is done during app launch).
1208   AddTrackedLocationsToMap(output_filename,
1209                            code_paths,
1210                            &tracked_dex_base_locations_to_be_resolved_);
1211 }
1212 
DumpInstanceInfo(std::ostream & os)1213 void ProfileSaver::DumpInstanceInfo(std::ostream& os) {
1214   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1215   if (instance_ != nullptr) {
1216     instance_->DumpInfo(os);
1217   }
1218 }
1219 
DumpInfo(std::ostream & os)1220 void ProfileSaver::DumpInfo(std::ostream& os) {
1221   os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n'
1222      << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n'
1223      << "ProfileSaver total_number_of_code_cache_queries="
1224      << total_number_of_code_cache_queries_ << '\n'
1225      << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n'
1226      << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n'
1227      << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n'
1228      << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n'
1229      << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n'
1230      << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n';
1231 }
1232 
1233 
ForceProcessProfiles()1234 void ProfileSaver::ForceProcessProfiles() {
1235   ProfileSaver* saver = nullptr;
1236   {
1237     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1238     saver = instance_;
1239   }
1240   // TODO(calin): this is not actually thread safe as the instance_ may have been deleted,
1241   // but we only use this in testing when we now this won't happen.
1242   // Refactor the way we handle the instance so that we don't end up in this situation.
1243   if (saver != nullptr) {
1244     saver->ProcessProfilingInfo(
1245         /*force_save=*/ true,
1246         /*skip_class_and_method_fetching=*/ false,
1247         /*number_of_new_methods=*/ nullptr);
1248   }
1249 }
1250 
ResolveTrackedLocations()1251 void ProfileSaver::ResolveTrackedLocations() {
1252   SafeMap<std::string, std::set<std::string>> locations_to_be_resolved;
1253   {
1254     // Make a copy so that we don't hold the lock while doing I/O.
1255     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1256     locations_to_be_resolved = tracked_dex_base_locations_to_be_resolved_;
1257     tracked_dex_base_locations_to_be_resolved_.clear();
1258   }
1259 
1260   // Resolve the locations.
1261   SafeMap<std::string, std::vector<std::string>> resolved_locations_map;
1262   for (const auto& it : locations_to_be_resolved) {
1263     const std::string& filename = it.first;
1264     const std::set<std::string>& locations = it.second;
1265     auto resolved_locations_it = resolved_locations_map.Put(
1266         filename,
1267         std::vector<std::string>(locations.size()));
1268 
1269     for (const auto& location : locations) {
1270       UniqueCPtr<const char[]> location_real(realpath(location.c_str(), nullptr));
1271       // Note that it's ok if we cannot get the real path.
1272       if (location_real != nullptr) {
1273         resolved_locations_it->second.emplace_back(location_real.get());
1274       }
1275     }
1276   }
1277 
1278   // Add the resolved locations to the tracked collection.
1279   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1280   for (const auto& it : resolved_locations_map) {
1281     AddTrackedLocationsToMap(it.first, it.second, &tracked_dex_base_locations_);
1282   }
1283 }
1284 
GetProfileSampleAnnotation()1285 ProfileCompilationInfo::ProfileSampleAnnotation ProfileSaver::GetProfileSampleAnnotation() {
1286   // Ideally, this would be cached in the ProfileSaver class, when we start the thread.
1287   // However the profile is initialized before the process package name is set and fixing this
1288   // would require unnecessary complex synchronizations.
1289   std::string package_name = Runtime::Current()->GetProcessPackageName();
1290   if (package_name.empty()) {
1291     package_name = "unknown";
1292   }
1293   // We only use annotation for the boot image profiles. Regular apps do not use the extra
1294   // metadata and as such there is no need to pay the cost (storage and computational)
1295   // that comes with the annotations.
1296   return options_.GetProfileBootClassPath()
1297       ? ProfileCompilationInfo::ProfileSampleAnnotation(package_name)
1298       : ProfileCompilationInfo::ProfileSampleAnnotation::kNone;
1299 }
1300 
GetExtraMethodHotnessFlags(const ProfileSaverOptions & options)1301 uint32_t ProfileSaver::GetExtraMethodHotnessFlags(const ProfileSaverOptions& options) {
1302   // We only add the extra flags for the boot image profile because individual apps do not use
1303   // this information.
1304   if (options.GetProfileBootClassPath()) {
1305     return Is64BitInstructionSet(Runtime::Current()->GetInstructionSet())
1306         ? Hotness::kFlag64bit
1307         : Hotness::kFlag32bit;
1308   } else {
1309     return 0u;
1310   }
1311 }
1312 
AnnotateSampleFlags(uint32_t flags)1313 Hotness::Flag ProfileSaver::AnnotateSampleFlags(uint32_t flags) {
1314   uint32_t extra_flags = GetExtraMethodHotnessFlags(options_);
1315   return static_cast<Hotness::Flag>(flags | extra_flags);
1316 }
1317 
1318 }   // namespace art
1319