Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/stress/AbstractStressArrayCopy.java
64507 views
1
/*
2
* Copyright (c) 2021, Red Hat, Inc. 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
package compiler.arraycopy.stress;
25
26
import java.util.Random;
27
28
public abstract class AbstractStressArrayCopy {
29
/**
30
* Max array size to test. This should be reasonably high to test
31
* massive vectorized copies, plus cases that cross the cache lines and
32
* (small) page boundaries. But it should also be reasonably low to
33
* keep the test costs down.
34
*
35
* A rough guideline:
36
* - AVX-512: 64-byte copies over 32 registers copies roughly 2K per step.
37
* - AArch64: small pages can be about 64K large
38
*/
39
static final int MAX_SIZE = 128*1024 + 1;
40
41
/**
42
* Arrays up to this size would be tested exhaustively: with all combinations
43
* of source/destination starts and copy lengths. Exercise restraint when bumping
44
* this value, as the test costs are proportional to N^3 of this setting.
45
*/
46
static final int EXHAUSTIVE_SIZES = Integer.getInteger("exhaustiveSizes", 192);
47
48
/*
49
* Larger arrays would fuzzed with this many attempts.
50
*/
51
static final int FUZZ_COUNT = Integer.getInteger("fuzzCount", 300);
52
53
public static void throwSeedError(int len, int pos) {
54
throw new RuntimeException("Error after seed: " +
55
len + " elements, at pos " + pos);
56
}
57
58
public static void throwContentsError(int l, int r, int len, int pos) {
59
throwError("in contents", l, r, len, pos);
60
}
61
62
public static void throwHeadError(int l, int r, int len, int pos) {
63
throwError("in head", l, r, len, pos);
64
}
65
66
public static void throwTailError(int l, int r, int len, int pos) {
67
throwError("in tail", l, r, len, pos);
68
}
69
70
private static void throwError(String phase, int l, int r, int len, int pos) {
71
throw new RuntimeException("Error " + phase + ": " +
72
len + " elements, " +
73
"[" + l + ", " + (l+len) + ") -> " +
74
"[" + r + ", " + (r+len) + "), " +
75
"at pos " + pos);
76
}
77
78
protected abstract void testWith(int size, int l, int r, int len);
79
80
private void checkBounds(int size, int l, int r, int len) {
81
if (l >= size) throw new IllegalStateException("l is out of bounds");
82
if (l + len > size) throw new IllegalStateException("l+len is out of bounds");
83
if (r >= size) throw new IllegalStateException("r is out of bounds");
84
if (r + len > size) throw new IllegalStateException("r+len is out of bounds: " + l + " " + r + " " + len + " " + size);
85
}
86
87
private void checkDisjoint(int size, int l, int r, int len) {
88
if (l == r) throw new IllegalStateException("Not disjoint: l == r");
89
if (l < r && l + len > r) throw new IllegalStateException("Not disjoint");
90
if (l > r && r + len > l) throw new IllegalStateException("Not disjoint");
91
}
92
93
private void checkConjoint(int size, int l, int r, int len) {
94
if (l == r) return; // Definitely conjoint, even with zero len
95
if (l < r && l + len < r) throw new IllegalStateException("Not conjoint");
96
if (l > r && r + len < l) throw new IllegalStateException("Not conjoint");
97
}
98
99
public void exhaustiveWith(int size) {
100
for (int l = 0; l < size; l++) {
101
for (int r = 0; r < size; r++) {
102
int maxLen = Math.min(size - l, size - r);
103
for (int len = 0; len <= maxLen; len++) {
104
checkBounds(size, l, r, len);
105
testWith(size, l, r, len);
106
}
107
}
108
}
109
}
110
111
public void fuzzWith(Random rand, int size) {
112
// Some basic checks first
113
testWith(size, 0, 1, 1);
114
testWith(size, 0, 1, size-1);
115
116
// Test disjoint:
117
for (int c = 0; c < FUZZ_COUNT; c++) {
118
int l = rand.nextInt(size / 2);
119
int len = rand.nextInt((size - l) / 2);
120
int r = (l + len + 1) + rand.nextInt(size - 2*len - l - 1);
121
122
checkBounds(size, l, r, len);
123
checkDisjoint(size, l, r, len);
124
125
testWith(size, l, r, len);
126
testWith(size, r, l, len);
127
}
128
129
// Test conjoint:
130
for (int c = 0; c < FUZZ_COUNT; c++) {
131
int l = rand.nextInt(size);
132
int len = rand.nextInt(size - l);
133
int r = Math.min(l + (len > 0 ? rand.nextInt(len) : 0), size - len);
134
135
checkBounds(size, l, r, len);
136
checkConjoint(size, l, r, len);
137
138
testWith(size, l, r, len);
139
testWith(size, r, l, len);
140
}
141
}
142
143
public void run(Random rand) {
144
// Exhaustive on all small arrays
145
for (int size = 1; size <= EXHAUSTIVE_SIZES; size++) {
146
exhaustiveWith(size);
147
}
148
149
// Fuzz powers of ten
150
for (int size = 10; size < MAX_SIZE; size *= 10) {
151
if (size <= EXHAUSTIVE_SIZES) continue;
152
fuzzWith(rand, size - 1);
153
fuzzWith(rand, size);
154
fuzzWith(rand, size + 1);
155
}
156
157
// Fuzz powers of two
158
for (int size = 2; size < MAX_SIZE; size *= 2) {
159
if (size <= EXHAUSTIVE_SIZES) continue;
160
fuzzWith(rand, size - 1);
161
fuzzWith(rand, size);
162
fuzzWith(rand, size + 1);
163
}
164
}
165
166
}
167
168