1 /*
2 * Copyright (C) 2012 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 "space_bitmap.h"
18
19 #include <stdint.h>
20 #include <memory>
21
22 #include "base/mutex.h"
23 #include "common_runtime_test.h"
24 #include "gc/space/large_object_space.h"
25 #include "runtime_globals.h"
26 #include "space_bitmap-inl.h"
27
28 namespace art HIDDEN {
29 namespace gc {
30 namespace accounting {
31
32 // SpaceBitmapTest is a CommonRuntimeTest as the test requires runtime to be initialized to enable
33 // access to space::LargeObjectSpace::ObjectAlignment().
34 template <typename T>
35 class SpaceBitmapTest : public CommonRuntimeTest {};
36
37 // Main test parameters. For each test case, we pair together a SpaceBitmap
38 // implementation with an object alignment. The object alignment may be larger
39 // than the underlying SpaceBitmap alignment.
40 template <typename T, size_t kAlignment>
41 struct SpaceBitmapTestType {
42 using SpaceBitmap = T;
GetObjectAlignmentart::gc::accounting::SpaceBitmapTestType43 static constexpr size_t GetObjectAlignment() {
44 return kAlignment;
45 }
46 };
47
48 // This is a special case where object alignment is chosen to be the large-object
49 // alignment determined at runtime.
50 template <typename T>
51 struct SpaceBitmapTestPageSizeType {
52 using SpaceBitmap = T;
GetObjectAlignmentart::gc::accounting::SpaceBitmapTestPageSizeType53 static size_t GetObjectAlignment() {
54 return space::LargeObjectSpace::ObjectAlignment();
55 }
56 };
57
58 using SpaceBitmapTestTypes =
59 ::testing::Types<SpaceBitmapTestType<ContinuousSpaceBitmap, kObjectAlignment>,
60 // Large objects are aligned to the OS page size, try
61 // different supported values, including the current
62 // runtime page size.
63 SpaceBitmapTestType<LargeObjectBitmap, kMinPageSize>,
64 SpaceBitmapTestPageSizeType<LargeObjectBitmap>,
65 SpaceBitmapTestType<LargeObjectBitmap, kMaxPageSize>>;
66
67 TYPED_TEST_CASE(SpaceBitmapTest, SpaceBitmapTestTypes);
68
TYPED_TEST(SpaceBitmapTest,Init)69 TYPED_TEST(SpaceBitmapTest, Init) {
70 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
71 size_t heap_capacity = 16 * MB;
72 auto space_bitmap(TypeParam::SpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
73 EXPECT_TRUE(space_bitmap.IsValid());
74 }
75
76 template <typename SpaceBitmap>
77 class BitmapVerify {
78 public:
BitmapVerify(SpaceBitmap * bitmap,const mirror::Object * begin,const mirror::Object * end)79 BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin,
80 const mirror::Object* end)
81 : bitmap_(bitmap),
82 begin_(begin),
83 end_(end) {}
84
operator ()(const mirror::Object * obj)85 void operator()(const mirror::Object* obj) {
86 EXPECT_TRUE(obj >= begin_);
87 EXPECT_TRUE(obj <= end_);
88 EXPECT_EQ(bitmap_->Test(obj), ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0));
89 }
90
91 SpaceBitmap* const bitmap_;
92 const mirror::Object* begin_;
93 const mirror::Object* end_;
94 };
95
TYPED_TEST(SpaceBitmapTest,ScanRange)96 TYPED_TEST(SpaceBitmapTest, ScanRange) {
97 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
98 size_t heap_capacity = 16 * MB;
99 const size_t gObjectAlignment = TypeParam::GetObjectAlignment();
100
101 auto space_bitmap(TypeParam::SpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
102 EXPECT_TRUE(space_bitmap.IsValid());
103
104 // Set all the odd bits in the first BitsPerIntPtrT * 3 to one.
105 for (size_t j = 0; j < kBitsPerIntPtrT * 3; ++j) {
106 const mirror::Object* obj =
107 reinterpret_cast<mirror::Object*>(heap_begin + j * gObjectAlignment);
108 if (reinterpret_cast<uintptr_t>(obj) & 0xF) {
109 space_bitmap.Set(obj);
110 }
111 }
112 // Try every possible starting bit in the first word. Then for each starting bit, try each
113 // possible length up to a maximum of `kBitsPerIntPtrT * 2 - 1` bits.
114 // This handles all the cases, having runs which start and end on the same word, and different
115 // words.
116 for (size_t i = 0; i < static_cast<size_t>(kBitsPerIntPtrT); ++i) {
117 mirror::Object* start =
118 reinterpret_cast<mirror::Object*>(heap_begin + i * gObjectAlignment);
119 for (size_t j = 0; j < static_cast<size_t>(kBitsPerIntPtrT * 2); ++j) {
120 mirror::Object* end =
121 reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * gObjectAlignment);
122 BitmapVerify(&space_bitmap, start, end);
123 }
124 }
125 }
126
TYPED_TEST(SpaceBitmapTest,ClearRange)127 TYPED_TEST(SpaceBitmapTest, ClearRange) {
128 const size_t page_size = MemMap::GetPageSize();
129 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
130 size_t heap_capacity = 16 * MB;
131 const size_t gObjectAlignment = TypeParam::GetObjectAlignment();
132
133 auto bitmap(TypeParam::SpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
134 EXPECT_TRUE(bitmap.IsValid());
135
136 // Set all of the bits in the bitmap.
137 for (size_t j = 0; j < heap_capacity; j += gObjectAlignment) {
138 const mirror::Object* obj = reinterpret_cast<mirror::Object*>(heap_begin + j);
139 bitmap.Set(obj);
140 }
141
142 std::vector<std::pair<uintptr_t, uintptr_t>> ranges = {
143 {0, RoundUp(10 * KB, gObjectAlignment) + gObjectAlignment},
144 {gObjectAlignment, gObjectAlignment},
145 {gObjectAlignment, 2 * gObjectAlignment},
146 {gObjectAlignment, 5 * gObjectAlignment},
147 {RoundUp(1 * KB, gObjectAlignment) + gObjectAlignment,
148 RoundUp(2 * KB, gObjectAlignment) + 5 * gObjectAlignment},
149 };
150 // Try clearing a few ranges.
151 for (const std::pair<uintptr_t, uintptr_t>& range : ranges) {
152 const mirror::Object* obj_begin = reinterpret_cast<mirror::Object*>(heap_begin + range.first);
153 const mirror::Object* obj_end = reinterpret_cast<mirror::Object*>(heap_begin + range.second);
154 bitmap.ClearRange(obj_begin, obj_end);
155 // Boundaries should still be marked.
156 for (uintptr_t i = 0; i < range.first; i += gObjectAlignment) {
157 EXPECT_TRUE(bitmap.Test(reinterpret_cast<mirror::Object*>(heap_begin + i)));
158 }
159 for (uintptr_t i = range.second; i < range.second + page_size; i += gObjectAlignment) {
160 EXPECT_TRUE(bitmap.Test(reinterpret_cast<mirror::Object*>(heap_begin + i)));
161 }
162 // Everything inside should be cleared.
163 for (uintptr_t i = range.first; i < range.second; i += gObjectAlignment) {
164 EXPECT_FALSE(bitmap.Test(reinterpret_cast<mirror::Object*>(heap_begin + i)));
165 bitmap.Set(reinterpret_cast<mirror::Object*>(heap_begin + i));
166 }
167 }
168 }
169
170
171 class SimpleCounter {
172 public:
SimpleCounter(size_t * counter)173 explicit SimpleCounter(size_t* counter) : count_(counter) {}
174
operator ()(mirror::Object * obj) const175 void operator()([[maybe_unused]] mirror::Object* obj) const { (*count_)++; }
176
177 size_t* const count_;
178 };
179
180 class RandGen {
181 public:
RandGen(uint32_t seed)182 explicit RandGen(uint32_t seed) : val_(seed) {}
183
next()184 uint32_t next() {
185 val_ = val_ * 48271 % 2147483647 + 13;
186 return val_;
187 }
188
189 uint32_t val_;
190 };
191
192 template <typename SpaceBitmap, typename TestFn>
RunTest(size_t alignment,TestFn && fn)193 static void RunTest(size_t alignment, TestFn&& fn) NO_THREAD_SAFETY_ANALYSIS {
194 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
195 size_t heap_capacity = 16 * MB;
196
197 // Seed with 0x1234 for reproducability.
198 RandGen r(0x1234);
199
200 for (int i = 0; i < 5 ; ++i) {
201 SpaceBitmap space_bitmap(SpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
202
203 for (int j = 0; j < 10000; ++j) {
204 size_t offset = RoundDown(r.next() % heap_capacity, alignment);
205 bool set = r.next() % 2 == 1;
206
207 if (set) {
208 space_bitmap.Set(reinterpret_cast<mirror::Object*>(heap_begin + offset));
209 } else {
210 space_bitmap.Clear(reinterpret_cast<mirror::Object*>(heap_begin + offset));
211 }
212 }
213
214 for (int j = 0; j < 50; ++j) {
215 const size_t offset = RoundDown(r.next() % heap_capacity, alignment);
216 const size_t remain = heap_capacity - offset;
217 const size_t end = offset + RoundDown(r.next() % (remain + 1), alignment);
218
219 size_t manual = 0;
220 for (uintptr_t k = offset; k < end; k += alignment) {
221 if (space_bitmap.Test(reinterpret_cast<mirror::Object*>(heap_begin + k))) {
222 manual++;
223 }
224 }
225
226 uintptr_t range_begin = reinterpret_cast<uintptr_t>(heap_begin) + offset;
227 uintptr_t range_end = reinterpret_cast<uintptr_t>(heap_begin) + end;
228
229 fn(&space_bitmap, range_begin, range_end, manual);
230 }
231 }
232 }
233
TYPED_TEST(SpaceBitmapTest,VisitorAlignment)234 TYPED_TEST(SpaceBitmapTest, VisitorAlignment) {
235 using SpaceBitmap = typename TypeParam::SpaceBitmap;
236 auto count_test_fn = [](SpaceBitmap* space_bitmap,
237 uintptr_t range_begin,
238 uintptr_t range_end,
239 size_t manual_count) {
240 size_t count = 0;
241 auto count_fn = [&count]([[maybe_unused]] mirror::Object* obj) { count++; };
242 space_bitmap->VisitMarkedRange(range_begin, range_end, count_fn);
243 EXPECT_EQ(count, manual_count);
244 };
245 RunTest<SpaceBitmap>(TypeParam::GetObjectAlignment(), count_test_fn);
246 }
247
TYPED_TEST(SpaceBitmapTest,OrderAlignment)248 TYPED_TEST(SpaceBitmapTest, OrderAlignment) {
249 using SpaceBitmap = typename TypeParam::SpaceBitmap;
250 auto order_test_fn = [](SpaceBitmap* space_bitmap,
251 uintptr_t range_begin,
252 uintptr_t range_end,
253 size_t manual_count)
254 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
255 mirror::Object* last_ptr = nullptr;
256 auto order_check = [&last_ptr](mirror::Object* obj) {
257 EXPECT_LT(last_ptr, obj);
258 last_ptr = obj;
259 };
260
261 // Test complete walk.
262 space_bitmap->Walk(order_check);
263 if (manual_count > 0) {
264 EXPECT_NE(nullptr, last_ptr);
265 }
266
267 // Test range.
268 last_ptr = nullptr;
269 space_bitmap->VisitMarkedRange(range_begin, range_end, order_check);
270 if (manual_count > 0) {
271 EXPECT_NE(nullptr, last_ptr);
272 }
273 };
274 RunTest<SpaceBitmap>(TypeParam::GetObjectAlignment(), order_test_fn);
275 }
276
277 } // namespace accounting
278 } // namespace gc
279 } // namespace art
280