blob: ffb67ae2be85373ffe8d990f821d1292bc472707 [file] [log] [blame]
Calin Juravle31f2c152015-10-23 17:56:15 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Calin Juravle33083d62017-01-18 15:29:12 -080017#ifndef ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
18#define ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_
Calin Juravle31f2c152015-10-23 17:56:15 +010019
20#include <set>
Calin Juravle4d77b6a2015-12-01 18:38:09 +000021#include <vector>
Calin Juravle31f2c152015-10-23 17:56:15 +010022
23#include "atomic.h"
Calin Juravlecc3171a2017-05-19 16:47:53 -070024#include "base/arena_containers.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "base/arena_object.h"
Mathieu Chartierea650f32017-05-24 12:04:13 -070026#include "bit_memory_region.h"
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080027#include "dex_cache_resolved_classes.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010028#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080029#include "dex_file_types.h"
Calin Juravle226501b2015-12-11 14:41:31 +000030#include "method_reference.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010031#include "safe_map.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070032#include "type_reference.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010033
34namespace art {
35
Calin Juravle31f2c152015-10-23 17:56:15 +010036/**
Calin Juravle940eb0c2017-01-30 19:30:44 -080037 * Convenient class to pass around profile information (including inline caches)
38 * without the need to hold GC-able objects.
39 */
40struct ProfileMethodInfo {
Calin Juravle940eb0c2017-01-30 19:30:44 -080041 struct ProfileInlineCache {
Calin Juravle589e71e2017-03-03 16:05:05 -080042 ProfileInlineCache(uint32_t pc,
43 bool missing_types,
Mathieu Chartierdbddc222017-05-24 12:04:13 -070044 const std::vector<TypeReference>& profile_classes)
Calin Juravle589e71e2017-03-03 16:05:05 -080045 : dex_pc(pc), is_missing_types(missing_types), classes(profile_classes) {}
Calin Juravle940eb0c2017-01-30 19:30:44 -080046
47 const uint32_t dex_pc;
Calin Juravle589e71e2017-03-03 16:05:05 -080048 const bool is_missing_types;
Mathieu Chartierdbddc222017-05-24 12:04:13 -070049 const std::vector<TypeReference> classes;
Calin Juravle940eb0c2017-01-30 19:30:44 -080050 };
51
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -070052 explicit ProfileMethodInfo(MethodReference reference) : ref(reference) {}
Calin Juravle940eb0c2017-01-30 19:30:44 -080053
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -070054 ProfileMethodInfo(MethodReference reference, const std::vector<ProfileInlineCache>& caches)
55 : ref(reference),
Mathieu Chartierea650f32017-05-24 12:04:13 -070056 inline_caches(caches) {}
Calin Juravle940eb0c2017-01-30 19:30:44 -080057
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -070058 MethodReference ref;
59 std::vector<ProfileInlineCache> inline_caches;
Calin Juravle940eb0c2017-01-30 19:30:44 -080060};
61
62/**
Calin Juravle998c2162015-12-21 15:39:33 +020063 * Profile information in a format suitable to be queried by the compiler and
64 * performing profile guided compilation.
65 * It is a serialize-friendly format based on information collected by the
66 * interpreter (ProfileInfo).
Calin Juravle31f2c152015-10-23 17:56:15 +010067 * Currently it stores only the hot compiled methods.
68 */
Calin Juravle226501b2015-12-11 14:41:31 +000069class ProfileCompilationInfo {
70 public:
Calin Juravle64142952016-03-21 14:37:55 +000071 static const uint8_t kProfileMagic[];
72 static const uint8_t kProfileVersion[];
73
Calin Juravle940eb0c2017-01-30 19:30:44 -080074 // Data structures for encoding the offline representation of inline caches.
75 // This is exposed as public in order to make it available to dex2oat compilations
76 // (see compiler/optimizing/inliner.cc).
77
78 // A dex location together with its checksum.
79 struct DexReference {
Mathieu Chartierea650f32017-05-24 12:04:13 -070080 DexReference() : dex_checksum(0), num_method_ids(0) {}
Calin Juravle940eb0c2017-01-30 19:30:44 -080081
Mathieu Chartierea650f32017-05-24 12:04:13 -070082 DexReference(const std::string& location, uint32_t checksum, uint32_t num_methods)
83 : dex_location(location), dex_checksum(checksum), num_method_ids(num_methods) {}
Calin Juravle940eb0c2017-01-30 19:30:44 -080084
85 bool operator==(const DexReference& other) const {
Mathieu Chartierea650f32017-05-24 12:04:13 -070086 return dex_checksum == other.dex_checksum &&
87 dex_location == other.dex_location &&
88 num_method_ids == other.num_method_ids;
Calin Juravle940eb0c2017-01-30 19:30:44 -080089 }
90
Calin Juravlee0ac1152017-02-13 19:03:47 -080091 bool MatchesDex(const DexFile* dex_file) const {
92 return dex_checksum == dex_file->GetLocationChecksum() &&
93 dex_location == GetProfileDexFileKey(dex_file->GetLocation());
94 }
95
Calin Juravle940eb0c2017-01-30 19:30:44 -080096 std::string dex_location;
97 uint32_t dex_checksum;
Mathieu Chartierea650f32017-05-24 12:04:13 -070098 uint32_t num_method_ids;
Calin Juravle940eb0c2017-01-30 19:30:44 -080099 };
100
101 // Encodes a class reference in the profile.
102 // The owning dex file is encoded as the index (dex_profile_index) it has in the
103 // profile rather than as a full DexRefence(location,checksum).
104 // This avoids excessive string copying when managing the profile data.
105 // The dex_profile_index is an index in either of:
106 // - OfflineProfileMethodInfo#dex_references vector (public use)
107 // - DexFileData#profile_index (internal use).
108 // Note that the dex_profile_index is not necessary the multidex index.
109 // We cannot rely on the actual multidex index because a single profile may store
110 // data from multiple splits. This means that a profile may contain a classes2.dex from split-A
111 // and one from split-B.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700112 struct ClassReference : public ValueObject {
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700113 ClassReference(uint8_t dex_profile_idx, const dex::TypeIndex type_idx) :
Calin Juravle940eb0c2017-01-30 19:30:44 -0800114 dex_profile_index(dex_profile_idx), type_index(type_idx) {}
115
116 bool operator==(const ClassReference& other) const {
117 return dex_profile_index == other.dex_profile_index && type_index == other.type_index;
118 }
119 bool operator<(const ClassReference& other) const {
120 return dex_profile_index == other.dex_profile_index
121 ? type_index < other.type_index
122 : dex_profile_index < other.dex_profile_index;
123 }
124
125 uint8_t dex_profile_index; // the index of the owning dex in the profile info
126 dex::TypeIndex type_index; // the type index of the class
127 };
128
129 // The set of classes that can be found at a given dex pc.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700130 using ClassSet = ArenaSet<ClassReference>;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800131
132 // Encodes the actual inline cache for a given dex pc (whether or not the receiver is
133 // megamorphic and its possible types).
Calin Juravle589e71e2017-03-03 16:05:05 -0800134 // If the receiver is megamorphic or is missing types the set of classes will be empty.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700135 struct DexPcData : public ArenaObject<kArenaAllocProfile> {
136 explicit DexPcData(ArenaAllocator* arena)
137 : is_missing_types(false),
138 is_megamorphic(false),
139 classes(std::less<ClassReference>(), arena->Adapter(kArenaAllocProfile)) {}
Calin Juravle940eb0c2017-01-30 19:30:44 -0800140 void AddClass(uint16_t dex_profile_idx, const dex::TypeIndex& type_idx);
Calin Juravle589e71e2017-03-03 16:05:05 -0800141 void SetIsMegamorphic() {
142 if (is_missing_types) return;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800143 is_megamorphic = true;
144 classes.clear();
145 }
Calin Juravle589e71e2017-03-03 16:05:05 -0800146 void SetIsMissingTypes() {
147 is_megamorphic = false;
148 is_missing_types = true;
149 classes.clear();
150 }
Calin Juravle940eb0c2017-01-30 19:30:44 -0800151 bool operator==(const DexPcData& other) const {
Calin Juravle589e71e2017-03-03 16:05:05 -0800152 return is_megamorphic == other.is_megamorphic &&
153 is_missing_types == other.is_missing_types &&
154 classes == other.classes;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800155 }
156
Calin Juravle589e71e2017-03-03 16:05:05 -0800157 // Not all runtime types can be encoded in the profile. For example if the receiver
158 // type is in a dex file which is not tracked for profiling its type cannot be
159 // encoded. When types are missing this field will be set to true.
160 bool is_missing_types;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800161 bool is_megamorphic;
162 ClassSet classes;
163 };
164
165 // The inline cache map: DexPc -> DexPcData.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700166 using InlineCacheMap = ArenaSafeMap<uint16_t, DexPcData>;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800167
Mathieu Chartier34067262017-04-06 13:55:46 -0700168 // Maps a method dex index to its inline cache.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700169 using MethodMap = ArenaSafeMap<uint16_t, InlineCacheMap>;
Mathieu Chartier34067262017-04-06 13:55:46 -0700170
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700171 // Profile method hotness information for a single method. Also includes a pointer to the inline
172 // cache map.
173 class MethodHotness {
174 public:
175 enum Flag {
176 kFlagHot = 0x1,
177 kFlagStartup = 0x2,
178 kFlagPostStartup = 0x4,
179 };
180
181 bool IsHot() const {
182 return (flags_ & kFlagHot) != 0;
183 }
184
185 bool IsStartup() const {
186 return (flags_ & kFlagStartup) != 0;
187 }
188
189 bool IsPostStartup() const {
190 return (flags_ & kFlagPostStartup) != 0;
191 }
192
193 void AddFlag(Flag flag) {
194 flags_ |= flag;
195 }
196
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -0700197 uint8_t GetFlags() const {
198 return flags_;
199 }
200
Mathieu Chartiere46f3a82017-06-19 19:54:12 -0700201 bool IsInProfile() const {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700202 return flags_ != 0;
203 }
204
Mathieu Chartiere46f3a82017-06-19 19:54:12 -0700205 private:
206 const InlineCacheMap* inline_cache_map_ = nullptr;
207 uint8_t flags_ = 0;
208
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700209 const InlineCacheMap* GetInlineCacheMap() const {
210 return inline_cache_map_;
211 }
212
213 void SetInlineCacheMap(const InlineCacheMap* info) {
214 inline_cache_map_ = info;
215 }
216
Mathieu Chartiere46f3a82017-06-19 19:54:12 -0700217 friend class ProfileCompilationInfo;
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700218 };
219
Calin Juravle940eb0c2017-01-30 19:30:44 -0800220 // Encodes the full set of inline caches for a given method.
221 // The dex_references vector is indexed according to the ClassReference::dex_profile_index.
222 // i.e. the dex file of any ClassReference present in the inline caches can be found at
223 // dex_references[ClassReference::dex_profile_index].
224 struct OfflineProfileMethodInfo {
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700225 explicit OfflineProfileMethodInfo(const InlineCacheMap* inline_cache_map)
226 : inline_caches(inline_cache_map) {}
Calin Juravlecc3171a2017-05-19 16:47:53 -0700227
Calin Juravle940eb0c2017-01-30 19:30:44 -0800228 bool operator==(const OfflineProfileMethodInfo& other) const;
229
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700230 const InlineCacheMap* const inline_caches;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800231 std::vector<DexReference> dex_references;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800232 };
233
234 // Public methods to create, extend or query the profile.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700235 ProfileCompilationInfo();
236 explicit ProfileCompilationInfo(ArenaPool* arena_pool);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800237
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700238 ~ProfileCompilationInfo();
239
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700240 // Add the given methods to the current profile object.
241 bool AddMethods(const std::vector<ProfileMethodInfo>& methods);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800242
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700243 // Add the given classes to the current profile object.
244 bool AddClasses(const std::set<DexCacheResolvedClasses>& resolved_classes);
245
246 // Add multiple type ids for classes in a single dex file. Iterator is for type_ids not
247 // class_defs.
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700248 template <class Iterator>
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700249 bool AddClassesForDex(const DexFile* dex_file, Iterator index_begin, Iterator index_end) {
250 DexFileData* data = GetOrAddDexFileData(dex_file);
251 if (data == nullptr) {
252 return false;
253 }
254 data->class_set.insert(index_begin, index_end);
255 return true;
256 }
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700257
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700258 // Add a method index to the profile (without inline caches). The method flags determine if it is
259 // hot, startup, or post startup, or a combination of the previous.
260 bool AddMethodIndex(MethodHotness::Flag flags,
261 const std::string& dex_location,
Mathieu Chartierea650f32017-05-24 12:04:13 -0700262 uint32_t checksum,
263 uint16_t method_idx,
264 uint32_t num_method_ids);
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700265 bool AddMethodIndex(MethodHotness::Flag flags, const MethodReference& ref);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700266
267 // Add a method to the profile using its online representation (containing runtime structures).
268 bool AddMethod(const ProfileMethodInfo& pmi);
269
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700270 // Bulk add sampled methods and/or hot methods for a single dex, fast since it only has one
271 // GetOrAddDexFileData call.
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700272 template <class Iterator>
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700273 bool AddMethodsForDex(MethodHotness::Flag flags,
274 const DexFile* dex_file,
275 Iterator index_begin,
276 Iterator index_end) {
277 DexFileData* data = GetOrAddDexFileData(dex_file);
278 if (data == nullptr) {
279 return false;
280 }
281 for (Iterator it = index_begin; it != index_end; ++it) {
282 DCHECK_LT(*it, data->num_method_ids);
283 data->AddMethod(flags, *it);
284 }
285 return true;
286 }
Mathieu Chartierea650f32017-05-24 12:04:13 -0700287
Mathieu Chartier2f794552017-06-19 10:58:08 -0700288 // Add hotness flags for a simple method.
289 bool AddMethodHotness(const MethodReference& method_ref, const MethodHotness& hotness);
290
Calin Juravle940eb0c2017-01-30 19:30:44 -0800291 // Load profile information from the given file descriptor.
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700292 // If the current profile is non-empty the load will fail.
Calin Juravle2e2db782016-02-23 12:00:03 +0000293 bool Load(int fd);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800294
Calin Juravledcab1902017-05-12 19:18:47 -0700295 // Load profile information from the given file
296 // If the current profile is non-empty the load will fail.
297 // If clear_if_invalid is true and the file is invalid the method clears the
298 // the file and returns true.
299 bool Load(const std::string& filename, bool clear_if_invalid);
300
Mathieu Chartier2f794552017-06-19 10:58:08 -0700301 // Merge the data from another ProfileCompilationInfo into the current object. Only merges
302 // classes if merge_classes is true. This is used for creating the boot profile since
303 // we don't want all of the classes to be image classes.
304 bool MergeWith(const ProfileCompilationInfo& info, bool merge_classes = true);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800305
306 // Save the profile data to the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +0000307 bool Save(int fd);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800308
Calin Juravledcab1902017-05-12 19:18:47 -0700309 // Save the current profile into the given file. The file will be cleared before saving.
310 bool Save(const std::string& filename, uint64_t* bytes_written);
Calin Juravle67265462016-03-18 16:23:40 +0000311
Calin Juravle940eb0c2017-01-30 19:30:44 -0800312 // Return the number of methods that were profiled.
Calin Juravle998c2162015-12-21 15:39:33 +0200313 uint32_t GetNumberOfMethods() const;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800314
315 // Return the number of resolved classes that were profiled.
Calin Juravle67265462016-03-18 16:23:40 +0000316 uint32_t GetNumberOfResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +0000317
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700318 // Returns the profile method info for a given method reference.
319 MethodHotness GetMethodHotness(const MethodReference& method_ref) const;
320 MethodHotness GetMethodHotness(const std::string& dex_location,
321 uint32_t dex_checksum,
322 uint16_t dex_method_index) const;
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700323
Calin Juravle940eb0c2017-01-30 19:30:44 -0800324 // Return true if the class's type is present in the profiling info.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800325 bool ContainsClass(const DexFile& dex_file, dex::TypeIndex type_idx) const;
Mathieu Chartiera8077802016-03-16 19:08:31 -0700326
Calin Juravlecc3171a2017-05-19 16:47:53 -0700327 // Return the method data for the given location and index from the profiling info.
328 // If the method index is not found or the checksum doesn't match, null is returned.
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700329 // Note: the inline cache map is a pointer to the map stored in the profile and
330 // its allocation will go away if the profile goes out of scope.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700331 std::unique_ptr<OfflineProfileMethodInfo> GetMethod(const std::string& dex_location,
332 uint32_t dex_checksum,
333 uint16_t dex_method_index) const;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800334
335 // Dump all the loaded profile info into a string and returns it.
Calin Juravle998c2162015-12-21 15:39:33 +0200336 // If dex_files is not null then the method indices will be resolved to their
337 // names.
Calin Juravle226501b2015-12-11 14:41:31 +0000338 // This is intended for testing and debugging.
David Sehrb18991b2017-02-08 20:58:10 -0800339 std::string DumpInfo(const std::vector<std::unique_ptr<const DexFile>>* dex_files,
340 bool print_full_dex_location = true) const;
Calin Juravle998c2162015-12-21 15:39:33 +0200341 std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
342 bool print_full_dex_location = true) const;
Calin Juravle226501b2015-12-11 14:41:31 +0000343
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700344 // Return the classes and methods for a given dex file through out args. The out args are the set
Mathieu Chartier34067262017-04-06 13:55:46 -0700345 // of class as well as the methods and their associated inline caches. Returns true if the dex
346 // file is register and has a matching checksum, false otherwise.
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700347 bool GetClassesAndMethods(const DexFile& dex_file,
348 /*out*/std::set<dex::TypeIndex>* class_set,
Mathieu Chartierea650f32017-05-24 12:04:13 -0700349 /*out*/std::set<uint16_t>* hot_method_set,
350 /*out*/std::set<uint16_t>* startup_method_set,
351 /*out*/std::set<uint16_t>* post_startup_method_method_set) const;
David Sehr7c80f2d2017-02-07 16:47:58 -0800352
Calin Juravle940eb0c2017-01-30 19:30:44 -0800353 // Perform an equality test with the `other` profile information.
Calin Juravle2e2db782016-02-23 12:00:03 +0000354 bool Equals(const ProfileCompilationInfo& other);
Calin Juravle67265462016-03-18 16:23:40 +0000355
Calin Juravle940eb0c2017-01-30 19:30:44 -0800356 // Return the class descriptors for all of the classes in the profiles' class sets.
Mathieu Chartier046854b2017-03-01 17:16:22 -0800357 std::set<DexCacheResolvedClasses> GetResolvedClasses(
Calin Juravle08556882017-05-26 16:40:45 -0700358 const std::vector<const DexFile*>& dex_files_) const;
Calin Juravle226501b2015-12-11 14:41:31 +0000359
Calin Juravle940eb0c2017-01-30 19:30:44 -0800360 // Return the profile key associated with the given dex location.
361 static std::string GetProfileDexFileKey(const std::string& dex_location);
362
363 // Generate a test profile which will contain a percentage of the total maximum
364 // number of methods and classes (method_ratio and class_ratio).
Calin Juravle7bcdb532016-06-07 16:14:47 +0100365 static bool GenerateTestProfile(int fd,
366 uint16_t number_of_dex_files,
367 uint16_t method_ratio,
Jeff Haof0a31f82017-03-27 15:50:37 -0700368 uint16_t class_ratio,
369 uint32_t random_seed);
370
371 // Generate a test profile which will randomly contain classes and methods from
372 // the provided list of dex files.
373 static bool GenerateTestProfile(int fd,
374 std::vector<std::unique_ptr<const DexFile>>& dex_files,
375 uint32_t random_seed);
Calin Juravle7bcdb532016-06-07 16:14:47 +0100376
Calin Juravle940eb0c2017-01-30 19:30:44 -0800377 // Check that the given profile method info contain the same data.
378 static bool Equals(const ProfileCompilationInfo::OfflineProfileMethodInfo& pmi1,
379 const ProfileCompilationInfo::OfflineProfileMethodInfo& pmi2);
380
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700381 ArenaAllocator* GetArena() { return &arena_; }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700382
Mathieu Chartier4f342b02017-07-21 17:12:39 -0700383 // Return all of the class descriptors in the profile for a set of dex files.
384 std::unordered_set<std::string> GetClassDescriptors(const std::vector<const DexFile*>& dex_files);
385
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800386 private:
Calin Juravle64142952016-03-21 14:37:55 +0000387 enum ProfileLoadSatus {
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700388 kProfileLoadWouldOverwiteData,
Calin Juravle64142952016-03-21 14:37:55 +0000389 kProfileLoadIOError,
390 kProfileLoadVersionMismatch,
391 kProfileLoadBadData,
392 kProfileLoadSuccess
393 };
394
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700395 const uint32_t kProfileSizeWarningThresholdInBytes = 500000U;
396 const uint32_t kProfileSizeErrorThresholdInBytes = 1000000U;
397
Calin Juravle940eb0c2017-01-30 19:30:44 -0800398 // Internal representation of the profile information belonging to a dex file.
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700399 // Note that we could do without profile_key (the key used to encode the dex
400 // file in the profile) and profile_index (the index of the dex file in the
401 // profile) fields in this struct because we can infer them from
402 // profile_key_map_ and info_. However, it makes the profiles logic much
403 // simpler if we have references here as well.
Calin Juravle798ba162017-05-23 23:01:53 -0700404 struct DexFileData : public DeletableArenaObject<kArenaAllocProfile> {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700405 DexFileData(ArenaAllocator* arena,
406 const std::string& key,
407 uint32_t location_checksum,
Mathieu Chartierea650f32017-05-24 12:04:13 -0700408 uint16_t index,
409 uint32_t num_methods)
Calin Juravlecc3171a2017-05-19 16:47:53 -0700410 : arena_(arena),
411 profile_key(key),
412 profile_index(index),
413 checksum(location_checksum),
414 method_map(std::less<uint16_t>(), arena->Adapter(kArenaAllocProfile)),
Mathieu Chartierea650f32017-05-24 12:04:13 -0700415 class_set(std::less<dex::TypeIndex>(), arena->Adapter(kArenaAllocProfile)),
416 num_method_ids(num_methods),
417 bitmap_storage(arena->Adapter(kArenaAllocProfile)) {
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700418 const size_t num_bits = num_method_ids * kBitmapIndexCount;
Mathieu Chartiercebf99c2017-06-05 11:06:52 -0700419 bitmap_storage.resize(RoundUp(num_bits, kBitsPerByte) / kBitsPerByte);
420 if (!bitmap_storage.empty()) {
421 method_bitmap =
422 BitMemoryRegion(MemoryRegion(&bitmap_storage[0], bitmap_storage.size()), 0, num_bits);
423 }
Mathieu Chartierea650f32017-05-24 12:04:13 -0700424 }
425
426 bool operator==(const DexFileData& other) const {
427 return checksum == other.checksum && method_map == other.method_map;
428 }
429
430 // Mark a method as executed at least once.
Mathieu Chartiere46f3a82017-06-19 19:54:12 -0700431 void AddMethod(MethodHotness::Flag flags, size_t index);
Calin Juravlecc3171a2017-05-19 16:47:53 -0700432
Mathieu Chartiercebf99c2017-06-05 11:06:52 -0700433 void MergeBitmap(const DexFileData& other) {
434 DCHECK_EQ(bitmap_storage.size(), other.bitmap_storage.size());
435 for (size_t i = 0; i < bitmap_storage.size(); ++i) {
436 bitmap_storage[i] |= other.bitmap_storage[i];
437 }
438 }
439
Mathieu Chartiere46f3a82017-06-19 19:54:12 -0700440 MethodHotness GetHotnessInfo(uint32_t dex_method_index) const;
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700441
Calin Juravlecc3171a2017-05-19 16:47:53 -0700442 // The arena used to allocate new inline cache maps.
443 ArenaAllocator* arena_;
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700444 // The profile key this data belongs to.
445 std::string profile_key;
446 // The profile index of this dex file (matches ClassReference#dex_profile_index).
Calin Juravle940eb0c2017-01-30 19:30:44 -0800447 uint8_t profile_index;
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700448 // The dex checksum.
Calin Juravle998c2162015-12-21 15:39:33 +0200449 uint32_t checksum;
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700450 // The methonds' profile information.
Calin Juravle940eb0c2017-01-30 19:30:44 -0800451 MethodMap method_map;
452 // The classes which have been profiled. Note that these don't necessarily include
453 // all the classes that can be found in the inline caches reference.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700454 ArenaSet<dex::TypeIndex> class_set;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700455 // Find the inline caches of the the given method index. Add an empty entry if
456 // no previous data is found.
457 InlineCacheMap* FindOrAddMethod(uint16_t method_index);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700458 // Num method ids.
459 uint32_t num_method_ids;
460 ArenaVector<uint8_t> bitmap_storage;
461 BitMemoryRegion method_bitmap;
462
Mathieu Chartierea650f32017-05-24 12:04:13 -0700463 private:
Mathieu Chartiercebf99c2017-06-05 11:06:52 -0700464 enum BitmapIndex {
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700465 kBitmapIndexStartup,
466 kBitmapIndexPostStartup,
467 kBitmapIndexCount,
Mathieu Chartierea650f32017-05-24 12:04:13 -0700468 };
469
470 size_t MethodBitIndex(bool startup, size_t index) const {
471 DCHECK_LT(index, num_method_ids);
Mathieu Chartiercebf99c2017-06-05 11:06:52 -0700472 // The format is [startup bitmap][post startup bitmap]
473 // This compresses better than ([startup bit][post statup bit])*
474
475 return index + (startup
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700476 ? kBitmapIndexStartup * num_method_ids
477 : kBitmapIndexPostStartup * num_method_ids);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700478 }
Calin Juravle998c2162015-12-21 15:39:33 +0200479 };
Calin Juravle226501b2015-12-11 14:41:31 +0000480
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700481 // Return the profile data for the given profile key or null if the dex location
Calin Juravle940eb0c2017-01-30 19:30:44 -0800482 // already exists but has a different checksum
Mathieu Chartierea650f32017-05-24 12:04:13 -0700483 DexFileData* GetOrAddDexFileData(const std::string& profile_key,
484 uint32_t checksum,
485 uint32_t num_method_ids);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800486
Mathieu Chartierea650f32017-05-24 12:04:13 -0700487 DexFileData* GetOrAddDexFileData(const DexFile* dex_file) {
488 return GetOrAddDexFileData(GetProfileDexFileKey(dex_file->GetLocation()),
489 dex_file->GetLocationChecksum(),
490 dex_file->NumMethodIds());
491 }
Calin Juravle940eb0c2017-01-30 19:30:44 -0800492
493 // Add a method to the profile using its offline representation.
494 // This is mostly used to facilitate testing.
495 bool AddMethod(const std::string& dex_location,
496 uint32_t dex_checksum,
497 uint16_t method_index,
Mathieu Chartierea650f32017-05-24 12:04:13 -0700498 uint32_t num_method_ids,
Calin Juravle940eb0c2017-01-30 19:30:44 -0800499 const OfflineProfileMethodInfo& pmi);
500
501 // Add a class index to the profile.
Mathieu Chartierea650f32017-05-24 12:04:13 -0700502 bool AddClassIndex(const std::string& dex_location,
503 uint32_t checksum,
504 dex::TypeIndex type_idx,
505 uint32_t num_method_ids);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800506
507 // Add all classes from the given dex cache to the the profile.
Calin Juravle99629622016-04-19 16:33:46 +0100508 bool AddResolvedClasses(const DexCacheResolvedClasses& classes);
Calin Juravle64142952016-03-21 14:37:55 +0000509
Calin Juravle940eb0c2017-01-30 19:30:44 -0800510 // Encode the known dex_files into a vector. The index of a dex_reference will
511 // be the same as the profile index of the dex file (used to encode the ClassReferences).
512 void DexFileToProfileIndex(/*out*/std::vector<DexReference>* dex_references) const;
513
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700514 // Return the dex data associated with the given profile key or null if the profile
515 // doesn't contain the key.
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700516 const DexFileData* FindDexData(const std::string& profile_key,
517 uint32_t checksum,
518 bool verify_checksum = true) const;
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700519
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700520 // Return the dex data associated with the given dex file or null if the profile doesn't contain
521 // the key or the checksum mismatches.
522 const DexFileData* FindDexData(const DexFile* dex_file) const;
523
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700524 // Checks if the profile is empty.
525 bool IsEmpty() const;
526
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700527 // Inflate the input buffer (in_buffer) of size in_size. It returns a buffer of
528 // compressed data for the input buffer of "compressed_data_size" size.
529 std::unique_ptr<uint8_t[]> DeflateBuffer(const uint8_t* in_buffer,
530 uint32_t in_size,
531 /*out*/uint32_t* compressed_data_size);
532
533 // Inflate the input buffer(in_buffer) of size in_size. out_size is the expected output
534 // size of the buffer. It puts the output in out_buffer. It returns Z_STREAM_END on
535 // success. On error, it returns Z_STREAM_ERROR if the compressed data is inconsistent
536 // and Z_DATA_ERROR if the stream ended prematurely or the stream has extra data.
537 int InflateBuffer(const uint8_t* in_buffer,
538 uint32_t in_size,
539 uint32_t out_size,
540 /*out*/uint8_t* out_buffer);
541
Calin Juravle64142952016-03-21 14:37:55 +0000542 // Parsing functionality.
543
Calin Juravle940eb0c2017-01-30 19:30:44 -0800544 // The information present in the header of each profile line.
Calin Juravle64142952016-03-21 14:37:55 +0000545 struct ProfileLineHeader {
546 std::string dex_location;
Calin Juravle64142952016-03-21 14:37:55 +0000547 uint16_t class_set_size;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800548 uint32_t method_region_size_bytes;
Calin Juravle64142952016-03-21 14:37:55 +0000549 uint32_t checksum;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700550 uint32_t num_method_ids;
Calin Juravle64142952016-03-21 14:37:55 +0000551 };
552
553 // A helper structure to make sure we don't read past our buffers in the loops.
554 struct SafeBuffer {
555 public:
556 explicit SafeBuffer(size_t size) : storage_(new uint8_t[size]) {
557 ptr_current_ = storage_.get();
558 ptr_end_ = ptr_current_ + size;
559 }
560
561 // Reads the content of the descriptor at the current position.
562 ProfileLoadSatus FillFromFd(int fd,
563 const std::string& source,
564 /*out*/std::string* error);
565
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700566 ProfileLoadSatus FillFromBuffer(uint8_t* buffer_ptr,
567 const std::string& source,
568 /*out*/std::string* error);
569
Calin Juravle64142952016-03-21 14:37:55 +0000570 // Reads an uint value (high bits to low bits) and advances the current pointer
571 // with the number of bits read.
Calin Juravle940eb0c2017-01-30 19:30:44 -0800572 template <typename T> bool ReadUintAndAdvance(/*out*/ T* value);
Calin Juravle64142952016-03-21 14:37:55 +0000573
574 // Compares the given data with the content current pointer. If the contents are
575 // equal it advances the current pointer by data_size.
576 bool CompareAndAdvance(const uint8_t* data, size_t data_size);
577
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700578 // Advances current pointer by data_size.
579 void Advance(size_t data_size);
580
581 // Returns the count of unread bytes.
582 size_t CountUnreadBytes();
583
584 // Returns the current pointer.
585 const uint8_t* GetCurrentPtr();
Calin Juravle940eb0c2017-01-30 19:30:44 -0800586
Calin Juravle64142952016-03-21 14:37:55 +0000587 // Get the underlying raw buffer.
588 uint8_t* Get() { return storage_.get(); }
589
590 private:
Andreas Gampe7b8a2652016-11-11 17:11:25 -0800591 std::unique_ptr<uint8_t[]> storage_;
Calin Juravle64142952016-03-21 14:37:55 +0000592 uint8_t* ptr_end_;
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700593 uint8_t* ptr_current_;
Calin Juravle64142952016-03-21 14:37:55 +0000594 };
595
Calin Juravle940eb0c2017-01-30 19:30:44 -0800596 // Entry point for profile loding functionality.
Calin Juravle64142952016-03-21 14:37:55 +0000597 ProfileLoadSatus LoadInternal(int fd, std::string* error);
598
Calin Juravle940eb0c2017-01-30 19:30:44 -0800599 // Read the profile header from the given fd and store the number of profile
600 // lines into number_of_dex_files.
Calin Juravle64142952016-03-21 14:37:55 +0000601 ProfileLoadSatus ReadProfileHeader(int fd,
Calin Juravle940eb0c2017-01-30 19:30:44 -0800602 /*out*/uint8_t* number_of_dex_files,
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700603 /*out*/uint32_t* size_uncompressed_data,
604 /*out*/uint32_t* size_compressed_data,
Calin Juravle64142952016-03-21 14:37:55 +0000605 /*out*/std::string* error);
606
Calin Juravle940eb0c2017-01-30 19:30:44 -0800607 // Read the header of a profile line from the given fd.
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700608 ProfileLoadSatus ReadProfileLineHeader(SafeBuffer& buffer,
Calin Juravle64142952016-03-21 14:37:55 +0000609 /*out*/ProfileLineHeader* line_header,
610 /*out*/std::string* error);
Calin Juravle940eb0c2017-01-30 19:30:44 -0800611
612 // Read individual elements from the profile line header.
613 bool ReadProfileLineHeaderElements(SafeBuffer& buffer,
614 /*out*/uint16_t* dex_location_size,
615 /*out*/ProfileLineHeader* line_header,
616 /*out*/std::string* error);
617
618 // Read a single profile line from the given fd.
Shubham Ajmera4f0a15a2017-04-26 19:26:46 -0700619 ProfileLoadSatus ReadProfileLine(SafeBuffer& buffer,
Calin Juravle940eb0c2017-01-30 19:30:44 -0800620 uint8_t number_of_dex_files,
Calin Juravle64142952016-03-21 14:37:55 +0000621 const ProfileLineHeader& line_header,
622 /*out*/std::string* error);
623
Calin Juravle940eb0c2017-01-30 19:30:44 -0800624 // Read all the classes from the buffer into the profile `info_` structure.
625 bool ReadClasses(SafeBuffer& buffer,
Calin Juravle940eb0c2017-01-30 19:30:44 -0800626 const ProfileLineHeader& line_header,
627 /*out*/std::string* error);
628
629 // Read all the methods from the buffer into the profile `info_` structure.
630 bool ReadMethods(SafeBuffer& buffer,
631 uint8_t number_of_dex_files,
632 const ProfileLineHeader& line_header,
633 /*out*/std::string* error);
634
635 // Read the inline cache encoding from line_bufer into inline_cache.
636 bool ReadInlineCache(SafeBuffer& buffer,
637 uint8_t number_of_dex_files,
638 /*out*/InlineCacheMap* inline_cache,
639 /*out*/std::string* error);
640
641 // Encode the inline cache into the given buffer.
642 void AddInlineCacheToBuffer(std::vector<uint8_t>* buffer,
643 const InlineCacheMap& inline_cache);
644
645 // Return the number of bytes needed to encode the profile information
646 // for the methods in dex_data.
647 uint32_t GetMethodsRegionSize(const DexFileData& dex_data);
648
649 // Group `classes` by their owning dex profile index and put the result in
650 // `dex_to_classes_map`.
651 void GroupClassesByDex(
652 const ClassSet& classes,
653 /*out*/SafeMap<uint8_t, std::vector<dex::TypeIndex>>* dex_to_classes_map);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800654
Calin Juravlecc3171a2017-05-19 16:47:53 -0700655 // Find the data for the dex_pc in the inline cache. Adds an empty entry
656 // if no previous data exists.
657 DexPcData* FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc);
658
Calin Juravle877fd962016-01-05 14:29:29 +0000659 friend class ProfileCompilationInfoTest;
660 friend class CompilerDriverProfileTest;
661 friend class ProfileAssistantTest;
Jeff Hao41fba6a2016-11-28 11:53:33 -0800662 friend class Dex2oatLayoutTest;
Calin Juravle877fd962016-01-05 14:29:29 +0000663
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700664 ArenaPool default_arena_pool_;
665 ArenaAllocator arena_;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700666
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700667 // Vector containing the actual profile info.
668 // The vector index is the profile index of the dex data and
669 // matched DexFileData::profile_index.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700670 ArenaVector<DexFileData*> info_;
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700671
672 // Cache mapping profile keys to profile index.
673 // This is used to speed up searches since it avoids iterating
674 // over the info_ vector when searching by profile key.
Calin Juravlecc3171a2017-05-19 16:47:53 -0700675 ArenaSafeMap<const std::string, uint8_t> profile_key_map_;
Calin Juravle226501b2015-12-11 14:41:31 +0000676};
677
Calin Juravle31f2c152015-10-23 17:56:15 +0100678} // namespace art
679
Calin Juravle33083d62017-01-18 15:29:12 -0800680#endif // ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_