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 #ifndef ART_LIBPROFILE_PROFILE_PROFILE_COMPILATION_INFO_H_
18 #define ART_LIBPROFILE_PROFILE_PROFILE_COMPILATION_INFO_H_
19 
20 #include <array>
21 #include <list>
22 #include <set>
23 #include <string_view>
24 #include <vector>
25 
26 #include "base/arena_containers.h"
27 #include "base/arena_object.h"
28 #include "base/array_ref.h"
29 #include "base/atomic.h"
30 #include "base/bit_memory_region.h"
31 #include "base/hash_map.h"
32 #include "base/hash_set.h"
33 #include "base/malloc_arena_pool.h"
34 #include "base/mem_map.h"
35 #include "base/safe_map.h"
36 #include "dex/dex_file.h"
37 #include "dex/dex_file_types.h"
38 #include "dex/method_reference.h"
39 #include "dex/type_reference.h"
40 
41 namespace art {
42 
43 /**
44  *  Convenient class to pass around profile information (including inline caches)
45  *  without the need to hold GC-able objects.
46  */
47 struct ProfileMethodInfo {
48   struct ProfileInlineCache {
49     ProfileInlineCache(uint32_t pc,
50                        bool missing_types,
51                        const std::vector<TypeReference>& profile_classes,
52                        // Only used by profman for creating profiles from text
53                        bool megamorphic = false)
dex_pcProfileMethodInfo::ProfileInlineCache54         : dex_pc(pc),
55           is_missing_types(missing_types),
56           classes(profile_classes),
57           is_megamorphic(megamorphic) {}
58 
59     const uint32_t dex_pc;
60     const bool is_missing_types;
61     // TODO: Replace `TypeReference` with `dex::TypeIndex` and allow artificial
62     // type indexes for types without a `dex::TypeId` in any dex file processed
63     // by the profman. See `ProfileCompilationInfo::FindOrCreateTypeIndex()`.
64     const std::vector<TypeReference> classes;
65     const bool is_megamorphic;
66   };
67 
ProfileMethodInfoProfileMethodInfo68   explicit ProfileMethodInfo(MethodReference reference) : ref(reference) {}
69 
ProfileMethodInfoProfileMethodInfo70   ProfileMethodInfo(MethodReference reference, const std::vector<ProfileInlineCache>& caches)
71       : ref(reference),
72         inline_caches(caches) {}
73 
74   MethodReference ref;
75   std::vector<ProfileInlineCache> inline_caches;
76 };
77 
78 class FlattenProfileData;
79 
80 /**
81  * Profile information in a format suitable to be queried by the compiler and
82  * performing profile guided compilation.
83  * It is a serialize-friendly format based on information collected by the
84  * interpreter (ProfileInfo).
85  * Currently it stores only the hot compiled methods.
86  */
87 class ProfileCompilationInfo {
88  public:
89   static const uint8_t kProfileMagic[];
90   static const uint8_t kProfileVersion[];
91   static const uint8_t kProfileVersionForBootImage[];
92   static const char kDexMetadataProfileEntry[];
93 
94   static constexpr size_t kProfileVersionSize = 4;
95   static constexpr uint8_t kIndividualInlineCacheSize = 5;
96 
97   // Data structures for encoding the offline representation of inline caches.
98   // This is exposed as public in order to make it available to dex2oat compilations
99   // (see compiler/optimizing/inliner.cc).
100 
101   // The type used to manipulate the profile index of dex files.
102   // It sets an upper limit to how many dex files a given profile can record.
103   using ProfileIndexType = uint16_t;
104 
105   // Encodes a class reference in the profile.
106   // The owning dex file is encoded as the index (dex_profile_index) it has in the
107   // profile rather than as a full reference (location, checksum).
108   // This avoids excessive string copying when managing the profile data.
109   // The dex_profile_index is an index in the `DexFileData::profile_index` (internal use)
110   // and a matching dex file can found with `FindDexFileForProfileIndex()`.
111   // Note that the dex_profile_index is not necessary the multidex index.
112   // We cannot rely on the actual multidex index because a single profile may store
113   // data from multiple splits. This means that a profile may contain a classes2.dex from split-A
114   // and one from split-B.
115   struct ClassReference : public ValueObject {
ClassReferenceClassReference116     ClassReference(ProfileIndexType dex_profile_idx, const dex::TypeIndex type_idx) :
117       dex_profile_index(dex_profile_idx), type_index(type_idx) {}
118 
119     bool operator==(const ClassReference& other) const {
120       return dex_profile_index == other.dex_profile_index && type_index == other.type_index;
121     }
122     bool operator<(const ClassReference& other) const {
123       return dex_profile_index == other.dex_profile_index
124           ? type_index < other.type_index
125           : dex_profile_index < other.dex_profile_index;
126     }
127 
128     ProfileIndexType dex_profile_index;  // the index of the owning dex in the profile info
129     dex::TypeIndex type_index;  // the type index of the class
130   };
131 
132   // Encodes the actual inline cache for a given dex pc (whether or not the receiver is
133   // megamorphic and its possible types).
134   // If the receiver is megamorphic or is missing types the set of classes will be empty.
135   struct DexPcData : public ArenaObject<kArenaAllocProfile> {
DexPcDataDexPcData136     explicit DexPcData(ArenaAllocator* allocator)
137         : DexPcData(allocator->Adapter(kArenaAllocProfile)) {}
DexPcDataDexPcData138     explicit DexPcData(const ArenaAllocatorAdapter<void>& allocator)
139         : is_missing_types(false),
140           is_megamorphic(false),
141           classes(std::less<dex::TypeIndex>(), allocator) {}
142     void AddClass(const dex::TypeIndex& type_idx);
SetIsMegamorphicDexPcData143     void SetIsMegamorphic() {
144       if (is_missing_types) return;
145       is_megamorphic = true;
146       classes.clear();
147     }
SetIsMissingTypesDexPcData148     void SetIsMissingTypes() {
149       is_megamorphic = false;
150       is_missing_types = true;
151       classes.clear();
152     }
153     bool operator==(const DexPcData& other) const {
154       return is_megamorphic == other.is_megamorphic &&
155           is_missing_types == other.is_missing_types &&
156           classes == other.classes;
157     }
158 
159     // Not all runtime types can be encoded in the profile. For example if the receiver
160     // type is in a dex file which is not tracked for profiling its type cannot be
161     // encoded. When types are missing this field will be set to true.
162     bool is_missing_types;
163     bool is_megamorphic;
164     ArenaSet<dex::TypeIndex> classes;
165   };
166 
167   // The inline cache map: DexPc -> DexPcData.
168   using InlineCacheMap = ArenaSafeMap<uint16_t, DexPcData>;
169 
170   // Maps a method dex index to its inline cache.
171   using MethodMap = ArenaSafeMap<uint16_t, InlineCacheMap>;
172 
173   // Profile method hotness information for a single method. Also includes a pointer to the inline
174   // cache map.
175   class MethodHotness {
176    public:
177     enum Flag {
178       // Marker flag used to simplify iterations.
179       kFlagFirst = 1 << 0,
180       // The method is profile-hot (this is implementation specific, e.g. equivalent to JIT-warm)
181       kFlagHot = 1 << 0,
182       // Executed during the app startup as determined by the runtime.
183       kFlagStartup = 1 << 1,
184       // Executed after app startup as determined by the runtime.
185       kFlagPostStartup = 1 << 2,
186       // Marker flag used to simplify iterations.
187       kFlagLastRegular = 1 << 2,
188       // Executed by a 32bit process.
189       kFlag32bit = 1 << 3,
190       // Executed by a 64bit process.
191       kFlag64bit = 1 << 4,
192       // Executed on sensitive thread (e.g. UI).
193       kFlagSensitiveThread = 1 << 5,
194       // Executed during the app startup as determined by the framework (equivalent to am start).
195       kFlagAmStartup = 1 << 6,
196       // Executed after the app startup as determined by the framework (equivalent to am start).
197       kFlagAmPostStartup = 1 << 7,
198       // Executed during system boot.
199       kFlagBoot = 1 << 8,
200       // Executed after the system has booted.
201       kFlagPostBoot = 1 << 9,
202 
203       // The startup bins captured the relative order of when a method become hot. There are 6
204       // total bins supported and each hot method will have at least one bit set. If the profile was
205       // merged multiple times more than one bit may be set as a given method may become hot at
206       // various times during subsequent executions.
207       // The granularity of the bins is unspecified (i.e. the runtime is free to change the
208       // values it uses - this may be 100ms, 200ms etc...).
209       kFlagStartupBin = 1 << 10,
210       kFlagStartupMaxBin = 1 << 15,
211       // Marker flag used to simplify iterations.
212       kFlagLastBoot = 1 << 15,
213     };
214 
IsHot()215     bool IsHot() const {
216       return (flags_ & kFlagHot) != 0;
217     }
218 
IsStartup()219     bool IsStartup() const {
220       return (flags_ & kFlagStartup) != 0;
221     }
222 
IsPostStartup()223     bool IsPostStartup() const {
224       return (flags_ & kFlagPostStartup) != 0;
225     }
226 
AddFlag(Flag flag)227     void AddFlag(Flag flag) {
228       flags_ |= flag;
229     }
230 
GetFlags()231     uint32_t GetFlags() const {
232       return flags_;
233     }
234 
HasFlagSet(MethodHotness::Flag flag)235     bool HasFlagSet(MethodHotness::Flag flag) {
236       return (flags_ & flag ) != 0;
237     }
238 
IsInProfile()239     bool IsInProfile() const {
240       return flags_ != 0;
241     }
242 
GetInlineCacheMap()243     const InlineCacheMap* GetInlineCacheMap() const {
244       return inline_cache_map_;
245     }
246 
247    private:
248     const InlineCacheMap* inline_cache_map_ = nullptr;
249     uint32_t flags_ = 0;
250 
SetInlineCacheMap(const InlineCacheMap * info)251     void SetInlineCacheMap(const InlineCacheMap* info) {
252       inline_cache_map_ = info;
253     }
254 
255     friend class ProfileCompilationInfo;
256   };
257 
258   // Encapsulates metadata that can be associated with the methods and classes added to the profile.
259   // The additional metadata is serialized in the profile and becomes part of the profile key
260   // representation. It can be used to differentiate the samples that are added to the profile
261   // based on the supported criteria (e.g. keep track of which app generated what sample when
262   // constructing a boot profile.).
263   class ProfileSampleAnnotation {
264    public:
ProfileSampleAnnotation(const std::string & package_name)265     explicit ProfileSampleAnnotation(const std::string& package_name) :
266         origin_package_name_(package_name) {}
267 
GetOriginPackageName()268     const std::string& GetOriginPackageName() const { return origin_package_name_; }
269 
270     bool operator==(const ProfileSampleAnnotation& other) const {
271       return origin_package_name_ == other.origin_package_name_;
272     }
273 
274     bool operator<(const ProfileSampleAnnotation& other) const {
275       return origin_package_name_ < other.origin_package_name_;
276     }
277 
278     // A convenient empty annotation object that can be used to denote that no annotation should
279     // be associated with the profile samples.
280     static const ProfileSampleAnnotation kNone;
281 
282    private:
283     // The name of the package that generated the samples.
284     const std::string origin_package_name_;
285   };
286 
287   // Helper class for printing referenced dex file information to a stream.
288   struct DexReferenceDumper;
289 
290   // Public methods to create, extend or query the profile.
291   ProfileCompilationInfo();
292   explicit ProfileCompilationInfo(bool for_boot_image);
293   explicit ProfileCompilationInfo(ArenaPool* arena_pool);
294   ProfileCompilationInfo(ArenaPool* arena_pool, bool for_boot_image);
295 
296   ~ProfileCompilationInfo();
297 
298   // Returns the maximum value for the profile index.
MaxProfileIndex()299   static constexpr ProfileIndexType MaxProfileIndex() {
300     return std::numeric_limits<ProfileIndexType>::max();
301   }
302 
303   // Find or add a tracked dex file. Returns `MaxProfileIndex()` on failure, whether due to
304   // checksum/num_type_ids/num_method_ids mismatch or reaching the maximum number of dex files.
305   ProfileIndexType FindOrAddDexFile(
306       const DexFile& dex_file,
307       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) {
308     DexFileData* data = GetOrAddDexFileData(&dex_file, annotation);
309     return (data != nullptr) ? data->profile_index : MaxProfileIndex();
310   }
311 
312   // Add the given methods to the current profile object.
313   //
314   // Note: if an annotation is provided, the methods/classes will be associated with the group
315   // (dex_file, sample_annotation). Each group keeps its unique set of methods/classes.
316   bool AddMethods(const std::vector<ProfileMethodInfo>& methods,
317                   MethodHotness::Flag flags,
318                   const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone);
319 
320   // Find a type index in the `dex_file` if there is a `TypeId` for it. Otherwise,
321   // find or insert the descriptor in "extra descriptors" and return an artificial
322   // type index beyond `dex_file.NumTypeIds()`. This fails if the artificial index
323   // would be kDexNoIndex16 (0xffffu) or higher, returning an invalid type index.
324   // The returned type index can be used, if valid, for `AddClass()` or (TODO) as
325   // a type index for inline caches.
326   dex::TypeIndex FindOrCreateTypeIndex(const DexFile& dex_file, TypeReference class_ref);
327   dex::TypeIndex FindOrCreateTypeIndex(const DexFile& dex_file, const char* descriptor);
328 
329   // Add a class with the specified `type_index` to the profile. The `type_index`
330   // can be either a normal index for a `TypeId` in the dex file, or an artificial
331   // type index created by `FindOrCreateTypeIndex()`.
AddClass(ProfileIndexType profile_index,dex::TypeIndex type_index)332   void AddClass(ProfileIndexType profile_index, dex::TypeIndex type_index) {
333     DCHECK_LT(profile_index, info_.size());
334     DexFileData* const data = info_[profile_index].get();
335     DCHECK(type_index.IsValid());
336     DCHECK(type_index.index_ <= data->num_type_ids ||
337            type_index.index_ - data->num_type_ids < extra_descriptors_.size());
338     data->class_set.insert(type_index);
339   }
340 
341   // Add a class with the specified `type_index` to the profile. The `type_index`
342   // can be either a normal index for a `TypeId` in the dex file, or an artificial
343   // type index created by `FindOrCreateTypeIndex()`.
344   // Returns `true` on success, `false` on failure.
345   bool AddClass(const DexFile& dex_file,
346                 dex::TypeIndex type_index,
347                 const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) {
348     DCHECK(type_index.IsValid());
349     DCHECK(type_index.index_ <= dex_file.NumTypeIds() ||
350            type_index.index_ - dex_file.NumTypeIds() < extra_descriptors_.size());
351     DexFileData* const data = GetOrAddDexFileData(&dex_file, annotation);
352     if (data == nullptr) {  // Checksum/num_type_ids/num_method_ids mismatch or too many dex files.
353       return false;
354     }
355     data->class_set.insert(type_index);
356     return true;
357   }
358 
359   // Add a class with the specified `descriptor` to the profile.
360   // Returns `true` on success, `false` on failure.
361   bool AddClass(const DexFile& dex_file,
362                 const char* descriptor,
363                 const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone);
364   bool AddClass(const DexFile& dex_file,
365                 const std::string& descriptor,
366                 const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) {
367     return AddClass(dex_file, descriptor.c_str(), annotation);
368   }
369   bool AddClass(const DexFile& dex_file,
370                 std::string_view descriptor,
371                 const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) {
372     return AddClass(dex_file, std::string(descriptor).c_str(), annotation);
373   }
374 
375   // Add multiple type ids for classes in a single dex file. Iterator is for type_ids not
376   // class_defs.
377   //
378   // Note: see AddMethods docs for the handling of annotations.
379   template <class Iterator>
380   bool AddClassesForDex(
381       const DexFile* dex_file,
382       Iterator index_begin,
383       Iterator index_end,
384       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) {
385     DexFileData* data = GetOrAddDexFileData(dex_file, annotation);
386     if (data == nullptr) {
387       return false;
388     }
389     data->class_set.insert(index_begin, index_end);
390     return true;
391   }
392 
AddMethod(ProfileIndexType profile_index,uint32_t method_index,MethodHotness::Flag flags)393   void AddMethod(ProfileIndexType profile_index, uint32_t method_index, MethodHotness::Flag flags) {
394     DCHECK_LT(profile_index, info_.size());
395     DexFileData* const data = info_[profile_index].get();
396     DCHECK_LT(method_index, data->num_method_ids);
397     data->AddMethod(flags, method_index);
398   }
399 
400   // Add a method to the profile using its online representation (containing runtime structures).
401   //
402   // Note: see AddMethods docs for the handling of annotations.
403   bool AddMethod(const ProfileMethodInfo& pmi,
404                  MethodHotness::Flag flags,
405                  const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone);
406 
407   // Bulk add sampled methods and/or hot methods for a single dex, fast since it only has one
408   // GetOrAddDexFileData call.
409   //
410   // Note: see AddMethods docs for the handling of annotations.
411   template <class Iterator>
412   bool AddMethodsForDex(
413       MethodHotness::Flag flags,
414       const DexFile* dex_file,
415       Iterator index_begin,
416       Iterator index_end,
417       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) {
418     DexFileData* data = GetOrAddDexFileData(dex_file, annotation);
419     if (data == nullptr) {
420       return false;
421     }
422     for (Iterator it = index_begin; it != index_end; ++it) {
423       DCHECK_LT(*it, data->num_method_ids);
424       if (!data->AddMethod(flags, *it)) {
425         return false;
426       }
427     }
428     return true;
429   }
430 
431   // Load or Merge profile information from the given file descriptor.
432   // If the current profile is non-empty the load will fail.
433   // If merge_classes is set to false, classes will not be merged/loaded.
434   // If filter_fn is present, it will be used to filter out profile data belonging
435   // to dex file which do not comply with the filter
436   // (i.e. for which filter_fn(dex_location, dex_checksum) is false).
437   using ProfileLoadFilterFn = std::function<bool(const std::string&, uint32_t)>;
438   // Profile filter method which accepts all dex locations.
439   // This is convenient to use when we need to accept all locations without repeating the same
440   // lambda.
441   static bool ProfileFilterFnAcceptAll(const std::string& dex_location, uint32_t checksum);
442 
443   bool Load(
444       int fd,
445       bool merge_classes = true,
446       const ProfileLoadFilterFn& filter_fn = ProfileFilterFnAcceptAll);
447 
448   // Verify integrity of the profile file with the provided dex files.
449   // If there exists a DexData object which maps to a dex_file, then it verifies that:
450   // - The checksums of the DexData and dex_file are equals.
451   // - No method id exceeds NumMethodIds corresponding to the dex_file.
452   // - No class id exceeds NumTypeIds corresponding to the dex_file.
453   // - For every inline_caches, class_ids does not exceed NumTypeIds corresponding to
454   //   the dex_file they are in.
455   bool VerifyProfileData(const std::vector<const DexFile*>& dex_files);
456 
457   // Load profile information from the given file
458   // If the current profile is non-empty the load will fail.
459   // If clear_if_invalid is true and the file is invalid the method clears the
460   // the file and returns true.
461   bool Load(const std::string& filename, bool clear_if_invalid);
462 
463   // Merge the data from another ProfileCompilationInfo into the current object. Only merges
464   // classes if merge_classes is true. This is used for creating the boot profile since
465   // we don't want all of the classes to be image classes.
466   bool MergeWith(const ProfileCompilationInfo& info, bool merge_classes = true);
467 
468   // Merge profile information from the given file descriptor.
469   bool MergeWith(const std::string& filename);
470 
471   // Save the profile data to the given file descriptor.
472   bool Save(int fd);
473 
474   // Save the current profile into the given file. The file will be cleared before saving.
475   bool Save(const std::string& filename, uint64_t* bytes_written);
476 
477   // Return the number of dex files referenced in the profile.
GetNumberOfDexFiles()478   size_t GetNumberOfDexFiles() const {
479     return info_.size();
480   }
481 
482   // Return the number of methods that were profiled.
483   uint32_t GetNumberOfMethods() const;
484 
485   // Return the number of resolved classes that were profiled.
486   uint32_t GetNumberOfResolvedClasses() const;
487 
488   // Returns the profile method info for a given method reference.
489   //
490   // Note that if the profile was built with annotations, the same dex file may be
491   // represented multiple times in the profile (due to different annotation associated with it).
492   // If so, and if no annotation is passed to this method, then only the first dex file is searched.
493   //
494   // Implementation details: It is suitable to pass kNone for regular profile guided compilation
495   // because during compilation we generally don't care about annotations. The metadata is
496   // useful for boot profiles which need the extra information.
497   MethodHotness GetMethodHotness(
498       const MethodReference& method_ref,
499       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) const;
500 
501   // Return true if the class's type is present in the profiling info.
502   //
503   // Note: see GetMethodHotness docs for the handling of annotations.
504   bool ContainsClass(
505       const DexFile& dex_file,
506       dex::TypeIndex type_idx,
507       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) const;
508 
509   // Return the dex file for the given `profile_index`, or null if none of the provided
510   // dex files has a matching checksum and a location with the same base key.
511   template <typename Container>
FindDexFileForProfileIndex(ProfileIndexType profile_index,const Container & dex_files)512   const DexFile* FindDexFileForProfileIndex(ProfileIndexType profile_index,
513                                             const Container& dex_files) const {
514     static_assert(std::is_same_v<typename Container::value_type, const DexFile*> ||
515                   std::is_same_v<typename Container::value_type, std::unique_ptr<const DexFile>>);
516     DCHECK_LE(profile_index, info_.size());
517     const DexFileData* dex_file_data = info_[profile_index].get();
518     DCHECK(dex_file_data != nullptr);
519     uint32_t dex_checksum = dex_file_data->checksum;
520     std::string_view base_key = GetBaseKeyViewFromAugmentedKey(dex_file_data->profile_key);
521     for (const auto& dex_file : dex_files) {
522       if (dex_checksum == dex_file->GetLocationChecksum() &&
523           base_key == GetProfileDexFileBaseKeyView(dex_file->GetLocation())) {
524         return std::addressof(*dex_file);
525       }
526     }
527     return nullptr;
528   }
529 
530   DexReferenceDumper DumpDexReference(ProfileIndexType profile_index) const;
531 
532   // Dump all the loaded profile info into a string and returns it.
533   // If dex_files is not empty then the method indices will be resolved to their
534   // names.
535   // This is intended for testing and debugging.
536   std::string DumpInfo(const std::vector<const DexFile*>& dex_files,
537                        bool print_full_dex_location = true) const;
538 
539   // Return the classes and methods for a given dex file through out args. The out args are the set
540   // of class as well as the methods and their associated inline caches. Returns true if the dex
541   // file is register and has a matching checksum, false otherwise.
542   //
543   // Note: see GetMethodHotness docs for the handling of annotations.
544   bool GetClassesAndMethods(
545       const DexFile& dex_file,
546       /*out*/std::set<dex::TypeIndex>* class_set,
547       /*out*/std::set<uint16_t>* hot_method_set,
548       /*out*/std::set<uint16_t>* startup_method_set,
549       /*out*/std::set<uint16_t>* post_startup_method_method_set,
550       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone) const;
551 
552   // Returns true iff both profiles have the same version.
553   bool SameVersion(const ProfileCompilationInfo& other) const;
554 
555   // Perform an equality test with the `other` profile information.
556   bool Equals(const ProfileCompilationInfo& other);
557 
558   // Return the base profile key associated with the given dex location. The base profile key
559   // is solely constructed based on the dex location (as opposed to the one produced by
560   // GetProfileDexFileAugmentedKey which may include additional metadata like the origin
561   // package name)
562   static std::string GetProfileDexFileBaseKey(const std::string& dex_location);
563 
564   // Returns a base key without the annotation information.
565   static std::string GetBaseKeyFromAugmentedKey(const std::string& profile_key);
566 
567   // Returns the annotations from an augmented key.
568   // If the key is a base key it return ProfileSampleAnnotation::kNone.
569   static ProfileSampleAnnotation GetAnnotationFromKey(const std::string& augmented_key);
570 
571   // Generate a test profile which will contain a percentage of the total maximum
572   // number of methods and classes (method_ratio and class_ratio).
573   static bool GenerateTestProfile(int fd,
574                                   uint16_t number_of_dex_files,
575                                   uint16_t method_ratio,
576                                   uint16_t class_ratio,
577                                   uint32_t random_seed);
578 
579   // Generate a test profile which will randomly contain classes and methods from
580   // the provided list of dex files.
581   static bool GenerateTestProfile(int fd,
582                                   std::vector<std::unique_ptr<const DexFile>>& dex_files,
583                                   uint16_t method_percentage,
584                                   uint16_t class_percentage,
585                                   uint32_t random_seed);
586 
GetAllocator()587   ArenaAllocator* GetAllocator() { return &allocator_; }
588 
589   // Return all of the class descriptors in the profile for a set of dex files.
590   // Note: see GetMethodHotness docs for the handling of annotations..
591   HashSet<std::string> GetClassDescriptors(
592       const std::vector<const DexFile*>& dex_files,
593       const ProfileSampleAnnotation& annotation = ProfileSampleAnnotation::kNone);
594 
595   // Return true if the fd points to a profile file.
596   bool IsProfileFile(int fd);
597 
598   // Update the profile keys corresponding to the given dex files based on their current paths.
599   // This method allows fix-ups in the profile for dex files that might have been renamed.
600   // The new profile key will be constructed based on the current dex location.
601   //
602   // The matching [profile key <-> dex_file] is done based on the dex checksum and the number of
603   // methods ids. If neither is a match then the profile key is not updated.
604   //
605   // If the new profile key would collide with an existing key (for a different dex)
606   // the method returns false. Otherwise it returns true.
607   bool UpdateProfileKeys(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
608 
609   // Checks if the profile is empty.
610   bool IsEmpty() const;
611 
612   // Clears all the data from the profile.
613   void ClearData();
614 
615   // Clears all the data from the profile and adjust the object version.
616   void ClearDataAndAdjustVersion(bool for_boot_image);
617 
618   // Prepare the profile to store aggregation counters.
619   // This will change the profile version and allocate extra storage for the counters.
620   // It allocates 2 bytes for every possible method and class, so do not use in performance
621   // critical code which needs to be memory efficient.
622   void PrepareForAggregationCounters();
623 
624   // Returns true if the profile is configured to store aggregation counters.
625   bool IsForBootImage() const;
626 
627   // Get type descriptor for a valid type index, whether a normal type index
628   // referencing a `dex::TypeId` in the dex file, or an artificial type index
629   // referencing an "extra descriptor".
GetTypeDescriptor(const DexFile * dex_file,dex::TypeIndex type_index)630   const char* GetTypeDescriptor(const DexFile* dex_file, dex::TypeIndex type_index) const {
631     DCHECK(type_index.IsValid());
632     uint32_t num_type_ids = dex_file->NumTypeIds();
633     if (type_index.index_ < num_type_ids) {
634       return dex_file->StringByTypeIdx(type_index);
635     } else {
636       return extra_descriptors_[type_index.index_ - num_type_ids].c_str();
637     }
638   }
639 
640   // Return the version of this profile.
641   const uint8_t* GetVersion() const;
642 
643   // Extracts the data that the profile has on the given dex files:
644   //  - for each method and class, a list of the corresponding annotations and flags
645   //  - the maximum number of aggregations for classes and classes across dex files with different
646   //    annotations (essentially this sums up how many different packages used the corresponding
647   //    method). This information is reconstructible from the other two pieces of info, but it's
648   //    convenient to have it precomputed.
649   std::unique_ptr<FlattenProfileData> ExtractProfileData(
650       const std::vector<std::unique_ptr<const DexFile>>& dex_files) const;
651 
652  private:
653   // Helper classes.
654   class FileHeader;
655   class FileSectionInfo;
656   enum class FileSectionType : uint32_t;
657   enum class ProfileLoadStatus : uint32_t;
658   class ProfileSource;
659   class SafeBuffer;
660 
661   // Extra descriptors are used to reference classes with `TypeIndex` between the dex
662   // file's `NumTypeIds()` and the `DexFile::kDexNoIndex16`. The range of usable
663   // extra descriptor indexes is therefore also limited by `DexFile::kDexNoIndex16`.
664   using ExtraDescriptorIndex = uint16_t;
665   static constexpr ExtraDescriptorIndex kMaxExtraDescriptors = DexFile::kDexNoIndex16;
666 
667   class ExtraDescriptorIndexEmpty {
668    public:
MakeEmpty(ExtraDescriptorIndex & index)669     void MakeEmpty(ExtraDescriptorIndex& index) const {
670       index = kMaxExtraDescriptors;
671     }
IsEmpty(const ExtraDescriptorIndex & index)672     bool IsEmpty(const ExtraDescriptorIndex& index) const {
673       return index == kMaxExtraDescriptors;
674     }
675   };
676 
677   class ExtraDescriptorHash {
678    public:
ExtraDescriptorHash(const dchecked_vector<std::string> * extra_descriptors)679     explicit ExtraDescriptorHash(const dchecked_vector<std::string>* extra_descriptors)
680         : extra_descriptors_(extra_descriptors) {}
681 
operator()682     size_t operator()(const ExtraDescriptorIndex& index) const {
683       std::string_view str = (*extra_descriptors_)[index];
684       return (*this)(str);
685     }
686 
operator()687     size_t operator()(std::string_view str) const {
688       return DataHash()(str);
689     }
690 
691    private:
692     const dchecked_vector<std::string>* extra_descriptors_;
693   };
694 
695   class ExtraDescriptorEquals {
696    public:
ExtraDescriptorEquals(const dchecked_vector<std::string> * extra_descriptors)697     explicit ExtraDescriptorEquals(const dchecked_vector<std::string>* extra_descriptors)
698         : extra_descriptors_(extra_descriptors) {}
699 
operator()700     size_t operator()(const ExtraDescriptorIndex& lhs, const ExtraDescriptorIndex& rhs) const {
701       DCHECK_EQ(lhs == rhs, (*this)(lhs, (*extra_descriptors_)[rhs]));
702       return lhs == rhs;
703     }
704 
operator()705     size_t operator()(const ExtraDescriptorIndex& lhs, std::string_view rhs_str) const {
706       std::string_view lhs_str = (*extra_descriptors_)[lhs];
707       return lhs_str == rhs_str;
708     }
709 
710    private:
711     const dchecked_vector<std::string>* extra_descriptors_;
712   };
713 
714   using ExtraDescriptorHashSet = HashSet<ExtraDescriptorIndex,
715                                          ExtraDescriptorIndexEmpty,
716                                          ExtraDescriptorHash,
717                                          ExtraDescriptorEquals>;
718 
719   // Internal representation of the profile information belonging to a dex file.
720   // Note that we could do without the profile_index (the index of the dex file
721   // in the profile) field in this struct because we can infer it from
722   // `profile_key_map_` and `info_`. However, it makes the profiles logic much
723   // simpler if we have the profile index here as well.
724   struct DexFileData : public DeletableArenaObject<kArenaAllocProfile> {
DexFileDataDexFileData725     DexFileData(ArenaAllocator* allocator,
726                 const std::string& key,
727                 uint32_t location_checksum,
728                 uint16_t index,
729                 uint32_t num_types,
730                 uint32_t num_methods,
731                 bool for_boot_image)
732         : allocator_(allocator),
733           profile_key(key),
734           profile_index(index),
735           checksum(location_checksum),
736           method_map(std::less<uint16_t>(), allocator->Adapter(kArenaAllocProfile)),
737           class_set(std::less<dex::TypeIndex>(), allocator->Adapter(kArenaAllocProfile)),
738           num_type_ids(num_types),
739           num_method_ids(num_methods),
740           bitmap_storage(allocator->Adapter(kArenaAllocProfile)),
741           is_for_boot_image(for_boot_image) {
742       bitmap_storage.resize(ComputeBitmapStorage(is_for_boot_image, num_method_ids));
743       if (!bitmap_storage.empty()) {
744         method_bitmap =
745             BitMemoryRegion(MemoryRegion(
746                 &bitmap_storage[0],
747                 bitmap_storage.size()),
748                 0,
749                 ComputeBitmapBits(is_for_boot_image, num_method_ids));
750       }
751     }
752 
ComputeBitmapBitsDexFileData753     static size_t ComputeBitmapBits(bool is_for_boot_image, uint32_t num_method_ids) {
754       size_t flag_bitmap_index = FlagBitmapIndex(is_for_boot_image
755           ? MethodHotness::kFlagLastBoot
756           : MethodHotness::kFlagLastRegular);
757       return num_method_ids * (flag_bitmap_index + 1);
758     }
ComputeBitmapStorageDexFileData759     static size_t ComputeBitmapStorage(bool is_for_boot_image, uint32_t num_method_ids) {
760       return RoundUp(ComputeBitmapBits(is_for_boot_image, num_method_ids), kBitsPerByte) /
761           kBitsPerByte;
762     }
763 
764     bool operator==(const DexFileData& other) const {
765       return checksum == other.checksum &&
766           num_method_ids == other.num_method_ids &&
767           method_map == other.method_map &&
768           class_set == other.class_set &&
769           (BitMemoryRegion::Compare(method_bitmap, other.method_bitmap) == 0);
770     }
771 
772     // Mark a method as executed at least once.
773     bool AddMethod(MethodHotness::Flag flags, size_t index);
774 
MergeBitmapDexFileData775     void MergeBitmap(const DexFileData& other) {
776       DCHECK_EQ(bitmap_storage.size(), other.bitmap_storage.size());
777       for (size_t i = 0; i < bitmap_storage.size(); ++i) {
778         bitmap_storage[i] |= other.bitmap_storage[i];
779       }
780     }
781 
782     void SetMethodHotness(size_t index, MethodHotness::Flag flags);
783     MethodHotness GetHotnessInfo(uint32_t dex_method_index) const;
784 
785     bool ContainsClass(dex::TypeIndex type_index) const;
786 
787     uint32_t ClassesDataSize() const;
788     void WriteClasses(SafeBuffer& buffer) const;
789     ProfileLoadStatus ReadClasses(
790         SafeBuffer& buffer,
791         const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
792         std::string* error);
793     static ProfileLoadStatus SkipClasses(SafeBuffer& buffer, std::string* error);
794 
795     uint32_t MethodsDataSize(/*out*/ uint16_t* method_flags = nullptr,
796                              /*out*/ size_t* saved_bitmap_bit_size = nullptr) const;
797     void WriteMethods(SafeBuffer& buffer) const;
798     ProfileLoadStatus ReadMethods(
799         SafeBuffer& buffer,
800         const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
801         std::string* error);
802     static ProfileLoadStatus SkipMethods(SafeBuffer& buffer, std::string* error);
803 
804     // The allocator used to allocate new inline cache maps.
805     ArenaAllocator* const allocator_;
806     // The profile key this data belongs to.
807     std::string profile_key;
808     // The profile index of this dex file (matches ClassReference#dex_profile_index).
809     ProfileIndexType profile_index;
810     // The dex checksum.
811     uint32_t checksum;
812     // The methods' profile information.
813     MethodMap method_map;
814     // The classes which have been profiled. Note that these don't necessarily include
815     // all the classes that can be found in the inline caches reference.
816     ArenaSet<dex::TypeIndex> class_set;
817     // Find the inline caches of the the given method index. Add an empty entry if
818     // no previous data is found.
819     InlineCacheMap* FindOrAddHotMethod(uint16_t method_index);
820     // Num type ids.
821     uint32_t num_type_ids;
822     // Num method ids.
823     uint32_t num_method_ids;
824     ArenaVector<uint8_t> bitmap_storage;
825     BitMemoryRegion method_bitmap;
826     bool is_for_boot_image;
827 
828    private:
829     template <typename Fn>
830     void ForMethodBitmapHotnessFlags(Fn fn) const;
831 
832     static void WriteClassSet(SafeBuffer& buffer, const ArenaSet<dex::TypeIndex>& class_set);
833     size_t MethodFlagBitmapIndex(MethodHotness::Flag flag, size_t method_index) const;
834     static size_t FlagBitmapIndex(MethodHotness::Flag flag);
835 
836     uint16_t GetUsedBitmapFlags() const;
837   };
838 
839   // Return the profile data for the given profile key or null if the dex location
840   // already exists but has a different checksum
841   DexFileData* GetOrAddDexFileData(const std::string& profile_key,
842                                    uint32_t checksum,
843                                    uint32_t num_type_ids,
844                                    uint32_t num_method_ids);
845 
GetOrAddDexFileData(const DexFile * dex_file,const ProfileSampleAnnotation & annotation)846   DexFileData* GetOrAddDexFileData(const DexFile* dex_file,
847                                    const ProfileSampleAnnotation& annotation) {
848     return GetOrAddDexFileData(GetProfileDexFileAugmentedKey(dex_file->GetLocation(), annotation),
849                                dex_file->GetLocationChecksum(),
850                                dex_file->NumTypeIds(),
851                                dex_file->NumMethodIds());
852   }
853 
854   // Return the dex data associated with the given profile key or null if the profile
855   // doesn't contain the key.
856   const DexFileData* FindDexData(const std::string& profile_key,
857                                  uint32_t checksum,
858                                  bool verify_checksum = true) const;
859   // Same as FindDexData but performs the searching using the given annotation:
860   //   - If the annotation is kNone then the search ignores it and only looks at the base keys.
861   //     In this case only the first matching dex is searched.
862   //   - If the annotation is not kNone, the augmented key is constructed and used to invoke
863   //     the regular FindDexData.
864   const DexFileData* FindDexDataUsingAnnotations(
865       const DexFile* dex_file,
866       const ProfileSampleAnnotation& annotation) const;
867 
868   // Same as FindDexDataUsingAnnotations but extracts the data for all annotations.
869   void FindAllDexData(
870       const DexFile* dex_file,
871       /*out*/ std::vector<const ProfileCompilationInfo::DexFileData*>* result) const;
872 
873   // Add a new extra descriptor. Returns kMaxExtraDescriptors on failure.
874   ExtraDescriptorIndex AddExtraDescriptor(std::string_view extra_descriptor);
875 
876   // Parsing functionality.
877 
878   ProfileLoadStatus OpenSource(int32_t fd,
879                                /*out*/ std::unique_ptr<ProfileSource>* source,
880                                /*out*/ std::string* error);
881 
882   ProfileLoadStatus ReadSectionData(ProfileSource& source,
883                                     const FileSectionInfo& section_info,
884                                     /*out*/ SafeBuffer* buffer,
885                                     /*out*/ std::string* error);
886 
887   ProfileLoadStatus ReadDexFilesSection(
888       ProfileSource& source,
889       const FileSectionInfo& section_info,
890       const ProfileLoadFilterFn& filter_fn,
891       /*out*/ dchecked_vector<ProfileIndexType>* dex_profile_index_remap,
892       /*out*/ std::string* error);
893 
894   ProfileLoadStatus ReadExtraDescriptorsSection(
895       ProfileSource& source,
896       const FileSectionInfo& section_info,
897       /*out*/ dchecked_vector<ExtraDescriptorIndex>* extra_descriptors_remap,
898       /*out*/ std::string* error);
899 
900   ProfileLoadStatus ReadClassesSection(
901       ProfileSource& source,
902       const FileSectionInfo& section_info,
903       const dchecked_vector<ProfileIndexType>& dex_profile_index_remap,
904       const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
905       /*out*/ std::string* error);
906 
907   ProfileLoadStatus ReadMethodsSection(
908       ProfileSource& source,
909       const FileSectionInfo& section_info,
910       const dchecked_vector<ProfileIndexType>& dex_profile_index_remap,
911       const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
912       /*out*/ std::string* error);
913 
914   // Entry point for profile loading functionality.
915   ProfileLoadStatus LoadInternal(
916       int32_t fd,
917       std::string* error,
918       bool merge_classes = true,
919       const ProfileLoadFilterFn& filter_fn = ProfileFilterFnAcceptAll);
920 
921   // Find the data for the dex_pc in the inline cache. Adds an empty entry
922   // if no previous data exists.
923   static DexPcData* FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc);
924 
925   // Initializes the profile version to the desired one.
926   void InitProfileVersionInternal(const uint8_t version[]);
927 
928   // Returns the threshold size (in bytes) which will trigger save/load warnings.
929   size_t GetSizeWarningThresholdBytes() const;
930   // Returns the threshold size (in bytes) which will cause save/load failures.
931   size_t GetSizeErrorThresholdBytes() const;
932 
933   // Implementation of `GetProfileDexFileBaseKey()` but returning a subview
934   // referencing the same underlying data to avoid excessive heap allocations.
935   static std::string_view GetProfileDexFileBaseKeyView(std::string_view dex_location);
936 
937   // Implementation of `GetBaseKeyFromAugmentedKey()` but returning a subview
938   // referencing the same underlying data to avoid excessive heap allocations.
939   static std::string_view GetBaseKeyViewFromAugmentedKey(std::string_view dex_location);
940 
941   // Returns the augmented profile key associated with the given dex location.
942   // The return key will contain a serialized form of the information from the provided
943   // annotation. If the annotation is ProfileSampleAnnotation::kNone then no extra info is
944   // added to the key and this method is equivalent to GetProfileDexFileBaseKey.
945   static std::string GetProfileDexFileAugmentedKey(const std::string& dex_location,
946                                                    const ProfileSampleAnnotation& annotation);
947 
948   // Migrates the annotation from an augmented key to a base key.
949   static std::string MigrateAnnotationInfo(const std::string& base_key,
950                                            const std::string& augmented_key);
951 
952   friend class ProfileCompilationInfoTest;
953   friend class CompilerDriverProfileTest;
954   friend class ProfileAssistantTest;
955   friend class Dex2oatLayoutTest;
956 
957   MallocArenaPool default_arena_pool_;
958   ArenaAllocator allocator_;
959 
960   // Vector containing the actual profile info.
961   // The vector index is the profile index of the dex data and
962   // matched DexFileData::profile_index.
963   ArenaVector<std::unique_ptr<DexFileData>> info_;
964 
965   // Cache mapping profile keys to profile index.
966   // This is used to speed up searches since it avoids iterating
967   // over the info_ vector when searching by profile key.
968   // The backing storage for the `string_view` is the associated `DexFileData`.
969   ArenaSafeMap<const std::string_view, ProfileIndexType> profile_key_map_;
970 
971   // Additional descriptors for referencing types not present in a dex files's `TypeId`s.
972   dchecked_vector<std::string> extra_descriptors_;
973   ExtraDescriptorHashSet extra_descriptors_indexes_;
974 
975   // The version of the profile.
976   uint8_t version_[kProfileVersionSize];
977 };
978 
979 /**
980  * Flatten profile data that list all methods and type references together
981  * with their metadata (such as flags or annotation list).
982  */
983 class FlattenProfileData {
984  public:
985   class ItemMetadata {
986    public:
987     ItemMetadata();
988     ItemMetadata(const ItemMetadata& other);
989 
GetFlags()990     uint16_t GetFlags() const {
991       return flags_;
992     }
993 
GetAnnotations()994     const std::list<ProfileCompilationInfo::ProfileSampleAnnotation>& GetAnnotations() const {
995       return annotations_;
996     }
997 
AddFlag(ProfileCompilationInfo::MethodHotness::Flag flag)998     void AddFlag(ProfileCompilationInfo::MethodHotness::Flag flag) {
999       flags_ |= flag;
1000     }
1001 
HasFlagSet(ProfileCompilationInfo::MethodHotness::Flag flag)1002     bool HasFlagSet(ProfileCompilationInfo::MethodHotness::Flag flag) const {
1003       return (flags_ & flag) != 0;
1004     }
1005 
1006    private:
1007     // will be 0 for classes and MethodHotness::Flags for methods.
1008     uint16_t flags_;
1009     // This is a list that may contain duplicates after a merge operation.
1010     // It represents that a method was used multiple times across different devices.
1011     std::list<ProfileCompilationInfo::ProfileSampleAnnotation> annotations_;
1012 
1013     friend class ProfileCompilationInfo;
1014     friend class FlattenProfileData;
1015   };
1016 
1017   FlattenProfileData();
1018 
GetMethodData()1019   const SafeMap<MethodReference, ItemMetadata>& GetMethodData() const {
1020     return method_metadata_;
1021   }
1022 
GetClassData()1023   const SafeMap<TypeReference, ItemMetadata>& GetClassData() const {
1024     return class_metadata_;
1025   }
1026 
GetMaxAggregationForMethods()1027   uint32_t GetMaxAggregationForMethods() const {
1028     return max_aggregation_for_methods_;
1029   }
1030 
GetMaxAggregationForClasses()1031   uint32_t GetMaxAggregationForClasses() const {
1032     return max_aggregation_for_classes_;
1033   }
1034 
1035   void MergeData(const FlattenProfileData& other);
1036 
1037  private:
1038   // Method data.
1039   SafeMap<MethodReference, ItemMetadata> method_metadata_;
1040   // Class data.
1041   SafeMap<TypeReference, ItemMetadata> class_metadata_;
1042   // Maximum aggregation counter for all methods.
1043   // This is essentially a cache equal to the max size of any method's annotation set.
1044   // It avoids the traversal of all the methods which can be quite expensive.
1045   uint32_t max_aggregation_for_methods_;
1046   // Maximum aggregation counter for all classes.
1047   // Simillar to max_aggregation_for_methods_.
1048   uint32_t max_aggregation_for_classes_;
1049 
1050   friend class ProfileCompilationInfo;
1051 };
1052 
1053 struct ProfileCompilationInfo::DexReferenceDumper {
GetProfileKeyDexReferenceDumper1054   const std::string& GetProfileKey() {
1055     return dex_file_data->profile_key;
1056   }
1057 
GetDexChecksumDexReferenceDumper1058   uint32_t GetDexChecksum() const {
1059     return dex_file_data->checksum;
1060   }
1061 
GetNumTypeIdsDexReferenceDumper1062   uint32_t GetNumTypeIds() const {
1063     return dex_file_data->num_type_ids;
1064   }
1065 
GetNumMethodIdsDexReferenceDumper1066   uint32_t GetNumMethodIds() const {
1067     return dex_file_data->num_method_ids;
1068   }
1069 
1070   const DexFileData* dex_file_data;
1071 };
1072 
DumpDexReference(ProfileIndexType profile_index)1073 inline ProfileCompilationInfo::DexReferenceDumper ProfileCompilationInfo::DumpDexReference(
1074     ProfileIndexType profile_index) const {
1075   return DexReferenceDumper{info_[profile_index].get()};
1076 }
1077 
1078 std::ostream& operator<<(std::ostream& stream, ProfileCompilationInfo::DexReferenceDumper dumper);
1079 
1080 }  // namespace art
1081 
1082 #endif  // ART_LIBPROFILE_PROFILE_PROFILE_COMPILATION_INFO_H_
1083