Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/stress/AbstractStressArrayCopy.java
64507 views
/*1* Copyright (c) 2021, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.arraycopy.stress;2425import java.util.Random;2627public abstract class AbstractStressArrayCopy {28/**29* Max array size to test. This should be reasonably high to test30* massive vectorized copies, plus cases that cross the cache lines and31* (small) page boundaries. But it should also be reasonably low to32* keep the test costs down.33*34* A rough guideline:35* - AVX-512: 64-byte copies over 32 registers copies roughly 2K per step.36* - AArch64: small pages can be about 64K large37*/38static final int MAX_SIZE = 128*1024 + 1;3940/**41* Arrays up to this size would be tested exhaustively: with all combinations42* of source/destination starts and copy lengths. Exercise restraint when bumping43* this value, as the test costs are proportional to N^3 of this setting.44*/45static final int EXHAUSTIVE_SIZES = Integer.getInteger("exhaustiveSizes", 192);4647/*48* Larger arrays would fuzzed with this many attempts.49*/50static final int FUZZ_COUNT = Integer.getInteger("fuzzCount", 300);5152public static void throwSeedError(int len, int pos) {53throw new RuntimeException("Error after seed: " +54len + " elements, at pos " + pos);55}5657public static void throwContentsError(int l, int r, int len, int pos) {58throwError("in contents", l, r, len, pos);59}6061public static void throwHeadError(int l, int r, int len, int pos) {62throwError("in head", l, r, len, pos);63}6465public static void throwTailError(int l, int r, int len, int pos) {66throwError("in tail", l, r, len, pos);67}6869private static void throwError(String phase, int l, int r, int len, int pos) {70throw new RuntimeException("Error " + phase + ": " +71len + " elements, " +72"[" + l + ", " + (l+len) + ") -> " +73"[" + r + ", " + (r+len) + "), " +74"at pos " + pos);75}7677protected abstract void testWith(int size, int l, int r, int len);7879private void checkBounds(int size, int l, int r, int len) {80if (l >= size) throw new IllegalStateException("l is out of bounds");81if (l + len > size) throw new IllegalStateException("l+len is out of bounds");82if (r >= size) throw new IllegalStateException("r is out of bounds");83if (r + len > size) throw new IllegalStateException("r+len is out of bounds: " + l + " " + r + " " + len + " " + size);84}8586private void checkDisjoint(int size, int l, int r, int len) {87if (l == r) throw new IllegalStateException("Not disjoint: l == r");88if (l < r && l + len > r) throw new IllegalStateException("Not disjoint");89if (l > r && r + len > l) throw new IllegalStateException("Not disjoint");90}9192private void checkConjoint(int size, int l, int r, int len) {93if (l == r) return; // Definitely conjoint, even with zero len94if (l < r && l + len < r) throw new IllegalStateException("Not conjoint");95if (l > r && r + len < l) throw new IllegalStateException("Not conjoint");96}9798public void exhaustiveWith(int size) {99for (int l = 0; l < size; l++) {100for (int r = 0; r < size; r++) {101int maxLen = Math.min(size - l, size - r);102for (int len = 0; len <= maxLen; len++) {103checkBounds(size, l, r, len);104testWith(size, l, r, len);105}106}107}108}109110public void fuzzWith(Random rand, int size) {111// Some basic checks first112testWith(size, 0, 1, 1);113testWith(size, 0, 1, size-1);114115// Test disjoint:116for (int c = 0; c < FUZZ_COUNT; c++) {117int l = rand.nextInt(size / 2);118int len = rand.nextInt((size - l) / 2);119int r = (l + len + 1) + rand.nextInt(size - 2*len - l - 1);120121checkBounds(size, l, r, len);122checkDisjoint(size, l, r, len);123124testWith(size, l, r, len);125testWith(size, r, l, len);126}127128// Test conjoint:129for (int c = 0; c < FUZZ_COUNT; c++) {130int l = rand.nextInt(size);131int len = rand.nextInt(size - l);132int r = Math.min(l + (len > 0 ? rand.nextInt(len) : 0), size - len);133134checkBounds(size, l, r, len);135checkConjoint(size, l, r, len);136137testWith(size, l, r, len);138testWith(size, r, l, len);139}140}141142public void run(Random rand) {143// Exhaustive on all small arrays144for (int size = 1; size <= EXHAUSTIVE_SIZES; size++) {145exhaustiveWith(size);146}147148// Fuzz powers of ten149for (int size = 10; size < MAX_SIZE; size *= 10) {150if (size <= EXHAUSTIVE_SIZES) continue;151fuzzWith(rand, size - 1);152fuzzWith(rand, size);153fuzzWith(rand, size + 1);154}155156// Fuzz powers of two157for (int size = 2; size < MAX_SIZE; size *= 2) {158if (size <= EXHAUSTIVE_SIZES) continue;159fuzzWith(rand, size - 1);160fuzzWith(rand, size);161fuzzWith(rand, size + 1);162}163}164165}166167168