Skip to content

Commit

Permalink
8313894: Rename isTrivial linker option to critical
Browse files Browse the repository at this point in the history
Reviewed-by: pminborg, mcimadamore
  • Loading branch information
JornVernee committed Aug 8, 2023
1 parent 29e621e commit ea82d41
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 58 deletions.
16 changes: 8 additions & 8 deletions src/java.base/share/classes/java/lang/foreign/Linker.java
Expand Up @@ -729,7 +729,7 @@ static Option firstVariadicArg(int index) {
* }
* }
*
* @apiNote This linker option can not be combined with {@link #isTrivial}.
* @apiNote This linker option can not be combined with {@link #critical}.
*
* @param capturedState the names of the values to save.
* @throws IllegalArgumentException if at least one of the provided {@code capturedState} names
Expand Down Expand Up @@ -773,19 +773,19 @@ static StructLayout captureStateLayout() {
}

/**
* {@return a linker option used to mark a foreign function as <em>trivial</em>}
* {@return a linker option used to mark a foreign function as <em>critical</em>}
* <p>
* A trivial function is a function that has an extremely short running time
* in all cases (similar to calling an empty function), and does not call back into Java (e.g. using an upcall stub).
* A critical function is a function that has an extremely short running time in all cases
* (similar to calling an empty function), and does not call back into Java (e.g. using an upcall stub).
* <p>
* Using this linker option is a hint which some implementations may use to apply
* optimizations that are only valid for trivial functions.
* optimizations that are only valid for critical functions.
* <p>
* Using this linker option when linking non trivial functions is likely to have adverse effects,
* Using this linker option when linking non-critical functions is likely to have adverse effects,
* such as loss of performance, or JVM crashes.
*/
static Option isTrivial() {
return LinkerOptions.IsTrivial.INSTANCE;
static Option critical() {
return LinkerOptions.Critical.INSTANCE;
}
}
}
24 changes: 12 additions & 12 deletions src/java.base/share/classes/jdk/internal/foreign/StringSupport.java
Expand Up @@ -45,9 +45,9 @@
*/
public class StringSupport {

// Maximum segment byte size for which a trivial method will be invoked.
private static final long MAX_TRIVIAL_SIZE = 1024L;
private static final MethodHandle STRNLEN_TRIVIAL;
// Maximum segment byte size for which a critical method will be invoked.
private static final long MAX_CRITICAL_SIZE = 1024L;
private static final MethodHandle STRNLEN_CRITICAL;
private static final MethodHandle STRNLEN;
private static final boolean SIZE_T_IS_INT;

Expand All @@ -57,7 +57,7 @@ public class StringSupport {
var strnlen = linker.defaultLookup().find("strnlen").orElseThrow();
var description = FunctionDescriptor.of(size_t, ADDRESS, size_t);

STRNLEN_TRIVIAL = linker.downcallHandle(strnlen, description, Linker.Option.isTrivial());
STRNLEN_CRITICAL = linker.downcallHandle(strnlen, description, Linker.Option.critical());
STRNLEN = linker.downcallHandle(strnlen, description);
SIZE_T_IS_INT = (size_t.byteSize() == Integer.BYTES);
}
Expand Down Expand Up @@ -131,8 +131,8 @@ private static int native_strlen_byte(MemorySegment segment, long start) {
long segmentSize = segment.byteSize();
final long len;
if (SIZE_T_IS_INT) {
if (segmentSize < MAX_TRIVIAL_SIZE) {
len = strnlen_int_trivial(segment, segmentSize);
if (segmentSize < MAX_CRITICAL_SIZE) {
len = strnlen_int_critical(segment, segmentSize);
} else if (segmentSize < Integer.MAX_VALUE * 2L) { // size_t is unsigned
len = strnlen_int(segment, segmentSize);
} else {
Expand All @@ -142,8 +142,8 @@ private static int native_strlen_byte(MemorySegment segment, long start) {
len = strlen_byte(segment, 0);
}
} else {
len = segmentSize < MAX_TRIVIAL_SIZE
? strnlen_long_trivial(segment, segmentSize)
len = segmentSize < MAX_CRITICAL_SIZE
? strnlen_long_critical(segment, segmentSize)
: strnlen_long(segment, segmentSize);
}
if (len > ArraysSupport.SOFT_MAX_ARRAY_LENGTH) {
Expand All @@ -152,9 +152,9 @@ private static int native_strlen_byte(MemorySegment segment, long start) {
return (int)len;
}

static long strnlen_int_trivial(MemorySegment segment, long size) {
static long strnlen_int_critical(MemorySegment segment, long size) {
try {
return Integer.toUnsignedLong((int)STRNLEN_TRIVIAL.invokeExact(segment, (int)size));
return Integer.toUnsignedLong((int)STRNLEN_CRITICAL.invokeExact(segment, (int)size));
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable e) {
Expand All @@ -172,9 +172,9 @@ static long strnlen_int(MemorySegment segment, long size) {
}
}

static long strnlen_long_trivial(MemorySegment segment, long size) {
static long strnlen_long_critical(MemorySegment segment, long size) {
try {
return (long)STRNLEN_TRIVIAL.invokeExact(segment, size);
return (long)STRNLEN_CRITICAL.invokeExact(segment, size);
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable e) {
Expand Down
Expand Up @@ -192,7 +192,7 @@ public int capturedStateMask() {
}

public boolean needsTransition() {
return !linkerOptions.isTrivial();
return !linkerOptions.isCritical();
}

public int numLeadingParams() {
Expand Down
Expand Up @@ -64,8 +64,8 @@ private static LinkerOptions forShared(BiConsumer<LinkerOptionImpl, FunctionDesc
}

LinkerOptions linkerOptions = new LinkerOptions(optionMap);
if (linkerOptions.hasCapturedCallState() && linkerOptions.isTrivial()) {
throw new IllegalArgumentException("Incompatible linker options: captureCallState, isTrivial");
if (linkerOptions.hasCapturedCallState() && linkerOptions.isCritical()) {
throw new IllegalArgumentException("Incompatible linker options: captureCallState, critical");
}
return linkerOptions;
}
Expand Down Expand Up @@ -101,9 +101,9 @@ public int firstVariadicArgIndex() {
return getOption(FirstVariadicArg.class).index();
}

public boolean isTrivial() {
IsTrivial it = getOption(IsTrivial.class);
return it != null;
public boolean isCritical() {
Critical c = getOption(Critical.class);
return c != null;
}

@Override
Expand All @@ -119,7 +119,7 @@ public int hashCode() {
}

public sealed interface LinkerOptionImpl extends Linker.Option
permits CaptureCallState, FirstVariadicArg, IsTrivial {
permits CaptureCallState, FirstVariadicArg, Critical {
default void validateForDowncall(FunctionDescriptor descriptor) {
throw new IllegalArgumentException("Not supported for downcall: " + this);
}
Expand All @@ -145,8 +145,8 @@ public void validateForDowncall(FunctionDescriptor descriptor) {
}
}

public record IsTrivial() implements LinkerOptionImpl {
public static IsTrivial INSTANCE = new IsTrivial();
public record Critical() implements LinkerOptionImpl {
public static Critical INSTANCE = new Critical();

@Override
public void validateForDowncall(FunctionDescriptor descriptor) {
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/java/foreign/TestIllegalLink.java
Expand Up @@ -102,7 +102,7 @@ public static Object[][] downcallOnlyOptions() {
return new Object[][]{
{ Linker.Option.firstVariadicArg(0) },
{ Linker.Option.captureCallState("errno") },
{ Linker.Option.isTrivial() },
{ Linker.Option.critical() },
};
}

Expand Down Expand Up @@ -194,8 +194,8 @@ public static Object[][] types() {
},
{
FunctionDescriptor.ofVoid(),
new Linker.Option[]{Linker.Option.isTrivial(), Linker.Option.captureCallState("errno")},
"Incompatible linker options: captureCallState, isTrivial"
new Linker.Option[]{Linker.Option.critical(), Linker.Option.captureCallState("errno")},
"Incompatible linker options: captureCallState, critical"
},
}));

Expand Down
Expand Up @@ -24,7 +24,7 @@
/*
* @test
* @library ../ /test/lib
* @run testng/othervm --enable-native-access=ALL-UNNAMED TestTrivial
* @run testng/othervm --enable-native-access=ALL-UNNAMED TestCritical
*/

import org.testng.annotations.Test;
Expand All @@ -41,21 +41,21 @@

import static org.testng.Assert.assertEquals;

public class TestTrivial extends NativeTestHelper {
public class TestCritical extends NativeTestHelper {

static {
System.loadLibrary("Trivial");
System.loadLibrary("Critical");
}

@Test
public void testEmpty() throws Throwable {
MethodHandle handle = downcallHandle("empty", FunctionDescriptor.ofVoid(), Linker.Option.isTrivial());
MethodHandle handle = downcallHandle("empty", FunctionDescriptor.ofVoid(), Linker.Option.critical());
handle.invokeExact();
}

@Test
public void testIdentity() throws Throwable {
MethodHandle handle = downcallHandle("identity", FunctionDescriptor.of(C_INT, C_INT), Linker.Option.isTrivial());
MethodHandle handle = downcallHandle("identity", FunctionDescriptor.of(C_INT, C_INT), Linker.Option.critical());
int result = (int) handle.invokeExact(42);
assertEquals(result, 42);
}
Expand All @@ -66,7 +66,7 @@ public void testWithReturnBuffer() throws Throwable {
C_LONG_LONG.withName("x"),
C_LONG_LONG.withName("y"));

MethodHandle handle = downcallHandle("with_return_buffer", FunctionDescriptor.of(bigLayout), Linker.Option.isTrivial());
MethodHandle handle = downcallHandle("with_return_buffer", FunctionDescriptor.of(bigLayout), Linker.Option.critical());
VarHandle vhX = bigLayout.varHandle(MemoryLayout.PathElement.groupElement("x"));
VarHandle vhY = bigLayout.varHandle(MemoryLayout.PathElement.groupElement("y"));
try (Arena arena = Arena.ofConfined()) {
Expand Down
Expand Up @@ -25,7 +25,7 @@
* @test
* @library ../ /test/lib
* @requires jdk.foreign.linker != "FALLBACK"
* @run testng/othervm --enable-native-access=ALL-UNNAMED TestTrivialUpcall
* @run testng/othervm --enable-native-access=ALL-UNNAMED TestCriticalUpcall
*/

import org.testng.annotations.Test;
Expand All @@ -38,7 +38,7 @@

import static org.testng.Assert.fail;

public class TestTrivialUpcall extends UpcallTestHelper {
public class TestCriticalUpcall extends UpcallTestHelper {

@Test
public void testUpcallFailure() throws IOException, InterruptedException {
Expand All @@ -48,9 +48,9 @@ public void testUpcallFailure() throws IOException, InterruptedException {

public static class Runner extends NativeTestHelper {
public static void main(String[] args) throws Throwable {
System.loadLibrary("Trivial");
System.loadLibrary("Critical");

MethodHandle mh = downcallHandle("do_upcall", FunctionDescriptor.ofVoid(C_POINTER), Linker.Option.isTrivial());
MethodHandle mh = downcallHandle("do_upcall", FunctionDescriptor.ofVoid(C_POINTER), Linker.Option.critical());
MemorySegment stub = upcallStub(Runner.class, "target", FunctionDescriptor.ofVoid());
mh.invokeExact(stub);
}
Expand Down
File renamed without changes.
Expand Up @@ -55,8 +55,8 @@ public void panama_blank() throws Throwable {
}

@Benchmark
public void panama_blank_trivial() throws Throwable {
func_trivial.invokeExact();
public void panama_blank_critical() throws Throwable {
func_critical.invokeExact();
}

@Benchmark
Expand All @@ -70,8 +70,8 @@ public int panama_identity() throws Throwable {
}

@Benchmark
public int panama_identity_trivial() throws Throwable {
return (int) identity_trivial.invokeExact(10);
public int panama_identity_critical() throws Throwable {
return (int) identity_critical.invokeExact(10);
}

@Benchmark
Expand Down
Expand Up @@ -34,14 +34,14 @@ public class CallOverheadHelper extends CLayouts {
static final Linker abi = Linker.nativeLinker();

static final MethodHandle func;
static final MethodHandle func_trivial;
static final MethodHandle func_critical;
static final MethodHandle func_v;
static final MethodHandle func_trivial_v;
static final MethodHandle func_critical_v;
static MemorySegment func_addr;
static final MethodHandle identity;
static final MethodHandle identity_trivial;
static final MethodHandle identity_critical;
static final MethodHandle identity_v;
static final MethodHandle identity_trivial_v;
static final MethodHandle identity_critical_v;
static MemorySegment identity_addr;
static final MethodHandle identity_struct;
static final MethodHandle identity_struct_v;
Expand Down Expand Up @@ -113,17 +113,17 @@ public class CallOverheadHelper extends CLayouts {
MethodType mt = MethodType.methodType(void.class);
FunctionDescriptor fd = FunctionDescriptor.ofVoid();
func_v = abi.downcallHandle(fd);
func_trivial_v = abi.downcallHandle(fd, Linker.Option.isTrivial());
func_critical_v = abi.downcallHandle(fd, Linker.Option.critical());
func = insertArguments(func_v, 0, func_addr);
func_trivial = insertArguments(func_trivial_v, 0, func_addr);
func_critical = insertArguments(func_critical_v, 0, func_addr);
}
{
identity_addr = loaderLibs.find("identity").orElseThrow();
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT);
identity_v = abi.downcallHandle(fd);
identity_trivial_v = abi.downcallHandle(fd, Linker.Option.isTrivial());
identity_critical_v = abi.downcallHandle(fd, Linker.Option.critical());
identity = insertArguments(identity_v, 0, identity_addr);
identity_trivial = insertArguments(identity_trivial_v, 0, identity_addr);
identity_critical = insertArguments(identity_critical_v, 0, identity_addr);
}
identity_struct_addr = loaderLibs.find("identity_struct").orElseThrow();
identity_struct_v = abi.downcallHandle(
Expand Down
Expand Up @@ -55,8 +55,8 @@ public void panama_blank() throws Throwable {
}

@Benchmark
public void panama_blank_trivial() throws Throwable {
func_trivial_v.invokeExact(func_addr);
public void panama_blank_critical() throws Throwable {
func_critical_v.invokeExact(func_addr);
}

@Benchmark
Expand Down Expand Up @@ -109,8 +109,8 @@ public int panama_identity() throws Throwable {
}

@Benchmark
public int panama_identity_trivial() throws Throwable {
return (int) identity_trivial_v.invokeExact(identity_addr, 10);
public int panama_identity_critical() throws Throwable {
return (int) identity_critical_v.invokeExact(identity_addr, 10);
}

@Benchmark
Expand Down

0 comments on commit ea82d41

Please sign in to comment.