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