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