OpenJDK / valhalla / valhalla
changeset 48429:c0169c05bafd mvt
More VVT tests
author | fparain |
---|---|
date | Mon, 18 Dec 2017 10:03:34 -0500 |
parents | d31d52d92b08 |
children | 46ac66b221b3 |
files | test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue1.java test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue2.java test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue3.java test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue4.java test/hotspot/jtreg/runtime/valhalla/valuetypes/ValueTypesTest.java |
diffstat | 5 files changed, 600 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue1.java Mon Dec 18 10:03:34 2017 -0500 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package runtime.valhalla.valuetypes; + +final class ContainerValue1 { + static TestValue1 staticValueField; + TestValue1 nonStaticValueField; + TestValue1[] valueArray; +} + +public __ByValue final class TestValue1 { + + static TestValue1 staticValue = getInstance(); + + final int i; + final String name; + + private TestValue1() { + i = (int)System.nanoTime(); + name = Integer.valueOf(i).toString(); + } + + __ValueFactory public static TestValue1 create(int i) { + TestValue1 v = __MakeDefault TestValue1(); + v.i = i; + v.name = Integer.valueOf(i).toString(); + return v; + } + + __ValueFactory public static TestValue1 create() { + TestValue1 v = __MakeDefault TestValue1(); + v.i = (int)System.nanoTime(); + v.name = Integer.valueOf(v.i).toString(); + return v; + } + + public static TestValue1 getInstance() { + return create(); + } + + public static TestValue1 getNonBufferedInstance() { + return staticValue; + } + + public boolean verify() { + if (name == null) return i == 0; + return Integer.valueOf(i).toString().compareTo(name) == 0; + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue2.java Mon Dec 18 10:03:34 2017 -0500 @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package runtime.valhalla.valuetypes; + +final class ContainerValue2 { + static TestValue2 staticValueField; + TestValue2 nonStaticValueField; + TestValue2[] valueArray; +} + +public __ByValue final class TestValue2 { + static TestValue2 staticValue = getInstance(); + + final long l; + final double d; + final String s; + + private TestValue2() { + l = System.nanoTime(); + s = Long.valueOf(l).toString(); + d = Double.parseDouble(s); + } + + __ValueFactory public static TestValue2 create(long l) { + TestValue2 v = __MakeDefault TestValue2(); + v.l = l; + v.s = Long.valueOf(l).toString(); + v.d = Double.parseDouble(v.s); + return v; + } + + __ValueFactory public static TestValue2 create() { + TestValue2 v = __MakeDefault TestValue2(); + v.l = System.nanoTime(); + v.s = Long.valueOf(v.l).toString(); + v.d = Double.parseDouble(v.s); + return v; + } + + public static TestValue2 getInstance() { + return create(); + } + + public static TestValue2 getNonBufferedInstance() { + return staticValue; + } + + public boolean verify() { + if (s == null) { + return d == 0 && l == 0; + } + return Long.valueOf(l).toString().compareTo(s) == 0 + && Double.parseDouble(s) == d; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue3.java Mon Dec 18 10:03:34 2017 -0500 @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package runtime.valhalla.valuetypes; + +final class ContainerValue3 { + static TestValue3 staticValueField; + TestValue3 nonStaticValueField; + TestValue3[] valueArray; +} + +public __ByValue final class TestValue3 { + + static TestValue3 staticValue = getInstance(); + + final byte b; + + private TestValue3() { + b = 123; + } + + __ValueFactory public static TestValue3 create(byte b) { + TestValue3 v = __MakeDefault TestValue3(); + v.b = b; + return v; + } + + __ValueFactory public static TestValue3 create() { + TestValue3 v = __MakeDefault TestValue3(); + v.b = 123; + return v; + } + + public static TestValue3 getInstance() { + return create(); + } + + public static TestValue3 getNonBufferedInstance() { + return staticValue; + } + + public boolean verify() { + return b == 0 || b == 123; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/runtime/valhalla/valuetypes/TestValue4.java Mon Dec 18 10:03:34 2017 -0500 @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package runtime.valhalla.valuetypes; + +import java.nio.ByteBuffer; + +final class ContainerValue4 { + static TestValue4 staticValueField; + TestValue4 nonStaticValueField; + TestValue4[] valueArray; +} + +public __ByValue final class TestValue4 { + + static TestValue4 staticValue = getInstance(); + + final byte b1; + final byte b2; + final byte b3; + final byte b4; + final short s1; + final short s2; + final int i; + final long l; + final String val; + + private TestValue4() { + i = (int)System.nanoTime(); + val = Integer.valueOf(i).toString(); + l = ((long)i) << Integer.SIZE | i; + s1 = (short)(i & ~Short.MIN_VALUE); + s2 = (short)(i >> Short.SIZE); + b1 = (byte)(i & ~Byte.MIN_VALUE); + b2 = (byte)((i >> Byte.SIZE) & ~Byte.MIN_VALUE); + b3 = (byte)((i >> (2 * Byte.SIZE)) & ~Byte.MIN_VALUE); + b4 = (byte)((i >> (3 * Byte.SIZE)) & ~Byte.MIN_VALUE); + } + + __ValueFactory public static TestValue4 create(int i) { + TestValue4 v = __MakeDefault TestValue4(); + v.i = i; + v.val = Integer.valueOf(i).toString(); + ByteBuffer bf = ByteBuffer.allocate(8); + bf.putInt(0, i); + bf.putInt(4, i); + v.l = bf.getLong(0); + v.s1 = bf.getShort(2); + v.s2 = bf.getShort(0); + v.b1 = bf.get(3); + v.b2 = bf.get(2); + v.b3 = bf.get(1); + v.b4 = bf.get(0); + return v; + } + + __ValueFactory public static TestValue4 create() { + return create((int)System.nanoTime()); + } + + public static TestValue4 getInstance() { + return create(); + } + + public static TestValue4 getNonBufferedInstance() { + return staticValue; + } + + public boolean verify() { + if (val == null) { + return i == 0 && l == 0 && b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 + && s1 == 0 && s2 == 0; + } + ByteBuffer bf = ByteBuffer.allocate(8); + bf.putInt(0, i); + bf.putInt(4, i); + long nl = bf.getLong(0); + bf.clear(); + bf.putShort(0, s2); + bf.putShort(2, s1); + int from_s = bf.getInt(0); + bf.clear(); + bf.put(0, b4); + bf.put(1, b3); + bf.put(2, b2); + bf.put(3, b1); + int from_b = bf.getInt(0); + return l == nl && Integer.valueOf(i).toString().compareTo(val) == 0 + && from_s == i && from_b == i; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/runtime/valhalla/valuetypes/ValueTypesTest.java Mon Dec 18 10:03:34 2017 -0500 @@ -0,0 +1,282 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package runtime.valhalla.valuetypes; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.lang.invoke.*; +import java.lang.ref.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.*; + +import static jdk.test.lib.Asserts.*; + +import jdk.experimental.bytecode.MacroCodeBuilder; +import jdk.experimental.bytecode.MacroCodeBuilder.CondKind; +import jdk.experimental.bytecode.TypeTag; +import jdk.test.lib.Utils; + +import jdk.experimental.value.MethodHandleBuilder; + +import javax.tools.*; + +/** + * @test ValueTypesTest + * @summary Test data movement with value types + * @modules java.base/jdk.experimental.bytecode + * java.base/jdk.experimental.value + * @library /test/lib + * @compile -XDenableValueTypes TestValue1.java TestValue2.java TestValue3.java TestValue4.java ValueTypesTest.java + * @run main/othervm -Xint -Xmx128m -XX:+EnableValhalla -XX:-ShowMessageBoxOnError + * -XX:+ExplicitGCInvokesConcurrent + * -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=false + * runtime.valhalla.valuetypes.ValueTypesTest + * @run main/othervm -Xmx128m -XX:+EnableValhalla -XX:-ShowMessageBoxOnError + * -XX:+ExplicitGCInvokesConcurrent + * -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions + * -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true + * runtime.valhalla.valuetypes.ValueTypesTest + */ +public class ValueTypesTest { + + public static void main(String[] args) { + Class<?> valueClass = runtime.valhalla.valuetypes.TestValue1.class; + Class<?> testClasses[] = { + runtime.valhalla.valuetypes.TestValue1.class, + runtime.valhalla.valuetypes.TestValue2.class, + runtime.valhalla.valuetypes.TestValue3.class, + runtime.valhalla.valuetypes.TestValue4.class + }; + Class<?> containerClasses[] = { + runtime.valhalla.valuetypes.ContainerValue1.class, + runtime.valhalla.valuetypes.ContainerValue2.class, + runtime.valhalla.valuetypes.ContainerValue3.class, + runtime.valhalla.valuetypes.ContainerValue4.class + }; + + for (int i = 0; i < testClasses.length; i++) { + try { + testExecutionStackToLocalVariable(testClasses[i]); + testExecutionStackToFields(testClasses[i], containerClasses[i]); + testExecutionStackToValueArray(testClasses[i], containerClasses[i]); + } catch (Throwable t) { + t.printStackTrace(); + throw new RuntimeException(t); + } + } + } + + static MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); + + static void testExecutionStackToLocalVariable(Class<?> valueClass) throws Throwable { + String sig = "()Q" + valueClass.getName() + ";"; + final String signature = sig.replace('.', '/'); + System.out.println(signature); + MethodHandle fromExecStackToLocalVar = MethodHandleBuilder.loadCode( + LOOKUP, + "execStackToLocalVar", + MethodType.methodType(boolean.class), + CODE -> { + CODE.invokestatic(System.class, "gc", "()V", false); + int n = -1; + while (n < 1024) { + n++; + CODE + .invokestatic(valueClass, "getInstance", signature, false) + .vstore(n); + n++; + CODE + .invokestatic(valueClass, "getNonBufferedInstance", signature, false) + .vstore(n); + } + CODE.invokestatic(System.class, "gc", "()V", false); + while (n > 0) { + CODE + .vload(n) + .invokevirtual(valueClass, "verify", "()Z", false) + .iconst_1() + .ifcmp(TypeTag.I, CondKind.NE, "end"); + n--; + } + CODE + .iconst_1() + .return_(TypeTag.Z) + .label("end") + .iconst_0() + .return_(TypeTag.Z); + }); + boolean result = (boolean) fromExecStackToLocalVar.invokeExact(); + System.out.println(result); + assertTrue(result, "Invariant"); + } + + static void testExecutionStackToFields(Class<?> valueClass, Class<?> containerClass) throws Throwable { + final int ITERATIONS = 512; + String sig = "()Q" + valueClass.getName() + ";"; + final String methodSignature = sig.replace('.', '/'); + final String fieldSignature = "Q" + valueClass.getName().replace('.', '/') + ";"; + System.out.println(methodSignature); + MethodHandle fromExecStackToFields = MethodHandleBuilder.loadCode( + LOOKUP, + "execStackToFields", + MethodType.methodType(boolean.class), + CODE -> { + CODE + .invokestatic(System.class, "gc", "()V", false) + .new_(containerClass) + .dup() + .invoke(MacroCodeBuilder.InvocationKind.INVOKESPECIAL, containerClass, "<init>", "()V", false) + .astore_1() + .iconst_m1() + .istore_2() + .label("loop") + .iload_2() + .ldc(ITERATIONS) + .ifcmp(TypeTag.I, CondKind.EQ, "end") + .aload_1() + .invokestatic(valueClass, "getInstance", methodSignature, false) + .putfield(containerClass, "nonStaticValueField", fieldSignature) + .invokestatic(System.class, "gc", "()V", false) + .aload_1() + .getfield(containerClass, "nonStaticValueField", fieldSignature) + .invokevirtual(valueClass, "verify", "()Z", false) + .iconst_1() + .ifcmp(TypeTag.I, CondKind.NE, "failed") + .aload_1() + .invokestatic(valueClass, "getNonBufferedInstance", methodSignature, false) + .putfield(containerClass, "nonStaticValueField", fieldSignature) + .invokestatic(System.class, "gc", "()V", false) + .aload_1() + .getfield(containerClass, "nonStaticValueField", fieldSignature) + .invokevirtual(valueClass, "verify", "()Z", false) + .iconst_1() + .ifcmp(TypeTag.I, CondKind.NE, "failed") + .invokestatic(valueClass, "getInstance", methodSignature, false) + .putstatic(containerClass, "staticValueField", fieldSignature) + .invokestatic(System.class, "gc", "()V", false) + .getstatic(containerClass, "staticValueField", fieldSignature) + .invokevirtual(valueClass, "verify", "()Z", false) + .iconst_1() + .ifcmp(TypeTag.I, CondKind.NE, "failed") + .invokestatic(valueClass, "getNonBufferedInstance", methodSignature, false) + .putstatic(containerClass, "staticValueField", fieldSignature) + .invokestatic(System.class, "gc", "()V", false) + .getstatic(containerClass, "staticValueField", fieldSignature) + .invokevirtual(valueClass, "verify", "()Z", false) + .iconst_1() + .ifcmp(TypeTag.I, CondKind.NE, "failed") + .iinc(2, 1) + .goto_("loop") + .label("end") + .iconst_1() + .return_(TypeTag.Z) + .label("failed") + .iconst_0() + .return_(TypeTag.Z); + }); + boolean result = (boolean) fromExecStackToFields.invokeExact(); + System.out.println(result); + assertTrue(result, "Invariant"); + } + + static void testExecutionStackToValueArray(Class<?> valueClass, Class<?> containerClass) throws Throwable { + final int ITERATIONS = 100; + String sig = "()Q" + valueClass.getName() + ";"; + final String signature = sig.replace('.', '/'); + final String arraySignature = "[Q" + valueClass.getName().replace('.', '/') + ";"; + System.out.println(arraySignature); + MethodHandle fromExecStackToValueArray = MethodHandleBuilder.loadCode( + LOOKUP, + "execStackToValueArray", + MethodType.methodType(boolean.class), + CODE -> { + CODE + .invokestatic(System.class, "gc", "()V", false) + .new_(containerClass) + .dup() + .invoke(MacroCodeBuilder.InvocationKind.INVOKESPECIAL, containerClass, "<init>", "()V", false) + .astore_1() + .ldc(ITERATIONS * 3) + .anewarray(valueClass) + .astore_2() + .aload_2() + .aload_1() + .swap() + .putfield(containerClass, "valueArray", arraySignature) + .iconst_0() + .istore_3() + .label("loop1") + .iload_3() + .ldc(ITERATIONS) + .ifcmp(TypeTag.I, CondKind.GE, "end1") + .aload_2() + .iload_3() + .invokestatic(valueClass, "getInstance", signature, false) + .vastore() + .iinc(3, 1) + .aload_2() + .iload_3() + .invokestatic(valueClass, "getNonBufferedInstance", signature, false) + .vastore() + .iinc(3, 1) + .aload_2() + .iload_3() + .vdefault(valueClass) + .vastore() + .iinc(3, 1) + .goto_("loop1") + .label("end1") + .invokestatic(System.class, "gc", "()V", false) + .iconst_0() + .istore_3() + .label("loop2") + .iload_3() + .ldc(ITERATIONS * 3) + .ifcmp(TypeTag.I, CondKind.GE, "end2") + .aload_2() + .iload_3() + .vaload() + .invokevirtual(valueClass, "verify", "()Z", false) + .iconst_1() + .ifcmp(TypeTag.I, CondKind.NE, "failed") + .iinc(3, 1) + .goto_("loop2") + .label("end2") + .iconst_1() + .return_(TypeTag.Z) + .label("failed") + .iconst_0() + .return_(TypeTag.Z); + }); + boolean result = (boolean) fromExecStackToValueArray.invokeExact(); + System.out.println(result); + assertTrue(result, "Invariant"); + } + + +}