1 /**
2 * Copyright (c) 2021-2022 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 RUNTIME_TESTS_BITMAP_TEST_BASE_H
17 #define 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
fn_rounddown(size_t val,size_t alignment)52 size_t fn_rounddown(size_t val, size_t alignment)
53 {
54 size_t mask = ~((static_cast<size_t>(1) * alignment) - 1);
55 return val & mask;
56 }
57
58 template <size_t kAlignment, typename TestFn>
RunTest(TestFn && fn)59 static void RunTest(TestFn &&fn)
60 {
61 auto heap_begin = BitmapTest::HEAP_STARTING_ADDRESS;
62 const size_t heap_capacity = 16_MB;
63
64 #ifdef PANDA_NIGHTLY_TEST_ON
65 std::srand(time(nullptr));
66 #else
67 std::srand(0x1234);
68 #endif
69
70 constexpr int TEST_REPEAT = 1;
71 for (int i = 0; i < TEST_REPEAT; ++i) {
72 auto bm_ptr = std::make_unique<BitmapWordType[]>((heap_capacity >> Bitmap::LOG_BITSPERWORD) / kAlignment);
73 MemBitmap<kAlignment> bm(ToVoidPtr(heap_begin), heap_capacity, bm_ptr.get());
74
75 constexpr int NUM_BITS_TO_MODIFY = 1000;
76 for (int j = 0; j < NUM_BITS_TO_MODIFY; ++j) {
77 size_t offset = fn_rounddown(std::rand() % heap_capacity, kAlignment);
78 bool set = std::rand() % 2 == 1;
79
80 if (set) {
81 bm.Set(ToVoidPtr(heap_begin + offset));
82 } else {
83 bm.Clear(ToVoidPtr(heap_begin + offset));
84 }
85 }
86
87 constexpr int NUM_TEST_RANGES = 50;
88 for (int j = 0; j < NUM_TEST_RANGES; ++j) {
89 const size_t offset = fn_rounddown(std::rand() % heap_capacity, kAlignment);
90 const size_t remain = heap_capacity - offset;
91 const size_t end = offset + fn_rounddown(std::rand() % (remain + 1), kAlignment);
92
93 size_t manual = 0;
94 for (object_pointer_type k = offset; k < end; k += kAlignment) {
95 if (bm.Test(ToVoidPtr(heap_begin + k))) {
96 manual++;
97 }
98 }
99
100 fn(&bm, heap_begin + offset, heap_begin + end, manual);
101 }
102 }
103 }
104
105 template <size_t kAlignment>
RunTestCount()106 static void RunTestCount()
107 {
108 auto count_test_fn = [](MemBitmap<kAlignment> *bitmap, object_pointer_type begin, object_pointer_type end,
109 size_t manual_count) {
110 size_t count = 0;
111 auto count_fn = [&count]([[maybe_unused]] void *obj) { count++; };
112 bitmap->IterateOverMarkedChunkInRange(ToVoidPtr(begin), ToVoidPtr(end), count_fn);
113 EXPECT_EQ(count, manual_count);
114 };
115 RunTest<kAlignment>(count_test_fn);
116 }
117
118 template <size_t kAlignment>
RunTestOrder()119 void RunTestOrder()
120 {
121 auto order_test_fn = [](MemBitmap<kAlignment> *bitmap, object_pointer_type begin, object_pointer_type end,
122 size_t manual_count) {
123 void *last_ptr = nullptr;
124 auto order_check = [&last_ptr](void *obj) {
125 EXPECT_LT(last_ptr, obj);
126 last_ptr = obj;
127 };
128
129 // Test complete walk.
130 bitmap->IterateOverChunks(order_check);
131 if (manual_count > 0) {
132 EXPECT_NE(nullptr, last_ptr);
133 }
134
135 // Test range.
136 last_ptr = nullptr;
137 bitmap->IterateOverMarkedChunkInRange(ToVoidPtr(begin), ToVoidPtr(end), order_check);
138 if (manual_count > 0) {
139 EXPECT_NE(nullptr, last_ptr);
140 }
141 };
142 RunTest<kAlignment>(order_test_fn);
143 }
144
TEST_F(BitmapTest,AtomicClearSetTest)145 TEST_F(BitmapTest, AtomicClearSetTest)
146 {
147 void *object = ToVoidPtr(HEAP_STARTING_ADDRESS);
148 const size_t sz = 1_MB;
149 auto bm_ptr = std::make_unique<BitmapWordType[]>(sz >> MemBitmap<>::LOG_BITSPERWORD);
150 MemBitmap<> bm(ToVoidPtr(HEAP_STARTING_ADDRESS), sz, bm_ptr.get());
151
152 // Set bit
153 ASSERT_TRUE(bm.Test(object) == bm.AtomicTestAndSet(object));
154 ASSERT_TRUE(bm.Test(object));
155 ASSERT_TRUE(bm.AtomicTest(object));
156
157 // Clear bit
158 ASSERT_TRUE(bm.Test(object) == bm.AtomicTestAndClear(object));
159 ASSERT_TRUE(!bm.Test(object));
160 ASSERT_TRUE(!bm.AtomicTest(object));
161 }
162
163 } // namespace panda::mem
164
165 #endif // RUNTIME_TESTS_BITMAP_TEST_BASE_H
166