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