Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/gc/z/test_zForwarding.cpp
41149 views
1
/*
2
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include "precompiled.hpp"
25
#include "gc/z/zAddress.inline.hpp"
26
#include "gc/z/zForwarding.inline.hpp"
27
#include "gc/z/zForwardingAllocator.inline.hpp"
28
#include "gc/z/zGlobals.hpp"
29
#include "gc/z/zPage.inline.hpp"
30
#include "unittest.hpp"
31
32
using namespace testing;
33
34
#define CAPTURE_DELIM "\n"
35
#define CAPTURE1(expression) #expression << " evaluates to " << expression
36
#define CAPTURE2(e0, e1) CAPTURE1(e0) << CAPTURE_DELIM << CAPTURE1(e1)
37
38
#define CAPTURE(expression) CAPTURE1(expression)
39
40
class ZForwardingTest : public Test {
41
public:
42
// Helper functions
43
44
class SequenceToFromIndex : AllStatic {
45
public:
46
static uintptr_t even(size_t sequence_number) {
47
return sequence_number * 2;
48
}
49
static uintptr_t odd(size_t sequence_number) {
50
return even(sequence_number) + 1;
51
}
52
static uintptr_t one_to_one(size_t sequence_number) {
53
return sequence_number;
54
}
55
};
56
57
// Test functions
58
59
static void setup(ZForwarding* forwarding) {
60
EXPECT_PRED1(is_power_of_2<size_t>, forwarding->_entries.length()) << CAPTURE(forwarding->_entries.length());
61
}
62
63
static void find_empty(ZForwarding* forwarding) {
64
size_t size = forwarding->_entries.length();
65
size_t entries_to_check = size * 2;
66
67
for (size_t i = 0; i < entries_to_check; i++) {
68
uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
69
70
ZForwardingCursor cursor;
71
ZForwardingEntry entry = forwarding->find(from_index, &cursor);
72
EXPECT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
73
}
74
}
75
76
static void find_full(ZForwarding* forwarding) {
77
size_t size = forwarding->_entries.length();
78
size_t entries_to_populate = size;
79
80
// Populate
81
for (size_t i = 0; i < entries_to_populate; i++) {
82
uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
83
84
ZForwardingCursor cursor;
85
ZForwardingEntry entry = forwarding->find(from_index, &cursor);
86
ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
87
88
forwarding->insert(from_index, from_index, &cursor);
89
}
90
91
// Verify
92
for (size_t i = 0; i < entries_to_populate; i++) {
93
uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
94
95
ZForwardingCursor cursor;
96
ZForwardingEntry entry = forwarding->find(from_index, &cursor);
97
ASSERT_TRUE(entry.populated()) << CAPTURE2(from_index, size);
98
99
ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);
100
ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);
101
}
102
}
103
104
static void find_every_other(ZForwarding* forwarding) {
105
size_t size = forwarding->_entries.length();
106
size_t entries_to_populate = size / 2;
107
108
// Populate even from indices
109
for (size_t i = 0; i < entries_to_populate; i++) {
110
uintptr_t from_index = SequenceToFromIndex::even(i);
111
112
ZForwardingCursor cursor;
113
ZForwardingEntry entry = forwarding->find(from_index, &cursor);
114
ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
115
116
forwarding->insert(from_index, from_index, &cursor);
117
}
118
119
// Verify populated even indices
120
for (size_t i = 0; i < entries_to_populate; i++) {
121
uintptr_t from_index = SequenceToFromIndex::even(i);
122
123
ZForwardingCursor cursor;
124
ZForwardingEntry entry = forwarding->find(from_index, &cursor);
125
ASSERT_TRUE(entry.populated()) << CAPTURE2(from_index, size);
126
127
ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);
128
ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);
129
}
130
131
// Verify empty odd indices
132
//
133
// This check could be done on a larger range of sequence numbers,
134
// but currently entries_to_populate is used.
135
for (size_t i = 0; i < entries_to_populate; i++) {
136
uintptr_t from_index = SequenceToFromIndex::odd(i);
137
138
ZForwardingCursor cursor;
139
ZForwardingEntry entry = forwarding->find(from_index, &cursor);
140
141
ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
142
}
143
}
144
145
static void test(void (*function)(ZForwarding*), uint32_t size) {
146
// Create page
147
const ZVirtualMemory vmem(0, ZPageSizeSmall);
148
const ZPhysicalMemory pmem(ZPhysicalMemorySegment(0, ZPageSizeSmall, true));
149
ZPage page(ZPageTypeSmall, vmem, pmem);
150
151
page.reset();
152
153
const size_t object_size = 16;
154
const uintptr_t object = page.alloc_object(object_size);
155
156
ZGlobalSeqNum++;
157
158
bool dummy = false;
159
page.mark_object(ZAddress::marked(object), dummy, dummy);
160
161
const uint32_t live_objects = size;
162
const size_t live_bytes = live_objects * object_size;
163
page.inc_live(live_objects, live_bytes);
164
165
// Setup allocator
166
ZForwardingAllocator allocator;
167
const uint32_t nentries = ZForwarding::nentries(&page);
168
allocator.reset((sizeof(ZForwarding)) + (nentries * sizeof(ZForwardingEntry)));
169
170
// Setup forwarding
171
ZForwarding* const forwarding = ZForwarding::alloc(&allocator, &page);
172
173
// Actual test function
174
(*function)(forwarding);
175
}
176
177
// Run the given function with a few different input values.
178
static void test(void (*function)(ZForwarding*)) {
179
test(function, 1);
180
test(function, 2);
181
test(function, 3);
182
test(function, 4);
183
test(function, 7);
184
test(function, 8);
185
test(function, 1023);
186
test(function, 1024);
187
test(function, 1025);
188
}
189
};
190
191
TEST_F(ZForwardingTest, setup) {
192
test(&ZForwardingTest::setup);
193
}
194
195
TEST_F(ZForwardingTest, find_empty) {
196
test(&ZForwardingTest::find_empty);
197
}
198
199
TEST_F(ZForwardingTest, find_full) {
200
test(&ZForwardingTest::find_full);
201
}
202
203
TEST_F(ZForwardingTest, find_every_other) {
204
test(&ZForwardingTest::find_every_other);
205
}
206
207