1 /*
2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2020 SAP SE. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "precompiled.hpp"
27 #include "memory/metaspace/binList.hpp"
28 #include "memory/metaspace/counters.hpp"
29 //#define LOG_PLEASE
30 #include "metaspaceGtestCommon.hpp"
31
32 using metaspace::BinList32;
33 using metaspace::BinListImpl;
34 using metaspace::MemRangeCounter;
35
36 #define CHECK_BL_CONTENT(bl, expected_num, expected_size) { \
37 EXPECT_EQ(bl.count(), (unsigned)expected_num); \
38 EXPECT_EQ(bl.total_size(), (size_t)expected_size); \
39 if (expected_num == 0) { \
40 EXPECT_TRUE(bl.is_empty()); \
41 } else { \
42 EXPECT_FALSE(bl.is_empty()); \
43 } \
44 }
45
46 template <class BINLISTTYPE>
47 struct BinListBasicTest {
48
49 static const size_t minws;
50 static const size_t maxws;
51
basic_testBinListBasicTest52 static void basic_test() {
53
54 BINLISTTYPE bl;
55
56 CHECK_BL_CONTENT(bl, 0, 0);
57
58 MetaWord arr[1000];
59
60 size_t innocous_size = minws + ((maxws - minws) / 2);
61
62 // Try to get a block from an empty list.
63 size_t real_size = 4711;
64 MetaWord* p = bl.remove_block(innocous_size, &real_size);
65 EXPECT_EQ(p, (MetaWord*)NULL);
66 EXPECT_EQ((size_t)0, real_size);
67
68 // Add a block...
69 bl.add_block(arr, innocous_size);
70 CHECK_BL_CONTENT(bl, 1, innocous_size);
71 DEBUG_ONLY(bl.verify();)
72
73 // And retrieve it.
74 real_size = 4711;
75 p = bl.remove_block(innocous_size, &real_size);
76 EXPECT_EQ(p, arr);
77 EXPECT_EQ((size_t)innocous_size, real_size);
78 CHECK_BL_CONTENT(bl, 0, 0);
79 DEBUG_ONLY(bl.verify();)
80
81 }
82
basic_test_2BinListBasicTest83 static void basic_test_2() {
84
85 BINLISTTYPE bl;
86
87 CHECK_BL_CONTENT(bl, 0, 0);
88
89 MetaWord arr[1000];
90
91 for (size_t s1 = minws; s1 <= maxws; s1++) {
92 for (size_t s2 = minws; s2 <= maxws; s2++) {
93
94 bl.add_block(arr, s1);
95 CHECK_BL_CONTENT(bl, 1, s1);
96 DEBUG_ONLY(bl.verify();)
97
98 size_t real_size = 4711;
99 MetaWord* p = bl.remove_block(s2, &real_size);
100 if (s1 >= s2) {
101 EXPECT_EQ(p, arr);
102 EXPECT_EQ((size_t)s1, real_size);
103 CHECK_BL_CONTENT(bl, 0, 0);
104 DEBUG_ONLY(bl.verify();)
105 } else {
106 EXPECT_EQ(p, (MetaWord*)NULL);
107 EXPECT_EQ((size_t)0, real_size);
108 CHECK_BL_CONTENT(bl, 1, s1);
109 DEBUG_ONLY(bl.verify();)
110 // drain bl
111 p = bl.remove_block(minws, &real_size);
112 EXPECT_EQ(p, arr);
113 EXPECT_EQ((size_t)s1, real_size);
114 CHECK_BL_CONTENT(bl, 0, 0);
115 }
116 }
117 }
118 }
119
random_testBinListBasicTest120 static void random_test() {
121
122 BINLISTTYPE bl[2];
123 MemRangeCounter cnt[2];
124
125 #define CHECK_COUNTERS \
126 ASSERT_EQ(cnt[0].count(), bl[0].count()); \
127 ASSERT_EQ(cnt[1].count(), bl[1].count()); \
128 ASSERT_EQ(cnt[0].total_size(), bl[0].total_size()); \
129 ASSERT_EQ(cnt[1].total_size(), bl[1].total_size());
130
131 FeederBuffer fb(1024);
132 RandSizeGenerator rgen(minws, maxws + 1);
133
134 // feed all
135 int which = 0;
136 for (;;) {
137 size_t s = rgen.get();
138 MetaWord* p = fb.get(s);
139 if (p != NULL) {
140 bl[which].add_block(p, s);
141 cnt[which].add(s);
142 which = which == 0 ? 1 : 0;
143 } else {
144 break;
145 }
146 }
147
148 CHECK_COUNTERS;
149 DEBUG_ONLY(bl[0].verify();)
150 DEBUG_ONLY(bl[1].verify();)
151
152 // play pingpong
153 for (int iter = 0; iter < 1000; iter++) {
154 size_t s = rgen.get();
155 int taker = iter % 2;
156 int giver = taker == 0 ? 1 : 0;
157
158 size_t real_size = 4711;
159 MetaWord* p = bl[giver].remove_block(s, &real_size);
160 if (p != NULL) {
161
162 ASSERT_TRUE(fb.is_valid_range(p, real_size));
163 ASSERT_GE(real_size, s);
164 cnt[giver].sub(real_size);
165
166 bl[taker].add_block(p, real_size);
167 cnt[taker].add(real_size);
168
169 } else {
170 ASSERT_EQ(real_size, (size_t)NULL);
171 }
172
173 CHECK_COUNTERS;
174
175 }
176
177 CHECK_COUNTERS;
178 DEBUG_ONLY(bl[0].verify();)
179 DEBUG_ONLY(bl[1].verify();)
180
181 // drain both lists.
182 for (int which = 0; which < 2; which++) {
183 size_t last_size = 0;
184 while (bl[which].is_empty() == false) {
185
186 size_t real_size = 4711;
187 MetaWord* p = bl[which].remove_block(minws, &real_size);
188
189 ASSERT_NE(p, (MetaWord*) NULL);
190 ASSERT_GE(real_size, minws);
191 ASSERT_TRUE(fb.is_valid_range(p, real_size));
192
193 // This must hold true since we always return the smallest fit.
194 ASSERT_GE(real_size, last_size);
195 if (real_size > last_size) {
196 last_size = real_size;
197 }
198
199 cnt[which].sub(real_size);
200
201 CHECK_COUNTERS;
202 }
203 }
204
205 }
206 };
207
208 template <typename BINLISTTYPE> const size_t BinListBasicTest<BINLISTTYPE>::minws = BINLISTTYPE::MinWordSize;
209 template <typename BINLISTTYPE> const size_t BinListBasicTest<BINLISTTYPE>::maxws = BINLISTTYPE::MaxWordSize;
210
TEST_VM(metaspace,BinList_basic_8)211 TEST_VM(metaspace, BinList_basic_8) { BinListBasicTest< BinListImpl<2, 8> >::basic_test(); }
TEST_VM(metaspace,BinList_basic_16)212 TEST_VM(metaspace, BinList_basic_16) { BinListBasicTest< BinListImpl<2, 16> >::basic_test(); }
TEST_VM(metaspace,BinList_basic_32)213 TEST_VM(metaspace, BinList_basic_32) { BinListBasicTest<BinList32>::basic_test(); }
TEST_VM(metaspace,BinList_basic_1331)214 TEST_VM(metaspace, BinList_basic_1331) { BinListBasicTest< BinListImpl<13, 31> >::basic_test(); }
TEST_VM(metaspace,BinList_basic_131)215 TEST_VM(metaspace, BinList_basic_131) { BinListBasicTest< BinListImpl<13, 1> >::basic_test(); }
216
TEST_VM(metaspace,BinList_basic2_8)217 TEST_VM(metaspace, BinList_basic2_8) { BinListBasicTest< BinListImpl<2, 8> >::basic_test_2(); }
TEST_VM(metaspace,BinList_basic2_16)218 TEST_VM(metaspace, BinList_basic2_16) { BinListBasicTest< BinListImpl<2, 16> >::basic_test_2(); }
TEST_VM(metaspace,BinList_basic2_32)219 TEST_VM(metaspace, BinList_basic2_32) { BinListBasicTest<BinList32 >::basic_test_2(); }
TEST_VM(metaspace,BinList_basic2_1331)220 TEST_VM(metaspace, BinList_basic2_1331) { BinListBasicTest< BinListImpl<13, 31> >::basic_test_2(); }
TEST_VM(metaspace,BinList_basic2_131)221 TEST_VM(metaspace, BinList_basic2_131) { BinListBasicTest< BinListImpl<13, 1> >::basic_test_2(); }
222
TEST_VM(metaspace,BinList_random_test_8)223 TEST_VM(metaspace, BinList_random_test_8) { BinListBasicTest< BinListImpl<2, 8> >::random_test(); }
TEST_VM(metaspace,BinList_random_test_16)224 TEST_VM(metaspace, BinList_random_test_16) { BinListBasicTest< BinListImpl<2, 16> >::random_test(); }
TEST_VM(metaspace,BinList_random_test_32)225 TEST_VM(metaspace, BinList_random_test_32) { BinListBasicTest<BinList32>::random_test(); }
TEST_VM(metaspace,BinList_random_test_1331)226 TEST_VM(metaspace, BinList_random_test_1331) { BinListBasicTest< BinListImpl<13, 31> >::random_test(); }
TEST_VM(metaspace,BinList_random_test_131)227 TEST_VM(metaspace, BinList_random_test_131) { BinListBasicTest< BinListImpl<13, 1> >::random_test(); }
228
229