Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_ |
| 18 | #define ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_ |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 19 | |
| 20 | #include <set> |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 21 | #include <vector> |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 22 | |
| 23 | #include "atomic.h" |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 24 | #include "base/arena_containers.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "base/arena_object.h" |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 26 | #include "bit_memory_region.h" |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 27 | #include "dex_cache_resolved_classes.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 28 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 29 | #include "dex_file_types.h" |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 30 | #include "method_reference.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 31 | #include "safe_map.h" |
Mathieu Chartier | dbddc22 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 32 | #include "type_reference.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 36 | /** |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 37 | * Convenient class to pass around profile information (including inline caches) |
| 38 | * without the need to hold GC-able objects. |
| 39 | */ |
| 40 | struct ProfileMethodInfo { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 41 | struct ProfileInlineCache { |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 42 | ProfileInlineCache(uint32_t pc, |
| 43 | bool missing_types, |
Mathieu Chartier | dbddc22 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 44 | const std::vector<TypeReference>& profile_classes) |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 45 | : dex_pc(pc), is_missing_types(missing_types), classes(profile_classes) {} |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 46 | |
| 47 | const uint32_t dex_pc; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 48 | const bool is_missing_types; |
Mathieu Chartier | dbddc22 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 49 | const std::vector<TypeReference> classes; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 52 | explicit ProfileMethodInfo(MethodReference reference) : ref(reference) {} |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 53 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 54 | ProfileMethodInfo(MethodReference reference, const std::vector<ProfileInlineCache>& caches) |
| 55 | : ref(reference), |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 56 | inline_caches(caches) {} |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 57 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 58 | MethodReference ref; |
| 59 | std::vector<ProfileInlineCache> inline_caches; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /** |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 63 | * 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 Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 67 | * Currently it stores only the hot compiled methods. |
| 68 | */ |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 69 | class ProfileCompilationInfo { |
| 70 | public: |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 71 | static const uint8_t kProfileMagic[]; |
| 72 | static const uint8_t kProfileVersion[]; |
| 73 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 74 | // 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 Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 80 | DexReference() : dex_checksum(0), num_method_ids(0) {} |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 81 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 82 | 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 Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 84 | |
| 85 | bool operator==(const DexReference& other) const { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 86 | return dex_checksum == other.dex_checksum && |
| 87 | dex_location == other.dex_location && |
| 88 | num_method_ids == other.num_method_ids; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 91 | bool MatchesDex(const DexFile* dex_file) const { |
| 92 | return dex_checksum == dex_file->GetLocationChecksum() && |
| 93 | dex_location == GetProfileDexFileKey(dex_file->GetLocation()); |
| 94 | } |
| 95 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 96 | std::string dex_location; |
| 97 | uint32_t dex_checksum; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 98 | uint32_t num_method_ids; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 99 | }; |
| 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 Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 112 | struct ClassReference : public ValueObject { |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 113 | ClassReference(uint8_t dex_profile_idx, const dex::TypeIndex type_idx) : |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 114 | 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 Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 130 | using ClassSet = ArenaSet<ClassReference>; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 131 | |
| 132 | // Encodes the actual inline cache for a given dex pc (whether or not the receiver is |
| 133 | // megamorphic and its possible types). |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 134 | // If the receiver is megamorphic or is missing types the set of classes will be empty. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 135 | 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 Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 140 | void AddClass(uint16_t dex_profile_idx, const dex::TypeIndex& type_idx); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 141 | void SetIsMegamorphic() { |
| 142 | if (is_missing_types) return; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 143 | is_megamorphic = true; |
| 144 | classes.clear(); |
| 145 | } |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 146 | void SetIsMissingTypes() { |
| 147 | is_megamorphic = false; |
| 148 | is_missing_types = true; |
| 149 | classes.clear(); |
| 150 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 151 | bool operator==(const DexPcData& other) const { |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 152 | return is_megamorphic == other.is_megamorphic && |
| 153 | is_missing_types == other.is_missing_types && |
| 154 | classes == other.classes; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 157 | // 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 Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 161 | bool is_megamorphic; |
| 162 | ClassSet classes; |
| 163 | }; |
| 164 | |
| 165 | // The inline cache map: DexPc -> DexPcData. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 166 | using InlineCacheMap = ArenaSafeMap<uint16_t, DexPcData>; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 167 | |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 168 | // Maps a method dex index to its inline cache. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 169 | using MethodMap = ArenaSafeMap<uint16_t, InlineCacheMap>; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 170 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 171 | // 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 Chartier | 7c1be8b | 2017-06-15 13:56:05 -0700 | [diff] [blame] | 197 | uint8_t GetFlags() const { |
| 198 | return flags_; |
| 199 | } |
| 200 | |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 201 | bool IsInProfile() const { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 202 | return flags_ != 0; |
| 203 | } |
| 204 | |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 205 | private: |
| 206 | const InlineCacheMap* inline_cache_map_ = nullptr; |
| 207 | uint8_t flags_ = 0; |
| 208 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 209 | 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 Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 217 | friend class ProfileCompilationInfo; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 220 | // 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 Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 225 | explicit OfflineProfileMethodInfo(const InlineCacheMap* inline_cache_map) |
| 226 | : inline_caches(inline_cache_map) {} |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 227 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 228 | bool operator==(const OfflineProfileMethodInfo& other) const; |
| 229 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 230 | const InlineCacheMap* const inline_caches; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 231 | std::vector<DexReference> dex_references; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | // Public methods to create, extend or query the profile. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 235 | ProfileCompilationInfo(); |
| 236 | explicit ProfileCompilationInfo(ArenaPool* arena_pool); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 237 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 238 | ~ProfileCompilationInfo(); |
| 239 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 240 | // Add the given methods to the current profile object. |
| 241 | bool AddMethods(const std::vector<ProfileMethodInfo>& methods); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 242 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 243 | // 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 Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 248 | template <class Iterator> |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 249 | 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 Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 257 | // Add a single type id for a dex file. |
| 258 | bool AddClassForDex(const TypeReference& ref) { |
| 259 | DexFileData* data = GetOrAddDexFileData(ref.dex_file); |
| 260 | if (data == nullptr) { |
| 261 | return false; |
| 262 | } |
| 263 | data->class_set.insert(ref.TypeIndex()); |
| 264 | return true; |
| 265 | } |
| 266 | |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 267 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 268 | // Add a method index to the profile (without inline caches). The method flags determine if it is |
| 269 | // hot, startup, or post startup, or a combination of the previous. |
| 270 | bool AddMethodIndex(MethodHotness::Flag flags, |
| 271 | const std::string& dex_location, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 272 | uint32_t checksum, |
| 273 | uint16_t method_idx, |
| 274 | uint32_t num_method_ids); |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 275 | bool AddMethodIndex(MethodHotness::Flag flags, const MethodReference& ref); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 276 | |
| 277 | // Add a method to the profile using its online representation (containing runtime structures). |
| 278 | bool AddMethod(const ProfileMethodInfo& pmi); |
| 279 | |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 280 | // Bulk add sampled methods and/or hot methods for a single dex, fast since it only has one |
| 281 | // GetOrAddDexFileData call. |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 282 | template <class Iterator> |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 283 | bool AddMethodsForDex(MethodHotness::Flag flags, |
| 284 | const DexFile* dex_file, |
| 285 | Iterator index_begin, |
| 286 | Iterator index_end) { |
| 287 | DexFileData* data = GetOrAddDexFileData(dex_file); |
| 288 | if (data == nullptr) { |
| 289 | return false; |
| 290 | } |
| 291 | for (Iterator it = index_begin; it != index_end; ++it) { |
| 292 | DCHECK_LT(*it, data->num_method_ids); |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 293 | if (!data->AddMethod(flags, *it)) { |
| 294 | return false; |
| 295 | } |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 296 | } |
| 297 | return true; |
| 298 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 299 | |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 300 | // Add hotness flags for a simple method. |
| 301 | bool AddMethodHotness(const MethodReference& method_ref, const MethodHotness& hotness); |
| 302 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 303 | // Load or Merge profile information from the given file descriptor. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 304 | // If the current profile is non-empty the load will fail. |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 305 | // If merge_classes is set to false, classes will not be merged/loaded. |
| 306 | bool Load(int fd, bool merge_classes = true); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 307 | |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 308 | // Verify integrity of the profile file with the provided dex files. |
| 309 | // If there exists a DexData object which maps to a dex_file, then it verifies that: |
| 310 | // - The checksums of the DexData and dex_file are equals. |
| 311 | // - No method id exceeds NumMethodIds corresponding to the dex_file. |
| 312 | // - No class id exceeds NumTypeIds corresponding to the dex_file. |
| 313 | // - For every inline_caches, class_ids does not exceed NumTypeIds corresponding to |
| 314 | // the dex_file they are in. |
| 315 | bool VerifyProfileData(const std::vector<const DexFile *> &dex_files); |
| 316 | |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 317 | // Load profile information from the given file |
| 318 | // If the current profile is non-empty the load will fail. |
| 319 | // If clear_if_invalid is true and the file is invalid the method clears the |
| 320 | // the file and returns true. |
| 321 | bool Load(const std::string& filename, bool clear_if_invalid); |
| 322 | |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 323 | // Merge the data from another ProfileCompilationInfo into the current object. Only merges |
| 324 | // classes if merge_classes is true. This is used for creating the boot profile since |
| 325 | // we don't want all of the classes to be image classes. |
| 326 | bool MergeWith(const ProfileCompilationInfo& info, bool merge_classes = true); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 327 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 328 | // Merge profile information from the given file descriptor. |
| 329 | bool MergeWith(const std::string& filename); |
| 330 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 331 | // Save the profile data to the given file descriptor. |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 332 | bool Save(int fd); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 333 | |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 334 | // Save the current profile into the given file. The file will be cleared before saving. |
| 335 | bool Save(const std::string& filename, uint64_t* bytes_written); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 336 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 337 | // Return the number of methods that were profiled. |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 338 | uint32_t GetNumberOfMethods() const; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 339 | |
| 340 | // Return the number of resolved classes that were profiled. |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 341 | uint32_t GetNumberOfResolvedClasses() const; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 342 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 343 | // Returns the profile method info for a given method reference. |
| 344 | MethodHotness GetMethodHotness(const MethodReference& method_ref) const; |
| 345 | MethodHotness GetMethodHotness(const std::string& dex_location, |
| 346 | uint32_t dex_checksum, |
| 347 | uint16_t dex_method_index) const; |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 348 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 349 | // Return true if the class's type is present in the profiling info. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 350 | bool ContainsClass(const DexFile& dex_file, dex::TypeIndex type_idx) const; |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 351 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 352 | // Return the method data for the given location and index from the profiling info. |
| 353 | // If the method index is not found or the checksum doesn't match, null is returned. |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 354 | // Note: the inline cache map is a pointer to the map stored in the profile and |
| 355 | // its allocation will go away if the profile goes out of scope. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 356 | std::unique_ptr<OfflineProfileMethodInfo> GetMethod(const std::string& dex_location, |
| 357 | uint32_t dex_checksum, |
| 358 | uint16_t dex_method_index) const; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 359 | |
| 360 | // Dump all the loaded profile info into a string and returns it. |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 361 | // If dex_files is not null then the method indices will be resolved to their |
| 362 | // names. |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 363 | // This is intended for testing and debugging. |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 364 | std::string DumpInfo(const std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 365 | bool print_full_dex_location = true) const; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 366 | std::string DumpInfo(const std::vector<const DexFile*>* dex_files, |
| 367 | bool print_full_dex_location = true) const; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 368 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 369 | // Return the classes and methods for a given dex file through out args. The out args are the set |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 370 | // of class as well as the methods and their associated inline caches. Returns true if the dex |
| 371 | // file is register and has a matching checksum, false otherwise. |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 372 | bool GetClassesAndMethods(const DexFile& dex_file, |
| 373 | /*out*/std::set<dex::TypeIndex>* class_set, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 374 | /*out*/std::set<uint16_t>* hot_method_set, |
| 375 | /*out*/std::set<uint16_t>* startup_method_set, |
| 376 | /*out*/std::set<uint16_t>* post_startup_method_method_set) const; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 377 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 378 | // Perform an equality test with the `other` profile information. |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 379 | bool Equals(const ProfileCompilationInfo& other); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 380 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 381 | // Return the class descriptors for all of the classes in the profiles' class sets. |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 382 | std::set<DexCacheResolvedClasses> GetResolvedClasses( |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 383 | const std::vector<const DexFile*>& dex_files_) const; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 384 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 385 | // Return the profile key associated with the given dex location. |
| 386 | static std::string GetProfileDexFileKey(const std::string& dex_location); |
| 387 | |
| 388 | // Generate a test profile which will contain a percentage of the total maximum |
| 389 | // number of methods and classes (method_ratio and class_ratio). |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 390 | static bool GenerateTestProfile(int fd, |
| 391 | uint16_t number_of_dex_files, |
| 392 | uint16_t method_ratio, |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 393 | uint16_t class_ratio, |
| 394 | uint32_t random_seed); |
| 395 | |
| 396 | // Generate a test profile which will randomly contain classes and methods from |
| 397 | // the provided list of dex files. |
| 398 | static bool GenerateTestProfile(int fd, |
| 399 | std::vector<std::unique_ptr<const DexFile>>& dex_files, |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 400 | uint16_t method_percentage, |
| 401 | uint16_t class_percentage, |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 402 | uint32_t random_seed); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 403 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 404 | // Check that the given profile method info contain the same data. |
| 405 | static bool Equals(const ProfileCompilationInfo::OfflineProfileMethodInfo& pmi1, |
| 406 | const ProfileCompilationInfo::OfflineProfileMethodInfo& pmi2); |
| 407 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame^] | 408 | ArenaAllocator* GetAllocator() { return &allocator_; } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 409 | |
Mathieu Chartier | 4f342b0 | 2017-07-21 17:12:39 -0700 | [diff] [blame] | 410 | // Return all of the class descriptors in the profile for a set of dex files. |
| 411 | std::unordered_set<std::string> GetClassDescriptors(const std::vector<const DexFile*>& dex_files); |
| 412 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 413 | private: |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 414 | enum ProfileLoadSatus { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 415 | kProfileLoadWouldOverwiteData, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 416 | kProfileLoadIOError, |
| 417 | kProfileLoadVersionMismatch, |
| 418 | kProfileLoadBadData, |
| 419 | kProfileLoadSuccess |
| 420 | }; |
| 421 | |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 422 | const uint32_t kProfileSizeWarningThresholdInBytes = 500000U; |
| 423 | const uint32_t kProfileSizeErrorThresholdInBytes = 1000000U; |
| 424 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 425 | // Internal representation of the profile information belonging to a dex file. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 426 | // Note that we could do without profile_key (the key used to encode the dex |
| 427 | // file in the profile) and profile_index (the index of the dex file in the |
| 428 | // profile) fields in this struct because we can infer them from |
| 429 | // profile_key_map_ and info_. However, it makes the profiles logic much |
| 430 | // simpler if we have references here as well. |
Calin Juravle | 798ba16 | 2017-05-23 23:01:53 -0700 | [diff] [blame] | 431 | struct DexFileData : public DeletableArenaObject<kArenaAllocProfile> { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 432 | DexFileData(ArenaAllocator* arena, |
| 433 | const std::string& key, |
| 434 | uint32_t location_checksum, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 435 | uint16_t index, |
| 436 | uint32_t num_methods) |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 437 | : arena_(arena), |
| 438 | profile_key(key), |
| 439 | profile_index(index), |
| 440 | checksum(location_checksum), |
| 441 | method_map(std::less<uint16_t>(), arena->Adapter(kArenaAllocProfile)), |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 442 | class_set(std::less<dex::TypeIndex>(), arena->Adapter(kArenaAllocProfile)), |
| 443 | num_method_ids(num_methods), |
| 444 | bitmap_storage(arena->Adapter(kArenaAllocProfile)) { |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 445 | const size_t num_bits = num_method_ids * kBitmapIndexCount; |
Mathieu Chartier | cebf99c | 2017-06-05 11:06:52 -0700 | [diff] [blame] | 446 | bitmap_storage.resize(RoundUp(num_bits, kBitsPerByte) / kBitsPerByte); |
| 447 | if (!bitmap_storage.empty()) { |
| 448 | method_bitmap = |
| 449 | BitMemoryRegion(MemoryRegion(&bitmap_storage[0], bitmap_storage.size()), 0, num_bits); |
| 450 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | bool operator==(const DexFileData& other) const { |
| 454 | return checksum == other.checksum && method_map == other.method_map; |
| 455 | } |
| 456 | |
| 457 | // Mark a method as executed at least once. |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 458 | bool AddMethod(MethodHotness::Flag flags, size_t index); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 459 | |
Mathieu Chartier | cebf99c | 2017-06-05 11:06:52 -0700 | [diff] [blame] | 460 | void MergeBitmap(const DexFileData& other) { |
| 461 | DCHECK_EQ(bitmap_storage.size(), other.bitmap_storage.size()); |
| 462 | for (size_t i = 0; i < bitmap_storage.size(); ++i) { |
| 463 | bitmap_storage[i] |= other.bitmap_storage[i]; |
| 464 | } |
| 465 | } |
| 466 | |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 467 | MethodHotness GetHotnessInfo(uint32_t dex_method_index) const; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 468 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 469 | // The arena used to allocate new inline cache maps. |
| 470 | ArenaAllocator* arena_; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 471 | // The profile key this data belongs to. |
| 472 | std::string profile_key; |
| 473 | // The profile index of this dex file (matches ClassReference#dex_profile_index). |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 474 | uint8_t profile_index; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 475 | // The dex checksum. |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 476 | uint32_t checksum; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 477 | // The methonds' profile information. |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 478 | MethodMap method_map; |
| 479 | // The classes which have been profiled. Note that these don't necessarily include |
| 480 | // all the classes that can be found in the inline caches reference. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 481 | ArenaSet<dex::TypeIndex> class_set; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 482 | // Find the inline caches of the the given method index. Add an empty entry if |
| 483 | // no previous data is found. |
| 484 | InlineCacheMap* FindOrAddMethod(uint16_t method_index); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 485 | // Num method ids. |
| 486 | uint32_t num_method_ids; |
| 487 | ArenaVector<uint8_t> bitmap_storage; |
| 488 | BitMemoryRegion method_bitmap; |
| 489 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 490 | private: |
Mathieu Chartier | cebf99c | 2017-06-05 11:06:52 -0700 | [diff] [blame] | 491 | enum BitmapIndex { |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 492 | kBitmapIndexStartup, |
| 493 | kBitmapIndexPostStartup, |
| 494 | kBitmapIndexCount, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | size_t MethodBitIndex(bool startup, size_t index) const { |
| 498 | DCHECK_LT(index, num_method_ids); |
Mathieu Chartier | cebf99c | 2017-06-05 11:06:52 -0700 | [diff] [blame] | 499 | // The format is [startup bitmap][post startup bitmap] |
| 500 | // This compresses better than ([startup bit][post statup bit])* |
| 501 | |
| 502 | return index + (startup |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 503 | ? kBitmapIndexStartup * num_method_ids |
| 504 | : kBitmapIndexPostStartup * num_method_ids); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 505 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 506 | }; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 507 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 508 | // Return the profile data for the given profile key or null if the dex location |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 509 | // already exists but has a different checksum |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 510 | DexFileData* GetOrAddDexFileData(const std::string& profile_key, |
| 511 | uint32_t checksum, |
| 512 | uint32_t num_method_ids); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 513 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 514 | DexFileData* GetOrAddDexFileData(const DexFile* dex_file) { |
| 515 | return GetOrAddDexFileData(GetProfileDexFileKey(dex_file->GetLocation()), |
| 516 | dex_file->GetLocationChecksum(), |
| 517 | dex_file->NumMethodIds()); |
| 518 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 519 | |
| 520 | // Add a method to the profile using its offline representation. |
| 521 | // This is mostly used to facilitate testing. |
| 522 | bool AddMethod(const std::string& dex_location, |
| 523 | uint32_t dex_checksum, |
| 524 | uint16_t method_index, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 525 | uint32_t num_method_ids, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 526 | const OfflineProfileMethodInfo& pmi); |
| 527 | |
| 528 | // Add a class index to the profile. |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 529 | bool AddClassIndex(const std::string& dex_location, |
| 530 | uint32_t checksum, |
| 531 | dex::TypeIndex type_idx, |
| 532 | uint32_t num_method_ids); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 533 | |
| 534 | // Add all classes from the given dex cache to the the profile. |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 535 | bool AddResolvedClasses(const DexCacheResolvedClasses& classes); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 536 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 537 | // Encode the known dex_files into a vector. The index of a dex_reference will |
| 538 | // be the same as the profile index of the dex file (used to encode the ClassReferences). |
| 539 | void DexFileToProfileIndex(/*out*/std::vector<DexReference>* dex_references) const; |
| 540 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 541 | // Return the dex data associated with the given profile key or null if the profile |
| 542 | // doesn't contain the key. |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 543 | const DexFileData* FindDexData(const std::string& profile_key, |
| 544 | uint32_t checksum, |
| 545 | bool verify_checksum = true) const; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 546 | |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 547 | // Return the dex data associated with the given dex file or null if the profile doesn't contain |
| 548 | // the key or the checksum mismatches. |
| 549 | const DexFileData* FindDexData(const DexFile* dex_file) const; |
| 550 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 551 | // Checks if the profile is empty. |
| 552 | bool IsEmpty() const; |
| 553 | |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 554 | // Inflate the input buffer (in_buffer) of size in_size. It returns a buffer of |
| 555 | // compressed data for the input buffer of "compressed_data_size" size. |
| 556 | std::unique_ptr<uint8_t[]> DeflateBuffer(const uint8_t* in_buffer, |
| 557 | uint32_t in_size, |
| 558 | /*out*/uint32_t* compressed_data_size); |
| 559 | |
| 560 | // Inflate the input buffer(in_buffer) of size in_size. out_size is the expected output |
| 561 | // size of the buffer. It puts the output in out_buffer. It returns Z_STREAM_END on |
| 562 | // success. On error, it returns Z_STREAM_ERROR if the compressed data is inconsistent |
| 563 | // and Z_DATA_ERROR if the stream ended prematurely or the stream has extra data. |
| 564 | int InflateBuffer(const uint8_t* in_buffer, |
| 565 | uint32_t in_size, |
| 566 | uint32_t out_size, |
| 567 | /*out*/uint8_t* out_buffer); |
| 568 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 569 | // Parsing functionality. |
| 570 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 571 | // The information present in the header of each profile line. |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 572 | struct ProfileLineHeader { |
| 573 | std::string dex_location; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 574 | uint16_t class_set_size; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 575 | uint32_t method_region_size_bytes; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 576 | uint32_t checksum; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 577 | uint32_t num_method_ids; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 578 | }; |
| 579 | |
| 580 | // A helper structure to make sure we don't read past our buffers in the loops. |
| 581 | struct SafeBuffer { |
| 582 | public: |
| 583 | explicit SafeBuffer(size_t size) : storage_(new uint8_t[size]) { |
| 584 | ptr_current_ = storage_.get(); |
| 585 | ptr_end_ = ptr_current_ + size; |
| 586 | } |
| 587 | |
| 588 | // Reads the content of the descriptor at the current position. |
| 589 | ProfileLoadSatus FillFromFd(int fd, |
| 590 | const std::string& source, |
| 591 | /*out*/std::string* error); |
| 592 | |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 593 | ProfileLoadSatus FillFromBuffer(uint8_t* buffer_ptr, |
| 594 | const std::string& source, |
| 595 | /*out*/std::string* error); |
| 596 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 597 | // Reads an uint value (high bits to low bits) and advances the current pointer |
| 598 | // with the number of bits read. |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 599 | template <typename T> bool ReadUintAndAdvance(/*out*/ T* value); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 600 | |
| 601 | // Compares the given data with the content current pointer. If the contents are |
| 602 | // equal it advances the current pointer by data_size. |
| 603 | bool CompareAndAdvance(const uint8_t* data, size_t data_size); |
| 604 | |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 605 | // Advances current pointer by data_size. |
| 606 | void Advance(size_t data_size); |
| 607 | |
| 608 | // Returns the count of unread bytes. |
| 609 | size_t CountUnreadBytes(); |
| 610 | |
| 611 | // Returns the current pointer. |
| 612 | const uint8_t* GetCurrentPtr(); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 613 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 614 | // Get the underlying raw buffer. |
| 615 | uint8_t* Get() { return storage_.get(); } |
| 616 | |
| 617 | private: |
Andreas Gampe | 7b8a265 | 2016-11-11 17:11:25 -0800 | [diff] [blame] | 618 | std::unique_ptr<uint8_t[]> storage_; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 619 | uint8_t* ptr_end_; |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 620 | uint8_t* ptr_current_; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 621 | }; |
| 622 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 623 | // Entry point for profile loding functionality. |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 624 | ProfileLoadSatus LoadInternal(int fd, std::string* error, bool merge_classes = true); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 625 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 626 | // Read the profile header from the given fd and store the number of profile |
| 627 | // lines into number_of_dex_files. |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 628 | ProfileLoadSatus ReadProfileHeader(int fd, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 629 | /*out*/uint8_t* number_of_dex_files, |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 630 | /*out*/uint32_t* size_uncompressed_data, |
| 631 | /*out*/uint32_t* size_compressed_data, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 632 | /*out*/std::string* error); |
| 633 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 634 | // Read the header of a profile line from the given fd. |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 635 | ProfileLoadSatus ReadProfileLineHeader(SafeBuffer& buffer, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 636 | /*out*/ProfileLineHeader* line_header, |
| 637 | /*out*/std::string* error); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 638 | |
| 639 | // Read individual elements from the profile line header. |
| 640 | bool ReadProfileLineHeaderElements(SafeBuffer& buffer, |
| 641 | /*out*/uint16_t* dex_location_size, |
| 642 | /*out*/ProfileLineHeader* line_header, |
| 643 | /*out*/std::string* error); |
| 644 | |
| 645 | // Read a single profile line from the given fd. |
Shubham Ajmera | 4f0a15a | 2017-04-26 19:26:46 -0700 | [diff] [blame] | 646 | ProfileLoadSatus ReadProfileLine(SafeBuffer& buffer, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 647 | uint8_t number_of_dex_files, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 648 | const ProfileLineHeader& line_header, |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 649 | const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap, |
| 650 | bool merge_classes, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 651 | /*out*/std::string* error); |
| 652 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 653 | // Read all the classes from the buffer into the profile `info_` structure. |
| 654 | bool ReadClasses(SafeBuffer& buffer, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 655 | const ProfileLineHeader& line_header, |
| 656 | /*out*/std::string* error); |
| 657 | |
| 658 | // Read all the methods from the buffer into the profile `info_` structure. |
| 659 | bool ReadMethods(SafeBuffer& buffer, |
| 660 | uint8_t number_of_dex_files, |
| 661 | const ProfileLineHeader& line_header, |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 662 | const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 663 | /*out*/std::string* error); |
| 664 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 665 | // The method generates mapping of profile indices while merging a new profile |
| 666 | // data into current data. It returns true, if the mapping was successful. |
| 667 | bool RemapProfileIndex(const std::vector<ProfileLineHeader>& profile_line_headers, |
| 668 | /*out*/SafeMap<uint8_t, uint8_t>* dex_profile_index_remap); |
| 669 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 670 | // Read the inline cache encoding from line_bufer into inline_cache. |
| 671 | bool ReadInlineCache(SafeBuffer& buffer, |
| 672 | uint8_t number_of_dex_files, |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 673 | const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 674 | /*out*/InlineCacheMap* inline_cache, |
| 675 | /*out*/std::string* error); |
| 676 | |
| 677 | // Encode the inline cache into the given buffer. |
| 678 | void AddInlineCacheToBuffer(std::vector<uint8_t>* buffer, |
| 679 | const InlineCacheMap& inline_cache); |
| 680 | |
| 681 | // Return the number of bytes needed to encode the profile information |
| 682 | // for the methods in dex_data. |
| 683 | uint32_t GetMethodsRegionSize(const DexFileData& dex_data); |
| 684 | |
| 685 | // Group `classes` by their owning dex profile index and put the result in |
| 686 | // `dex_to_classes_map`. |
| 687 | void GroupClassesByDex( |
| 688 | const ClassSet& classes, |
| 689 | /*out*/SafeMap<uint8_t, std::vector<dex::TypeIndex>>* dex_to_classes_map); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 690 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 691 | // Find the data for the dex_pc in the inline cache. Adds an empty entry |
| 692 | // if no previous data exists. |
| 693 | DexPcData* FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc); |
| 694 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 695 | friend class ProfileCompilationInfoTest; |
| 696 | friend class CompilerDriverProfileTest; |
| 697 | friend class ProfileAssistantTest; |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 698 | friend class Dex2oatLayoutTest; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 699 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 700 | ArenaPool default_arena_pool_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame^] | 701 | ArenaAllocator allocator_; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 702 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 703 | // Vector containing the actual profile info. |
| 704 | // The vector index is the profile index of the dex data and |
| 705 | // matched DexFileData::profile_index. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 706 | ArenaVector<DexFileData*> info_; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 707 | |
| 708 | // Cache mapping profile keys to profile index. |
| 709 | // This is used to speed up searches since it avoids iterating |
| 710 | // over the info_ vector when searching by profile key. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 711 | ArenaSafeMap<const std::string, uint8_t> profile_key_map_; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 712 | }; |
| 713 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 714 | } // namespace art |
| 715 | |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 716 | #endif // ART_RUNTIME_JIT_PROFILE_COMPILATION_INFO_H_ |