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