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 | |
| 19 | #include <fstream> |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 20 | #include <vector> |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 21 | #include <sys/file.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/uio.h> |
| 24 | |
| 25 | #include "art_method-inl.h" |
| 26 | #include "base/mutex.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 27 | #include "base/scoped_flock.h" |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 28 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 29 | #include "base/systrace.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 31 | #include "jit/profiling_info.h" |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 32 | #include "os.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 33 | #include "safe_map.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 37 | // Transform the actual dex location into relative paths. |
| 38 | // Note: this is OK because we don't store profiles of different apps into the same file. |
| 39 | // Apps with split apks don't cause trouble because each split has a different name and will not |
| 40 | // collide with other entries. |
Calin Juravle | 31708b7 | 2016-02-05 19:44:05 +0000 | [diff] [blame] | 41 | std::string ProfileCompilationInfo::GetProfileDexFileKey(const std::string& dex_location) { |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 42 | DCHECK(!dex_location.empty()); |
| 43 | size_t last_sep_index = dex_location.find_last_of('/'); |
| 44 | if (last_sep_index == std::string::npos) { |
| 45 | return dex_location; |
| 46 | } else { |
Calin Juravle | 31708b7 | 2016-02-05 19:44:05 +0000 | [diff] [blame] | 47 | DCHECK(last_sep_index < dex_location.size()); |
| 48 | return dex_location.substr(last_sep_index + 1); |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 52 | bool ProfileCompilationInfo::AddMethodsAndClasses( |
| 53 | const std::vector<ArtMethod*>& methods, |
| 54 | const std::set<DexCacheResolvedClasses>& resolved_classes) { |
| 55 | ScopedObjectAccess soa(Thread::Current()); |
| 56 | for (ArtMethod* method : methods) { |
| 57 | const DexFile* dex_file = method->GetDexFile(); |
| 58 | if (!AddMethodIndex(GetProfileDexFileKey(dex_file->GetLocation()), |
| 59 | dex_file->GetLocationChecksum(), |
| 60 | method->GetDexMethodIndex())) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | for (const DexCacheResolvedClasses& dex_cache : resolved_classes) { |
| 65 | if (!AddResolvedClasses(dex_cache)) { |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool ProfileCompilationInfo::MergeAndSave(const std::string& filename, uint64_t* bytes_written) { |
| 73 | ScopedTrace trace(__PRETTY_FUNCTION__); |
| 74 | ScopedFlock flock; |
| 75 | std::string error; |
| 76 | if (!flock.Init(filename.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC, /* block */ false, &error)) { |
| 77 | LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error; |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | int fd = flock.GetFile()->Fd(); |
| 82 | |
| 83 | // Load the file but keep a copy around to be able to infer if the content has changed. |
| 84 | ProfileCompilationInfo fileInfo; |
| 85 | if (!fileInfo.Load(fd)) { |
| 86 | LOG(WARNING) << "Could not load previous profile data from file " << filename; |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // Merge the content of file into the current object. |
| 91 | if (!MergeWith(fileInfo)) { |
| 92 | LOG(WARNING) << "Could not merge previous profile data from file " << filename; |
| 93 | } |
| 94 | |
| 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 | |
| 104 | // We need to clear the data because we don't support append to the profiles yet. |
| 105 | if (!flock.GetFile()->ClearContent()) { |
| 106 | PLOG(WARNING) << "Could not clear profile file: " << filename; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // This doesn't need locking because we are trying to lock the file for exclusive |
| 111 | // access and fail immediately if we can't. |
| 112 | bool result = Save(fd); |
| 113 | if (result) { |
| 114 | VLOG(profiler) << "Successfully saved profile info to " << filename |
| 115 | << " Size: " << GetFileSizeBytes(filename); |
| 116 | if (bytes_written != nullptr) { |
| 117 | *bytes_written = GetFileSizeBytes(filename); |
| 118 | } |
| 119 | } else { |
| 120 | VLOG(profiler) << "Failed to save profile info to " << filename; |
| 121 | } |
| 122 | return result; |
| 123 | } |
| 124 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 125 | bool ProfileCompilationInfo::SaveProfilingInfo( |
| 126 | const std::string& filename, |
| 127 | const std::vector<ArtMethod*>& methods, |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 128 | const std::set<DexCacheResolvedClasses>& resolved_classes, |
| 129 | uint64_t* bytes_written) { |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 130 | if (methods.empty() && resolved_classes.empty()) { |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 131 | VLOG(profiler) << "No info to save to " << filename; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 132 | if (bytes_written != nullptr) { |
| 133 | *bytes_written = 0; |
| 134 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 135 | return true; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 138 | ProfileCompilationInfo info; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 139 | if (!info.AddMethodsAndClasses(methods, resolved_classes)) { |
| 140 | LOG(WARNING) << "Checksum mismatch when processing methods and resolved classes for " |
| 141 | << filename; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 142 | if (bytes_written != nullptr) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 143 | *bytes_written = 0; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 144 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 145 | return false; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 146 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 147 | return info.MergeAndSave(filename, bytes_written); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 150 | static bool WriteToFile(int fd, const std::ostringstream& os) { |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 151 | std::string data(os.str()); |
| 152 | const char *p = data.c_str(); |
| 153 | size_t length = data.length(); |
| 154 | do { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 155 | int n = TEMP_FAILURE_RETRY(write(fd, p, length)); |
| 156 | if (n < 0) { |
| 157 | PLOG(WARNING) << "Failed to write to descriptor: " << fd; |
| 158 | return false; |
| 159 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 160 | p += n; |
| 161 | length -= n; |
| 162 | } while (length > 0); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 163 | return true; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 166 | static constexpr const char kFieldSeparator = ','; |
| 167 | static constexpr const char kLineSeparator = '\n'; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 168 | static constexpr const char* kClassesMarker = "classes"; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Serialization format: |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 172 | * dex_location1,dex_location_checksum1,method_id11,method_id12...,classes,class_id1,class_id2... |
| 173 | * dex_location2,dex_location_checksum2,method_id21,method_id22...,classes,class_id1,class_id2... |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 174 | * e.g. |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 175 | * app.apk,131232145,11,23,454,54,classes,1,2,4,1234 |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 176 | * app.apk:classes5.dex,218490184,39,13,49,1 |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 177 | **/ |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 178 | bool ProfileCompilationInfo::Save(int fd) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 179 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 180 | DCHECK_GE(fd, 0); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 181 | // TODO(calin): Profile this and see how much memory it takes. If too much, |
| 182 | // write to file directly. |
| 183 | std::ostringstream os; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 184 | for (const auto& it : info_) { |
| 185 | const std::string& dex_location = it.first; |
| 186 | const DexFileData& dex_data = it.second; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 187 | if (dex_data.method_set.empty() && dex_data.class_set.empty()) { |
| 188 | continue; |
| 189 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 190 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 191 | os << dex_location << kFieldSeparator << dex_data.checksum; |
| 192 | for (auto method_it : dex_data.method_set) { |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 193 | os << kFieldSeparator << method_it; |
| 194 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 195 | if (!dex_data.class_set.empty()) { |
| 196 | os << kFieldSeparator << kClassesMarker; |
| 197 | for (auto class_id : dex_data.class_set) { |
| 198 | os << kFieldSeparator << class_id; |
| 199 | } |
| 200 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 201 | os << kLineSeparator; |
| 202 | } |
| 203 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 204 | return WriteToFile(fd, os); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 205 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 206 | |
| 207 | // TODO(calin): This a duplicate of Utils::Split fixing the case where the first character |
| 208 | // is the separator. Merge the fix into Utils::Split once verified that it doesn't break its users. |
| 209 | static void SplitString(const std::string& s, char separator, std::vector<std::string>* result) { |
| 210 | const char* p = s.data(); |
| 211 | const char* end = p + s.size(); |
| 212 | // Check if the first character is the separator. |
| 213 | if (p != end && *p ==separator) { |
| 214 | result->push_back(""); |
| 215 | ++p; |
| 216 | } |
| 217 | // Process the rest of the characters. |
| 218 | while (p != end) { |
| 219 | if (*p == separator) { |
| 220 | ++p; |
| 221 | } else { |
| 222 | const char* start = p; |
| 223 | while (++p != end && *p != separator) { |
| 224 | // Skip to the next occurrence of the separator. |
| 225 | } |
| 226 | result->push_back(std::string(start, p - start)); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 231 | ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData( |
| 232 | const std::string& dex_location, |
| 233 | uint32_t checksum) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 234 | auto info_it = info_.find(dex_location); |
| 235 | if (info_it == info_.end()) { |
| 236 | info_it = info_.Put(dex_location, DexFileData(checksum)); |
| 237 | } |
| 238 | if (info_it->second.checksum != checksum) { |
| 239 | LOG(WARNING) << "Checksum mismatch for dex " << dex_location; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 240 | return nullptr; |
| 241 | } |
| 242 | return &info_it->second; |
| 243 | } |
| 244 | |
| 245 | bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) { |
| 246 | const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation()); |
| 247 | const uint32_t checksum = classes.GetLocationChecksum(); |
| 248 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum); |
| 249 | if (data == nullptr) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 250 | return false; |
| 251 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 252 | data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end()); |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | bool ProfileCompilationInfo::AddMethodIndex(const std::string& dex_location, |
| 257 | uint32_t checksum, |
| 258 | uint16_t method_idx) { |
| 259 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum); |
| 260 | if (data == nullptr) { |
| 261 | return false; |
| 262 | } |
| 263 | data->method_set.insert(method_idx); |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location, |
| 268 | uint32_t checksum, |
| 269 | uint16_t class_idx) { |
| 270 | DexFileData* const data = GetOrAddDexFileData(dex_location, checksum); |
| 271 | if (data == nullptr) { |
| 272 | return false; |
| 273 | } |
| 274 | data->class_set.insert(class_idx); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | |
| 278 | bool ProfileCompilationInfo::ProcessLine(const std::string& line) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 279 | std::vector<std::string> parts; |
| 280 | SplitString(line, kFieldSeparator, &parts); |
| 281 | if (parts.size() < 3) { |
| 282 | LOG(WARNING) << "Invalid line: " << line; |
| 283 | return false; |
| 284 | } |
| 285 | |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 286 | const std::string& dex_location = parts[0]; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 287 | uint32_t checksum; |
| 288 | if (!ParseInt(parts[1].c_str(), &checksum)) { |
| 289 | return false; |
| 290 | } |
| 291 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 292 | for (size_t i = 2; i < parts.size(); i++) { |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 293 | if (parts[i] == kClassesMarker) { |
| 294 | ++i; |
| 295 | // All of the remaining idx are class def indexes. |
| 296 | for (++i; i < parts.size(); ++i) { |
| 297 | uint32_t class_def_idx; |
| 298 | if (!ParseInt(parts[i].c_str(), &class_def_idx)) { |
| 299 | LOG(WARNING) << "Cannot parse class_def_idx " << parts[i]; |
| 300 | return false; |
| 301 | } else if (class_def_idx >= std::numeric_limits<uint16_t>::max()) { |
| 302 | LOG(WARNING) << "Class def idx " << class_def_idx << " is larger than uint16_t max"; |
| 303 | return false; |
| 304 | } |
| 305 | if (!AddClassIndex(dex_location, checksum, class_def_idx)) { |
| 306 | return false; |
| 307 | } |
| 308 | } |
| 309 | break; |
| 310 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 311 | uint32_t method_idx; |
| 312 | if (!ParseInt(parts[i].c_str(), &method_idx)) { |
| 313 | LOG(WARNING) << "Cannot parse method_idx " << parts[i]; |
| 314 | return false; |
| 315 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 316 | if (!AddMethodIndex(dex_location, checksum, method_idx)) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 319 | } |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | // Parses the buffer (of length n) starting from start_from and identify new lines |
| 324 | // based on kLineSeparator marker. |
| 325 | // Returns the first position after kLineSeparator in the buffer (starting from start_from), |
| 326 | // or -1 if the marker doesn't appear. |
| 327 | // The processed characters are appended to the given line. |
| 328 | static int GetLineFromBuffer(char* buffer, int n, int start_from, std::string& line) { |
| 329 | if (start_from >= n) { |
| 330 | return -1; |
| 331 | } |
| 332 | int new_line_pos = -1; |
| 333 | for (int i = start_from; i < n; i++) { |
| 334 | if (buffer[i] == kLineSeparator) { |
| 335 | new_line_pos = i; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | int append_limit = new_line_pos == -1 ? n : new_line_pos; |
| 340 | line.append(buffer + start_from, append_limit - start_from); |
| 341 | // Jump over kLineSeparator and return the position of the next character. |
| 342 | return new_line_pos == -1 ? new_line_pos : new_line_pos + 1; |
| 343 | } |
| 344 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 345 | bool ProfileCompilationInfo::Load(int fd) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 346 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 347 | DCHECK_GE(fd, 0); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 348 | |
| 349 | std::string current_line; |
| 350 | const int kBufferSize = 1024; |
| 351 | char buffer[kBufferSize]; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 352 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 353 | while (true) { |
| 354 | int n = TEMP_FAILURE_RETRY(read(fd, buffer, kBufferSize)); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 355 | if (n < 0) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 356 | PLOG(WARNING) << "Error when reading profile file"; |
| 357 | return false; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 358 | } else if (n == 0) { |
| 359 | break; |
| 360 | } |
| 361 | // Detect the new lines from the buffer. If we manage to complete a line, |
| 362 | // process it. Otherwise append to the current line. |
| 363 | int current_start_pos = 0; |
| 364 | while (current_start_pos < n) { |
| 365 | current_start_pos = GetLineFromBuffer(buffer, n, current_start_pos, current_line); |
| 366 | if (current_start_pos == -1) { |
| 367 | break; |
| 368 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 369 | if (!ProcessLine(current_line)) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 370 | return false; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 371 | } |
| 372 | // Reset the current line (we just processed it). |
| 373 | current_line.clear(); |
| 374 | } |
| 375 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 376 | return true; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 379 | bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 380 | for (const auto& other_it : other.info_) { |
| 381 | const std::string& other_dex_location = other_it.first; |
| 382 | const DexFileData& other_dex_data = other_it.second; |
| 383 | |
| 384 | auto info_it = info_.find(other_dex_location); |
| 385 | if (info_it == info_.end()) { |
| 386 | info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum)); |
| 387 | } |
| 388 | if (info_it->second.checksum != other_dex_data.checksum) { |
| 389 | LOG(WARNING) << "Checksum mismatch for dex " << other_dex_location; |
| 390 | return false; |
| 391 | } |
| 392 | info_it->second.method_set.insert(other_dex_data.method_set.begin(), |
| 393 | other_dex_data.method_set.end()); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 394 | info_it->second.class_set.insert(other_dex_data.class_set.begin(), |
| 395 | other_dex_data.class_set.end()); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 396 | } |
| 397 | return true; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const { |
Calin Juravle | 34900cc | 2016-02-05 16:19:19 +0000 | [diff] [blame] | 401 | auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation())); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 402 | if (info_it != info_.end()) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 403 | if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) { |
| 404 | return false; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 405 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 406 | const std::set<uint16_t>& methods = info_it->second.method_set; |
| 407 | return methods.find(method_ref.dex_method_index) != methods.end(); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 412 | bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const { |
| 413 | auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation())); |
| 414 | if (info_it != info_.end()) { |
| 415 | if (dex_file.GetLocationChecksum() != info_it->second.checksum) { |
| 416 | return false; |
| 417 | } |
| 418 | const std::set<uint16_t>& classes = info_it->second.class_set; |
| 419 | return classes.find(class_def_idx) != classes.end(); |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 424 | uint32_t ProfileCompilationInfo::GetNumberOfMethods() const { |
| 425 | uint32_t total = 0; |
| 426 | for (const auto& it : info_) { |
| 427 | total += it.second.method_set.size(); |
| 428 | } |
| 429 | return total; |
| 430 | } |
| 431 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 432 | uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const { |
| 433 | uint32_t total = 0; |
| 434 | for (const auto& it : info_) { |
| 435 | total += it.second.class_set.size(); |
| 436 | } |
| 437 | return total; |
| 438 | } |
| 439 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 440 | std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files, |
| 441 | bool print_full_dex_location) const { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 442 | std::ostringstream os; |
| 443 | if (info_.empty()) { |
| 444 | return "ProfileInfo: empty"; |
| 445 | } |
| 446 | |
| 447 | os << "ProfileInfo:"; |
| 448 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 449 | const std::string kFirstDexFileKeySubstitute = ":classes.dex"; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 450 | for (const auto& it : info_) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 451 | os << "\n"; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 452 | const std::string& location = it.first; |
| 453 | const DexFileData& dex_data = it.second; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 454 | if (print_full_dex_location) { |
| 455 | os << location; |
| 456 | } else { |
| 457 | // Replace the (empty) multidex suffix of the first key with a substitute for easier reading. |
| 458 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(location); |
| 459 | os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix); |
| 460 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 461 | for (const auto method_it : dex_data.method_set) { |
| 462 | if (dex_files != nullptr) { |
| 463 | const DexFile* dex_file = nullptr; |
| 464 | for (size_t i = 0; i < dex_files->size(); i++) { |
| 465 | if (location == (*dex_files)[i]->GetLocation()) { |
| 466 | dex_file = (*dex_files)[i]; |
| 467 | } |
| 468 | } |
| 469 | if (dex_file != nullptr) { |
| 470 | os << "\n " << PrettyMethod(method_it, *dex_file, true); |
| 471 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 472 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 473 | os << "\n " << method_it; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | return os.str(); |
| 477 | } |
| 478 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 479 | bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 480 | return info_.Equals(other.info_); |
| 481 | } |
| 482 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 483 | std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const { |
| 484 | std::set<DexCacheResolvedClasses> ret; |
| 485 | for (auto&& pair : info_) { |
| 486 | const std::string& profile_key = pair.first; |
| 487 | const DexFileData& data = pair.second; |
| 488 | DexCacheResolvedClasses classes(profile_key, data.checksum); |
| 489 | classes.AddClasses(data.class_set.begin(), data.class_set.end()); |
| 490 | ret.insert(classes); |
| 491 | } |
| 492 | return ret; |
| 493 | } |
| 494 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame^] | 495 | void ProfileCompilationInfo::ClearResolvedClasses() { |
| 496 | for (auto& pair : info_) { |
| 497 | pair.second.class_set.clear(); |
| 498 | } |
| 499 | } |
| 500 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 501 | } // namespace art |