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