mod_union_table_test.cc revision 90443477f9a0061581c420775ce3b7eeae7468bc
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "mod_union_table-inl.h"
18
19#include "class_linker-inl.h"
20#include "common_runtime_test.h"
21#include "gc/space/space-inl.h"
22#include "mirror/array-inl.h"
23#include "space_bitmap-inl.h"
24#include "thread-inl.h"
25
26namespace art {
27namespace gc {
28namespace accounting {
29
30class ModUnionTableFactory {
31 public:
32  enum TableType {
33    kTableTypeCardCache,
34    kTableTypeReferenceCache,
35    kTableTypeCount,  // Number of values in the enum.
36  };
37
38  // Target space is ignored for the card cache implementation.
39  static ModUnionTable* Create(
40      TableType type, space::ContinuousSpace* space, space::ContinuousSpace* target_space);
41};
42
43class ModUnionTableTest : public CommonRuntimeTest {
44 public:
45  ModUnionTableTest() : java_lang_object_array_(nullptr) {
46  }
47  mirror::ObjectArray<mirror::Object>* AllocObjectArray(
48      Thread* self, space::ContinuousMemMapAllocSpace* space, size_t component_count)
49      SHARED_REQUIRES(Locks::mutator_lock_) {
50    auto* klass = GetObjectArrayClass(self, space);
51    const size_t size = mirror::ComputeArraySize(component_count, 2);
52    size_t bytes_allocated = 0, bytes_tl_bulk_allocated;
53    auto* obj = down_cast<mirror::ObjectArray<mirror::Object>*>(
54        space->Alloc(self, size, &bytes_allocated, nullptr, &bytes_tl_bulk_allocated));
55    if (obj != nullptr) {
56      obj->SetClass(klass);
57      obj->SetLength(static_cast<int32_t>(component_count));
58      space->GetLiveBitmap()->Set(obj);
59      EXPECT_GE(bytes_allocated, size);
60    }
61    return obj;
62  }
63  void ResetClass() {
64    java_lang_object_array_ = nullptr;
65  }
66  void RunTest(ModUnionTableFactory::TableType type);
67
68 private:
69  mirror::Class* GetObjectArrayClass(Thread* self, space::ContinuousMemMapAllocSpace* space)
70      SHARED_REQUIRES(Locks::mutator_lock_) {
71    if (java_lang_object_array_ == nullptr) {
72      java_lang_object_array_ =
73          Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kObjectArrayClass);
74      // Since the test doesn't have an image, the class of the object array keeps cards live
75      // inside the card cache mod-union table and causes the check
76      // ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj3)));
77      // to fail since the class ends up keeping the card dirty. To get around this, we make a fake
78      // copy of the class in the same space that we are allocating in.
79      DCHECK(java_lang_object_array_ != nullptr);
80      const size_t class_size = java_lang_object_array_->GetClassSize();
81      size_t bytes_allocated = 0, bytes_tl_bulk_allocated;
82      auto* klass = down_cast<mirror::Class*>(space->Alloc(self, class_size, &bytes_allocated,
83                                                           nullptr,
84                                                           &bytes_tl_bulk_allocated));
85      DCHECK(klass != nullptr);
86      memcpy(klass, java_lang_object_array_, class_size);
87      Runtime::Current()->GetHeap()->GetCardTable()->MarkCard(klass);
88      java_lang_object_array_ = klass;
89    }
90    return java_lang_object_array_;
91  }
92  mirror::Class* java_lang_object_array_;
93};
94
95// Collect visited objects into container.
96class CollectVisitedVisitor : public MarkObjectVisitor {
97 public:
98  explicit CollectVisitedVisitor(std::set<mirror::Object*>* out) : out_(out) {}
99  virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>* ref) OVERRIDE
100      SHARED_REQUIRES(Locks::mutator_lock_) {
101    DCHECK(ref != nullptr);
102    MarkObject(ref->AsMirrorPtr());
103  }
104  virtual mirror::Object* MarkObject(mirror::Object* obj) OVERRIDE
105      SHARED_REQUIRES(Locks::mutator_lock_) {
106    DCHECK(obj != nullptr);
107    out_->insert(obj);
108    return obj;
109  }
110
111 private:
112  std::set<mirror::Object*>* const out_;
113};
114
115// A mod union table that only holds references to a specified target space.
116class ModUnionTableRefCacheToSpace : public ModUnionTableReferenceCache {
117 public:
118  explicit ModUnionTableRefCacheToSpace(
119      const std::string& name, Heap* heap, space::ContinuousSpace* space,
120      space::ContinuousSpace* target_space)
121      : ModUnionTableReferenceCache(name, heap, space), target_space_(target_space) {}
122
123  bool ShouldAddReference(const mirror::Object* ref) const OVERRIDE {
124    return target_space_->HasAddress(ref);
125  }
126
127 private:
128  space::ContinuousSpace* const target_space_;
129};
130
131std::ostream& operator<<(std::ostream& oss, ModUnionTableFactory::TableType type) {
132  switch (type) {
133    case ModUnionTableFactory::kTableTypeCardCache: {
134      oss << "CardCache";
135      break;
136    }
137    case ModUnionTableFactory::kTableTypeReferenceCache: {
138      oss << "ReferenceCache";
139      break;
140    }
141    default: {
142      UNIMPLEMENTED(FATAL) << static_cast<size_t>(type);
143    }
144  }
145  return oss;
146}
147
148ModUnionTable* ModUnionTableFactory::Create(
149    TableType type, space::ContinuousSpace* space, space::ContinuousSpace* target_space) {
150  std::ostringstream name;
151  name << "Mod union table: " << type;
152  switch (type) {
153    case kTableTypeCardCache: {
154      return new ModUnionTableCardCache(name.str(), Runtime::Current()->GetHeap(), space);
155    }
156    case kTableTypeReferenceCache: {
157      return new ModUnionTableRefCacheToSpace(name.str(), Runtime::Current()->GetHeap(), space,
158                                              target_space);
159    }
160    default: {
161      UNIMPLEMENTED(FATAL) << "Invalid type " << type;
162    }
163  }
164  return nullptr;
165}
166
167TEST_F(ModUnionTableTest, TestCardCache) {
168  RunTest(ModUnionTableFactory::kTableTypeCardCache);
169}
170
171TEST_F(ModUnionTableTest, TestReferenceCache) {
172  RunTest(ModUnionTableFactory::kTableTypeReferenceCache);
173}
174
175void ModUnionTableTest::RunTest(ModUnionTableFactory::TableType type) {
176  Thread* const self = Thread::Current();
177  ScopedObjectAccess soa(self);
178  Runtime* const runtime = Runtime::Current();
179  gc::Heap* const heap = runtime->GetHeap();
180  // Use non moving space since moving GC don't necessarily have a primary free list space.
181  auto* space = heap->GetNonMovingSpace();
182  ResetClass();
183  // Create another space that we can put references in.
184  std::unique_ptr<space::DlMallocSpace> other_space(space::DlMallocSpace::Create(
185      "other space", 128 * KB, 4 * MB, 4 * MB, nullptr, false));
186  ASSERT_TRUE(other_space.get() != nullptr);
187  heap->AddSpace(other_space.get());
188  std::unique_ptr<ModUnionTable> table(ModUnionTableFactory::Create(
189      type, space, other_space.get()));
190  ASSERT_TRUE(table.get() != nullptr);
191  // Create some fake objects and put the main space and dirty cards in the non moving space.
192  auto* obj1 = AllocObjectArray(self, space, CardTable::kCardSize);
193  ASSERT_TRUE(obj1 != nullptr);
194  auto* obj2 = AllocObjectArray(self, space, CardTable::kCardSize);
195  ASSERT_TRUE(obj2 != nullptr);
196  auto* obj3 = AllocObjectArray(self, space, CardTable::kCardSize);
197  ASSERT_TRUE(obj3 != nullptr);
198  auto* obj4 = AllocObjectArray(self, space, CardTable::kCardSize);
199  ASSERT_TRUE(obj4 != nullptr);
200  // Dirty some cards.
201  obj1->Set(0, obj2);
202  obj2->Set(0, obj3);
203  obj3->Set(0, obj4);
204  obj4->Set(0, obj1);
205  // Dirty some more cards to objects in another space.
206  auto* other_space_ref1 = AllocObjectArray(self, other_space.get(), CardTable::kCardSize);
207  ASSERT_TRUE(other_space_ref1 != nullptr);
208  auto* other_space_ref2 = AllocObjectArray(self, other_space.get(), CardTable::kCardSize);
209  ASSERT_TRUE(other_space_ref2 != nullptr);
210  obj1->Set(1, other_space_ref1);
211  obj2->Set(3, other_space_ref2);
212  table->ClearCards();
213  std::set<mirror::Object*> visited_before;
214  CollectVisitedVisitor collector_before(&visited_before);
215  table->UpdateAndMarkReferences(&collector_before);
216  // Check that we visited all the references in other spaces only.
217  ASSERT_GE(visited_before.size(), 2u);
218  ASSERT_TRUE(visited_before.find(other_space_ref1) != visited_before.end());
219  ASSERT_TRUE(visited_before.find(other_space_ref2) != visited_before.end());
220  // Verify that all the other references were visited.
221  // obj1, obj2 cards should still be in mod union table since they have references to other
222  // spaces.
223  ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj1)));
224  ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj2)));
225  // obj3, obj4 don't have a reference to any object in the other space, their cards should have
226  // been removed from the mod union table during UpdateAndMarkReferences.
227  ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj3)));
228  ASSERT_FALSE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(obj4)));
229  {
230    // Currently no-op, make sure it still works however.
231    ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
232    table->Verify();
233  }
234  // Verify that dump doesn't crash.
235  std::ostringstream oss;
236  table->Dump(oss);
237  // Set all the cards, then verify.
238  table->SetCards();
239  // TODO: Check that the cards are actually set.
240  for (auto* ptr = space->Begin(); ptr < AlignUp(space->End(), CardTable::kCardSize);
241      ptr += CardTable::kCardSize) {
242    ASSERT_TRUE(table->ContainsCardFor(reinterpret_cast<uintptr_t>(ptr)));
243  }
244  // Visit again and make sure the cards got cleared back to their sane state.
245  std::set<mirror::Object*> visited_after;
246  CollectVisitedVisitor collector_after(&visited_after);
247  table->UpdateAndMarkReferences(&collector_after);
248  // Check that we visited a superset after.
249  for (auto* obj : visited_before) {
250    ASSERT_TRUE(visited_after.find(obj) != visited_after.end()) << obj;
251  }
252  // Verify that the dump still works.
253  std::ostringstream oss2;
254  table->Dump(oss2);
255  // Remove the space we added so it doesn't persist to the next test.
256  heap->RemoveSpace(other_space.get());
257}
258
259}  // namespace accounting
260}  // namespace gc
261}  // namespace art
262