• 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 #include "art_method-inl.h"
27 #include "base/compiler_filter.h"
28 #include "base/enums.h"
29 #include "base/logging.h"  // For VLOG.
30 #include "base/scoped_arena_containers.h"
31 #include "base/stl_util.h"
32 #include "base/systrace.h"
33 #include "base/time_utils.h"
34 #include "base/unix_file/fd_file.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 sleep_time = MsToNs(force_early_first_save
140       ? options_.GetMinFirstSaveMs()
141       : options_.GetSaveResolvedClassesDelayMs());
142     const uint64_t start_time = NanoTime();
143     const uint64_t end_time = start_time + sleep_time;
144     while (!Runtime::Current()->GetStartupCompleted() || force_early_first_save) {
145       const uint64_t current_time = NanoTime();
146       if (current_time >= end_time) {
147         break;
148       }
149       period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0);
150     }
151     total_ms_of_sleep_ += NsToMs(NanoTime() - start_time);
152   }
153 
154   FetchAndCacheResolvedClassesAndMethods(/*startup=*/ true);
155 
156   // When we save without waiting for JIT notifications we use a simple
157   // exponential back off policy bounded by max_wait_without_jit.
158   uint32_t max_wait_without_jit = options_.GetMinSavePeriodMs() * 16;
159   uint64_t cur_wait_without_jit = options_.GetMinSavePeriodMs();
160 
161   // Loop for the profiled methods.
162   while (!ShuttingDown(self)) {
163     // Sleep only if we don't have to force an early first save configured
164     // with GetMinFirstSaveMs().
165     // If we do have to save early, move directly to the processing part
166     // since we already slept before fetching and resolving the startup
167     // classes.
168     if (!force_early_first_save) {
169       uint64_t sleep_start = NanoTime();
170       uint64_t sleep_time = 0;
171       {
172         MutexLock mu(self, wait_lock_);
173         if (options_.GetWaitForJitNotificationsToSave()) {
174           period_condition_.Wait(self);
175         } else {
176           period_condition_.TimedWait(self, cur_wait_without_jit, 0);
177           if (cur_wait_without_jit < max_wait_without_jit) {
178             cur_wait_without_jit *= 2;
179           }
180         }
181         sleep_time = NanoTime() - sleep_start;
182       }
183       // Check if the thread was woken up for shutdown.
184       if (ShuttingDown(self)) {
185         break;
186       }
187       total_number_of_wake_ups_++;
188       // We might have been woken up by a huge number of notifications to guarantee saving.
189       // If we didn't meet the minimum saving period go back to sleep (only if missed by
190       // a reasonable margin).
191       uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs());
192       while (min_save_period_ns * 0.9 > sleep_time) {
193         {
194           MutexLock mu(self, wait_lock_);
195           period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0);
196           sleep_time = NanoTime() - sleep_start;
197         }
198         // Check if the thread was woken up for shutdown.
199         if (ShuttingDown(self)) {
200           break;
201         }
202         total_number_of_wake_ups_++;
203       }
204       total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start);
205     }
206 
207     if (ShuttingDown(self)) {
208       break;
209     }
210 
211     uint16_t number_of_new_methods = 0;
212     uint64_t start_work = NanoTime();
213     // If we force an early_first_save do not run FetchAndCacheResolvedClassesAndMethods
214     // again. We just did it. So pass true to skip_class_and_method_fetching.
215     bool profile_saved_to_disk = ProcessProfilingInfo(
216         /*force_save=*/ false,
217         /*skip_class_and_method_fetching=*/ force_early_first_save,
218         &number_of_new_methods);
219 
220     // Reset the flag, so we can continue on the normal schedule.
221     force_early_first_save = false;
222 
223     // Update the notification counter based on result. Note that there might be contention on this
224     // but we don't care about to be 100% precise.
225     if (!profile_saved_to_disk) {
226       // If we didn't save to disk it may be because we didn't have enough new methods.
227       // Set the jit activity notifications to number_of_new_methods so we can wake up earlier
228       // if needed.
229       jit_activity_notifications_ = number_of_new_methods;
230     }
231     total_ns_of_work_ += NanoTime() - start_work;
232   }
233 }
234 
235 // Checks if the profile file is empty.
236 // Return true if the size of the profile file is 0 or if there were errors when
237 // trying to open the file.
IsProfileEmpty(const std::string & location)238 static bool IsProfileEmpty(const std::string& location) {
239   if (location.empty()) {
240     return true;
241   }
242 
243   struct stat stat_buffer;
244   if (stat(location.c_str(), &stat_buffer) != 0) {
245     if (VLOG_IS_ON(profiler)) {
246       PLOG(WARNING) << "Failed to stat profile location for IsFirstUse: " << location;
247     }
248     return true;
249   }
250 
251   VLOG(profiler) << "Profile " << location << " size=" << stat_buffer.st_size;
252   return stat_buffer.st_size == 0;
253 }
254 
IsFirstSave()255 bool ProfileSaver::IsFirstSave() {
256   Thread* self = Thread::Current();
257   SafeMap<std::string, std::string> tracked_locations;
258   {
259     // Make a copy so that we don't hold the lock while doing I/O.
260     MutexLock mu(self, *Locks::profiler_lock_);
261     tracked_locations = tracked_profiles_;
262   }
263 
264   for (const auto& it : tracked_locations) {
265     if (ShuttingDown(self)) {
266       return false;
267     }
268     const std::string& cur_profile = it.first;
269     const std::string& ref_profile = it.second;
270 
271     // Check if any profile is non empty. If so, then this is not the first save.
272     if (!IsProfileEmpty(cur_profile) || !IsProfileEmpty(ref_profile)) {
273       return false;
274     }
275   }
276 
277   // All locations are empty. Assume this is the first use.
278   VLOG(profiler) << "All profile locations are empty. This is considered to be first save";
279   return true;
280 }
281 
NotifyJitActivity()282 void ProfileSaver::NotifyJitActivity() {
283   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
284   if (instance_ == nullptr || instance_->shutting_down_) {
285     return;
286   }
287   instance_->NotifyJitActivityInternal();
288 }
289 
WakeUpSaver()290 void ProfileSaver::WakeUpSaver() {
291   jit_activity_notifications_ = 0;
292   last_time_ns_saver_woke_up_ = NanoTime();
293   period_condition_.Signal(Thread::Current());
294 }
295 
NotifyJitActivityInternal()296 void ProfileSaver::NotifyJitActivityInternal() {
297   // Unlikely to overflow but if it happens,
298   // we would have waken up the saver long before that.
299   jit_activity_notifications_++;
300   // Note that we are not as precise as we could be here but we don't want to wake the saver
301   // every time we see a hot method.
302   if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) {
303     MutexLock wait_mutex(Thread::Current(), wait_lock_);
304     if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) {
305       WakeUpSaver();
306     } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) {
307       // Make sure to wake up the saver if we see a spike in the number of notifications.
308       // This is a precaution to avoid losing a big number of methods in case
309       // this is a spike with no jit after.
310       total_number_of_hot_spikes_++;
311       WakeUpSaver();
312     }
313   }
314 }
315 
316 class ProfileSaver::ScopedDefaultPriority {
317  public:
ScopedDefaultPriority(pthread_t thread)318   explicit ScopedDefaultPriority(pthread_t thread) : thread_(thread) {
319     SetProfileSaverThreadPriority(thread_, GetDefaultThreadPriority());
320   }
321 
~ScopedDefaultPriority()322   ~ScopedDefaultPriority() {
323     SetProfileSaverThreadPriority(thread_, kProfileSaverPthreadPriority);
324   }
325 
326  private:
327   const pthread_t thread_;
328 };
329 
330 class ProfileSaver::GetClassesAndMethodsHelper {
331  public:
GetClassesAndMethodsHelper(bool startup,const ProfileSaverOptions & options,const ProfileCompilationInfo::ProfileSampleAnnotation & annotation)332   GetClassesAndMethodsHelper(bool startup,
333                              const ProfileSaverOptions& options,
334                              const ProfileCompilationInfo::ProfileSampleAnnotation& annotation)
335       REQUIRES_SHARED(Locks::mutator_lock_)
336       : startup_(startup),
337         profile_boot_class_path_(options.GetProfileBootClassPath()),
338         hot_method_sample_threshold_(CalculateHotMethodSampleThreshold(startup, options)),
339         extra_flags_(GetExtraMethodHotnessFlags(options)),
340         annotation_(annotation),
341         arena_stack_(Runtime::Current()->GetArenaPool()),
342         allocator_(&arena_stack_),
343         class_loaders_(std::nullopt),
344         dex_file_records_map_(allocator_.Adapter(kArenaAllocProfile)),
345         number_of_hot_methods_(0u),
346         number_of_sampled_methods_(0u) {
347     std::fill_n(max_primitive_array_dimensions_.data(), max_primitive_array_dimensions_.size(), 0u);
348   }
349 
REQUIRES_SHARED(Locks::mutator_lock_)350   ~GetClassesAndMethodsHelper() REQUIRES_SHARED(Locks::mutator_lock_) {
351     // The `class_loaders_` member destructor needs the mutator lock.
352     // We need to destroy arena-allocated dex file records.
353     for (const auto& entry : dex_file_records_map_) {
354       delete entry.second;
355     }
356   }
357 
358   void CollectClasses(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
359   void UpdateProfile(const std::set<std::string>& locations, ProfileCompilationInfo* profile_info);
360 
GetHotMethodSampleThreshold() const361   uint32_t GetHotMethodSampleThreshold() const {
362     return hot_method_sample_threshold_;
363   }
364 
GetNumberOfHotMethods() const365   size_t GetNumberOfHotMethods() const {
366     return number_of_hot_methods_;
367   }
368 
GetNumberOfSampledMethods() const369   size_t GetNumberOfSampledMethods() const {
370     return number_of_sampled_methods_;
371   }
372 
373  private:
374   // GetClassLoadersVisitor collects visited class loaders.
375   class GetClassLoadersVisitor : public ClassLoaderVisitor {
376    public:
GetClassLoadersVisitor(VariableSizedHandleScope * class_loaders)377     explicit GetClassLoadersVisitor(VariableSizedHandleScope* class_loaders)
378         : class_loaders_(class_loaders) {}
379 
Visit(ObjPtr<mirror::ClassLoader> class_loader)380     void Visit(ObjPtr<mirror::ClassLoader> class_loader)
381         REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
382       DCHECK(class_loader != nullptr);
383       class_loaders_->NewHandle(class_loader);
384     }
385 
386    private:
387     VariableSizedHandleScope* const class_loaders_;
388   };
389 
390   class CollectInternalVisitor {
391    public:
CollectInternalVisitor(GetClassesAndMethodsHelper * helper)392     explicit CollectInternalVisitor(GetClassesAndMethodsHelper* helper)
393         : helper_(helper) {}
394 
VisitRootIfNonNull(StackReference<mirror::Object> * ref)395     void VisitRootIfNonNull(StackReference<mirror::Object>* ref)
396         REQUIRES_SHARED(Locks::mutator_lock_) {
397       if (!ref->IsNull()) {
398         helper_->CollectInternal</*kBootClassLoader=*/ false>(ref->AsMirrorPtr()->AsClassLoader());
399       }
400     }
401 
402    private:
403     GetClassesAndMethodsHelper* helper_;
404   };
405 
406   struct ClassRecord {
407     dex::TypeIndex type_index;
408     uint16_t array_dimension;
409     uint32_t copied_methods_start;
410     LengthPrefixedArray<ArtMethod>* methods;
411   };
412 
413   struct DexFileRecords : public DeletableArenaObject<kArenaAllocProfile> {
DexFileRecordsart::ProfileSaver::GetClassesAndMethodsHelper::DexFileRecords414     explicit DexFileRecords(ScopedArenaAllocator* allocator)
415         : class_records(allocator->Adapter(kArenaAllocProfile)),
416           copied_methods(allocator->Adapter(kArenaAllocProfile)) {
417       class_records.reserve(kInitialClassRecordsReservation);
418     }
419 
420     static constexpr size_t kInitialClassRecordsReservation = 512;
421 
422     ScopedArenaVector<ClassRecord> class_records;
423     ScopedArenaVector<ArtMethod*> copied_methods;
424   };
425 
426   using DexFileRecordsMap = ScopedArenaHashMap<const DexFile*, DexFileRecords*>;
427 
CalculateHotMethodSampleThreshold(bool startup,const ProfileSaverOptions & options)428   static uint32_t CalculateHotMethodSampleThreshold(bool startup,
429                                                     const ProfileSaverOptions& options) {
430     Runtime* runtime = Runtime::Current();
431     if (startup) {
432       const bool is_low_ram = runtime->GetHeap()->IsLowMemoryMode();
433       return options.GetHotStartupMethodSamples(is_low_ram);
434     } else if (runtime->GetJit() != nullptr) {
435       return runtime->GetJit()->WarmMethodThreshold();
436     } else {
437       return std::numeric_limits<uint32_t>::max();
438     }
439   }
440 
ShouldCollectClasses(bool startup)441   ALWAYS_INLINE static bool ShouldCollectClasses(bool startup) {
442     // We only record classes for the startup case. This may change in the future.
443     return startup;
444   }
445 
446   // Collect classes and methods from one class loader.
447   template <bool kBootClassLoader>
448   void CollectInternal(ObjPtr<mirror::ClassLoader> class_loader) NO_INLINE
449       REQUIRES_SHARED(Locks::mutator_lock_);
450 
451   const bool startup_;
452   const bool profile_boot_class_path_;
453   const uint32_t hot_method_sample_threshold_;
454   const uint32_t extra_flags_;
455   const ProfileCompilationInfo::ProfileSampleAnnotation annotation_;
456   ArenaStack arena_stack_;
457   ScopedArenaAllocator allocator_;
458   std::optional<VariableSizedHandleScope> class_loaders_;
459   DexFileRecordsMap dex_file_records_map_;
460 
461   static_assert(Primitive::kPrimLast == Primitive::kPrimVoid);  // There are no arrays of void.
462   std::array<uint8_t, static_cast<size_t>(Primitive::kPrimLast)> max_primitive_array_dimensions_;
463 
464   size_t number_of_hot_methods_;
465   size_t number_of_sampled_methods_;
466 };
467 
468 template <bool kBootClassLoader>
CollectInternal(ObjPtr<mirror::ClassLoader> class_loader)469 void ProfileSaver::GetClassesAndMethodsHelper::CollectInternal(
470     ObjPtr<mirror::ClassLoader> class_loader) {
471   ScopedTrace trace(__PRETTY_FUNCTION__);
472   DCHECK_EQ(kBootClassLoader, class_loader == nullptr);
473 
474   // If the class loader has not loaded any classes, it may have a null table.
475   ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
476   ClassTable* const table =
477       class_linker->ClassTableForClassLoader(kBootClassLoader ? nullptr : class_loader);
478   if (table == nullptr) {
479     return;
480   }
481 
482   // Move members to local variables to allow the compiler to optimize this properly.
483   const bool startup = startup_;
484   table->Visit([&](ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
485     if (kBootClassLoader ? (!klass->IsBootStrapClassLoaded())
486                          : (klass->GetClassLoader() != class_loader)) {
487       // To avoid processing a class more than once, we process each class only
488       // when we encounter it in the defining class loader's class table.
489       // This class has a different defining class loader, skip it.
490       return true;
491     }
492 
493     uint16_t dim = 0u;
494     ObjPtr<mirror::Class> k = klass;
495     if (klass->IsArrayClass()) {
496       DCHECK_EQ(klass->NumMethods(), 0u);  // No methods to collect.
497       if (!ShouldCollectClasses(startup)) {
498         return true;
499       }
500       do {
501         DCHECK(k->IsResolved());  // Array classes are always resolved.
502         ++dim;
503         // At the time of array class creation, the element type is already either
504         // resolved or erroneous unresoved and either shall remain an invariant.
505         // Similarly, the access flag indicating a proxy class is an invariant.
506         // Read barrier is unnecessary for reading a chain of constant references
507         // in order to read primitive fields to check such invariants, or to read
508         // other constant primitive fields (dex file, primitive type) below.
509         k = k->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>();
510       } while (k->IsArrayClass());
511 
512       DCHECK(kBootClassLoader || !k->IsPrimitive());
513       if (kBootClassLoader && UNLIKELY(k->IsPrimitive())) {
514         size_t index = enum_cast<size_t>(k->GetPrimitiveType());
515         DCHECK_LT(index, max_primitive_array_dimensions_.size());
516         if (dim > max_primitive_array_dimensions_[index]) {
517           // Enforce an upper limit of 255 for primitive array dimensions.
518           max_primitive_array_dimensions_[index] =
519               std::min<size_t>(dim, std::numeric_limits<uint8_t>::max());
520         }
521         return true;
522       }
523 
524       // Attribute the array class to the defining dex file of the element class.
525       DCHECK_EQ(klass->GetCopiedMethodsStartOffset(), 0u);
526       DCHECK(klass->GetMethodsPtr() == nullptr);
527     } else {
528       // Non-array class. There is no need to collect primitive types.
529       DCHECK(kBootClassLoader || !k->IsPrimitive());
530       if (kBootClassLoader && UNLIKELY(klass->IsPrimitive())) {
531         DCHECK(profile_boot_class_path_);
532         DCHECK_EQ(klass->NumMethods(), 0u);  // No methods to collect.
533         return true;
534       }
535     }
536 
537     if (!k->IsResolved() || k->IsProxyClass()) {
538       return true;
539     }
540 
541     const DexFile& dex_file = k->GetDexFile();
542     dex::TypeIndex type_index = k->GetDexTypeIndex();
543     uint32_t copied_methods_start = klass->GetCopiedMethodsStartOffset();
544     LengthPrefixedArray<ArtMethod>* methods = klass->GetMethodsPtr();
545 
546     DexFileRecords* dex_file_records;
547     auto it = dex_file_records_map_.find(&dex_file);
548     if (it != dex_file_records_map_.end()) {
549       dex_file_records = it->second;
550     } else {
551       dex_file_records = new (&allocator_) DexFileRecords(&allocator_);
552       dex_file_records_map_.insert(std::make_pair(&dex_file, dex_file_records));
553     }
554     dex_file_records->class_records.push_back(
555         ClassRecord{type_index, dim, copied_methods_start, methods});
556     return true;
557   });
558 }
559 
CollectClasses(Thread * self)560 void ProfileSaver::GetClassesAndMethodsHelper::CollectClasses(Thread* self) {
561   ScopedTrace trace(__PRETTY_FUNCTION__);
562 
563   // Collect class loaders into a `VariableSizedHandleScope` to prevent contention
564   // problems on the class_linker_classes_lock. Hold those class loaders in
565   // a member variable to keep them alive and prevent unloading their classes,
566   // so that methods referenced in collected `DexFileRecords` remain valid.
567   class_loaders_.emplace(self);
568   {
569     GetClassLoadersVisitor class_loader_visitor(&class_loaders_.value());
570     ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
571     ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_);
572     class_linker->VisitClassLoaders(&class_loader_visitor);
573   }
574 
575   // Collect classes and their method array pointers.
576   if (profile_boot_class_path_) {
577     // Collect classes from the boot class loader since visit classloaders doesn't visit it.
578     CollectInternal</*kBootClassLoader=*/ true>(/*class_loader=*/ nullptr);
579   }
580   {
581     CollectInternalVisitor visitor(this);
582     class_loaders_->VisitRoots(visitor);
583   }
584 
585   // Attribute copied methods to defining dex files while holding the mutator lock.
586   for (const auto& entry : dex_file_records_map_) {
587     const DexFile* dex_file = entry.first;
588     DexFileRecords* dex_file_records = entry.second;
589 
590     for (const ClassRecord& class_record : dex_file_records->class_records) {
591       LengthPrefixedArray<ArtMethod>* methods = class_record.methods;
592       if (methods == nullptr) {
593         continue;
594       }
595       const size_t methods_size = methods->size();
596       for (size_t index = class_record.copied_methods_start; index != methods_size; ++index) {
597         // Note: Using `ArtMethod` array with implicit `kRuntimePointerSize`.
598         ArtMethod& method = methods->At(index);
599         DCHECK(method.IsCopied());
600         DCHECK(!method.IsNative());
601         if (method.IsInvokable()) {
602           const DexFile* method_dex_file = method.GetDexFile();
603           DexFileRecords* method_dex_file_records = dex_file_records;
604           if (method_dex_file != dex_file) {
605             auto it = dex_file_records_map_.find(method_dex_file);
606             if (it == dex_file_records_map_.end()) {
607               // We have not seen any class in the dex file that defines the interface with this
608               // copied method. This can happen if the interface is in the boot class path and
609               // we are not profiling boot class path; or when we first visit classes for the
610               // interface's defining class loader before it has any resolved classes and then
611               // the interface is resolved and an implementing class is defined in a child class
612               // loader before we visit that child class loader's classes.
613               continue;
614             }
615             method_dex_file_records = it->second;
616           }
617           method_dex_file_records->copied_methods.push_back(&method);
618         }
619       }
620     }
621   }
622 }
623 
UpdateProfile(const std::set<std::string> & locations,ProfileCompilationInfo * profile_info)624 void ProfileSaver::GetClassesAndMethodsHelper::UpdateProfile(const std::set<std::string>& locations,
625                                                              ProfileCompilationInfo* profile_info) {
626   // Move members to local variables to allow the compiler to optimize this properly.
627   const bool startup = startup_;
628   const uint32_t hot_method_sample_threshold = hot_method_sample_threshold_;
629   const uint32_t base_flags =
630       (startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup) | extra_flags_;
631 
632   // Collect the number of hot and sampled methods.
633   size_t number_of_hot_methods = 0u;
634   size_t number_of_sampled_methods = 0u;
635 
636   uint16_t initial_value = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
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     if (method.PreviouslyWarm() ||
641         method.CounterHasReached(hot_method_sample_threshold, initial_value)) {
642       ++number_of_hot_methods;
643       return enum_cast<ProfileCompilationInfo::MethodHotness::Flag>(base_flags | Hotness::kFlagHot);
644     } else if (method.CounterHasChanged(initial_value)) {
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, ThreadState::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       // Load the existing profile before saving.
873       // If the file is updated between `Load` and `Save`, the update will be lost. This is
874       // acceptable. The main reason is that the lost entries will eventually come back if the user
875       // keeps using the same methods, or they won't be needed if the user doesn't use the same
876       // methods again.
877       if (!info.Load(filename, /*clear_if_invalid=*/true)) {
878         LOG(WARNING) << "Could not forcefully load profile " << filename;
879         continue;
880       }
881 
882       uint64_t last_save_number_of_methods = info.GetNumberOfMethods();
883       uint64_t last_save_number_of_classes = info.GetNumberOfResolvedClasses();
884       VLOG(profiler) << "last_save_number_of_methods=" << last_save_number_of_methods
885                      << " last_save_number_of_classes=" << last_save_number_of_classes
886                      << " number of profiled methods=" << profile_methods.size();
887 
888       // Try to add the method data. Note this may fail is the profile loaded from disk contains
889       // outdated data (e.g. the previous profiled dex files might have been updated).
890       // If this happens we clear the profile data and for the save to ensure the file is cleared.
891       if (!info.AddMethods(
892               profile_methods,
893               AnnotateSampleFlags(Hotness::kFlagHot | Hotness::kFlagPostStartup),
894               GetProfileSampleAnnotation())) {
895         LOG(WARNING) << "Could not add methods to the existing profiler. "
896             << "Clearing the profile data.";
897         info.ClearData();
898         force_save = true;
899       }
900 
901       {
902         MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
903         auto profile_cache_it = profile_cache_.find(filename);
904         if (profile_cache_it != profile_cache_.end()) {
905           if (!info.MergeWith(*(profile_cache_it->second))) {
906             LOG(WARNING) << "Could not merge the profile. Clearing the profile data.";
907             info.ClearData();
908             force_save = true;
909           }
910         } else if (VLOG_IS_ON(profiler)) {
911           LOG(INFO) << "Failed to find cached profile for " << filename;
912           for (auto&& pair : profile_cache_) {
913             LOG(INFO) << "Cached profile " << pair.first;
914           }
915         }
916 
917         int64_t delta_number_of_methods =
918             info.GetNumberOfMethods() - last_save_number_of_methods;
919         int64_t delta_number_of_classes =
920             info.GetNumberOfResolvedClasses() - last_save_number_of_classes;
921 
922         if (!force_save &&
923             delta_number_of_methods < options_.GetMinMethodsToSave() &&
924             delta_number_of_classes < options_.GetMinClassesToSave()) {
925           VLOG(profiler) << "Not enough information to save to: " << filename
926                         << " Number of methods: " << delta_number_of_methods
927                         << " Number of classes: " << delta_number_of_classes;
928           total_number_of_skipped_writes_++;
929           continue;
930         }
931 
932         if (number_of_new_methods != nullptr) {
933           *number_of_new_methods =
934               std::max(static_cast<uint16_t>(delta_number_of_methods),
935                       *number_of_new_methods);
936         }
937         uint64_t bytes_written;
938         // Force the save. In case the profile data is corrupted or the profile
939         // has the wrong version this will "fix" the file to the correct format.
940         if (info.Save(filename, &bytes_written)) {
941           // We managed to save the profile. Clear the cache stored during startup.
942           if (profile_cache_it != profile_cache_.end()) {
943             ProfileCompilationInfo *cached_info = profile_cache_it->second;
944             profile_cache_.erase(profile_cache_it);
945             delete cached_info;
946           }
947           if (bytes_written > 0) {
948             total_number_of_writes_++;
949             total_bytes_written_ += bytes_written;
950             profile_file_saved = true;
951           } else {
952             // At this point we could still have avoided the write.
953             // We load and merge the data from the file lazily at its first ever
954             // save attempt. So, whatever we are trying to save could already be
955             // in the file.
956             total_number_of_skipped_writes_++;
957           }
958         } else {
959           LOG(WARNING) << "Could not save profiling info to " << filename;
960           total_number_of_failed_writes_++;
961         }
962       }
963     }
964   }
965 
966   // Trim the maps to madvise the pages used for profile info.
967   // It is unlikely we will need them again in the near feature.
968   Runtime::Current()->GetArenaPool()->TrimMaps();
969 
970   return profile_file_saved;
971 }
972 
RunProfileSaverThread(void * arg)973 void* ProfileSaver::RunProfileSaverThread(void* arg) {
974   Runtime* runtime = Runtime::Current();
975 
976   bool attached = runtime->AttachCurrentThread("Profile Saver",
977                                                /*as_daemon=*/true,
978                                                runtime->GetSystemThreadGroup(),
979                                                /*create_peer=*/true);
980   if (!attached) {
981     CHECK(runtime->IsShuttingDown(Thread::Current()));
982     return nullptr;
983   }
984 
985   {
986     Locks::profiler_lock_->ExclusiveLock(Thread::Current());
987     CHECK_EQ(reinterpret_cast<ProfileSaver*>(arg), instance_);
988     instance_->Run();
989   }
990 
991   runtime->DetachCurrentThread();
992   VLOG(profiler) << "Profile saver shutdown";
993   return nullptr;
994 }
995 
ShouldProfileLocation(const std::string & location,bool profile_aot_code)996 static bool ShouldProfileLocation(const std::string& location, bool profile_aot_code) {
997   if (profile_aot_code) {
998     // If we have to profile all the code, irrespective of its compilation state, return true
999     // right away.
1000     return true;
1001   }
1002 
1003   OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
1004   const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location);
1005   if (oat_file == nullptr) {
1006     // This can happen if we fallback to run code directly from the APK.
1007     // Profile it with the hope that the background dexopt will get us back into
1008     // a good state.
1009     VLOG(profiler) << "Asked to profile a location without an oat file:" << location;
1010     return true;
1011   }
1012   CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
1013   if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) {
1014     VLOG(profiler)
1015         << "Skip profiling oat file because it's already speed|everything compiled: "
1016         << location << " oat location: " << oat_file->GetLocation();
1017     return false;
1018   }
1019   return true;
1020 }
1021 
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)1022 void  ProfileSaver::Start(const ProfileSaverOptions& options,
1023                           const std::string& output_filename,
1024                           jit::JitCodeCache* jit_code_cache,
1025                           const std::vector<std::string>& code_paths,
1026                           const std::string& ref_profile_filename) {
1027   Runtime* const runtime = Runtime::Current();
1028   DCHECK(options.IsEnabled());
1029   DCHECK(runtime->GetJit() != nullptr);
1030   DCHECK(!output_filename.empty());
1031   DCHECK(jit_code_cache != nullptr);
1032 
1033   std::vector<std::string> code_paths_to_profile;
1034   for (const std::string& location : code_paths) {
1035     if (ShouldProfileLocation(location, options.GetProfileAOTCode()))  {
1036       VLOG(profiler) << "Code path to profile " << location;
1037       code_paths_to_profile.push_back(location);
1038     }
1039   }
1040 
1041   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1042   // Support getting profile samples for the boot class path. This will be used to generate the boot
1043   // image profile. The intention is to use this code to generate to boot image but not use it in
1044   // production. b/37966211
1045   if (options.GetProfileBootClassPath()) {
1046     std::set<std::string> code_paths_keys;
1047     for (const std::string& location : code_paths) {
1048       // Use the profile base key for checking file uniqueness (as it is constructed solely based
1049       // on the location and ignores other metadata like origin package).
1050       code_paths_keys.insert(ProfileCompilationInfo::GetProfileDexFileBaseKey(location));
1051     }
1052     for (const DexFile* dex_file : runtime->GetClassLinker()->GetBootClassPath()) {
1053       // Don't check ShouldProfileLocation since the boot class path may be speed compiled.
1054       const std::string& location = dex_file->GetLocation();
1055       const std::string key = ProfileCompilationInfo::GetProfileDexFileBaseKey(location);
1056       VLOG(profiler) << "Registering boot dex file " << location;
1057       if (code_paths_keys.find(key) != code_paths_keys.end()) {
1058         LOG(WARNING) << "Boot class path location key conflicts with code path " << location;
1059       } else if (instance_ == nullptr) {
1060         // Only add the boot class path once since Start may be called multiple times for secondary
1061         // dexes.
1062         // We still do the collision check above. This handles any secondary dexes that conflict
1063         // with the boot class path dex files.
1064         code_paths_to_profile.push_back(location);
1065       }
1066     }
1067   }
1068   if (code_paths_to_profile.empty()) {
1069     VLOG(profiler) << "No code paths should be profiled.";
1070     return;
1071   }
1072 
1073   if (instance_ != nullptr) {
1074     // If we already have an instance, make sure it uses the same jit_code_cache.
1075     // This may be called multiple times via Runtime::registerAppInfo (e.g. for
1076     // apps which share the same runtime).
1077     DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache);
1078     // Add the code_paths to the tracked locations.
1079     instance_->AddTrackedLocations(output_filename, code_paths_to_profile, ref_profile_filename);
1080     return;
1081   }
1082 
1083   VLOG(profiler) << "Starting profile saver using output file: " << output_filename
1084       << ". Tracking: " << android::base::Join(code_paths_to_profile, ':')
1085       << ". With reference profile: " << ref_profile_filename;
1086 
1087   instance_ = new ProfileSaver(options, jit_code_cache);
1088   instance_->AddTrackedLocations(output_filename, code_paths_to_profile, ref_profile_filename);
1089 
1090   // Create a new thread which does the saving.
1091   CHECK_PTHREAD_CALL(
1092       pthread_create,
1093       (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
1094       "Profile saver thread");
1095 
1096   SetProfileSaverThreadPriority(profiler_pthread_, kProfileSaverPthreadPriority);
1097 }
1098 
Stop(bool dump_info)1099 void ProfileSaver::Stop(bool dump_info) {
1100   ProfileSaver* profile_saver = nullptr;
1101   pthread_t profiler_pthread = 0U;
1102 
1103   {
1104     MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
1105     VLOG(profiler) << "Stopping profile saver thread";
1106     profile_saver = instance_;
1107     profiler_pthread = profiler_pthread_;
1108     if (instance_ == nullptr) {
1109       DCHECK(false) << "Tried to stop a profile saver which was not started";
1110       return;
1111     }
1112     if (instance_->shutting_down_) {
1113       DCHECK(false) << "Tried to stop the profile saver twice";
1114       return;
1115     }
1116     instance_->shutting_down_ = true;
1117   }
1118 
1119   {
1120     // Wake up the saver thread if it is sleeping to allow for a clean exit.
1121     MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
1122     profile_saver->period_condition_.Signal(Thread::Current());
1123   }
1124 
1125   // Force save everything before destroying the thread since we want profiler_pthread_ to remain
1126   // valid.
1127   profile_saver->ProcessProfilingInfo(
1128       /*force_ save=*/ true,
1129       /*skip_class_and_method_fetching=*/ false,
1130       /*number_of_new_methods=*/ nullptr);
1131 
1132   // Wait for the saver thread to stop.
1133   CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
1134 
1135   {
1136     MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
1137     if (dump_info) {
1138       instance_->DumpInfo(LOG_STREAM(INFO));
1139     }
1140     instance_ = nullptr;
1141     profiler_pthread_ = 0U;
1142   }
1143   delete profile_saver;
1144 }
1145 
ShuttingDown(Thread * self)1146 bool ProfileSaver::ShuttingDown(Thread* self) {
1147   MutexLock mu(self, *Locks::profiler_lock_);
1148   return shutting_down_;
1149 }
1150 
IsStarted()1151 bool ProfileSaver::IsStarted() {
1152   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1153   return instance_ != nullptr;
1154 }
1155 
AddTrackedLocationsToMap(const std::string & output_filename,const std::vector<std::string> & code_paths,SafeMap<std::string,std::set<std::string>> * map)1156 static void AddTrackedLocationsToMap(const std::string& output_filename,
1157                                      const std::vector<std::string>& code_paths,
1158                                      SafeMap<std::string, std::set<std::string>>* map) {
1159   std::vector<std::string> code_paths_and_filenames;
1160   // The dex locations are sometimes set to the filename instead of the full path.
1161   // So make sure we have both "locations" when tracking what needs to be profiled.
1162   //   - apps + system server have filenames
1163   //   - boot classpath elements have full paths
1164 
1165   // TODO(calin, ngeoffray, vmarko) This is an workaround for using filanames as
1166   // dex locations - needed to prebuilt with a partial boot image
1167   // (commit: c4a924d8c74241057d957d360bf31cd5cd0e4f9c).
1168   // We should find a better way which allows us to do the tracking based on full paths.
1169   for (const std::string& path : code_paths) {
1170     size_t last_sep_index = path.find_last_of('/');
1171     if (last_sep_index == path.size() - 1) {
1172       // Should not happen, but anyone can register code paths so better be prepared and ignore
1173       // such locations.
1174       continue;
1175     }
1176     std::string filename = last_sep_index == std::string::npos
1177         ? path
1178         : path.substr(last_sep_index + 1);
1179 
1180     code_paths_and_filenames.push_back(path);
1181     code_paths_and_filenames.push_back(filename);
1182   }
1183 
1184   auto it = map->find(output_filename);
1185   if (it == map->end()) {
1186     map->Put(
1187         output_filename,
1188         std::set<std::string>(code_paths_and_filenames.begin(), code_paths_and_filenames.end()));
1189   } else {
1190     it->second.insert(code_paths_and_filenames.begin(), code_paths_and_filenames.end());
1191   }
1192 }
1193 
AddTrackedLocations(const std::string & output_filename,const std::vector<std::string> & code_paths,const std::string & ref_profile_filename)1194 void ProfileSaver::AddTrackedLocations(const std::string& output_filename,
1195                                        const std::vector<std::string>& code_paths,
1196                                        const std::string& ref_profile_filename) {
1197   // Register the output profile and its reference profile.
1198   auto it = tracked_profiles_.find(output_filename);
1199   if (it == tracked_profiles_.end()) {
1200     tracked_profiles_.Put(output_filename, ref_profile_filename);
1201   }
1202 
1203   // Add the code paths to the list of tracked location.
1204   AddTrackedLocationsToMap(output_filename, code_paths, &tracked_dex_base_locations_);
1205   // The code paths may contain symlinks which could fool the profiler.
1206   // If the dex file is compiled with an absolute location but loaded with symlink
1207   // the profiler could skip the dex due to location mismatch.
1208   // To avoid this, we add the code paths to the temporary cache of 'to_be_resolved'
1209   // locations. When the profiler thread executes we will resolve the paths to their
1210   // real paths.
1211   // Note that we delay taking the realpath to avoid spending more time than needed
1212   // when registering location (as it is done during app launch).
1213   AddTrackedLocationsToMap(output_filename,
1214                            code_paths,
1215                            &tracked_dex_base_locations_to_be_resolved_);
1216 }
1217 
DumpInstanceInfo(std::ostream & os)1218 void ProfileSaver::DumpInstanceInfo(std::ostream& os) {
1219   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1220   if (instance_ != nullptr) {
1221     instance_->DumpInfo(os);
1222   }
1223 }
1224 
DumpInfo(std::ostream & os)1225 void ProfileSaver::DumpInfo(std::ostream& os) {
1226   os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n'
1227      << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n'
1228      << "ProfileSaver total_number_of_code_cache_queries="
1229      << total_number_of_code_cache_queries_ << '\n'
1230      << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n'
1231      << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n'
1232      << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n'
1233      << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n'
1234      << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n'
1235      << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n';
1236 }
1237 
1238 
ForceProcessProfiles()1239 void ProfileSaver::ForceProcessProfiles() {
1240   ProfileSaver* saver = nullptr;
1241   {
1242     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1243     saver = instance_;
1244   }
1245   // TODO(calin): this is not actually thread safe as the instance_ may have been deleted,
1246   // but we only use this in testing when we now this won't happen.
1247   // Refactor the way we handle the instance so that we don't end up in this situation.
1248   if (saver != nullptr) {
1249     saver->ProcessProfilingInfo(
1250         /*force_save=*/ true,
1251         /*skip_class_and_method_fetching=*/ false,
1252         /*number_of_new_methods=*/ nullptr);
1253   }
1254 }
1255 
ResolveTrackedLocations()1256 void ProfileSaver::ResolveTrackedLocations() {
1257   SafeMap<std::string, std::set<std::string>> locations_to_be_resolved;
1258   {
1259     // Make a copy so that we don't hold the lock while doing I/O.
1260     MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1261     locations_to_be_resolved = tracked_dex_base_locations_to_be_resolved_;
1262     tracked_dex_base_locations_to_be_resolved_.clear();
1263   }
1264 
1265   // Resolve the locations.
1266   SafeMap<std::string, std::vector<std::string>> resolved_locations_map;
1267   for (const auto& it : locations_to_be_resolved) {
1268     const std::string& filename = it.first;
1269     const std::set<std::string>& locations = it.second;
1270     auto resolved_locations_it = resolved_locations_map.Put(
1271         filename,
1272         std::vector<std::string>(locations.size()));
1273 
1274     for (const auto& location : locations) {
1275       UniqueCPtr<const char[]> location_real(realpath(location.c_str(), nullptr));
1276       // Note that it's ok if we cannot get the real path.
1277       if (location_real != nullptr) {
1278         resolved_locations_it->second.emplace_back(location_real.get());
1279       }
1280     }
1281   }
1282 
1283   // Add the resolved locations to the tracked collection.
1284   MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
1285   for (const auto& it : resolved_locations_map) {
1286     AddTrackedLocationsToMap(it.first, it.second, &tracked_dex_base_locations_);
1287   }
1288 }
1289 
GetProfileSampleAnnotation()1290 ProfileCompilationInfo::ProfileSampleAnnotation ProfileSaver::GetProfileSampleAnnotation() {
1291   // Ideally, this would be cached in the ProfileSaver class, when we start the thread.
1292   // However the profile is initialized before the process package name is set and fixing this
1293   // would require unnecessary complex synchronizations.
1294   std::string package_name = Runtime::Current()->GetProcessPackageName();
1295   if (package_name.empty()) {
1296     package_name = "unknown";
1297   }
1298   // We only use annotation for the boot image profiles. Regular apps do not use the extra
1299   // metadata and as such there is no need to pay the cost (storage and computational)
1300   // that comes with the annotations.
1301   return options_.GetProfileBootClassPath()
1302       ? ProfileCompilationInfo::ProfileSampleAnnotation(package_name)
1303       : ProfileCompilationInfo::ProfileSampleAnnotation::kNone;
1304 }
1305 
GetExtraMethodHotnessFlags(const ProfileSaverOptions & options)1306 uint32_t ProfileSaver::GetExtraMethodHotnessFlags(const ProfileSaverOptions& options) {
1307   // We only add the extra flags for the boot image profile because individual apps do not use
1308   // this information.
1309   if (options.GetProfileBootClassPath()) {
1310     return Is64BitInstructionSet(Runtime::Current()->GetInstructionSet())
1311         ? Hotness::kFlag64bit
1312         : Hotness::kFlag32bit;
1313   } else {
1314     return 0u;
1315   }
1316 }
1317 
AnnotateSampleFlags(uint32_t flags)1318 Hotness::Flag ProfileSaver::AnnotateSampleFlags(uint32_t flags) {
1319   uint32_t extra_flags = GetExtraMethodHotnessFlags(options_);
1320   return static_cast<Hotness::Flag>(flags | extra_flags);
1321 }
1322 
1323 }   // namespace art
1324