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