You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2021/05/11 05:25:19 UTC
[datasketches-memory] 03/04: interim commit, not done.
This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch noSplitPackage
in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git
commit 5a23ec61beef8412f466a7a7f8993be9b146316f
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Mon May 10 22:05:01 2021 -0700
interim commit, not done.
---
.../memory/BaseWritableBufferImpl.java | 2 +-
.../memory/BaseWritableMemoryImpl.java | 4 +-
.../apache/datasketches/memory/CompareAndCopy.java | 13 +-
.../memory/DirectWritableMemoryImpl.java | 7 -
.../java/org/apache/datasketches/memory/Util.java | 12 ++
.../memory/test/AllocateDirectMemoryTest.java | 71 ++++---
.../test/AllocateDirectWritableMapMemoryTest.java | 58 +++---
.../datasketches/memory/test/BaseStateTest.java | 21 +-
.../datasketches/memory/test/CopyMemoryTest.java | 2 +-
.../datasketches/memory/test/LeafImplTest.java | 115 +++++++----
.../datasketches/memory/test/MemoryTest.java | 73 +++++--
.../memory/test/MemoryWriteToTest.java | 16 +-
.../datasketches/memory/test/NioBitsTest.java | 112 ++++++++--
.../datasketches/memory/test/ReflectUtil.java | 28 ++-
.../datasketches/memory/test/SpecificLeafTest.java | 226 ++++++++++++++-------
15 files changed, 509 insertions(+), 251 deletions(-)
diff --git a/src/main/java/org/apache/datasketches/memory/BaseWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/BaseWritableBufferImpl.java
index 1894345..32e43ec 100644
--- a/src/main/java/org/apache/datasketches/memory/BaseWritableBufferImpl.java
+++ b/src/main/java/org/apache/datasketches/memory/BaseWritableBufferImpl.java
@@ -395,7 +395,7 @@ abstract class BaseWritableBufferImpl extends WritableBuffer {
long len = getEnd() - pos;
checkInvariants(getStart(), pos + len, getEnd(), getCapacity());
while (len > 0) {
- final long chunk = Math.min(len, CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES);
+ final long chunk = Math.min(len, Util.UNSAFE_COPY_THRESHOLD_BYTES);
unsafe.setMemory(getUnsafeObject(), getCumulativeOffset(pos), chunk, value);
pos += chunk;
len -= chunk;
diff --git a/src/main/java/org/apache/datasketches/memory/BaseWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/BaseWritableMemoryImpl.java
index 820358f..184c1f3 100644
--- a/src/main/java/org/apache/datasketches/memory/BaseWritableMemoryImpl.java
+++ b/src/main/java/org/apache/datasketches/memory/BaseWritableMemoryImpl.java
@@ -450,7 +450,7 @@ abstract class BaseWritableMemoryImpl extends WritableMemory {
public final void fill(long offsetBytes, long lengthBytes, final byte value) {
checkValidAndBoundsForWrite(offsetBytes, lengthBytes);
while (lengthBytes > 0) {
- final long chunk = Math.min(lengthBytes, CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES);
+ final long chunk = Math.min(lengthBytes, Util.UNSAFE_COPY_THRESHOLD_BYTES);
unsafe.setMemory(getUnsafeObject(), getCumulativeOffset(offsetBytes), chunk, value);
offsetBytes += chunk;
lengthBytes -= chunk;
@@ -482,7 +482,7 @@ abstract class BaseWritableMemoryImpl extends WritableMemory {
// or file-backed WritableByteChannel implementations with direct ByteBuffer argument could
// be subject of the same safepoint problems as in Unsafe.copyMemory and Unsafe.setMemory.
while (lengthBytes > 0) {
- final int chunk = (int) Math.min(CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES, lengthBytes);
+ final int chunk = (int) Math.min(Util.UNSAFE_COPY_THRESHOLD_BYTES, lengthBytes);
final ByteBuffer bufToWrite = AccessByteBuffer.getDummyReadOnlyDirectByteBuffer(addr, chunk);
writeFully(bufToWrite, out);
addr += chunk;
diff --git a/src/main/java/org/apache/datasketches/memory/CompareAndCopy.java b/src/main/java/org/apache/datasketches/memory/CompareAndCopy.java
index f9bbc0f..75cbf0f 100644
--- a/src/main/java/org/apache/datasketches/memory/CompareAndCopy.java
+++ b/src/main/java/org/apache/datasketches/memory/CompareAndCopy.java
@@ -27,6 +27,7 @@ import static org.apache.datasketches.memory.UnsafeUtil.LONG_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.SHORT_SHIFT;
import static org.apache.datasketches.memory.UnsafeUtil.checkBounds;
import static org.apache.datasketches.memory.UnsafeUtil.unsafe;
+import static org.apache.datasketches.memory.Util.UNSAFE_COPY_THRESHOLD_BYTES;
/**
* @author Lee Rhodes
@@ -34,18 +35,6 @@ import static org.apache.datasketches.memory.UnsafeUtil.unsafe;
@SuppressWarnings({"restriction"})
final class CompareAndCopy {
- /**
- * Don't use sun.misc.Unsafe#copyMemory to copy blocks of memory larger than this
- * threshold, because internally it doesn't have safepoint polls, that may cause long
- * "Time To Safe Point" pauses in the application. This has been fixed in JDK 9 (see
- * https://bugs.openjdk.java.net/browse/JDK-8149596 and
- * https://bugs.openjdk.java.net/browse/JDK-8141491), but not in JDK 8, so the Memory library
- * should keep having this boilerplate as long as it supports Java 8.
- *
- * <p>A reference to this can be found in java.nio.Bits.</p>
- */
- static final int UNSAFE_COPY_THRESHOLD_BYTES = 1024 * 1024;
-
private CompareAndCopy() { }
static int compare(
diff --git a/src/main/java/org/apache/datasketches/memory/DirectWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/DirectWritableMemoryImpl.java
index 886c7bb..7295e1c 100644
--- a/src/main/java/org/apache/datasketches/memory/DirectWritableMemoryImpl.java
+++ b/src/main/java/org/apache/datasketches/memory/DirectWritableMemoryImpl.java
@@ -94,11 +94,4 @@ final class DirectWritableMemoryImpl extends WritableMemoryImpl {
return valid.get();
}
- @Override
- void checkValid() {
- if (!this.isValid()) {
- throw new IllegalStateException("Memory not valid.");
- }
- }
-
}
diff --git a/src/main/java/org/apache/datasketches/memory/Util.java b/src/main/java/org/apache/datasketches/memory/Util.java
index 3866c59..61f1785 100644
--- a/src/main/java/org/apache/datasketches/memory/Util.java
+++ b/src/main/java/org/apache/datasketches/memory/Util.java
@@ -42,6 +42,18 @@ public final class Util {
public static final ByteOrder nonNativeByteOrder = nativeByteOrder == ByteOrder.LITTLE_ENDIAN
? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
+ /**
+ * Don't use sun.misc.Unsafe#copyMemory to copy blocks of memory larger than this
+ * threshold, because internally it doesn't have safepoint polls, that may cause long
+ * "Time To Safe Point" pauses in the application. This has been fixed in JDK 9 (see
+ * https://bugs.openjdk.java.net/browse/JDK-8149596 and
+ * https://bugs.openjdk.java.net/browse/JDK-8141491), but not in JDK 8, so the Memory library
+ * should keep having this boilerplate as long as it supports Java 8.
+ *
+ * <p>A reference to this can be found in java.nio.Bits.</p>
+ */
+ public static final int UNSAFE_COPY_THRESHOLD_BYTES = 1024 * 1024;
+
private Util() { }
//Byte Order Related
diff --git a/src/test/java/org/apache/datasketches/memory/test/AllocateDirectMemoryTest.java b/src/test/java/org/apache/datasketches/memory/test/AllocateDirectMemoryTest.java
index 6a37003..583327e 100644
--- a/src/test/java/org/apache/datasketches/memory/test/AllocateDirectMemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/AllocateDirectMemoryTest.java
@@ -39,6 +39,34 @@ import org.testng.annotations.Test;
@SuppressWarnings("javadoc")
public class AllocateDirectMemoryTest {
+ static final Method CHECK_VALID;
+ static final Method WRAP_DIRECT;
+ static final Method GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS;
+
+ static {
+ CHECK_VALID =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "checkValid", (Class<?>[])null); //not static
+ WRAP_DIRECT =
+ ReflectUtil.getMethod(ReflectUtil.BASE_WRITABLE_MEMORY_IMPL,
+ "wrapDirect", long.class, ByteOrder.class, MemoryRequestServer.class); //static method
+ GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE,
+ "getCurrentDirectMemoryAllocations", (Class<?>[])null); //static method
+ }
+
+ private static long getCurrentDirectMemoryAllocations() {
+ try {
+ return (long) GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static WritableDirectHandle wrapDirect(final long capacityBytes,
+ final ByteOrder byteOrder, final MemoryRequestServer memReqSvr) {
+ try {
+ return (WritableDirectHandle) WRAP_DIRECT.invoke(null, capacityBytes, byteOrder, memReqSvr);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
@Test
public void simpleAllocateDirect() {
int longs = 32;
@@ -49,16 +77,23 @@ public class AllocateDirectMemoryTest {
wMem.putLong(i << 3, i);
assertEquals(wMem.getLong(i << 3), i);
}
+ //inside the TWR block the memory should be valid
+ try {
+ CHECK_VALID.invoke(wMem);
+ //wMem.checkValid();
+ //OK
+ } catch (final Exception e) {
+ throw new RuntimeException(e);
+ }
}
//The TWR block has exited, so the memory should be invalid
try {
- final Class<?> wMemClass = wMem.getClass();
- Method method = ReflectUtil.getMethod(wMemClass, "checkValid", (Class<?>[])null);
- method.invoke(wMem, (Object[]) null);
+ CHECK_VALID.invoke(wMem);
//wMem.checkValid();
fail();
} catch (final Exception e) {
if (e instanceof IllegalStateException || e instanceof InvocationTargetException) { } //OK
+ //if IllegalAccessException or IllegalArgumentException NOT OK
else { throw new RuntimeException(e); }
}
}
@@ -101,19 +136,12 @@ public class AllocateDirectMemoryTest {
@Test
public void checkNonNativeDirect() { //not allowed in public API
- Class<?> bwMemImpl = ReflectUtil.getClass("org.apache.datasketches.memory.BaseWritableMemoryImpl");
- Method wrapDirectMethod =
- ReflectUtil.getMethod(bwMemImpl, "wrapDirect", long.class, ByteOrder.class, MemoryRequestServer.class);
- try (WritableDirectHandle h = (WritableDirectHandle)
- wrapDirectMethod.invoke(wrapDirectMethod, 8, Util.nonNativeByteOrder, null)) {
+ try (WritableDirectHandle h = wrapDirect(8, Util.nonNativeByteOrder, null)) {
//BaseWritableMemoryImpl.wrapDirect(8, Util.nonNativeByteOrder, null)) {
WritableMemory wmem = h.get();
wmem.putChar(0, (char) 1);
assertEquals(wmem.getByte(1), (byte) 1);
- } catch (final Exception e) {
- if (e instanceof IllegalStateException || e instanceof InvocationTargetException) { } //OK
- else { throw new RuntimeException(e); }
- }
+ }
}
@Test
@@ -126,20 +154,11 @@ public class AllocateDirectMemoryTest {
@AfterClass
public void checkDirectCounter() {
- final Class<?> baseStateClass = ReflectUtil.getClass("org.apache.datasketches.memory.BaseState");
- final Method currDirMemAllocMethod =
- ReflectUtil.getMethod(baseStateClass, "getCurrentDirectMemoryAllocations", (Class<?>[])null);
- long count;
- try {
- count = (long) currDirMemAllocMethod.invoke(currDirMemAllocMethod, (Object[])null);
- //final long count = BaseState.getCurrentDirectMemoryAllocations();
- if (count != 0) {
- println(""+count);
- fail();
- }
- } catch (final Exception e) {
- if (e instanceof IllegalStateException || e instanceof InvocationTargetException) { } //OK
- else { throw new RuntimeException(e); }
+ long count = getCurrentDirectMemoryAllocations();
+ //final long count = BaseState.getCurrentDirectMemoryAllocations();
+ if (count != 0) {
+ println(""+count);
+ fail();
}
}
diff --git a/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java b/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
index 25af375..657c1a2 100644
--- a/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
@@ -35,7 +35,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteOrder;
@@ -54,6 +53,28 @@ import org.testng.annotations.Test;
public class AllocateDirectWritableMapMemoryTest {
private static final String LS = System.getProperty("line.separator");
+ static final Method IS_FILE_READ_ONLY;
+ static final Method GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATIONS;
+
+ static {
+ IS_FILE_READ_ONLY =
+ ReflectUtil.getMethod(ReflectUtil.ALLOCATE_DIRECT_MAP, "isFileReadOnly", File.class);
+ GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATIONS =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getCurrentDirectMemoryMapAllocations", (Class<?>[])null); //static
+ }
+
+ private static boolean isFileReadOnly(final File file) {
+ try {
+ return (boolean) IS_FILE_READ_ONLY.invoke(null, file);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getCurrentDirectMemoryMapAllocations() {
+ try {
+ return (long) GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATIONS.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
@BeforeClass
public void setReadOnly() {
UtilTest.setGettysburgAddressFileToReadOnly();
@@ -150,16 +171,8 @@ public class AllocateDirectWritableMapMemoryTest {
@Test(expectedExceptions = ReadOnlyException.class)
public void simpleMap2() throws IOException {
File file = getResourceFile("GettysburgAddress.txt");
- final Class<?> allocDirMapClass = ReflectUtil.getClass("org.apache.datasketches.memory.AllocateDirectMap");
- final Method roMethod = ReflectUtil.getMethod(allocDirMapClass, "isFileReadOnly", file.getClass());
- try {
- assertTrue((boolean)roMethod.invoke(roMethod, file));
- //assertTrue(isFileReadOnly(file));
- } catch (final Exception e) {
- if (e instanceof IllegalStateException || e instanceof InvocationTargetException) { } //OK
- else { throw new RuntimeException(e); }
- }
- try (WritableMapHandle rh = WritableMemory.map(file)) {
+ assertTrue(isFileReadOnly(file));
+ try (WritableMapHandle rh = WritableMemory.map(file)) { //throws
//
}
}
@@ -235,32 +248,13 @@ public class AllocateDirectWritableMapMemoryTest {
@AfterClass
public void checkDirectCounter() {
- final Class<?> baseStateClass = ReflectUtil.getClass("org.apache.datasketches.memory.BaseState");
- final Method currDirMemAllocMethod =
- ReflectUtil.getMethod(baseStateClass, "getCurrentDirectMemoryMapAllocations", (Class<?>[])null);
- long count;
- try {
- count = (long) currDirMemAllocMethod.invoke(currDirMemAllocMethod, (Object[])null);
+ long count = getCurrentDirectMemoryMapAllocations();
//final long count = BaseState.getCurrentDirectMemoryMapAllocations();
if (count != 0) {
println(""+count);
fail();
}
- } catch (final Exception e) {
- if (e instanceof IllegalStateException || e instanceof InvocationTargetException) { } //OK
- else { throw new RuntimeException(e); }
- }
- }
-
-
-// @AfterClass
-// public void checkMapCounter() {
-// final long count = BaseState.getCurrentDirectMemoryMapAllocations();
-// if (count != 0) {
-// println(""+count);
-// fail();
-// }
-// }
+ }
@Test
public void printlnTest() {
diff --git a/src/test/java/org/apache/datasketches/memory/test/BaseStateTest.java b/src/test/java/org/apache/datasketches/memory/test/BaseStateTest.java
index 21045ff..83320a3 100644
--- a/src/test/java/org/apache/datasketches/memory/test/BaseStateTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/BaseStateTest.java
@@ -25,6 +25,7 @@ import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
+import java.lang.reflect.Method;
import java.nio.ByteOrder;
import org.apache.datasketches.memory.Buffer;
@@ -39,6 +40,19 @@ import org.testng.annotations.Test;
@SuppressWarnings("javadoc")
public class BaseStateTest {
+ static final Method GET_NATIVE_BASE_OFFSET;
+
+ static {
+ GET_NATIVE_BASE_OFFSET =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getNativeBaseOffset", (Class<?>[])null);
+ }
+
+ private static long getNativeBaseOffset(final Object owner) {
+ try {
+ return (long) GET_NATIVE_BASE_OFFSET.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
@Test
public void checkPrimOffset() {
int off = (int)Prim.BYTE.off();
@@ -89,11 +103,10 @@ public class BaseStateTest {
}
@Test
- public void checkGetNativeBaseOffset_Heap() {
+ public void checkGetNativeBaseOffset_Heap() throws Exception {
WritableMemory wmem = WritableMemory.allocate(8); //heap
- final long offset;
-
- assertEquals(wmem.getNativeBaseOffset(), 0L);
+ final long offset = getNativeBaseOffset(wmem);
+ assertEquals(offset, 0L);
}
@Test
diff --git a/src/test/java/org/apache/datasketches/memory/test/CopyMemoryTest.java b/src/test/java/org/apache/datasketches/memory/test/CopyMemoryTest.java
index b0552b3..a1f21e8 100644
--- a/src/test/java/org/apache/datasketches/memory/test/CopyMemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/CopyMemoryTest.java
@@ -19,7 +19,7 @@
package org.apache.datasketches.memory.test;
-import static org.apache.datasketches.memory.CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES;
+import static org.apache.datasketches.memory.Util.UNSAFE_COPY_THRESHOLD_BYTES;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.ThreadLocalRandom;
diff --git a/src/test/java/org/apache/datasketches/memory/test/LeafImplTest.java b/src/test/java/org/apache/datasketches/memory/test/LeafImplTest.java
index ce69bb8..3daee5e 100644
--- a/src/test/java/org/apache/datasketches/memory/test/LeafImplTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/LeafImplTest.java
@@ -27,14 +27,15 @@ import static org.testng.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import org.apache.datasketches.memory.BaseState;
import org.apache.datasketches.memory.WritableBuffer;
import org.apache.datasketches.memory.WritableDirectHandle;
import org.apache.datasketches.memory.WritableMapHandle;
import org.apache.datasketches.memory.WritableMemory;
+import org.apache.datasketches.memory.Util;
import org.testng.annotations.Test;
/**
@@ -45,7 +46,13 @@ public class LeafImplTest {
static final ByteOrder LE = ByteOrder.LITTLE_ENDIAN;
static final ByteOrder BE = ByteOrder.BIG_ENDIAN;
-
+ static final Method GET_UNSAFE_OBJECT;
+
+ static {
+ GET_UNSAFE_OBJECT = ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getUnsafeObject", (Class<?>[])null); //not static
+ }
+
+
@Test
public void checkDirectLeafs() {
long off = 0;
@@ -57,6 +64,12 @@ public class LeafImplTest {
}
}
+ private static Object getUnsafeObject(Object target) {
+ try {
+ return GET_UNSAFE_OBJECT.invoke(target);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
private static void checkDirectImpl(WritableMemory mem, long off, long cap) {
assertEquals(mem.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(mem.writableRegion(off, cap, BE).getShort(0), 256);
@@ -64,10 +77,11 @@ public class LeafImplTest {
assertEquals(mem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(mem.getByteBuffer() == null);
- assertTrue(mem.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() != null);
assertTrue(mem.isDirect());
- assertTrue(mem.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(mem) == null);
+ //assertTrue(mem.getUnsafeObject() == null);
assertTrue(mem.isValid() == true);
WritableBuffer buf = mem.asWritableBuffer();
@@ -78,13 +92,14 @@ public class LeafImplTest {
assertEquals(buf.writableDuplicate(BE).getShort(0), 256);
assertTrue(buf.getByteBuffer() == null);
- assertTrue(buf.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() != null);
assertTrue(buf.isDirect());
- assertTrue(buf.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(buf) == null);
+ //assertTrue(buf.getUnsafeObject() == null);
assertTrue(buf.isValid() == true);
- WritableMemory nnMem = mem.writableRegion(off, cap, BaseState.nonNativeByteOrder);
+ WritableMemory nnMem = mem.writableRegion(off, cap, Util.nonNativeByteOrder);
assertEquals(nnMem.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnMem.writableRegion(off, cap, BE).getShort(0), 256);
@@ -92,13 +107,14 @@ public class LeafImplTest {
assertEquals(nnMem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(nnMem.getByteBuffer() == null);
- assertTrue(nnMem.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() != null);
assertTrue(nnMem.isDirect());
- assertTrue(nnMem.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(nnMem) == null);
+ //assertTrue(nnMem.getUnsafeObject() == null);
assertTrue(nnMem.isValid() == true);
- WritableBuffer nnBuf = mem.asWritableBuffer(BaseState.nonNativeByteOrder);
+ WritableBuffer nnBuf = mem.asWritableBuffer(Util.nonNativeByteOrder);
assertEquals(nnBuf.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnBuf.writableRegion(off, cap, BE).getShort(0), 256);
@@ -106,10 +122,11 @@ public class LeafImplTest {
assertEquals(nnBuf.writableDuplicate(BE).getShort(0), 256);
assertTrue(nnBuf.getByteBuffer() == null);
- assertTrue(nnBuf.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() != null);
assertTrue(nnBuf.isDirect());
- assertTrue(nnBuf.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(nnBuf) == null);
+ //assertTrue(nnBuf.getUnsafeObject() == null);
assertTrue(nnBuf.isValid() == true);
}
@@ -130,7 +147,7 @@ public class LeafImplTest {
assertTrue(file.isFile());
file.deleteOnExit(); //comment out if you want to examine the file.
- try (WritableMapHandle wmh = WritableMemory.map(file, off, cap, BaseState.nativeByteOrder)) {
+ try (WritableMapHandle wmh = WritableMemory.map(file, off, cap, Util.nativeByteOrder)) {
WritableMemory mem = wmh.get();
mem.putShort(0, (short) 1);
assertEquals(mem.getByte(0), (byte) 1);
@@ -145,10 +162,11 @@ public class LeafImplTest {
assertEquals(mem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(mem.getByteBuffer() == null);
- assertTrue(mem.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() == null);
assertTrue(mem.isDirect());
- assertTrue(mem.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(mem) == null);
+ //assertTrue(mem.getUnsafeObject() == null);
assertTrue(mem.isValid() == true);
WritableBuffer buf = mem.asWritableBuffer();
@@ -159,13 +177,14 @@ public class LeafImplTest {
assertEquals(buf.writableDuplicate(BE).getShort(0), 256);
assertTrue(buf.getByteBuffer() == null);
- assertTrue(buf.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() == null);
assertTrue(buf.isDirect());
- assertTrue(buf.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(buf) == null);
+ //assertTrue(buf.getUnsafeObject() == null);
assertTrue(buf.isValid() == true);
- WritableMemory nnMem = mem.writableRegion(off, cap, BaseState.nonNativeByteOrder);
+ WritableMemory nnMem = mem.writableRegion(off, cap, Util.nonNativeByteOrder);
assertEquals(nnMem.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnMem.writableRegion(off, cap, BE).getShort(0), 256);
@@ -173,13 +192,14 @@ public class LeafImplTest {
assertEquals(nnMem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(nnMem.getByteBuffer() == null);
- assertTrue(nnMem.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() == null);
assertTrue(nnMem.isDirect());
- assertTrue(nnMem.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(nnMem) == null);
+ //assertTrue(nnMem.getUnsafeObject() == null);
assertTrue(nnMem.isValid() == true);
- WritableBuffer nnBuf = mem.asWritableBuffer(BaseState.nonNativeByteOrder);
+ WritableBuffer nnBuf = mem.asWritableBuffer(Util.nonNativeByteOrder);
assertEquals(nnBuf.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnBuf.writableRegion(off, cap, BE).getShort(0), 256);
@@ -187,10 +207,11 @@ public class LeafImplTest {
assertEquals(nnBuf.writableDuplicate(BE).getShort(0), 256);
assertTrue(nnBuf.getByteBuffer() == null);
- assertTrue(nnBuf.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() == null);
assertTrue(nnBuf.isDirect());
- assertTrue(nnBuf.getUnsafeObject() == null);
+ assertTrue(getUnsafeObject(nnBuf) == null);
+ //assertTrue(nnBuf.getUnsafeObject() == null);
assertTrue(nnBuf.isValid() == true);
}
@@ -218,9 +239,10 @@ public class LeafImplTest {
assertEquals(mem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(mem.getByteBuffer() != null);
- assertTrue(mem.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() == null);
- Object obj = mem.getUnsafeObject();
+ Object obj = getUnsafeObject(mem);
+ //Object obj = mem.getUnsafeObject();
if (direct) {
assertTrue(mem.isDirect());
assertNull(obj);
@@ -238,9 +260,10 @@ public class LeafImplTest {
assertEquals(buf.writableDuplicate(BE).getShort(0), 256);
assertTrue(buf.getByteBuffer() != null);
- assertTrue(buf.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() == null);
- obj = buf.getUnsafeObject();
+ obj = getUnsafeObject(buf);
+ //obj = buf.getUnsafeObject();
if (direct) {
assertTrue(buf.isDirect());
assertNull(obj);
@@ -250,7 +273,7 @@ public class LeafImplTest {
}
assertTrue(buf.isValid() == true);
- WritableMemory nnMem = mem.writableRegion(off, cap, BaseState.nonNativeByteOrder);
+ WritableMemory nnMem = mem.writableRegion(off, cap, Util.nonNativeByteOrder);
assertEquals(nnMem.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnMem.writableRegion(off, cap, BE).getShort(0), 256);
@@ -258,9 +281,10 @@ public class LeafImplTest {
assertEquals(nnMem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(nnMem.getByteBuffer() != null);
- assertTrue(nnMem.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() == null);
- obj = nnMem.getUnsafeObject();
+ obj = getUnsafeObject(nnMem);
+ //obj = nnMem.getUnsafeObject();
if (direct) {
assertTrue(nnMem.isDirect());
assertNull(obj);
@@ -270,7 +294,7 @@ public class LeafImplTest {
}
assertTrue(nnMem.isValid() == true);
- WritableBuffer nnBuf = mem.asWritableBuffer(BaseState.nonNativeByteOrder);
+ WritableBuffer nnBuf = mem.asWritableBuffer(Util.nonNativeByteOrder);
assertEquals(nnBuf.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnBuf.writableRegion(off, cap, BE).getShort(0), 256);
@@ -278,9 +302,10 @@ public class LeafImplTest {
assertEquals(nnBuf.writableDuplicate(BE).getShort(0), 256);
assertTrue(nnBuf.getByteBuffer() != null);
- assertTrue(nnBuf.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() == null);
- obj = nnBuf.getUnsafeObject();
+ obj = getUnsafeObject(nnBuf);
+ //obj = nnBuf.getUnsafeObject();
if (direct) {
assertTrue(nnBuf.isDirect());
assertNull(obj);
@@ -307,10 +332,11 @@ public class LeafImplTest {
assertEquals(mem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(mem.getByteBuffer() == null);
- assertTrue(mem.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() == null);
assertFalse(mem.isDirect());
- assertTrue(mem.getUnsafeObject() != null);
+ assertTrue(getUnsafeObject(mem) != null);
+ //assertTrue(mem.getUnsafeObject() != null);
assertTrue(mem.isValid() == true);
WritableBuffer buf = mem.asWritableBuffer();
@@ -321,13 +347,14 @@ public class LeafImplTest {
assertEquals(buf.writableDuplicate(BE).getShort(0), 256);
assertTrue(buf.getByteBuffer() == null);
- assertTrue(buf.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() == null);
assertFalse(buf.isDirect());
- assertTrue(buf.getUnsafeObject() != null);
+ assertTrue(getUnsafeObject(buf) != null);
+ //assertTrue(buf.getUnsafeObject() != null);
assertTrue(buf.isValid() == true);
- WritableMemory nnMem = mem.writableRegion(off, cap, BaseState.nonNativeByteOrder);
+ WritableMemory nnMem = mem.writableRegion(off, cap, Util.nonNativeByteOrder);
assertEquals(nnMem.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnMem.writableRegion(off, cap, BE).getShort(0), 256);
@@ -335,13 +362,14 @@ public class LeafImplTest {
assertEquals(nnMem.asWritableBuffer(BE).getShort(0), 256);
assertTrue(nnMem.getByteBuffer() == null);
- assertTrue(nnMem.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() == null);
assertFalse(nnMem.isDirect());
- assertTrue(nnMem.getUnsafeObject() != null);
+ assertTrue(getUnsafeObject(nnMem) != null);
+ //assertTrue(nnMem.getUnsafeObject() != null);
assertTrue(nnMem.isValid() == true);
- WritableBuffer nnBuf = mem.asWritableBuffer(BaseState.nonNativeByteOrder);
+ WritableBuffer nnBuf = mem.asWritableBuffer(Util.nonNativeByteOrder);
assertEquals(nnBuf.writableRegion(off, cap, LE).getShort(0), 1);
assertEquals(nnBuf.writableRegion(off, cap, BE).getShort(0), 256);
@@ -349,10 +377,11 @@ public class LeafImplTest {
assertEquals(nnBuf.writableDuplicate(BE).getShort(0), 256);
assertTrue(nnBuf.getByteBuffer() == null);
- assertTrue(nnBuf.getTypeByteOrder() == BaseState.nonNativeByteOrder);
+ assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() == null);
assertFalse(nnBuf.isDirect());
- assertTrue(nnBuf.getUnsafeObject() != null);
+ assertTrue(getUnsafeObject(nnBuf) != null);
+ //assertTrue(nnBuf.getUnsafeObject() != null);
assertTrue(nnBuf.isValid() == true);
}
diff --git a/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java b/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
index 0022dfd..989ea10 100644
--- a/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
@@ -31,13 +31,14 @@ import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.io.File;
+import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
-import org.apache.datasketches.memory.BaseState;
import org.apache.datasketches.memory.MapHandle;
import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.Util;
import org.apache.datasketches.memory.WritableDirectHandle;
import org.apache.datasketches.memory.WritableHandle;
import org.apache.datasketches.memory.WritableMemory;
@@ -50,6 +51,46 @@ import org.testng.collections.Lists;
public class MemoryTest {
private static final String LS = System.getProperty("line.separator");
+ static final Method GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS;
+ static final Method GET_CURRENT_DIRECT_MEMORY_ALLOCATED;
+ static final Method GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATIONS;
+ static final Method GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATED;
+
+ static {
+ GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getCurrentDirectMemoryAllocations", (Class<?>[])null); //static
+ GET_CURRENT_DIRECT_MEMORY_ALLOCATED =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getCurrentDirectMemoryAllocated", (Class<?>[])null); //static
+ GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATIONS =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getCurrentDirectMemoryMapAllocations", (Class<?>[])null); //static
+ GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATED =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getCurrentDirectMemoryMapAllocated", (Class<?>[])null); //static
+ }
+
+ private static long getCurrentDirectMemoryAllocations() {
+ try {
+ return (long) GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getCurrentDirectMemoryAllocated() {
+ try {
+ return (long) GET_CURRENT_DIRECT_MEMORY_ALLOCATED.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getCurrentDirectMemoryMapAllocations() {
+ try {
+ return (long) GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATIONS.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getCurrentDirectMemoryMapAllocated() {
+ try {
+ return (long) GET_CURRENT_DIRECT_MEMORY_MAP_ALLOCATED.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
@BeforeClass
public void setReadOnly() {
UtilTest.setGettysburgAddressFileToReadOnly();
@@ -212,7 +253,7 @@ public class MemoryTest {
ByteBuffer bb = ByteBuffer.allocate(n * 8);
bb.order(ByteOrder.BIG_ENDIAN);
Memory mem = Memory.wrap(bb);
- assertFalse(mem.getTypeByteOrder() == BaseState.nativeByteOrder);
+ assertFalse(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertEquals(mem.getTypeByteOrder(), ByteOrder.BIG_ENDIAN);
}
@@ -268,7 +309,7 @@ public class MemoryTest {
long[] arr = new long[n];
for (int i = 0; i < n; i++) { arr[i] = i; }
Memory mem = Memory.wrap(arr);
- Memory reg = mem.region(n2 * 8, n2 * 8, BaseState.nonNativeByteOrder); //top half
+ Memory reg = mem.region(n2 * 8, n2 * 8, Util.nonNativeByteOrder); //top half
for (int i = 0; i < n2; i++) {
long v = Long.reverseBytes(reg.getLong(i * 8));
long e = i + n2;
@@ -308,7 +349,7 @@ public class MemoryTest {
//println("" + wmem.getLong(i * 8));
}
//println("");
- WritableMemory reg = wmem.writableRegion(n2 * 8, n2 * 8, BaseState.nonNativeByteOrder);
+ WritableMemory reg = wmem.writableRegion(n2 * 8, n2 * 8, Util.nonNativeByteOrder);
for (int i = 0; i < n2; i++) { reg.putLong(i * 8, i); }
for (int i = 0; i < n; i++) {
long v = wmem.getLong(i * 8);
@@ -389,17 +430,17 @@ public class MemoryTest {
int bytes = 1024;
WritableHandle wh1 = WritableMemory.allocateDirect(bytes);
WritableHandle wh2 = WritableMemory.allocateDirect(bytes);
- assertEquals(BaseState.getCurrentDirectMemoryAllocations(), 2L);
- assertEquals(BaseState.getCurrentDirectMemoryAllocated(), 2 * bytes);
+ assertEquals(getCurrentDirectMemoryAllocations(), 2L);
+ assertEquals(getCurrentDirectMemoryAllocated(), 2 * bytes);
wh1.close();
- assertEquals(BaseState.getCurrentDirectMemoryAllocations(), 1L);
- assertEquals(BaseState.getCurrentDirectMemoryAllocated(), bytes);
+ assertEquals(getCurrentDirectMemoryAllocations(), 1L);
+ assertEquals(getCurrentDirectMemoryAllocated(), bytes);
wh2.close();
wh2.close(); //check that it doesn't go negative.
- assertEquals(BaseState.getCurrentDirectMemoryAllocations(), 0L);
- assertEquals(BaseState.getCurrentDirectMemoryAllocated(), 0L);
+ assertEquals(getCurrentDirectMemoryAllocations(), 0L);
+ assertEquals(getCurrentDirectMemoryAllocated(), 0L);
}
@SuppressWarnings("resource")
@@ -411,17 +452,17 @@ public class MemoryTest {
MapHandle mmh1 = Memory.map(file);
MapHandle mmh2 = Memory.map(file);
- assertEquals(BaseState.getCurrentDirectMemoryMapAllocations(), 2L);
- assertEquals(BaseState.getCurrentDirectMemoryMapAllocated(), 2 * bytes);
+ assertEquals(getCurrentDirectMemoryMapAllocations(), 2L);
+ assertEquals(getCurrentDirectMemoryMapAllocated(), 2 * bytes);
mmh1.close();
- assertEquals(BaseState.getCurrentDirectMemoryMapAllocations(), 1L);
- assertEquals(BaseState.getCurrentDirectMemoryMapAllocated(), bytes);
+ assertEquals(getCurrentDirectMemoryMapAllocations(), 1L);
+ assertEquals(getCurrentDirectMemoryMapAllocated(), bytes);
mmh2.close();
mmh2.close(); //check that it doesn't go negative.
- assertEquals(BaseState.getCurrentDirectMemoryMapAllocations(), 0L);
- assertEquals(BaseState.getCurrentDirectMemoryMapAllocated(), 0L);
+ assertEquals(getCurrentDirectMemoryMapAllocations(), 0L);
+ assertEquals(getCurrentDirectMemoryMapAllocated(), 0L);
}
@Test
diff --git a/src/test/java/org/apache/datasketches/memory/test/MemoryWriteToTest.java b/src/test/java/org/apache/datasketches/memory/test/MemoryWriteToTest.java
index fdfe3eb..09ce3f7 100644
--- a/src/test/java/org/apache/datasketches/memory/test/MemoryWriteToTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/MemoryWriteToTest.java
@@ -25,7 +25,7 @@ import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.ThreadLocalRandom;
-import org.apache.datasketches.memory.CompareAndCopy;
+import static org.apache.datasketches.memory.Util.UNSAFE_COPY_THRESHOLD_BYTES;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.WritableHandle;
import org.apache.datasketches.memory.WritableMemory;
@@ -41,8 +41,8 @@ public class MemoryWriteToTest {
testWriteTo(createRandomBytesMemory(7));
testWriteTo(createRandomBytesMemory(1023));
testWriteTo(createRandomBytesMemory(10_000));
- testWriteTo(createRandomBytesMemory(CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5));
- testWriteTo(createRandomBytesMemory((CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10));
+ testWriteTo(createRandomBytesMemory(UNSAFE_COPY_THRESHOLD_BYTES * 5));
+ testWriteTo(createRandomBytesMemory((UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10));
}
@Test
@@ -51,21 +51,21 @@ public class MemoryWriteToTest {
testWriteTo(createRandomIntsMemory(7));
testWriteTo(createRandomIntsMemory(1023));
testWriteTo(createRandomIntsMemory(10_000));
- testWriteTo(createRandomIntsMemory(CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5));
- testWriteTo(createRandomIntsMemory((CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10));
+ testWriteTo(createRandomIntsMemory(UNSAFE_COPY_THRESHOLD_BYTES * 5));
+ testWriteTo(createRandomIntsMemory((UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10));
}
@Test
public void testOffHeap() throws IOException {
try (WritableHandle handle =
- WritableMemory.allocateDirect((CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10)) {
+ WritableMemory.allocateDirect((UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10)) {
WritableMemory mem = handle.get();
testWriteTo(mem.region(0, 0));
testOffHeap(mem, 7);
testOffHeap(mem, 1023);
testOffHeap(mem, 10_000);
- testOffHeap(mem, CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5);
- testOffHeap(mem, (CompareAndCopy.UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10);
+ testOffHeap(mem, UNSAFE_COPY_THRESHOLD_BYTES * 5);
+ testOffHeap(mem, (UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10);
}
}
diff --git a/src/test/java/org/apache/datasketches/memory/test/NioBitsTest.java b/src/test/java/org/apache/datasketches/memory/test/NioBitsTest.java
index b5e56e1..f6b7710 100644
--- a/src/test/java/org/apache/datasketches/memory/test/NioBitsTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/NioBitsTest.java
@@ -19,9 +19,10 @@
package org.apache.datasketches.memory.test;
+import java.lang.reflect.Method;
+
import static org.testng.Assert.assertEquals;
-import org.apache.datasketches.memory.NioBits;
import org.testng.annotations.Test;
/**
@@ -30,36 +31,121 @@ import org.testng.annotations.Test;
@SuppressWarnings("javadoc")
public class NioBitsTest {
+ static final Method GET_MAX_DIRECT_BYTE_BUFFER_MEMORY;
+ static final Method IS_PAGE_ALIGHED;
+ static final Method PAGE_SIZE;
+ static final Method PAGE_COUNT;
+ static final Method GET_DIRECT_ALLOCATIONS_COUNT;
+ static final Method GET_RESERVED_MEMORY;
+ static final Method GET_TOTAL_CAPACITY;
+ static final Method RESERVE_MEMORY;
+ static final Method UNRESERVE_MEMORY;
+
+ static {
+ GET_MAX_DIRECT_BYTE_BUFFER_MEMORY =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "getMaxDirectByteBufferMemory", (Class<?>[])null); //static
+ IS_PAGE_ALIGHED =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "isPageAligned", (Class<?>[])null); //static
+ PAGE_SIZE =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "pageSize", (Class<?>[])null); //static
+ PAGE_COUNT =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "pageCount", long.class); //static
+ GET_DIRECT_ALLOCATIONS_COUNT =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "getDirectAllocationsCount", (Class<?>[])null); //static
+ GET_RESERVED_MEMORY =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "getReservedMemory", (Class<?>[])null); //static
+ GET_TOTAL_CAPACITY =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "getTotalCapacity", (Class<?>[])null); //static
+ RESERVE_MEMORY =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "reserveMemory", long.class, long.class); //static
+ UNRESERVE_MEMORY =
+ ReflectUtil.getMethod(ReflectUtil.NIO_BITS, "unreserveMemory", long.class, long.class); //static
+ }
+
+ private static long getMaxDirectByteBufferMemory() {
+ try {
+ return (long) GET_MAX_DIRECT_BYTE_BUFFER_MEMORY.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isPageAligned() {
+ try {
+ return (boolean) IS_PAGE_ALIGHED.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static int pageSize() {
+ try {
+ return (int) PAGE_SIZE.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static int pageCount(final long bytes) {
+ try {
+ return (int) PAGE_COUNT.invoke(null, bytes);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getDirectAllocationsCount() {
+ try {
+ return (long) GET_DIRECT_ALLOCATIONS_COUNT.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getReservedMemory() {
+ try {
+ return (long) GET_RESERVED_MEMORY.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static long getTotalCapacity() {
+ try {
+ return (long) GET_TOTAL_CAPACITY.invoke(null);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static void reserveMemory(final long allocationSize, final long capacity) {
+ try {
+ RESERVE_MEMORY.invoke(null, allocationSize, capacity);
+ } catch (Exception e) {
+ throw new RuntimeException(e); }
+ }
+ private static void unreserveMemory(final long allocationSize, final long capacity) {
+ try {
+ UNRESERVE_MEMORY.invoke(null, allocationSize, capacity);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
@Test
public void checkVMParams() {
- println("Max Memory: " + NioBits.getMaxDirectByteBufferMemory());
- println("Page Aligned: " + NioBits.isPageAligned());
- println("Page Size: " + NioBits.pageSize());
+ println("Max Memory: " + getMaxDirectByteBufferMemory());
+ println("Page Aligned: " + isPageAligned());
+ println("Page Size: " + pageSize());
}
-
+
@Test
//testing this beyond 2GB may not work on JVMs < 8GB.
//This must be checked manually
public void checkGetAtomicFields() {
long cap = 1024L + Integer.MAX_VALUE;
printStats();
- NioBits.reserveMemory(cap, cap);
+ reserveMemory(cap, cap);
printStats();
- NioBits.unreserveMemory(cap, cap);
+ unreserveMemory(cap, cap);
printStats();
}
@Test
public void checkPageCount() {
- assertEquals(NioBits.pageCount(0), 0);
- assertEquals(NioBits.pageCount(1), 1);
+ assertEquals(pageCount(0), 0);
+ assertEquals(pageCount(1), 1);
}
private static void printStats() {
- long count = NioBits.getDirectAllocationsCount();
- long resMem = NioBits.getReservedMemory();
- long totCap = NioBits.getTotalCapacity();
- long maxDBBmem = NioBits.getMaxDirectByteBufferMemory();
+ long count = getDirectAllocationsCount();
+ long resMem = getReservedMemory();
+ long totCap = getTotalCapacity();
+ long maxDBBmem = getMaxDirectByteBufferMemory();
String s = String.format("%,10d\t%,15d\t%,15d\t%,15d", count, resMem, totCap, maxDBBmem);
println(s);
}
diff --git a/src/test/java/org/apache/datasketches/memory/test/ReflectUtil.java b/src/test/java/org/apache/datasketches/memory/test/ReflectUtil.java
index c60e113..fc6333c 100644
--- a/src/test/java/org/apache/datasketches/memory/test/ReflectUtil.java
+++ b/src/test/java/org/apache/datasketches/memory/test/ReflectUtil.java
@@ -23,6 +23,18 @@ import java.lang.reflect.*;
public class ReflectUtil {
+ final static Class<?> BASE_STATE;
+ final static Class<?> BASE_WRITABLE_MEMORY_IMPL;
+ final static Class<?> ALLOCATE_DIRECT_MAP;
+ final static Class<?> NIO_BITS;
+
+ static {
+ BASE_STATE = getClass("org.apache.datasketches.memory.BaseState");
+ BASE_WRITABLE_MEMORY_IMPL = getClass("org.apache.datasketches.memory.BaseWritableMemoryImpl");
+ ALLOCATE_DIRECT_MAP = getClass("org.apache.datasketches.memory.AllocateDirectMap");
+ NIO_BITS = getClass("org.apache.datasketches.memory.NioBits");
+ }
+
/**
* Gets a Class reference to the given class loaded by the SystemClassLoader.
* This will work for private, package-private and abstract classes.
@@ -122,22 +134,6 @@ public class ReflectUtil {
}
}
-// /**
-// * Invoke the given method along with its concreteOwnerClass and method argueents.
-// * Any exception is returned to the caller.
-// * @param method the given method
-// * @param concreteOwnerInstance the concrete owning class instance
-// * @param args the method arguments
-// * @return the result of the invoked method.
-// */
-// public static Object invokeMethod(
-// final Method method,
-// final Object concreteOwnerInstance,
-// Object... args) throws IllegalAccessException, InvocationTargetException, IllegalArgumentException {
-// method.setAccessible(true);
-// return method.invoke(concreteOwnerInstance, args);
-// }
-
}
diff --git a/src/test/java/org/apache/datasketches/memory/test/SpecificLeafTest.java b/src/test/java/org/apache/datasketches/memory/test/SpecificLeafTest.java
index 893fc22..72cbd01 100644
--- a/src/test/java/org/apache/datasketches/memory/test/SpecificLeafTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/SpecificLeafTest.java
@@ -19,18 +19,17 @@
package org.apache.datasketches.memory.test;
-import static org.apache.datasketches.memory.BaseState.DUPLICATE;
-import static org.apache.datasketches.memory.BaseState.REGION;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.nio.ByteBuffer;
-import org.apache.datasketches.memory.BaseState;
import org.apache.datasketches.memory.Buffer;
import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.Util;
import org.apache.datasketches.memory.WritableDirectHandle;
import org.apache.datasketches.memory.WritableMapHandle;
import org.apache.datasketches.memory.WritableMemory;
@@ -42,28 +41,115 @@ import org.testng.annotations.Test;
@SuppressWarnings("javadoc")
public class SpecificLeafTest {
+ static final Method IS_READ_ONLY_TYPE;
+ static final Method IS_BUFFER_TYPE;
+ static final Method IS_DUPLICATE_TYPE;
+ static final Method IS_REGION_TYPE;
+ static final Method IS_NON_NATIVE_TYPE;
+ static final Method IS_HEAP_TYPE;
+ static final Method IS_DIRECT_TYPE;
+ static final Method IS_MAP_TYPE;
+ static final Method IS_BB_TYPE;
+
+ static {
+ IS_READ_ONLY_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isReadOnlyType", (Class<?>[])null); //not static
+ IS_BUFFER_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isBufferType", (Class<?>[])null); //not static
+ IS_DUPLICATE_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isDuplicateType", (Class<?>[])null); //not static
+ IS_REGION_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isRegionType", (Class<?>[])null); //not static
+ IS_NON_NATIVE_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isNonNativeType", (Class<?>[])null); //not static
+ IS_HEAP_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isHeapType", (Class<?>[])null); //not static
+ IS_DIRECT_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isDirectType", (Class<?>[])null); //not static
+ IS_MAP_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isMapType", (Class<?>[])null); //not static
+ IS_BB_TYPE =
+ ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "isBBType", (Class<?>[])null); //not static
+ }
+
+ private static boolean isReadOnlyType(final Object owner) {
+ try {
+ return (boolean) IS_READ_ONLY_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isBufferType(final Object owner) {
+ try {
+ return (boolean) IS_BUFFER_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isDuplicateType(final Object owner) {
+ try {
+ return (boolean) IS_DUPLICATE_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isRegionType(final Object owner) {
+ try {
+ return (boolean) IS_REGION_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isNonNativeType(final Object owner) {
+ try {
+ return (boolean) IS_NON_NATIVE_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isHeapType(final Object owner) {
+ try {
+ return (boolean) IS_HEAP_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isDirectType(final Object owner) {
+ try {
+ return (boolean) IS_DIRECT_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isMapType(final Object owner) {
+ try {
+ return (boolean) IS_MAP_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ private static boolean isBBType(final Object owner) {
+ try {
+ return (boolean) IS_BB_TYPE.invoke(owner);
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+
+
@Test
public void checkByteBufferLeafs() {
int bytes = 128;
ByteBuffer bb = ByteBuffer.allocate(bytes);
- bb.order(BaseState.nativeByteOrder);
+ bb.order(Util.nativeByteOrder);
- Memory mem = Memory.wrap(bb).region(0, bytes, BaseState.nativeByteOrder);
- assertTrue(mem.isBBType());
+ Memory mem = Memory.wrap(bb).region(0, bytes, Util.nativeByteOrder);
+ assertTrue(isBBType(mem));
assertTrue(mem.isReadOnly());
checkCrossLeafTypeIds(mem);
- Buffer buf = mem.asBuffer().region(0, bytes, BaseState.nativeByteOrder);
+ Buffer buf = mem.asBuffer().region(0, bytes, Util.nativeByteOrder);
- bb.order(BaseState.nonNativeByteOrder);
- Memory mem2 = Memory.wrap(bb).region(0, bytes, BaseState.nonNativeByteOrder);
- Buffer buf2 = mem2.asBuffer().region(0, bytes, BaseState.nonNativeByteOrder);
+ bb.order(Util.nonNativeByteOrder);
+ Memory mem2 = Memory.wrap(bb).region(0, bytes, Util.nonNativeByteOrder);
+ Buffer buf2 = mem2.asBuffer().region(0, bytes, Util.nonNativeByteOrder);
Buffer buf3 = buf2.duplicate();
- assertTrue((mem.getTypeId() & REGION) > 0);
- assertTrue((mem2.getTypeId() & REGION) > 0);
- assertTrue((buf.getTypeId() & REGION) > 0);
- assertTrue((buf2.getTypeId() & REGION) > 0);
- assertTrue((buf3.getTypeId() & DUPLICATE) > 0);
+ assertTrue(isRegionType(mem));
+ assertTrue(isRegionType(mem2));
+ assertTrue(isRegionType(buf));
+ assertTrue(isRegionType(buf2));
+ assertTrue(isDuplicateType(buf3));
}
@Test
@@ -71,24 +157,24 @@ public class SpecificLeafTest {
int bytes = 128;
try (WritableDirectHandle h = WritableMemory.allocateDirect(bytes)) {
WritableMemory wmem = h.get(); //native mem
- assertTrue(wmem.isDirectType());
+ assertTrue(isDirectType(wmem));
assertFalse(wmem.isReadOnly());
checkCrossLeafTypeIds(wmem);
- WritableMemory nnwmem = wmem.writableRegion(0, bytes, BaseState.nonNativeByteOrder);
+ WritableMemory nnwmem = wmem.writableRegion(0, bytes, Util.nonNativeByteOrder);
- Memory mem = wmem.region(0, bytes, BaseState.nativeByteOrder);
- Buffer buf = mem.asBuffer().region(0, bytes, BaseState.nativeByteOrder);
+ Memory mem = wmem.region(0, bytes, Util.nativeByteOrder);
+ Buffer buf = mem.asBuffer().region(0, bytes, Util.nativeByteOrder);
- Memory mem2 = nnwmem.region(0, bytes, BaseState.nonNativeByteOrder);
- Buffer buf2 = mem2.asBuffer().region(0, bytes, BaseState.nonNativeByteOrder);
+ Memory mem2 = nnwmem.region(0, bytes, Util.nonNativeByteOrder);
+ Buffer buf2 = mem2.asBuffer().region(0, bytes, Util.nonNativeByteOrder);
Buffer buf3 = buf2.duplicate();
- assertTrue((mem.getTypeId() & REGION) > 0);
- assertTrue((mem2.getTypeId() & REGION) > 0);
- assertTrue((buf.getTypeId() & REGION) > 0);
- assertTrue((buf2.getTypeId() & REGION) > 0);
- assertTrue((buf3.getTypeId() & DUPLICATE) > 0);
+ assertTrue(isRegionType(mem));
+ assertTrue(isRegionType(mem2));
+ assertTrue(isRegionType(buf));
+ assertTrue(isRegionType(buf2));
+ assertTrue(isDuplicateType(buf3));
}
}
@@ -109,27 +195,27 @@ public class SpecificLeafTest {
final long bytes = 128;
- try (WritableMapHandle h = WritableMemory.map(file, 0L, bytes, BaseState.nativeByteOrder)) {
+ try (WritableMapHandle h = WritableMemory.map(file, 0L, bytes, Util.nativeByteOrder)) {
WritableMemory mem = h.get(); //native mem
- assertTrue(mem.isMapType());
+ assertTrue(isMapType(mem));
assertFalse(mem.isReadOnly());
checkCrossLeafTypeIds(mem);
- Memory nnreg = mem.region(0, bytes, BaseState.nonNativeByteOrder);
+ Memory nnreg = mem.region(0, bytes, Util.nonNativeByteOrder);
- Memory reg = mem.region(0, bytes, BaseState.nativeByteOrder);
- Buffer buf = reg.asBuffer().region(0, bytes, BaseState.nativeByteOrder);
+ Memory reg = mem.region(0, bytes, Util.nativeByteOrder);
+ Buffer buf = reg.asBuffer().region(0, bytes, Util.nativeByteOrder);
Buffer buf4 = buf.duplicate();
- Memory reg2 = nnreg.region(0, bytes, BaseState.nonNativeByteOrder);
- Buffer buf2 = reg2.asBuffer().region(0, bytes, BaseState.nonNativeByteOrder);
+ Memory reg2 = nnreg.region(0, bytes, Util.nonNativeByteOrder);
+ Buffer buf2 = reg2.asBuffer().region(0, bytes, Util.nonNativeByteOrder);
Buffer buf3 = buf2.duplicate();
- assertTrue((reg.getTypeId() & REGION) > 0);
- assertTrue((reg2.getTypeId() & REGION) > 0);
- assertTrue((buf.getTypeId() & REGION) > 0);
- assertTrue((buf2.getTypeId() & REGION) > 0);
- assertTrue((buf3.getTypeId() & DUPLICATE) > 0);
- assertTrue((buf4.getTypeId() & DUPLICATE) > 0);
+ assertTrue(isRegionType(reg));
+ assertTrue(isRegionType(reg2));
+ assertTrue(isRegionType(buf));
+ assertTrue(isRegionType(buf2));
+ assertTrue(isDuplicateType(buf3));
+ assertTrue(isDuplicateType(buf4));
}
}
@@ -137,56 +223,56 @@ public class SpecificLeafTest {
public void checkHeapLeafs() {
int bytes = 128;
Memory mem = Memory.wrap(new byte[bytes]);
- assertTrue(mem.isHeapType());
- assertTrue(mem.isReadOnlyType());
+ assertTrue(isHeapType(mem));
+ assertTrue(isReadOnlyType(mem));
checkCrossLeafTypeIds(mem);
- Memory nnreg = mem.region(0, bytes, BaseState.nonNativeByteOrder);
+ Memory nnreg = mem.region(0, bytes, Util.nonNativeByteOrder);
- Memory reg = mem.region(0, bytes, BaseState.nativeByteOrder);
- Buffer buf = reg.asBuffer().region(0, bytes, BaseState.nativeByteOrder);
+ Memory reg = mem.region(0, bytes, Util.nativeByteOrder);
+ Buffer buf = reg.asBuffer().region(0, bytes, Util.nativeByteOrder);
Buffer buf4 = buf.duplicate();
- Memory reg2 = nnreg.region(0, bytes, BaseState.nonNativeByteOrder);
- Buffer buf2 = reg2.asBuffer().region(0, bytes, BaseState.nonNativeByteOrder);
+ Memory reg2 = nnreg.region(0, bytes, Util.nonNativeByteOrder);
+ Buffer buf2 = reg2.asBuffer().region(0, bytes, Util.nonNativeByteOrder);
Buffer buf3 = buf2.duplicate();
- assertFalse((mem.getTypeId() & REGION) > 0);
- assertTrue((reg2.getTypeId() & REGION) > 0);
- assertTrue((buf.getTypeId() & REGION) > 0);
- assertTrue((buf2.getTypeId() & REGION) > 0);
- assertTrue((buf3.getTypeId() & DUPLICATE) > 0);
- assertTrue((buf4.getTypeId() & DUPLICATE) > 0);
+ assertFalse(isRegionType(mem));
+ assertTrue(isRegionType(reg2));
+ assertTrue(isRegionType(buf));
+ assertTrue(isRegionType(buf2));
+ assertTrue(isDuplicateType(buf3));
+ assertTrue(isDuplicateType(buf4));
}
private static void checkCrossLeafTypeIds(Memory mem) {
Memory reg1 = mem.region(0, mem.getCapacity());
- assertTrue(reg1.isRegionType());
+ assertTrue(isRegionType(reg1));
Buffer buf1 = reg1.asBuffer();
- assertTrue(buf1.isRegionType());
- assertTrue(buf1.isBufferType());
+ assertTrue(isRegionType(buf1));
+ assertTrue(isBufferType(buf1));
Buffer buf2 = buf1.duplicate();
- assertTrue(buf2.isRegionType());
- assertTrue(buf2.isBufferType());
- assertTrue(buf2.isDuplicateType());
+ assertTrue(isRegionType(buf2));
+ assertTrue(isBufferType(buf2));
+ assertTrue(isDuplicateType(buf2));
Memory mem2 = buf1.asMemory();
- assertTrue(mem2.isRegionType());
- assertFalse(mem2.isBufferType());
- assertFalse(mem2.isDuplicateType());
+ assertTrue(isRegionType(mem2));
+ assertFalse(isBufferType(mem2));
+ assertFalse(isDuplicateType(mem2));
- Buffer buf3 = buf1.duplicate(BaseState.nonNativeByteOrder);
- assertTrue(buf3.isRegionType());
- assertTrue(buf3.isBufferType());
- assertTrue(buf3.isDuplicateType());
- assertTrue(buf3.isNonNativeType());
+ Buffer buf3 = buf1.duplicate(Util.nonNativeByteOrder);
+ assertTrue(isRegionType(buf3));
+ assertTrue(isBufferType(buf3));
+ assertTrue(isDuplicateType(buf3));
+ assertTrue(isNonNativeType(buf3));
Memory mem3 = buf3.asMemory();
- assertTrue(mem3.isRegionType());
- assertFalse(mem3.isBufferType());
- assertFalse(mem3.isDuplicateType());
- assertFalse(mem3.isNonNativeType());
+ assertTrue(isRegionType(mem3));
+ assertFalse(isBufferType(mem3));
+ assertFalse(isDuplicateType(mem3));
+ assertFalse(isNonNativeType(mem3));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org