This is an automated email from the ASF dual-hosted git repository.

dcromberge pushed a commit to branch multi-module-experimental
in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git


The following commit(s) were added to refs/heads/multi-module-experimental by 
this push:
     new a497e26  Additional tests for VM reflection
a497e26 is described below

commit a497e26e0f1c4afd3d04ee086b14ee3c44588bf4
Author: David Cromberge <[email protected]>
AuthorDate: Thu Apr 1 17:52:03 2021 +0100

    Additional tests for VM reflection
---
 .../org/apache/datasketches/memory/NioBits.java    | 22 ++-----
 .../apache/datasketches/memory/NioBitsFields.java  |  1 -
 .../memory/internal/VirtualMachineMemory.java      | 74 ++++++++++++++++++++++
 .../memory/internal/VirtualMachineMemoryTest.java} | 31 ++++-----
 datasketches-memory-java-nine-tests/pom.xml        |  1 +
 .../java/nine/tests/VirtualMachineMemoryTest.java  | 32 +++++-----
 datasketches-memory-java-nine/pom.xml              |  2 +
 .../apache/datasketches/memory/NioBitsFields.java  |  1 -
 .../memory/internal/VirtualMachineMemory.java      | 74 ++++++++++++++++++++++
 9 files changed, 189 insertions(+), 49 deletions(-)

diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBits.java
 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBits.java
index af58916..61b67e7 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBits.java
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBits.java
@@ -19,6 +19,8 @@
 
 package org.apache.datasketches.memory;
 
+import org.apache.datasketches.memory.internal.VirtualMachineMemory;
+
 import static org.apache.datasketches.memory.UnsafeUtil.unsafe;
 
 import java.lang.reflect.Field;
@@ -32,10 +34,6 @@ import java.util.concurrent.atomic.AtomicLong;
  */
 @SuppressWarnings({"restriction"})
 final class NioBits {
-  private static final Class<?> VM_CLASS;
-  private static final Method VM_MAX_DIRECT_MEMORY_METHOD;
-  private static final Method VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD;
-
   private static final Class<?> NIO_BITS_CLASS;
   private static final Method NIO_BITS_RESERVE_MEMORY_METHOD;
   private static final Method NIO_BITS_UNRESERVE_MEMORY_METHOD;
@@ -44,24 +42,14 @@ final class NioBits {
   private static final AtomicLong nioBitsReservedMemory;
   private static final AtomicLong nioBitsTotalCapacity;
 
-  private static int pageSize = unsafe.pageSize();
+  private static final int pageSize = unsafe.pageSize();
   private static final long maxDBBMemory;
   private static final boolean isPageAligned;
 
   static {
     try {
-      VM_CLASS = Class.forName(NioBitsFields.VM_CLASS_NAME);
-      VM_MAX_DIRECT_MEMORY_METHOD =
-              VM_CLASS.getDeclaredMethod("maxDirectMemory");
-      VM_MAX_DIRECT_MEMORY_METHOD.setAccessible(true);
-      maxDBBMemory = (long)VM_MAX_DIRECT_MEMORY_METHOD
-              .invoke(null); //static method
-
-      VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD =
-              VM_CLASS.getDeclaredMethod("isDirectMemoryPageAligned");
-      VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD.setAccessible(true);
-      isPageAligned = (boolean)VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD
-              .invoke(null); //static method
+      isPageAligned = VirtualMachineMemory.getIsPageAligned();
+      maxDBBMemory = VirtualMachineMemory.getMaxDBBMemory();
 
       NIO_BITS_CLASS = Class.forName("java.nio.Bits");
 
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
index a5a264c..8032769 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
@@ -29,7 +29,6 @@ package org.apache.datasketches.memory;
  * https://openjdk.java.net/jeps/238
  */
 class NioBitsFields {
-    static String VM_CLASS_NAME = "sun.misc.VM";
     static String COUNT_FIELD_NAME = "count";
     static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
     static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/internal/VirtualMachineMemory.java
 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/internal/VirtualMachineMemory.java
new file mode 100644
index 0000000..efc0cc4
--- /dev/null
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/internal/VirtualMachineMemory.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import java.lang.reflect.Method;
+
+/**
+ * Extracts a version-dependent reference to the `sun.misc.VM` into
+ * a standalone class. The package name for VM has changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
+ *
+ * For more information, see:
+ * https://openjdk.java.net/jeps/238
+ */
+public final class VirtualMachineMemory {
+
+    private static final Class<?> VM_CLASS;
+    private static final Method VM_MAX_DIRECT_MEMORY_METHOD;
+    private static final Method VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD;
+    private static final long maxDBBMemory;
+    private static final boolean isPageAligned;
+
+    static {
+        try {
+            VM_CLASS = Class.forName("sun.misc.VM");
+            VM_MAX_DIRECT_MEMORY_METHOD = 
VM_CLASS.getDeclaredMethod("maxDirectMemory");
+            VM_MAX_DIRECT_MEMORY_METHOD.setAccessible(true);
+            maxDBBMemory = (long) VM_MAX_DIRECT_MEMORY_METHOD.invoke(null); 
//static method
+
+            VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD =
+                    VM_CLASS.getDeclaredMethod("isDirectMemoryPageAligned");
+            VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD.setAccessible(true);
+            isPageAligned = (boolean) VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD
+                    .invoke(null); //static method
+        } catch (final Exception e) {
+            throw new RuntimeException("Could not acquire 
jdk.internal.misc.VM: " + e.getClass());
+        }
+    }
+
+    /**
+     * Returns the maximum amount of allocatable direct buffer memory.
+     * The directMemory variable is initialized during system initialization.
+     * @return the maximum amount of allocatable direct buffer memory.
+     */
+    public static long getMaxDBBMemory() {
+        return maxDBBMemory;
+    }
+
+    /**
+     * Returns true if the direct buffers should be page aligned.
+     * @return flag that determines whether direct buffers should be page 
aligned.
+     */
+    public static boolean getIsPageAligned() {
+        return isPageAligned;
+    }
+}
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-base/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
similarity index 57%
copy from 
datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
copy to 
datasketches-memory-base/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
index a5a264c..f883ba8 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-base/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
@@ -17,20 +17,21 @@
  * under the License.
  */
 
-package org.apache.datasketches.memory;
+package org.apache.datasketches.memory.internal;
 
-/**
- * Extracts version-dependent field names into standalone class.
- * Some field names in the VM internal class have changed in
- * later versions. The appropriate class will be loaded by the class loader
- * depending on the Java version that is used.
- *
- * For more information, see:
- * https://openjdk.java.net/jeps/238
- */
-class NioBitsFields {
-    static String VM_CLASS_NAME = "sun.misc.VM";
-    static String COUNT_FIELD_NAME = "count";
-    static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
-    static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
+import org.testng.annotations.Test;
+
+@SuppressWarnings("javadoc")
+public class VirtualMachineMemoryTest {
+
+    @Test
+    public void maxDirectBufferMemory() {
+       assert(VirtualMachineMemory.getMaxDBBMemory() >= 0);
+    }
+
+    @Test
+    public void inertPageAlignment() {
+        System.out.println("VM page alignment:" + 
VirtualMachineMemory.getIsPageAligned());
+        assert(true);
+    }
 }
diff --git a/datasketches-memory-java-nine-tests/pom.xml 
b/datasketches-memory-java-nine-tests/pom.xml
index dfd3d24..204e34a 100644
--- a/datasketches-memory-java-nine-tests/pom.xml
+++ b/datasketches-memory-java-nine-tests/pom.xml
@@ -82,6 +82,7 @@
                         
<redirectTestOutputToFile>true</redirectTestOutputToFile>
                         <argLine>
                             --add-opens 
java.base/jdk.internal.ref=org.apache.datasketches.memory
+                            --add-opens 
java.base/jdk.internal.misc=org.apache.datasketches.memory
                             --add-exports 
org.apache.datasketches.memory.java.nine.tests/org.apache.datasketches.memory.java.nine.tests=org.testng
                         </argLine>
                         <jdkToolchain>
diff --git 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-java-nine-tests/src/test/java/org/apache/datasketches/memory/java/nine/tests/VirtualMachineMemoryTest.java
similarity index 57%
copy from 
datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
copy to 
datasketches-memory-java-nine-tests/src/test/java/org/apache/datasketches/memory/java/nine/tests/VirtualMachineMemoryTest.java
index a5a264c..5f74c97 100644
--- 
a/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-java-nine-tests/src/test/java/org/apache/datasketches/memory/java/nine/tests/VirtualMachineMemoryTest.java
@@ -17,20 +17,22 @@
  * under the License.
  */
 
-package org.apache.datasketches.memory;
+package org.apache.datasketches.memory.java.nine.tests;
 
-/**
- * Extracts version-dependent field names into standalone class.
- * Some field names in the VM internal class have changed in
- * later versions. The appropriate class will be loaded by the class loader
- * depending on the Java version that is used.
- *
- * For more information, see:
- * https://openjdk.java.net/jeps/238
- */
-class NioBitsFields {
-    static String VM_CLASS_NAME = "sun.misc.VM";
-    static String COUNT_FIELD_NAME = "count";
-    static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
-    static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
+import org.apache.datasketches.memory.internal.VirtualMachineMemory;
+import org.testng.annotations.Test;
+
+@SuppressWarnings("javadoc")
+public class VirtualMachineMemoryTest {
+
+    @Test
+    public void maxDirectBufferMemory() {
+       assert(VirtualMachineMemory.getMaxDBBMemory() >= 0);
+    }
+
+    @Test
+    public void inertPageAlignment() {
+        System.out.println("VM page alignment:" + 
VirtualMachineMemory.getIsPageAligned());
+        assert(true);
+    }
 }
diff --git a/datasketches-memory-java-nine/pom.xml 
b/datasketches-memory-java-nine/pom.xml
index 67b388e..c2901ec 100644
--- a/datasketches-memory-java-nine/pom.xml
+++ b/datasketches-memory-java-nine/pom.xml
@@ -59,6 +59,8 @@
                         <compilerArgs>
                             <arg>--add-exports</arg>
                             
<arg>java.base/jdk.internal.ref=org.apache.datasketches.memory</arg>
+                            <arg>--add-exports</arg>
+                            
<arg>java.base/jdk.internal.misc=org.apache.datasketches.memory</arg>
                         </compilerArgs>
                         <jdkToolchain>
                             <version>9</version>
diff --git 
a/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
index 4637585..8032769 100644
--- 
a/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
+++ 
b/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
@@ -29,7 +29,6 @@ package org.apache.datasketches.memory;
  * https://openjdk.java.net/jeps/238
  */
 class NioBitsFields {
-    static String VM_CLASS_NAME = "jdk.internal.misc.VM";
     static String COUNT_FIELD_NAME = "count";
     static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
     static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
diff --git 
a/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/internal/VirtualMachineMemory.java
 
b/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/internal/VirtualMachineMemory.java
new file mode 100644
index 0000000..605abf4
--- /dev/null
+++ 
b/datasketches-memory-java-nine/src/main/java/org/apache/datasketches/memory/internal/VirtualMachineMemory.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import java.lang.reflect.Method;
+
+/**
+ * Extracts a version-dependent reference to the `jdk.internal.misc.VM` into
+ * a standalone class. The package name for VM has changed in
+ * later versions. The appropriate class will be loaded by the class loader
+ * depending on the Java version that is used.
+ *
+ * For more information, see:
+ * https://openjdk.java.net/jeps/238
+ */
+public final class VirtualMachineMemory {
+
+    private static final Class<?> VM_CLASS;
+    private static final Method VM_MAX_DIRECT_MEMORY_METHOD;
+    private static final Method VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD;
+    private static final long maxDBBMemory;
+    private static final boolean isPageAligned;
+
+    static {
+        try {
+            VM_CLASS = Class.forName("jdk.internal.misc.VM");
+            VM_MAX_DIRECT_MEMORY_METHOD = 
VM_CLASS.getDeclaredMethod("maxDirectMemory");
+            VM_MAX_DIRECT_MEMORY_METHOD.setAccessible(true);
+            maxDBBMemory = (long) VM_MAX_DIRECT_MEMORY_METHOD.invoke(null); 
//static method
+
+            VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD =
+                    VM_CLASS.getDeclaredMethod("isDirectMemoryPageAligned");
+            VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD.setAccessible(true);
+            isPageAligned = (boolean) VM_IS_DIRECT_MEMORY_PAGE_ALIGNED_METHOD
+                    .invoke(null); //static method
+        } catch (final Exception e) {
+            throw new RuntimeException("Could not acquire 
jdk.internal.misc.VM: " + e.getClass());
+        }
+    }
+
+    /**
+     * Returns the maximum amount of allocatable direct buffer memory.
+     * The directMemory variable is initialized during system initialization.
+     * @return the maximum amount of allocatable direct buffer memory.
+     */
+    public static long getMaxDBBMemory() {
+        return maxDBBMemory;
+    }
+
+    /**
+     * Returns true if the direct buffers should be page aligned.
+     * @return flag that determines whether direct buffers should be page 
aligned.
+     */
+    public static boolean getIsPageAligned() {
+        return isPageAligned;
+    }
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to