1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_RUNTIME_TESTS_BITMAP_TEST_BASE_H_
17 #define PANDA_RUNTIME_TESTS_BITMAP_TEST_BASE_H_
18
19 #include <cstdlib>
20 #include <memory>
21 #include <vector>
22
23 #include "runtime/mem/gc/bitmap.h"
24
25 namespace panda::mem {
26
27 class BitmapTest : public testing::Test {
28 public:
29 static constexpr object_pointer_type HEAP_STARTING_ADDRESS = static_cast<object_pointer_type>(0x10000000);
30 };
31
32 using BitmapWordType = panda::mem::Bitmap::BitmapWordType;
33
34 class BitmapVerify {
35 public:
36 using BitmapType = MemBitmap<DEFAULT_ALIGNMENT_IN_BYTES>;
BitmapVerify(BitmapType * bitmap,void * begin,void * end)37 BitmapVerify(BitmapType *bitmap, void *begin, void *end) : bitmap_(bitmap), begin_(begin), end_(end) {}
38
operator()39 void operator()(void *obj)
40 {
41 EXPECT_TRUE(obj >= begin_);
42 EXPECT_TRUE(obj <= end_);
43 EXPECT_EQ(bitmap_->Test(obj), ((BitmapType::ToPointerType(obj) & ADDRESS_MASK_TO_SET) != 0));
44 }
45
46 BitmapType *const bitmap_;
47 void *begin_;
48 void *end_;
49 static constexpr BitmapWordType ADDRESS_MASK_TO_SET = 0xF;
50 };
51
52 template <size_t kAlignment, typename TestFn>
RunTest(TestFn && fn)53 static void RunTest(TestFn &&fn)
54 {
55 auto heap_begin = BitmapTest::HEAP_STARTING_ADDRESS;
56 const size_t heap_capacity = 16_MB;
57
58 auto fn_rounddown = [](size_t val, size_t alignment) -> size_t {
59 size_t mask = ~((static_cast<size_t>(1) * alignment) - 1);
60 return val & mask;
61 };
62
63 #ifdef PANDA_NIGHTLY_TEST_ON
64 std::srand(time(nullptr));
65 #else
66 std::srand(0x1234);
67 #endif
68
69 constexpr int TEST_REPEAT = 1;
70 for (int i = 0; i < TEST_REPEAT; ++i) {
71 auto bm_ptr = std::make_unique<BitmapWordType[]>((heap_capacity >> Bitmap::LOG_BITSPERWORD) / kAlignment);
72 MemBitmap<kAlignment> bm(ToVoidPtr(heap_begin), heap_capacity, bm_ptr.get());
73
74 constexpr int NUM_BITS_TO_MODIFY = 1000;
75 for (int j = 0; j < NUM_BITS_TO_MODIFY; ++j) {
76 size_t offset = fn_rounddown(std::rand() % heap_capacity, kAlignment);
77 bool set = std::rand() % 2U == 1;
78
79 if (set) {
80 bm.Set(ToVoidPtr(heap_begin + offset));
81 } else {
82 bm.Clear(ToVoidPtr(heap_begin + offset));
83 }
84 }
85
86 constexpr int NUM_TEST_RANGES = 50;
87 for (int j = 0; j < NUM_TEST_RANGES; ++j) {
88 const size_t offset = fn_rounddown(std::rand() % heap_capacity, kAlignment);
89 const size_t remain = heap_capacity - offset;
90 const size_t end = offset + fn_rounddown(std::rand() % (remain + 1), kAlignment);
91
92 size_t manual = 0;
93 for (object_pointer_type k = offset; k < end; k += kAlignment) {
94 if (bm.Test(ToVoidPtr(heap_begin + k))) {
95 manual++;
96 }
97 }
98
99 fn(&bm, heap_begin + offset, heap_begin + end, manual);
100 }
101 }
102 }
103
104 template <size_t kAlignment>
RunTestCount()105 static void RunTestCount()
106 {
107 auto count_test_fn = [](MemBitmap<kAlignment> *bitmap, object_pointer_type begin, object_pointer_type end,
108 size_t manual_count) {
109 size_t count = 0;
110 auto count_fn = [&count]([[maybe_unused]] void *obj) { count++; };
111 bitmap->IterateOverMarkedChunkInRange(ToVoidPtr(begin), ToVoidPtr(end), count_fn);
112 EXPECT_EQ(count, manual_count);
113 };
114 RunTest<kAlignment>(count_test_fn);
115 }
116
117 template <size_t kAlignment>
RunTestOrder()118 void RunTestOrder()
119 {
120 auto order_test_fn = [](MemBitmap<kAlignment> *bitmap, object_pointer_type begin, object_pointer_type end,
121 size_t manual_count) {
122 void *last_ptr = nullptr;
123 auto order_check = [&last_ptr](void *obj) {
124 EXPECT_LT(last_ptr, obj);
125 last_ptr = obj;
126 };
127
128 // Test complete walk.
129 bitmap->IterateOverChunks(order_check);
130 if (manual_count > 0) {
131 EXPECT_NE(nullptr, last_ptr);
132 }
133
134 // Test range.
135 last_ptr = nullptr;
136 bitmap->IterateOverMarkedChunkInRange(ToVoidPtr(begin), ToVoidPtr(end), order_check);
137 if (manual_count > 0) {
138 EXPECT_NE(nullptr, last_ptr);
139 }
140 };
141 RunTest<kAlignment>(order_test_fn);
142 }
143
TEST_F(BitmapTest,AtomicClearSetTest)144 TEST_F(BitmapTest, AtomicClearSetTest)
145 {
146 void *object = ToVoidPtr(HEAP_STARTING_ADDRESS);
147 const size_t sz = 1_MB;
148 auto bm_ptr = std::make_unique<BitmapWordType[]>(sz >> MemBitmap<>::LOG_BITSPERWORD);
149 MemBitmap<> bm(ToVoidPtr(HEAP_STARTING_ADDRESS), sz, bm_ptr.get());
150
151 // Set bit
152 ASSERT_TRUE(bm.Test(object) == bm.AtomicTestAndSet(object));
153 ASSERT_TRUE(bm.Test(object));
154 ASSERT_TRUE(bm.AtomicTest(object));
155
156 // Clear bit
157 ASSERT_TRUE(bm.Test(object) == bm.AtomicTestAndClear(object));
158 ASSERT_TRUE(!bm.Test(object));
159 ASSERT_TRUE(!bm.AtomicTest(object));
160 }
161
162 } // namespace panda::mem
163
164 #endif // PANDA_RUNTIME_TESTS_BITMAP_TEST_BASE_H_
165