From 6603026f1bfec02e3486c52270a09a355a1bf914 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 19 Aug 2019 02:58:22 +0200 Subject: Bug 1363: Java 11: Resolve Buffers.Cleaner implementation As of Java9, sun.misc.Cleaner has moved to jdk.internal.ref.Cleaner. However, access has been made (under the table) via sun.misc.Unsafe, which we are using for now as we cannot set the jdk.internal.ref.Cleaner method accessible. In this regard, we had to change our Cleaner.clean(..) method using a ByteBuffer instead of a Buffer object paramter. All tests have passed, no more illegal access case running on Java11 has been exposed. --- src/java/com/jogamp/common/nio/Buffers.java | 54 ++++- src/junit/com/jogamp/common/nio/BuffersTest.java | 163 ------------- .../jogamp/common/nio/CachedBufferFactoryTest.java | 252 --------------------- src/junit/com/jogamp/common/nio/TestBuffers.java | 169 ++++++++++++++ .../common/nio/TestByteBufferCopyStream.java | 11 + .../jogamp/common/nio/TestCachedBufferFactory.java | 252 +++++++++++++++++++++ 6 files changed, 478 insertions(+), 423 deletions(-) delete mode 100644 src/junit/com/jogamp/common/nio/BuffersTest.java delete mode 100644 src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java create mode 100644 src/junit/com/jogamp/common/nio/TestBuffers.java create mode 100644 src/junit/com/jogamp/common/nio/TestCachedBufferFactory.java (limited to 'src') diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index fb23627..5125ee8 100644 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -39,6 +39,7 @@ */ package com.jogamp.common.nio; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.nio.Buffer; import java.nio.ByteBuffer; @@ -56,6 +57,7 @@ import com.jogamp.common.util.ReflectionUtil; import com.jogamp.common.util.ValueConv; import jogamp.common.Debug; +import jogamp.common.os.PlatformPropsImpl; /** * Utility methods allowing easy {@link java.nio.Buffer} manipulations. @@ -1165,22 +1167,42 @@ public class Buffers { * Access to NIO {@link sun.misc.Cleaner}, allowing caller to deterministically clean a given {@link sun.nio.ch.DirectBuffer}. */ public static class Cleaner { + private static final Object theUnsafe; private static final Method mbbCleaner; private static final Method cClean; private static final boolean hasCleaner; /** OK to be lazy on thread synchronization, just for early out **/ private static volatile boolean cleanerError; static { + final Object[] _theUnsafe = { null }; final Method[] _mbbCleaner = { null }; final Method[] _cClean = { null }; if( AccessController.doPrivileged(new PrivilegedAction() { @Override public Boolean run() { try { - _mbbCleaner[0] = ReflectionUtil.getMethod("sun.nio.ch.DirectBuffer", "cleaner", null, Buffers.class.getClassLoader()); - _mbbCleaner[0].setAccessible(true); - _cClean[0] = Class.forName("sun.misc.Cleaner").getMethod("clean"); - _cClean[0].setAccessible(true); + if(PlatformPropsImpl.JAVA_9) { + // Using: sun.misc.Unsafe { public void invokeCleaner(java.nio.ByteBuffer directBuffer); } + _mbbCleaner[0] = null; + final Class unsafeClass = Class.forName("sun.misc.Unsafe"); + { + final Field f = unsafeClass.getDeclaredField("theUnsafe"); + f.setAccessible(true); + _theUnsafe[0] = f.get(null); + } + _cClean[0] = unsafeClass.getMethod("invokeCleaner", java.nio.ByteBuffer.class); + _cClean[0].setAccessible(true); + } else { + _mbbCleaner[0] = ReflectionUtil.getMethod("sun.nio.ch.DirectBuffer", "cleaner", null, Buffers.class.getClassLoader()); + _mbbCleaner[0].setAccessible(true); + final Class cleanerType = _mbbCleaner[0].getReturnType(); + // Java >= 9: jdk.internal.ref.Cleaner (NOT accessible!) + // "Unable to make public void jdk.internal.ref.Cleaner.clean() accessible: + // module java.base does not "exports jdk.internal.ref" to unnamed module" + // Java <= 8: sun.misc.Cleaner OK + _cClean[0] = cleanerType.getMethod("clean"); + _cClean[0].setAccessible(true); + } return Boolean.TRUE; } catch(final Throwable t) { if( DEBUG ) { @@ -1189,27 +1211,43 @@ public class Buffers { } return Boolean.FALSE; } } } ).booleanValue() ) { + theUnsafe = _theUnsafe[0]; mbbCleaner = _mbbCleaner[0]; cClean = _cClean[0]; - hasCleaner = null != mbbCleaner && null != cClean; + hasCleaner = PlatformPropsImpl.JAVA_9 ? null != theUnsafe && null != cClean : null != mbbCleaner && null != cClean; } else { + theUnsafe = null; mbbCleaner = null; cClean = null; hasCleaner = false; } cleanerError = !hasCleaner; + if( DEBUG ) { + System.err.print("Buffers.Cleaner.init: hasCleaner: "+hasCleaner+", cleanerError "+cleanerError); + if( null != mbbCleaner ) { + System.err.print(", using Cleaner class: "+mbbCleaner.getReturnType().getName()); + } + if( null != theUnsafe ) { + System.err.print(", using sun.misc.Unsafe::invokeCleaner(..);"); + } + System.err.println(); + } } /** * If {@code b} is an direct NIO buffer, i.e {@link sun.nio.ch.DirectBuffer}, * calls it's {@link sun.misc.Cleaner} instance {@code clean()} method. * @return {@code true} if successful, otherwise {@code false}. */ - public static boolean clean(final Buffer b) { - if( !hasCleaner || cleanerError || !b.isDirect() ) { + public static boolean clean(final ByteBuffer bb) { + if( !hasCleaner || cleanerError || !bb.isDirect() ) { return false; } try { - cClean.invoke(mbbCleaner.invoke(b)); + if( PlatformPropsImpl.JAVA_9 ) { + cClean.invoke(theUnsafe, bb); + } else { + cClean.invoke(mbbCleaner.invoke(bb)); + } return true; } catch(final Throwable t) { cleanerError = true; diff --git a/src/junit/com/jogamp/common/nio/BuffersTest.java b/src/junit/com/jogamp/common/nio/BuffersTest.java deleted file mode 100644 index c267100..0000000 --- a/src/junit/com/jogamp/common/nio/BuffersTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Copyright 2010 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ - -/* - * Created on Sunday, July 04 2010 20:00 - */ -package com.jogamp.common.nio; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.DoubleBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.LongBuffer; -import java.nio.ShortBuffer; - -import org.junit.Test; - -import com.jogamp.junit.util.SingletonJunitCase; - -import static org.junit.Assert.*; - -/** - * @author Michael Bien - */ -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class BuffersTest extends SingletonJunitCase { - - @Test - public void test01PositionLimitCapacityAfterArrayAllocation() { - final byte[] byteData = { 1, 2, 3, 4, 5, 6, 7, 8 }; - final ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(byteData); - assertEquals(0, byteBuffer.position()); - assertEquals(8, byteBuffer.limit()); - assertEquals(8, byteBuffer.capacity()); - assertEquals(8, byteBuffer.remaining()); - assertEquals(5, byteBuffer.get(4)); - - final double[] doubleData = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; - final DoubleBuffer doubleBuffer = Buffers.newDirectDoubleBuffer(doubleData); - assertEquals(0, doubleBuffer.position()); - assertEquals(8, doubleBuffer.limit()); - assertEquals(8, doubleBuffer.capacity()); - assertEquals(8, doubleBuffer.remaining()); - assertEquals(5.0, doubleBuffer.get(4), 0.1); - - final float[] floatData = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; - final FloatBuffer floatBuffer = Buffers.newDirectFloatBuffer(floatData); - assertEquals(0, floatBuffer.position()); - assertEquals(8, floatBuffer.limit()); - assertEquals(8, floatBuffer.capacity()); - assertEquals(8, floatBuffer.remaining()); - assertEquals(5.0f, floatBuffer.get(4), 0.1f); - - final int[] intData = { 1, 2, 3, 4, 5, 6, 7, 8 }; - final IntBuffer intBuffer = Buffers.newDirectIntBuffer(intData); - assertEquals(0, intBuffer.position()); - assertEquals(8, intBuffer.limit()); - assertEquals(8, intBuffer.capacity()); - assertEquals(8, intBuffer.remaining()); - assertEquals(5, intBuffer.get(4)); - - final long[] longData = { 1, 2, 3, 4, 5, 6, 7, 8 }; - final LongBuffer longBuffer = Buffers.newDirectLongBuffer(longData); - assertEquals(0, longBuffer.position()); - assertEquals(8, longBuffer.limit()); - assertEquals(8, longBuffer.capacity()); - assertEquals(8, longBuffer.remaining()); - assertEquals(5, longBuffer.get(4)); - - final short[] shortData = { 1, 2, 3, 4, 5, 6, 7, 8 }; - final ShortBuffer shortBuffer = Buffers.newDirectShortBuffer(shortData); - assertEquals(0, shortBuffer.position()); - assertEquals(8, shortBuffer.limit()); - assertEquals(8, shortBuffer.capacity()); - assertEquals(8, shortBuffer.remaining()); - assertEquals(5, shortBuffer.get(4)); - - final char[] charData = { 1, 2, 3, 4, 5, 6, 7, 8 }; - final CharBuffer charBuffer = Buffers.newDirectCharBuffer(charData); - assertEquals(0, charBuffer.position()); - assertEquals(8, charBuffer.limit()); - assertEquals(8, charBuffer.capacity()); - assertEquals(8, charBuffer.remaining()); - assertEquals(5, charBuffer.get(4)); - } - - @Test - public void test10Slice() { - - final IntBuffer buffer = Buffers.newDirectIntBuffer(6); - buffer.put(new int[]{1,2,3,4,5,6}).rewind(); - - final IntBuffer threefour = Buffers.slice(buffer, 2, 2); - - assertEquals(3, threefour.get(0)); - assertEquals(4, threefour.get(1)); - assertEquals(2, threefour.capacity()); - - assertEquals(0, buffer.position()); - assertEquals(6, buffer.limit()); - - final IntBuffer fourfivesix = Buffers.slice(buffer, 3, 3); - - assertEquals(4, fourfivesix.get(0)); - assertEquals(5, fourfivesix.get(1)); - assertEquals(6, fourfivesix.get(2)); - assertEquals(3, fourfivesix.capacity()); - - assertEquals(0, buffer.position()); - assertEquals(6, buffer.limit()); - - final IntBuffer onetwothree = Buffers.slice(buffer, 0, 3); - - assertEquals(1, onetwothree.get(0)); - assertEquals(2, onetwothree.get(1)); - assertEquals(3, onetwothree.get(2)); - assertEquals(3, onetwothree.capacity()); - - assertEquals(0, buffer.position()); - assertEquals(6, buffer.limit()); - - // is it really sliced? - buffer.put(2, 42); - - assertEquals(42, buffer.get(2)); - assertEquals(42, onetwothree.get(2)); - } - - public static void main(final String args[]) throws IOException { - final String tstname = BuffersTest.class.getName(); - org.junit.runner.JUnitCore.main(tstname); - } -} diff --git a/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java b/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java deleted file mode 100644 index 6b9409f..0000000 --- a/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright 2011 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ -package com.jogamp.common.nio; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.IntBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.jogamp.junit.util.SingletonJunitCase; - -import static java.lang.System.*; -import static org.junit.Assert.*; - -/** - * - * @author Michael Bien - */ -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class CachedBufferFactoryTest extends SingletonJunitCase { - - private final int BUFFERCOUNT = 120; - - private static int[] sizes; - private static int[] values; - private static IntBuffer[] buffers; - - @Before - public void setup() { - - sizes = new int[BUFFERCOUNT]; - values = new int[sizes.length]; - buffers = new IntBuffer[sizes.length]; - - final Random rnd = new Random(7); - - // setup - for (int i = 0; i < sizes.length; i++) { - sizes[i] = rnd.nextInt(80)+1; - values[i] = rnd.nextInt(); - } - - } - - @After - public void teardown() { - sizes = null; - values = null; - buffers = null; - } - - @Test - public void dynamicTest() { - - final CachedBufferFactory factory = CachedBufferFactory.create(64); - - // create - for (int i = 0; i < sizes.length; i++) { - buffers[i] = factory.newDirectIntBuffer(sizes[i]); - assertEquals(ByteOrder.nativeOrder(), buffers[i].order()); - fill(buffers[i], values[i]); - } - - // check - checkBuffers(buffers, sizes, values); - - } - - @Test - public void dynamicConcurrentTest() throws InterruptedException, ExecutionException { - - final CachedBufferFactory factory = CachedBufferFactory.createSynchronized(24); - - final List> callables = new ArrayList>(); - - final CountDownLatch latch = new CountDownLatch(10); - - // create - for (int i = 0; i < sizes.length; i++) { - final int n = i; - final Callable c = new Callable() { - public Object call() throws Exception { - latch.countDown(); - latch.await(); - buffers[n] = factory.newDirectIntBuffer(sizes[n]); - fill(buffers[n], values[n]); - return null; - } - }; - callables.add(c); - } - - final ExecutorService dathVader = Executors.newFixedThreadPool(10); - dathVader.invokeAll(callables); - - dathVader.shutdown(); - - // check - checkBuffers(buffers, sizes, values); - - } - - private void checkBuffers(final IntBuffer[] buffers, final int[] sizes, final int[] values) { - for (int i = 0; i < buffers.length; i++) { - final IntBuffer buffer = buffers[i]; - assertEquals(sizes[i], buffer.capacity()); - assertEquals(0, buffer.position()); - assertTrue(equals(buffer, values[i])); - } - } - - @Test - public void staticTest() { - - final CachedBufferFactory factory = CachedBufferFactory.create(10, true); - - for (int i = 0; i < 5; i++) { - factory.newDirectByteBuffer(2); - } - - try{ - factory.newDirectByteBuffer(1); - fail(); - }catch (final RuntimeException ex) { - // expected - } - - } - - private void fill(final IntBuffer buffer, final int value) { - while(buffer.remaining() != 0) - buffer.put(value); - - buffer.rewind(); - } - - private boolean equals(final IntBuffer buffer, final int value) { - while(buffer.remaining() != 0) { - if(value != buffer.get()) - return false; - } - - buffer.rewind(); - return true; - } - - - /* load testing */ - - private int size = 4; - private final int iterations = 10000; - -// @Test - public Object loadTest() { - final CachedBufferFactory factory = CachedBufferFactory.create(); - final ByteBuffer[] buffer = new ByteBuffer[iterations]; - for (int i = 0; i < buffer.length; i++) { - buffer[i] = factory.newDirectByteBuffer(size); - } - return buffer; - } - -// @Test - public Object referenceTest() { - final ByteBuffer[] buffer = new ByteBuffer[iterations]; - for (int i = 0; i < buffer.length; i++) { - buffer[i] = Buffers.newDirectByteBuffer(size); - } - return buffer; - } - - - public static void main(final String[] args) { - - CachedBufferFactoryTest test = new CachedBufferFactoryTest(); - - out.print("warmup..."); - Object obj = null; - for (int i = 0; i < 100; i++) { - obj = test.referenceTest(); - obj = test.loadTest(); - gc(); - } - out.println("done"); - - test = new CachedBufferFactoryTest(); - gc(); - - for (int i = 0; i < 10; i++) { - - out.println("allocation size: "+test.size); - - long time = System.currentTimeMillis(); - obj = test.referenceTest(); - if(obj == null) return; // ref lock - - out.println("reference: "+ (System.currentTimeMillis()-time)); - - gc(); - - time = currentTimeMillis(); - obj = test.loadTest(); - if(obj == null) return; // ref lock - - out.println("factory: "+ (System.currentTimeMillis()-time)); - - gc(); - - test.size*=2; - } - - } - -} diff --git a/src/junit/com/jogamp/common/nio/TestBuffers.java b/src/junit/com/jogamp/common/nio/TestBuffers.java new file mode 100644 index 0000000..89b5671 --- /dev/null +++ b/src/junit/com/jogamp/common/nio/TestBuffers.java @@ -0,0 +1,169 @@ +/** + * Copyright 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +/* + * Created on Sunday, July 04 2010 20:00 + */ +package com.jogamp.common.nio; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.DoubleBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; +import java.nio.ShortBuffer; + +import org.junit.Test; + +import com.jogamp.junit.util.SingletonJunitCase; + +import static org.junit.Assert.*; + +/** + * @author Michael Bien + */ +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestBuffers extends SingletonJunitCase { + + @Test + public void test01PositionLimitCapacityAfterArrayAllocation() { + final byte[] byteData = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(byteData); + assertEquals(0, byteBuffer.position()); + assertEquals(8, byteBuffer.limit()); + assertEquals(8, byteBuffer.capacity()); + assertEquals(8, byteBuffer.remaining()); + assertEquals(5, byteBuffer.get(4)); + + final double[] doubleData = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; + final DoubleBuffer doubleBuffer = Buffers.newDirectDoubleBuffer(doubleData); + assertEquals(0, doubleBuffer.position()); + assertEquals(8, doubleBuffer.limit()); + assertEquals(8, doubleBuffer.capacity()); + assertEquals(8, doubleBuffer.remaining()); + assertEquals(5.0, doubleBuffer.get(4), 0.1); + + final float[] floatData = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; + final FloatBuffer floatBuffer = Buffers.newDirectFloatBuffer(floatData); + assertEquals(0, floatBuffer.position()); + assertEquals(8, floatBuffer.limit()); + assertEquals(8, floatBuffer.capacity()); + assertEquals(8, floatBuffer.remaining()); + assertEquals(5.0f, floatBuffer.get(4), 0.1f); + + final int[] intData = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final IntBuffer intBuffer = Buffers.newDirectIntBuffer(intData); + assertEquals(0, intBuffer.position()); + assertEquals(8, intBuffer.limit()); + assertEquals(8, intBuffer.capacity()); + assertEquals(8, intBuffer.remaining()); + assertEquals(5, intBuffer.get(4)); + + final long[] longData = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final LongBuffer longBuffer = Buffers.newDirectLongBuffer(longData); + assertEquals(0, longBuffer.position()); + assertEquals(8, longBuffer.limit()); + assertEquals(8, longBuffer.capacity()); + assertEquals(8, longBuffer.remaining()); + assertEquals(5, longBuffer.get(4)); + + final short[] shortData = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final ShortBuffer shortBuffer = Buffers.newDirectShortBuffer(shortData); + assertEquals(0, shortBuffer.position()); + assertEquals(8, shortBuffer.limit()); + assertEquals(8, shortBuffer.capacity()); + assertEquals(8, shortBuffer.remaining()); + assertEquals(5, shortBuffer.get(4)); + + final char[] charData = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final CharBuffer charBuffer = Buffers.newDirectCharBuffer(charData); + assertEquals(0, charBuffer.position()); + assertEquals(8, charBuffer.limit()); + assertEquals(8, charBuffer.capacity()); + assertEquals(8, charBuffer.remaining()); + assertEquals(5, charBuffer.get(4)); + } + + @Test + public void test10Slice() { + + final IntBuffer buffer = Buffers.newDirectIntBuffer(6); + buffer.put(new int[]{1,2,3,4,5,6}).rewind(); + + final IntBuffer threefour = Buffers.slice(buffer, 2, 2); + + assertEquals(3, threefour.get(0)); + assertEquals(4, threefour.get(1)); + assertEquals(2, threefour.capacity()); + + assertEquals(0, buffer.position()); + assertEquals(6, buffer.limit()); + + final IntBuffer fourfivesix = Buffers.slice(buffer, 3, 3); + + assertEquals(4, fourfivesix.get(0)); + assertEquals(5, fourfivesix.get(1)); + assertEquals(6, fourfivesix.get(2)); + assertEquals(3, fourfivesix.capacity()); + + assertEquals(0, buffer.position()); + assertEquals(6, buffer.limit()); + + final IntBuffer onetwothree = Buffers.slice(buffer, 0, 3); + + assertEquals(1, onetwothree.get(0)); + assertEquals(2, onetwothree.get(1)); + assertEquals(3, onetwothree.get(2)); + assertEquals(3, onetwothree.capacity()); + + assertEquals(0, buffer.position()); + assertEquals(6, buffer.limit()); + + // is it really sliced? + buffer.put(2, 42); + + assertEquals(42, buffer.get(2)); + assertEquals(42, onetwothree.get(2)); + } + + @Test + public void test20Cleaner() { + final ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(1024); + Buffers.Cleaner.clean(byteBuffer); + } + + public static void main(final String args[]) throws IOException { + final String tstname = TestBuffers.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java b/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java index 9524e91..aef0d3e 100644 --- a/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java +++ b/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java @@ -52,6 +52,8 @@ public class TestByteBufferCopyStream extends SingletonJunitCase { final MappedByteBufferInputStream.CacheMode srcCacheMode, final int srcSliceShift, final String dstFileName, final MappedByteBufferInputStream.CacheMode dstCacheMode, final int dstSliceShift ) throws IOException { + System.err.println("Test: source[CacheMode "+srcCacheMode+", SliceShift "+srcSliceShift+"]"); + System.err.println(" destin[CacheMode "+dstCacheMode+", SliceShift "+dstSliceShift+"]"); final Runtime runtime = Runtime.getRuntime(); final long[] usedMem0 = { 0 }; final long[] freeMem0 = { 0 }; @@ -60,6 +62,7 @@ public class TestByteBufferCopyStream extends SingletonJunitCase { final String prefix = "test "+String.format(TestByteBufferInputStream.PrintPrecision+" MiB", size/TestByteBufferInputStream.MIB); TestByteBufferInputStream.dumpMem(prefix+" before", runtime, -1, -1, usedMem0, freeMem0 ); + final long t0 = Platform.currentTimeMillis(); final File srcFile = new File(srcFileName); srcFile.delete(); srcFile.createNewFile(); @@ -72,6 +75,7 @@ public class TestByteBufferCopyStream extends SingletonJunitCase { _input.close(); input = new RandomAccessFile(srcFile, "r"); } + final long t1 = Platform.currentTimeMillis(); final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(input.getChannel(), FileChannel.MapMode.READ_ONLY, srcCacheMode, @@ -131,12 +135,19 @@ public class TestByteBufferCopyStream extends SingletonJunitCase { output.close(); srcFile.delete(); dstFile.delete(); + final long t5 = Platform.currentTimeMillis(); TestByteBufferInputStream.dumpMem(prefix+" after ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 ); System.gc(); + final long t6 = Platform.currentTimeMillis(); try { Thread.sleep(500); } catch (final InterruptedException e) { } TestByteBufferInputStream.dumpMem(prefix+" gc'ed ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 ); + System.err.println("Performance Stats: "); + System.err.printf("- File-Create %6d ms\n", t1-t0); + System.err.printf("- File-Copy %6d ms\n", t5-t1); + System.err.printf("- GC %6d ms\n", t6-t5); + System.err.printf("- Total %6d ms\n", t6-t0); } if( null != ioe || null != oome ) { if( null != oome ) { diff --git a/src/junit/com/jogamp/common/nio/TestCachedBufferFactory.java b/src/junit/com/jogamp/common/nio/TestCachedBufferFactory.java new file mode 100644 index 0000000..6ed1b91 --- /dev/null +++ b/src/junit/com/jogamp/common/nio/TestCachedBufferFactory.java @@ -0,0 +1,252 @@ +/* + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.common.nio; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.jogamp.junit.util.SingletonJunitCase; + +import static java.lang.System.*; +import static org.junit.Assert.*; + +/** + * + * @author Michael Bien + */ +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestCachedBufferFactory extends SingletonJunitCase { + + private final int BUFFERCOUNT = 120; + + private static int[] sizes; + private static int[] values; + private static IntBuffer[] buffers; + + @Before + public void setup() { + + sizes = new int[BUFFERCOUNT]; + values = new int[sizes.length]; + buffers = new IntBuffer[sizes.length]; + + final Random rnd = new Random(7); + + // setup + for (int i = 0; i < sizes.length; i++) { + sizes[i] = rnd.nextInt(80)+1; + values[i] = rnd.nextInt(); + } + + } + + @After + public void teardown() { + sizes = null; + values = null; + buffers = null; + } + + @Test + public void dynamicTest() { + + final CachedBufferFactory factory = CachedBufferFactory.create(64); + + // create + for (int i = 0; i < sizes.length; i++) { + buffers[i] = factory.newDirectIntBuffer(sizes[i]); + assertEquals(ByteOrder.nativeOrder(), buffers[i].order()); + fill(buffers[i], values[i]); + } + + // check + checkBuffers(buffers, sizes, values); + + } + + @Test + public void dynamicConcurrentTest() throws InterruptedException, ExecutionException { + + final CachedBufferFactory factory = CachedBufferFactory.createSynchronized(24); + + final List> callables = new ArrayList>(); + + final CountDownLatch latch = new CountDownLatch(10); + + // create + for (int i = 0; i < sizes.length; i++) { + final int n = i; + final Callable c = new Callable() { + public Object call() throws Exception { + latch.countDown(); + latch.await(); + buffers[n] = factory.newDirectIntBuffer(sizes[n]); + fill(buffers[n], values[n]); + return null; + } + }; + callables.add(c); + } + + final ExecutorService dathVader = Executors.newFixedThreadPool(10); + dathVader.invokeAll(callables); + + dathVader.shutdown(); + + // check + checkBuffers(buffers, sizes, values); + + } + + private void checkBuffers(final IntBuffer[] buffers, final int[] sizes, final int[] values) { + for (int i = 0; i < buffers.length; i++) { + final IntBuffer buffer = buffers[i]; + assertEquals(sizes[i], buffer.capacity()); + assertEquals(0, buffer.position()); + assertTrue(equals(buffer, values[i])); + } + } + + @Test + public void staticTest() { + + final CachedBufferFactory factory = CachedBufferFactory.create(10, true); + + for (int i = 0; i < 5; i++) { + factory.newDirectByteBuffer(2); + } + + try{ + factory.newDirectByteBuffer(1); + fail(); + }catch (final RuntimeException ex) { + // expected + } + + } + + private void fill(final IntBuffer buffer, final int value) { + while(buffer.remaining() != 0) + buffer.put(value); + + buffer.rewind(); + } + + private boolean equals(final IntBuffer buffer, final int value) { + while(buffer.remaining() != 0) { + if(value != buffer.get()) + return false; + } + + buffer.rewind(); + return true; + } + + + /* load testing */ + + private int size = 4; + private final int iterations = 10000; + +// @Test + public Object loadTest() { + final CachedBufferFactory factory = CachedBufferFactory.create(); + final ByteBuffer[] buffer = new ByteBuffer[iterations]; + for (int i = 0; i < buffer.length; i++) { + buffer[i] = factory.newDirectByteBuffer(size); + } + return buffer; + } + +// @Test + public Object referenceTest() { + final ByteBuffer[] buffer = new ByteBuffer[iterations]; + for (int i = 0; i < buffer.length; i++) { + buffer[i] = Buffers.newDirectByteBuffer(size); + } + return buffer; + } + + + public static void main(final String[] args) { + + TestCachedBufferFactory test = new TestCachedBufferFactory(); + + out.print("warmup..."); + Object obj = null; + for (int i = 0; i < 100; i++) { + obj = test.referenceTest(); + obj = test.loadTest(); + gc(); + } + out.println("done"); + + test = new TestCachedBufferFactory(); + gc(); + + for (int i = 0; i < 10; i++) { + + out.println("allocation size: "+test.size); + + long time = System.currentTimeMillis(); + obj = test.referenceTest(); + if(obj == null) return; // ref lock + + out.println("reference: "+ (System.currentTimeMillis()-time)); + + gc(); + + time = currentTimeMillis(); + obj = test.loadTest(); + if(obj == null) return; // ref lock + + out.println("factory: "+ (System.currentTimeMillis()-time)); + + gc(); + + test.size*=2; + } + + } + +} -- cgit v1.2.3