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 | #include "profile_compilation_info.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 18 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 19 | #include <sys/file.h> |
| 20 | #include <sys/stat.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 22 | #include <sys/uio.h> |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | #include <zlib.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | |
| 26 | #include <cerrno> |
| 27 | #include <climits> |
| 28 | #include <cstdlib> |
| 29 | #include <string> |
| 30 | #include <vector> |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 31 | #include <iostream> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 32 | |
| 33 | #include "android-base/file.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 34 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 35 | #include "base/arena_allocator.h" |
| 36 | #include "base/dumpable.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 37 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 38 | #include "base/logging.h" // For VLOG. |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 39 | #include "base/mutex.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 40 | #include "base/scoped_flock.h" |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 41 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 42 | #include "base/systrace.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 43 | #include "base/time_utils.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 44 | #include "base/unix_file/fd_file.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 45 | #include "dex/dex_file_loader.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 46 | #include "jit/profiling_info.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 47 | #include "os.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 48 | #include "safe_map.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 49 | #include "utils.h" |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 50 | #include "zip_archive.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 54 | const uint8_t ProfileCompilationInfo::kProfileMagic[] = { 'p', 'r', 'o', '\0' }; |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 55 | // Last profile version: merge profiles directly from the file without creating |
| 56 | // profile_compilation_info object. All the profile line headers are now placed together |
| 57 | // before corresponding method_encodings and class_ids. |
| 58 | const uint8_t ProfileCompilationInfo::kProfileVersion[] = { '0', '1', '0', '\0' }; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 59 | |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 60 | // The name of the profile entry in the dex metadata file. |
| 61 | // DO NOT CHANGE THIS! (it's similar to classes.dex in the apk files). |
| 62 | const char* ProfileCompilationInfo::kDexMetadataProfileEntry = "primary.prof"; |
| 63 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 64 | static constexpr uint16_t kMaxDexFileKeyLength = PATH_MAX; |
| 65 | |
Calin Juravle | c458857 | 2016-06-08 14:24:13 +0100 | [diff] [blame] | 66 | // Debug flag to ignore checksums when testing if a method or a class is present in the profile. |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 67 | // Used to facilitate testing profile guided compilation across a large number of apps |
Calin Juravle | c458857 | 2016-06-08 14:24:13 +0100 | [diff] [blame] | 68 | // using the same test profile. |
| 69 | static constexpr bool kDebugIgnoreChecksum = false; |
| 70 | |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 71 | static constexpr uint8_t kIsMissingTypesEncoding = 6; |
| 72 | static constexpr uint8_t kIsMegamorphicEncoding = 7; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 73 | |
| 74 | static_assert(sizeof(InlineCache::kIndividualCacheSize) == sizeof(uint8_t), |
| 75 | "InlineCache::kIndividualCacheSize does not have the expect type size"); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 76 | static_assert(InlineCache::kIndividualCacheSize < kIsMegamorphicEncoding, |
| 77 | "InlineCache::kIndividualCacheSize is larger than expected"); |
| 78 | static_assert(InlineCache::kIndividualCacheSize < kIsMissingTypesEncoding, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 79 | "InlineCache::kIndividualCacheSize is larger than expected"); |
| 80 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 81 | static bool ChecksumMatch(uint32_t dex_file_checksum, uint32_t checksum) { |
| 82 | return kDebugIgnoreChecksum || dex_file_checksum == checksum; |
| 83 | } |
| 84 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 85 | ProfileCompilationInfo::ProfileCompilationInfo(ArenaPool* custom_arena_pool) |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 86 | : default_arena_pool_(), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 87 | allocator_(custom_arena_pool), |
| 88 | info_(allocator_.Adapter(kArenaAllocProfile)), |
| 89 | profile_key_map_(std::less<const std::string>(), allocator_.Adapter(kArenaAllocProfile)) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | ProfileCompilationInfo::ProfileCompilationInfo() |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 93 | : default_arena_pool_(/*use_malloc*/true, /*low_4gb*/false, "ProfileCompilationInfo"), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 94 | allocator_(&default_arena_pool_), |
| 95 | info_(allocator_.Adapter(kArenaAllocProfile)), |
| 96 | profile_key_map_(std::less<const std::string>(), allocator_.Adapter(kArenaAllocProfile)) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | ProfileCompilationInfo::~ProfileCompilationInfo() { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 100 | VLOG(profiler) << Dumpable<MemStats>(allocator_.GetMemStats()); |
Calin Juravle | 798ba16 | 2017-05-23 23:01:53 -0700 | [diff] [blame] | 101 | for (DexFileData* data : info_) { |
| 102 | delete data; |
| 103 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 106 | void ProfileCompilationInfo::DexPcData::AddClass(uint16_t dex_profile_idx, |
| 107 | const dex::TypeIndex& type_idx) { |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 108 | if (is_megamorphic || is_missing_types) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 109 | return; |
| 110 | } |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 111 | |
| 112 | // Perform an explicit lookup for the type instead of directly emplacing the |
| 113 | // element. We do this because emplace() allocates the node before doing the |
| 114 | // lookup and if it then finds an identical element, it shall deallocate the |
| 115 | // node. For Arena allocations, that's essentially a leak. |
| 116 | ClassReference ref(dex_profile_idx, type_idx); |
| 117 | auto it = classes.find(ref); |
| 118 | if (it != classes.end()) { |
| 119 | // The type index exists. |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // Check if the adding the type will cause the cache to become megamorphic. |
| 124 | if (classes.size() + 1 >= InlineCache::kIndividualCacheSize) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 125 | is_megamorphic = true; |
| 126 | classes.clear(); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 127 | return; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 128 | } |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 129 | |
| 130 | // The type does not exist and the inline cache will not be megamorphic. |
| 131 | classes.insert(ref); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 134 | // Transform the actual dex location into relative paths. |
| 135 | // Note: this is OK because we don't store profiles of different apps into the same file. |
| 136 | // Apps with split apks don't cause trouble because each split has a different name and will not |
| 137 | // collide with other entries. |
Calin Juravle | 31708b7 | 2016-02-05 19:44:05 +0000 | [diff] [blame] | 138 | std::string ProfileCompilationInfo::GetProfileDexFileKey(const std::string& dex_location) { |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 139 | DCHECK(!dex_location.empty()); |
| 140 | size_t last_sep_index = dex_location.find_last_of('/'); |
| 141 | if (last_sep_index == std::string::npos) { |
| 142 | return dex_location; |
| 143 | } else { |
Calin Juravle | 31708b7 | 2016-02-05 19:44:05 +0000 | [diff] [blame] | 144 | DCHECK(last_sep_index < dex_location.size()); |
| 145 | return dex_location.substr(last_sep_index + 1); |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 149 | bool ProfileCompilationInfo::AddMethodIndex(MethodHotness::Flag flags, const MethodReference& ref) { |
| 150 | DexFileData* data = GetOrAddDexFileData(ref.dex_file); |
| 151 | if (data == nullptr) { |
| 152 | return false; |
| 153 | } |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 154 | return data->AddMethod(flags, ref.index); |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool ProfileCompilationInfo::AddMethodIndex(MethodHotness::Flag flags, |
| 158 | const std::string& dex_location, |
| 159 | uint32_t checksum, |
| 160 | uint16_t method_idx, |
| 161 | uint32_t num_method_ids) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 162 | DexFileData* data = GetOrAddDexFileData(GetProfileDexFileKey(dex_location), |
| 163 | checksum, |
| 164 | num_method_ids); |
| 165 | if (data == nullptr) { |
| 166 | return false; |
| 167 | } |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 168 | return data->AddMethod(flags, method_idx); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 171 | bool ProfileCompilationInfo::AddMethods(const std::vector<ProfileMethodInfo>& methods) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 172 | for (const ProfileMethodInfo& method : methods) { |
| 173 | if (!AddMethod(method)) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | } |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool ProfileCompilationInfo::AddClasses(const std::set<DexCacheResolvedClasses>& resolved_classes) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 181 | for (const DexCacheResolvedClasses& dex_cache : resolved_classes) { |
| 182 | if (!AddResolvedClasses(dex_cache)) { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 189 | bool ProfileCompilationInfo::MergeWith(const std::string& filename) { |
| 190 | std::string error; |
| 191 | int flags = O_RDONLY | O_NOFOLLOW | O_CLOEXEC; |
| 192 | ScopedFlock profile_file = LockedFile::Open(filename.c_str(), flags, |
| 193 | /*block*/false, &error); |
| 194 | |
| 195 | if (profile_file.get() == nullptr) { |
| 196 | LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error; |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | int fd = profile_file->Fd(); |
| 201 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 202 | ProfileLoadStatus status = LoadInternal(fd, &error); |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 203 | if (status == kProfileLoadSuccess) { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error; |
| 208 | return false; |
| 209 | } |
| 210 | |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 211 | bool ProfileCompilationInfo::Load(const std::string& filename, bool clear_if_invalid) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 212 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 213 | std::string error; |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 214 | |
| 215 | if (!IsEmpty()) { |
| 216 | return kProfileLoadWouldOverwiteData; |
| 217 | } |
| 218 | |
Calin Juravle | df674c4 | 2017-04-27 19:30:16 -0700 | [diff] [blame] | 219 | int flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC; |
| 220 | // There's no need to fsync profile data right away. We get many chances |
| 221 | // to write it again in case something goes wrong. We can rely on a simple |
| 222 | // close(), no sync, and let to the kernel decide when to write to disk. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 223 | ScopedFlock profile_file = LockedFile::Open(filename.c_str(), flags, |
| 224 | /*block*/false, &error); |
| 225 | |
| 226 | if (profile_file.get() == nullptr) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 227 | LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error; |
| 228 | return false; |
| 229 | } |
| 230 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 231 | int fd = profile_file->Fd(); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 232 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 233 | ProfileLoadStatus status = LoadInternal(fd, &error); |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 234 | if (status == kProfileLoadSuccess) { |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
| 238 | if (clear_if_invalid && |
| 239 | ((status == kProfileLoadVersionMismatch) || (status == kProfileLoadBadData))) { |
| 240 | LOG(WARNING) << "Clearing bad or obsolete profile data from file " |
| 241 | << filename << ": " << error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 242 | if (profile_file->ClearContent()) { |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 243 | return true; |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 244 | } else { |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 245 | PLOG(WARNING) << "Could not clear profile file: " << filename; |
| 246 | return false; |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 247 | } |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error; |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | bool ProfileCompilationInfo::Save(const std::string& filename, uint64_t* bytes_written) { |
| 255 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 256 | std::string error; |
| 257 | int flags = O_WRONLY | O_NOFOLLOW | O_CLOEXEC; |
| 258 | // There's no need to fsync profile data right away. We get many chances |
| 259 | // to write it again in case something goes wrong. We can rely on a simple |
| 260 | // close(), no sync, and let to the kernel decide when to write to disk. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 261 | ScopedFlock profile_file = LockedFile::Open(filename.c_str(), flags, |
| 262 | /*block*/false, &error); |
| 263 | if (profile_file.get() == nullptr) { |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 264 | LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 268 | int fd = profile_file->Fd(); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 269 | |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 270 | // We need to clear the data because we don't support appending to the profiles yet. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 271 | if (!profile_file->ClearContent()) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 272 | PLOG(WARNING) << "Could not clear profile file: " << filename; |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // This doesn't need locking because we are trying to lock the file for exclusive |
| 277 | // access and fail immediately if we can't. |
| 278 | bool result = Save(fd); |
| 279 | if (result) { |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 280 | int64_t size = GetFileSizeBytes(filename); |
| 281 | if (size != -1) { |
| 282 | VLOG(profiler) |
| 283 | << "Successfully saved profile info to " << filename << " Size: " |
| 284 | << size; |
| 285 | if (bytes_written != nullptr) { |
| 286 | *bytes_written = static_cast<uint64_t>(size); |
| 287 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 288 | } |
| 289 | } else { |
| 290 | VLOG(profiler) << "Failed to save profile info to " << filename; |
| 291 | } |
| 292 | return result; |
| 293 | } |
| 294 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 295 | // Returns true if all the bytes were successfully written to the file descriptor. |
| 296 | static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) { |
| 297 | while (byte_count > 0) { |
| 298 | int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count)); |
| 299 | if (bytes_written == -1) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 302 | byte_count -= bytes_written; // Reduce the number of remaining bytes. |
| 303 | buffer += bytes_written; // Move the buffer forward. |
| 304 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 305 | return true; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 308 | // Add the string bytes to the buffer. |
| 309 | static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) { |
| 310 | buffer->insert(buffer->end(), value.begin(), value.end()); |
| 311 | } |
| 312 | |
| 313 | // Insert each byte, from low to high into the buffer. |
| 314 | template <typename T> |
| 315 | static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) { |
| 316 | for (size_t i = 0; i < sizeof(T); i++) { |
| 317 | buffer->push_back((value >> (i * kBitsPerByte)) & 0xff); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | static constexpr size_t kLineHeaderSize = |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 322 | 2 * sizeof(uint16_t) + // class_set.size + dex_location.size |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 323 | 3 * sizeof(uint32_t); // method_map.size + checksum + num_method_ids |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * Serialization format: |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 327 | * [profile_header, zipped[[profile_line_header1, profile_line_header2...],[profile_line_data1, |
| 328 | * profile_line_data2...]]] |
| 329 | * profile_header: |
| 330 | * magic,version,number_of_dex_files,uncompressed_size_of_zipped_data,compressed_data_size |
| 331 | * profile_line_header: |
| 332 | * dex_location,number_of_classes,methods_region_size,dex_location_checksum,num_method_ids |
| 333 | * profile_line_data: |
| 334 | * method_encoding_1,method_encoding_2...,class_id1,class_id2...,startup/post startup bitmap |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 335 | * The method_encoding is: |
| 336 | * method_id,number_of_inline_caches,inline_cache1,inline_cache2... |
| 337 | * The inline_cache is: |
| 338 | * dex_pc,[M|dex_map_size], dex_profile_index,class_id1,class_id2...,dex_profile_index2,... |
| 339 | * dex_map_size is the number of dex_indeces that follows. |
| 340 | * Classes are grouped per their dex files and the line |
| 341 | * `dex_profile_index,class_id1,class_id2...,dex_profile_index2,...` encodes the |
| 342 | * mapping from `dex_profile_index` to the set of classes `class_id1,class_id2...` |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 343 | * M stands for megamorphic or missing types and it's encoded as either |
| 344 | * the byte kIsMegamorphicEncoding or kIsMissingTypesEncoding. |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 345 | * When present, there will be no class ids following. |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 346 | **/ |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 347 | bool ProfileCompilationInfo::Save(int fd) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 348 | uint64_t start = NanoTime(); |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 349 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 350 | DCHECK_GE(fd, 0); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 351 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 352 | // Use a vector wrapper to avoid keeping track of offsets when we add elements. |
| 353 | std::vector<uint8_t> buffer; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 354 | if (!WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic))) { |
| 355 | return false; |
| 356 | } |
| 357 | if (!WriteBuffer(fd, kProfileVersion, sizeof(kProfileVersion))) { |
| 358 | return false; |
| 359 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 360 | DCHECK_LE(info_.size(), std::numeric_limits<uint8_t>::max()); |
| 361 | AddUintToBuffer(&buffer, static_cast<uint8_t>(info_.size())); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 362 | |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 363 | uint32_t required_capacity = 0; |
| 364 | for (const DexFileData* dex_data_ptr : info_) { |
| 365 | const DexFileData& dex_data = *dex_data_ptr; |
| 366 | uint32_t methods_region_size = GetMethodsRegionSize(dex_data); |
| 367 | required_capacity += kLineHeaderSize + |
| 368 | dex_data.profile_key.size() + |
| 369 | sizeof(uint16_t) * dex_data.class_set.size() + |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 370 | methods_region_size + |
| 371 | dex_data.bitmap_storage.size(); |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 372 | } |
Mathieu Chartier | f2e2af8 | 2017-07-12 21:09:57 -0700 | [diff] [blame] | 373 | // Allow large profiles for non target builds for the case where we are merging many profiles |
| 374 | // to generate a boot image profile. |
| 375 | if (kIsTargetBuild && required_capacity > kProfileSizeErrorThresholdInBytes) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 376 | LOG(ERROR) << "Profile data size exceeds " |
| 377 | << std::to_string(kProfileSizeErrorThresholdInBytes) |
| 378 | << " bytes. Profile will not be written to disk."; |
| 379 | return false; |
| 380 | } |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 381 | AddUintToBuffer(&buffer, required_capacity); |
| 382 | if (!WriteBuffer(fd, buffer.data(), buffer.size())) { |
| 383 | return false; |
| 384 | } |
| 385 | // Make sure that the buffer has enough capacity to avoid repeated resizings |
| 386 | // while we add data. |
| 387 | buffer.reserve(required_capacity); |
| 388 | buffer.clear(); |
| 389 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 390 | // Dex files must be written in the order of their profile index. This |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 391 | // avoids writing the index in the output file and simplifies the parsing logic. |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 392 | // Write profile line headers. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 393 | for (const DexFileData* dex_data_ptr : info_) { |
| 394 | const DexFileData& dex_data = *dex_data_ptr; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 395 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 396 | if (dex_data.profile_key.size() >= kMaxDexFileKeyLength) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 397 | LOG(WARNING) << "DexFileKey exceeds allocated limit"; |
| 398 | return false; |
| 399 | } |
| 400 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 401 | uint32_t methods_region_size = GetMethodsRegionSize(dex_data); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 402 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 403 | DCHECK_LE(dex_data.profile_key.size(), std::numeric_limits<uint16_t>::max()); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 404 | DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max()); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 405 | // Write profile line header. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 406 | AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.profile_key.size())); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 407 | AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size())); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 408 | AddUintToBuffer(&buffer, methods_region_size); // uint32_t |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 409 | AddUintToBuffer(&buffer, dex_data.checksum); // uint32_t |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 410 | AddUintToBuffer(&buffer, dex_data.num_method_ids); // uint32_t |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 411 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 412 | AddStringToBuffer(&buffer, dex_data.profile_key); |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | for (const DexFileData* dex_data_ptr : info_) { |
| 416 | const DexFileData& dex_data = *dex_data_ptr; |
| 417 | |
| 418 | // Note that we allow dex files without any methods or classes, so that |
| 419 | // inline caches can refer valid dex files. |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 420 | |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 421 | uint16_t last_method_index = 0; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 422 | for (const auto& method_it : dex_data.method_map) { |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 423 | // Store the difference between the method indices. The SafeMap is ordered by |
| 424 | // method_id, so the difference will always be non negative. |
| 425 | DCHECK_GE(method_it.first, last_method_index); |
| 426 | uint16_t diff_with_last_method_index = method_it.first - last_method_index; |
| 427 | last_method_index = method_it.first; |
| 428 | AddUintToBuffer(&buffer, diff_with_last_method_index); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 429 | AddInlineCacheToBuffer(&buffer, method_it.second); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 430 | } |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 431 | |
| 432 | uint16_t last_class_index = 0; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 433 | for (const auto& class_id : dex_data.class_set) { |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 434 | // Store the difference between the class indices. The set is ordered by |
| 435 | // class_id, so the difference will always be non negative. |
| 436 | DCHECK_GE(class_id.index_, last_class_index); |
| 437 | uint16_t diff_with_last_class_index = class_id.index_ - last_class_index; |
| 438 | last_class_index = class_id.index_; |
| 439 | AddUintToBuffer(&buffer, diff_with_last_class_index); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 440 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 441 | |
| 442 | buffer.insert(buffer.end(), |
| 443 | dex_data.bitmap_storage.begin(), |
| 444 | dex_data.bitmap_storage.end()); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 447 | uint32_t output_size = 0; |
| 448 | std::unique_ptr<uint8_t[]> compressed_buffer = DeflateBuffer(buffer.data(), |
| 449 | required_capacity, |
| 450 | &output_size); |
| 451 | |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 452 | if (output_size > kProfileSizeWarningThresholdInBytes) { |
| 453 | LOG(WARNING) << "Profile data size exceeds " |
| 454 | << std::to_string(kProfileSizeWarningThresholdInBytes); |
| 455 | } |
| 456 | |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 457 | buffer.clear(); |
| 458 | AddUintToBuffer(&buffer, output_size); |
| 459 | |
| 460 | if (!WriteBuffer(fd, buffer.data(), buffer.size())) { |
| 461 | return false; |
| 462 | } |
| 463 | if (!WriteBuffer(fd, compressed_buffer.get(), output_size)) { |
| 464 | return false; |
| 465 | } |
| 466 | uint64_t total_time = NanoTime() - start; |
| 467 | VLOG(profiler) << "Compressed from " |
| 468 | << std::to_string(required_capacity) |
| 469 | << " to " |
| 470 | << std::to_string(output_size); |
| 471 | VLOG(profiler) << "Time to save profile: " << std::to_string(total_time); |
| 472 | return true; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 475 | void ProfileCompilationInfo::AddInlineCacheToBuffer(std::vector<uint8_t>* buffer, |
| 476 | const InlineCacheMap& inline_cache_map) { |
| 477 | // Add inline cache map size. |
| 478 | AddUintToBuffer(buffer, static_cast<uint16_t>(inline_cache_map.size())); |
| 479 | if (inline_cache_map.size() == 0) { |
| 480 | return; |
| 481 | } |
| 482 | for (const auto& inline_cache_it : inline_cache_map) { |
| 483 | uint16_t dex_pc = inline_cache_it.first; |
| 484 | const DexPcData dex_pc_data = inline_cache_it.second; |
| 485 | const ClassSet& classes = dex_pc_data.classes; |
| 486 | |
| 487 | // Add the dex pc. |
| 488 | AddUintToBuffer(buffer, dex_pc); |
| 489 | |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 490 | // Add the megamorphic/missing_types encoding if needed and continue. |
| 491 | // In either cases we don't add any classes to the profiles and so there's |
| 492 | // no point to continue. |
| 493 | // TODO(calin): in case we miss types there is still value to add the |
| 494 | // rest of the classes. They can be added without bumping the profile version. |
| 495 | if (dex_pc_data.is_missing_types) { |
| 496 | DCHECK(!dex_pc_data.is_megamorphic); // at this point the megamorphic flag should not be set. |
| 497 | DCHECK_EQ(classes.size(), 0u); |
| 498 | AddUintToBuffer(buffer, kIsMissingTypesEncoding); |
| 499 | continue; |
| 500 | } else if (dex_pc_data.is_megamorphic) { |
| 501 | DCHECK_EQ(classes.size(), 0u); |
| 502 | AddUintToBuffer(buffer, kIsMegamorphicEncoding); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 503 | continue; |
| 504 | } |
| 505 | |
| 506 | DCHECK_LT(classes.size(), InlineCache::kIndividualCacheSize); |
| 507 | DCHECK_NE(classes.size(), 0u) << "InlineCache contains a dex_pc with 0 classes"; |
| 508 | |
| 509 | SafeMap<uint8_t, std::vector<dex::TypeIndex>> dex_to_classes_map; |
| 510 | // Group the classes by dex. We expect that most of the classes will come from |
| 511 | // the same dex, so this will be more efficient than encoding the dex index |
| 512 | // for each class reference. |
| 513 | GroupClassesByDex(classes, &dex_to_classes_map); |
| 514 | // Add the dex map size. |
| 515 | AddUintToBuffer(buffer, static_cast<uint8_t>(dex_to_classes_map.size())); |
| 516 | for (const auto& dex_it : dex_to_classes_map) { |
| 517 | uint8_t dex_profile_index = dex_it.first; |
| 518 | const std::vector<dex::TypeIndex>& dex_classes = dex_it.second; |
| 519 | // Add the dex profile index. |
| 520 | AddUintToBuffer(buffer, dex_profile_index); |
| 521 | // Add the the number of classes for each dex profile index. |
| 522 | AddUintToBuffer(buffer, static_cast<uint8_t>(dex_classes.size())); |
| 523 | for (size_t i = 0; i < dex_classes.size(); i++) { |
| 524 | // Add the type index of the classes. |
| 525 | AddUintToBuffer(buffer, dex_classes[i].index_); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | uint32_t ProfileCompilationInfo::GetMethodsRegionSize(const DexFileData& dex_data) { |
| 532 | // ((uint16_t)method index + (uint16_t)inline cache size) * number of methods |
| 533 | uint32_t size = 2 * sizeof(uint16_t) * dex_data.method_map.size(); |
| 534 | for (const auto& method_it : dex_data.method_map) { |
| 535 | const InlineCacheMap& inline_cache = method_it.second; |
| 536 | size += sizeof(uint16_t) * inline_cache.size(); // dex_pc |
| 537 | for (const auto& inline_cache_it : inline_cache) { |
| 538 | const ClassSet& classes = inline_cache_it.second.classes; |
| 539 | SafeMap<uint8_t, std::vector<dex::TypeIndex>> dex_to_classes_map; |
| 540 | GroupClassesByDex(classes, &dex_to_classes_map); |
| 541 | size += sizeof(uint8_t); // dex_to_classes_map size |
| 542 | for (const auto& dex_it : dex_to_classes_map) { |
| 543 | size += sizeof(uint8_t); // dex profile index |
| 544 | size += sizeof(uint8_t); // number of classes |
| 545 | const std::vector<dex::TypeIndex>& dex_classes = dex_it.second; |
| 546 | size += sizeof(uint16_t) * dex_classes.size(); // the actual classes |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | return size; |
| 551 | } |
| 552 | |
| 553 | void ProfileCompilationInfo::GroupClassesByDex( |
| 554 | const ClassSet& classes, |
| 555 | /*out*/SafeMap<uint8_t, std::vector<dex::TypeIndex>>* dex_to_classes_map) { |
| 556 | for (const auto& classes_it : classes) { |
| 557 | auto dex_it = dex_to_classes_map->FindOrAdd(classes_it.dex_profile_index); |
| 558 | dex_it->second.push_back(classes_it.type_index); |
| 559 | } |
| 560 | } |
| 561 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 562 | ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData( |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 563 | const std::string& profile_key, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 564 | uint32_t checksum, |
| 565 | uint32_t num_method_ids) { |
Vladimir Marko | 3bada4b | 2017-05-19 12:32:47 +0100 | [diff] [blame] | 566 | const auto profile_index_it = profile_key_map_.FindOrAdd(profile_key, profile_key_map_.size()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 567 | if (profile_key_map_.size() > std::numeric_limits<uint8_t>::max()) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 568 | // Allow only 255 dex files to be profiled. This allows us to save bytes |
| 569 | // when encoding. The number is well above what we expect for normal applications. |
| 570 | if (kIsDebugBuild) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 571 | LOG(ERROR) << "Exceeded the maximum number of dex files (255). Something went wrong"; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 572 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 573 | profile_key_map_.erase(profile_key); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 574 | return nullptr; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 575 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 576 | |
| 577 | uint8_t profile_index = profile_index_it->second; |
| 578 | if (info_.size() <= profile_index) { |
| 579 | // This is a new addition. Add it to the info_ array. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 580 | DexFileData* dex_file_data = new (&allocator_) DexFileData( |
| 581 | &allocator_, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 582 | profile_key, |
| 583 | checksum, |
| 584 | profile_index, |
| 585 | num_method_ids); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 586 | info_.push_back(dex_file_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 587 | } |
| 588 | DexFileData* result = info_[profile_index]; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 589 | |
| 590 | // Check that the checksum matches. |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 591 | // This may different if for example the dex file was updated and we had a record of the old one. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 592 | if (result->checksum != checksum) { |
| 593 | LOG(WARNING) << "Checksum mismatch for dex " << profile_key; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 594 | return nullptr; |
| 595 | } |
Shubham Ajmera | 61200a0 | 2017-08-30 16:29:41 -0700 | [diff] [blame] | 596 | |
| 597 | // DCHECK that profile info map key is consistent with the one stored in the dex file data. |
| 598 | // This should always be the case since since the cache map is managed by ProfileCompilationInfo. |
| 599 | DCHECK_EQ(profile_key, result->profile_key); |
| 600 | DCHECK_EQ(profile_index, result->profile_index); |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 601 | |
| 602 | if (num_method_ids != result->num_method_ids) { |
| 603 | // This should not happen... added to help investigating b/65812889. |
| 604 | LOG(ERROR) << "num_method_ids mismatch for dex " << profile_key |
| 605 | << ", expected=" << num_method_ids |
| 606 | << ", actual=" << result->num_method_ids; |
| 607 | return nullptr; |
| 608 | } |
Shubham Ajmera | 61200a0 | 2017-08-30 16:29:41 -0700 | [diff] [blame] | 609 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 610 | return result; |
| 611 | } |
| 612 | |
| 613 | const ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::FindDexData( |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 614 | const std::string& profile_key, |
| 615 | uint32_t checksum, |
| 616 | bool verify_checksum) const { |
Vladimir Marko | 3bada4b | 2017-05-19 12:32:47 +0100 | [diff] [blame] | 617 | const auto profile_index_it = profile_key_map_.find(profile_key); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 618 | if (profile_index_it == profile_key_map_.end()) { |
| 619 | return nullptr; |
| 620 | } |
| 621 | |
| 622 | uint8_t profile_index = profile_index_it->second; |
| 623 | const DexFileData* result = info_[profile_index]; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 624 | if (verify_checksum && !ChecksumMatch(result->checksum, checksum)) { |
| 625 | return nullptr; |
| 626 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 627 | DCHECK_EQ(profile_key, result->profile_key); |
| 628 | DCHECK_EQ(profile_index, result->profile_index); |
| 629 | return result; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) { |
| 633 | const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation()); |
| 634 | const uint32_t checksum = classes.GetLocationChecksum(); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 635 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum, classes.NumMethodIds()); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 636 | if (data == nullptr) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 637 | return false; |
| 638 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 639 | data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end()); |
| 640 | return true; |
| 641 | } |
| 642 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 643 | bool ProfileCompilationInfo::AddMethod(const std::string& dex_location, |
| 644 | uint32_t dex_checksum, |
| 645 | uint16_t method_index, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 646 | uint32_t num_method_ids, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 647 | const OfflineProfileMethodInfo& pmi) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 648 | DexFileData* const data = GetOrAddDexFileData(GetProfileDexFileKey(dex_location), |
| 649 | dex_checksum, |
| 650 | num_method_ids); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 651 | if (data == nullptr) { // checksum mismatch |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 652 | return false; |
| 653 | } |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 654 | // Add the method. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 655 | InlineCacheMap* inline_cache = data->FindOrAddMethod(method_index); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 656 | |
| 657 | if (pmi.inline_caches == nullptr) { |
| 658 | // If we don't have inline caches return success right away. |
| 659 | return true; |
| 660 | } |
| 661 | for (const auto& pmi_inline_cache_it : *pmi.inline_caches) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 662 | uint16_t pmi_ic_dex_pc = pmi_inline_cache_it.first; |
| 663 | const DexPcData& pmi_ic_dex_pc_data = pmi_inline_cache_it.second; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 664 | DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, pmi_ic_dex_pc); |
| 665 | if (dex_pc_data->is_missing_types || dex_pc_data->is_megamorphic) { |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 666 | // We are already megamorphic or we are missing types; no point in going forward. |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 667 | continue; |
| 668 | } |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 669 | |
| 670 | if (pmi_ic_dex_pc_data.is_missing_types) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 671 | dex_pc_data->SetIsMissingTypes(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 672 | continue; |
| 673 | } |
| 674 | if (pmi_ic_dex_pc_data.is_megamorphic) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 675 | dex_pc_data->SetIsMegamorphic(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 676 | continue; |
| 677 | } |
| 678 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 679 | for (const ClassReference& class_ref : pmi_ic_dex_pc_data.classes) { |
| 680 | const DexReference& dex_ref = pmi.dex_references[class_ref.dex_profile_index]; |
| 681 | DexFileData* class_dex_data = GetOrAddDexFileData( |
| 682 | GetProfileDexFileKey(dex_ref.dex_location), |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 683 | dex_ref.dex_checksum, |
| 684 | dex_ref.num_method_ids); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 685 | if (class_dex_data == nullptr) { // checksum mismatch |
| 686 | return false; |
| 687 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 688 | dex_pc_data->AddClass(class_dex_data->profile_index, class_ref.type_index); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | return true; |
| 692 | } |
| 693 | |
| 694 | bool ProfileCompilationInfo::AddMethod(const ProfileMethodInfo& pmi) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 695 | DexFileData* const data = GetOrAddDexFileData(pmi.ref.dex_file); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 696 | if (data == nullptr) { // checksum mismatch |
| 697 | return false; |
| 698 | } |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 699 | InlineCacheMap* inline_cache = data->FindOrAddMethod(pmi.ref.index); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 700 | |
| 701 | for (const ProfileMethodInfo::ProfileInlineCache& cache : pmi.inline_caches) { |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 702 | if (cache.is_missing_types) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 703 | FindOrAddDexPc(inline_cache, cache.dex_pc)->SetIsMissingTypes(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 704 | continue; |
| 705 | } |
Mathieu Chartier | dbddc22 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 706 | for (const TypeReference& class_ref : cache.classes) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 707 | DexFileData* class_dex_data = GetOrAddDexFileData(class_ref.dex_file); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 708 | if (class_dex_data == nullptr) { // checksum mismatch |
| 709 | return false; |
| 710 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 711 | DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, cache.dex_pc); |
| 712 | if (dex_pc_data->is_missing_types) { |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 713 | // Don't bother adding classes if we are missing types. |
| 714 | break; |
| 715 | } |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 716 | dex_pc_data->AddClass(class_dex_data->profile_index, class_ref.TypeIndex()); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 719 | return true; |
| 720 | } |
| 721 | |
| 722 | bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location, |
| 723 | uint32_t checksum, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 724 | dex::TypeIndex type_idx, |
| 725 | uint32_t num_method_ids) { |
| 726 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum, num_method_ids); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 727 | if (data == nullptr) { |
| 728 | return false; |
| 729 | } |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 730 | data->class_set.insert(type_idx); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 731 | return true; |
| 732 | } |
| 733 | |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 734 | #define READ_UINT(type, buffer, dest, error) \ |
| 735 | do { \ |
| 736 | if (!(buffer).ReadUintAndAdvance<type>(&(dest))) { \ |
| 737 | *(error) = "Could not read "#dest; \ |
| 738 | return false; \ |
| 739 | } \ |
| 740 | } \ |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 741 | while (false) |
| 742 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 743 | bool ProfileCompilationInfo::ReadInlineCache( |
| 744 | SafeBuffer& buffer, |
| 745 | uint8_t number_of_dex_files, |
| 746 | const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap, |
| 747 | /*out*/ InlineCacheMap* inline_cache, |
| 748 | /*out*/ std::string* error) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 749 | uint16_t inline_cache_size; |
| 750 | READ_UINT(uint16_t, buffer, inline_cache_size, error); |
| 751 | for (; inline_cache_size > 0; inline_cache_size--) { |
| 752 | uint16_t dex_pc; |
| 753 | uint8_t dex_to_classes_map_size; |
| 754 | READ_UINT(uint16_t, buffer, dex_pc, error); |
| 755 | READ_UINT(uint8_t, buffer, dex_to_classes_map_size, error); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 756 | DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, dex_pc); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 757 | if (dex_to_classes_map_size == kIsMissingTypesEncoding) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 758 | dex_pc_data->SetIsMissingTypes(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 759 | continue; |
| 760 | } |
| 761 | if (dex_to_classes_map_size == kIsMegamorphicEncoding) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 762 | dex_pc_data->SetIsMegamorphic(); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 763 | continue; |
| 764 | } |
| 765 | for (; dex_to_classes_map_size > 0; dex_to_classes_map_size--) { |
| 766 | uint8_t dex_profile_index; |
| 767 | uint8_t dex_classes_size; |
| 768 | READ_UINT(uint8_t, buffer, dex_profile_index, error); |
| 769 | READ_UINT(uint8_t, buffer, dex_classes_size, error); |
| 770 | if (dex_profile_index >= number_of_dex_files) { |
| 771 | *error = "dex_profile_index out of bounds "; |
| 772 | *error += std::to_string(dex_profile_index) + " " + std::to_string(number_of_dex_files); |
| 773 | return false; |
| 774 | } |
| 775 | for (; dex_classes_size > 0; dex_classes_size--) { |
| 776 | uint16_t type_index; |
| 777 | READ_UINT(uint16_t, buffer, type_index, error); |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 778 | dex_pc_data->AddClass(dex_profile_index_remap.Get(dex_profile_index), |
| 779 | dex::TypeIndex(type_index)); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | return true; |
| 784 | } |
| 785 | |
| 786 | bool ProfileCompilationInfo::ReadMethods(SafeBuffer& buffer, |
| 787 | uint8_t number_of_dex_files, |
| 788 | const ProfileLineHeader& line_header, |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 789 | const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 790 | /*out*/std::string* error) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 791 | uint32_t unread_bytes_before_operation = buffer.CountUnreadBytes(); |
| 792 | if (unread_bytes_before_operation < line_header.method_region_size_bytes) { |
| 793 | *error += "Profile EOF reached prematurely for ReadMethod"; |
| 794 | return kProfileLoadBadData; |
| 795 | } |
| 796 | size_t expected_unread_bytes_after_operation = buffer.CountUnreadBytes() |
| 797 | - line_header.method_region_size_bytes; |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 798 | uint16_t last_method_index = 0; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 799 | while (buffer.CountUnreadBytes() > expected_unread_bytes_after_operation) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 800 | DexFileData* const data = GetOrAddDexFileData(line_header.dex_location, |
| 801 | line_header.checksum, |
| 802 | line_header.num_method_ids); |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 803 | uint16_t diff_with_last_method_index; |
| 804 | READ_UINT(uint16_t, buffer, diff_with_last_method_index, error); |
| 805 | uint16_t method_index = last_method_index + diff_with_last_method_index; |
| 806 | last_method_index = method_index; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 807 | InlineCacheMap* inline_cache = data->FindOrAddMethod(method_index); |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 808 | if (!ReadInlineCache(buffer, |
| 809 | number_of_dex_files, |
| 810 | dex_profile_index_remap, |
| 811 | inline_cache, |
| 812 | error)) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 813 | return false; |
| 814 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 815 | } |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 816 | uint32_t total_bytes_read = unread_bytes_before_operation - buffer.CountUnreadBytes(); |
| 817 | if (total_bytes_read != line_header.method_region_size_bytes) { |
| 818 | *error += "Profile data inconsistent for ReadMethods"; |
| 819 | return false; |
| 820 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 821 | return true; |
| 822 | } |
| 823 | |
| 824 | bool ProfileCompilationInfo::ReadClasses(SafeBuffer& buffer, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 825 | const ProfileLineHeader& line_header, |
| 826 | /*out*/std::string* error) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 827 | size_t unread_bytes_before_op = buffer.CountUnreadBytes(); |
| 828 | if (unread_bytes_before_op < line_header.class_set_size) { |
| 829 | *error += "Profile EOF reached prematurely for ReadClasses"; |
| 830 | return kProfileLoadBadData; |
| 831 | } |
| 832 | |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 833 | uint16_t last_class_index = 0; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 834 | for (uint16_t i = 0; i < line_header.class_set_size; i++) { |
Shubham Ajmera | 4b8a96b | 2017-05-12 18:00:14 +0000 | [diff] [blame] | 835 | uint16_t diff_with_last_class_index; |
| 836 | READ_UINT(uint16_t, buffer, diff_with_last_class_index, error); |
| 837 | uint16_t type_index = last_class_index + diff_with_last_class_index; |
| 838 | last_class_index = type_index; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 839 | if (!AddClassIndex(line_header.dex_location, |
| 840 | line_header.checksum, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 841 | dex::TypeIndex(type_index), |
| 842 | line_header.num_method_ids)) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 843 | return false; |
| 844 | } |
| 845 | } |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 846 | size_t total_bytes_read = unread_bytes_before_op - buffer.CountUnreadBytes(); |
| 847 | uint32_t expected_bytes_read = line_header.class_set_size * sizeof(uint16_t); |
| 848 | if (total_bytes_read != expected_bytes_read) { |
| 849 | *error += "Profile data inconsistent for ReadClasses"; |
| 850 | return false; |
| 851 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 852 | return true; |
| 853 | } |
| 854 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 855 | // Tests for EOF by trying to read 1 byte from the descriptor. |
| 856 | // Returns: |
| 857 | // 0 if the descriptor is at the EOF, |
| 858 | // -1 if there was an IO error |
| 859 | // 1 if the descriptor has more content to read |
| 860 | static int testEOF(int fd) { |
| 861 | uint8_t buffer[1]; |
| 862 | return TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 863 | } |
| 864 | |
| 865 | // Reads an uint value previously written with AddUintToBuffer. |
| 866 | template <typename T> |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 867 | bool ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance(/*out*/T* value) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 868 | static_assert(std::is_unsigned<T>::value, "Type is not unsigned"); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 869 | if (ptr_current_ + sizeof(T) > ptr_end_) { |
| 870 | return false; |
| 871 | } |
| 872 | *value = 0; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 873 | for (size_t i = 0; i < sizeof(T); i++) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 874 | *value += ptr_current_[i] << (i * kBitsPerByte); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 875 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 876 | ptr_current_ += sizeof(T); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 877 | return true; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) { |
| 881 | if (ptr_current_ + data_size > ptr_end_) { |
| 882 | return false; |
| 883 | } |
| 884 | if (memcmp(ptr_current_, data, data_size) == 0) { |
| 885 | ptr_current_ += data_size; |
| 886 | return true; |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 891 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::SafeBuffer::Fill( |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 892 | ProfileSource& source, |
| 893 | const std::string& debug_stage, |
| 894 | /*out*/ std::string* error) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 895 | size_t byte_count = (ptr_end_ - ptr_current_) * sizeof(*ptr_current_); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 896 | uint8_t* buffer = ptr_current_; |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 897 | return source.Read(buffer, byte_count, debug_stage, error); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 900 | size_t ProfileCompilationInfo::SafeBuffer::CountUnreadBytes() { |
| 901 | return (ptr_end_ - ptr_current_) * sizeof(*ptr_current_); |
| 902 | } |
| 903 | |
| 904 | const uint8_t* ProfileCompilationInfo::SafeBuffer::GetCurrentPtr() { |
| 905 | return ptr_current_; |
| 906 | } |
| 907 | |
| 908 | void ProfileCompilationInfo::SafeBuffer::Advance(size_t data_size) { |
| 909 | ptr_current_ += data_size; |
| 910 | } |
| 911 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 912 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadProfileHeader( |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 913 | ProfileSource& source, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 914 | /*out*/uint8_t* number_of_dex_files, |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 915 | /*out*/uint32_t* uncompressed_data_size, |
| 916 | /*out*/uint32_t* compressed_data_size, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 917 | /*out*/std::string* error) { |
| 918 | // Read magic and version |
| 919 | const size_t kMagicVersionSize = |
| 920 | sizeof(kProfileMagic) + |
| 921 | sizeof(kProfileVersion) + |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 922 | sizeof(uint8_t) + // number of dex files |
| 923 | sizeof(uint32_t) + // size of uncompressed profile data |
| 924 | sizeof(uint32_t); // size of compressed profile data |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 925 | |
| 926 | SafeBuffer safe_buffer(kMagicVersionSize); |
| 927 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 928 | ProfileLoadStatus status = safe_buffer.Fill(source, "ReadProfileHeader", error); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 929 | if (status != kProfileLoadSuccess) { |
| 930 | return status; |
| 931 | } |
| 932 | |
| 933 | if (!safe_buffer.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) { |
| 934 | *error = "Profile missing magic"; |
| 935 | return kProfileLoadVersionMismatch; |
| 936 | } |
| 937 | if (!safe_buffer.CompareAndAdvance(kProfileVersion, sizeof(kProfileVersion))) { |
| 938 | *error = "Profile version mismatch"; |
| 939 | return kProfileLoadVersionMismatch; |
| 940 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 941 | if (!safe_buffer.ReadUintAndAdvance<uint8_t>(number_of_dex_files)) { |
| 942 | *error = "Cannot read the number of dex files"; |
| 943 | return kProfileLoadBadData; |
| 944 | } |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 945 | if (!safe_buffer.ReadUintAndAdvance<uint32_t>(uncompressed_data_size)) { |
| 946 | *error = "Cannot read the size of uncompressed data"; |
| 947 | return kProfileLoadBadData; |
| 948 | } |
| 949 | if (!safe_buffer.ReadUintAndAdvance<uint32_t>(compressed_data_size)) { |
| 950 | *error = "Cannot read the size of compressed data"; |
| 951 | return kProfileLoadBadData; |
| 952 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 953 | return kProfileLoadSuccess; |
| 954 | } |
| 955 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 956 | bool ProfileCompilationInfo::ReadProfileLineHeaderElements(SafeBuffer& buffer, |
| 957 | /*out*/uint16_t* dex_location_size, |
| 958 | /*out*/ProfileLineHeader* line_header, |
| 959 | /*out*/std::string* error) { |
| 960 | READ_UINT(uint16_t, buffer, *dex_location_size, error); |
| 961 | READ_UINT(uint16_t, buffer, line_header->class_set_size, error); |
| 962 | READ_UINT(uint32_t, buffer, line_header->method_region_size_bytes, error); |
| 963 | READ_UINT(uint32_t, buffer, line_header->checksum, error); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 964 | READ_UINT(uint32_t, buffer, line_header->num_method_ids, error); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 965 | return true; |
| 966 | } |
| 967 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 968 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadProfileLineHeader( |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 969 | SafeBuffer& buffer, |
| 970 | /*out*/ProfileLineHeader* line_header, |
| 971 | /*out*/std::string* error) { |
| 972 | if (buffer.CountUnreadBytes() < kLineHeaderSize) { |
| 973 | *error += "Profile EOF reached prematurely for ReadProfileLineHeader"; |
| 974 | return kProfileLoadBadData; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 977 | uint16_t dex_location_size; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 978 | if (!ReadProfileLineHeaderElements(buffer, &dex_location_size, line_header, error)) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 979 | return kProfileLoadBadData; |
| 980 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 981 | |
| 982 | if (dex_location_size == 0 || dex_location_size > kMaxDexFileKeyLength) { |
Goran Jakovljevic | 4eb6fbf | 2016-04-25 19:14:17 +0200 | [diff] [blame] | 983 | *error = "DexFileKey has an invalid size: " + |
| 984 | std::to_string(static_cast<uint32_t>(dex_location_size)); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 985 | return kProfileLoadBadData; |
| 986 | } |
| 987 | |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 988 | if (buffer.CountUnreadBytes() < dex_location_size) { |
| 989 | *error += "Profile EOF reached prematurely for ReadProfileHeaderDexLocation"; |
| 990 | return kProfileLoadBadData; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 991 | } |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 992 | const uint8_t* base_ptr = buffer.GetCurrentPtr(); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 993 | line_header->dex_location.assign( |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 994 | reinterpret_cast<const char*>(base_ptr), dex_location_size); |
| 995 | buffer.Advance(dex_location_size); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 996 | return kProfileLoadSuccess; |
| 997 | } |
| 998 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 999 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadProfileLine( |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1000 | SafeBuffer& buffer, |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1001 | uint8_t number_of_dex_files, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1002 | const ProfileLineHeader& line_header, |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1003 | const SafeMap<uint8_t, uint8_t>& dex_profile_index_remap, |
| 1004 | bool merge_classes, |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1005 | /*out*/std::string* error) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1006 | DexFileData* data = GetOrAddDexFileData(line_header.dex_location, |
| 1007 | line_header.checksum, |
| 1008 | line_header.num_method_ids); |
| 1009 | if (data == nullptr) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1010 | *error = "Error when reading profile file line header: checksum mismatch for " |
| 1011 | + line_header.dex_location; |
| 1012 | return kProfileLoadBadData; |
| 1013 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1014 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1015 | if (!ReadMethods(buffer, number_of_dex_files, line_header, dex_profile_index_remap, error)) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1016 | return kProfileLoadBadData; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1017 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1018 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1019 | if (merge_classes) { |
| 1020 | if (!ReadClasses(buffer, line_header, error)) { |
| 1021 | return kProfileLoadBadData; |
| 1022 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1023 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1024 | |
| 1025 | const size_t bytes = data->bitmap_storage.size(); |
| 1026 | if (buffer.CountUnreadBytes() < bytes) { |
| 1027 | *error += "Profile EOF reached prematurely for ReadProfileHeaderDexLocation"; |
| 1028 | return kProfileLoadBadData; |
| 1029 | } |
| 1030 | const uint8_t* base_ptr = buffer.GetCurrentPtr(); |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 1031 | std::copy_n(base_ptr, bytes, data->bitmap_storage.data()); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1032 | buffer.Advance(bytes); |
| 1033 | // Read method bitmap. |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1034 | return kProfileLoadSuccess; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1037 | // TODO(calin): Fix this API. ProfileCompilationInfo::Load should be static and |
| 1038 | // return a unique pointer to a ProfileCompilationInfo upon success. |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1039 | bool ProfileCompilationInfo::Load(int fd, bool merge_classes) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1040 | std::string error; |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1041 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 1042 | ProfileLoadStatus status = LoadInternal(fd, &error, merge_classes); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1043 | |
| 1044 | if (status == kProfileLoadSuccess) { |
| 1045 | return true; |
| 1046 | } else { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1047 | LOG(WARNING) << "Error when reading profile: " << error; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1048 | return false; |
| 1049 | } |
| 1050 | } |
| 1051 | |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 1052 | bool ProfileCompilationInfo::VerifyProfileData(const std::vector<const DexFile*>& dex_files) { |
| 1053 | std::unordered_map<std::string, const DexFile*> key_to_dex_file; |
| 1054 | for (const DexFile* dex_file : dex_files) { |
| 1055 | key_to_dex_file.emplace(GetProfileDexFileKey(dex_file->GetLocation()), dex_file); |
| 1056 | } |
| 1057 | for (const DexFileData* dex_data : info_) { |
| 1058 | const auto it = key_to_dex_file.find(dex_data->profile_key); |
| 1059 | if (it == key_to_dex_file.end()) { |
| 1060 | // It is okay if profile contains data for additional dex files. |
| 1061 | continue; |
| 1062 | } |
| 1063 | const DexFile* dex_file = it->second; |
| 1064 | const std::string& dex_location = dex_file->GetLocation(); |
| 1065 | if (!ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) { |
| 1066 | LOG(ERROR) << "Dex checksum mismatch while verifying profile " |
| 1067 | << "dex location " << dex_location << " (checksum=" |
| 1068 | << dex_file->GetLocationChecksum() << ", profile checksum=" |
| 1069 | << dex_data->checksum; |
| 1070 | return false; |
| 1071 | } |
Shubham Ajmera | 460ab79 | 2017-09-21 13:44:07 -0700 | [diff] [blame] | 1072 | |
| 1073 | if (dex_data->num_method_ids != dex_file->NumMethodIds()) { |
| 1074 | LOG(ERROR) << "Number of method ids in dex file and profile don't match." |
| 1075 | << "dex location " << dex_location << " NumMethodId in DexFile" |
| 1076 | << dex_file->NumMethodIds() << ", NumMethodId in profile" |
| 1077 | << dex_data->num_method_ids; |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 1081 | // Verify method_encoding. |
| 1082 | for (const auto& method_it : dex_data->method_map) { |
| 1083 | size_t method_id = (size_t)(method_it.first); |
| 1084 | if (method_id >= dex_file->NumMethodIds()) { |
| 1085 | LOG(ERROR) << "Invalid method id in profile file. dex location=" |
| 1086 | << dex_location << " method_id=" << method_id << " NumMethodIds=" |
| 1087 | << dex_file->NumMethodIds(); |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | // Verify class indices of inline caches. |
| 1092 | const InlineCacheMap &inline_cache_map = method_it.second; |
| 1093 | for (const auto& inline_cache_it : inline_cache_map) { |
| 1094 | const DexPcData dex_pc_data = inline_cache_it.second; |
| 1095 | if (dex_pc_data.is_missing_types || dex_pc_data.is_megamorphic) { |
| 1096 | // No class indices to verify. |
| 1097 | continue; |
| 1098 | } |
| 1099 | |
| 1100 | const ClassSet &classes = dex_pc_data.classes; |
| 1101 | SafeMap<uint8_t, std::vector<dex::TypeIndex>> dex_to_classes_map; |
| 1102 | // Group the classes by dex. We expect that most of the classes will come from |
| 1103 | // the same dex, so this will be more efficient than encoding the dex index |
| 1104 | // for each class reference. |
| 1105 | GroupClassesByDex(classes, &dex_to_classes_map); |
| 1106 | for (const auto &dex_it : dex_to_classes_map) { |
| 1107 | uint8_t dex_profile_index = dex_it.first; |
| 1108 | const auto dex_file_inline_cache_it = key_to_dex_file.find( |
| 1109 | info_[dex_profile_index]->profile_key); |
| 1110 | if (dex_file_inline_cache_it == key_to_dex_file.end()) { |
| 1111 | // It is okay if profile contains data for additional dex files. |
| 1112 | continue; |
| 1113 | } |
| 1114 | const DexFile *dex_file_for_inline_cache_check = dex_file_inline_cache_it->second; |
| 1115 | const std::vector<dex::TypeIndex> &dex_classes = dex_it.second; |
| 1116 | for (size_t i = 0; i < dex_classes.size(); i++) { |
| 1117 | if (dex_classes[i].index_ >= dex_file_for_inline_cache_check->NumTypeIds()) { |
| 1118 | LOG(ERROR) << "Invalid inline cache in profile file. dex location=" |
| 1119 | << dex_location << " method_id=" << method_id |
| 1120 | << " dex_profile_index=" |
| 1121 | << static_cast<uint16_t >(dex_profile_index) << " type_index=" |
| 1122 | << dex_classes[i].index_ |
| 1123 | << " NumTypeIds=" |
| 1124 | << dex_file_for_inline_cache_check->NumTypeIds(); |
| 1125 | return false; |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | // Verify class_ids. |
| 1132 | for (const auto& class_id : dex_data->class_set) { |
| 1133 | if (class_id.index_ >= dex_file->NumTypeIds()) { |
| 1134 | LOG(ERROR) << "Invalid class id in profile file. dex_file location " |
| 1135 | << dex_location << " class_id=" << class_id.index_ << " NumClassIds=" |
| 1136 | << dex_file->NumClassDefs(); |
| 1137 | return false; |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 1144 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::OpenSource( |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1145 | int32_t fd, |
| 1146 | /*out*/ std::unique_ptr<ProfileSource>* source, |
| 1147 | /*out*/ std::string* error) { |
| 1148 | if (IsProfileFile(fd)) { |
| 1149 | source->reset(ProfileSource::Create(fd)); |
| 1150 | return kProfileLoadSuccess; |
| 1151 | } else { |
| 1152 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, "profile", error)); |
| 1153 | if (zip_archive.get() == nullptr) { |
| 1154 | *error = "Could not open the profile zip archive"; |
| 1155 | return kProfileLoadBadData; |
| 1156 | } |
| 1157 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(kDexMetadataProfileEntry, error)); |
| 1158 | if (zip_entry == nullptr) { |
| 1159 | // Allow archives without the profile entry. In this case, create an empty profile. |
| 1160 | // This gives more flexible when ure-using archives that may miss the entry. |
| 1161 | // (e.g. dex metadata files) |
| 1162 | LOG(WARNING) << std::string("Could not find entry ") + kDexMetadataProfileEntry + |
| 1163 | " in the zip archive. Creating an empty profile."; |
| 1164 | source->reset(ProfileSource::Create(nullptr)); |
| 1165 | return kProfileLoadSuccess; |
| 1166 | } |
| 1167 | if (zip_entry->GetUncompressedLength() == 0) { |
| 1168 | *error = "Empty profile entry in the zip archive."; |
| 1169 | return kProfileLoadBadData; |
| 1170 | } |
| 1171 | |
| 1172 | std::unique_ptr<MemMap> map; |
| 1173 | if (zip_entry->IsUncompressed()) { |
| 1174 | // Map uncompressed files within zip as file-backed to avoid a dirty copy. |
| 1175 | map.reset(zip_entry->MapDirectlyFromFile(kDexMetadataProfileEntry, error)); |
| 1176 | if (map == nullptr) { |
| 1177 | LOG(WARNING) << "Can't mmap profile directly; " |
| 1178 | << "is your ZIP file corrupted? Falling back to extraction."; |
| 1179 | // Try again with Extraction which still has a chance of recovery. |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | if (map == nullptr) { |
| 1184 | // Default path for compressed ZIP entries, and fallback for stored ZIP entries. |
| 1185 | // TODO(calin) pass along file names to assist with debugging. |
| 1186 | map.reset(zip_entry->ExtractToMemMap("profile file", kDexMetadataProfileEntry, error)); |
| 1187 | } |
| 1188 | |
| 1189 | if (map != nullptr) { |
| 1190 | source->reset(ProfileSource::Create(std::move(map))); |
| 1191 | return kProfileLoadSuccess; |
| 1192 | } else { |
| 1193 | return kProfileLoadBadData; |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 1198 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ProfileSource::Read( |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1199 | uint8_t* buffer, |
| 1200 | size_t byte_count, |
| 1201 | const std::string& debug_stage, |
| 1202 | std::string* error) { |
| 1203 | if (IsMemMap()) { |
| 1204 | if (mem_map_cur_ + byte_count > mem_map_->Size()) { |
| 1205 | return kProfileLoadBadData; |
| 1206 | } |
| 1207 | for (size_t i = 0; i < byte_count; i++) { |
| 1208 | buffer[i] = *(mem_map_->Begin() + mem_map_cur_); |
| 1209 | mem_map_cur_++; |
| 1210 | } |
| 1211 | } else { |
| 1212 | while (byte_count > 0) { |
| 1213 | int bytes_read = TEMP_FAILURE_RETRY(read(fd_, buffer, byte_count));; |
| 1214 | if (bytes_read == 0) { |
| 1215 | *error += "Profile EOF reached prematurely for " + debug_stage; |
| 1216 | return kProfileLoadBadData; |
| 1217 | } else if (bytes_read < 0) { |
| 1218 | *error += "Profile IO error for " + debug_stage + strerror(errno); |
| 1219 | return kProfileLoadIOError; |
| 1220 | } |
| 1221 | byte_count -= bytes_read; |
| 1222 | buffer += bytes_read; |
| 1223 | } |
| 1224 | } |
| 1225 | return kProfileLoadSuccess; |
| 1226 | } |
| 1227 | |
| 1228 | bool ProfileCompilationInfo::ProfileSource::HasConsumedAllData() const { |
| 1229 | return IsMemMap() |
| 1230 | ? (mem_map_ == nullptr || mem_map_cur_ == mem_map_->Size()) |
| 1231 | : (testEOF(fd_) == 0); |
| 1232 | } |
| 1233 | |
| 1234 | bool ProfileCompilationInfo::ProfileSource::HasEmptyContent() const { |
| 1235 | if (IsMemMap()) { |
| 1236 | return mem_map_ == nullptr || mem_map_->Size() == 0; |
| 1237 | } else { |
| 1238 | struct stat stat_buffer; |
| 1239 | if (fstat(fd_, &stat_buffer) != 0) { |
| 1240 | return false; |
| 1241 | } |
| 1242 | return stat_buffer.st_size == 0; |
| 1243 | } |
| 1244 | } |
| 1245 | |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 1246 | // TODO(calin): fail fast if the dex checksums don't match. |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 1247 | ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::LoadInternal( |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1248 | int32_t fd, std::string* error, bool merge_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1249 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1250 | DCHECK_GE(fd, 0); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1251 | |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1252 | std::unique_ptr<ProfileSource> source; |
Calin Juravle | 36060d1 | 2018-01-19 16:28:38 -0800 | [diff] [blame^] | 1253 | ProfileLoadStatus status = OpenSource(fd, &source, error); |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1254 | if (status != kProfileLoadSuccess) { |
| 1255 | return status; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1256 | } |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1257 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1258 | // We allow empty profile files. |
| 1259 | // Profiles may be created by ActivityManager or installd before we manage to |
| 1260 | // process them in the runtime or profman. |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1261 | if (source->HasEmptyContent()) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1262 | return kProfileLoadSuccess; |
| 1263 | } |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1264 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1265 | // Read profile header: magic + version + number_of_dex_files. |
| 1266 | uint8_t number_of_dex_files; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1267 | uint32_t uncompressed_data_size; |
| 1268 | uint32_t compressed_data_size; |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1269 | status = ReadProfileHeader(*source, |
| 1270 | &number_of_dex_files, |
| 1271 | &uncompressed_data_size, |
| 1272 | &compressed_data_size, |
| 1273 | error); |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1274 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1275 | if (status != kProfileLoadSuccess) { |
| 1276 | return status; |
| 1277 | } |
Mathieu Chartier | f2e2af8 | 2017-07-12 21:09:57 -0700 | [diff] [blame] | 1278 | // Allow large profiles for non target builds for the case where we are merging many profiles |
| 1279 | // to generate a boot image profile. |
| 1280 | if (kIsTargetBuild && uncompressed_data_size > kProfileSizeErrorThresholdInBytes) { |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1281 | LOG(ERROR) << "Profile data size exceeds " |
| 1282 | << std::to_string(kProfileSizeErrorThresholdInBytes) |
| 1283 | << " bytes"; |
| 1284 | return kProfileLoadBadData; |
| 1285 | } |
| 1286 | if (uncompressed_data_size > kProfileSizeWarningThresholdInBytes) { |
| 1287 | LOG(WARNING) << "Profile data size exceeds " |
| 1288 | << std::to_string(kProfileSizeWarningThresholdInBytes) |
| 1289 | << " bytes"; |
| 1290 | } |
| 1291 | |
| 1292 | std::unique_ptr<uint8_t[]> compressed_data(new uint8_t[compressed_data_size]); |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1293 | status = source->Read(compressed_data.get(), compressed_data_size, "ReadContent", error); |
| 1294 | if (status != kProfileLoadSuccess) { |
| 1295 | *error += "Unable to read compressed profile data"; |
| 1296 | return status; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 1299 | if (!source->HasConsumedAllData()) { |
| 1300 | *error += "Unexpected data in the profile file."; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1301 | return kProfileLoadBadData; |
| 1302 | } |
| 1303 | |
| 1304 | SafeBuffer uncompressed_data(uncompressed_data_size); |
| 1305 | |
| 1306 | int ret = InflateBuffer(compressed_data.get(), |
| 1307 | compressed_data_size, |
| 1308 | uncompressed_data_size, |
| 1309 | uncompressed_data.Get()); |
| 1310 | |
| 1311 | if (ret != Z_STREAM_END) { |
| 1312 | *error += "Error reading uncompressed profile data"; |
| 1313 | return kProfileLoadBadData; |
| 1314 | } |
| 1315 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1316 | std::vector<ProfileLineHeader> profile_line_headers; |
| 1317 | // Read profile line headers. |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1318 | for (uint8_t k = 0; k < number_of_dex_files; k++) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1319 | ProfileLineHeader line_header; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1320 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1321 | // First, read the line header to get the amount of data we need to read. |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1322 | status = ReadProfileLineHeader(uncompressed_data, &line_header, error); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1323 | if (status != kProfileLoadSuccess) { |
| 1324 | return status; |
| 1325 | } |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1326 | profile_line_headers.push_back(line_header); |
| 1327 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1328 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1329 | SafeMap<uint8_t, uint8_t> dex_profile_index_remap; |
| 1330 | if (!RemapProfileIndex(profile_line_headers, &dex_profile_index_remap)) { |
| 1331 | return kProfileLoadBadData; |
| 1332 | } |
| 1333 | |
| 1334 | for (uint8_t k = 0; k < number_of_dex_files; k++) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1335 | // Now read the actual profile line. |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1336 | status = ReadProfileLine(uncompressed_data, |
| 1337 | number_of_dex_files, |
| 1338 | profile_line_headers[k], |
| 1339 | dex_profile_index_remap, |
| 1340 | merge_classes, |
| 1341 | error); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1342 | if (status != kProfileLoadSuccess) { |
| 1343 | return status; |
| 1344 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | // Check that we read everything and that profiles don't contain junk data. |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1348 | if (uncompressed_data.CountUnreadBytes() > 0) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1349 | *error = "Unexpected content in the profile file"; |
| 1350 | return kProfileLoadBadData; |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1351 | } else { |
| 1352 | return kProfileLoadSuccess; |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 1353 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Shubham Ajmera | afbbf18 | 2017-08-04 14:33:34 -0700 | [diff] [blame] | 1356 | bool ProfileCompilationInfo::RemapProfileIndex( |
| 1357 | const std::vector<ProfileLineHeader>& profile_line_headers, |
| 1358 | /*out*/SafeMap<uint8_t, uint8_t>* dex_profile_index_remap) { |
| 1359 | // First verify that all checksums match. This will avoid adding garbage to |
| 1360 | // the current profile info. |
| 1361 | // Note that the number of elements should be very small, so this should not |
| 1362 | // be a performance issue. |
| 1363 | for (const ProfileLineHeader other_profile_line_header : profile_line_headers) { |
| 1364 | // verify_checksum is false because we want to differentiate between a missing dex data and |
| 1365 | // a mismatched checksum. |
| 1366 | const DexFileData* dex_data = FindDexData(other_profile_line_header.dex_location, |
| 1367 | 0u, |
| 1368 | false /* verify_checksum */); |
| 1369 | if ((dex_data != nullptr) && (dex_data->checksum != other_profile_line_header.checksum)) { |
| 1370 | LOG(WARNING) << "Checksum mismatch for dex " << other_profile_line_header.dex_location; |
| 1371 | return false; |
| 1372 | } |
| 1373 | } |
| 1374 | // All checksums match. Import the data. |
| 1375 | uint32_t num_dex_files = static_cast<uint32_t>(profile_line_headers.size()); |
| 1376 | for (uint32_t i = 0; i < num_dex_files; i++) { |
| 1377 | const DexFileData* dex_data = GetOrAddDexFileData(profile_line_headers[i].dex_location, |
| 1378 | profile_line_headers[i].checksum, |
| 1379 | profile_line_headers[i].num_method_ids); |
| 1380 | if (dex_data == nullptr) { |
| 1381 | return false; // Could happen if we exceed the number of allowed dex files. |
| 1382 | } |
| 1383 | dex_profile_index_remap->Put(i, dex_data->profile_index); |
| 1384 | } |
| 1385 | return true; |
| 1386 | } |
Shubham Ajmera | 61200a0 | 2017-08-30 16:29:41 -0700 | [diff] [blame] | 1387 | |
Shubham Ajmera | 4d198e0 | 2017-05-12 17:45:29 +0000 | [diff] [blame] | 1388 | std::unique_ptr<uint8_t[]> ProfileCompilationInfo::DeflateBuffer(const uint8_t* in_buffer, |
| 1389 | uint32_t in_size, |
| 1390 | uint32_t* compressed_data_size) { |
| 1391 | z_stream strm; |
| 1392 | strm.zalloc = Z_NULL; |
| 1393 | strm.zfree = Z_NULL; |
| 1394 | strm.opaque = Z_NULL; |
| 1395 | int ret = deflateInit(&strm, 1); |
| 1396 | if (ret != Z_OK) { |
| 1397 | return nullptr; |
| 1398 | } |
| 1399 | |
| 1400 | uint32_t out_size = deflateBound(&strm, in_size); |
| 1401 | |
| 1402 | std::unique_ptr<uint8_t[]> compressed_buffer(new uint8_t[out_size]); |
| 1403 | strm.avail_in = in_size; |
| 1404 | strm.next_in = const_cast<uint8_t*>(in_buffer); |
| 1405 | strm.avail_out = out_size; |
| 1406 | strm.next_out = &compressed_buffer[0]; |
| 1407 | ret = deflate(&strm, Z_FINISH); |
| 1408 | if (ret == Z_STREAM_ERROR) { |
| 1409 | return nullptr; |
| 1410 | } |
| 1411 | *compressed_data_size = out_size - strm.avail_out; |
| 1412 | deflateEnd(&strm); |
| 1413 | return compressed_buffer; |
| 1414 | } |
| 1415 | |
| 1416 | int ProfileCompilationInfo::InflateBuffer(const uint8_t* in_buffer, |
| 1417 | uint32_t in_size, |
| 1418 | uint32_t expected_uncompressed_data_size, |
| 1419 | uint8_t* out_buffer) { |
| 1420 | z_stream strm; |
| 1421 | |
| 1422 | /* allocate inflate state */ |
| 1423 | strm.zalloc = Z_NULL; |
| 1424 | strm.zfree = Z_NULL; |
| 1425 | strm.opaque = Z_NULL; |
| 1426 | strm.avail_in = in_size; |
| 1427 | strm.next_in = const_cast<uint8_t*>(in_buffer); |
| 1428 | strm.avail_out = expected_uncompressed_data_size; |
| 1429 | strm.next_out = out_buffer; |
| 1430 | |
| 1431 | int ret; |
| 1432 | inflateInit(&strm); |
| 1433 | ret = inflate(&strm, Z_NO_FLUSH); |
| 1434 | |
| 1435 | if (strm.avail_in != 0 || strm.avail_out != 0) { |
| 1436 | return Z_DATA_ERROR; |
| 1437 | } |
| 1438 | inflateEnd(&strm); |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 1442 | bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other, |
| 1443 | bool merge_classes) { |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 1444 | // First verify that all checksums match. This will avoid adding garbage to |
| 1445 | // the current profile info. |
| 1446 | // Note that the number of elements should be very small, so this should not |
| 1447 | // be a performance issue. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1448 | for (const DexFileData* other_dex_data : other.info_) { |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1449 | // verify_checksum is false because we want to differentiate between a missing dex data and |
| 1450 | // a mismatched checksum. |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1451 | const DexFileData* dex_data = FindDexData(other_dex_data->profile_key, |
| 1452 | 0u, |
| 1453 | /* verify_checksum */ false); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1454 | if ((dex_data != nullptr) && (dex_data->checksum != other_dex_data->checksum)) { |
| 1455 | LOG(WARNING) << "Checksum mismatch for dex " << other_dex_data->profile_key; |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
| 1458 | } |
| 1459 | // All checksums match. Import the data. |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1460 | |
| 1461 | // The other profile might have a different indexing of dex files. |
| 1462 | // That is because each dex files gets a 'dex_profile_index' on a first come first served basis. |
| 1463 | // That means that the order in with the methods are added to the profile matters for the |
| 1464 | // actual indices. |
| 1465 | // The reason we cannot rely on the actual multidex index is that a single profile may store |
| 1466 | // data from multiple splits. This means that a profile may contain a classes2.dex from split-A |
| 1467 | // and one from split-B. |
| 1468 | |
| 1469 | // First, build a mapping from other_dex_profile_index to this_dex_profile_index. |
| 1470 | // This will make sure that the ClassReferences will point to the correct dex file. |
| 1471 | SafeMap<uint8_t, uint8_t> dex_profile_index_remap; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1472 | for (const DexFileData* other_dex_data : other.info_) { |
| 1473 | const DexFileData* dex_data = GetOrAddDexFileData(other_dex_data->profile_key, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1474 | other_dex_data->checksum, |
| 1475 | other_dex_data->num_method_ids); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 1476 | if (dex_data == nullptr) { |
| 1477 | return false; // Could happen if we exceed the number of allowed dex files. |
| 1478 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1479 | dex_profile_index_remap.Put(other_dex_data->profile_index, dex_data->profile_index); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | // Merge the actual profile data. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1483 | for (const DexFileData* other_dex_data : other.info_) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1484 | DexFileData* dex_data = const_cast<DexFileData*>(FindDexData(other_dex_data->profile_key, |
| 1485 | other_dex_data->checksum)); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1486 | DCHECK(dex_data != nullptr); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1487 | |
| 1488 | // Merge the classes. |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 1489 | if (merge_classes) { |
| 1490 | dex_data->class_set.insert(other_dex_data->class_set.begin(), |
| 1491 | other_dex_data->class_set.end()); |
| 1492 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1493 | |
| 1494 | // Merge the methods and the inline caches. |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1495 | for (const auto& other_method_it : other_dex_data->method_map) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1496 | uint16_t other_method_index = other_method_it.first; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1497 | InlineCacheMap* inline_cache = dex_data->FindOrAddMethod(other_method_index); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1498 | const auto& other_inline_cache = other_method_it.second; |
| 1499 | for (const auto& other_ic_it : other_inline_cache) { |
| 1500 | uint16_t other_dex_pc = other_ic_it.first; |
| 1501 | const ClassSet& other_class_set = other_ic_it.second.classes; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1502 | DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, other_dex_pc); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1503 | if (other_ic_it.second.is_missing_types) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1504 | dex_pc_data->SetIsMissingTypes(); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1505 | } else if (other_ic_it.second.is_megamorphic) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1506 | dex_pc_data->SetIsMegamorphic(); |
Calin Juravle | 0def68d | 2017-02-21 19:00:33 -0800 | [diff] [blame] | 1507 | } else { |
| 1508 | for (const auto& class_it : other_class_set) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1509 | dex_pc_data->AddClass(dex_profile_index_remap.Get( |
Calin Juravle | 0def68d | 2017-02-21 19:00:33 -0800 | [diff] [blame] | 1510 | class_it.dex_profile_index), class_it.type_index); |
| 1511 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1515 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1516 | // Merge the method bitmaps. |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1517 | dex_data->MergeBitmap(*other_dex_data); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1518 | } |
| 1519 | return true; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 1522 | const ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::FindDexData( |
| 1523 | const DexFile* dex_file) const { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1524 | return FindDexData(GetProfileDexFileKey(dex_file->GetLocation()), |
| 1525 | dex_file->GetLocationChecksum()); |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1528 | ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::GetMethodHotness( |
| 1529 | const MethodReference& method_ref) const { |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 1530 | const DexFileData* dex_data = FindDexData(method_ref.dex_file); |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1531 | return dex_data != nullptr |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 1532 | ? dex_data->GetHotnessInfo(method_ref.index) |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1533 | : MethodHotness(); |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 1536 | bool ProfileCompilationInfo::AddMethodHotness(const MethodReference& method_ref, |
| 1537 | const MethodHotness& hotness) { |
| 1538 | DexFileData* dex_data = GetOrAddDexFileData(method_ref.dex_file); |
| 1539 | if (dex_data != nullptr) { |
| 1540 | // TODO: Add inline caches. |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 1541 | return dex_data->AddMethod( |
| 1542 | static_cast<MethodHotness::Flag>(hotness.GetFlags()), method_ref.index); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 1543 | } |
| 1544 | return false; |
| 1545 | } |
| 1546 | |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1547 | ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::GetMethodHotness( |
| 1548 | const std::string& dex_location, |
| 1549 | uint32_t dex_checksum, |
| 1550 | uint16_t dex_method_index) const { |
| 1551 | const DexFileData* dex_data = FindDexData(GetProfileDexFileKey(dex_location), dex_checksum); |
| 1552 | return dex_data != nullptr ? dex_data->GetHotnessInfo(dex_method_index) : MethodHotness(); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1555 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1556 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> ProfileCompilationInfo::GetMethod( |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1557 | const std::string& dex_location, |
| 1558 | uint32_t dex_checksum, |
| 1559 | uint16_t dex_method_index) const { |
| 1560 | MethodHotness hotness(GetMethodHotness(dex_location, dex_checksum, dex_method_index)); |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1561 | if (!hotness.IsHot()) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1562 | return nullptr; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1563 | } |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1564 | const InlineCacheMap* inline_caches = hotness.GetInlineCacheMap(); |
| 1565 | DCHECK(inline_caches != nullptr); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 1566 | std::unique_ptr<OfflineProfileMethodInfo> pmi(new OfflineProfileMethodInfo(inline_caches)); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1567 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1568 | pmi->dex_references.resize(info_.size()); |
| 1569 | for (const DexFileData* dex_data : info_) { |
| 1570 | pmi->dex_references[dex_data->profile_index].dex_location = dex_data->profile_key; |
| 1571 | pmi->dex_references[dex_data->profile_index].dex_checksum = dex_data->checksum; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1572 | pmi->dex_references[dex_data->profile_index].num_method_ids = dex_data->num_method_ids; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1575 | return pmi; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1579 | bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, dex::TypeIndex type_idx) const { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1580 | const DexFileData* dex_data = FindDexData(&dex_file); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1581 | if (dex_data != nullptr) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1582 | const ArenaSet<dex::TypeIndex>& classes = dex_data->class_set; |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 1583 | return classes.find(type_idx) != classes.end(); |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 1584 | } |
| 1585 | return false; |
| 1586 | } |
| 1587 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1588 | uint32_t ProfileCompilationInfo::GetNumberOfMethods() const { |
| 1589 | uint32_t total = 0; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1590 | for (const DexFileData* dex_data : info_) { |
| 1591 | total += dex_data->method_map.size(); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1592 | } |
| 1593 | return total; |
| 1594 | } |
| 1595 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 1596 | uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const { |
| 1597 | uint32_t total = 0; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1598 | for (const DexFileData* dex_data : info_) { |
| 1599 | total += dex_data->class_set.size(); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 1600 | } |
| 1601 | return total; |
| 1602 | } |
| 1603 | |
David Sehr | b18991b | 2017-02-08 20:58:10 -0800 | [diff] [blame] | 1604 | // Produce a non-owning vector from a vector. |
| 1605 | template<typename T> |
| 1606 | const std::vector<T*>* MakeNonOwningVector(const std::vector<std::unique_ptr<T>>* owning_vector) { |
| 1607 | auto non_owning_vector = new std::vector<T*>(); |
| 1608 | for (auto& element : *owning_vector) { |
| 1609 | non_owning_vector->push_back(element.get()); |
| 1610 | } |
| 1611 | return non_owning_vector; |
| 1612 | } |
| 1613 | |
| 1614 | std::string ProfileCompilationInfo::DumpInfo( |
| 1615 | const std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 1616 | bool print_full_dex_location) const { |
| 1617 | std::unique_ptr<const std::vector<const DexFile*>> non_owning_dex_files( |
| 1618 | MakeNonOwningVector(dex_files)); |
| 1619 | return DumpInfo(non_owning_dex_files.get(), print_full_dex_location); |
| 1620 | } |
| 1621 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1622 | std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files, |
| 1623 | bool print_full_dex_location) const { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1624 | std::ostringstream os; |
| 1625 | if (info_.empty()) { |
| 1626 | return "ProfileInfo: empty"; |
| 1627 | } |
| 1628 | |
| 1629 | os << "ProfileInfo:"; |
| 1630 | |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 1631 | const std::string kFirstDexFileKeySubstitute = "!classes.dex"; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1632 | |
| 1633 | for (const DexFileData* dex_data : info_) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1634 | os << "\n"; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1635 | if (print_full_dex_location) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1636 | os << dex_data->profile_key; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1637 | } else { |
| 1638 | // Replace the (empty) multidex suffix of the first key with a substitute for easier reading. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 1639 | std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(dex_data->profile_key); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1640 | os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix); |
| 1641 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1642 | os << " [index=" << static_cast<uint32_t>(dex_data->profile_index) << "]"; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1643 | const DexFile* dex_file = nullptr; |
| 1644 | if (dex_files != nullptr) { |
| 1645 | for (size_t i = 0; i < dex_files->size(); i++) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1646 | if (dex_data->profile_key == (*dex_files)[i]->GetLocation()) { |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1647 | dex_file = (*dex_files)[i]; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1648 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1649 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1650 | } |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 1651 | os << "\n\thot methods: "; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1652 | for (const auto& method_it : dex_data->method_map) { |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1653 | if (dex_file != nullptr) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1654 | os << "\n\t\t" << dex_file->PrettyMethod(method_it.first, true); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1655 | } else { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1656 | os << method_it.first; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1657 | } |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1658 | |
| 1659 | os << "["; |
| 1660 | for (const auto& inline_cache_it : method_it.second) { |
| 1661 | os << "{" << std::hex << inline_cache_it.first << std::dec << ":"; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1662 | if (inline_cache_it.second.is_missing_types) { |
| 1663 | os << "MT"; |
| 1664 | } else if (inline_cache_it.second.is_megamorphic) { |
| 1665 | os << "MM"; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1666 | } else { |
| 1667 | for (const ClassReference& class_ref : inline_cache_it.second.classes) { |
| 1668 | os << "(" << static_cast<uint32_t>(class_ref.dex_profile_index) |
| 1669 | << "," << class_ref.type_index.index_ << ")"; |
| 1670 | } |
| 1671 | } |
| 1672 | os << "}"; |
| 1673 | } |
| 1674 | os << "], "; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1675 | } |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 1676 | bool startup = true; |
| 1677 | while (true) { |
| 1678 | os << "\n\t" << (startup ? "startup methods: " : "post startup methods: "); |
| 1679 | for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1680 | MethodHotness hotness_info(dex_data->GetHotnessInfo(method_idx)); |
| 1681 | if (startup ? hotness_info.IsStartup() : hotness_info.IsPostStartup()) { |
Calin Juravle | 0545d4a | 2017-12-01 13:36:26 -0800 | [diff] [blame] | 1682 | if (dex_file != nullptr) { |
| 1683 | os << "\n\t\t" << dex_file->PrettyMethod(method_idx, true); |
| 1684 | } else { |
| 1685 | os << method_idx << ", "; |
| 1686 | } |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | if (startup == false) { |
| 1690 | break; |
| 1691 | } |
| 1692 | startup = false; |
| 1693 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1694 | os << "\n\tclasses: "; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1695 | for (const auto class_it : dex_data->class_set) { |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1696 | if (dex_file != nullptr) { |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 1697 | os << "\n\t\t" << dex_file->PrettyType(class_it); |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1698 | } else { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1699 | os << class_it.index_ << ","; |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 1700 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | return os.str(); |
| 1704 | } |
| 1705 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1706 | bool ProfileCompilationInfo::GetClassesAndMethods( |
| 1707 | const DexFile& dex_file, |
| 1708 | /*out*/std::set<dex::TypeIndex>* class_set, |
| 1709 | /*out*/std::set<uint16_t>* hot_method_set, |
| 1710 | /*out*/std::set<uint16_t>* startup_method_set, |
| 1711 | /*out*/std::set<uint16_t>* post_startup_method_method_set) const { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 1712 | std::set<std::string> ret; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1713 | const DexFileData* dex_data = FindDexData(&dex_file); |
| 1714 | if (dex_data == nullptr) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 1715 | return false; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 1716 | } |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 1717 | for (const auto& it : dex_data->method_map) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1718 | hot_method_set->insert(it.first); |
| 1719 | } |
| 1720 | for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1721 | MethodHotness hotness = dex_data->GetHotnessInfo(method_idx); |
| 1722 | if (hotness.IsStartup()) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1723 | startup_method_set->insert(method_idx); |
| 1724 | } |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1725 | if (hotness.IsPostStartup()) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1726 | post_startup_method_method_set->insert(method_idx); |
| 1727 | } |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 1728 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1729 | for (const dex::TypeIndex& type_index : dex_data->class_set) { |
| 1730 | class_set->insert(type_index); |
| 1731 | } |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 1732 | return true; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1735 | bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1736 | // No need to compare profile_key_map_. That's only a cache for fast search. |
| 1737 | // All the information is already in the info_ vector. |
| 1738 | if (info_.size() != other.info_.size()) { |
| 1739 | return false; |
| 1740 | } |
| 1741 | for (size_t i = 0; i < info_.size(); i++) { |
| 1742 | const DexFileData& dex_data = *info_[i]; |
| 1743 | const DexFileData& other_dex_data = *other.info_[i]; |
| 1744 | if (!(dex_data == other_dex_data)) { |
| 1745 | return false; |
| 1746 | } |
| 1747 | } |
| 1748 | return true; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 1751 | std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses( |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1752 | const std::vector<const DexFile*>& dex_files) const { |
| 1753 | std::unordered_map<std::string, const DexFile* > key_to_dex_file; |
| 1754 | for (const DexFile* dex_file : dex_files) { |
| 1755 | key_to_dex_file.emplace(GetProfileDexFileKey(dex_file->GetLocation()), dex_file); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 1756 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1757 | std::set<DexCacheResolvedClasses> ret; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1758 | for (const DexFileData* dex_data : info_) { |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1759 | const auto it = key_to_dex_file.find(dex_data->profile_key); |
| 1760 | if (it != key_to_dex_file.end()) { |
| 1761 | const DexFile* dex_file = it->second; |
| 1762 | const std::string& dex_location = dex_file->GetLocation(); |
| 1763 | if (dex_data->checksum != it->second->GetLocationChecksum()) { |
| 1764 | LOG(ERROR) << "Dex checksum mismatch when getting resolved classes from profile for " |
| 1765 | << "location " << dex_location << " (checksum=" << dex_file->GetLocationChecksum() |
| 1766 | << ", profile checksum=" << dex_data->checksum; |
| 1767 | return std::set<DexCacheResolvedClasses>(); |
| 1768 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1769 | DexCacheResolvedClasses classes(dex_location, |
| 1770 | dex_location, |
| 1771 | dex_data->checksum, |
| 1772 | dex_data->num_method_ids); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1773 | classes.AddClasses(dex_data->class_set.begin(), dex_data->class_set.end()); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 1774 | ret.insert(classes); |
| 1775 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1776 | } |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1780 | // Naive implementation to generate a random profile file suitable for testing. |
| 1781 | bool ProfileCompilationInfo::GenerateTestProfile(int fd, |
| 1782 | uint16_t number_of_dex_files, |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1783 | uint16_t method_percentage, |
| 1784 | uint16_t class_percentage, |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1785 | uint32_t random_seed) { |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1786 | const std::string base_dex_location = "base.apk"; |
| 1787 | ProfileCompilationInfo info; |
| 1788 | // The limits are defined by the dex specification. |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1789 | const uint16_t max_method = std::numeric_limits<uint16_t>::max(); |
| 1790 | const uint16_t max_classes = std::numeric_limits<uint16_t>::max(); |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1791 | uint16_t number_of_methods = max_method * method_percentage / 100; |
| 1792 | uint16_t number_of_classes = max_classes * class_percentage / 100; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1793 | |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1794 | std::srand(random_seed); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1795 | |
| 1796 | // Make sure we generate more samples with a low index value. |
| 1797 | // This makes it more likely to hit valid method/class indices in small apps. |
| 1798 | const uint16_t kFavorFirstN = 10000; |
| 1799 | const uint16_t kFavorSplit = 2; |
| 1800 | |
| 1801 | for (uint16_t i = 0; i < number_of_dex_files; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 1802 | std::string dex_location = DexFileLoader::GetMultiDexLocation(i, base_dex_location.c_str()); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1803 | std::string profile_key = GetProfileDexFileKey(dex_location); |
| 1804 | |
| 1805 | for (uint16_t m = 0; m < number_of_methods; m++) { |
| 1806 | uint16_t method_idx = rand() % max_method; |
| 1807 | if (m < (number_of_methods / kFavorSplit)) { |
| 1808 | method_idx %= kFavorFirstN; |
| 1809 | } |
Mathieu Chartier | c46cf80 | 2017-09-28 11:52:19 -0700 | [diff] [blame] | 1810 | // Alternate between startup and post startup. |
| 1811 | uint32_t flags = MethodHotness::kFlagHot; |
| 1812 | flags |= ((m & 1) != 0) ? MethodHotness::kFlagPostStartup : MethodHotness::kFlagStartup; |
| 1813 | info.AddMethodIndex(static_cast<MethodHotness::Flag>(flags), |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 1814 | profile_key, |
| 1815 | /*method_idx*/ 0, |
| 1816 | method_idx, |
| 1817 | max_method); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | for (uint16_t c = 0; c < number_of_classes; c++) { |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 1821 | uint16_t type_idx = rand() % max_classes; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1822 | if (c < (number_of_classes / kFavorSplit)) { |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 1823 | type_idx %= kFavorFirstN; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1824 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1825 | info.AddClassIndex(profile_key, 0, dex::TypeIndex(type_idx), max_method); |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | return info.Save(fd); |
| 1829 | } |
| 1830 | |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1831 | // Naive implementation to generate a random profile file suitable for testing. |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1832 | // Description of random selection: |
| 1833 | // * Select a random starting point S. |
| 1834 | // * For every index i, add (S+i) % (N - total number of methods/classes) to profile with the |
| 1835 | // probably of 1/(N - i - number of methods/classes needed to add in profile). |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1836 | bool ProfileCompilationInfo::GenerateTestProfile( |
| 1837 | int fd, |
| 1838 | std::vector<std::unique_ptr<const DexFile>>& dex_files, |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1839 | uint16_t method_percentage, |
| 1840 | uint16_t class_percentage, |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1841 | uint32_t random_seed) { |
| 1842 | std::srand(random_seed); |
| 1843 | ProfileCompilationInfo info; |
| 1844 | for (std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 1845 | const std::string& location = dex_file->GetLocation(); |
| 1846 | uint32_t checksum = dex_file->GetLocationChecksum(); |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1847 | |
| 1848 | uint32_t number_of_classes = dex_file->NumClassDefs(); |
| 1849 | uint32_t classes_required_in_profile = (number_of_classes * class_percentage) / 100; |
| 1850 | uint32_t class_start_index = rand() % number_of_classes; |
| 1851 | for (uint32_t i = 0; i < number_of_classes && classes_required_in_profile; ++i) { |
| 1852 | if (number_of_classes - i == classes_required_in_profile || |
| 1853 | std::rand() % (number_of_classes - i - classes_required_in_profile) == 0) { |
| 1854 | uint32_t class_index = (i + class_start_index) % number_of_classes; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1855 | info.AddClassIndex(location, |
| 1856 | checksum, |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1857 | dex_file->GetClassDef(class_index).class_idx_, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1858 | dex_file->NumMethodIds()); |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1859 | classes_required_in_profile--; |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1860 | } |
| 1861 | } |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1862 | |
| 1863 | uint32_t number_of_methods = dex_file->NumMethodIds(); |
| 1864 | uint32_t methods_required_in_profile = (number_of_methods * method_percentage) / 100; |
| 1865 | uint32_t method_start_index = rand() % number_of_methods; |
| 1866 | for (uint32_t i = 0; i < number_of_methods && methods_required_in_profile; ++i) { |
| 1867 | if (number_of_methods - i == methods_required_in_profile || |
| 1868 | std::rand() % (number_of_methods - i - methods_required_in_profile) == 0) { |
| 1869 | uint32_t method_index = (method_start_index + i) % number_of_methods; |
Mathieu Chartier | c46cf80 | 2017-09-28 11:52:19 -0700 | [diff] [blame] | 1870 | // Alternate between startup and post startup. |
| 1871 | uint32_t flags = MethodHotness::kFlagHot; |
| 1872 | flags |= ((method_index & 1) != 0) |
| 1873 | ? MethodHotness::kFlagPostStartup |
| 1874 | : MethodHotness::kFlagStartup; |
| 1875 | info.AddMethodIndex(static_cast<MethodHotness::Flag>(flags), |
| 1876 | MethodReference(dex_file.get(), method_index)); |
Shubham Ajmera | d704f0b | 2017-07-26 16:33:35 -0700 | [diff] [blame] | 1877 | methods_required_in_profile--; |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | return info.Save(fd); |
| 1882 | } |
| 1883 | |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1884 | bool ProfileCompilationInfo::OfflineProfileMethodInfo::operator==( |
| 1885 | const OfflineProfileMethodInfo& other) const { |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 1886 | if (inline_caches->size() != other.inline_caches->size()) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1887 | return false; |
| 1888 | } |
| 1889 | |
| 1890 | // We can't use a simple equality test because we need to match the dex files |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1891 | // of the inline caches which might have different profile indexes. |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 1892 | for (const auto& inline_cache_it : *inline_caches) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1893 | uint16_t dex_pc = inline_cache_it.first; |
| 1894 | const DexPcData dex_pc_data = inline_cache_it.second; |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 1895 | const auto& other_it = other.inline_caches->find(dex_pc); |
| 1896 | if (other_it == other.inline_caches->end()) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1897 | return false; |
| 1898 | } |
| 1899 | const DexPcData& other_dex_pc_data = other_it->second; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 1900 | if (dex_pc_data.is_megamorphic != other_dex_pc_data.is_megamorphic || |
| 1901 | dex_pc_data.is_missing_types != other_dex_pc_data.is_missing_types) { |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 1902 | return false; |
| 1903 | } |
| 1904 | for (const ClassReference& class_ref : dex_pc_data.classes) { |
| 1905 | bool found = false; |
| 1906 | for (const ClassReference& other_class_ref : other_dex_pc_data.classes) { |
| 1907 | CHECK_LE(class_ref.dex_profile_index, dex_references.size()); |
| 1908 | CHECK_LE(other_class_ref.dex_profile_index, other.dex_references.size()); |
| 1909 | const DexReference& dex_ref = dex_references[class_ref.dex_profile_index]; |
| 1910 | const DexReference& other_dex_ref = other.dex_references[other_class_ref.dex_profile_index]; |
| 1911 | if (class_ref.type_index == other_class_ref.type_index && |
| 1912 | dex_ref == other_dex_ref) { |
| 1913 | found = true; |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | if (!found) { |
| 1918 | return false; |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 1925 | bool ProfileCompilationInfo::IsEmpty() const { |
| 1926 | DCHECK_EQ(info_.empty(), profile_key_map_.empty()); |
| 1927 | return info_.empty(); |
| 1928 | } |
| 1929 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1930 | ProfileCompilationInfo::InlineCacheMap* |
| 1931 | ProfileCompilationInfo::DexFileData::FindOrAddMethod(uint16_t method_index) { |
| 1932 | return &(method_map.FindOrAdd( |
| 1933 | method_index, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 1934 | InlineCacheMap(std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile)))->second); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1937 | // Mark a method as executed at least once. |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 1938 | bool ProfileCompilationInfo::DexFileData::AddMethod(MethodHotness::Flag flags, size_t index) { |
| 1939 | if (index >= num_method_ids) { |
| 1940 | LOG(ERROR) << "Invalid method index " << index << ". num_method_ids=" << num_method_ids; |
| 1941 | return false; |
| 1942 | } |
| 1943 | |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1944 | if ((flags & MethodHotness::kFlagStartup) != 0) { |
| 1945 | method_bitmap.StoreBit(MethodBitIndex(/*startup*/ true, index), /*value*/ true); |
| 1946 | } |
| 1947 | if ((flags & MethodHotness::kFlagPostStartup) != 0) { |
| 1948 | method_bitmap.StoreBit(MethodBitIndex(/*startup*/ false, index), /*value*/ true); |
| 1949 | } |
| 1950 | if ((flags & MethodHotness::kFlagHot) != 0) { |
| 1951 | method_map.FindOrAdd( |
| 1952 | index, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 1953 | InlineCacheMap(std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile))); |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1954 | } |
Calin Juravle | 1ad1e3f | 2017-09-19 18:20:37 -0700 | [diff] [blame] | 1955 | return true; |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::DexFileData::GetHotnessInfo( |
| 1959 | uint32_t dex_method_index) const { |
| 1960 | MethodHotness ret; |
| 1961 | if (method_bitmap.LoadBit(MethodBitIndex(/*startup*/ true, dex_method_index))) { |
| 1962 | ret.AddFlag(MethodHotness::kFlagStartup); |
| 1963 | } |
| 1964 | if (method_bitmap.LoadBit(MethodBitIndex(/*startup*/ false, dex_method_index))) { |
| 1965 | ret.AddFlag(MethodHotness::kFlagPostStartup); |
| 1966 | } |
| 1967 | auto it = method_map.find(dex_method_index); |
| 1968 | if (it != method_map.end()) { |
| 1969 | ret.SetInlineCacheMap(&it->second); |
| 1970 | ret.AddFlag(MethodHotness::kFlagHot); |
| 1971 | } |
| 1972 | return ret; |
| 1973 | } |
| 1974 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1975 | ProfileCompilationInfo::DexPcData* |
| 1976 | ProfileCompilationInfo::FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1977 | return &(inline_cache->FindOrAdd(dex_pc, DexPcData(&allocator_))->second); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
Mathieu Chartier | 4f342b0 | 2017-07-21 17:12:39 -0700 | [diff] [blame] | 1980 | std::unordered_set<std::string> ProfileCompilationInfo::GetClassDescriptors( |
| 1981 | const std::vector<const DexFile*>& dex_files) { |
| 1982 | std::unordered_set<std::string> ret; |
| 1983 | for (const DexFile* dex_file : dex_files) { |
| 1984 | const DexFileData* data = FindDexData(dex_file); |
| 1985 | if (data != nullptr) { |
| 1986 | for (dex::TypeIndex type_idx : data->class_set) { |
| 1987 | if (!dex_file->IsTypeIndexValid(type_idx)) { |
| 1988 | // Something went bad. The profile is probably corrupted. Abort and return an emtpy set. |
| 1989 | LOG(WARNING) << "Corrupted profile: invalid type index " |
| 1990 | << type_idx.index_ << " in dex " << dex_file->GetLocation(); |
| 1991 | return std::unordered_set<std::string>(); |
| 1992 | } |
| 1993 | const DexFile::TypeId& type_id = dex_file->GetTypeId(type_idx); |
| 1994 | ret.insert(dex_file->GetTypeDescriptor(type_id)); |
| 1995 | } |
| 1996 | } else { |
| 1997 | VLOG(compiler) << "Failed to find profile data for " << dex_file->GetLocation(); |
| 1998 | } |
| 1999 | } |
| 2000 | return ret; |
| 2001 | } |
| 2002 | |
Calin Juravle | 1e2de64 | 2018-01-18 01:08:23 -0800 | [diff] [blame] | 2003 | bool ProfileCompilationInfo::IsProfileFile(int fd) { |
| 2004 | // First check if it's an empty file as we allow empty profile files. |
| 2005 | // Profiles may be created by ActivityManager or installd before we manage to |
| 2006 | // process them in the runtime or profman. |
| 2007 | struct stat stat_buffer; |
| 2008 | if (fstat(fd, &stat_buffer) != 0) { |
| 2009 | return false; |
| 2010 | } |
| 2011 | |
| 2012 | if (stat_buffer.st_size == 0) { |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
| 2016 | // The files is not empty. Check if it contains the profile magic. |
| 2017 | size_t byte_count = sizeof(kProfileMagic); |
| 2018 | uint8_t buffer[sizeof(kProfileMagic)]; |
| 2019 | if (!android::base::ReadFully(fd, buffer, byte_count)) { |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
| 2023 | // Reset the offset to prepare the file for reading. |
| 2024 | off_t rc = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)); |
| 2025 | if (rc == static_cast<off_t>(-1)) { |
| 2026 | PLOG(ERROR) << "Failed to reset the offset"; |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
| 2030 | return memcmp(buffer, kProfileMagic, byte_count) == 0; |
| 2031 | } |
| 2032 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 2033 | } // namespace art |