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 | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 28 | #include "jit/profiling_info.h" |
| 29 | #include "safe_map.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 33 | bool ProfileCompilationInfo::SaveProfilingInfo(const std::string& filename, |
| 34 | const std::vector<ArtMethod*>& methods) { |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 35 | if (methods.empty()) { |
| 36 | VLOG(profiler) << "No info to save to " << filename; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 37 | return true; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 40 | ProfileCompilationInfo info; |
| 41 | if (!info.Load(filename)) { |
| 42 | LOG(WARNING) << "Could not load previous profile data from file " << filename; |
| 43 | return false; |
| 44 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 45 | { |
| 46 | ScopedObjectAccess soa(Thread::Current()); |
| 47 | for (auto it = methods.begin(); it != methods.end(); it++) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 48 | const DexFile* dex_file = (*it)->GetDexFile(); |
| 49 | if (!info.AddData(dex_file->GetLocation(), |
| 50 | dex_file->GetLocationChecksum(), |
| 51 | (*it)->GetDexMethodIndex())) { |
| 52 | return false; |
| 53 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | // This doesn't need locking because we are trying to lock the file for exclusive |
| 58 | // access and fail immediately if we can't. |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 59 | bool result = info.Save(filename); |
| 60 | if (result) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 61 | VLOG(profiler) << "Successfully saved profile info to " << filename |
| 62 | << " Size: " << GetFileSizeBytes(filename); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 63 | } else { |
| 64 | VLOG(profiler) << "Failed to save profile info to " << filename; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 65 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 66 | return result; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 69 | enum OpenMode { |
| 70 | READ, |
| 71 | READ_WRITE |
| 72 | }; |
| 73 | |
| 74 | static int OpenFile(const std::string& filename, OpenMode open_mode) { |
| 75 | int fd = -1; |
| 76 | switch (open_mode) { |
| 77 | case READ: |
| 78 | fd = open(filename.c_str(), O_RDONLY); |
| 79 | break; |
| 80 | case READ_WRITE: |
| 81 | // TODO(calin) allow the shared uid of the app to access the file. |
Calin Juravle | 5e2b971 | 2015-12-18 14:10:00 +0200 | [diff] [blame] | 82 | fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 83 | break; |
| 84 | } |
| 85 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 86 | if (fd < 0) { |
| 87 | PLOG(WARNING) << "Failed to open profile file " << filename; |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | // Lock the file for exclusive access but don't wait if we can't lock it. |
| 92 | int err = flock(fd, LOCK_EX | LOCK_NB); |
| 93 | if (err < 0) { |
| 94 | PLOG(WARNING) << "Failed to lock profile file " << filename; |
| 95 | return -1; |
| 96 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 97 | return fd; |
| 98 | } |
| 99 | |
| 100 | static bool CloseDescriptorForFile(int fd, const std::string& filename) { |
| 101 | // Now unlock the file, allowing another process in. |
| 102 | int err = flock(fd, LOCK_UN); |
| 103 | if (err < 0) { |
| 104 | PLOG(WARNING) << "Failed to unlock profile file " << filename; |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // Done, close the file. |
| 109 | err = ::close(fd); |
| 110 | if (err < 0) { |
| 111 | PLOG(WARNING) << "Failed to close descriptor for profile file" << filename; |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | static void WriteToFile(int fd, const std::ostringstream& os) { |
| 119 | std::string data(os.str()); |
| 120 | const char *p = data.c_str(); |
| 121 | size_t length = data.length(); |
| 122 | do { |
| 123 | int n = ::write(fd, p, length); |
| 124 | p += n; |
| 125 | length -= n; |
| 126 | } while (length > 0); |
| 127 | } |
| 128 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 129 | static constexpr const char kFieldSeparator = ','; |
| 130 | static constexpr const char kLineSeparator = '\n'; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Serialization format: |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 134 | * dex_location1,dex_location_checksum1,method_id11,method_id12... |
| 135 | * dex_location2,dex_location_checksum2,method_id21,method_id22... |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 136 | * e.g. |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 137 | * /system/priv-app/app/app.apk,131232145,11,23,454,54 |
| 138 | * /system/priv-app/app/app.apk:classes5.dex,218490184,39,13,49,1 |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 139 | **/ |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 140 | bool ProfileCompilationInfo::Save(const std::string& filename) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 141 | int fd = OpenFile(filename, READ_WRITE); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 142 | if (fd == -1) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // TODO(calin): Merge with a previous existing profile. |
| 147 | // TODO(calin): Profile this and see how much memory it takes. If too much, |
| 148 | // write to file directly. |
| 149 | std::ostringstream os; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 150 | for (const auto& it : info_) { |
| 151 | const std::string& dex_location = it.first; |
| 152 | const DexFileData& dex_data = it.second; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 153 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 154 | os << dex_location << kFieldSeparator << dex_data.checksum; |
| 155 | for (auto method_it : dex_data.method_set) { |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 156 | os << kFieldSeparator << method_it; |
| 157 | } |
| 158 | os << kLineSeparator; |
| 159 | } |
| 160 | |
| 161 | WriteToFile(fd, os); |
| 162 | |
| 163 | return CloseDescriptorForFile(fd, filename); |
| 164 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 165 | |
| 166 | // TODO(calin): This a duplicate of Utils::Split fixing the case where the first character |
| 167 | // is the separator. Merge the fix into Utils::Split once verified that it doesn't break its users. |
| 168 | static void SplitString(const std::string& s, char separator, std::vector<std::string>* result) { |
| 169 | const char* p = s.data(); |
| 170 | const char* end = p + s.size(); |
| 171 | // Check if the first character is the separator. |
| 172 | if (p != end && *p ==separator) { |
| 173 | result->push_back(""); |
| 174 | ++p; |
| 175 | } |
| 176 | // Process the rest of the characters. |
| 177 | while (p != end) { |
| 178 | if (*p == separator) { |
| 179 | ++p; |
| 180 | } else { |
| 181 | const char* start = p; |
| 182 | while (++p != end && *p != separator) { |
| 183 | // Skip to the next occurrence of the separator. |
| 184 | } |
| 185 | result->push_back(std::string(start, p - start)); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 190 | bool ProfileCompilationInfo::AddData(const std::string& dex_location, |
| 191 | uint32_t checksum, |
| 192 | uint16_t method_idx) { |
| 193 | auto info_it = info_.find(dex_location); |
| 194 | if (info_it == info_.end()) { |
| 195 | info_it = info_.Put(dex_location, DexFileData(checksum)); |
| 196 | } |
| 197 | if (info_it->second.checksum != checksum) { |
| 198 | LOG(WARNING) << "Checksum mismatch for dex " << dex_location; |
| 199 | return false; |
| 200 | } |
| 201 | info_it->second.method_set.insert(method_idx); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool ProfileCompilationInfo::ProcessLine(const std::string& line) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 206 | std::vector<std::string> parts; |
| 207 | SplitString(line, kFieldSeparator, &parts); |
| 208 | if (parts.size() < 3) { |
| 209 | LOG(WARNING) << "Invalid line: " << line; |
| 210 | return false; |
| 211 | } |
| 212 | |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 213 | const std::string& dex_location = parts[0]; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 214 | uint32_t checksum; |
| 215 | if (!ParseInt(parts[1].c_str(), &checksum)) { |
| 216 | return false; |
| 217 | } |
| 218 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 219 | for (size_t i = 2; i < parts.size(); i++) { |
| 220 | uint32_t method_idx; |
| 221 | if (!ParseInt(parts[i].c_str(), &method_idx)) { |
| 222 | LOG(WARNING) << "Cannot parse method_idx " << parts[i]; |
| 223 | return false; |
| 224 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 225 | AddData(dex_location, checksum, method_idx); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | // Parses the buffer (of length n) starting from start_from and identify new lines |
| 231 | // based on kLineSeparator marker. |
| 232 | // Returns the first position after kLineSeparator in the buffer (starting from start_from), |
| 233 | // or -1 if the marker doesn't appear. |
| 234 | // The processed characters are appended to the given line. |
| 235 | static int GetLineFromBuffer(char* buffer, int n, int start_from, std::string& line) { |
| 236 | if (start_from >= n) { |
| 237 | return -1; |
| 238 | } |
| 239 | int new_line_pos = -1; |
| 240 | for (int i = start_from; i < n; i++) { |
| 241 | if (buffer[i] == kLineSeparator) { |
| 242 | new_line_pos = i; |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | int append_limit = new_line_pos == -1 ? n : new_line_pos; |
| 247 | line.append(buffer + start_from, append_limit - start_from); |
| 248 | // Jump over kLineSeparator and return the position of the next character. |
| 249 | return new_line_pos == -1 ? new_line_pos : new_line_pos + 1; |
| 250 | } |
| 251 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 252 | bool ProfileCompilationInfo::Load(const std::string& filename) { |
| 253 | int fd = OpenFile(filename, READ); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 254 | if (fd == -1) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | std::string current_line; |
| 259 | const int kBufferSize = 1024; |
| 260 | char buffer[kBufferSize]; |
| 261 | bool success = true; |
| 262 | |
| 263 | while (success) { |
| 264 | int n = read(fd, buffer, kBufferSize); |
| 265 | if (n < 0) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 266 | PLOG(WARNING) << "Error when reading profile file " << filename; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 267 | success = false; |
| 268 | break; |
| 269 | } else if (n == 0) { |
| 270 | break; |
| 271 | } |
| 272 | // Detect the new lines from the buffer. If we manage to complete a line, |
| 273 | // process it. Otherwise append to the current line. |
| 274 | int current_start_pos = 0; |
| 275 | while (current_start_pos < n) { |
| 276 | current_start_pos = GetLineFromBuffer(buffer, n, current_start_pos, current_line); |
| 277 | if (current_start_pos == -1) { |
| 278 | break; |
| 279 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 280 | if (!ProcessLine(current_line)) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 281 | success = false; |
| 282 | break; |
| 283 | } |
| 284 | // Reset the current line (we just processed it). |
| 285 | current_line.clear(); |
| 286 | } |
| 287 | } |
| 288 | if (!success) { |
| 289 | info_.clear(); |
| 290 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 291 | return CloseDescriptorForFile(fd, filename) && success; |
| 292 | } |
| 293 | |
| 294 | bool ProfileCompilationInfo::Load(const ProfileCompilationInfo& other) { |
| 295 | for (const auto& other_it : other.info_) { |
| 296 | const std::string& other_dex_location = other_it.first; |
| 297 | const DexFileData& other_dex_data = other_it.second; |
| 298 | |
| 299 | auto info_it = info_.find(other_dex_location); |
| 300 | if (info_it == info_.end()) { |
| 301 | info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum)); |
| 302 | } |
| 303 | if (info_it->second.checksum != other_dex_data.checksum) { |
| 304 | LOG(WARNING) << "Checksum mismatch for dex " << other_dex_location; |
| 305 | return false; |
| 306 | } |
| 307 | info_it->second.method_set.insert(other_dex_data.method_set.begin(), |
| 308 | other_dex_data.method_set.end()); |
| 309 | } |
| 310 | return true; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 314 | auto info_it = info_.find(method_ref.dex_file->GetLocation()); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 315 | if (info_it != info_.end()) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 316 | if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) { |
| 317 | return false; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 318 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 319 | const std::set<uint16_t>& methods = info_it->second.method_set; |
| 320 | return methods.find(method_ref.dex_method_index) != methods.end(); |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 325 | uint32_t ProfileCompilationInfo::GetNumberOfMethods() const { |
| 326 | uint32_t total = 0; |
| 327 | for (const auto& it : info_) { |
| 328 | total += it.second.method_set.size(); |
| 329 | } |
| 330 | return total; |
| 331 | } |
| 332 | |
| 333 | std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files, |
| 334 | bool print_full_dex_location) const { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 335 | std::ostringstream os; |
| 336 | if (info_.empty()) { |
| 337 | return "ProfileInfo: empty"; |
| 338 | } |
| 339 | |
| 340 | os << "ProfileInfo:"; |
| 341 | |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 342 | const std::string kFirstDexFileKeySubstitute = ":classes.dex"; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 343 | for (const auto& it : info_) { |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 344 | os << "\n"; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 345 | const std::string& location = it.first; |
| 346 | const DexFileData& dex_data = it.second; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 347 | if (print_full_dex_location) { |
| 348 | os << location; |
| 349 | } else { |
| 350 | // Replace the (empty) multidex suffix of the first key with a substitute for easier reading. |
| 351 | std::string multidex_suffix = DexFile::GetMultiDexSuffix(location); |
| 352 | os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix); |
| 353 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 354 | for (const auto method_it : dex_data.method_set) { |
| 355 | if (dex_files != nullptr) { |
| 356 | const DexFile* dex_file = nullptr; |
| 357 | for (size_t i = 0; i < dex_files->size(); i++) { |
| 358 | if (location == (*dex_files)[i]->GetLocation()) { |
| 359 | dex_file = (*dex_files)[i]; |
| 360 | } |
| 361 | } |
| 362 | if (dex_file != nullptr) { |
| 363 | os << "\n " << PrettyMethod(method_it, *dex_file, true); |
| 364 | } |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 365 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame^] | 366 | os << "\n " << method_it; |
Calin Juravle | 226501b | 2015-12-11 14:41:31 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | return os.str(); |
| 370 | } |
| 371 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 372 | } // namespace art |