Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <fstream> |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 21 | #include <functional> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <set> |
| 26 | #include <map> |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 27 | #include <unordered_set> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 28 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 29 | #include "art_method-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
| 31 | #include "base/stringprintf.h" |
| 32 | #include "gc/space/image_space.h" |
| 33 | #include "gc/heap.h" |
| 34 | #include "mirror/class-inl.h" |
| 35 | #include "mirror/object-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 36 | #include "image.h" |
| 37 | #include "scoped_thread_state_change.h" |
| 38 | #include "os.h" |
| 39 | #include "gc_map.h" |
| 40 | |
| 41 | #include "cmdline.h" |
| 42 | #include "backtrace/BacktraceMap.h" |
| 43 | |
| 44 | #include <sys/stat.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <signal.h> |
| 47 | |
| 48 | namespace art { |
| 49 | |
| 50 | class ImgDiagDumper { |
| 51 | public: |
| 52 | explicit ImgDiagDumper(std::ostream* os, |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 53 | const ImageHeader& image_header, |
| 54 | const std::string& image_location, |
| 55 | pid_t image_diff_pid) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 56 | : os_(os), |
| 57 | image_header_(image_header), |
| 58 | image_location_(image_location), |
| 59 | image_diff_pid_(image_diff_pid) {} |
| 60 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 61 | bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 62 | std::ostream& os = *os_; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 63 | os << "IMAGE LOCATION: " << image_location_ << "\n\n"; |
| 64 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 65 | os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; |
| 66 | |
| 67 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
| 68 | |
| 69 | bool ret = true; |
| 70 | if (image_diff_pid_ >= 0) { |
| 71 | os << "IMAGE DIFF PID (" << image_diff_pid_ << "): "; |
| 72 | ret = DumpImageDiff(image_diff_pid_); |
| 73 | os << "\n\n"; |
| 74 | } else { |
| 75 | os << "IMAGE DIFF PID: disabled\n\n"; |
| 76 | } |
| 77 | |
| 78 | os << std::flush; |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | static bool EndsWith(const std::string& str, const std::string& suffix) { |
| 85 | return str.size() >= suffix.size() && |
| 86 | str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; |
| 87 | } |
| 88 | |
| 89 | // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar) |
| 90 | static std::string BaseName(const std::string& str) { |
| 91 | size_t idx = str.rfind("/"); |
| 92 | if (idx == std::string::npos) { |
| 93 | return str; |
| 94 | } |
| 95 | |
| 96 | return str.substr(idx + 1); |
| 97 | } |
| 98 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 99 | bool DumpImageDiff(pid_t image_diff_pid) SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 100 | std::ostream& os = *os_; |
| 101 | |
| 102 | { |
| 103 | struct stat sts; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 104 | std::string proc_pid_str = |
| 105 | StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int] |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 106 | if (stat(proc_pid_str.c_str(), &sts) == -1) { |
| 107 | os << "Process does not exist"; |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Open /proc/$pid/maps to view memory maps |
| 113 | auto proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid)); |
| 114 | if (proc_maps == nullptr) { |
| 115 | os << "Could not read backtrace maps"; |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool found_boot_map = false; |
| 120 | backtrace_map_t boot_map = backtrace_map_t(); |
| 121 | // Find the memory map only for boot.art |
| 122 | for (const backtrace_map_t& map : *proc_maps) { |
| 123 | if (EndsWith(map.name, GetImageLocationBaseName())) { |
| 124 | if ((map.flags & PROT_WRITE) != 0) { |
| 125 | boot_map = map; |
| 126 | found_boot_map = true; |
| 127 | break; |
| 128 | } |
| 129 | // In actuality there's more than 1 map, but the second one is read-only. |
| 130 | // The one we care about is the write-able map. |
| 131 | // The readonly maps are guaranteed to be identical, so its not interesting to compare |
| 132 | // them. |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!found_boot_map) { |
| 137 | os << "Could not find map for " << GetImageLocationBaseName(); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | // Future idea: diff against zygote so we can ignore the shared dirty pages. |
| 142 | return DumpImageDiffMap(image_diff_pid, boot_map); |
| 143 | } |
| 144 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 145 | static std::string PrettyFieldValue(ArtField* field, mirror::Object* obj) |
| 146 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 147 | std::ostringstream oss; |
| 148 | switch (field->GetTypeAsPrimitiveType()) { |
| 149 | case Primitive::kPrimNot: { |
| 150 | oss << obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 151 | field->GetOffset()); |
| 152 | break; |
| 153 | } |
| 154 | case Primitive::kPrimBoolean: { |
| 155 | oss << static_cast<bool>(obj->GetFieldBoolean<kVerifyNone>(field->GetOffset())); |
| 156 | break; |
| 157 | } |
| 158 | case Primitive::kPrimByte: { |
| 159 | oss << static_cast<int32_t>(obj->GetFieldByte<kVerifyNone>(field->GetOffset())); |
| 160 | break; |
| 161 | } |
| 162 | case Primitive::kPrimChar: { |
| 163 | oss << obj->GetFieldChar<kVerifyNone>(field->GetOffset()); |
| 164 | break; |
| 165 | } |
| 166 | case Primitive::kPrimShort: { |
| 167 | oss << obj->GetFieldShort<kVerifyNone>(field->GetOffset()); |
| 168 | break; |
| 169 | } |
| 170 | case Primitive::kPrimInt: { |
| 171 | oss << obj->GetField32<kVerifyNone>(field->GetOffset()); |
| 172 | break; |
| 173 | } |
| 174 | case Primitive::kPrimLong: { |
| 175 | oss << obj->GetField64<kVerifyNone>(field->GetOffset()); |
| 176 | break; |
| 177 | } |
| 178 | case Primitive::kPrimFloat: { |
| 179 | oss << obj->GetField32<kVerifyNone>(field->GetOffset()); |
| 180 | break; |
| 181 | } |
| 182 | case Primitive::kPrimDouble: { |
| 183 | oss << obj->GetField64<kVerifyNone>(field->GetOffset()); |
| 184 | break; |
| 185 | } |
| 186 | case Primitive::kPrimVoid: { |
| 187 | oss << "void"; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | return oss.str(); |
| 192 | } |
| 193 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 194 | // Aggregate and detail class data from an image diff. |
| 195 | struct ClassData { |
| 196 | int dirty_object_count = 0; |
| 197 | |
| 198 | // Track only the byte-per-byte dirtiness (in bytes) |
| 199 | int dirty_object_byte_count = 0; |
| 200 | |
| 201 | // Track the object-by-object dirtiness (in bytes) |
| 202 | int dirty_object_size_in_bytes = 0; |
| 203 | |
| 204 | int clean_object_count = 0; |
| 205 | |
| 206 | std::string descriptor; |
| 207 | |
| 208 | int false_dirty_byte_count = 0; |
| 209 | int false_dirty_object_count = 0; |
| 210 | std::vector<mirror::Object*> false_dirty_objects; |
| 211 | |
| 212 | // Remote pointers to dirty objects |
| 213 | std::vector<mirror::Object*> dirty_objects; |
| 214 | }; |
| 215 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 216 | // Look at /proc/$pid/mem and only diff the things from there |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 217 | bool DumpImageDiffMap(pid_t image_diff_pid, const backtrace_map_t& boot_map) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 218 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 219 | std::ostream& os = *os_; |
| 220 | const size_t pointer_size = InstructionSetPointerSize( |
| 221 | Runtime::Current()->GetInstructionSet()); |
| 222 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 223 | std::string file_name = |
| 224 | StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int] |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 225 | |
| 226 | size_t boot_map_size = boot_map.end - boot_map.start; |
| 227 | |
| 228 | // Open /proc/$pid/mem as a file |
| 229 | auto map_file = std::unique_ptr<File>(OS::OpenFileForReading(file_name.c_str())); |
| 230 | if (map_file == nullptr) { |
| 231 | os << "Failed to open " << file_name << " for reading"; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | // Memory-map /proc/$pid/mem subset from the boot map |
| 236 | CHECK(boot_map.end >= boot_map.start); |
| 237 | |
| 238 | std::string error_msg; |
| 239 | |
| 240 | // Walk the bytes and diff against our boot image |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 241 | const ImageHeader& boot_image_header = image_header_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 242 | |
| 243 | os << "\nObserving boot image header at address " |
| 244 | << reinterpret_cast<const void*>(&boot_image_header) |
| 245 | << "\n\n"; |
| 246 | |
| 247 | const uint8_t* image_begin_unaligned = boot_image_header.GetImageBegin(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 248 | const uint8_t* image_mirror_end_unaligned = image_begin_unaligned + |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 249 | boot_image_header.GetImageSection(ImageHeader::kSectionObjects).Size(); |
| 250 | const uint8_t* image_end_unaligned = image_begin_unaligned + boot_image_header.GetImageSize(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 251 | |
| 252 | // Adjust range to nearest page |
| 253 | const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize); |
| 254 | const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize); |
| 255 | |
| 256 | ptrdiff_t page_off_begin = boot_image_header.GetImageBegin() - image_begin; |
| 257 | |
| 258 | if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start || |
| 259 | reinterpret_cast<uintptr_t>(image_end) < boot_map.end) { |
| 260 | // Sanity check that we aren't trying to read a completely different boot image |
| 261 | os << "Remote boot map is out of range of local boot map: " << |
| 262 | "local begin " << reinterpret_cast<const void*>(image_begin) << |
| 263 | ", local end " << reinterpret_cast<const void*>(image_end) << |
| 264 | ", remote begin " << reinterpret_cast<const void*>(boot_map.start) << |
| 265 | ", remote end " << reinterpret_cast<const void*>(boot_map.end); |
| 266 | return false; |
| 267 | // If we wanted even more validation we could map the ImageHeader from the file |
| 268 | } |
| 269 | |
| 270 | std::vector<uint8_t> remote_contents(boot_map_size); |
| 271 | if (!map_file->PreadFully(&remote_contents[0], boot_map_size, boot_map.start)) { |
| 272 | os << "Could not fully read file " << file_name; |
| 273 | return false; |
| 274 | } |
| 275 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 276 | std::string page_map_file_name = StringPrintf( |
| 277 | "/proc/%ld/pagemap", static_cast<long>(image_diff_pid)); // NOLINT [runtime/int] |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 278 | auto page_map_file = std::unique_ptr<File>(OS::OpenFileForReading(page_map_file_name.c_str())); |
| 279 | if (page_map_file == nullptr) { |
| 280 | os << "Failed to open " << page_map_file_name << " for reading: " << strerror(errno); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough |
| 285 | const char* clean_page_map_file_name = "/proc/self/pagemap"; |
| 286 | auto clean_page_map_file = std::unique_ptr<File>( |
| 287 | OS::OpenFileForReading(clean_page_map_file_name)); |
| 288 | if (clean_page_map_file == nullptr) { |
| 289 | os << "Failed to open " << clean_page_map_file_name << " for reading: " << strerror(errno); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | auto kpage_flags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags")); |
| 294 | if (kpage_flags_file == nullptr) { |
| 295 | os << "Failed to open /proc/kpageflags for reading: " << strerror(errno); |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | auto kpage_count_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount")); |
| 300 | if (kpage_count_file == nullptr) { |
| 301 | os << "Failed to open /proc/kpagecount for reading:" << strerror(errno); |
| 302 | return false; |
| 303 | } |
| 304 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 305 | // Set of the remote virtual page indices that are dirty |
| 306 | std::set<size_t> dirty_page_set_remote; |
| 307 | // Set of the local virtual page indices that are dirty |
| 308 | std::set<size_t> dirty_page_set_local; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 309 | |
| 310 | size_t different_int32s = 0; |
| 311 | size_t different_bytes = 0; |
| 312 | size_t different_pages = 0; |
| 313 | size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address) |
| 314 | size_t page_idx = 0; // Page index relative to 0 |
| 315 | size_t previous_page_idx = 0; // Previous page index relative to 0 |
| 316 | size_t dirty_pages = 0; |
| 317 | size_t private_pages = 0; |
| 318 | size_t private_dirty_pages = 0; |
| 319 | |
| 320 | // Iterate through one page at a time. Boot map begin/end already implicitly aligned. |
| 321 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) { |
| 322 | ptrdiff_t offset = begin - boot_map.start; |
| 323 | |
| 324 | // We treat the image header as part of the memory map for now |
| 325 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 326 | // But it might still be interesting to see if any of the ImageHeader data mutated |
| 327 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset; |
| 328 | uint8_t* remote_ptr = &remote_contents[offset]; |
| 329 | |
| 330 | if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) { |
| 331 | different_pages++; |
| 332 | |
| 333 | // Count the number of 32-bit integers that are different. |
| 334 | for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) { |
| 335 | uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr); |
| 336 | const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr); |
| 337 | |
| 338 | if (remote_ptr_int32[i] != local_ptr_int32[i]) { |
| 339 | different_int32s++; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Iterate through one byte at a time. |
| 346 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) { |
| 347 | previous_page_idx = page_idx; |
| 348 | ptrdiff_t offset = begin - boot_map.start; |
| 349 | |
| 350 | // We treat the image header as part of the memory map for now |
| 351 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 352 | // But it might still be interesting to see if any of the ImageHeader data mutated |
| 353 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset; |
| 354 | uint8_t* remote_ptr = &remote_contents[offset]; |
| 355 | |
| 356 | virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize; |
| 357 | |
| 358 | // Calculate the page index, relative to the 0th page where the image begins |
| 359 | page_idx = (offset + page_off_begin) / kPageSize; |
| 360 | if (*local_ptr != *remote_ptr) { |
| 361 | // Track number of bytes that are different |
| 362 | different_bytes++; |
| 363 | } |
| 364 | |
| 365 | // Independently count the # of dirty pages on the remote side |
| 366 | size_t remote_virtual_page_idx = begin / kPageSize; |
| 367 | if (previous_page_idx != page_idx) { |
| 368 | uint64_t page_count = 0xC0FFEE; |
| 369 | // TODO: virtual_page_idx needs to be from the same process |
| 370 | int dirtiness = (IsPageDirty(page_map_file.get(), // Image-diff-pid procmap |
| 371 | clean_page_map_file.get(), // Self procmap |
| 372 | kpage_flags_file.get(), |
| 373 | kpage_count_file.get(), |
| 374 | remote_virtual_page_idx, // potentially "dirty" page |
| 375 | virtual_page_idx, // true "clean" page |
| 376 | &page_count, |
| 377 | &error_msg)); |
| 378 | if (dirtiness < 0) { |
| 379 | os << error_msg; |
| 380 | return false; |
| 381 | } else if (dirtiness > 0) { |
| 382 | dirty_pages++; |
| 383 | dirty_page_set_remote.insert(dirty_page_set_remote.end(), remote_virtual_page_idx); |
| 384 | dirty_page_set_local.insert(dirty_page_set_local.end(), virtual_page_idx); |
| 385 | } |
| 386 | |
| 387 | bool is_dirty = dirtiness > 0; |
| 388 | bool is_private = page_count == 1; |
| 389 | |
| 390 | if (page_count == 1) { |
| 391 | private_pages++; |
| 392 | } |
| 393 | |
| 394 | if (is_dirty && is_private) { |
| 395 | private_dirty_pages++; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 400 | std::map<mirror::Class*, ClassData> class_data; |
| 401 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 402 | // Walk each object in the remote image space and compare it against ours |
| 403 | size_t different_objects = 0; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 404 | |
| 405 | std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | std::vector<ArtMethod*> art_method_dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 407 | |
| 408 | std::map<off_t /* field offset */, int /* count */> class_field_dirty_count; |
| 409 | std::vector<mirror::Class*> class_dirty_objects; |
| 410 | |
| 411 | // List of local objects that are clean, but located on dirty pages. |
| 412 | std::vector<mirror::Object*> false_dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 413 | size_t false_dirty_object_bytes = 0; |
| 414 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 415 | // Look up remote classes by their descriptor |
| 416 | std::map<std::string, mirror::Class*> remote_class_map; |
| 417 | // Look up local classes by their descriptor |
| 418 | std::map<std::string, mirror::Class*> local_class_map; |
| 419 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 420 | std::unordered_set<mirror::Object*> dirty_objects; |
| 421 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 422 | size_t dirty_object_bytes = 0; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 423 | const uint8_t* begin_image_ptr = image_begin_unaligned; |
| 424 | const uint8_t* end_image_ptr = image_mirror_end_unaligned; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 425 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 426 | const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 427 | while (reinterpret_cast<uintptr_t>(current) < reinterpret_cast<uintptr_t>(end_image_ptr)) { |
| 428 | CHECK_ALIGNED(current, kObjectAlignment); |
| 429 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 430 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 431 | // Sanity check that we are reading a real object |
| 432 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
| 433 | if (kUseBakerOrBrooksReadBarrier) { |
| 434 | obj->AssertReadBarrierPointer(); |
| 435 | } |
| 436 | |
| 437 | // Iterate every page this object belongs to |
| 438 | bool on_dirty_page = false; |
| 439 | size_t page_off = 0; |
| 440 | size_t current_page_idx; |
| 441 | uintptr_t object_address; |
| 442 | do { |
| 443 | object_address = reinterpret_cast<uintptr_t>(current); |
| 444 | current_page_idx = object_address / kPageSize + page_off; |
| 445 | |
| 446 | if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) { |
| 447 | // This object is on a dirty page |
| 448 | on_dirty_page = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 451 | page_off++; |
| 452 | } while ((current_page_idx * kPageSize) < |
| 453 | RoundUp(object_address + obj->SizeOf(), kObjectAlignment)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 454 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 455 | mirror::Class* klass = obj->GetClass(); |
| 456 | |
| 457 | bool different_object = false; |
| 458 | |
| 459 | // Check against the other object and see if they are different |
| 460 | ptrdiff_t offset = current - begin_image_ptr; |
| 461 | const uint8_t* current_remote = &remote_contents[offset]; |
| 462 | mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>( |
| 463 | const_cast<uint8_t*>(current_remote)); |
| 464 | if (memcmp(current, current_remote, obj->SizeOf()) != 0) { |
| 465 | different_objects++; |
| 466 | dirty_object_bytes += obj->SizeOf(); |
| 467 | dirty_objects.insert(obj); |
| 468 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 469 | ++class_data[klass].dirty_object_count; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 470 | |
| 471 | // Go byte-by-byte and figure out what exactly got dirtied |
| 472 | size_t dirty_byte_count_per_object = 0; |
| 473 | for (size_t i = 0; i < obj->SizeOf(); ++i) { |
| 474 | if (current[i] != current_remote[i]) { |
| 475 | dirty_byte_count_per_object++; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 476 | } |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 477 | } |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 478 | class_data[klass].dirty_object_byte_count += dirty_byte_count_per_object; |
| 479 | class_data[klass].dirty_object_size_in_bytes += obj->SizeOf(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 480 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 481 | different_object = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 482 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 483 | class_data[klass].dirty_objects.push_back(remote_obj); |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 484 | } else { |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 485 | ++class_data[klass].clean_object_count; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 486 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 487 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 488 | std::string descriptor = GetClassDescriptor(klass); |
| 489 | if (different_object) { |
| 490 | if (klass->IsClassClass()) { |
| 491 | // this is a "Class" |
| 492 | mirror::Class* obj_as_class = reinterpret_cast<mirror::Class*>(remote_obj); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 493 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 494 | // print the fields that are dirty |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 495 | for (size_t i = 0; i < obj->SizeOf(); ++i) { |
| 496 | if (current[i] != current_remote[i]) { |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 497 | class_field_dirty_count[i]++; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 500 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 501 | class_dirty_objects.push_back(obj_as_class); |
| 502 | } else if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) { |
| 503 | // this is an ArtMethod |
| 504 | ArtMethod* art_method = reinterpret_cast<ArtMethod*>(remote_obj); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 505 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 506 | // print the fields that are dirty |
| 507 | for (size_t i = 0; i < obj->SizeOf(); ++i) { |
| 508 | if (current[i] != current_remote[i]) { |
| 509 | art_method_field_dirty_count[i]++; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 510 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 511 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 512 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 513 | art_method_dirty_objects.push_back(art_method); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 514 | } |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 515 | } else if (on_dirty_page) { |
| 516 | // This object was either never mutated or got mutated back to the same value. |
| 517 | // TODO: Do I want to distinguish a "different" vs a "dirty" page here? |
| 518 | false_dirty_objects.push_back(obj); |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 519 | class_data[klass].false_dirty_objects.push_back(obj); |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 520 | false_dirty_object_bytes += obj->SizeOf(); |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 521 | class_data[obj->GetClass()].false_dirty_byte_count += obj->SizeOf(); |
| 522 | class_data[obj->GetClass()].false_dirty_object_count += 1; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 523 | } |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 524 | |
| 525 | if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) { |
| 526 | local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj); |
| 527 | remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj); |
| 528 | } |
| 529 | |
| 530 | // Unconditionally store the class descriptor in case we need it later |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 531 | class_data[klass].descriptor = descriptor; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 532 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects. |
| 536 | float true_dirtied_percent = dirty_object_bytes * 1.0f / (dirty_pages * kPageSize); |
| 537 | size_t false_dirty_pages = dirty_pages - different_pages; |
| 538 | |
| 539 | os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", " |
| 540 | << reinterpret_cast<void*>(boot_map.end) << ") had: \n " |
| 541 | << different_bytes << " differing bytes, \n " |
| 542 | << different_int32s << " differing int32s, \n " |
| 543 | << different_objects << " different objects, \n " |
| 544 | << dirty_object_bytes << " different object [bytes], \n " |
| 545 | << false_dirty_objects.size() << " false dirty objects,\n " |
| 546 | << false_dirty_object_bytes << " false dirty object [bytes], \n " |
| 547 | << true_dirtied_percent << " different objects-vs-total in a dirty page;\n " |
| 548 | << different_pages << " different pages; \n " |
| 549 | << dirty_pages << " pages are dirty; \n " |
| 550 | << false_dirty_pages << " pages are false dirty; \n " |
| 551 | << private_pages << " pages are private; \n " |
| 552 | << private_dirty_pages << " pages are Private_Dirty\n " |
| 553 | << ""; |
| 554 | |
| 555 | // vector of pairs (int count, Class*) |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 556 | auto dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>( |
| 557 | class_data, [](const ClassData& d) { return d.dirty_object_count; }); |
| 558 | auto clean_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>( |
| 559 | class_data, [](const ClassData& d) { return d.clean_object_count; }); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 560 | |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 561 | os << "\n" << " Dirty objects: " << dirty_objects.size() << "\n"; |
| 562 | for (mirror::Object* obj : dirty_objects) { |
| 563 | const char* tabs = " "; |
| 564 | // Attempt to find fields for all dirty bytes. |
| 565 | mirror::Class* klass = obj->GetClass(); |
| 566 | if (obj->IsClass()) { |
| 567 | os << tabs << "Class " << PrettyClass(obj->AsClass()) << " " << obj << "\n"; |
| 568 | } else { |
| 569 | os << tabs << "Instance of " << PrettyClass(klass) << " " << obj << "\n"; |
| 570 | } |
| 571 | |
| 572 | std::unordered_set<ArtField*> dirty_instance_fields; |
| 573 | std::unordered_set<ArtField*> dirty_static_fields; |
| 574 | const uint8_t* obj_bytes = reinterpret_cast<const uint8_t*>(obj); |
| 575 | ptrdiff_t offset = obj_bytes - begin_image_ptr; |
| 576 | uint8_t* remote_bytes = &remote_contents[offset]; |
| 577 | mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(remote_bytes); |
| 578 | for (size_t i = 0, count = obj->SizeOf(); i < count; ++i) { |
| 579 | if (obj_bytes[i] != remote_bytes[i]) { |
| 580 | ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i); |
| 581 | if (field != nullptr) { |
| 582 | dirty_instance_fields.insert(field); |
| 583 | } else if (obj->IsClass()) { |
| 584 | field = ArtField::FindStaticFieldWithOffset</*exact*/false>(obj->AsClass(), i); |
| 585 | if (field != nullptr) { |
| 586 | dirty_static_fields.insert(field); |
| 587 | } |
| 588 | } |
| 589 | if (field == nullptr) { |
| 590 | if (klass->IsArrayClass()) { |
| 591 | mirror::Class* component_type = klass->GetComponentType(); |
| 592 | Primitive::Type primitive_type = component_type->GetPrimitiveType(); |
| 593 | size_t component_size = Primitive::ComponentSize(primitive_type); |
| 594 | size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
| 595 | if (i >= data_offset) { |
| 596 | os << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n"; |
| 597 | // Skip to next element to prevent spam. |
| 598 | i += component_size - 1; |
| 599 | continue; |
| 600 | } |
| 601 | } |
| 602 | os << tabs << "No field for byte offset " << i << "\n"; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | // Dump different fields. TODO: Dump field contents. |
| 607 | if (!dirty_instance_fields.empty()) { |
| 608 | os << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n"; |
| 609 | for (ArtField* field : dirty_instance_fields) { |
| 610 | os << tabs << PrettyField(field) |
| 611 | << " original=" << PrettyFieldValue(field, obj) |
| 612 | << " remote=" << PrettyFieldValue(field, remote_obj) << "\n"; |
| 613 | } |
| 614 | } |
| 615 | if (!dirty_static_fields.empty()) { |
| 616 | os << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n"; |
| 617 | for (ArtField* field : dirty_static_fields) { |
| 618 | os << tabs << PrettyField(field) |
| 619 | << " original=" << PrettyFieldValue(field, obj) |
| 620 | << " remote=" << PrettyFieldValue(field, remote_obj) << "\n"; |
| 621 | } |
| 622 | } |
| 623 | os << "\n"; |
| 624 | } |
| 625 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 626 | os << "\n" << " Dirty object count by class:\n"; |
| 627 | for (const auto& vk_pair : dirty_object_class_values) { |
| 628 | int dirty_object_count = vk_pair.first; |
| 629 | mirror::Class* klass = vk_pair.second; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 630 | int object_sizes = class_data[klass].dirty_object_size_in_bytes; |
| 631 | float avg_dirty_bytes_per_class = |
| 632 | class_data[klass].dirty_object_byte_count * 1.0f / object_sizes; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 633 | float avg_object_size = object_sizes * 1.0f / dirty_object_count; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 634 | const std::string& descriptor = class_data[klass].descriptor; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 635 | os << " " << PrettyClass(klass) << " (" |
| 636 | << "objects: " << dirty_object_count << ", " |
| 637 | << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", " |
| 638 | << "avg object size: " << avg_object_size << ", " |
| 639 | << "class descriptor: '" << descriptor << "'" |
| 640 | << ")\n"; |
| 641 | |
| 642 | constexpr size_t kMaxAddressPrint = 5; |
| 643 | if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) { |
| 644 | os << " sample object addresses: "; |
| 645 | for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) { |
| 646 | auto art_method = art_method_dirty_objects[i]; |
| 647 | |
| 648 | os << reinterpret_cast<void*>(art_method) << ", "; |
| 649 | } |
| 650 | os << "\n"; |
| 651 | |
| 652 | os << " dirty byte +offset:count list = "; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 653 | auto art_method_field_dirty_count_sorted = |
| 654 | SortByValueDesc<off_t, int, int>(art_method_field_dirty_count); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 655 | for (auto pair : art_method_field_dirty_count_sorted) { |
| 656 | off_t offset = pair.second; |
| 657 | int count = pair.first; |
| 658 | |
| 659 | os << "+" << offset << ":" << count << ", "; |
| 660 | } |
| 661 | |
| 662 | os << "\n"; |
| 663 | |
| 664 | os << " field contents:\n"; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 665 | const auto& dirty_objects_list = class_data[klass].dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 666 | for (mirror::Object* obj : dirty_objects_list) { |
| 667 | // remote method |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 668 | auto art_method = reinterpret_cast<ArtMethod*>(obj); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 669 | |
| 670 | // remote class |
| 671 | mirror::Class* remote_declaring_class = |
| 672 | FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents, boot_map); |
| 673 | |
| 674 | // local class |
| 675 | mirror::Class* declaring_class = |
| 676 | RemoteContentsPointerToLocal(remote_declaring_class, |
| 677 | remote_contents, |
| 678 | boot_image_header); |
| 679 | |
| 680 | os << " " << reinterpret_cast<void*>(obj) << " "; |
| 681 | os << " entryPointFromJni: " |
| 682 | << reinterpret_cast<const void*>( |
| 683 | art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 684 | os << " entryPointFromQuickCompiledCode: " |
| 685 | << reinterpret_cast<const void*>( |
| 686 | art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)) |
| 687 | << ", "; |
| 688 | os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", "; |
| 689 | os << " class_status (local): " << declaring_class->GetStatus(); |
| 690 | os << " class_status (remote): " << remote_declaring_class->GetStatus(); |
| 691 | os << "\n"; |
| 692 | } |
| 693 | } |
| 694 | if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) { |
| 695 | os << " sample object addresses: "; |
| 696 | for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) { |
| 697 | auto class_ptr = class_dirty_objects[i]; |
| 698 | |
| 699 | os << reinterpret_cast<void*>(class_ptr) << ", "; |
| 700 | } |
| 701 | os << "\n"; |
| 702 | |
| 703 | os << " dirty byte +offset:count list = "; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 704 | auto class_field_dirty_count_sorted = |
| 705 | SortByValueDesc<off_t, int, int>(class_field_dirty_count); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 706 | for (auto pair : class_field_dirty_count_sorted) { |
| 707 | off_t offset = pair.second; |
| 708 | int count = pair.first; |
| 709 | |
| 710 | os << "+" << offset << ":" << count << ", "; |
| 711 | } |
| 712 | os << "\n"; |
| 713 | |
| 714 | os << " field contents:\n"; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 715 | const auto& dirty_objects_list = class_data[klass].dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 716 | for (mirror::Object* obj : dirty_objects_list) { |
| 717 | // remote class object |
| 718 | auto remote_klass = reinterpret_cast<mirror::Class*>(obj); |
| 719 | |
| 720 | // local class object |
| 721 | auto local_klass = RemoteContentsPointerToLocal(remote_klass, |
| 722 | remote_contents, |
| 723 | boot_image_header); |
| 724 | |
| 725 | os << " " << reinterpret_cast<void*>(obj) << " "; |
| 726 | os << " class_status (remote): " << remote_klass->GetStatus() << ", "; |
| 727 | os << " class_status (local): " << local_klass->GetStatus(); |
| 728 | os << "\n"; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 733 | auto false_dirty_object_class_values = SortByValueDesc<mirror::Class*, int, ClassData>( |
| 734 | class_data, [](const ClassData& d) { return d.false_dirty_object_count; }); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 735 | |
| 736 | os << "\n" << " False-dirty object count by class:\n"; |
| 737 | for (const auto& vk_pair : false_dirty_object_class_values) { |
| 738 | int object_count = vk_pair.first; |
| 739 | mirror::Class* klass = vk_pair.second; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 740 | int object_sizes = class_data[klass].false_dirty_byte_count; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 741 | float avg_object_size = object_sizes * 1.0f / object_count; |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 742 | const std::string& descriptor = class_data[klass].descriptor; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 743 | os << " " << PrettyClass(klass) << " (" |
| 744 | << "objects: " << object_count << ", " |
| 745 | << "avg object size: " << avg_object_size << ", " |
| 746 | << "total bytes: " << object_sizes << ", " |
| 747 | << "class descriptor: '" << descriptor << "'" |
| 748 | << ")\n"; |
| 749 | |
| 750 | if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) { |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 751 | auto& art_method_false_dirty_objects = class_data[klass].false_dirty_objects; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 752 | |
| 753 | os << " field contents:\n"; |
| 754 | for (mirror::Object* obj : art_method_false_dirty_objects) { |
| 755 | // local method |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 756 | auto art_method = reinterpret_cast<ArtMethod*>(obj); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 757 | |
| 758 | // local class |
| 759 | mirror::Class* declaring_class = art_method->GetDeclaringClass(); |
| 760 | |
| 761 | os << " " << reinterpret_cast<void*>(obj) << " "; |
| 762 | os << " entryPointFromJni: " |
| 763 | << reinterpret_cast<const void*>( |
| 764 | art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", "; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 765 | os << " entryPointFromQuickCompiledCode: " |
| 766 | << reinterpret_cast<const void*>( |
| 767 | art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)) |
| 768 | << ", "; |
| 769 | os << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", "; |
| 770 | os << " class_status (local): " << declaring_class->GetStatus(); |
| 771 | os << "\n"; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | os << "\n" << " Clean object count by class:\n"; |
| 777 | for (const auto& vk_pair : clean_object_class_values) { |
| 778 | os << " " << PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n"; |
| 779 | } |
| 780 | |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory. |
| 785 | // Returned pointer will point to inside of remote_contents. |
| 786 | template <typename T> |
| 787 | static T* FixUpRemotePointer(T* remote_ptr, |
| 788 | std::vector<uint8_t>& remote_contents, |
| 789 | const backtrace_map_t& boot_map) { |
| 790 | if (remote_ptr == nullptr) { |
| 791 | return nullptr; |
| 792 | } |
| 793 | |
| 794 | uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr); |
| 795 | |
| 796 | CHECK_LE(boot_map.start, remote); |
| 797 | CHECK_GT(boot_map.end, remote); |
| 798 | |
| 799 | off_t boot_offset = remote - boot_map.start; |
| 800 | |
| 801 | return reinterpret_cast<T*>(&remote_contents[boot_offset]); |
| 802 | } |
| 803 | |
| 804 | template <typename T> |
| 805 | static T* RemoteContentsPointerToLocal(T* remote_ptr, |
| 806 | std::vector<uint8_t>& remote_contents, |
| 807 | const ImageHeader& image_header) { |
| 808 | if (remote_ptr == nullptr) { |
| 809 | return nullptr; |
| 810 | } |
| 811 | |
| 812 | uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr); |
| 813 | ptrdiff_t boot_offset = remote - &remote_contents[0]; |
| 814 | |
| 815 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset; |
| 816 | |
| 817 | return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr)); |
| 818 | } |
| 819 | |
| 820 | static std::string GetClassDescriptor(mirror::Class* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 821 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 822 | CHECK(klass != nullptr); |
| 823 | |
| 824 | std::string descriptor; |
| 825 | const char* descriptor_str = klass->GetDescriptor(&descriptor); |
| 826 | |
| 827 | return std::string(descriptor_str); |
| 828 | } |
| 829 | |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 830 | template <typename K, typename V, typename D> |
| 831 | static std::vector<std::pair<V, K>> SortByValueDesc( |
| 832 | const std::map<K, D> map, |
| 833 | std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 834 | // Store value->key so that we can use the default sort from pair which |
| 835 | // sorts by value first and then key |
| 836 | std::vector<std::pair<V, K>> value_key_vector; |
| 837 | |
| 838 | for (const auto& kv_pair : map) { |
Andreas Gampe | 7ad71d0 | 2016-04-04 13:49:18 -0700 | [diff] [blame^] | 839 | value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | // Sort in reverse (descending order) |
| 843 | std::sort(value_key_vector.rbegin(), value_key_vector.rend()); |
| 844 | return value_key_vector; |
| 845 | } |
| 846 | |
| 847 | static bool GetPageFrameNumber(File* page_map_file, |
| 848 | size_t virtual_page_index, |
| 849 | uint64_t* page_frame_number, |
| 850 | std::string* error_msg) { |
| 851 | CHECK(page_map_file != nullptr); |
| 852 | CHECK(page_frame_number != nullptr); |
| 853 | CHECK(error_msg != nullptr); |
| 854 | |
| 855 | constexpr size_t kPageMapEntrySize = sizeof(uint64_t); |
| 856 | constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; // bits 0-54 [in /proc/$pid/pagemap] |
| 857 | constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55); // bit 55 [in /proc/$pid/pagemap] |
| 858 | |
| 859 | uint64_t page_map_entry = 0; |
| 860 | |
| 861 | // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number |
| 862 | if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize, |
| 863 | virtual_page_index * kPageMapEntrySize)) { |
| 864 | *error_msg = StringPrintf("Failed to read the virtual page index entry from %s", |
| 865 | page_map_file->GetPath().c_str()); |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | // TODO: seems useless, remove this. |
| 870 | bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0; |
| 871 | if ((false)) { |
| 872 | LOG(VERBOSE) << soft_dirty; // Suppress unused warning |
| 873 | UNREACHABLE(); |
| 874 | } |
| 875 | |
| 876 | *page_frame_number = page_map_entry & kPageFrameNumberMask; |
| 877 | |
| 878 | return true; |
| 879 | } |
| 880 | |
| 881 | static int IsPageDirty(File* page_map_file, |
| 882 | File* clean_page_map_file, |
| 883 | File* kpage_flags_file, |
| 884 | File* kpage_count_file, |
| 885 | size_t virtual_page_idx, |
| 886 | size_t clean_virtual_page_idx, |
| 887 | // Out parameters: |
| 888 | uint64_t* page_count, std::string* error_msg) { |
| 889 | CHECK(page_map_file != nullptr); |
| 890 | CHECK(clean_page_map_file != nullptr); |
| 891 | CHECK_NE(page_map_file, clean_page_map_file); |
| 892 | CHECK(kpage_flags_file != nullptr); |
| 893 | CHECK(kpage_count_file != nullptr); |
| 894 | CHECK(page_count != nullptr); |
| 895 | CHECK(error_msg != nullptr); |
| 896 | |
| 897 | // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt |
| 898 | |
| 899 | constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t); |
| 900 | constexpr size_t kPageCountEntrySize = sizeof(uint64_t); |
| 901 | constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags |
| 902 | constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags |
| 903 | constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags |
| 904 | |
| 905 | uint64_t page_frame_number = 0; |
| 906 | if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) { |
| 907 | return -1; |
| 908 | } |
| 909 | |
| 910 | uint64_t page_frame_number_clean = 0; |
| 911 | if (!GetPageFrameNumber(clean_page_map_file, clean_virtual_page_idx, &page_frame_number_clean, |
| 912 | error_msg)) { |
| 913 | return -1; |
| 914 | } |
| 915 | |
| 916 | // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page |
| 917 | uint64_t kpage_flags_entry = 0; |
| 918 | if (!kpage_flags_file->PreadFully(&kpage_flags_entry, |
| 919 | kPageFlagsEntrySize, |
| 920 | page_frame_number * kPageFlagsEntrySize)) { |
| 921 | *error_msg = StringPrintf("Failed to read the page flags from %s", |
| 922 | kpage_flags_file->GetPath().c_str()); |
| 923 | return -1; |
| 924 | } |
| 925 | |
| 926 | // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page |
| 927 | if (!kpage_count_file->PreadFully(page_count /*out*/, |
| 928 | kPageCountEntrySize, |
| 929 | page_frame_number * kPageCountEntrySize)) { |
| 930 | *error_msg = StringPrintf("Failed to read the page count from %s", |
| 931 | kpage_count_file->GetPath().c_str()); |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | // There must be a page frame at the requested address. |
| 936 | CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u); |
| 937 | // The page frame must be memory mapped |
| 938 | CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u); |
| 939 | |
| 940 | // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1 |
| 941 | bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0; |
| 942 | |
| 943 | // page_frame_number_clean must come from the *same* process |
| 944 | // but a *different* mmap than page_frame_number |
| 945 | if (flags_dirty) { |
| 946 | CHECK_NE(page_frame_number, page_frame_number_clean); |
| 947 | } |
| 948 | |
| 949 | return page_frame_number != page_frame_number_clean; |
| 950 | } |
| 951 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 952 | private: |
| 953 | // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art" |
| 954 | std::string GetImageLocationBaseName() const { |
| 955 | return BaseName(std::string(image_location_)); |
| 956 | } |
| 957 | |
| 958 | std::ostream* os_; |
| 959 | const ImageHeader& image_header_; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 960 | const std::string image_location_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 961 | pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative |
| 962 | |
| 963 | DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper); |
| 964 | }; |
| 965 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 966 | static int DumpImage(Runtime* runtime, std::ostream* os, pid_t image_diff_pid) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 967 | ScopedObjectAccess soa(Thread::Current()); |
| 968 | gc::Heap* heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 969 | std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces(); |
| 970 | CHECK(!image_spaces.empty()); |
| 971 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 972 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 973 | if (!image_header.IsValid()) { |
| 974 | fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str()); |
| 975 | return EXIT_FAILURE; |
| 976 | } |
| 977 | |
| 978 | ImgDiagDumper img_diag_dumper( |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 979 | os, image_header, image_space->GetImageLocation(), image_diff_pid); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 980 | if (!img_diag_dumper.Dump()) { |
| 981 | return EXIT_FAILURE; |
| 982 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 983 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 984 | return EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | struct ImgDiagArgs : public CmdlineArgs { |
| 988 | protected: |
| 989 | using Base = CmdlineArgs; |
| 990 | |
| 991 | virtual ParseStatus ParseCustom(const StringPiece& option, |
| 992 | std::string* error_msg) OVERRIDE { |
| 993 | { |
| 994 | ParseStatus base_parse = Base::ParseCustom(option, error_msg); |
| 995 | if (base_parse != kParseUnknownArgument) { |
| 996 | return base_parse; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | if (option.starts_with("--image-diff-pid=")) { |
| 1001 | const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data(); |
| 1002 | |
| 1003 | if (!ParseInt(image_diff_pid, &image_diff_pid_)) { |
| 1004 | *error_msg = "Image diff pid out of range"; |
| 1005 | return kParseError; |
| 1006 | } |
| 1007 | } else { |
| 1008 | return kParseUnknownArgument; |
| 1009 | } |
| 1010 | |
| 1011 | return kParseOk; |
| 1012 | } |
| 1013 | |
| 1014 | virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE { |
| 1015 | // Perform the parent checks. |
| 1016 | ParseStatus parent_checks = Base::ParseChecks(error_msg); |
| 1017 | if (parent_checks != kParseOk) { |
| 1018 | return parent_checks; |
| 1019 | } |
| 1020 | |
| 1021 | // Perform our own checks. |
| 1022 | |
| 1023 | if (kill(image_diff_pid_, |
| 1024 | /*sig*/0) != 0) { // No signal is sent, perform error-checking only. |
| 1025 | // Check if the pid exists before proceeding. |
| 1026 | if (errno == ESRCH) { |
| 1027 | *error_msg = "Process specified does not exist"; |
| 1028 | } else { |
| 1029 | *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno)); |
| 1030 | } |
| 1031 | return kParseError; |
| 1032 | } else if (instruction_set_ != kRuntimeISA) { |
| 1033 | // Don't allow different ISAs since the images are ISA-specific. |
| 1034 | // Right now the code assumes both the runtime ISA and the remote ISA are identical. |
| 1035 | *error_msg = "Must use the default runtime ISA; changing ISA is not supported."; |
| 1036 | return kParseError; |
| 1037 | } |
| 1038 | |
| 1039 | return kParseOk; |
| 1040 | } |
| 1041 | |
| 1042 | virtual std::string GetUsage() const { |
| 1043 | std::string usage; |
| 1044 | |
| 1045 | usage += |
| 1046 | "Usage: imgdiag [options] ...\n" |
| 1047 | " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n" |
| 1048 | " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n" |
| 1049 | "\n"; |
| 1050 | |
| 1051 | usage += Base::GetUsage(); |
| 1052 | |
| 1053 | usage += // Optional. |
| 1054 | " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n" |
| 1055 | " Example: --image-diff-pid=$(pid zygote)\n" |
| 1056 | "\n"; |
| 1057 | |
| 1058 | return usage; |
| 1059 | } |
| 1060 | |
| 1061 | public: |
| 1062 | pid_t image_diff_pid_ = -1; |
| 1063 | }; |
| 1064 | |
| 1065 | struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> { |
| 1066 | virtual bool ExecuteWithRuntime(Runtime* runtime) { |
| 1067 | CHECK(args_ != nullptr); |
| 1068 | |
| 1069 | return DumpImage(runtime, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1070 | args_->os_, |
| 1071 | args_->image_diff_pid_) == EXIT_SUCCESS; |
| 1072 | } |
| 1073 | }; |
| 1074 | |
| 1075 | } // namespace art |
| 1076 | |
| 1077 | int main(int argc, char** argv) { |
| 1078 | art::ImgDiagMain main; |
| 1079 | return main.Main(argc, argv); |
| 1080 | } |