Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 19 | #include <stdint.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 21 | |
| 22 | #include "common_runtime_test.h" |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 23 | #include "globals.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "space_bitmap-inl.h" |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 25 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 26 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | namespace gc { |
| 28 | namespace accounting { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | class SpaceBitmapTest : public CommonRuntimeTest {}; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 31 | |
| 32 | TEST_F(SpaceBitmapTest, Init) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 33 | uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 34 | size_t heap_capacity = 16 * MB; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 35 | std::unique_ptr<ContinuousSpaceBitmap> space_bitmap( |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 36 | ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame^] | 37 | EXPECT_TRUE(space_bitmap.get() != nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | class BitmapVerify { |
| 41 | public: |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 42 | BitmapVerify(ContinuousSpaceBitmap* bitmap, const mirror::Object* begin, |
| 43 | const mirror::Object* end) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 44 | : bitmap_(bitmap), |
| 45 | begin_(begin), |
| 46 | end_(end) {} |
| 47 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 48 | void operator()(const mirror::Object* obj) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 49 | EXPECT_TRUE(obj >= begin_); |
| 50 | EXPECT_TRUE(obj <= end_); |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 51 | EXPECT_EQ(bitmap_->Test(obj), ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 54 | ContinuousSpaceBitmap* const bitmap_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | const mirror::Object* begin_; |
| 56 | const mirror::Object* end_; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | TEST_F(SpaceBitmapTest, ScanRange) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 60 | uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 61 | size_t heap_capacity = 16 * MB; |
| 62 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 63 | std::unique_ptr<ContinuousSpaceBitmap> space_bitmap( |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 64 | ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame^] | 65 | EXPECT_TRUE(space_bitmap.get() != nullptr); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 66 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 67 | // Set all the odd bits in the first BitsPerIntPtrT * 3 to one. |
| 68 | for (size_t j = 0; j < kBitsPerIntPtrT * 3; ++j) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | const mirror::Object* obj = |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 70 | reinterpret_cast<mirror::Object*>(heap_begin + j * kObjectAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 71 | if (reinterpret_cast<uintptr_t>(obj) & 0xF) { |
| 72 | space_bitmap->Set(obj); |
| 73 | } |
| 74 | } |
| 75 | // Try every possible starting bit in the first word. Then for each starting bit, try each |
| 76 | // possible length up to a maximum of kBitsPerWord * 2 - 1 bits. |
| 77 | // This handles all the cases, having runs which start and end on the same word, and different |
| 78 | // words. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 79 | for (size_t i = 0; i < static_cast<size_t>(kBitsPerIntPtrT); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | mirror::Object* start = |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 81 | reinterpret_cast<mirror::Object*>(heap_begin + i * kObjectAlignment); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 82 | for (size_t j = 0; j < static_cast<size_t>(kBitsPerIntPtrT * 2); ++j) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | mirror::Object* end = |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 84 | reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * kObjectAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 85 | BitmapVerify(space_bitmap.get(), start, end); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 90 | class SimpleCounter { |
| 91 | public: |
| 92 | explicit SimpleCounter(size_t* counter) : count_(counter) {} |
| 93 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 94 | void operator()(mirror::Object* obj ATTRIBUTE_UNUSED) const { |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 95 | (*count_)++; |
| 96 | } |
| 97 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 98 | size_t* const count_; |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | class RandGen { |
| 102 | public: |
| 103 | explicit RandGen(uint32_t seed) : val_(seed) {} |
| 104 | |
| 105 | uint32_t next() { |
| 106 | val_ = val_ * 48271 % 2147483647; |
| 107 | return val_; |
| 108 | } |
| 109 | |
| 110 | uint32_t val_; |
| 111 | }; |
| 112 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 113 | template <size_t kAlignment> |
| 114 | void RunTest() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 115 | uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000); |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 116 | size_t heap_capacity = 16 * MB; |
| 117 | |
| 118 | // Seed with 0x1234 for reproducability. |
| 119 | RandGen r(0x1234); |
| 120 | |
| 121 | |
| 122 | for (int i = 0; i < 5 ; ++i) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 123 | std::unique_ptr<ContinuousSpaceBitmap> space_bitmap( |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 124 | ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity)); |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 125 | |
| 126 | for (int j = 0; j < 10000; ++j) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 127 | size_t offset = RoundDown(r.next() % heap_capacity, kAlignment); |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 128 | bool set = r.next() % 2 == 1; |
| 129 | |
| 130 | if (set) { |
| 131 | space_bitmap->Set(reinterpret_cast<mirror::Object*>(heap_begin + offset)); |
| 132 | } else { |
| 133 | space_bitmap->Clear(reinterpret_cast<mirror::Object*>(heap_begin + offset)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | for (int j = 0; j < 50; ++j) { |
| 138 | size_t count = 0; |
| 139 | SimpleCounter c(&count); |
| 140 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 141 | size_t offset = RoundDown(r.next() % heap_capacity, kAlignment); |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 142 | size_t remain = heap_capacity - offset; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 143 | size_t end = offset + RoundDown(r.next() % (remain + 1), kAlignment); |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 144 | |
| 145 | space_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(heap_begin) + offset, |
| 146 | reinterpret_cast<uintptr_t>(heap_begin) + end, c); |
| 147 | |
| 148 | size_t manual = 0; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 149 | for (uintptr_t k = offset; k < end; k += kAlignment) { |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 150 | if (space_bitmap->Test(reinterpret_cast<mirror::Object*>(heap_begin + k))) { |
| 151 | manual++; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | EXPECT_EQ(count, manual); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 160 | TEST_F(SpaceBitmapTest, VisitorObjectAlignment) { |
| 161 | RunTest<kObjectAlignment>(); |
| 162 | } |
| 163 | |
| 164 | TEST_F(SpaceBitmapTest, VisitorPageAlignment) { |
| 165 | RunTest<kPageSize>(); |
Andreas Gampe | be73e57 | 2014-04-03 10:46:42 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 168 | } // namespace accounting |
| 169 | } // namespace gc |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 170 | } // namespace art |