blob: edb08ef3d9362e6a4064fb44363ff7fd22fb2936 [file] [log] [blame]
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001/*
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 Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include <stdint.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021
22#include "common_runtime_test.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070023#include "globals.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "space_bitmap-inl.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070025
Mathieu Chartiercc236d72012-07-20 10:29:05 -070026namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace accounting {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070029
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class SpaceBitmapTest : public CommonRuntimeTest {};
Mathieu Chartiercc236d72012-07-20 10:29:05 -070031
32TEST_F(SpaceBitmapTest, Init) {
Ian Rogers13735952014-10-08 12:43:28 -070033 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070034 size_t heap_capacity = 16 * MB;
Ian Rogers700a4022014-05-19 16:49:03 -070035 std::unique_ptr<ContinuousSpaceBitmap> space_bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070036 ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 EXPECT_TRUE(space_bitmap.get() != nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070038}
39
40class BitmapVerify {
41 public:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070042 BitmapVerify(ContinuousSpaceBitmap* bitmap, const mirror::Object* begin,
43 const mirror::Object* end)
Mathieu Chartiercc236d72012-07-20 10:29:05 -070044 : bitmap_(bitmap),
45 begin_(begin),
46 end_(end) {}
47
Brian Carlstromdf629502013-07-17 22:39:56 -070048 void operator()(const mirror::Object* obj) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070049 EXPECT_TRUE(obj >= begin_);
50 EXPECT_TRUE(obj <= end_);
Brian Carlstrom42748892013-07-18 18:04:08 -070051 EXPECT_EQ(bitmap_->Test(obj), ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0));
Mathieu Chartiercc236d72012-07-20 10:29:05 -070052 }
53
Ian Rogers13735952014-10-08 12:43:28 -070054 ContinuousSpaceBitmap* const bitmap_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 const mirror::Object* begin_;
56 const mirror::Object* end_;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070057};
58
59TEST_F(SpaceBitmapTest, ScanRange) {
Ian Rogers13735952014-10-08 12:43:28 -070060 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070061 size_t heap_capacity = 16 * MB;
62
Ian Rogers700a4022014-05-19 16:49:03 -070063 std::unique_ptr<ContinuousSpaceBitmap> space_bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070064 ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070065 EXPECT_TRUE(space_bitmap.get() != nullptr);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070066
Ian Rogers13735952014-10-08 12:43:28 -070067 // Set all the odd bits in the first BitsPerIntPtrT * 3 to one.
68 for (size_t j = 0; j < kBitsPerIntPtrT * 3; ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 const mirror::Object* obj =
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070070 reinterpret_cast<mirror::Object*>(heap_begin + j * kObjectAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070071 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 Rogers13735952014-10-08 12:43:28 -070079 for (size_t i = 0; i < static_cast<size_t>(kBitsPerIntPtrT); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 mirror::Object* start =
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070081 reinterpret_cast<mirror::Object*>(heap_begin + i * kObjectAlignment);
Ian Rogers13735952014-10-08 12:43:28 -070082 for (size_t j = 0; j < static_cast<size_t>(kBitsPerIntPtrT * 2); ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 mirror::Object* end =
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070084 reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * kObjectAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070085 BitmapVerify(space_bitmap.get(), start, end);
86 }
87 }
88}
89
Andreas Gampebe73e572014-04-03 10:46:42 -070090class SimpleCounter {
91 public:
92 explicit SimpleCounter(size_t* counter) : count_(counter) {}
93
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070094 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED) const {
Andreas Gampebe73e572014-04-03 10:46:42 -070095 (*count_)++;
96 }
97
Ian Rogers13735952014-10-08 12:43:28 -070098 size_t* const count_;
Andreas Gampebe73e572014-04-03 10:46:42 -070099};
100
101class 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 Chartierbbd695c2014-04-16 09:48:48 -0700113template <size_t kAlignment>
114void RunTest() NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers13735952014-10-08 12:43:28 -0700115 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x10000000);
Andreas Gampebe73e572014-04-03 10:46:42 -0700116 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 Rogers700a4022014-05-19 16:49:03 -0700123 std::unique_ptr<ContinuousSpaceBitmap> space_bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700124 ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
Andreas Gampebe73e572014-04-03 10:46:42 -0700125
126 for (int j = 0; j < 10000; ++j) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700127 size_t offset = RoundDown(r.next() % heap_capacity, kAlignment);
Andreas Gampebe73e572014-04-03 10:46:42 -0700128 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 Chartierbbd695c2014-04-16 09:48:48 -0700141 size_t offset = RoundDown(r.next() % heap_capacity, kAlignment);
Andreas Gampebe73e572014-04-03 10:46:42 -0700142 size_t remain = heap_capacity - offset;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700143 size_t end = offset + RoundDown(r.next() % (remain + 1), kAlignment);
Andreas Gampebe73e572014-04-03 10:46:42 -0700144
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 Chartierbbd695c2014-04-16 09:48:48 -0700149 for (uintptr_t k = offset; k < end; k += kAlignment) {
Andreas Gampebe73e572014-04-03 10:46:42 -0700150 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 Chartierbbd695c2014-04-16 09:48:48 -0700160TEST_F(SpaceBitmapTest, VisitorObjectAlignment) {
161 RunTest<kObjectAlignment>();
162}
163
164TEST_F(SpaceBitmapTest, VisitorPageAlignment) {
165 RunTest<kPageSize>();
Andreas Gampebe73e572014-04-03 10:46:42 -0700166}
167
Ian Rogers1d54e732013-05-02 21:10:01 -0700168} // namespace accounting
169} // namespace gc
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700170} // namespace art