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> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 23 | #include <map> |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 24 | #include <optional> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <string> |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 27 | #include <unordered_set> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include <vector> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 29 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 30 | #include <android-base/parseint.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 31 | #include "android-base/stringprintf.h" |
| 32 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 33 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | #include "art_method-inl.h" |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 35 | #include "base/array_ref.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 36 | #include "base/os.h" |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 37 | #include "base/string_view_cpp20.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 38 | #include "base/unix_file/fd_file.h" |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 39 | #include "class_linker.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 40 | #include "gc/heap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 41 | #include "gc/space/image_space.h" |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 42 | #include "image-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 43 | #include "mirror/class-inl.h" |
| 44 | #include "mirror/object-inl.h" |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 45 | #include "oat.h" |
| 46 | #include "oat_file.h" |
| 47 | #include "oat_file_manager.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 48 | #include "scoped_thread_state_change-inl.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 49 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 50 | #include "backtrace/BacktraceMap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 51 | #include "cmdline.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 52 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 53 | #include <signal.h> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 54 | #include <sys/stat.h> |
| 55 | #include <sys/types.h> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 56 | |
| 57 | namespace art { |
| 58 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 59 | using android::base::StringPrintf; |
| 60 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | constexpr size_t kMaxAddressPrint = 5; |
| 64 | |
| 65 | enum class ProcessType { |
| 66 | kZygote, |
| 67 | kRemote |
| 68 | }; |
| 69 | |
| 70 | enum class RemoteProcesses { |
| 71 | kImageOnly, |
| 72 | kZygoteOnly, |
| 73 | kImageAndZygote |
| 74 | }; |
| 75 | |
| 76 | struct MappingData { |
| 77 | // The count of pages that are considered dirty by the OS. |
| 78 | size_t dirty_pages = 0; |
| 79 | // The count of pages that differ by at least one byte. |
| 80 | size_t different_pages = 0; |
| 81 | // The count of differing bytes. |
| 82 | size_t different_bytes = 0; |
| 83 | // The count of differing four-byte units. |
| 84 | size_t different_int32s = 0; |
| 85 | // The count of pages that have mapping count == 1. |
| 86 | size_t private_pages = 0; |
| 87 | // The count of private pages that are also dirty. |
| 88 | size_t private_dirty_pages = 0; |
| 89 | // The count of pages that are marked dirty but do not differ. |
| 90 | size_t false_dirty_pages = 0; |
| 91 | // Set of the local virtual page indices that are dirty. |
| 92 | std::set<size_t> dirty_page_set; |
| 93 | }; |
| 94 | |
| 95 | static std::string GetClassDescriptor(mirror::Class* klass) |
| 96 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 97 | CHECK(klass != nullptr); |
| 98 | |
| 99 | std::string descriptor; |
| 100 | const char* descriptor_str = klass->GetDescriptor(&descriptor /*out*/); |
| 101 | |
| 102 | return std::string(descriptor_str); |
| 103 | } |
| 104 | |
| 105 | static std::string PrettyFieldValue(ArtField* field, mirror::Object* object) |
| 106 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 107 | std::ostringstream oss; |
| 108 | switch (field->GetTypeAsPrimitiveType()) { |
| 109 | case Primitive::kPrimNot: { |
| 110 | oss << object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 111 | field->GetOffset()); |
| 112 | break; |
| 113 | } |
| 114 | case Primitive::kPrimBoolean: { |
| 115 | oss << static_cast<bool>(object->GetFieldBoolean<kVerifyNone>(field->GetOffset())); |
| 116 | break; |
| 117 | } |
| 118 | case Primitive::kPrimByte: { |
| 119 | oss << static_cast<int32_t>(object->GetFieldByte<kVerifyNone>(field->GetOffset())); |
| 120 | break; |
| 121 | } |
| 122 | case Primitive::kPrimChar: { |
| 123 | oss << object->GetFieldChar<kVerifyNone>(field->GetOffset()); |
| 124 | break; |
| 125 | } |
| 126 | case Primitive::kPrimShort: { |
| 127 | oss << object->GetFieldShort<kVerifyNone>(field->GetOffset()); |
| 128 | break; |
| 129 | } |
| 130 | case Primitive::kPrimInt: { |
| 131 | oss << object->GetField32<kVerifyNone>(field->GetOffset()); |
| 132 | break; |
| 133 | } |
| 134 | case Primitive::kPrimLong: { |
| 135 | oss << object->GetField64<kVerifyNone>(field->GetOffset()); |
| 136 | break; |
| 137 | } |
| 138 | case Primitive::kPrimFloat: { |
| 139 | oss << object->GetField32<kVerifyNone>(field->GetOffset()); |
| 140 | break; |
| 141 | } |
| 142 | case Primitive::kPrimDouble: { |
| 143 | oss << object->GetField64<kVerifyNone>(field->GetOffset()); |
| 144 | break; |
| 145 | } |
| 146 | case Primitive::kPrimVoid: { |
| 147 | oss << "void"; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | return oss.str(); |
| 152 | } |
| 153 | |
| 154 | template <typename K, typename V, typename D> |
| 155 | static std::vector<std::pair<V, K>> SortByValueDesc( |
| 156 | const std::map<K, D> map, |
| 157 | std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) { |
| 158 | // Store value->key so that we can use the default sort from pair which |
| 159 | // sorts by value first and then key |
| 160 | std::vector<std::pair<V, K>> value_key_vector; |
| 161 | |
| 162 | for (const auto& kv_pair : map) { |
| 163 | value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first)); |
| 164 | } |
| 165 | |
| 166 | // Sort in reverse (descending order) |
| 167 | std::sort(value_key_vector.rbegin(), value_key_vector.rend()); |
| 168 | return value_key_vector; |
| 169 | } |
| 170 | |
| 171 | // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory. |
| 172 | // Returned pointer will point to inside of remote_contents. |
| 173 | template <typename T> |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 174 | static ObjPtr<T> FixUpRemotePointer(ObjPtr<T> remote_ptr, |
| 175 | std::vector<uint8_t>& remote_contents, |
| 176 | const backtrace_map_t& boot_map) |
| 177 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 178 | if (remote_ptr == nullptr) { |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 182 | uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr.Ptr()); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 183 | |
Mathieu Chartier | 21f7ac1 | 2018-07-09 16:18:27 -0700 | [diff] [blame] | 184 | // In the case the remote pointer is out of range, it probably belongs to another image. |
| 185 | // Just return null for this case. |
| 186 | if (remote < boot_map.start || remote >= boot_map.end) { |
| 187 | return nullptr; |
| 188 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 189 | |
| 190 | off_t boot_offset = remote - boot_map.start; |
| 191 | |
| 192 | return reinterpret_cast<T*>(&remote_contents[boot_offset]); |
| 193 | } |
| 194 | |
| 195 | template <typename T> |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 196 | static ObjPtr<T> RemoteContentsPointerToLocal(ObjPtr<T> remote_ptr, |
| 197 | std::vector<uint8_t>& remote_contents, |
| 198 | const ImageHeader& image_header) |
| 199 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 200 | if (remote_ptr == nullptr) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 204 | uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr.Ptr()); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 205 | ptrdiff_t boot_offset = remote - &remote_contents[0]; |
| 206 | |
| 207 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset; |
| 208 | |
| 209 | return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr)); |
| 210 | } |
| 211 | |
| 212 | template <typename T> size_t EntrySize(T* entry); |
| 213 | template<> size_t EntrySize(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 214 | return object->SizeOf(); |
| 215 | } |
| 216 | template<> size_t EntrySize(ArtMethod* art_method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 217 | return sizeof(*art_method); |
| 218 | } |
| 219 | |
| 220 | template <typename T> |
| 221 | static bool EntriesDiffer(T* entry1, T* entry2) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 222 | return memcmp(entry1, entry2, EntrySize(entry1)) != 0; |
| 223 | } |
| 224 | |
| 225 | template <typename T> |
| 226 | struct RegionCommon { |
| 227 | public: |
| 228 | RegionCommon(std::ostream* os, |
| 229 | std::vector<uint8_t>* remote_contents, |
| 230 | std::vector<uint8_t>* zygote_contents, |
| 231 | const backtrace_map_t& boot_map, |
| 232 | const ImageHeader& image_header) : |
| 233 | os_(*os), |
| 234 | remote_contents_(remote_contents), |
| 235 | zygote_contents_(zygote_contents), |
| 236 | boot_map_(boot_map), |
| 237 | image_header_(image_header), |
| 238 | different_entries_(0), |
| 239 | dirty_entry_bytes_(0), |
| 240 | false_dirty_entry_bytes_(0) { |
| 241 | CHECK(remote_contents != nullptr); |
| 242 | CHECK(zygote_contents != nullptr); |
| 243 | } |
| 244 | |
| 245 | void DumpSamplesAndOffsetCount() { |
| 246 | os_ << " sample object addresses: "; |
| 247 | for (size_t i = 0; i < dirty_entries_.size() && i < kMaxAddressPrint; ++i) { |
| 248 | T* entry = dirty_entries_[i]; |
| 249 | os_ << reinterpret_cast<void*>(entry) << ", "; |
| 250 | } |
| 251 | os_ << "\n"; |
| 252 | os_ << " dirty byte +offset:count list = "; |
| 253 | std::vector<std::pair<size_t, off_t>> field_dirty_count_sorted = |
| 254 | SortByValueDesc<off_t, size_t, size_t>(field_dirty_count_); |
| 255 | for (const std::pair<size_t, off_t>& pair : field_dirty_count_sorted) { |
| 256 | off_t offset = pair.second; |
| 257 | size_t count = pair.first; |
| 258 | os_ << "+" << offset << ":" << count << ", "; |
| 259 | } |
| 260 | os_ << "\n"; |
| 261 | } |
| 262 | |
| 263 | size_t GetDifferentEntryCount() const { return different_entries_; } |
| 264 | size_t GetDirtyEntryBytes() const { return dirty_entry_bytes_; } |
| 265 | size_t GetFalseDirtyEntryCount() const { return false_dirty_entries_.size(); } |
| 266 | size_t GetFalseDirtyEntryBytes() const { return false_dirty_entry_bytes_; } |
| 267 | size_t GetZygoteDirtyEntryCount() const { return zygote_dirty_entries_.size(); } |
| 268 | |
| 269 | protected: |
| 270 | bool IsEntryOnDirtyPage(T* entry, const std::set<size_t>& dirty_pages) const |
| 271 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 272 | size_t size = EntrySize(entry); |
| 273 | size_t page_off = 0; |
| 274 | size_t current_page_idx; |
| 275 | uintptr_t entry_address = reinterpret_cast<uintptr_t>(entry); |
| 276 | // Iterate every page this entry belongs to |
| 277 | do { |
| 278 | current_page_idx = entry_address / kPageSize + page_off; |
| 279 | if (dirty_pages.find(current_page_idx) != dirty_pages.end()) { |
| 280 | // This entry is on a dirty page |
| 281 | return true; |
| 282 | } |
| 283 | page_off++; |
| 284 | } while ((current_page_idx * kPageSize) < RoundUp(entry_address + size, kObjectAlignment)); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | void AddZygoteDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 289 | zygote_dirty_entries_.insert(entry); |
| 290 | } |
| 291 | |
| 292 | void AddImageDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 293 | image_dirty_entries_.insert(entry); |
| 294 | } |
| 295 | |
| 296 | void AddFalseDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 297 | false_dirty_entries_.push_back(entry); |
| 298 | false_dirty_entry_bytes_ += EntrySize(entry); |
| 299 | } |
| 300 | |
| 301 | // The output stream to write to. |
| 302 | std::ostream& os_; |
| 303 | // The byte contents of the remote (image) process' image. |
| 304 | std::vector<uint8_t>* remote_contents_; |
| 305 | // The byte contents of the zygote process' image. |
| 306 | std::vector<uint8_t>* zygote_contents_; |
| 307 | const backtrace_map_t& boot_map_; |
| 308 | const ImageHeader& image_header_; |
| 309 | |
| 310 | // Count of entries that are different. |
| 311 | size_t different_entries_; |
| 312 | |
| 313 | // Local entries that are dirty (differ in at least one byte). |
| 314 | size_t dirty_entry_bytes_; |
| 315 | std::vector<T*> dirty_entries_; |
| 316 | |
| 317 | // Local entries that are clean, but located on dirty pages. |
| 318 | size_t false_dirty_entry_bytes_; |
| 319 | std::vector<T*> false_dirty_entries_; |
| 320 | |
| 321 | // Image dirty entries |
| 322 | // If zygote_pid_only_ == true, these are shared dirty entries in the zygote. |
| 323 | // If zygote_pid_only_ == false, these are private dirty entries in the application. |
| 324 | std::set<T*> image_dirty_entries_; |
| 325 | |
| 326 | // Zygote dirty entries (probably private dirty). |
| 327 | // We only add entries here if they differed in both the image and the zygote, so |
| 328 | // they are probably private dirty. |
| 329 | std::set<T*> zygote_dirty_entries_; |
| 330 | |
| 331 | std::map<off_t /* field offset */, size_t /* count */> field_dirty_count_; |
| 332 | |
| 333 | private: |
| 334 | DISALLOW_COPY_AND_ASSIGN(RegionCommon); |
| 335 | }; |
| 336 | |
| 337 | template <typename T> |
| 338 | class RegionSpecializedBase : public RegionCommon<T> { |
| 339 | }; |
| 340 | |
| 341 | // Region analysis for mirror::Objects |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 342 | class ImgObjectVisitor : public ObjectVisitor { |
| 343 | public: |
| 344 | using ComputeDirtyFunc = std::function<void(mirror::Object* object, |
| 345 | const uint8_t* begin_image_ptr, |
| 346 | const std::set<size_t>& dirty_pages)>; |
Andreas Gampe | 6856214 | 2018-06-20 21:49:11 +0000 | [diff] [blame] | 347 | ImgObjectVisitor(ComputeDirtyFunc dirty_func, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 348 | const uint8_t* begin_image_ptr, |
| 349 | const std::set<size_t>& dirty_pages) : |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 350 | dirty_func_(std::move(dirty_func)), |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 351 | begin_image_ptr_(begin_image_ptr), |
| 352 | dirty_pages_(dirty_pages) { } |
| 353 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 354 | ~ImgObjectVisitor() override { } |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 355 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 356 | void Visit(mirror::Object* object) override REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 357 | // Sanity check that we are reading a real mirror::Object |
| 358 | CHECK(object->GetClass() != nullptr) << "Image object at address " |
| 359 | << object |
| 360 | << " has null class"; |
| 361 | if (kUseBakerReadBarrier) { |
| 362 | object->AssertReadBarrierState(); |
| 363 | } |
| 364 | dirty_func_(object, begin_image_ptr_, dirty_pages_); |
| 365 | } |
| 366 | |
| 367 | private: |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 368 | const ComputeDirtyFunc dirty_func_; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 369 | const uint8_t* begin_image_ptr_; |
| 370 | const std::set<size_t>& dirty_pages_; |
| 371 | }; |
| 372 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 373 | template<> |
| 374 | class RegionSpecializedBase<mirror::Object> : public RegionCommon<mirror::Object> { |
| 375 | public: |
| 376 | RegionSpecializedBase(std::ostream* os, |
| 377 | std::vector<uint8_t>* remote_contents, |
| 378 | std::vector<uint8_t>* zygote_contents, |
| 379 | const backtrace_map_t& boot_map, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 380 | const ImageHeader& image_header, |
| 381 | bool dump_dirty_objects) |
| 382 | : RegionCommon<mirror::Object>(os, remote_contents, zygote_contents, boot_map, image_header), |
| 383 | os_(*os), |
| 384 | dump_dirty_objects_(dump_dirty_objects) { } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 385 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 386 | // Define a common public type name for use by RegionData. |
| 387 | using VisitorClass = ImgObjectVisitor; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 388 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 389 | void VisitEntries(VisitorClass* visitor, |
| 390 | uint8_t* base, |
| 391 | PointerSize pointer_size) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 392 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 393 | RegionCommon<mirror::Object>::image_header_.VisitObjects(visitor, base, pointer_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void VisitEntry(mirror::Object* entry) |
| 397 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 398 | // Unconditionally store the class descriptor in case we need it later |
| 399 | mirror::Class* klass = entry->GetClass(); |
| 400 | class_data_[klass].descriptor = GetClassDescriptor(klass); |
| 401 | } |
| 402 | |
| 403 | void AddCleanEntry(mirror::Object* entry) |
| 404 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 405 | class_data_[entry->GetClass()].AddCleanObject(); |
| 406 | } |
| 407 | |
| 408 | void AddFalseDirtyEntry(mirror::Object* entry) |
| 409 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 410 | RegionCommon<mirror::Object>::AddFalseDirtyEntry(entry); |
| 411 | class_data_[entry->GetClass()].AddFalseDirtyObject(entry); |
| 412 | } |
| 413 | |
| 414 | void AddDirtyEntry(mirror::Object* entry, mirror::Object* entry_remote) |
| 415 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 416 | size_t entry_size = EntrySize(entry); |
| 417 | ++different_entries_; |
| 418 | dirty_entry_bytes_ += entry_size; |
| 419 | // Log dirty count and objects for class objects only. |
| 420 | mirror::Class* klass = entry->GetClass(); |
| 421 | if (klass->IsClassClass()) { |
| 422 | // Increment counts for the fields that are dirty |
| 423 | const uint8_t* current = reinterpret_cast<const uint8_t*>(entry); |
| 424 | const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(entry_remote); |
| 425 | for (size_t i = 0; i < entry_size; ++i) { |
| 426 | if (current[i] != current_remote[i]) { |
| 427 | field_dirty_count_[i]++; |
| 428 | } |
| 429 | } |
| 430 | dirty_entries_.push_back(entry); |
| 431 | } |
| 432 | class_data_[klass].AddDirtyObject(entry, entry_remote); |
| 433 | } |
| 434 | |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 435 | void DiffEntryContents(mirror::Object* entry, |
| 436 | uint8_t* remote_bytes, |
| 437 | const uint8_t* base_ptr, |
| 438 | bool log_dirty_objects) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 439 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 440 | const char* tabs = " "; |
| 441 | // Attempt to find fields for all dirty bytes. |
| 442 | mirror::Class* klass = entry->GetClass(); |
| 443 | if (entry->IsClass()) { |
| 444 | os_ << tabs |
| 445 | << "Class " << mirror::Class::PrettyClass(entry->AsClass()) << " " << entry << "\n"; |
| 446 | } else { |
| 447 | os_ << tabs |
| 448 | << "Instance of " << mirror::Class::PrettyClass(klass) << " " << entry << "\n"; |
| 449 | } |
| 450 | |
| 451 | std::unordered_set<ArtField*> dirty_instance_fields; |
| 452 | std::unordered_set<ArtField*> dirty_static_fields; |
| 453 | // Examine the bytes comprising the Object, computing which fields are dirty |
| 454 | // and recording them for later display. If the Object is an array object, |
| 455 | // compute the dirty entries. |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 456 | mirror::Object* remote_entry = reinterpret_cast<mirror::Object*>(remote_bytes); |
| 457 | for (size_t i = 0, count = entry->SizeOf(); i < count; ++i) { |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 458 | if (base_ptr[i] != remote_bytes[i]) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 459 | ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i); |
| 460 | if (field != nullptr) { |
| 461 | dirty_instance_fields.insert(field); |
| 462 | } else if (entry->IsClass()) { |
| 463 | field = ArtField::FindStaticFieldWithOffset</*exact*/false>(entry->AsClass(), i); |
| 464 | if (field != nullptr) { |
| 465 | dirty_static_fields.insert(field); |
| 466 | } |
| 467 | } |
| 468 | if (field == nullptr) { |
| 469 | if (klass->IsArrayClass()) { |
| 470 | mirror::Class* component_type = klass->GetComponentType(); |
| 471 | Primitive::Type primitive_type = component_type->GetPrimitiveType(); |
| 472 | size_t component_size = Primitive::ComponentSize(primitive_type); |
| 473 | size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 474 | DCHECK_ALIGNED_PARAM(data_offset, component_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 475 | if (i >= data_offset) { |
| 476 | os_ << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n"; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 477 | // Skip the remaining bytes of this element to prevent spam. |
| 478 | DCHECK(IsPowerOfTwo(component_size)); |
| 479 | i |= component_size - 1; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 480 | continue; |
| 481 | } |
| 482 | } |
| 483 | os_ << tabs << "No field for byte offset " << i << "\n"; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | // Dump different fields. |
| 488 | if (!dirty_instance_fields.empty()) { |
| 489 | os_ << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n"; |
| 490 | for (ArtField* field : dirty_instance_fields) { |
| 491 | os_ << tabs << ArtField::PrettyField(field) |
| 492 | << " original=" << PrettyFieldValue(field, entry) |
| 493 | << " remote=" << PrettyFieldValue(field, remote_entry) << "\n"; |
| 494 | } |
| 495 | } |
| 496 | if (!dirty_static_fields.empty()) { |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 497 | if (dump_dirty_objects_ && log_dirty_objects) { |
| 498 | dirty_objects_.insert(entry); |
| 499 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 500 | os_ << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n"; |
| 501 | for (ArtField* field : dirty_static_fields) { |
| 502 | os_ << tabs << ArtField::PrettyField(field) |
| 503 | << " original=" << PrettyFieldValue(field, entry) |
| 504 | << " remote=" << PrettyFieldValue(field, remote_entry) << "\n"; |
| 505 | } |
| 506 | } |
| 507 | os_ << "\n"; |
| 508 | } |
| 509 | |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 510 | void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 511 | for (mirror::Object* obj : dirty_objects_) { |
| 512 | if (obj->IsClass()) { |
| 513 | os_ << "Private dirty object: " << obj->AsClass()->PrettyDescriptor() << "\n"; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 518 | void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 519 | // vector of pairs (size_t count, Class*) |
| 520 | auto dirty_object_class_values = |
| 521 | SortByValueDesc<mirror::Class*, size_t, ClassData>( |
| 522 | class_data_, |
| 523 | [](const ClassData& d) { return d.dirty_object_count; }); |
| 524 | os_ << "\n" << " Dirty object count by class:\n"; |
| 525 | for (const auto& vk_pair : dirty_object_class_values) { |
| 526 | size_t dirty_object_count = vk_pair.first; |
| 527 | mirror::Class* klass = vk_pair.second; |
| 528 | ClassData& class_data = class_data_[klass]; |
| 529 | size_t object_sizes = class_data.dirty_object_size_in_bytes; |
| 530 | float avg_dirty_bytes_per_class = |
| 531 | class_data.dirty_object_byte_count * 1.0f / object_sizes; |
| 532 | float avg_object_size = object_sizes * 1.0f / dirty_object_count; |
| 533 | const std::string& descriptor = class_data.descriptor; |
| 534 | os_ << " " << mirror::Class::PrettyClass(klass) << " (" |
| 535 | << "objects: " << dirty_object_count << ", " |
| 536 | << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", " |
| 537 | << "avg object size: " << avg_object_size << ", " |
| 538 | << "class descriptor: '" << descriptor << "'" |
| 539 | << ")\n"; |
| 540 | if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) { |
| 541 | DumpSamplesAndOffsetCount(); |
| 542 | os_ << " field contents:\n"; |
| 543 | for (mirror::Object* object : class_data.dirty_objects) { |
| 544 | // remote class object |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 545 | ObjPtr<mirror::Class> remote_klass = |
| 546 | ObjPtr<mirror::Class>::DownCast<mirror::Object>(object); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 547 | // local class object |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 548 | ObjPtr<mirror::Class> local_klass = |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 549 | RemoteContentsPointerToLocal(remote_klass, |
| 550 | *RegionCommon<mirror::Object>::remote_contents_, |
| 551 | RegionCommon<mirror::Object>::image_header_); |
| 552 | os_ << " " << reinterpret_cast<const void*>(object) << " "; |
| 553 | os_ << " class_status (remote): " << remote_klass->GetStatus() << ", "; |
| 554 | os_ << " class_status (local): " << local_klass->GetStatus(); |
| 555 | os_ << "\n"; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 562 | // vector of pairs (size_t count, Class*) |
| 563 | auto false_dirty_object_class_values = |
| 564 | SortByValueDesc<mirror::Class*, size_t, ClassData>( |
| 565 | class_data_, |
| 566 | [](const ClassData& d) { return d.false_dirty_object_count; }); |
| 567 | os_ << "\n" << " False-dirty object count by class:\n"; |
| 568 | for (const auto& vk_pair : false_dirty_object_class_values) { |
| 569 | size_t object_count = vk_pair.first; |
| 570 | mirror::Class* klass = vk_pair.second; |
| 571 | ClassData& class_data = class_data_[klass]; |
| 572 | size_t object_sizes = class_data.false_dirty_byte_count; |
| 573 | float avg_object_size = object_sizes * 1.0f / object_count; |
| 574 | const std::string& descriptor = class_data.descriptor; |
| 575 | os_ << " " << mirror::Class::PrettyClass(klass) << " (" |
| 576 | << "objects: " << object_count << ", " |
| 577 | << "avg object size: " << avg_object_size << ", " |
| 578 | << "total bytes: " << object_sizes << ", " |
| 579 | << "class descriptor: '" << descriptor << "'" |
| 580 | << ")\n"; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 585 | // vector of pairs (size_t count, Class*) |
| 586 | auto clean_object_class_values = |
| 587 | SortByValueDesc<mirror::Class*, size_t, ClassData>( |
| 588 | class_data_, |
| 589 | [](const ClassData& d) { return d.clean_object_count; }); |
| 590 | os_ << "\n" << " Clean object count by class:\n"; |
| 591 | for (const auto& vk_pair : clean_object_class_values) { |
| 592 | os_ << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n"; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | private: |
| 597 | // Aggregate and detail class data from an image diff. |
| 598 | struct ClassData { |
| 599 | size_t dirty_object_count = 0; |
| 600 | // Track only the byte-per-byte dirtiness (in bytes) |
| 601 | size_t dirty_object_byte_count = 0; |
| 602 | // Track the object-by-object dirtiness (in bytes) |
| 603 | size_t dirty_object_size_in_bytes = 0; |
| 604 | size_t clean_object_count = 0; |
| 605 | std::string descriptor; |
| 606 | size_t false_dirty_byte_count = 0; |
| 607 | size_t false_dirty_object_count = 0; |
| 608 | std::vector<mirror::Object*> false_dirty_objects; |
| 609 | // Remote pointers to dirty objects |
| 610 | std::vector<mirror::Object*> dirty_objects; |
| 611 | |
| 612 | void AddCleanObject() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 613 | ++clean_object_count; |
| 614 | } |
| 615 | |
| 616 | void AddDirtyObject(mirror::Object* object, mirror::Object* object_remote) |
| 617 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 618 | ++dirty_object_count; |
| 619 | dirty_object_byte_count += CountDirtyBytes(object, object_remote); |
| 620 | dirty_object_size_in_bytes += EntrySize(object); |
| 621 | dirty_objects.push_back(object_remote); |
| 622 | } |
| 623 | |
| 624 | void AddFalseDirtyObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 625 | ++false_dirty_object_count; |
| 626 | false_dirty_objects.push_back(object); |
| 627 | false_dirty_byte_count += EntrySize(object); |
| 628 | } |
| 629 | |
| 630 | private: |
| 631 | // Go byte-by-byte and figure out what exactly got dirtied |
| 632 | static size_t CountDirtyBytes(mirror::Object* object1, mirror::Object* object2) |
| 633 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 634 | const uint8_t* cur1 = reinterpret_cast<const uint8_t*>(object1); |
| 635 | const uint8_t* cur2 = reinterpret_cast<const uint8_t*>(object2); |
| 636 | size_t dirty_bytes = 0; |
| 637 | size_t object_size = EntrySize(object1); |
| 638 | for (size_t i = 0; i < object_size; ++i) { |
| 639 | if (cur1[i] != cur2[i]) { |
| 640 | dirty_bytes++; |
| 641 | } |
| 642 | } |
| 643 | return dirty_bytes; |
| 644 | } |
| 645 | }; |
| 646 | |
| 647 | std::ostream& os_; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 648 | bool dump_dirty_objects_; |
| 649 | std::unordered_set<mirror::Object*> dirty_objects_; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 650 | std::map<mirror::Class*, ClassData> class_data_; |
| 651 | |
| 652 | DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase); |
| 653 | }; |
| 654 | |
| 655 | // Region analysis for ArtMethods. |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 656 | class ImgArtMethodVisitor { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 657 | public: |
| 658 | using ComputeDirtyFunc = std::function<void(ArtMethod*, |
| 659 | const uint8_t*, |
| 660 | const std::set<size_t>&)>; |
Andreas Gampe | 6856214 | 2018-06-20 21:49:11 +0000 | [diff] [blame] | 661 | ImgArtMethodVisitor(ComputeDirtyFunc dirty_func, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 662 | const uint8_t* begin_image_ptr, |
| 663 | const std::set<size_t>& dirty_pages) : |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 664 | dirty_func_(std::move(dirty_func)), |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 665 | begin_image_ptr_(begin_image_ptr), |
| 666 | dirty_pages_(dirty_pages) { } |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 667 | void operator()(ArtMethod& method) const { |
| 668 | dirty_func_(&method, begin_image_ptr_, dirty_pages_); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | private: |
Andreas Gampe | bc802de | 2018-06-20 17:24:11 -0700 | [diff] [blame] | 672 | const ComputeDirtyFunc dirty_func_; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 673 | const uint8_t* begin_image_ptr_; |
| 674 | const std::set<size_t>& dirty_pages_; |
| 675 | }; |
| 676 | |
| 677 | // Struct and functor for computing offsets of members of ArtMethods. |
| 678 | // template <typename RegionType> |
| 679 | struct MemberInfo { |
| 680 | template <typename T> |
| 681 | void operator() (const ArtMethod* method, const T* member_address, const std::string& name) { |
| 682 | // Check that member_address is a pointer inside *method. |
| 683 | DCHECK(reinterpret_cast<uintptr_t>(method) <= reinterpret_cast<uintptr_t>(member_address)); |
| 684 | DCHECK(reinterpret_cast<uintptr_t>(member_address) + sizeof(T) <= |
| 685 | reinterpret_cast<uintptr_t>(method) + sizeof(ArtMethod)); |
| 686 | size_t offset = |
| 687 | reinterpret_cast<uintptr_t>(member_address) - reinterpret_cast<uintptr_t>(method); |
| 688 | offset_to_name_size_.insert({offset, NameAndSize(sizeof(T), name)}); |
| 689 | } |
| 690 | |
| 691 | struct NameAndSize { |
| 692 | size_t size_; |
| 693 | std::string name_; |
| 694 | NameAndSize(size_t size, const std::string& name) : size_(size), name_(name) { } |
| 695 | NameAndSize() : size_(0), name_("INVALID") { } |
| 696 | }; |
| 697 | |
| 698 | std::map<size_t, NameAndSize> offset_to_name_size_; |
| 699 | }; |
| 700 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 701 | template<> |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 702 | class RegionSpecializedBase<ArtMethod> : public RegionCommon<ArtMethod> { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 703 | public: |
| 704 | RegionSpecializedBase(std::ostream* os, |
| 705 | std::vector<uint8_t>* remote_contents, |
| 706 | std::vector<uint8_t>* zygote_contents, |
| 707 | const backtrace_map_t& boot_map, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 708 | const ImageHeader& image_header, |
| 709 | bool dump_dirty_objects ATTRIBUTE_UNUSED) |
| 710 | : RegionCommon<ArtMethod>(os, remote_contents, zygote_contents, boot_map, image_header), |
| 711 | os_(*os) { |
| 712 | // Prepare the table for offset to member lookups. |
| 713 | ArtMethod* art_method = reinterpret_cast<ArtMethod*>(&(*remote_contents)[0]); |
| 714 | art_method->VisitMembers(member_info_); |
| 715 | // Prepare the table for address to symbolic entry point names. |
| 716 | BuildEntryPointNames(); |
| 717 | class_linker_ = Runtime::Current()->GetClassLinker(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 718 | } |
| 719 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 720 | // Define a common public type name for use by RegionData. |
| 721 | using VisitorClass = ImgArtMethodVisitor; |
| 722 | |
| 723 | void VisitEntries(VisitorClass* visitor, |
| 724 | uint8_t* base, |
| 725 | PointerSize pointer_size) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 726 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 727 | RegionCommon<ArtMethod>::image_header_.VisitPackedArtMethods(*visitor, base, pointer_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void VisitEntry(ArtMethod* method ATTRIBUTE_UNUSED) |
| 731 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 732 | } |
| 733 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 734 | void AddCleanEntry(ArtMethod* method ATTRIBUTE_UNUSED) { |
| 735 | } |
| 736 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 737 | void AddFalseDirtyEntry(ArtMethod* method) |
| 738 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 739 | RegionCommon<ArtMethod>::AddFalseDirtyEntry(method); |
| 740 | } |
| 741 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 742 | void AddDirtyEntry(ArtMethod* method, ArtMethod* method_remote) |
| 743 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 744 | size_t entry_size = EntrySize(method); |
| 745 | ++different_entries_; |
| 746 | dirty_entry_bytes_ += entry_size; |
| 747 | // Increment counts for the fields that are dirty |
| 748 | const uint8_t* current = reinterpret_cast<const uint8_t*>(method); |
| 749 | const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(method_remote); |
| 750 | // ArtMethods always log their dirty count and entries. |
| 751 | for (size_t i = 0; i < entry_size; ++i) { |
| 752 | if (current[i] != current_remote[i]) { |
| 753 | field_dirty_count_[i]++; |
| 754 | } |
| 755 | } |
| 756 | dirty_entries_.push_back(method); |
| 757 | } |
| 758 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 759 | void DiffEntryContents(ArtMethod* method, |
| 760 | uint8_t* remote_bytes, |
| 761 | const uint8_t* base_ptr, |
| 762 | bool log_dirty_objects ATTRIBUTE_UNUSED) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 763 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 764 | const char* tabs = " "; |
| 765 | os_ << tabs << "ArtMethod " << ArtMethod::PrettyMethod(method) << "\n"; |
| 766 | |
| 767 | std::unordered_set<size_t> dirty_members; |
| 768 | // Examine the members comprising the ArtMethod, computing which members are dirty. |
Andreas Gampe | aad9d37 | 2018-09-18 15:58:47 -0700 | [diff] [blame] | 769 | for (const std::pair<const size_t, |
| 770 | MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 771 | const size_t offset = p.first; |
| 772 | if (memcmp(base_ptr + offset, remote_bytes + offset, p.second.size_) != 0) { |
| 773 | dirty_members.insert(p.first); |
| 774 | } |
| 775 | } |
| 776 | // Dump different fields. |
| 777 | if (!dirty_members.empty()) { |
| 778 | os_ << tabs << "Dirty members " << dirty_members.size() << "\n"; |
| 779 | for (size_t offset : dirty_members) { |
| 780 | const MemberInfo::NameAndSize& member_info = member_info_.offset_to_name_size_[offset]; |
| 781 | os_ << tabs << member_info.name_ |
| 782 | << " original=" << StringFromBytes(base_ptr + offset, member_info.size_) |
| 783 | << " remote=" << StringFromBytes(remote_bytes + offset, member_info.size_) |
| 784 | << "\n"; |
| 785 | } |
| 786 | } |
| 787 | os_ << "\n"; |
| 788 | } |
| 789 | |
| 790 | void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 794 | DumpSamplesAndOffsetCount(); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 795 | os_ << " offset to field map:\n"; |
Andreas Gampe | aad9d37 | 2018-09-18 15:58:47 -0700 | [diff] [blame] | 796 | for (const std::pair<const size_t, |
| 797 | MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 798 | const size_t offset = p.first; |
| 799 | const size_t size = p.second.size_; |
| 800 | os_ << StringPrintf(" %zu-%zu: ", offset, offset + size - 1) |
| 801 | << p.second.name_ |
| 802 | << std::endl; |
| 803 | } |
| 804 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 805 | os_ << " field contents:\n"; |
| 806 | for (ArtMethod* method : dirty_entries_) { |
| 807 | // remote method |
| 808 | auto art_method = reinterpret_cast<ArtMethod*>(method); |
| 809 | // remote class |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 810 | ObjPtr<mirror::Class> remote_declaring_class = |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 811 | FixUpRemotePointer(art_method->GetDeclaringClass(), |
| 812 | *RegionCommon<ArtMethod>::remote_contents_, |
| 813 | RegionCommon<ArtMethod>::boot_map_); |
| 814 | // local class |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 815 | ObjPtr<mirror::Class> declaring_class = |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 816 | RemoteContentsPointerToLocal(remote_declaring_class, |
| 817 | *RegionCommon<ArtMethod>::remote_contents_, |
| 818 | RegionCommon<ArtMethod>::image_header_); |
| 819 | DumpOneArtMethod(art_method, declaring_class, remote_declaring_class); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 824 | os_ << "\n" << " False-dirty ArtMethods\n"; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 825 | os_ << " field contents:\n"; |
| 826 | for (ArtMethod* method : false_dirty_entries_) { |
| 827 | // local class |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 828 | ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 829 | DumpOneArtMethod(method, declaring_class, nullptr); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 834 | } |
| 835 | |
| 836 | private: |
| 837 | std::ostream& os_; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 838 | MemberInfo member_info_; |
| 839 | std::map<const void*, std::string> entry_point_names_; |
| 840 | ClassLinker* class_linker_; |
| 841 | |
| 842 | // Compute a map of addresses to names in the boot OAT file(s). |
| 843 | void BuildEntryPointNames() { |
| 844 | OatFileManager& oat_file_manager = Runtime::Current()->GetOatFileManager(); |
| 845 | std::vector<const OatFile*> boot_oat_files = oat_file_manager.GetBootOatFiles(); |
| 846 | for (const OatFile* oat_file : boot_oat_files) { |
| 847 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 848 | const void* jdl = oat_header.GetJniDlsymLookup(); |
| 849 | if (jdl != nullptr) { |
| 850 | entry_point_names_[jdl] = "JniDlsymLookup (from boot oat file)"; |
| 851 | } |
| 852 | const void* qgjt = oat_header.GetQuickGenericJniTrampoline(); |
| 853 | if (qgjt != nullptr) { |
| 854 | entry_point_names_[qgjt] = "QuickGenericJniTrampoline (from boot oat file)"; |
| 855 | } |
| 856 | const void* qrt = oat_header.GetQuickResolutionTrampoline(); |
| 857 | if (qrt != nullptr) { |
| 858 | entry_point_names_[qrt] = "QuickResolutionTrampoline (from boot oat file)"; |
| 859 | } |
| 860 | const void* qict = oat_header.GetQuickImtConflictTrampoline(); |
| 861 | if (qict != nullptr) { |
| 862 | entry_point_names_[qict] = "QuickImtConflictTrampoline (from boot oat file)"; |
| 863 | } |
| 864 | const void* q2ib = oat_header.GetQuickToInterpreterBridge(); |
| 865 | if (q2ib != nullptr) { |
| 866 | entry_point_names_[q2ib] = "QuickToInterpreterBridge (from boot oat file)"; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | std::string StringFromBytes(const uint8_t* bytes, size_t size) { |
| 872 | switch (size) { |
| 873 | case 1: |
| 874 | return StringPrintf("%" PRIx8, *bytes); |
| 875 | case 2: |
| 876 | return StringPrintf("%" PRIx16, *reinterpret_cast<const uint16_t*>(bytes)); |
| 877 | case 4: |
| 878 | case 8: { |
| 879 | // Compute an address if the bytes might contain one. |
| 880 | uint64_t intval; |
| 881 | if (size == 4) { |
| 882 | intval = *reinterpret_cast<const uint32_t*>(bytes); |
| 883 | } else { |
| 884 | intval = *reinterpret_cast<const uint64_t*>(bytes); |
| 885 | } |
| 886 | const void* addr = reinterpret_cast<const void*>(intval); |
| 887 | // Match the address against those that have Is* methods in the ClassLinker. |
| 888 | if (class_linker_->IsQuickToInterpreterBridge(addr)) { |
| 889 | return "QuickToInterpreterBridge"; |
| 890 | } else if (class_linker_->IsQuickGenericJniStub(addr)) { |
| 891 | return "QuickGenericJniStub"; |
| 892 | } else if (class_linker_->IsQuickResolutionStub(addr)) { |
| 893 | return "QuickResolutionStub"; |
| 894 | } else if (class_linker_->IsJniDlsymLookupStub(addr)) { |
| 895 | return "JniDlsymLookupStub"; |
| 896 | } |
| 897 | // Match the address against those that we saved from the boot OAT files. |
| 898 | if (entry_point_names_.find(addr) != entry_point_names_.end()) { |
| 899 | return entry_point_names_[addr]; |
| 900 | } |
| 901 | return StringPrintf("%" PRIx64, intval); |
| 902 | } |
| 903 | default: |
| 904 | LOG(WARNING) << "Don't know how to convert " << size << " bytes to integer"; |
| 905 | return "<UNKNOWN>"; |
| 906 | } |
| 907 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 908 | |
| 909 | void DumpOneArtMethod(ArtMethod* art_method, |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 910 | ObjPtr<mirror::Class> declaring_class, |
| 911 | ObjPtr<mirror::Class> remote_declaring_class) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 912 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 913 | PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 914 | os_ << " " << reinterpret_cast<const void*>(art_method) << " "; |
| 915 | os_ << " entryPointFromJni: " |
| 916 | << reinterpret_cast<const void*>(art_method->GetDataPtrSize(pointer_size)) << ", "; |
| 917 | os_ << " entryPointFromQuickCompiledCode: " |
| 918 | << reinterpret_cast<const void*>( |
| 919 | art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size)) |
| 920 | << ", "; |
| 921 | os_ << " isNative? " << (art_method->IsNative() ? "yes" : "no") << ", "; |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 922 | // Null for runtime metionds. |
| 923 | if (declaring_class != nullptr) { |
| 924 | os_ << " class_status (local): " << declaring_class->GetStatus(); |
| 925 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 926 | if (remote_declaring_class != nullptr) { |
| 927 | os_ << ", class_status (remote): " << remote_declaring_class->GetStatus(); |
| 928 | } |
| 929 | os_ << "\n"; |
| 930 | } |
| 931 | |
| 932 | DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase); |
| 933 | }; |
| 934 | |
| 935 | template <typename T> |
| 936 | class RegionData : public RegionSpecializedBase<T> { |
| 937 | public: |
| 938 | RegionData(std::ostream* os, |
| 939 | std::vector<uint8_t>* remote_contents, |
| 940 | std::vector<uint8_t>* zygote_contents, |
| 941 | const backtrace_map_t& boot_map, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 942 | const ImageHeader& image_header, |
| 943 | bool dump_dirty_objects) |
| 944 | : RegionSpecializedBase<T>(os, |
| 945 | remote_contents, |
| 946 | zygote_contents, |
| 947 | boot_map, |
| 948 | image_header, |
| 949 | dump_dirty_objects), |
| 950 | os_(*os) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 951 | CHECK(remote_contents != nullptr); |
| 952 | CHECK(zygote_contents != nullptr); |
| 953 | } |
| 954 | |
| 955 | // Walk over the type T entries in theregion between begin_image_ptr and end_image_ptr, |
| 956 | // collecting and reporting data regarding dirty, difference, etc. |
| 957 | void ProcessRegion(const MappingData& mapping_data, |
| 958 | RemoteProcesses remotes, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 959 | const uint8_t* begin_image_ptr) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 960 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 961 | typename RegionSpecializedBase<T>::VisitorClass visitor( |
| 962 | [this](T* entry, |
| 963 | const uint8_t* begin_image_ptr, |
| 964 | const std::set<size_t>& dirty_page_set) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 965 | this->ComputeEntryDirty(entry, begin_image_ptr, dirty_page_set); |
| 966 | }, |
| 967 | begin_image_ptr, |
| 968 | mapping_data.dirty_page_set); |
| 969 | PointerSize pointer_size = InstructionSetPointerSize(Runtime::Current()->GetInstructionSet()); |
| 970 | RegionSpecializedBase<T>::VisitEntries(&visitor, |
| 971 | const_cast<uint8_t*>(begin_image_ptr), |
| 972 | pointer_size); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 973 | |
| 974 | // Looking at only dirty pages, figure out how many of those bytes belong to dirty entries. |
| 975 | // TODO: fix this now that there are multiple regions in a mapping. |
| 976 | float true_dirtied_percent = |
| 977 | RegionCommon<T>::GetDirtyEntryBytes() * 1.0f / (mapping_data.dirty_pages * kPageSize); |
| 978 | |
| 979 | // Entry specific statistics. |
| 980 | os_ << RegionCommon<T>::GetDifferentEntryCount() << " different entries, \n " |
| 981 | << RegionCommon<T>::GetDirtyEntryBytes() << " different entry [bytes], \n " |
| 982 | << RegionCommon<T>::GetFalseDirtyEntryCount() << " false dirty entries,\n " |
| 983 | << RegionCommon<T>::GetFalseDirtyEntryBytes() << " false dirty entry [bytes], \n " |
| 984 | << true_dirtied_percent << " different entries-vs-total in a dirty page;\n " |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 985 | << "\n"; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 986 | |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 987 | const uint8_t* base_ptr = begin_image_ptr; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 988 | switch (remotes) { |
| 989 | case RemoteProcesses::kZygoteOnly: |
| 990 | os_ << " Zygote shared dirty entries: "; |
| 991 | break; |
| 992 | case RemoteProcesses::kImageAndZygote: |
| 993 | os_ << " Application dirty entries (private dirty): "; |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 994 | // If we are dumping private dirty, diff against the zygote map to make it clearer what |
| 995 | // fields caused the page to be private dirty. |
| 996 | base_ptr = &RegionCommon<T>::zygote_contents_->operator[](0); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 997 | break; |
| 998 | case RemoteProcesses::kImageOnly: |
| 999 | os_ << " Application dirty entries (unknown whether private or shared dirty): "; |
| 1000 | break; |
| 1001 | } |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1002 | DiffDirtyEntries(ProcessType::kRemote, |
| 1003 | begin_image_ptr, |
| 1004 | RegionCommon<T>::remote_contents_, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1005 | base_ptr, |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 1006 | /*log_dirty_objects=*/true); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1007 | // Print shared dirty after since it's less important. |
| 1008 | if (RegionCommon<T>::GetZygoteDirtyEntryCount() != 0) { |
| 1009 | // We only reach this point if both pids were specified. Furthermore, |
| 1010 | // entries are only displayed here if they differed in both the image |
| 1011 | // and the zygote, so they are probably private dirty. |
| 1012 | CHECK(remotes == RemoteProcesses::kImageAndZygote); |
| 1013 | os_ << "\n" << " Zygote dirty entries (probably shared dirty): "; |
| 1014 | DiffDirtyEntries(ProcessType::kZygote, |
| 1015 | begin_image_ptr, |
| 1016 | RegionCommon<T>::zygote_contents_, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1017 | begin_image_ptr, |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 1018 | /*log_dirty_objects=*/false); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1019 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1020 | RegionSpecializedBase<T>::DumpDirtyObjects(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1021 | RegionSpecializedBase<T>::DumpDirtyEntries(); |
| 1022 | RegionSpecializedBase<T>::DumpFalseDirtyEntries(); |
| 1023 | RegionSpecializedBase<T>::DumpCleanEntries(); |
| 1024 | } |
| 1025 | |
| 1026 | private: |
| 1027 | std::ostream& os_; |
| 1028 | |
| 1029 | void DiffDirtyEntries(ProcessType process_type, |
| 1030 | const uint8_t* begin_image_ptr, |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1031 | std::vector<uint8_t>* contents, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1032 | const uint8_t* base_ptr, |
| 1033 | bool log_dirty_objects) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1034 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1035 | os_ << RegionCommon<T>::dirty_entries_.size() << "\n"; |
| 1036 | const std::set<T*>& entries = |
| 1037 | (process_type == ProcessType::kZygote) ? |
| 1038 | RegionCommon<T>::zygote_dirty_entries_: |
| 1039 | RegionCommon<T>::image_dirty_entries_; |
| 1040 | for (T* entry : entries) { |
| 1041 | uint8_t* entry_bytes = reinterpret_cast<uint8_t*>(entry); |
| 1042 | ptrdiff_t offset = entry_bytes - begin_image_ptr; |
| 1043 | uint8_t* remote_bytes = &(*contents)[offset]; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1044 | RegionSpecializedBase<T>::DiffEntryContents(entry, |
| 1045 | remote_bytes, |
| 1046 | &base_ptr[offset], |
| 1047 | log_dirty_objects); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | void ComputeEntryDirty(T* entry, |
| 1052 | const uint8_t* begin_image_ptr, |
| 1053 | const std::set<size_t>& dirty_pages) |
| 1054 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1055 | // Set up pointers in the remote and the zygote for comparison. |
| 1056 | uint8_t* current = reinterpret_cast<uint8_t*>(entry); |
| 1057 | ptrdiff_t offset = current - begin_image_ptr; |
| 1058 | T* entry_remote = |
| 1059 | reinterpret_cast<T*>(const_cast<uint8_t*>(&(*RegionCommon<T>::remote_contents_)[offset])); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1060 | const bool have_zygote = !RegionCommon<T>::zygote_contents_->empty(); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1061 | const uint8_t* current_zygote = |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1062 | have_zygote ? &(*RegionCommon<T>::zygote_contents_)[offset] : nullptr; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1063 | T* entry_zygote = reinterpret_cast<T*>(const_cast<uint8_t*>(current_zygote)); |
| 1064 | // Visit and classify entries at the current location. |
| 1065 | RegionSpecializedBase<T>::VisitEntry(entry); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1066 | |
| 1067 | // Test private dirty first. |
| 1068 | bool is_dirty = false; |
| 1069 | if (have_zygote) { |
| 1070 | bool private_dirty = EntriesDiffer(entry_zygote, entry_remote); |
| 1071 | if (private_dirty) { |
| 1072 | // Private dirty, app vs zygote. |
| 1073 | is_dirty = true; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1074 | RegionCommon<T>::AddImageDirtyEntry(entry); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1075 | } |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1076 | if (EntriesDiffer(entry_zygote, entry)) { |
| 1077 | // Shared dirty, zygote vs image. |
| 1078 | is_dirty = true; |
| 1079 | RegionCommon<T>::AddZygoteDirtyEntry(entry); |
| 1080 | } |
| 1081 | } else if (EntriesDiffer(entry_remote, entry)) { |
| 1082 | // Shared or private dirty, app vs image. |
| 1083 | is_dirty = true; |
| 1084 | RegionCommon<T>::AddImageDirtyEntry(entry); |
| 1085 | } |
| 1086 | if (is_dirty) { |
| 1087 | // TODO: Add support dirty entries in zygote and image. |
| 1088 | RegionSpecializedBase<T>::AddDirtyEntry(entry, entry_remote); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1089 | } else { |
| 1090 | RegionSpecializedBase<T>::AddCleanEntry(entry); |
Mathieu Chartier | 51e7965 | 2017-07-24 15:43:38 -0700 | [diff] [blame] | 1091 | if (RegionCommon<T>::IsEntryOnDirtyPage(entry, dirty_pages)) { |
| 1092 | // This entry was either never mutated or got mutated back to the same value. |
| 1093 | // TODO: Do I want to distinguish a "different" vs a "dirty" page here? |
| 1094 | RegionSpecializedBase<T>::AddFalseDirtyEntry(entry); |
| 1095 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | DISALLOW_COPY_AND_ASSIGN(RegionData); |
| 1100 | }; |
| 1101 | |
| 1102 | } // namespace |
| 1103 | |
| 1104 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1105 | class ImgDiagDumper { |
| 1106 | public: |
| 1107 | explicit ImgDiagDumper(std::ostream* os, |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1108 | pid_t image_diff_pid, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1109 | pid_t zygote_diff_pid, |
| 1110 | bool dump_dirty_objects) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1111 | : os_(os), |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1112 | image_diff_pid_(image_diff_pid), |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1113 | zygote_diff_pid_(zygote_diff_pid), |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1114 | dump_dirty_objects_(dump_dirty_objects), |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1115 | zygote_pid_only_(false) {} |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1116 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1117 | bool Init() { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1118 | std::ostream& os = *os_; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 1119 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1120 | if (image_diff_pid_ < 0 && zygote_diff_pid_ < 0) { |
| 1121 | os << "Either --image-diff-pid or --zygote-diff-pid (or both) must be specified.\n"; |
| 1122 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1125 | // To avoid the combinations of command-line argument use cases: |
| 1126 | // If the user invoked with only --zygote-diff-pid, shuffle that to |
| 1127 | // image_diff_pid_, invalidate zygote_diff_pid_, and remember that |
| 1128 | // image_diff_pid_ is now special. |
| 1129 | if (image_diff_pid_ < 0) { |
| 1130 | image_diff_pid_ = zygote_diff_pid_; |
| 1131 | zygote_diff_pid_ = -1; |
| 1132 | zygote_pid_only_ = true; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame] | 1133 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1134 | |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame] | 1135 | { |
| 1136 | struct stat sts; |
| 1137 | std::string proc_pid_str = |
| 1138 | StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid_)); // NOLINT [runtime/int] |
| 1139 | if (stat(proc_pid_str.c_str(), &sts) == -1) { |
| 1140 | os << "Process does not exist"; |
| 1141 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1145 | auto open_proc_maps = [&os](pid_t pid, /*out*/ std::unique_ptr<BacktraceMap>* proc_maps) { |
| 1146 | // Open /proc/<pid>/maps to view memory maps. |
| 1147 | proc_maps->reset(BacktraceMap::Create(pid)); |
| 1148 | if (*proc_maps == nullptr) { |
| 1149 | os << "Could not read backtrace maps for " << pid; |
| 1150 | return false; |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 1151 | } |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1152 | return true; |
| 1153 | }; |
| 1154 | auto open_file = [&os] (const char* file_name, /*out*/ std::unique_ptr<File>* file) { |
| 1155 | file->reset(OS::OpenFileForReading(file_name)); |
| 1156 | if (*file == nullptr) { |
| 1157 | os << "Failed to open " << file_name << " for reading"; |
| 1158 | return false; |
| 1159 | } |
| 1160 | return true; |
| 1161 | }; |
| 1162 | auto open_mem_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* mem_file) { |
| 1163 | // Open /proc/<pid>/mem and for reading remote contents. |
| 1164 | std::string mem_file_name = |
| 1165 | StringPrintf("/proc/%ld/mem", static_cast<long>(pid)); // NOLINT [runtime/int] |
| 1166 | return open_file(mem_file_name.c_str(), mem_file); |
| 1167 | }; |
| 1168 | auto open_pagemap_file = [&open_file](pid_t pid, /*out*/ std::unique_ptr<File>* pagemap_file) { |
| 1169 | // Open /proc/<pid>/pagemap. |
| 1170 | std::string pagemap_file_name = StringPrintf( |
| 1171 | "/proc/%ld/pagemap", static_cast<long>(pid)); // NOLINT [runtime/int] |
| 1172 | return open_file(pagemap_file_name.c_str(), pagemap_file); |
| 1173 | }; |
David Sehr | 0627be3 | 2017-06-16 13:50:02 -0700 | [diff] [blame] | 1174 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1175 | // Open files for inspecting image memory. |
| 1176 | std::unique_ptr<BacktraceMap> image_proc_maps; |
| 1177 | std::unique_ptr<File> image_mem_file; |
| 1178 | std::unique_ptr<File> image_pagemap_file; |
| 1179 | if (!open_proc_maps(image_diff_pid_, &image_proc_maps) || |
| 1180 | !open_mem_file(image_diff_pid_, &image_mem_file) || |
| 1181 | !open_pagemap_file(image_diff_pid_, &image_pagemap_file)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
| 1184 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1185 | // If zygote_diff_pid_ != -1, open files for inspecting zygote memory. |
| 1186 | std::unique_ptr<BacktraceMap> zygote_proc_maps; |
| 1187 | std::unique_ptr<File> zygote_mem_file; |
| 1188 | std::unique_ptr<File> zygote_pagemap_file; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1189 | if (zygote_diff_pid_ != -1) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1190 | if (!open_proc_maps(zygote_diff_pid_, &zygote_proc_maps) || |
| 1191 | !open_mem_file(zygote_diff_pid_, &zygote_mem_file) || |
| 1192 | !open_pagemap_file(zygote_diff_pid_, &zygote_pagemap_file)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
| 1195 | } |
| 1196 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1197 | std::unique_ptr<File> clean_pagemap_file; |
| 1198 | std::unique_ptr<File> kpageflags_file; |
| 1199 | std::unique_ptr<File> kpagecount_file; |
| 1200 | if (!open_file("/proc/self/pagemap", &clean_pagemap_file) || |
| 1201 | !open_file("/proc/kpageflags", &kpageflags_file) || |
| 1202 | !open_file("/proc/kpagecount", &kpagecount_file)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1203 | return false; |
| 1204 | } |
| 1205 | |
Vladimir Marko | d0430bf | 2019-03-18 10:54:17 +0000 | [diff] [blame] | 1206 | // Note: the boot image is not really clean but close enough. |
| 1207 | // For now, log pages found to be dirty. |
| 1208 | // TODO: Rewrite imgdiag to load boot image without creating a runtime. |
| 1209 | // FIXME: The following does not reliably detect dirty pages. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1210 | Runtime* runtime = Runtime::Current(); |
| 1211 | CHECK(!runtime->ShouldRelocate()); |
| 1212 | size_t total_dirty_pages = 0u; |
| 1213 | for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) { |
| 1214 | const ImageHeader& image_header = space->GetImageHeader(); |
| 1215 | const uint8_t* image_begin = image_header.GetImageBegin(); |
| 1216 | const uint8_t* image_end = AlignUp(image_begin + image_header.GetImageSize(), kPageSize); |
| 1217 | size_t virtual_page_idx_begin = reinterpret_cast<uintptr_t>(image_begin) / kPageSize; |
| 1218 | size_t virtual_page_idx_end = reinterpret_cast<uintptr_t>(image_end) / kPageSize; |
| 1219 | size_t num_virtual_pages = virtual_page_idx_end - virtual_page_idx_begin; |
| 1220 | |
| 1221 | std::string error_msg; |
| 1222 | std::vector<uint64_t> page_frame_numbers(num_virtual_pages); |
| 1223 | if (!GetPageFrameNumbers(clean_pagemap_file.get(), |
| 1224 | virtual_page_idx_begin, |
| 1225 | ArrayRef<uint64_t>(page_frame_numbers), |
| 1226 | &error_msg)) { |
| 1227 | os << "Failed to get page frame numbers for image space " << space->GetImageLocation() |
| 1228 | << ", error: " << error_msg; |
| 1229 | return false; |
| 1230 | } |
| 1231 | |
| 1232 | std::vector<uint64_t> page_flags(num_virtual_pages); |
| 1233 | if (!GetPageFlagsOrCounts(kpageflags_file.get(), |
| 1234 | ArrayRef<const uint64_t>(page_frame_numbers), |
| 1235 | ArrayRef<uint64_t>(page_flags), |
| 1236 | &error_msg)) { |
| 1237 | os << "Failed to get page flags for image space " << space->GetImageLocation() |
| 1238 | << ", error: " << error_msg; |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
| 1242 | size_t num_dirty_pages = 0u; |
| 1243 | std::optional<size_t> first_dirty_page; |
| 1244 | for (size_t i = 0u, size = page_flags.size(); i != size; ++i) { |
| 1245 | if (UNLIKELY((page_flags[i] & kPageFlagsDirtyMask) != 0u)) { |
| 1246 | ++num_dirty_pages; |
| 1247 | if (!first_dirty_page.has_value()) { |
| 1248 | first_dirty_page = i; |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | if (num_dirty_pages != 0u) { |
| 1253 | DCHECK(first_dirty_page.has_value()); |
| 1254 | os << "Found " << num_dirty_pages << " dirty pages for " << space->GetImageLocation() |
| 1255 | << ", first dirty page: " << first_dirty_page.value_or(0u); |
| 1256 | total_dirty_pages += num_dirty_pages; |
| 1257 | } |
| 1258 | } |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1259 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1260 | // Commit the mappings and files. |
| 1261 | image_proc_maps_ = std::move(image_proc_maps); |
| 1262 | image_mem_file_ = std::move(*image_mem_file); |
| 1263 | image_pagemap_file_ = std::move(*image_pagemap_file); |
| 1264 | if (zygote_diff_pid_ != -1) { |
| 1265 | zygote_proc_maps_ = std::move(zygote_proc_maps); |
| 1266 | zygote_mem_file_ = std::move(*zygote_mem_file); |
| 1267 | zygote_pagemap_file_ = std::move(*zygote_pagemap_file); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1268 | } |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1269 | clean_pagemap_file_ = std::move(*clean_pagemap_file); |
| 1270 | kpageflags_file_ = std::move(*kpageflags_file); |
| 1271 | kpagecount_file_ = std::move(*kpagecount_file); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1272 | |
| 1273 | return true; |
| 1274 | } |
| 1275 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1276 | bool Dump(const ImageHeader& image_header, const std::string& image_location) |
| 1277 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1278 | std::ostream& os = *os_; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1279 | os << "IMAGE LOCATION: " << image_location << "\n\n"; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1280 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1281 | os << "MAGIC: " << image_header.GetMagic() << "\n\n"; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1282 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1283 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n"; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1284 | |
| 1285 | PrintPidLine("IMAGE", image_diff_pid_); |
| 1286 | os << "\n\n"; |
| 1287 | PrintPidLine("ZYGOTE", zygote_diff_pid_); |
| 1288 | bool ret = true; |
| 1289 | if (image_diff_pid_ >= 0 || zygote_diff_pid_ >= 0) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1290 | ret = DumpImageDiff(image_header, image_location); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1291 | os << "\n\n"; |
| 1292 | } |
| 1293 | |
| 1294 | os << std::flush; |
| 1295 | |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | private: |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1300 | bool DumpImageDiff(const ImageHeader& image_header, const std::string& image_location) |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1301 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1302 | return DumpImageDiffMap(image_header, image_location); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1305 | bool ComputeDirtyBytes(const ImageHeader& image_header, |
| 1306 | const uint8_t* image_begin, |
| 1307 | const backtrace_map_t& boot_map, |
| 1308 | const std::vector<uint8_t>& remote_contents, |
| 1309 | MappingData* mapping_data /*out*/) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1310 | std::ostream& os = *os_; |
| 1311 | |
| 1312 | size_t virtual_page_idx = 0; // Virtual page number (for an absolute memory address) |
| 1313 | size_t page_idx = 0; // Page index relative to 0 |
| 1314 | size_t previous_page_idx = 0; // Previous page index relative to 0 |
| 1315 | |
| 1316 | |
| 1317 | // Iterate through one page at a time. Boot map begin/end already implicitly aligned. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1318 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) { |
| 1319 | ptrdiff_t offset = begin - boot_map.start; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1320 | |
| 1321 | // We treat the image header as part of the memory map for now |
| 1322 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 1323 | // But it might still be interesting to see if any of the ImageHeader data mutated |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1324 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset; |
| 1325 | const uint8_t* remote_ptr = &remote_contents[offset]; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1326 | |
| 1327 | if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1328 | mapping_data->different_pages++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1329 | |
| 1330 | // Count the number of 32-bit integers that are different. |
| 1331 | for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) { |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1332 | const uint32_t* remote_ptr_int32 = reinterpret_cast<const uint32_t*>(remote_ptr); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1333 | const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr); |
| 1334 | |
| 1335 | if (remote_ptr_int32[i] != local_ptr_int32[i]) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1336 | mapping_data->different_int32s++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1342 | std::vector<size_t> private_dirty_pages_for_section(ImageHeader::kSectionCount, 0u); |
| 1343 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1344 | // Iterate through one byte at a time. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1345 | ptrdiff_t page_off_begin = image_header.GetImageBegin() - image_begin; |
| 1346 | for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1347 | previous_page_idx = page_idx; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1348 | ptrdiff_t offset = begin - boot_map.start; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1349 | |
| 1350 | // We treat the image header as part of the memory map for now |
| 1351 | // If we wanted to change this, we could pass base=start+sizeof(ImageHeader) |
| 1352 | // But it might still be interesting to see if any of the ImageHeader data mutated |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1353 | const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + offset; |
| 1354 | const uint8_t* remote_ptr = &remote_contents[offset]; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1355 | |
| 1356 | virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize; |
| 1357 | |
| 1358 | // Calculate the page index, relative to the 0th page where the image begins |
| 1359 | page_idx = (offset + page_off_begin) / kPageSize; |
| 1360 | if (*local_ptr != *remote_ptr) { |
| 1361 | // Track number of bytes that are different |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1362 | mapping_data->different_bytes++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | // Independently count the # of dirty pages on the remote side |
| 1366 | size_t remote_virtual_page_idx = begin / kPageSize; |
| 1367 | if (previous_page_idx != page_idx) { |
| 1368 | uint64_t page_count = 0xC0FFEE; |
| 1369 | // TODO: virtual_page_idx needs to be from the same process |
| 1370 | std::string error_msg; |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1371 | int dirtiness = (IsPageDirty(&image_pagemap_file_, // Image-diff-pid procmap |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1372 | &clean_pagemap_file_, // Self procmap |
| 1373 | &kpageflags_file_, |
| 1374 | &kpagecount_file_, |
| 1375 | remote_virtual_page_idx, // potentially "dirty" page |
| 1376 | virtual_page_idx, // true "clean" page |
| 1377 | &page_count, |
| 1378 | &error_msg)); |
| 1379 | if (dirtiness < 0) { |
| 1380 | os << error_msg; |
| 1381 | return false; |
| 1382 | } else if (dirtiness > 0) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1383 | mapping_data->dirty_pages++; |
| 1384 | mapping_data->dirty_page_set.insert(mapping_data->dirty_page_set.end(), virtual_page_idx); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | bool is_dirty = dirtiness > 0; |
| 1388 | bool is_private = page_count == 1; |
| 1389 | |
| 1390 | if (page_count == 1) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1391 | mapping_data->private_pages++; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | if (is_dirty && is_private) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1395 | mapping_data->private_dirty_pages++; |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1396 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1397 | const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1398 | if (image_header.GetImageSection(section).Contains(offset)) { |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1399 | ++private_dirty_pages_for_section[i]; |
| 1400 | } |
| 1401 | } |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1405 | mapping_data->false_dirty_pages = mapping_data->dirty_pages - mapping_data->different_pages; |
| 1406 | // Print low-level (bytes, int32s, pages) statistics. |
| 1407 | os << mapping_data->different_bytes << " differing bytes,\n " |
| 1408 | << mapping_data->different_int32s << " differing int32s,\n " |
| 1409 | << mapping_data->different_pages << " differing pages,\n " |
| 1410 | << mapping_data->dirty_pages << " pages are dirty;\n " |
| 1411 | << mapping_data->false_dirty_pages << " pages are false dirty;\n " |
| 1412 | << mapping_data->private_pages << " pages are private;\n " |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1413 | << mapping_data->private_dirty_pages << " pages are Private_Dirty\n " |
| 1414 | << "\n"; |
| 1415 | |
| 1416 | size_t total_private_dirty_pages = std::accumulate(private_dirty_pages_for_section.begin(), |
| 1417 | private_dirty_pages_for_section.end(), |
| 1418 | 0u); |
| 1419 | os << "Image sections (total private dirty pages " << total_private_dirty_pages << ")\n"; |
| 1420 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1421 | const ImageHeader::ImageSections section = static_cast<ImageHeader::ImageSections>(i); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1422 | os << section << " " << image_header.GetImageSection(section) |
Mathieu Chartier | 728f850 | 2017-07-28 17:35:30 -0700 | [diff] [blame] | 1423 | << " private dirty pages=" << private_dirty_pages_for_section[i] << "\n"; |
| 1424 | } |
| 1425 | os << "\n"; |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1426 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1427 | return true; |
| 1428 | } |
| 1429 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1430 | // Look at /proc/$pid/mem and only diff the things from there |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1431 | bool DumpImageDiffMap(const ImageHeader& image_header, const std::string& image_location) |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1432 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1433 | std::ostream& os = *os_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1434 | std::string error_msg; |
| 1435 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1436 | std::string image_location_base_name = GetImageLocationBaseName(image_location); |
| 1437 | // FIXME: BacktraceMap should provide a const_iterator so that we can take `maps` as const&. |
| 1438 | auto find_boot_map = [&os, &image_location_base_name](BacktraceMap& maps, const char* tag) |
| 1439 | -> std::optional<backtrace_map_t> { |
| 1440 | // Find the memory map for the current boot image component. |
| 1441 | for (const backtrace_map_t* map : maps) { |
| 1442 | if (EndsWith(map->name, image_location_base_name)) { |
| 1443 | if ((map->flags & PROT_WRITE) != 0) { |
| 1444 | return *map; |
| 1445 | } |
| 1446 | // In actuality there's more than 1 map, but the second one is read-only. |
| 1447 | // The one we care about is the write-able map. |
| 1448 | // The readonly maps are guaranteed to be identical, so its not interesting to compare |
| 1449 | // them. |
| 1450 | } |
| 1451 | } |
| 1452 | os << "Could not find map for " << image_location_base_name << " in " << tag; |
| 1453 | return std::nullopt; |
| 1454 | }; |
| 1455 | |
| 1456 | // Find the current boot image mapping. |
| 1457 | std::optional<backtrace_map_t> maybe_boot_map = find_boot_map(*image_proc_maps_, "image"); |
| 1458 | if (maybe_boot_map == std::nullopt) { |
| 1459 | return false; |
| 1460 | } |
| 1461 | backtrace_map_t boot_map = maybe_boot_map.value_or(backtrace_map_t{}); |
| 1462 | // Sanity check boot_map_. |
| 1463 | CHECK(boot_map.end >= boot_map.start); |
| 1464 | // The size of the boot image mapping. |
| 1465 | size_t boot_map_size = boot_map.end - boot_map.start; |
| 1466 | |
| 1467 | // If zygote_diff_pid_ != -1, check that the zygote boot map is the same. |
| 1468 | if (zygote_diff_pid_ != -1) { |
| 1469 | std::optional<backtrace_map_t> maybe_zygote_boot_map = |
| 1470 | find_boot_map(*zygote_proc_maps_, "zygote"); |
| 1471 | if (maybe_zygote_boot_map == std::nullopt) { |
| 1472 | return false; |
| 1473 | } |
| 1474 | backtrace_map_t zygote_boot_map = maybe_zygote_boot_map.value_or(backtrace_map_t{}); |
| 1475 | if (zygote_boot_map.start != boot_map.start || zygote_boot_map.end != boot_map.end) { |
| 1476 | os << "Zygote boot map does not match image boot map: " |
| 1477 | << "zygote begin " << reinterpret_cast<const void*>(zygote_boot_map.start) |
| 1478 | << ", zygote end " << reinterpret_cast<const void*>(zygote_boot_map.end) |
| 1479 | << ", image begin " << reinterpret_cast<const void*>(boot_map.start) |
| 1480 | << ", image end " << reinterpret_cast<const void*>(boot_map.end); |
| 1481 | return false; |
| 1482 | } |
| 1483 | } |
| 1484 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1485 | // Walk the bytes and diff against our boot image |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1486 | os << "\nObserving boot image header at address " |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1487 | << reinterpret_cast<const void*>(&image_header) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1488 | << "\n\n"; |
| 1489 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1490 | const uint8_t* image_begin_unaligned = image_header.GetImageBegin(); |
| 1491 | const uint8_t* image_end_unaligned = image_begin_unaligned + image_header.GetImageSize(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1492 | |
| 1493 | // Adjust range to nearest page |
| 1494 | const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize); |
| 1495 | const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize); |
| 1496 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1497 | size_t image_size = image_end - image_begin; |
| 1498 | if (image_size != boot_map_size) { |
| 1499 | os << "Remote boot map size does not match local boot map size: " |
| 1500 | << "local size " << image_size |
| 1501 | << ", remote size " << boot_map_size; |
| 1502 | return false; |
| 1503 | } |
| 1504 | |
| 1505 | // The contents of /proc/<image_diff_pid_>/maps. |
| 1506 | std::vector<uint8_t> remote_contents(boot_map_size); |
| 1507 | if (!image_mem_file_.PreadFully(remote_contents.data(), boot_map_size, boot_map.start)) { |
| 1508 | os << "Could not fully read file " << image_mem_file_.GetPath(); |
| 1509 | return false; |
| 1510 | } |
| 1511 | // The contents of /proc/<zygote_diff_pid_>/maps. |
| 1512 | std::vector<uint8_t> zygote_contents; |
| 1513 | if (zygote_diff_pid_ != -1) { |
| 1514 | zygote_contents.resize(boot_map_size); |
| 1515 | if (!zygote_mem_file_.PreadFully(zygote_contents.data(), boot_map_size, boot_map.start)) { |
| 1516 | LOG(WARNING) << "Could not fully read zygote file " << zygote_mem_file_.GetPath(); |
| 1517 | return false; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | // FIXME: Because of ASLR, this check shall fail for most processes. |
| 1522 | // We need to update the entire diff to work with the ASLR. b/77856493 |
| 1523 | if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start || |
| 1524 | reinterpret_cast<uintptr_t>(image_end) < boot_map.end) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1525 | // Sanity check that we aren't trying to read a completely different boot image |
| 1526 | os << "Remote boot map is out of range of local boot map: " << |
| 1527 | "local begin " << reinterpret_cast<const void*>(image_begin) << |
| 1528 | ", local end " << reinterpret_cast<const void*>(image_end) << |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1529 | ", remote begin " << reinterpret_cast<const void*>(boot_map.start) << |
| 1530 | ", remote end " << reinterpret_cast<const void*>(boot_map.end); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1531 | return false; |
| 1532 | // If we wanted even more validation we could map the ImageHeader from the file |
| 1533 | } |
| 1534 | |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1535 | MappingData mapping_data; |
David Sehr | 45de57f | 2017-06-21 05:03:22 +0000 | [diff] [blame] | 1536 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1537 | os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", " |
| 1538 | << reinterpret_cast<void*>(boot_map.end) << ") had:\n "; |
| 1539 | if (!ComputeDirtyBytes(image_header, image_begin, boot_map, remote_contents, &mapping_data)) { |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1540 | return false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1541 | } |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1542 | RemoteProcesses remotes; |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1543 | if (zygote_pid_only_) { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1544 | remotes = RemoteProcesses::kZygoteOnly; |
| 1545 | } else if (zygote_diff_pid_ > 0) { |
| 1546 | remotes = RemoteProcesses::kImageAndZygote; |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1547 | } else { |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1548 | remotes = RemoteProcesses::kImageOnly; |
Mathieu Chartier | cb044bc | 2016-04-01 13:56:41 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1551 | // Check all the mirror::Object entries in the image. |
| 1552 | RegionData<mirror::Object> object_region_data(os_, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1553 | &remote_contents, |
| 1554 | &zygote_contents, |
| 1555 | boot_map, |
| 1556 | image_header, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1557 | dump_dirty_objects_); |
David Sehr | b4005f0 | 2017-06-20 19:11:40 -0700 | [diff] [blame] | 1558 | object_region_data.ProcessRegion(mapping_data, |
| 1559 | remotes, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1560 | image_begin_unaligned); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1561 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1562 | // Check all the ArtMethod entries in the image. |
| 1563 | RegionData<ArtMethod> artmethod_region_data(os_, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1564 | &remote_contents, |
| 1565 | &zygote_contents, |
| 1566 | boot_map, |
| 1567 | image_header, |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 1568 | dump_dirty_objects_); |
| 1569 | artmethod_region_data.ProcessRegion(mapping_data, |
| 1570 | remotes, |
| 1571 | image_begin_unaligned); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1572 | return true; |
| 1573 | } |
| 1574 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1575 | // Note: On failure, `*page_frame_number` shall be clobbered. |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1576 | static bool GetPageFrameNumber(File* page_map_file, |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1577 | size_t virtual_page_index, |
| 1578 | /*out*/ uint64_t* page_frame_number, |
| 1579 | /*out*/ std::string* error_msg) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1580 | CHECK(page_frame_number != nullptr); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1581 | return GetPageFrameNumbers(page_map_file, |
| 1582 | virtual_page_index, |
| 1583 | ArrayRef<uint64_t>(page_frame_number, 1u), |
| 1584 | error_msg); |
| 1585 | } |
| 1586 | |
| 1587 | // Note: On failure, `page_frame_numbers[.]` shall be clobbered. |
| 1588 | static bool GetPageFrameNumbers(File* page_map_file, |
| 1589 | size_t virtual_page_index, |
| 1590 | /*out*/ ArrayRef<uint64_t> page_frame_numbers, |
| 1591 | /*out*/ std::string* error_msg) { |
| 1592 | CHECK(page_map_file != nullptr); |
| 1593 | CHECK_NE(page_frame_numbers.size(), 0u); |
| 1594 | CHECK(page_frame_numbers.data() != nullptr); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1595 | CHECK(error_msg != nullptr); |
| 1596 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1597 | // Read 64-bit entries from /proc/$pid/pagemap to get the physical page frame numbers. |
| 1598 | if (!page_map_file->PreadFully(page_frame_numbers.data(), |
| 1599 | page_frame_numbers.size() * kPageMapEntrySize, |
| 1600 | virtual_page_index * kPageMapEntrySize)) { |
| 1601 | *error_msg = StringPrintf("Failed to read the virtual page index entries from %s, error: %s", |
| 1602 | page_map_file->GetPath().c_str(), |
| 1603 | strerror(errno)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1604 | return false; |
| 1605 | } |
| 1606 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1607 | // Extract page frame numbers from pagemap entries. |
| 1608 | for (uint64_t& page_frame_number : page_frame_numbers) { |
| 1609 | page_frame_number &= kPageFrameNumberMask; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1612 | return true; |
| 1613 | } |
| 1614 | |
| 1615 | // Note: On failure, `page_flags_or_counts[.]` shall be clobbered. |
| 1616 | static bool GetPageFlagsOrCounts(File* kpage_file, |
| 1617 | ArrayRef<const uint64_t> page_frame_numbers, |
| 1618 | /*out*/ ArrayRef<uint64_t> page_flags_or_counts, |
| 1619 | /*out*/ std::string* error_msg) { |
| 1620 | static_assert(kPageFlagsEntrySize == kPageCountEntrySize, "entry size check"); |
| 1621 | CHECK_NE(page_frame_numbers.size(), 0u); |
| 1622 | CHECK_EQ(page_flags_or_counts.size(), page_frame_numbers.size()); |
| 1623 | CHECK(kpage_file != nullptr); |
| 1624 | CHECK(page_frame_numbers.data() != nullptr); |
| 1625 | CHECK(page_flags_or_counts.data() != nullptr); |
| 1626 | CHECK(error_msg != nullptr); |
| 1627 | |
| 1628 | size_t size = page_frame_numbers.size(); |
| 1629 | size_t i = 0; |
| 1630 | while (i != size) { |
| 1631 | size_t start = i; |
| 1632 | ++i; |
| 1633 | while (i != size && page_frame_numbers[i] - page_frame_numbers[start] == i - start) { |
| 1634 | ++i; |
| 1635 | } |
| 1636 | // Read 64-bit entries from /proc/kpageflags or /proc/kpagecount. |
| 1637 | if (!kpage_file->PreadFully(page_flags_or_counts.data() + start, |
| 1638 | (i - start) * kPageMapEntrySize, |
| 1639 | page_frame_numbers[start] * kPageFlagsEntrySize)) { |
| 1640 | *error_msg = StringPrintf("Failed to read the page flags or counts from %s, error: %s", |
| 1641 | kpage_file->GetPath().c_str(), |
| 1642 | strerror(errno)); |
| 1643 | return false; |
| 1644 | } |
| 1645 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1646 | |
| 1647 | return true; |
| 1648 | } |
| 1649 | |
| 1650 | static int IsPageDirty(File* page_map_file, |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1651 | File* clean_pagemap_file, |
| 1652 | File* kpageflags_file, |
| 1653 | File* kpagecount_file, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1654 | size_t virtual_page_idx, |
| 1655 | size_t clean_virtual_page_idx, |
| 1656 | // Out parameters: |
| 1657 | uint64_t* page_count, std::string* error_msg) { |
| 1658 | CHECK(page_map_file != nullptr); |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1659 | CHECK(clean_pagemap_file != nullptr); |
| 1660 | CHECK_NE(page_map_file, clean_pagemap_file); |
| 1661 | CHECK(kpageflags_file != nullptr); |
| 1662 | CHECK(kpagecount_file != nullptr); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1663 | CHECK(page_count != nullptr); |
| 1664 | CHECK(error_msg != nullptr); |
| 1665 | |
| 1666 | // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt |
| 1667 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1668 | uint64_t page_frame_number = 0; |
| 1669 | if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) { |
| 1670 | return -1; |
| 1671 | } |
| 1672 | |
| 1673 | uint64_t page_frame_number_clean = 0; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1674 | if (!GetPageFrameNumber(clean_pagemap_file, clean_virtual_page_idx, &page_frame_number_clean, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1675 | error_msg)) { |
| 1676 | return -1; |
| 1677 | } |
| 1678 | |
| 1679 | // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page |
| 1680 | uint64_t kpage_flags_entry = 0; |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1681 | if (!kpageflags_file->PreadFully(&kpage_flags_entry, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1682 | kPageFlagsEntrySize, |
| 1683 | page_frame_number * kPageFlagsEntrySize)) { |
| 1684 | *error_msg = StringPrintf("Failed to read the page flags from %s", |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1685 | kpageflags_file->GetPath().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1686 | return -1; |
| 1687 | } |
| 1688 | |
| 1689 | // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1690 | if (!kpagecount_file->PreadFully(page_count /*out*/, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1691 | kPageCountEntrySize, |
| 1692 | page_frame_number * kPageCountEntrySize)) { |
| 1693 | *error_msg = StringPrintf("Failed to read the page count from %s", |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1694 | kpagecount_file->GetPath().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1695 | return -1; |
| 1696 | } |
| 1697 | |
| 1698 | // There must be a page frame at the requested address. |
| 1699 | CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u); |
| 1700 | // The page frame must be memory mapped |
| 1701 | CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u); |
| 1702 | |
| 1703 | // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1 |
| 1704 | bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0; |
| 1705 | |
| 1706 | // page_frame_number_clean must come from the *same* process |
| 1707 | // but a *different* mmap than page_frame_number |
| 1708 | if (flags_dirty) { |
Vladimir Marko | 20af92f | 2019-03-06 16:18:56 +0000 | [diff] [blame] | 1709 | CHECK_NE(page_frame_number, page_frame_number_clean) |
| 1710 | << " count: " << *page_count << " flags: 0x" << std::hex << kpage_flags_entry; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return page_frame_number != page_frame_number_clean; |
| 1714 | } |
| 1715 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1716 | void PrintPidLine(const std::string& kind, pid_t pid) { |
| 1717 | if (pid < 0) { |
| 1718 | *os_ << kind << " DIFF PID: disabled\n\n"; |
| 1719 | } else { |
| 1720 | *os_ << kind << " DIFF PID (" << pid << "): "; |
| 1721 | } |
| 1722 | } |
| 1723 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1724 | // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar) |
| 1725 | static std::string BaseName(const std::string& str) { |
| 1726 | size_t idx = str.rfind('/'); |
| 1727 | if (idx == std::string::npos) { |
| 1728 | return str; |
| 1729 | } |
| 1730 | |
| 1731 | return str.substr(idx + 1); |
| 1732 | } |
| 1733 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1734 | // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art" |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1735 | static std::string GetImageLocationBaseName(const std::string& image_location) { |
| 1736 | return BaseName(std::string(image_location)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1739 | static constexpr size_t kPageMapEntrySize = sizeof(uint64_t); |
| 1740 | // bits 0-54 [in /proc/$pid/pagemap] |
| 1741 | static constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1; |
| 1742 | |
| 1743 | static constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t); |
| 1744 | static constexpr size_t kPageCountEntrySize = sizeof(uint64_t); |
| 1745 | static constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4); // in /proc/kpageflags |
| 1746 | static constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20); // in /proc/kpageflags |
| 1747 | static constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11); // in /proc/kpageflags |
| 1748 | |
| 1749 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1750 | std::ostream* os_; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1751 | pid_t image_diff_pid_; // Dump image diff against boot.art if pid is non-negative |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1752 | pid_t zygote_diff_pid_; // Dump image diff against zygote boot.art if pid is non-negative |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1753 | bool dump_dirty_objects_; // Adds dumping of objects that are dirty. |
David Sehr | 20e271a | 2017-06-14 13:02:14 -0700 | [diff] [blame] | 1754 | bool zygote_pid_only_; // The user only specified a pid for the zygote. |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1755 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1756 | // BacktraceMap used for finding the memory mapping of the image file. |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1757 | std::unique_ptr<BacktraceMap> image_proc_maps_; |
| 1758 | // A File for reading /proc/<image_diff_pid_>/mem. |
| 1759 | File image_mem_file_; |
| 1760 | // A File for reading /proc/<image_diff_pid_>/pagemap. |
| 1761 | File image_pagemap_file_; |
| 1762 | |
| 1763 | // BacktraceMap used for finding the memory mapping of the zygote image file. |
| 1764 | std::unique_ptr<BacktraceMap> zygote_proc_maps_; |
| 1765 | // A File for reading /proc/<zygote_diff_pid_>/mem. |
| 1766 | File zygote_mem_file_; |
| 1767 | // A File for reading /proc/<zygote_diff_pid_>/pagemap. |
| 1768 | File zygote_pagemap_file_; |
| 1769 | |
David Sehr | 50005a0 | 2017-06-21 13:24:21 -0700 | [diff] [blame] | 1770 | // A File for reading /proc/self/pagemap. |
| 1771 | File clean_pagemap_file_; |
| 1772 | // A File for reading /proc/kpageflags. |
| 1773 | File kpageflags_file_; |
| 1774 | // A File for reading /proc/kpagecount. |
| 1775 | File kpagecount_file_; |
| 1776 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1777 | DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper); |
| 1778 | }; |
| 1779 | |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1780 | static int DumpImage(Runtime* runtime, |
| 1781 | std::ostream* os, |
| 1782 | pid_t image_diff_pid, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1783 | pid_t zygote_diff_pid, |
| 1784 | bool dump_dirty_objects) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1785 | ScopedObjectAccess soa(Thread::Current()); |
| 1786 | gc::Heap* heap = runtime->GetHeap(); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1787 | const std::vector<gc::space::ImageSpace*>& image_spaces = heap->GetBootImageSpaces(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1788 | CHECK(!image_spaces.empty()); |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1789 | ImgDiagDumper img_diag_dumper(os, |
| 1790 | image_diff_pid, |
| 1791 | zygote_diff_pid, |
| 1792 | dump_dirty_objects); |
| 1793 | if (!img_diag_dumper.Init()) { |
| 1794 | return EXIT_FAILURE; |
| 1795 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1796 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 1797 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1798 | if (!image_header.IsValid()) { |
| 1799 | fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str()); |
| 1800 | return EXIT_FAILURE; |
| 1801 | } |
| 1802 | |
Vladimir Marko | 1f146b7 | 2019-03-08 16:28:08 +0000 | [diff] [blame] | 1803 | if (!img_diag_dumper.Dump(image_header, image_space->GetImageLocation())) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1804 | return EXIT_FAILURE; |
| 1805 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1806 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1807 | return EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | struct ImgDiagArgs : public CmdlineArgs { |
| 1811 | protected: |
| 1812 | using Base = CmdlineArgs; |
| 1813 | |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1814 | ParseStatus ParseCustom(const char* raw_option, |
| 1815 | size_t raw_option_length, |
| 1816 | std::string* error_msg) override { |
| 1817 | DCHECK_EQ(strlen(raw_option), raw_option_length); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1818 | { |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1819 | ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1820 | if (base_parse != kParseUnknownArgument) { |
| 1821 | return base_parse; |
| 1822 | } |
| 1823 | } |
| 1824 | |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1825 | std::string_view option(raw_option, raw_option_length); |
| 1826 | if (StartsWith(option, "--image-diff-pid=")) { |
| 1827 | const char* image_diff_pid = raw_option + strlen("--image-diff-pid="); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1828 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 1829 | if (!android::base::ParseInt(image_diff_pid, &image_diff_pid_)) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1830 | *error_msg = "Image diff pid out of range"; |
| 1831 | return kParseError; |
| 1832 | } |
Vladimir Marko | 8581e2a | 2019-02-06 15:54:55 +0000 | [diff] [blame] | 1833 | } else if (StartsWith(option, "--zygote-diff-pid=")) { |
| 1834 | const char* zygote_diff_pid = raw_option + strlen("--zygote-diff-pid="); |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1835 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 1836 | if (!android::base::ParseInt(zygote_diff_pid, &zygote_diff_pid_)) { |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1837 | *error_msg = "Zygote diff pid out of range"; |
| 1838 | return kParseError; |
| 1839 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1840 | } else if (option == "--dump-dirty-objects") { |
| 1841 | dump_dirty_objects_ = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1842 | } else { |
| 1843 | return kParseUnknownArgument; |
| 1844 | } |
| 1845 | |
| 1846 | return kParseOk; |
| 1847 | } |
| 1848 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 1849 | ParseStatus ParseChecks(std::string* error_msg) override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1850 | // Perform the parent checks. |
| 1851 | ParseStatus parent_checks = Base::ParseChecks(error_msg); |
| 1852 | if (parent_checks != kParseOk) { |
| 1853 | return parent_checks; |
| 1854 | } |
| 1855 | |
| 1856 | // Perform our own checks. |
| 1857 | |
| 1858 | if (kill(image_diff_pid_, |
| 1859 | /*sig*/0) != 0) { // No signal is sent, perform error-checking only. |
| 1860 | // Check if the pid exists before proceeding. |
| 1861 | if (errno == ESRCH) { |
| 1862 | *error_msg = "Process specified does not exist"; |
| 1863 | } else { |
| 1864 | *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno)); |
| 1865 | } |
| 1866 | return kParseError; |
Andreas Gampe | 8fae4b5 | 2017-09-27 20:04:47 -0700 | [diff] [blame] | 1867 | } else if (instruction_set_ != InstructionSet::kNone && instruction_set_ != kRuntimeISA) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1868 | // Don't allow different ISAs since the images are ISA-specific. |
| 1869 | // Right now the code assumes both the runtime ISA and the remote ISA are identical. |
| 1870 | *error_msg = "Must use the default runtime ISA; changing ISA is not supported."; |
| 1871 | return kParseError; |
| 1872 | } |
| 1873 | |
| 1874 | return kParseOk; |
| 1875 | } |
| 1876 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1877 | std::string GetUsage() const override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1878 | std::string usage; |
| 1879 | |
| 1880 | usage += |
| 1881 | "Usage: imgdiag [options] ...\n" |
| 1882 | " Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n" |
| 1883 | " Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n" |
| 1884 | "\n"; |
| 1885 | |
| 1886 | usage += Base::GetUsage(); |
| 1887 | |
| 1888 | usage += // Optional. |
| 1889 | " --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n" |
| 1890 | " Example: --image-diff-pid=$(pid zygote)\n" |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1891 | " --zygote-diff-pid=<pid>: provide the PID of the zygote whose boot.art you want to diff " |
| 1892 | "against.\n" |
| 1893 | " Example: --zygote-diff-pid=$(pid zygote)\n" |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1894 | " --dump-dirty-objects: additionally output dirty objects of interest.\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1895 | "\n"; |
| 1896 | |
| 1897 | return usage; |
| 1898 | } |
| 1899 | |
| 1900 | public: |
| 1901 | pid_t image_diff_pid_ = -1; |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1902 | pid_t zygote_diff_pid_ = -1; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1903 | bool dump_dirty_objects_ = false; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1904 | }; |
| 1905 | |
| 1906 | struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> { |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1907 | bool ExecuteWithRuntime(Runtime* runtime) override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1908 | CHECK(args_ != nullptr); |
| 1909 | |
| 1910 | return DumpImage(runtime, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1911 | args_->os_, |
Mathieu Chartier | c5196cd | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 1912 | args_->image_diff_pid_, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 1913 | args_->zygote_diff_pid_, |
| 1914 | args_->dump_dirty_objects_) == EXIT_SUCCESS; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 1915 | } |
| 1916 | }; |
| 1917 | |
| 1918 | } // namespace art |
| 1919 | |
| 1920 | int main(int argc, char** argv) { |
| 1921 | art::ImgDiagMain main; |
| 1922 | return main.Main(argc, argv); |
| 1923 | } |