Home | History | Annotate | Download | only in jit
      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_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
     18 #define ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
     19 
     20 #include <set>
     21 #include <vector>
     22 
     23 #include "base/arena_containers.h"
     24 #include "base/arena_object.h"
     25 #include "base/atomic.h"
     26 #include "base/safe_map.h"
     27 #include "bit_memory_region.h"
     28 #include "dex/dex_cache_resolved_classes.h"
     29 #include "dex/dex_file.h"
     30 #include "dex/dex_file_types.h"
     31 #include "dex/method_reference.h"
     32 #include "dex/type_reference.h"
     33 #include "mem_map.h"
     34 
     35 namespace art {
     36 
     37 /**
     38  *  Convenient class to pass around profile information (including inline caches)
     39  *  without the need to hold GC-able objects.
     40  */
     41 struct ProfileMethodInfo {
     42   struct ProfileInlineCache {
     43     ProfileInlineCache(uint32_t pc,
     44                        bool missing_types,
     45                        const std::vector<TypeReference>& profile_classes)
     46         : dex_pc(pc), is_missing_types(missing_types), classes(profile_classes) {}
     47 
     48     const uint32_t dex_pc;
     49     const bool is_missing_types;
     50     const std::vector<TypeReference> classes;
     51   };
     52 
     53   explicit ProfileMethodInfo(MethodReference reference) : ref(reference) {}
     54 
     55   ProfileMethodInfo(MethodReference reference, const std::vector<ProfileInlineCache>& caches)
     56       : ref(reference),
     57         inline_caches(caches) {}
     58 
     59   MethodReference ref;
     60   std::vector<ProfileInlineCache> inline_caches;
     61 };
     62 
     63 /**
     64  * Profile information in a format suitable to be queried by the compiler and
     65  * performing profile guided compilation.
     66  * It is a serialize-friendly format based on information collected by the
     67  * interpreter (ProfileInfo).
     68  * Currently it stores only the hot compiled methods.
     69  */
     70 class ProfileCompilationInfo {
     71  public:
     72   static const uint8_t kProfileMagic[];
     73   static const uint8_t kProfileVersion[];
     74 
     75   static const char* kDexMetadataProfileEntry;
     76 
     77   // Data structures for encoding the offline representation of inline caches.
     78   // This is exposed as public in order to make it available to dex2oat compilations
     79   // (see compiler/optimizing/inliner.cc).
     80 
     81   // A dex location together with its checksum.
     82   struct DexReference {
     83     DexReference() : dex_checksum(0), num_method_ids(0) {}
     84 
     85     DexReference(const std::string& location, uint32_t checksum, uint32_t num_methods)
     86         : dex_location(location), dex_checksum(checksum), num_method_ids(num_methods) {}
     87 
     88     bool operator==(const DexReference& other) const {
     89       return dex_checksum == other.dex_checksum &&
     90           dex_location == other.dex_location &&
     91           num_method_ids == other.num_method_ids;
     92     }
     93 
     94     bool MatchesDex(const DexFile* dex_file) const {
     95       return dex_checksum == dex_file->GetLocationChecksum() &&
     96            dex_location == GetProfileDexFileKey(dex_file->GetLocation());
     97     }
     98 
     99     std::string dex_location;
    100     uint32_t dex_checksum;
    101     uint32_t num_method_ids;
    102   };
    103 
    104   // Encodes a class reference in the profile.
    105   // The owning dex file is encoded as the index (dex_profile_index) it has in the
    106   // profile rather than as a full DexRefence(location,checksum).
    107   // This avoids excessive string copying when managing the profile data.
    108   // The dex_profile_index is an index in either of:
    109   //  - OfflineProfileMethodInfo#dex_references vector (public use)
    110   //  - DexFileData#profile_index (internal use).
    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 {
    116     ClassReference(uint8_t 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     uint8_t 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   // The set of classes that can be found at a given dex pc.
    133   using ClassSet = ArenaSet<ClassReference>;
    134 
    135   // Encodes the actual inline cache for a given dex pc (whether or not the receiver is
    136   // megamorphic and its possible types).
    137   // If the receiver is megamorphic or is missing types the set of classes will be empty.
    138   struct DexPcData : public ArenaObject<kArenaAllocProfile> {
    139     explicit DexPcData(ArenaAllocator* allocator)
    140         : is_missing_types(false),
    141           is_megamorphic(false),
    142           classes(std::less<ClassReference>(), allocator->Adapter(kArenaAllocProfile)) {}
    143     void AddClass(uint16_t dex_profile_idx, const dex::TypeIndex& type_idx);
    144     void SetIsMegamorphic() {
    145       if (is_missing_types) return;
    146       is_megamorphic = true;
    147       classes.clear();
    148     }
    149     void SetIsMissingTypes() {
    150       is_megamorphic = false;
    151       is_missing_types = true;
    152       classes.clear();
    153     }
    154     bool operator==(const DexPcData& other) const {
    155       return is_megamorphic == other.is_megamorphic &&
    156           is_missing_types == other.is_missing_types &&
    157           classes == other.classes;
    158     }
    159 
    160     // Not all runtime types can be encoded in the profile. For example if the receiver
    161     // type is in a dex file which is not tracked for profiling its type cannot be
    162     // encoded. When types are missing this field will be set to true.
    163     bool is_missing_types;
    164     bool is_megamorphic;
    165     ClassSet classes;
    166   };
    167 
    168   // The inline cache map: DexPc -> DexPcData.
    169   using InlineCacheMap = ArenaSafeMap<uint16_t, DexPcData>;
    170 
    171   // Maps a method dex index to its inline cache.
    172   using MethodMap = ArenaSafeMap<uint16_t, InlineCacheMap>;
    173 
    174   // Profile method hotness information for a single method. Also includes a pointer to the inline
    175   // cache map.
    176   class MethodHotness {
    177    public:
    178     enum Flag {
    179       kFlagHot = 0x1,
    180       kFlagStartup = 0x2,
    181       kFlagPostStartup = 0x4,
    182     };
    183 
    184     bool IsHot() const {
    185       return (flags_ & kFlagHot) != 0;
    186     }
    187 
    188     bool IsStartup() const {
    189       return (flags_ & kFlagStartup) != 0;
    190     }
    191 
    192     bool IsPostStartup() const {
    193       return (flags_ & kFlagPostStartup) != 0;
    194     }
    195 
    196     void AddFlag(Flag flag) {
    197       flags_ |= flag;
    198     }
    199 
    200     uint8_t GetFlags() const {
    201       return flags_;
    202     }
    203 
    204     bool IsInProfile() const {
    205       return flags_ != 0;
    206     }
    207 
    208    private:
    209     const InlineCacheMap* inline_cache_map_ = nullptr;
    210     uint8_t flags_ = 0;
    211 
    212     const InlineCacheMap* GetInlineCacheMap() const {
    213       return inline_cache_map_;
    214     }
    215 
    216     void SetInlineCacheMap(const InlineCacheMap* info) {
    217       inline_cache_map_ = info;
    218     }
    219 
    220     friend class ProfileCompilationInfo;
    221   };
    222 
    223   // Encodes the full set of inline caches for a given method.
    224   // The dex_references vector is indexed according to the ClassReference::dex_profile_index.
    225   // i.e. the dex file of any ClassReference present in the inline caches can be found at
    226   // dex_references[ClassReference::dex_profile_index].
    227   struct OfflineProfileMethodInfo {
    228     explicit OfflineProfileMethodInfo(const InlineCacheMap* inline_cache_map)
    229         : inline_caches(inline_cache_map) {}
    230 
    231     bool operator==(const OfflineProfileMethodInfo& other) const;
    232 
    233     const InlineCacheMap* const inline_caches;
    234     std::vector<DexReference> dex_references;
    235   };
    236 
    237   // Public methods to create, extend or query the profile.
    238   ProfileCompilationInfo();
    239   explicit ProfileCompilationInfo(ArenaPool* arena_pool);
    240 
    241   ~ProfileCompilationInfo();
    242 
    243   // Add the given methods to the current profile object.
    244   bool AddMethods(const std::vector<ProfileMethodInfo>& methods, MethodHotness::Flag flags);
    245 
    246   // Add the given classes to the current profile object.
    247   bool AddClasses(const std::set<DexCacheResolvedClasses>& resolved_classes);
    248 
    249   // Add multiple type ids for classes in a single dex file. Iterator is for type_ids not
    250   // class_defs.
    251   template <class Iterator>
    252   bool AddClassesForDex(const DexFile* dex_file, Iterator index_begin, Iterator index_end) {
    253     DexFileData* data = GetOrAddDexFileData(dex_file);
    254     if (data == nullptr) {
    255       return false;
    256     }
    257     data->class_set.insert(index_begin, index_end);
    258     return true;
    259   }
    260   // Add a single type id for a dex file.
    261   bool AddClassForDex(const TypeReference& ref) {
    262     DexFileData* data = GetOrAddDexFileData(ref.dex_file);
    263     if (data == nullptr) {
    264       return false;
    265     }
    266     data->class_set.insert(ref.TypeIndex());
    267     return true;
    268   }
    269 
    270 
    271   // Add a method index to the profile (without inline caches). The method flags determine if it is
    272   // hot, startup, or post startup, or a combination of the previous.
    273   bool AddMethodIndex(MethodHotness::Flag flags,
    274                       const std::string& dex_location,
    275                       uint32_t checksum,
    276                       uint16_t method_idx,
    277                       uint32_t num_method_ids);
    278   bool AddMethodIndex(MethodHotness::Flag flags, const MethodReference& ref);
    279 
    280   // Add a method to the profile using its online representation (containing runtime structures).
    281   bool AddMethod(const ProfileMethodInfo& pmi, MethodHotness::Flag flags);
    282 
    283   // Bulk add sampled methods and/or hot methods for a single dex, fast since it only has one
    284   // GetOrAddDexFileData call.
    285   template <class Iterator>
    286   bool AddMethodsForDex(MethodHotness::Flag flags,
    287                         const DexFile* dex_file,
    288                         Iterator index_begin,
    289                         Iterator index_end) {
    290     DexFileData* data = GetOrAddDexFileData(dex_file);
    291     if (data == nullptr) {
    292       return false;
    293     }
    294     for (Iterator it = index_begin; it != index_end; ++it) {
    295       DCHECK_LT(*it, data->num_method_ids);
    296       if (!data->AddMethod(flags, *it)) {
    297         return false;
    298       }
    299     }
    300     return true;
    301   }
    302 
    303   // Add hotness flags for a simple method.
    304   bool AddMethodHotness(const MethodReference& method_ref, const MethodHotness& hotness);
    305 
    306   // Load or Merge profile information from the given file descriptor.
    307   // If the current profile is non-empty the load will fail.
    308   // If merge_classes is set to false, classes will not be merged/loaded.
    309   // If filter_fn is present, it will be used to filter out profile data belonging
    310   // to dex file which do not comply with the filter
    311   // (i.e. for which filter_fn(dex_location, dex_checksum) is false).
    312   using ProfileLoadFilterFn = std::function<bool(const std::string&, uint32_t)>;
    313   // Profile filter method which accepts all dex locations.
    314   // This is convenient to use when we need to accept all locations without repeating the same
    315   // lambda.
    316   static bool ProfileFilterFnAcceptAll(const std::string& dex_location, uint32_t checksum);
    317 
    318   bool Load(
    319       int fd,
    320       bool merge_classes = true,
    321       const ProfileLoadFilterFn& filter_fn = ProfileFilterFnAcceptAll);
    322 
    323   // Verify integrity of the profile file with the provided dex files.
    324   // If there exists a DexData object which maps to a dex_file, then it verifies that:
    325   // - The checksums of the DexData and dex_file are equals.
    326   // - No method id exceeds NumMethodIds corresponding to the dex_file.
    327   // - No class id exceeds NumTypeIds corresponding to the dex_file.
    328   // - For every inline_caches, class_ids does not exceed NumTypeIds corresponding to
    329   //   the dex_file they are in.
    330   bool VerifyProfileData(const std::vector<const DexFile *> &dex_files);
    331 
    332   // Load profile information from the given file
    333   // If the current profile is non-empty the load will fail.
    334   // If clear_if_invalid is true and the file is invalid the method clears the
    335   // the file and returns true.
    336   bool Load(const std::string& filename, bool clear_if_invalid);
    337 
    338   // Merge the data from another ProfileCompilationInfo into the current object. Only merges
    339   // classes if merge_classes is true. This is used for creating the boot profile since
    340   // we don't want all of the classes to be image classes.
    341   bool MergeWith(const ProfileCompilationInfo& info, bool merge_classes = true);
    342 
    343   // Merge profile information from the given file descriptor.
    344   bool MergeWith(const std::string& filename);
    345 
    346   // Save the profile data to the given file descriptor.
    347   bool Save(int fd);
    348 
    349   // Save the current profile into the given file. The file will be cleared before saving.
    350   bool Save(const std::string& filename, uint64_t* bytes_written);
    351 
    352   // Return the number of methods that were profiled.
    353   uint32_t GetNumberOfMethods() const;
    354 
    355   // Return the number of resolved classes that were profiled.
    356   uint32_t GetNumberOfResolvedClasses() const;
    357 
    358   // Returns the profile method info for a given method reference.
    359   MethodHotness GetMethodHotness(const MethodReference& method_ref) const;
    360   MethodHotness GetMethodHotness(const std::string& dex_location,
    361                                  uint32_t dex_checksum,
    362                                  uint16_t dex_method_index) const;
    363 
    364   // Return true if the class's type is present in the profiling info.
    365   bool ContainsClass(const DexFile& dex_file, dex::TypeIndex type_idx) const;
    366 
    367   // Return the method data for the given location and index from the profiling info.
    368   // If the method index is not found or the checksum doesn't match, null is returned.
    369   // Note: the inline cache map is a pointer to the map stored in the profile and
    370   // its allocation will go away if the profile goes out of scope.
    371   std::unique_ptr<OfflineProfileMethodInfo> GetMethod(const std::string& dex_location,
    372                                                       uint32_t dex_checksum,
    373                                                       uint16_t dex_method_index) const;
    374 
    375   // Dump all the loaded profile info into a string and returns it.
    376   // If dex_files is not null then the method indices will be resolved to their
    377   // names.
    378   // This is intended for testing and debugging.
    379   std::string DumpInfo(const std::vector<std::unique_ptr<const DexFile>>* dex_files,
    380                        bool print_full_dex_location = true) const;
    381   std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
    382                        bool print_full_dex_location = true) const;
    383 
    384   // Return the classes and methods for a given dex file through out args. The out args are the set
    385   // of class as well as the methods and their associated inline caches. Returns true if the dex
    386   // file is register and has a matching checksum, false otherwise.
    387   bool GetClassesAndMethods(const DexFile& dex_file,
    388                             /*out*/std::set<dex::TypeIndex>* class_set,
    389                             /*out*/std::set<uint16_t>* hot_method_set,
    390                             /*out*/std::set<uint16_t>* startup_method_set,
    391                             /*out*/std::set<uint16_t>* post_startup_method_method_set) const;
    392 
    393   // Perform an equality test with the `other` profile information.
    394   bool Equals(const ProfileCompilationInfo& other);
    395 
    396   // Return the class descriptors for all of the classes in the profiles' class sets.
    397   std::set<DexCacheResolvedClasses> GetResolvedClasses(
    398       const std::vector<const DexFile*>& dex_files_) const;
    399 
    400   // Return the profile key associated with the given dex location.
    401   static std::string GetProfileDexFileKey(const std::string& dex_location);
    402 
    403   // Generate a test profile which will contain a percentage of the total maximum
    404   // number of methods and classes (method_ratio and class_ratio).
    405   static bool GenerateTestProfile(int fd,
    406                                   uint16_t number_of_dex_files,
    407                                   uint16_t method_ratio,
    408                                   uint16_t class_ratio,
    409                                   uint32_t random_seed);
    410 
    411   // Generate a test profile which will randomly contain classes and methods from
    412   // the provided list of dex files.
    413   static bool GenerateTestProfile(int fd,
    414                                   std::vector<std::unique_ptr<const DexFile>>& dex_files,
    415                                   uint16_t method_percentage,
    416                                   uint16_t class_percentage,
    417                                   uint32_t random_seed);
    418 
    419   // Check that the given profile method info contain the same data.
    420   static bool Equals(const ProfileCompilationInfo::OfflineProfileMethodInfo& pmi1,
    421                      const ProfileCompilationInfo::OfflineProfileMethodInfo& pmi2);
    422 
    423   ArenaAllocator* GetAllocator() { return &allocator_; }
    424 
    425   // Return all of the class descriptors in the profile for a set of dex files.
    426   std::unordered_set<std::string> GetClassDescriptors(const std::vector<const DexFile*>& dex_files);
    427 
    428   // Return true if the fd points to a profile file.
    429   bool IsProfileFile(int fd);
    430 
    431   // Update the profile keys corresponding to the given dex files based on their current paths.
    432   // This method allows fix-ups in the profile for dex files that might have been renamed.
    433   // The new profile key will be constructed based on the current dex location.
    434   //
    435   // The matching [profile key <-> dex_file] is done based on the dex checksum and the number of
    436   // methods ids. If neither is a match then the profile key is not updated.
    437   //
    438   // If the new profile key would collide with an existing key (for a different dex)
    439   // the method returns false. Otherwise it returns true.
    440   bool UpdateProfileKeys(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
    441 
    442   // Checks if the profile is empty.
    443   bool IsEmpty() const;
    444 
    445   // Clears all the data from the profile.
    446   void ClearData();
    447 
    448  private:
    449   enum ProfileLoadStatus {
    450     kProfileLoadWouldOverwiteData,
    451     kProfileLoadIOError,
    452     kProfileLoadVersionMismatch,
    453     kProfileLoadBadData,
    454     kProfileLoadSuccess
    455   };
    456 
    457   const uint32_t kProfileSizeWarningThresholdInBytes = 500000U;
    458   const uint32_t kProfileSizeErrorThresholdInBytes = 1000000U;
    459 
    460   // Internal representation of the profile information belonging to a dex file.
    461   // Note that we could do without profile_key (the key used to encode the dex
    462   // file in the profile) and profile_index (the index of the dex file in the
    463   // profile) fields in this struct because we can infer them from
    464   // profile_key_map_ and info_. However, it makes the profiles logic much
    465   // simpler if we have references here as well.
    466   struct DexFileData : public DeletableArenaObject<kArenaAllocProfile> {
    467     DexFileData(ArenaAllocator* allocator,
    468                 const std::string& key,
    469                 uint32_t location_checksum,
    470                 uint16_t index,
    471                 uint32_t num_methods)
    472         : allocator_(allocator),
    473           profile_key(key),
    474           profile_index(index),
    475           checksum(location_checksum),
    476           method_map(std::less<uint16_t>(), allocator->Adapter(kArenaAllocProfile)),
    477           class_set(std::less<dex::TypeIndex>(), allocator->Adapter(kArenaAllocProfile)),
    478           num_method_ids(num_methods),
    479           bitmap_storage(allocator->Adapter(kArenaAllocProfile)) {
    480       bitmap_storage.resize(ComputeBitmapStorage(num_method_ids));
    481       if (!bitmap_storage.empty()) {
    482         method_bitmap =
    483             BitMemoryRegion(MemoryRegion(
    484                 &bitmap_storage[0], bitmap_storage.size()), 0, ComputeBitmapBits(num_method_ids));
    485       }
    486     }
    487 
    488     static size_t ComputeBitmapBits(uint32_t num_method_ids) {
    489       return num_method_ids * kBitmapIndexCount;
    490     }
    491     static size_t ComputeBitmapStorage(uint32_t num_method_ids) {
    492       return RoundUp(ComputeBitmapBits(num_method_ids), kBitsPerByte) / kBitsPerByte;
    493     }
    494 
    495     bool operator==(const DexFileData& other) const {
    496       return checksum == other.checksum && method_map == other.method_map;
    497     }
    498 
    499     // Mark a method as executed at least once.
    500     bool AddMethod(MethodHotness::Flag flags, size_t index);
    501 
    502     void MergeBitmap(const DexFileData& other) {
    503       DCHECK_EQ(bitmap_storage.size(), other.bitmap_storage.size());
    504       for (size_t i = 0; i < bitmap_storage.size(); ++i) {
    505         bitmap_storage[i] |= other.bitmap_storage[i];
    506       }
    507     }
    508 
    509     void SetMethodHotness(size_t index, MethodHotness::Flag flags);
    510     MethodHotness GetHotnessInfo(uint32_t dex_method_index) const;
    511 
    512     // The allocator used to allocate new inline cache maps.
    513     ArenaAllocator* const allocator_;
    514     // The profile key this data belongs to.
    515     std::string profile_key;
    516     // The profile index of this dex file (matches ClassReference#dex_profile_index).
    517     uint8_t profile_index;
    518     // The dex checksum.
    519     uint32_t checksum;
    520     // The methonds' profile information.
    521     MethodMap method_map;
    522     // The classes which have been profiled. Note that these don't necessarily include
    523     // all the classes that can be found in the inline caches reference.
    524     ArenaSet<dex::TypeIndex> class_set;
    525     // Find the inline caches of the the given method index. Add an empty entry if
    526     // no previous data is found.
    527     InlineCacheMap* FindOrAddMethod(uint16_t method_index);
    528     // Num method ids.
    529     uint32_t num_method_ids;
    530     ArenaVector<uint8_t> bitmap_storage;
    531     BitMemoryRegion method_bitmap;
    532 
    533    private:
    534     enum BitmapIndex {
    535       kBitmapIndexStartup,
    536       kBitmapIndexPostStartup,
    537       kBitmapIndexCount,
    538     };
    539 
    540     size_t MethodBitIndex(bool startup, size_t index) const {
    541       DCHECK_LT(index, num_method_ids);
    542       // The format is [startup bitmap][post startup bitmap]
    543       // This compresses better than ([startup bit][post statup bit])*
    544 
    545       return index + (startup
    546           ? kBitmapIndexStartup * num_method_ids
    547           : kBitmapIndexPostStartup * num_method_ids);
    548     }
    549   };
    550 
    551   // Return the profile data for the given profile key or null if the dex location
    552   // already exists but has a different checksum
    553   DexFileData* GetOrAddDexFileData(const std::string& profile_key,
    554                                    uint32_t checksum,
    555                                    uint32_t num_method_ids);
    556 
    557   DexFileData* GetOrAddDexFileData(const DexFile* dex_file) {
    558     return GetOrAddDexFileData(GetProfileDexFileKey(dex_file->GetLocation()),
    559                                dex_file->GetLocationChecksum(),
    560                                dex_file->NumMethodIds());
    561   }
    562 
    563   // Add a method to the profile using its offline representation.
    564   // This is mostly used to facilitate testing.
    565   bool AddMethod(const std::string& dex_location,
    566                  uint32_t dex_checksum,
    567                  uint16_t method_index,
    568                  uint32_t num_method_ids,
    569                  const OfflineProfileMethodInfo& pmi,
    570                  MethodHotness::Flag flags);
    571 
    572   // Add a class index to the profile.
    573   bool AddClassIndex(const std::string& dex_location,
    574                      uint32_t checksum,
    575                      dex::TypeIndex type_idx,
    576                      uint32_t num_method_ids);
    577 
    578   // Add all classes from the given dex cache to the the profile.
    579   bool AddResolvedClasses(const DexCacheResolvedClasses& classes);
    580 
    581   // Encode the known dex_files into a vector. The index of a dex_reference will
    582   // be the same as the profile index of the dex file (used to encode the ClassReferences).
    583   void DexFileToProfileIndex(/*out*/std::vector<DexReference>* dex_references) const;
    584 
    585   // Return the dex data associated with the given profile key or null if the profile
    586   // doesn't contain the key.
    587   const DexFileData* FindDexData(const std::string& profile_key,
    588                                  uint32_t checksum,
    589                                  bool verify_checksum = true) const;
    590 
    591   // Return the dex data associated with the given dex file or null if the profile doesn't contain
    592   // the key or the checksum mismatches.
    593   const DexFileData* FindDexData(const DexFile* dex_file) const;
    594 
    595   // Inflate the input buffer (in_buffer) of size in_size. It returns a buffer of
    596   // compressed data for the input buffer of "compressed_data_size" size.
    597   std::unique_ptr<uint8_t[]> DeflateBuffer(const uint8_t* in_buffer,
    598                                            uint32_t in_size,
    599                                            /*out*/uint32_t* compressed_data_size);
    600 
    601   // Inflate the input buffer(in_buffer) of size in_size. out_size is the expected output
    602   // size of the buffer. It puts the output in out_buffer. It returns Z_STREAM_END on
    603   // success. On error, it returns Z_STREAM_ERROR if the compressed data is inconsistent
    604   // and Z_DATA_ERROR if the stream ended prematurely or the stream has extra data.
    605   int InflateBuffer(const uint8_t* in_buffer,
    606                     uint32_t in_size,
    607                     uint32_t out_size,
    608                     /*out*/uint8_t* out_buffer);
    609 
    610   // Parsing functionality.
    611 
    612   // The information present in the header of each profile line.
    613   struct ProfileLineHeader {
    614     std::string dex_location;
    615     uint16_t class_set_size;
    616     uint32_t method_region_size_bytes;
    617     uint32_t checksum;
    618     uint32_t num_method_ids;
    619   };
    620 
    621   /**
    622    * Encapsulate the source of profile data for loading.
    623    * The source can be either a plain file or a zip file.
    624    * For zip files, the profile entry will be extracted to
    625    * the memory map.
    626    */
    627   class ProfileSource {
    628    public:
    629     /**
    630      * Create a profile source for the given fd. The ownership of the fd
    631      * remains to the caller; as this class will not attempt to close it at any
    632      * point.
    633      */
    634     static ProfileSource* Create(int32_t fd) {
    635       DCHECK_GT(fd, -1);
    636       return new ProfileSource(fd, /*map*/ nullptr);
    637     }
    638 
    639     /**
    640      * Create a profile source backed by a memory map. The map can be null in
    641      * which case it will the treated as an empty source.
    642      */
    643     static ProfileSource* Create(std::unique_ptr<MemMap>&& mem_map) {
    644       return new ProfileSource(/*fd*/ -1, std::move(mem_map));
    645     }
    646 
    647     /**
    648      * Read bytes from this source.
    649      * Reading will advance the current source position so subsequent
    650      * invocations will read from the las position.
    651      */
    652     ProfileLoadStatus Read(uint8_t* buffer,
    653                            size_t byte_count,
    654                            const std::string& debug_stage,
    655                            std::string* error);
    656 
    657     /** Return true if the source has 0 data. */
    658     bool HasEmptyContent() const;
    659     /** Return true if all the information from this source has been read. */
    660     bool HasConsumedAllData() const;
    661 
    662    private:
    663     ProfileSource(int32_t fd, std::unique_ptr<MemMap>&& mem_map)
    664         : fd_(fd), mem_map_(std::move(mem_map)), mem_map_cur_(0) {}
    665 
    666     bool IsMemMap() const { return fd_ == -1; }
    667 
    668     int32_t fd_;  // The fd is not owned by this class.
    669     std::unique_ptr<MemMap> mem_map_;
    670     size_t mem_map_cur_;  // Current position in the map to read from.
    671   };
    672 
    673   // A helper structure to make sure we don't read past our buffers in the loops.
    674   struct SafeBuffer {
    675    public:
    676     explicit SafeBuffer(size_t size) : storage_(new uint8_t[size]) {
    677       ptr_current_ = storage_.get();
    678       ptr_end_ = ptr_current_ + size;
    679     }
    680 
    681     // Reads the content of the descriptor at the current position.
    682     ProfileLoadStatus Fill(ProfileSource& source,
    683                            const std::string& debug_stage,
    684                            /*out*/std::string* error);
    685 
    686     // Reads an uint value (high bits to low bits) and advances the current pointer
    687     // with the number of bits read.
    688     template <typename T> bool ReadUintAndAdvance(/*out*/ T* value);
    689 
    690     // Compares the given data with the content current pointer. If the contents are
    691     // equal it advances the current pointer by data_size.
    692     bool CompareAndAdvance(const uint8_t* data, size_t data_size);
    693 
    694     // Advances current pointer by data_size.
    695     void Advance(size_t data_size);
    696 
    697     // Returns the count of unread bytes.
    698     size_t CountUnreadBytes();
    699 
    700     // Returns the current pointer.
    701     const uint8_t* GetCurrentPtr();
    702 
    703     // Get the underlying raw buffer.
    704     uint8_t* Get() { return storage_.get(); }
    705 
    706    private:
    707     std::unique_ptr<uint8_t[]> storage_;
    708     uint8_t* ptr_end_;
    709     uint8_t* ptr_current_;
    710   };
    711 
    712   ProfileLoadStatus OpenSource(int32_t fd,
    713                                /*out*/ std::unique_ptr<ProfileSource>* source,
    714                                /*out*/ std::string* error);
    715 
    716   // Entry point for profile loading functionality.
    717   ProfileLoadStatus LoadInternal(
    718       int32_t fd,
    719       std::string* error,
    720       bool merge_classes = true,
    721       const ProfileLoadFilterFn& filter_fn = ProfileFilterFnAcceptAll);
    722 
    723   // Read the profile header from the given fd and store the number of profile
    724   // lines into number_of_dex_files.
    725   ProfileLoadStatus ReadProfileHeader(ProfileSource& source,
    726                                       /*out*/uint8_t* number_of_dex_files,
    727                                       /*out*/uint32_t* size_uncompressed_data,
    728                                       /*out*/uint32_t* size_compressed_data,
    729                                       /*out*/std::string* error);
    730 
    731   // Read the header of a profile line from the given fd.
    732   ProfileLoadStatus ReadProfileLineHeader(SafeBuffer& buffer,
    733                                           /*out*/ProfileLineHeader* line_header,
    734                                           /*out*/std::string* error);
    735 
    736   // Read individual elements from the profile line header.
    737   bool ReadProfileLineHeaderElements(SafeBuffer& buffer,
    738                                      /*out*/uint16_t* dex_location_size,
    739                                      /*out*/ProfileLineHeader* line_header,
    740                                      /*out*/std::string* error);
    741 
    742   // Read a single profile line from the given fd.
    743   ProfileLoadStatus ReadProfileLine(SafeBuffer& buffer,
    744                                     uint8_t number_of_dex_files,
    745                                     const ProfileLineHeader& line_header,
    746                                     const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap,
    747                                     bool merge_classes,
    748                                     /*out*/std::string* error);
    749 
    750   // Read all the classes from the buffer into the profile `info_` structure.
    751   bool ReadClasses(SafeBuffer& buffer,
    752                    const ProfileLineHeader& line_header,
    753                    /*out*/std::string* error);
    754 
    755   // Read all the methods from the buffer into the profile `info_` structure.
    756   bool ReadMethods(SafeBuffer& buffer,
    757                    uint8_t number_of_dex_files,
    758                    const ProfileLineHeader& line_header,
    759                    const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap,
    760                    /*out*/std::string* error);
    761 
    762   // The method generates mapping of profile indices while merging a new profile
    763   // data into current data. It returns true, if the mapping was successful.
    764   bool RemapProfileIndex(const std::vector<ProfileLineHeader>& profile_line_headers,
    765                          const ProfileLoadFilterFn& filter_fn,
    766                          /*out*/SafeMap<uint8_t, uint8_t>* dex_profile_index_remap);
    767 
    768   // Read the inline cache encoding from line_bufer into inline_cache.
    769   bool ReadInlineCache(SafeBuffer& buffer,
    770                        uint8_t number_of_dex_files,
    771                        const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap,
    772                        /*out*/InlineCacheMap* inline_cache,
    773                        /*out*/std::string* error);
    774 
    775   // Encode the inline cache into the given buffer.
    776   void AddInlineCacheToBuffer(std::vector<uint8_t>* buffer,
    777                               const InlineCacheMap& inline_cache);
    778 
    779   // Return the number of bytes needed to encode the profile information
    780   // for the methods in dex_data.
    781   uint32_t GetMethodsRegionSize(const DexFileData& dex_data);
    782 
    783   // Group `classes` by their owning dex profile index and put the result in
    784   // `dex_to_classes_map`.
    785   void GroupClassesByDex(
    786       const ClassSet& classes,
    787       /*out*/SafeMap<uint8_t, std::vector<dex::TypeIndex>>* dex_to_classes_map);
    788 
    789   // Find the data for the dex_pc in the inline cache. Adds an empty entry
    790   // if no previous data exists.
    791   DexPcData* FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc);
    792 
    793   friend class ProfileCompilationInfoTest;
    794   friend class CompilerDriverProfileTest;
    795   friend class ProfileAssistantTest;
    796   friend class Dex2oatLayoutTest;
    797 
    798   ArenaPool default_arena_pool_;
    799   ArenaAllocator allocator_;
    800 
    801   // Vector containing the actual profile info.
    802   // The vector index is the profile index of the dex data and
    803   // matched DexFileData::profile_index.
    804   ArenaVector<DexFileData*> info_;
    805 
    806   // Cache mapping profile keys to profile index.
    807   // This is used to speed up searches since it avoids iterating
    808   // over the info_ vector when searching by profile key.
    809   ArenaSafeMap<const std::string, uint8_t> profile_key_map_;
    810 };
    811 
    812 }  // namespace art
    813 
    814 #endif  // ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
    815