Move testing properties to provider class (no classloading deadlock possible) and fallback to default provider in non-test mode

This commit is contained in:
Uwe Schindler 2023-10-15 14:33:43 +02:00
parent 650a47e707
commit c8852a012f
2 changed files with 46 additions and 17 deletions

View File

@ -26,8 +26,11 @@ import java.security.PrivilegedAction;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.apache.lucene.util.SuppressForbidden;
import org.apache.lucene.util.VectorUtil;
@ -40,6 +43,35 @@ import org.apache.lucene.util.VectorUtil;
*/
public abstract class VectorizationProvider {
static final OptionalInt TESTS_VECTOR_SIZE;
static final boolean TESTS_FORCE_INTEGER_VECTORS;
static {
var vs = OptionalInt.empty();
try {
vs =
Stream.ofNullable(System.getProperty("tests.vectorsize"))
.filter(Predicate.not(String::isEmpty))
.mapToInt(Integer::parseInt)
.findAny();
} catch (
@SuppressWarnings("unused")
SecurityException se) {
// ignored
}
TESTS_VECTOR_SIZE = vs;
boolean enforce = false;
try {
enforce = Boolean.getBoolean("tests.forceintegervectors");
} catch (
@SuppressWarnings("unused")
SecurityException se) {
// ignored
}
TESTS_FORCE_INTEGER_VECTORS = enforce;
}
/**
* Returns the default instance of the provider matching vectorization possibilities of actual
* runtime.
@ -90,11 +122,18 @@ public abstract class VectorizationProvider {
return new DefaultVectorizationProvider();
}
vectorMod.ifPresent(VectorizationProvider.class.getModule()::addReads);
// check if client VM
if (!testMode && isClientVM()) {
// check for testMode and otherwise fallback to default if slowness could happen
if (!testMode) {
if (TESTS_VECTOR_SIZE.isPresent() || TESTS_FORCE_INTEGER_VECTORS) {
LOG.warning(
"Vector bitsize and/or integer vectors enforcement; using default vectorization provider outside of testMode");
return new DefaultVectorizationProvider();
}
if (isClientVM()) {
LOG.warning("C2 compiler is disabled; Java vector incubator API can't be enabled");
return new DefaultVectorizationProvider();
}
}
try {
// we use method handles with lookup, so we do not need to deal with setAccessible as we
// have private access through the lookup:

View File

@ -57,10 +57,7 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {
// default to platform supported bitsize
int vectorBitSize = VectorShape.preferredShape().vectorBitSize();
// but allow easy overriding for testing
try {
vectorBitSize = Integer.getInteger("tests.vectorsize", vectorBitSize);
} catch (SecurityException ignored) {
}
vectorBitSize = VectorizationProvider.TESTS_VECTOR_SIZE.orElse(vectorBitSize);
INT_SPECIES = VectorSpecies.of(int.class, VectorShape.forBitSize(vectorBitSize));
VECTOR_BITSIZE = INT_SPECIES.vectorBitSize();
FLOAT_SPECIES = INT_SPECIES.withLanes(float.class);
@ -76,15 +73,8 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {
// hotspot misses some SSE intrinsics, workaround it
// to be fair, they do document this thing only works well with AVX2/AVX3 and Neon
boolean isAMD64withoutAVX2 = Constants.OS_ARCH.equals("amd64") && VECTOR_BITSIZE < 256;
boolean hasFastIntegerVectors = isAMD64withoutAVX2 == false;
try {
hasFastIntegerVectors =
Boolean.parseBoolean(
System.getProperty(
"tests.forceintegervectors", Boolean.toString(hasFastIntegerVectors)));
} catch (SecurityException ignored) {
}
HAS_FAST_INTEGER_VECTORS = hasFastIntegerVectors;
HAS_FAST_INTEGER_VECTORS =
VectorizationProvider.TESTS_FORCE_INTEGER_VECTORS || (isAMD64withoutAVX2 == false);
}
@Override