-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathimgdiag.cc
1965 lines (1748 loc) · 76.1 KB
/
imgdiag.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <android-base/parseint.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <functional>
#include <map>
#include <optional>
#include <ostream>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
#include "android-base/stringprintf.h"
#include "art_field-inl.h"
#include "art_method-inl.h"
#include "base/array_ref.h"
#include "base/os.h"
#include "base/string_view_cpp20.h"
#include "base/unix_file/fd_file.h"
#include "class_linker.h"
#include "cmdline.h"
#include "gc/heap.h"
#include "gc/space/image_space.h"
#include "mirror/class-inl.h"
#include "mirror/object-inl.h"
#include "mirror/object-refvisitor-inl.h"
#include "oat/image-inl.h"
#include "oat/oat.h"
#include "oat/oat_file.h"
#include "oat/oat_file_manager.h"
#include "page_util.h"
#include "procinfo/process_map.h"
#include "scoped_thread_state_change-inl.h"
namespace art {
using android::base::StringPrintf;
namespace {
constexpr size_t kMaxAddressPrint = 5;
enum class ProcessType {
kZygote,
kRemote
};
enum class RemoteProcesses {
kImageOnly,
kZygoteOnly,
kImageAndZygote
};
std::ostream& operator<<(std::ostream& os, RemoteProcesses remotes) {
switch (remotes) {
case RemoteProcesses::kImageOnly: os << "ImageOnly"; break;
case RemoteProcesses::kZygoteOnly: os << "ZygoteOnly"; break;
case RemoteProcesses::kImageAndZygote: os << "ImageAndZygote"; break;
default: UNREACHABLE();
}
return os;
}
struct MappingData {
// The count of pages that are considered dirty by the OS.
size_t dirty_pages = 0;
// The count of pages that differ by at least one byte.
size_t different_pages = 0;
// The count of differing bytes.
size_t different_bytes = 0;
// The count of differing four-byte units.
size_t different_int32s = 0;
// The count of pages that have mapping count == 1.
size_t private_pages = 0;
// The count of private pages that are also dirty.
size_t private_dirty_pages = 0;
// The count of pages that are marked dirty but do not differ.
size_t false_dirty_pages = 0;
// Set of the local virtual page indices that are dirty.
std::set<size_t> dirty_page_set;
// Private dirty page counts for each section of the image
std::array<size_t, ImageHeader::kSectionCount> private_dirty_pages_for_section = {};
};
static std::string GetClassDescriptor(mirror::Class* klass)
REQUIRES_SHARED(Locks::mutator_lock_) {
CHECK(klass != nullptr);
std::string descriptor;
const char* descriptor_str = klass->GetDescriptor(&descriptor /*out*/);
return std::string(descriptor_str);
}
static std::string PrettyFieldValue(ArtField* field, mirror::Object* object)
REQUIRES_SHARED(Locks::mutator_lock_) {
std::ostringstream oss;
switch (field->GetTypeAsPrimitiveType()) {
case Primitive::kPrimNot: {
oss << object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
field->GetOffset());
break;
}
case Primitive::kPrimBoolean: {
oss << static_cast<bool>(object->GetFieldBoolean<kVerifyNone>(field->GetOffset()));
break;
}
case Primitive::kPrimByte: {
oss << static_cast<int32_t>(object->GetFieldByte<kVerifyNone>(field->GetOffset()));
break;
}
case Primitive::kPrimChar: {
oss << object->GetFieldChar<kVerifyNone>(field->GetOffset());
break;
}
case Primitive::kPrimShort: {
oss << object->GetFieldShort<kVerifyNone>(field->GetOffset());
break;
}
case Primitive::kPrimInt: {
oss << object->GetField32<kVerifyNone>(field->GetOffset());
break;
}
case Primitive::kPrimLong: {
oss << object->GetField64<kVerifyNone>(field->GetOffset());
break;
}
case Primitive::kPrimFloat: {
oss << object->GetField32<kVerifyNone>(field->GetOffset());
break;
}
case Primitive::kPrimDouble: {
oss << object->GetField64<kVerifyNone>(field->GetOffset());
break;
}
case Primitive::kPrimVoid: {
oss << "void";
break;
}
}
return oss.str();
}
template <typename K, typename V, typename D>
static std::vector<std::pair<V, K>> SortByValueDesc(
const std::map<K, D> map,
std::function<V(const D&)> value_mapper = [](const D& d) { return static_cast<V>(d); }) {
// Store value->key so that we can use the default sort from pair which
// sorts by value first and then key
std::vector<std::pair<V, K>> value_key_vector;
value_key_vector.reserve(map.size());
for (const auto& kv_pair : map) {
value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
}
// Sort in reverse (descending order)
std::sort(value_key_vector.rbegin(), value_key_vector.rend());
return value_key_vector;
}
// Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
// Returned pointer will point to inside of remote_contents.
template <typename T>
static ObjPtr<T> FixUpRemotePointer(ObjPtr<T> remote_ptr,
ArrayRef<uint8_t> remote_contents,
const android::procinfo::MapInfo& boot_map)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (remote_ptr == nullptr) {
return nullptr;
}
uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr.Ptr());
// In the case the remote pointer is out of range, it probably belongs to another image.
// Just return null for this case.
if (remote < boot_map.start || remote >= boot_map.end) {
return nullptr;
}
off_t boot_offset = remote - boot_map.start;
return reinterpret_cast<T*>(&remote_contents[boot_offset]);
}
template <typename T>
static ObjPtr<T> RemoteContentsPointerToLocal(ObjPtr<T> remote_ptr,
ArrayRef<uint8_t> remote_contents,
const ImageHeader& image_header)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (remote_ptr == nullptr) {
return nullptr;
}
uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr.Ptr());
ptrdiff_t boot_offset = remote - &remote_contents[0];
const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
}
size_t EntrySize(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
return object->SizeOf();
}
size_t EntrySize(ArtMethod* art_method) REQUIRES_SHARED(Locks::mutator_lock_) {
return sizeof(*art_method);
}
// Print all pages the entry belongs to
void PrintEntryPages(uintptr_t entry_address, size_t entry_size, std::ostream& os) {
const char* tabs = " ";
const uintptr_t first_page_idx = entry_address / MemMap::GetPageSize();
const uintptr_t last_page_idx = RoundUp(entry_address + entry_size,
kObjectAlignment) / MemMap::GetPageSize();
for (uintptr_t page_idx = first_page_idx; page_idx <= last_page_idx; ++page_idx) {
os << tabs << "page_idx=" << page_idx << "\n";
}
}
// entry1 and entry2 might be relocated, this means we must use the runtime image's entry
// (image_entry) to avoid crashes.
template <typename T>
static bool EntriesDiffer(T* image_entry,
T* entry1,
T* entry2) REQUIRES_SHARED(Locks::mutator_lock_) {
// Use the image entry since entry1 and entry2 might both be remote and relocated.
return memcmp(entry1, entry2, EntrySize(image_entry)) != 0;
}
template <typename T>
struct RegionCommon {
public:
RegionCommon(std::ostream* os,
ArrayRef<uint8_t> remote_contents,
ArrayRef<uint8_t> zygote_contents,
const android::procinfo::MapInfo& boot_map,
const ImageHeader& image_header) :
os_(*os),
remote_contents_(remote_contents),
zygote_contents_(zygote_contents),
boot_map_(boot_map),
image_header_(image_header),
different_entries_(0),
dirty_entry_bytes_(0),
false_dirty_entry_bytes_(0) {
CHECK(!remote_contents.empty());
}
void DumpSamplesAndOffsetCount() {
os_ << " sample object addresses: ";
for (size_t i = 0; i < dirty_entries_.size() && i < kMaxAddressPrint; ++i) {
T* entry = dirty_entries_[i];
os_ << reinterpret_cast<void*>(entry) << ", ";
}
os_ << "\n";
os_ << " dirty byte +offset:count list = ";
std::vector<std::pair<size_t, off_t>> field_dirty_count_sorted =
SortByValueDesc<off_t, size_t, size_t>(field_dirty_count_);
for (const std::pair<size_t, off_t>& pair : field_dirty_count_sorted) {
off_t offset = pair.second;
size_t count = pair.first;
os_ << "+" << offset << ":" << count << ", ";
}
os_ << "\n";
}
size_t GetDifferentEntryCount() const { return different_entries_; }
size_t GetDirtyEntryBytes() const { return dirty_entry_bytes_; }
size_t GetFalseDirtyEntryCount() const { return false_dirty_entries_.size(); }
size_t GetFalseDirtyEntryBytes() const { return false_dirty_entry_bytes_; }
protected:
bool IsEntryOnDirtyPage(T* entry, const std::set<size_t>& dirty_pages) const
REQUIRES_SHARED(Locks::mutator_lock_) {
size_t size = EntrySize(entry);
size_t page_off = 0;
size_t current_page_idx;
uintptr_t entry_address = reinterpret_cast<uintptr_t>(entry);
// Iterate every page this entry belongs to
do {
current_page_idx = entry_address / MemMap::GetPageSize() + page_off;
if (dirty_pages.find(current_page_idx) != dirty_pages.end()) {
// This entry is on a dirty page
return true;
}
page_off++;
} while ((current_page_idx * MemMap::GetPageSize()) < RoundUp(entry_address + size,
kObjectAlignment));
return false;
}
void AddImageDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
image_dirty_entries_.insert(entry);
}
void AddFalseDirtyEntry(T* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
false_dirty_entries_.push_back(entry);
false_dirty_entry_bytes_ += EntrySize(entry);
}
// The output stream to write to.
std::ostream& os_;
// The byte contents of the remote (image) process' image.
ArrayRef<uint8_t> remote_contents_;
// The byte contents of the zygote process' image.
ArrayRef<uint8_t> zygote_contents_;
const android::procinfo::MapInfo& boot_map_;
const ImageHeader& image_header_;
// Count of entries that are different.
size_t different_entries_;
// Local entries that are dirty (differ in at least one byte).
size_t dirty_entry_bytes_;
std::vector<T*> dirty_entries_;
// Local entries that are clean, but located on dirty pages.
size_t false_dirty_entry_bytes_;
std::vector<T*> false_dirty_entries_;
// Image dirty entries
// If zygote_pid_only_ == true, these are shared dirty entries in the zygote.
// If zygote_pid_only_ == false, these are private dirty entries in the application.
std::set<T*> image_dirty_entries_;
std::map<off_t /* field offset */, size_t /* count */> field_dirty_count_;
private:
DISALLOW_COPY_AND_ASSIGN(RegionCommon);
};
template <typename T>
class RegionSpecializedBase : public RegionCommon<T> {
};
// Calls VisitFunc for each non-null (reference)Object/ArtField pair.
// Doesn't work with ObjectArray instances, because array elements don't have ArtField.
class ReferenceFieldVisitor {
public:
using VisitFunc = std::function<void(mirror::Object&, ArtField&)>;
explicit ReferenceFieldVisitor(VisitFunc visit_func) : visit_func_(std::move(visit_func)) {}
void operator()(ObjPtr<mirror::Object> obj, MemberOffset offset, bool is_static) const
REQUIRES_SHARED(Locks::mutator_lock_) {
CHECK(!obj->IsObjectArray());
mirror::Object* field_obj = obj->GetFieldObject<mirror::Object>(offset);
// Skip fields that contain null.
if (field_obj == nullptr) {
return;
}
// Skip self references.
if (field_obj == obj.Ptr()) {
return;
}
ArtField* field = nullptr;
// Don't use Object::FindFieldByOffset, because it can't find instance fields in classes.
// field = obj->FindFieldByOffset(offset);
if (is_static) {
CHECK(obj->IsClass());
field = ArtField::FindStaticFieldWithOffset(obj->AsClass(), offset.Uint32Value());
} else {
field = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), offset.Uint32Value());
}
CHECK(field != nullptr);
visit_func_(*field_obj, *field);
}
void operator()([[maybe_unused]] ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> ref) const
REQUIRES_SHARED(Locks::mutator_lock_) {
operator()(ref, mirror::Reference::ReferentOffset(), /* is_static */ false);
}
[[noreturn]] void VisitRootIfNonNull(
[[maybe_unused]] mirror::CompressedReference<mirror::Object>* root) const
REQUIRES_SHARED(Locks::mutator_lock_) {
UNREACHABLE();
}
[[noreturn]] void VisitRoot([[maybe_unused]] mirror::CompressedReference<mirror::Object>* root)
const REQUIRES_SHARED(Locks::mutator_lock_) {
UNREACHABLE();
}
private:
VisitFunc visit_func_;
};
// Region analysis for mirror::Objects
class ImgObjectVisitor : public ObjectVisitor {
public:
using ComputeDirtyFunc = std::function<void(mirror::Object* object)>;
explicit ImgObjectVisitor(ComputeDirtyFunc dirty_func) : dirty_func_(std::move(dirty_func)) {}
~ImgObjectVisitor() override { }
void Visit(mirror::Object* object) override REQUIRES_SHARED(Locks::mutator_lock_) {
// Check that we are reading a real mirror::Object
CHECK(object->GetClass() != nullptr) << "Image object at address "
<< object
<< " has null class";
if (kUseBakerReadBarrier) {
object->AssertReadBarrierState();
}
dirty_func_(object);
}
private:
const ComputeDirtyFunc dirty_func_;
};
struct ParentInfo {
mirror::Object* parent = nullptr;
// Field name and type of the parent object in the format: <field_name>:<field_type_descriptor>
// Note: <field_name> can be an integer if parent is an Array object.
std::string path;
};
using ParentMap = std::unordered_map<mirror::Object*, ParentInfo>;
// Returns the "path" from root class to an object in the format:
// <class_descriptor>(.<field_name>:<field_type_descriptor>)*
std::string GetPathFromClass(mirror::Object* obj, const ParentMap& parent_map)
REQUIRES_SHARED(Locks::mutator_lock_) {
auto parent_info_it = parent_map.find(obj);
std::string path;
while (parent_info_it != parent_map.end() && parent_info_it->second.parent != nullptr) {
const ParentInfo& parent_info = parent_info_it->second;
path = ART_FORMAT(".{}{}", parent_info.path, path);
parent_info_it = parent_map.find(parent_info.parent);
}
if (parent_info_it == parent_map.end()) {
return "<no path from class>";
}
mirror::Object* class_obj = parent_info_it->first;
CHECK(class_obj->IsClass());
std::string temp;
path = class_obj->AsClass()->GetDescriptor(&temp) + path;
return path;
}
// Calculate a map of: object -> parent and parent field that refers to the object.
// Class objects are considered roots, they have entries in the parent_map, but their parent==null.
ParentMap CalculateParentMap(const std::vector<const ImageHeader*>& image_headers)
REQUIRES_SHARED(Locks::mutator_lock_) {
ParentMap parent_map;
std::vector<mirror::Object*> next;
// Collect all Class objects.
ImgObjectVisitor collect_classes_visitor(
[&](mirror::Object* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
if (entry->IsClass() && parent_map.count(entry) == 0) {
parent_map[entry] = ParentInfo{};
next.push_back(entry);
}
});
for (const ImageHeader* image_header : image_headers) {
uint8_t* image_begin = image_header->GetImageBegin();
PointerSize pointer_size = image_header->GetPointerSize();
image_header->VisitObjects(&collect_classes_visitor, image_begin, pointer_size);
}
auto process_object_fields = [&parent_map, &next](mirror::Object* parent_obj)
REQUIRES_SHARED(Locks::mutator_lock_) {
CHECK(!parent_obj->IsObjectArray());
ReferenceFieldVisitor::VisitFunc visit_func =
[&](mirror::Object& ref_obj, ArtField& ref_field) REQUIRES_SHARED(Locks::mutator_lock_) {
if (parent_map.count(&ref_obj) == 0) {
std::string path =
ART_FORMAT("{}:{}", ref_field.GetName(), ref_field.GetTypeDescriptor());
parent_map[&ref_obj] = ParentInfo{parent_obj, path};
next.push_back(&ref_obj);
}
};
ReferenceFieldVisitor visitor(visit_func);
parent_obj->VisitReferences</*kVisitNativeRoots=*/false, kVerifyNone, kWithoutReadBarrier>(
visitor, visitor);
};
auto process_array_elements = [&parent_map, &next](mirror::Object* parent_obj)
REQUIRES_SHARED(Locks::mutator_lock_) {
CHECK(parent_obj->IsObjectArray());
ObjPtr<mirror::ObjectArray<mirror::Object>> array = parent_obj->AsObjectArray<mirror::Object>();
const int32_t length = array->GetLength();
for (int32_t i = 0; i < length; ++i) {
ObjPtr<mirror::Object> elem = array->Get(i);
if (elem != nullptr && parent_map.count(elem.Ptr()) == 0) {
std::string temp;
std::string path = ART_FORMAT("{}:{}", i, elem->GetClass()->GetDescriptor(&temp));
parent_map[elem.Ptr()] = ParentInfo{parent_obj, path};
next.push_back(elem.Ptr());
}
}
};
// Use DFS to traverse all objects that are reachable from classes.
while (!next.empty()) {
mirror::Object* parent_obj = next.back();
next.pop_back();
// Array elements don't have ArtField, handle them separately.
if (parent_obj->IsObjectArray()) {
process_array_elements(parent_obj);
} else {
process_object_fields(parent_obj);
}
}
return parent_map;
}
// Count non-string objects that are not reachable from classes.
// Strings are skipped because they are considered clean in dex2oat and not used for dirty
// object layout optimization.
size_t CountUnreachableObjects(const std::unordered_map<mirror::Object*, ParentInfo>& parent_map,
const std::vector<const ImageHeader*>& image_headers)
REQUIRES_SHARED(Locks::mutator_lock_) {
size_t non_reachable = 0;
ImgObjectVisitor count_non_reachable_visitor(
[&](mirror::Object* entry) REQUIRES_SHARED(Locks::mutator_lock_) {
if (parent_map.count(entry) == 0 && !entry->IsString()) {
non_reachable += 1;
}
});
for (const ImageHeader* image_header : image_headers) {
uint8_t* image_begin = image_header->GetImageBegin();
PointerSize pointer_size = image_header->GetPointerSize();
image_header->VisitObjects(&count_non_reachable_visitor, image_begin, pointer_size);
}
return non_reachable;
}
template<>
class RegionSpecializedBase<mirror::Object> : public RegionCommon<mirror::Object> {
public:
RegionSpecializedBase(std::ostream* os,
ArrayRef<uint8_t> remote_contents,
ArrayRef<uint8_t> zygote_contents,
const android::procinfo::MapInfo& boot_map,
const ImageHeader& image_header,
const ParentMap& parent_map,
bool dump_dirty_objects)
: RegionCommon<mirror::Object>(os, remote_contents, zygote_contents, boot_map, image_header),
os_(*os),
dump_dirty_objects_(dump_dirty_objects),
parent_map_(parent_map) {}
// Define a common public type name for use by RegionData.
using VisitorClass = ImgObjectVisitor;
void VisitEntries(VisitorClass* visitor,
uint8_t* base,
PointerSize pointer_size)
REQUIRES_SHARED(Locks::mutator_lock_) {
image_header_.VisitObjects(visitor, base, pointer_size);
}
void VisitEntry(mirror::Object* entry)
REQUIRES_SHARED(Locks::mutator_lock_) {
// Unconditionally store the class descriptor in case we need it later
mirror::Class* klass = entry->GetClass();
class_data_[klass].descriptor = GetClassDescriptor(klass);
}
void AddCleanEntry(mirror::Object* entry)
REQUIRES_SHARED(Locks::mutator_lock_) {
class_data_[entry->GetClass()].AddCleanObject();
}
void AddFalseDirtyEntry(mirror::Object* entry)
REQUIRES_SHARED(Locks::mutator_lock_) {
RegionCommon<mirror::Object>::AddFalseDirtyEntry(entry);
class_data_[entry->GetClass()].AddFalseDirtyObject(entry);
}
void AddDirtyEntry(mirror::Object* entry, mirror::Object* entry_remote)
REQUIRES_SHARED(Locks::mutator_lock_) {
size_t entry_size = EntrySize(entry);
++different_entries_;
dirty_entry_bytes_ += entry_size;
// Log dirty count and objects for class objects only.
mirror::Class* klass = entry->GetClass();
if (klass->IsClassClass()) {
// Increment counts for the fields that are dirty
const uint8_t* current = reinterpret_cast<const uint8_t*>(entry);
const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(entry_remote);
for (size_t i = 0; i < entry_size; ++i) {
if (current[i] != current_remote[i]) {
field_dirty_count_[i]++;
}
}
dirty_entries_.push_back(entry);
}
class_data_[klass].AddDirtyObject(entry, entry_remote);
}
void DiffEntryContents(mirror::Object* entry,
uint8_t* remote_bytes,
const uint8_t* base_ptr,
bool log_dirty_objects) REQUIRES_SHARED(Locks::mutator_lock_) {
const char* tabs = " ";
// Attempt to find fields for all dirty bytes.
mirror::Class* klass = entry->GetClass();
std::string temp;
if (entry->IsClass()) {
os_ << tabs << "Class " << mirror::Class::PrettyClass(entry->AsClass()) << " " << entry
<< "\n";
} else {
os_ << tabs << "Instance of " << mirror::Class::PrettyClass(klass) << " " << entry << "\n";
}
std::string path_from_root = GetPathFromClass(entry, parent_map_);
os_ << "dirty_obj: " << path_from_root << "\n";
PrintEntryPages(reinterpret_cast<uintptr_t>(entry), EntrySize(entry), os_);
std::unordered_set<ArtField*> dirty_instance_fields;
std::unordered_set<ArtField*> dirty_static_fields;
// Examine the bytes comprising the Object, computing which fields are dirty
// and recording them for later display. If the Object is an array object,
// compute the dirty entries.
mirror::Object* remote_entry = reinterpret_cast<mirror::Object*>(remote_bytes);
for (size_t i = 0, count = entry->SizeOf(); i < count; ++i) {
if (base_ptr[i] != remote_bytes[i]) {
ArtField* field = ArtField::FindInstanceFieldWithOffset</*exact*/false>(klass, i);
if (field != nullptr) {
dirty_instance_fields.insert(field);
} else if (entry->IsClass()) {
field = ArtField::FindStaticFieldWithOffset</*exact*/false>(entry->AsClass(), i);
if (field != nullptr) {
dirty_static_fields.insert(field);
}
}
if (field == nullptr) {
if (klass->IsArrayClass()) {
ObjPtr<mirror::Class> component_type = klass->GetComponentType();
Primitive::Type primitive_type = component_type->GetPrimitiveType();
size_t component_size = Primitive::ComponentSize(primitive_type);
size_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
DCHECK_ALIGNED_PARAM(data_offset, component_size);
if (i >= data_offset) {
os_ << tabs << "Dirty array element " << (i - data_offset) / component_size << "\n";
// Skip the remaining bytes of this element to prevent spam.
DCHECK(IsPowerOfTwo(component_size));
i |= component_size - 1;
continue;
}
}
os_ << tabs << "No field for byte offset " << i << "\n";
}
}
}
// Dump different fields.
if (!dirty_instance_fields.empty()) {
os_ << tabs << "Dirty instance fields " << dirty_instance_fields.size() << "\n";
for (ArtField* field : dirty_instance_fields) {
os_ << tabs << ArtField::PrettyField(field)
<< " original=" << PrettyFieldValue(field, entry)
<< " remote=" << PrettyFieldValue(field, remote_entry) << "\n";
}
}
if (!dirty_static_fields.empty()) {
if (dump_dirty_objects_ && log_dirty_objects) {
dirty_objects_.insert(entry);
}
os_ << tabs << "Dirty static fields " << dirty_static_fields.size() << "\n";
for (ArtField* field : dirty_static_fields) {
os_ << tabs << ArtField::PrettyField(field)
<< " original=" << PrettyFieldValue(field, entry)
<< " remote=" << PrettyFieldValue(field, remote_entry) << "\n";
}
}
os_ << "\n";
}
void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) {
for (mirror::Object* obj : dirty_objects_) {
if (obj->IsClass()) {
std::string temp;
os_ << "Private dirty object: " << obj->AsClass()->GetDescriptor(&temp) << "\n";
}
}
}
void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
// vector of pairs (size_t count, Class*)
auto dirty_object_class_values =
SortByValueDesc<mirror::Class*, size_t, ClassData>(
class_data_,
[](const ClassData& d) { return d.dirty_object_count; });
os_ << "\n" << " Dirty object count by class:\n";
for (const auto& vk_pair : dirty_object_class_values) {
size_t dirty_object_count = vk_pair.first;
mirror::Class* klass = vk_pair.second;
ClassData& class_data = class_data_[klass];
size_t object_sizes = class_data.dirty_object_size_in_bytes;
float avg_dirty_bytes_per_class =
class_data.dirty_object_byte_count * 1.0f / object_sizes;
float avg_object_size = object_sizes * 1.0f / dirty_object_count;
const std::string& descriptor = class_data.descriptor;
os_ << " " << mirror::Class::PrettyClass(klass) << " ("
<< "objects: " << dirty_object_count << ", "
<< "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
<< "avg object size: " << avg_object_size << ", "
<< "class descriptor: '" << descriptor << "'"
<< ")\n";
if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
DumpSamplesAndOffsetCount();
os_ << " field contents:\n";
for (mirror::Object* object : class_data.dirty_objects) {
// remote class object
ObjPtr<mirror::Class> remote_klass =
ObjPtr<mirror::Class>::DownCast<mirror::Object>(object);
// local class object
ObjPtr<mirror::Class> local_klass =
RemoteContentsPointerToLocal(remote_klass,
RegionCommon<mirror::Object>::remote_contents_,
RegionCommon<mirror::Object>::image_header_);
os_ << " " << reinterpret_cast<const void*>(object) << " ";
os_ << " class_status (remote): " << remote_klass->GetStatus() << ", ";
os_ << " class_status (local): " << local_klass->GetStatus();
os_ << "\n";
}
}
}
}
void DumpFalseDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
// vector of pairs (size_t count, Class*)
auto false_dirty_object_class_values =
SortByValueDesc<mirror::Class*, size_t, ClassData>(
class_data_,
[](const ClassData& d) { return d.false_dirty_object_count; });
os_ << "\n" << " False-dirty object count by class:\n";
for (const auto& vk_pair : false_dirty_object_class_values) {
size_t object_count = vk_pair.first;
mirror::Class* klass = vk_pair.second;
ClassData& class_data = class_data_[klass];
size_t object_sizes = class_data.false_dirty_byte_count;
float avg_object_size = object_sizes * 1.0f / object_count;
const std::string& descriptor = class_data.descriptor;
os_ << " " << mirror::Class::PrettyClass(klass) << " ("
<< "objects: " << object_count << ", "
<< "avg object size: " << avg_object_size << ", "
<< "total bytes: " << object_sizes << ", "
<< "class descriptor: '" << descriptor << "'"
<< ")\n";
}
}
void DumpCleanEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
// vector of pairs (size_t count, Class*)
auto clean_object_class_values =
SortByValueDesc<mirror::Class*, size_t, ClassData>(
class_data_,
[](const ClassData& d) { return d.clean_object_count; });
os_ << "\n" << " Clean object count by class:\n";
for (const auto& vk_pair : clean_object_class_values) {
os_ << " " << mirror::Class::PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
}
}
private:
// Aggregate and detail class data from an image diff.
struct ClassData {
size_t dirty_object_count = 0;
// Track only the byte-per-byte dirtiness (in bytes)
size_t dirty_object_byte_count = 0;
// Track the object-by-object dirtiness (in bytes)
size_t dirty_object_size_in_bytes = 0;
size_t clean_object_count = 0;
std::string descriptor;
size_t false_dirty_byte_count = 0;
size_t false_dirty_object_count = 0;
std::vector<mirror::Object*> false_dirty_objects;
// Remote pointers to dirty objects
std::vector<mirror::Object*> dirty_objects;
void AddCleanObject() REQUIRES_SHARED(Locks::mutator_lock_) {
++clean_object_count;
}
void AddDirtyObject(mirror::Object* object, mirror::Object* object_remote)
REQUIRES_SHARED(Locks::mutator_lock_) {
++dirty_object_count;
dirty_object_byte_count += CountDirtyBytes(object, object_remote);
dirty_object_size_in_bytes += EntrySize(object);
dirty_objects.push_back(object_remote);
}
void AddFalseDirtyObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
++false_dirty_object_count;
false_dirty_objects.push_back(object);
false_dirty_byte_count += EntrySize(object);
}
private:
// Go byte-by-byte and figure out what exactly got dirtied
static size_t CountDirtyBytes(mirror::Object* object1, mirror::Object* object2)
REQUIRES_SHARED(Locks::mutator_lock_) {
const uint8_t* cur1 = reinterpret_cast<const uint8_t*>(object1);
const uint8_t* cur2 = reinterpret_cast<const uint8_t*>(object2);
size_t dirty_bytes = 0;
size_t object_size = EntrySize(object1);
for (size_t i = 0; i < object_size; ++i) {
if (cur1[i] != cur2[i]) {
dirty_bytes++;
}
}
return dirty_bytes;
}
};
std::ostream& os_;
bool dump_dirty_objects_;
std::unordered_set<mirror::Object*> dirty_objects_;
std::map<mirror::Class*, ClassData> class_data_;
const ParentMap& parent_map_;
DISALLOW_COPY_AND_ASSIGN(RegionSpecializedBase);
};
// Region analysis for ArtMethods.
class ImgArtMethodVisitor {
public:
using ComputeDirtyFunc = std::function<void(ArtMethod*)>;
explicit ImgArtMethodVisitor(ComputeDirtyFunc dirty_func) : dirty_func_(std::move(dirty_func)) {}
void operator()(ArtMethod& method) const { dirty_func_(&method); }
private:
const ComputeDirtyFunc dirty_func_;
};
// Struct and functor for computing offsets of members of ArtMethods.
// template <typename RegionType>
struct MemberInfo {
template <typename T>
void operator() (const ArtMethod* method, const T* member_address, const std::string& name) {
// Check that member_address is a pointer inside *method.
DCHECK(reinterpret_cast<uintptr_t>(method) <= reinterpret_cast<uintptr_t>(member_address));
DCHECK(reinterpret_cast<uintptr_t>(member_address) + sizeof(T) <=
reinterpret_cast<uintptr_t>(method) + sizeof(ArtMethod));
size_t offset =
reinterpret_cast<uintptr_t>(member_address) - reinterpret_cast<uintptr_t>(method);
offset_to_name_size_.insert({offset, NameAndSize(sizeof(T), name)});
}
struct NameAndSize {
size_t size_;
std::string name_;
NameAndSize(size_t size, const std::string& name) : size_(size), name_(name) { }
NameAndSize() : size_(0), name_("INVALID") { }
};
std::map<size_t, NameAndSize> offset_to_name_size_;
};
template<>
class RegionSpecializedBase<ArtMethod> : public RegionCommon<ArtMethod> {
public:
RegionSpecializedBase(std::ostream* os,
ArrayRef<uint8_t> remote_contents,
ArrayRef<uint8_t> zygote_contents,
const android::procinfo::MapInfo& boot_map,
const ImageHeader& image_header,
[[maybe_unused]] const ParentMap& parent_map,
[[maybe_unused]] bool dump_dirty_objects)
: RegionCommon<ArtMethod>(os, remote_contents, zygote_contents, boot_map, image_header),
os_(*os) {
// Prepare the table for offset to member lookups.
ArtMethod* art_method = reinterpret_cast<ArtMethod*>(&remote_contents[0]);
art_method->VisitMembers(member_info_);
// Prepare the table for address to symbolic entry point names.
BuildEntryPointNames();
class_linker_ = Runtime::Current()->GetClassLinker();
}
// Define a common public type name for use by RegionData.
using VisitorClass = ImgArtMethodVisitor;
void VisitEntries(VisitorClass* visitor,
uint8_t* base,
PointerSize pointer_size)
REQUIRES_SHARED(Locks::mutator_lock_) {
RegionCommon<ArtMethod>::image_header_.VisitPackedArtMethods(*visitor, base, pointer_size);
}
void VisitEntry([[maybe_unused]] ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {}
void AddCleanEntry([[maybe_unused]] ArtMethod* method) {}
void AddFalseDirtyEntry(ArtMethod* method)
REQUIRES_SHARED(Locks::mutator_lock_) {
RegionCommon<ArtMethod>::AddFalseDirtyEntry(method);
}
void AddDirtyEntry(ArtMethod* method, ArtMethod* method_remote)
REQUIRES_SHARED(Locks::mutator_lock_) {
size_t entry_size = EntrySize(method);
++different_entries_;
dirty_entry_bytes_ += entry_size;
// Increment counts for the fields that are dirty
const uint8_t* current = reinterpret_cast<const uint8_t*>(method);
const uint8_t* current_remote = reinterpret_cast<const uint8_t*>(method_remote);
// ArtMethods always log their dirty count and entries.
for (size_t i = 0; i < entry_size; ++i) {
if (current[i] != current_remote[i]) {
field_dirty_count_[i]++;
}
}
dirty_entries_.push_back(method);
}
void DiffEntryContents(ArtMethod* method,
uint8_t* remote_bytes,
const uint8_t* base_ptr,
[[maybe_unused]] bool log_dirty_objects)
REQUIRES_SHARED(Locks::mutator_lock_) {
const char* tabs = " ";
os_ << tabs << "ArtMethod " << ArtMethod::PrettyMethod(method) << "\n";
PrintEntryPages(reinterpret_cast<uintptr_t>(method), EntrySize(method), os_);
std::unordered_set<size_t> dirty_members;
// Examine the members comprising the ArtMethod, computing which members are dirty.
for (const std::pair<const size_t,
MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) {
const size_t offset = p.first;
if (memcmp(base_ptr + offset, remote_bytes + offset, p.second.size_) != 0) {
dirty_members.insert(p.first);
}
}
// Dump different fields.
if (!dirty_members.empty()) {
os_ << tabs << "Dirty members " << dirty_members.size() << "\n";
for (size_t offset : dirty_members) {
const MemberInfo::NameAndSize& member_info = member_info_.offset_to_name_size_[offset];
os_ << tabs << member_info.name_
<< " original=" << StringFromBytes(base_ptr + offset, member_info.size_)
<< " remote=" << StringFromBytes(remote_bytes + offset, member_info.size_)
<< "\n";
}
}
os_ << "\n";
}
void DumpDirtyObjects() REQUIRES_SHARED(Locks::mutator_lock_) {
}
void DumpDirtyEntries() REQUIRES_SHARED(Locks::mutator_lock_) {
DumpSamplesAndOffsetCount();
os_ << " offset to field map:\n";
for (const std::pair<const size_t,
MemberInfo::NameAndSize>& p : member_info_.offset_to_name_size_) {
const size_t offset = p.first;
const size_t size = p.second.size_;
os_ << StringPrintf(" %zu-%zu: ", offset, offset + size - 1)
<< p.second.name_
<< std::endl;
}
os_ << " field contents:\n";
for (ArtMethod* method : dirty_entries_) {
// remote method
auto art_method = reinterpret_cast<ArtMethod*>(method);
// remote class
ObjPtr<mirror::Class> remote_declaring_class =
FixUpRemotePointer(art_method->GetDeclaringClass(),
RegionCommon<ArtMethod>::remote_contents_,
RegionCommon<ArtMethod>::boot_map_);
// local class
ObjPtr<mirror::Class> declaring_class =
RemoteContentsPointerToLocal(remote_declaring_class,
RegionCommon<ArtMethod>::remote_contents_,
RegionCommon<ArtMethod>::image_header_);
DumpOneArtMethod(art_method, declaring_class, remote_declaring_class);
}
}