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 | |
| 17 | #include "offline_profiling_info.h" |
| 18 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 19 | #include "errno.h" |
| 20 | #include <limits.h> |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 21 | #include <vector> |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 22 | #include <sys/file.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/uio.h> |
| 25 | |
| 26 | #include "art_method-inl.h" |
| 27 | #include "base/mutex.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 28 | #include "base/scoped_flock.h" |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 30 | #include "base/systrace.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 31 | #include "base/unix_file/fd_file.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 32 | #include "jit/profiling_info.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 33 | #include "os.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 34 | #include "safe_map.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 38 | const uint8_t ProfileCompilationInfo::kProfileMagic[] = { 'p', 'r', 'o', '\0' }; |
| 39 | const uint8_t ProfileCompilationInfo::kProfileVersion[] = { '0', '0', '1', '\0' }; |
| 40 | |
| 41 | static constexpr uint16_t kMaxDexFileKeyLength = PATH_MAX; |
| 42 | |
Calin Juravle | c458857 | 2016-06-08 14:24:13 +0100 | [diff] [blame^] | 43 | // Debug flag to ignore checksums when testing if a method or a class is present in the profile. |
| 44 | // Use to make facilitate testing profile guided compilation across a large number of apps |
| 45 | // using the same test profile. |
| 46 | static constexpr bool kDebugIgnoreChecksum = false; |
| 47 | |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 48 | // Transform the actual dex location into relative paths. |
| 49 | // Note: this is OK because we don't store profiles of different apps into the same file. |
| 50 | // Apps with split apks don't cause trouble because each split has a different name and will not |
| 51 | // collide with other entries. |
Calin Juravle | 31708b7 | 2016-02-05 19:44:05 +0000 | [diff] [blame] | 52 | std::string ProfileCompilationInfo::GetProfileDexFileKey(const std::string& dex_location) { |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 53 | DCHECK(!dex_location.empty()); |
| 54 | size_t last_sep_index = dex_location.find_last_of('/'); |
| 55 | if (last_sep_index == std::string::npos) { |
| 56 | return dex_location; |
| 57 | } else { |
Calin Juravle | 31708b7 | 2016-02-05 19:44:05 +0000 | [diff] [blame] | 58 | DCHECK(last_sep_index < dex_location.size()); |
| 59 | return dex_location.substr(last_sep_index + 1); |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 63 | bool ProfileCompilationInfo::AddMethodsAndClasses( |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 64 | const std::vector<MethodReference>& methods, |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 65 | const std::set<DexCacheResolvedClasses>& resolved_classes) { |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 66 | for (const MethodReference& method : methods) { |
| 67 | if (!AddMethodIndex(GetProfileDexFileKey(method.dex_file->GetLocation()), |
| 68 | method.dex_file->GetLocationChecksum(), |
| 69 | method.dex_method_index)) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | for (const DexCacheResolvedClasses& dex_cache : resolved_classes) { |
| 74 | if (!AddResolvedClasses(dex_cache)) { |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 81 | bool ProfileCompilationInfo::MergeAndSave(const std::string& filename, |
| 82 | uint64_t* bytes_written, |
| 83 | bool force) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 84 | ScopedTrace trace(__PRETTY_FUNCTION__); |
| 85 | ScopedFlock flock; |
| 86 | std::string error; |
| 87 | if (!flock.Init(filename.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC, /* block */ false, &error)) { |
| 88 | LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | int fd = flock.GetFile()->Fd(); |
| 93 | |
| 94 | // Load the file but keep a copy around to be able to infer if the content has changed. |
| 95 | ProfileCompilationInfo fileInfo; |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 96 | ProfileLoadSatus status = fileInfo.LoadInternal(fd, &error); |
| 97 | if (status == kProfileLoadSuccess) { |
| 98 | // Merge the content of file into the current object. |
| 99 | if (MergeWith(fileInfo)) { |
| 100 | // If after the merge we have the same data as what is the file there's no point |
| 101 | // in actually doing the write. The file will be exactly the same as before. |
| 102 | if (Equals(fileInfo)) { |
| 103 | if (bytes_written != nullptr) { |
| 104 | *bytes_written = 0; |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | } else { |
| 109 | LOG(WARNING) << "Could not merge previous profile data from file " << filename; |
| 110 | if (!force) { |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | } else if (force && |
| 115 | ((status == kProfileLoadVersionMismatch) || (status == kProfileLoadBadData))) { |
| 116 | // Log a warning but don't return false. We will clear the profile anyway. |
| 117 | LOG(WARNING) << "Clearing bad or obsolete profile data from file " |
| 118 | << filename << ": " << error; |
| 119 | } else { |
| 120 | LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 124 | // We need to clear the data because we don't support appending to the profiles yet. |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 125 | if (!flock.GetFile()->ClearContent()) { |
| 126 | PLOG(WARNING) << "Could not clear profile file: " << filename; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // This doesn't need locking because we are trying to lock the file for exclusive |
| 131 | // access and fail immediately if we can't. |
| 132 | bool result = Save(fd); |
| 133 | if (result) { |
| 134 | VLOG(profiler) << "Successfully saved profile info to " << filename |
| 135 | << " Size: " << GetFileSizeBytes(filename); |
| 136 | if (bytes_written != nullptr) { |
| 137 | *bytes_written = GetFileSizeBytes(filename); |
| 138 | } |
| 139 | } else { |
| 140 | VLOG(profiler) << "Failed to save profile info to " << filename; |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 145 | // Returns true if all the bytes were successfully written to the file descriptor. |
| 146 | static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) { |
| 147 | while (byte_count > 0) { |
| 148 | int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count)); |
| 149 | if (bytes_written == -1) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 152 | byte_count -= bytes_written; // Reduce the number of remaining bytes. |
| 153 | buffer += bytes_written; // Move the buffer forward. |
| 154 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 155 | return true; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 158 | // Add the string bytes to the buffer. |
| 159 | static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) { |
| 160 | buffer->insert(buffer->end(), value.begin(), value.end()); |
| 161 | } |
| 162 | |
| 163 | // Insert each byte, from low to high into the buffer. |
| 164 | template <typename T> |
| 165 | static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) { |
| 166 | for (size_t i = 0; i < sizeof(T); i++) { |
| 167 | buffer->push_back((value >> (i * kBitsPerByte)) & 0xff); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static constexpr size_t kLineHeaderSize = |
| 172 | 3 * sizeof(uint16_t) + // method_set.size + class_set.size + dex_location.size |
| 173 | sizeof(uint32_t); // checksum |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Serialization format: |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 177 | * magic,version,number_of_lines |
| 178 | * dex_location1,number_of_methods1,number_of_classes1,dex_location_checksum1, \ |
| 179 | * method_id11,method_id12...,class_id1,class_id2... |
| 180 | * dex_location2,number_of_methods2,number_of_classes2,dex_location_checksum2, \ |
| 181 | * method_id21,method_id22...,,class_id1,class_id2... |
| 182 | * ..... |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 183 | **/ |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 184 | bool ProfileCompilationInfo::Save(int fd) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 185 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 186 | DCHECK_GE(fd, 0); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 187 | |
| 188 | // Cache at most 5KB before writing. |
| 189 | static constexpr size_t kMaxSizeToKeepBeforeWriting = 5 * KB; |
| 190 | // Use a vector wrapper to avoid keeping track of offsets when we add elements. |
| 191 | std::vector<uint8_t> buffer; |
| 192 | WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic)); |
| 193 | WriteBuffer(fd, kProfileVersion, sizeof(kProfileVersion)); |
| 194 | AddUintToBuffer(&buffer, static_cast<uint16_t>(info_.size())); |
| 195 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 196 | for (const auto& it : info_) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 197 | if (buffer.size() > kMaxSizeToKeepBeforeWriting) { |
| 198 | if (!WriteBuffer(fd, buffer.data(), buffer.size())) { |
| 199 | return false; |
| 200 | } |
| 201 | buffer.clear(); |
| 202 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 203 | const std::string& dex_location = it.first; |
| 204 | const DexFileData& dex_data = it.second; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 205 | if (dex_data.method_set.empty() && dex_data.class_set.empty()) { |
| 206 | continue; |
| 207 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 208 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 209 | if (dex_location.size() >= kMaxDexFileKeyLength) { |
| 210 | LOG(WARNING) << "DexFileKey exceeds allocated limit"; |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // Make sure that the buffer has enough capacity to avoid repeated resizings |
| 215 | // while we add data. |
| 216 | size_t required_capacity = buffer.size() + |
| 217 | kLineHeaderSize + |
| 218 | dex_location.size() + |
| 219 | sizeof(uint16_t) * (dex_data.class_set.size() + dex_data.method_set.size()); |
| 220 | |
| 221 | buffer.reserve(required_capacity); |
| 222 | |
| 223 | DCHECK_LE(dex_location.size(), std::numeric_limits<uint16_t>::max()); |
| 224 | DCHECK_LE(dex_data.method_set.size(), std::numeric_limits<uint16_t>::max()); |
| 225 | DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max()); |
| 226 | AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_location.size())); |
| 227 | AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.method_set.size())); |
| 228 | AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size())); |
| 229 | AddUintToBuffer(&buffer, dex_data.checksum); // uint32_t |
| 230 | |
| 231 | AddStringToBuffer(&buffer, dex_location); |
| 232 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 233 | for (auto method_it : dex_data.method_set) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 234 | AddUintToBuffer(&buffer, method_it); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 235 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 236 | for (auto class_id : dex_data.class_set) { |
| 237 | AddUintToBuffer(&buffer, class_id); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 238 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 239 | DCHECK_EQ(required_capacity, buffer.size()) |
| 240 | << "Failed to add the expected number of bytes in the buffer"; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 243 | return WriteBuffer(fd, buffer.data(), buffer.size()); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 246 | ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData( |
| 247 | const std::string& dex_location, |
| 248 | uint32_t checksum) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 249 | auto info_it = info_.find(dex_location); |
| 250 | if (info_it == info_.end()) { |
| 251 | info_it = info_.Put(dex_location, DexFileData(checksum)); |
| 252 | } |
| 253 | if (info_it->second.checksum != checksum) { |
| 254 | LOG(WARNING) << "Checksum mismatch for dex " << dex_location; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 255 | return nullptr; |
| 256 | } |
| 257 | return &info_it->second; |
| 258 | } |
| 259 | |
| 260 | bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) { |
| 261 | const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation()); |
| 262 | const uint32_t checksum = classes.GetLocationChecksum(); |
| 263 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum); |
| 264 | if (data == nullptr) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 265 | return false; |
| 266 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 267 | data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end()); |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | bool ProfileCompilationInfo::AddMethodIndex(const std::string& dex_location, |
| 272 | uint32_t checksum, |
| 273 | uint16_t method_idx) { |
| 274 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum); |
| 275 | if (data == nullptr) { |
| 276 | return false; |
| 277 | } |
| 278 | data->method_set.insert(method_idx); |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location, |
| 283 | uint32_t checksum, |
| 284 | uint16_t class_idx) { |
| 285 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum); |
| 286 | if (data == nullptr) { |
| 287 | return false; |
| 288 | } |
| 289 | data->class_set.insert(class_idx); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 293 | bool ProfileCompilationInfo::ProcessLine(SafeBuffer& line_buffer, |
| 294 | uint16_t method_set_size, |
| 295 | uint16_t class_set_size, |
| 296 | uint32_t checksum, |
| 297 | const std::string& dex_location) { |
| 298 | for (uint16_t i = 0; i < method_set_size; i++) { |
| 299 | uint16_t method_idx = line_buffer.ReadUintAndAdvance<uint16_t>(); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 300 | if (!AddMethodIndex(dex_location, checksum, method_idx)) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 303 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 304 | |
| 305 | for (uint16_t i = 0; i < class_set_size; i++) { |
| 306 | uint16_t class_def_idx = line_buffer.ReadUintAndAdvance<uint16_t>(); |
| 307 | if (!AddClassIndex(dex_location, checksum, class_def_idx)) { |
| 308 | return false; |
| 309 | } |
| 310 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 314 | // Tests for EOF by trying to read 1 byte from the descriptor. |
| 315 | // Returns: |
| 316 | // 0 if the descriptor is at the EOF, |
| 317 | // -1 if there was an IO error |
| 318 | // 1 if the descriptor has more content to read |
| 319 | static int testEOF(int fd) { |
| 320 | uint8_t buffer[1]; |
| 321 | return TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 322 | } |
| 323 | |
| 324 | // Reads an uint value previously written with AddUintToBuffer. |
| 325 | template <typename T> |
| 326 | T ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance() { |
| 327 | static_assert(std::is_unsigned<T>::value, "Type is not unsigned"); |
| 328 | CHECK_LE(ptr_current_ + sizeof(T), ptr_end_); |
| 329 | T value = 0; |
| 330 | for (size_t i = 0; i < sizeof(T); i++) { |
| 331 | value += ptr_current_[i] << (i * kBitsPerByte); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 332 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 333 | ptr_current_ += sizeof(T); |
| 334 | return value; |
| 335 | } |
| 336 | |
| 337 | bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) { |
| 338 | if (ptr_current_ + data_size > ptr_end_) { |
| 339 | return false; |
| 340 | } |
| 341 | if (memcmp(ptr_current_, data, data_size) == 0) { |
| 342 | ptr_current_ += data_size; |
| 343 | return true; |
| 344 | } |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::SafeBuffer::FillFromFd( |
| 349 | int fd, |
| 350 | const std::string& source, |
| 351 | /*out*/std::string* error) { |
| 352 | size_t byte_count = ptr_end_ - ptr_current_; |
| 353 | uint8_t* buffer = ptr_current_; |
| 354 | while (byte_count > 0) { |
| 355 | int bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, byte_count)); |
| 356 | if (bytes_read == 0) { |
| 357 | *error += "Profile EOF reached prematurely for " + source; |
| 358 | return kProfileLoadBadData; |
| 359 | } else if (bytes_read < 0) { |
| 360 | *error += "Profile IO error for " + source + strerror(errno); |
| 361 | return kProfileLoadIOError; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 362 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 363 | byte_count -= bytes_read; |
| 364 | buffer += bytes_read; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 365 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 366 | return kProfileLoadSuccess; |
| 367 | } |
| 368 | |
| 369 | ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileHeader( |
| 370 | int fd, |
| 371 | /*out*/uint16_t* number_of_lines, |
| 372 | /*out*/std::string* error) { |
| 373 | // Read magic and version |
| 374 | const size_t kMagicVersionSize = |
| 375 | sizeof(kProfileMagic) + |
| 376 | sizeof(kProfileVersion) + |
| 377 | sizeof(uint16_t); // number of lines |
| 378 | |
| 379 | SafeBuffer safe_buffer(kMagicVersionSize); |
| 380 | |
| 381 | ProfileLoadSatus status = safe_buffer.FillFromFd(fd, "ReadProfileHeader", error); |
| 382 | if (status != kProfileLoadSuccess) { |
| 383 | return status; |
| 384 | } |
| 385 | |
| 386 | if (!safe_buffer.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) { |
| 387 | *error = "Profile missing magic"; |
| 388 | return kProfileLoadVersionMismatch; |
| 389 | } |
| 390 | if (!safe_buffer.CompareAndAdvance(kProfileVersion, sizeof(kProfileVersion))) { |
| 391 | *error = "Profile version mismatch"; |
| 392 | return kProfileLoadVersionMismatch; |
| 393 | } |
| 394 | *number_of_lines = safe_buffer.ReadUintAndAdvance<uint16_t>(); |
| 395 | return kProfileLoadSuccess; |
| 396 | } |
| 397 | |
| 398 | ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLineHeader( |
| 399 | int fd, |
| 400 | /*out*/ProfileLineHeader* line_header, |
| 401 | /*out*/std::string* error) { |
| 402 | SafeBuffer header_buffer(kLineHeaderSize); |
| 403 | ProfileLoadSatus status = header_buffer.FillFromFd(fd, "ReadProfileHeader", error); |
| 404 | if (status != kProfileLoadSuccess) { |
| 405 | return status; |
| 406 | } |
| 407 | |
| 408 | uint16_t dex_location_size = header_buffer.ReadUintAndAdvance<uint16_t>(); |
| 409 | line_header->method_set_size = header_buffer.ReadUintAndAdvance<uint16_t>(); |
| 410 | line_header->class_set_size = header_buffer.ReadUintAndAdvance<uint16_t>(); |
| 411 | line_header->checksum = header_buffer.ReadUintAndAdvance<uint32_t>(); |
| 412 | |
| 413 | if (dex_location_size == 0 || dex_location_size > kMaxDexFileKeyLength) { |
Goran Jakovljevic | 4eb6fbf | 2016-04-25 19:14:17 +0200 | [diff] [blame] | 414 | *error = "DexFileKey has an invalid size: " + |
| 415 | std::to_string(static_cast<uint32_t>(dex_location_size)); |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 416 | return kProfileLoadBadData; |
| 417 | } |
| 418 | |
| 419 | SafeBuffer location_buffer(dex_location_size); |
| 420 | status = location_buffer.FillFromFd(fd, "ReadProfileHeaderDexLocation", error); |
| 421 | if (status != kProfileLoadSuccess) { |
| 422 | return status; |
| 423 | } |
| 424 | line_header->dex_location.assign( |
| 425 | reinterpret_cast<char*>(location_buffer.Get()), dex_location_size); |
| 426 | return kProfileLoadSuccess; |
| 427 | } |
| 428 | |
| 429 | ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLine( |
| 430 | int fd, |
| 431 | const ProfileLineHeader& line_header, |
| 432 | /*out*/std::string* error) { |
| 433 | // Make sure that we don't try to read everything in memory (in case the profile if full). |
| 434 | // Split readings in chunks of at most 10kb. |
| 435 | static constexpr uint16_t kMaxNumberOfEntriesToRead = 5120; |
| 436 | uint16_t methods_left_to_read = line_header.method_set_size; |
| 437 | uint16_t classes_left_to_read = line_header.class_set_size; |
| 438 | |
| 439 | while ((methods_left_to_read > 0) || (classes_left_to_read > 0)) { |
| 440 | uint16_t methods_to_read = std::min(kMaxNumberOfEntriesToRead, methods_left_to_read); |
| 441 | uint16_t max_classes_to_read = kMaxNumberOfEntriesToRead - methods_to_read; |
| 442 | uint16_t classes_to_read = std::min(max_classes_to_read, classes_left_to_read); |
| 443 | |
| 444 | size_t line_size = sizeof(uint16_t) * (methods_to_read + classes_to_read); |
| 445 | SafeBuffer line_buffer(line_size); |
| 446 | |
| 447 | ProfileLoadSatus status = line_buffer.FillFromFd(fd, "ReadProfileLine", error); |
| 448 | if (status != kProfileLoadSuccess) { |
| 449 | return status; |
| 450 | } |
| 451 | if (!ProcessLine(line_buffer, |
| 452 | methods_to_read, |
| 453 | classes_to_read, |
| 454 | line_header.checksum, |
| 455 | line_header.dex_location)) { |
| 456 | *error = "Error when reading profile file line"; |
| 457 | return kProfileLoadBadData; |
| 458 | } |
| 459 | methods_left_to_read -= methods_to_read; |
| 460 | classes_left_to_read -= classes_to_read; |
| 461 | } |
| 462 | return kProfileLoadSuccess; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 465 | bool ProfileCompilationInfo::Load(int fd) { |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 466 | std::string error; |
| 467 | ProfileLoadSatus status = LoadInternal(fd, &error); |
| 468 | |
| 469 | if (status == kProfileLoadSuccess) { |
| 470 | return true; |
| 471 | } else { |
| 472 | PLOG(WARNING) << "Error when reading profile " << error; |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::LoadInternal( |
| 478 | int fd, std::string* error) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 479 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 480 | DCHECK_GE(fd, 0); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 481 | |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 482 | struct stat stat_buffer; |
| 483 | if (fstat(fd, &stat_buffer) != 0) { |
| 484 | return kProfileLoadIOError; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 485 | } |
Calin Juravle | 6414295 | 2016-03-21 14:37:55 +0000 | [diff] [blame] | 486 | // We allow empty profile files. |
| 487 | // Profiles may be created by ActivityManager or installd before we manage to |
| 488 | // process them in the runtime or profman. |
| 489 | if (stat_buffer.st_size == 0) { |
| 490 | return kProfileLoadSuccess; |
| 491 | } |
| 492 | // Read profile header: magic + version + number_of_lines. |
| 493 | uint16_t number_of_lines; |
| 494 | ProfileLoadSatus status = ReadProfileHeader(fd, &number_of_lines, error); |
| 495 | if (status != kProfileLoadSuccess) { |
| 496 | return status; |
| 497 | } |
| 498 | |
| 499 | while (number_of_lines > 0) { |
| 500 | ProfileLineHeader line_header; |
| 501 | // First, read the line header to get the amount of data we need to read. |
| 502 | status = ReadProfileLineHeader(fd, &line_header, error); |
| 503 | if (status != kProfileLoadSuccess) { |
| 504 | return status; |
| 505 | } |
| 506 | |
| 507 | // Now read the actual profile line. |
| 508 | status = ReadProfileLine(fd, line_header, error); |
| 509 | if (status != kProfileLoadSuccess) { |
| 510 | return status; |
| 511 | } |
| 512 | number_of_lines--; |
| 513 | } |
| 514 | |
| 515 | // Check that we read everything and that profiles don't contain junk data. |
| 516 | int result = testEOF(fd); |
| 517 | if (result == 0) { |
| 518 | return kProfileLoadSuccess; |
| 519 | } else if (result < 0) { |
| 520 | return kProfileLoadIOError; |
| 521 | } else { |
| 522 | *error = "Unexpected content in the profile file"; |
| 523 | return kProfileLoadBadData; |
| 524 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 527 | bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) { |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 528 | // First verify that all checksums match. This will avoid adding garbage to |
| 529 | // the current profile info. |
| 530 | // Note that the number of elements should be very small, so this should not |
| 531 | // be a performance issue. |
| 532 | for (const auto& other_it : other.info_) { |
| 533 | auto info_it = info_.find(other_it.first); |
| 534 | if ((info_it != info_.end()) && (info_it->second.checksum != other_it.second.checksum)) { |
| 535 | LOG(WARNING) << "Checksum mismatch for dex " << other_it.first; |
| 536 | return false; |
| 537 | } |
| 538 | } |
| 539 | // All checksums match. Import the data. |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 540 | for (const auto& other_it : other.info_) { |
| 541 | const std::string& other_dex_location = other_it.first; |
| 542 | const DexFileData& other_dex_data = other_it.second; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 543 | auto info_it = info_.find(other_dex_location); |
| 544 | if (info_it == info_.end()) { |
| 545 | info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum)); |
| 546 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 547 | info_it->second.method_set.insert(other_dex_data.method_set.begin(), |
| 548 | other_dex_data.method_set.end()); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 549 | info_it->second.class_set.insert(other_dex_data.class_set.begin(), |
| 550 | other_dex_data.class_set.end()); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 551 | } |
| 552 | return true; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Calin Juravle | c458857 | 2016-06-08 14:24:13 +0100 | [diff] [blame^] | 555 | static bool ChecksumMatch(const DexFile& dex_file, uint32_t checksum) { |
| 556 | return kDebugIgnoreChecksum || dex_file.GetLocationChecksum() == checksum; |
| 557 | } |
| 558 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 559 | bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const { |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 560 | auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation())); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 561 | if (info_it != info_.end()) { |
Calin Juravle | c458857 | 2016-06-08 14:24:13 +0100 | [diff] [blame^] | 562 | if (!ChecksumMatch(*method_ref.dex_file, info_it->second.checksum)) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 563 | return false; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 564 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 565 | const std::set<uint16_t>& methods = info_it->second.method_set; |
| 566 | return methods.find(method_ref.dex_method_index) != methods.end(); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 567 | } |
| 568 | return false; |
| 569 | } |
| 570 | |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 571 | bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const { |
| 572 | auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation())); |
| 573 | if (info_it != info_.end()) { |
Calin Juravle | c458857 | 2016-06-08 14:24:13 +0100 | [diff] [blame^] | 574 | if (!ChecksumMatch(dex_file, info_it->second.checksum)) { |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | const std::set<uint16_t>& classes = info_it->second.class_set; |
| 578 | return classes.find(class_def_idx) != classes.end(); |
| 579 | } |
| 580 | return false; |
| 581 | } |
| 582 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 583 | uint32_t ProfileCompilationInfo::GetNumberOfMethods() const { |
| 584 | uint32_t total = 0; |
| 585 | for (const auto& it : info_) { |
| 586 | total += it.second.method_set.size(); |
| 587 | } |
| 588 | return total; |
| 589 | } |
| 590 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 591 | uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const { |
| 592 | uint32_t total = 0; |
| 593 | for (const auto& it : info_) { |
| 594 | total += it.second.class_set.size(); |
| 595 | } |
| 596 | return total; |
| 597 | } |
| 598 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 599 | std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files, |
| 600 | bool print_full_dex_location) const { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 601 | std::ostringstream os; |
| 602 | if (info_.empty()) { |
| 603 | return "ProfileInfo: empty"; |
| 604 | } |
| 605 | |
| 606 | os << "ProfileInfo:"; |
| 607 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 608 | const std::string kFirstDexFileKeySubstitute = ":classes.dex"; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 609 | for (const auto& it : info_) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 610 | os << "\n"; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 611 | const std::string& location = it.first; |
| 612 | const DexFileData& dex_data = it.second; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 613 | if (print_full_dex_location) { |
| 614 | os << location; |
| 615 | } else { |
| 616 | // Replace the (empty) multidex suffix of the first key with a substitute for easier reading. |
| 617 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(location); |
| 618 | os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix); |
| 619 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 620 | const DexFile* dex_file = nullptr; |
| 621 | if (dex_files != nullptr) { |
| 622 | for (size_t i = 0; i < dex_files->size(); i++) { |
| 623 | if (location == (*dex_files)[i]->GetLocation()) { |
| 624 | dex_file = (*dex_files)[i]; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 625 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 626 | } |
Calin Juravle | 876f350 | 2016-03-24 16:16:34 +0000 | [diff] [blame] | 627 | } |
| 628 | os << "\n\tmethods: "; |
| 629 | for (const auto method_it : dex_data.method_set) { |
| 630 | if (dex_file != nullptr) { |
| 631 | os << "\n\t\t" << PrettyMethod(method_it, *dex_file, true); |
| 632 | } else { |
| 633 | os << method_it << ","; |
| 634 | } |
| 635 | } |
| 636 | os << "\n\tclasses: "; |
| 637 | for (const auto class_it : dex_data.class_set) { |
| 638 | if (dex_file != nullptr) { |
| 639 | os << "\n\t\t" << PrettyType(class_it, *dex_file); |
| 640 | } else { |
| 641 | os << class_it << ","; |
| 642 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | return os.str(); |
| 646 | } |
| 647 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 648 | bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 649 | return info_.Equals(other.info_); |
| 650 | } |
| 651 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 652 | std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const { |
| 653 | std::set<DexCacheResolvedClasses> ret; |
| 654 | for (auto&& pair : info_) { |
| 655 | const std::string& profile_key = pair.first; |
| 656 | const DexFileData& data = pair.second; |
Mathieu Chartier | b384e5e | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 657 | // TODO: Is it OK to use the same location for both base and dex location here? |
| 658 | DexCacheResolvedClasses classes(profile_key, profile_key, data.checksum); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 659 | classes.AddClasses(data.class_set.begin(), data.class_set.end()); |
| 660 | ret.insert(classes); |
| 661 | } |
| 662 | return ret; |
| 663 | } |
| 664 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 665 | void ProfileCompilationInfo::ClearResolvedClasses() { |
| 666 | for (auto& pair : info_) { |
| 667 | pair.second.class_set.clear(); |
| 668 | } |
| 669 | } |
| 670 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 671 | } // namespace art |