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 b91f6c3  Extract NioBitsFields constants
b91f6c3 is described below

commit b91f6c3e4d474a1fb07e2269b8e918c95d7ca3c1
Author: David Cromberge <[email protected]>
AuthorDate: Wed Mar 24 09:36:13 2021 +0000

    Extract NioBitsFields constants
    
    This removes one inter-module dependency on the Util class from
    the java8 module.  Furthermore, the amount of duplicated code
    for NioBits is minimized by capturing the differences in the
    fields class.
    
    Moreover, the tests in the Java9 module adhere to the MR JAR
    behaviour, and ensure this behaviour works across versions.
---
 .../org/apache/datasketches/memory/NioBits.java    |   8 +-
 .../apache/datasketches/memory/NioBitsFields.java  |  34 ++++
 .../org/apache/datasketches/memory/NioBits.java    | 176 ---------------------
 .../apache/datasketches/memory/NioBitsFields.java  |  34 ++++
 4 files changed, 72 insertions(+), 180 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 d0b4158..af58916 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
@@ -50,7 +50,7 @@ final class NioBits {
 
   static {
     try {
-      VM_CLASS = Class.forName("sun.misc.VM");
+      VM_CLASS = Class.forName(NioBitsFields.VM_CLASS_NAME);
       VM_MAX_DIRECT_MEMORY_METHOD =
               VM_CLASS.getDeclaredMethod("maxDirectMemory");
       VM_MAX_DIRECT_MEMORY_METHOD.setAccessible(true);
@@ -73,15 +73,15 @@ final class NioBits {
               .getDeclaredMethod("unreserveMemory", long.class, int.class);
       NIO_BITS_UNRESERVE_MEMORY_METHOD.setAccessible(true);
 
-      final Field countField = NIO_BITS_CLASS.getDeclaredField("count");
+      final Field countField = 
NIO_BITS_CLASS.getDeclaredField(NioBitsFields.COUNT_FIELD_NAME);
       countField.setAccessible(true);
       nioBitsCount = (AtomicLong) (countField.get(null));
 
-      final Field reservedMemoryField = 
NIO_BITS_CLASS.getDeclaredField("reservedMemory");
+      final Field reservedMemoryField = 
NIO_BITS_CLASS.getDeclaredField(NioBitsFields.RESERVED_MEMORY_FIELD_NAME);
       reservedMemoryField.setAccessible(true);
       nioBitsReservedMemory = (AtomicLong) (reservedMemoryField.get(null));
 
-      final Field totalCapacityField = 
NIO_BITS_CLASS.getDeclaredField("totalCapacity");
+      final Field totalCapacityField = 
NIO_BITS_CLASS.getDeclaredField(NioBitsFields.TOTAL_CAPACITY_FIELD_NAME);
       totalCapacityField.setAccessible(true);
       nioBitsTotalCapacity = (AtomicLong) (totalCapacityField.get(null));
 
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
new file mode 100644
index 0000000..ef3fc62
--- /dev/null
+++ 
b/datasketches-memory-base/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+/**
+ * Extracts version-dependent field names into standalone class.
+ * The appropriate version will be loaded by the class loader.
+ *
+ * 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";
+}
diff --git 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBits.java
 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBits.java
deleted file mode 100644
index 973fb54..0000000
--- 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBits.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * 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;
-
-import static org.apache.datasketches.memory.UnsafeUtil.unsafe;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- * Provide linkage to java.nio.Bits.
- *
- * @author Lee Rhodes
- */
-@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;
-
-  private static final AtomicLong nioBitsCount;
-  private static final AtomicLong nioBitsReservedMemory;
-  private static final AtomicLong nioBitsTotalCapacity;
-
-  private static int pageSize = unsafe.pageSize();
-  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
-
-      NIO_BITS_CLASS = Class.forName("java.nio.Bits");
-
-      NIO_BITS_RESERVE_MEMORY_METHOD = NIO_BITS_CLASS
-          .getDeclaredMethod("reserveMemory", long.class, int.class);
-      NIO_BITS_RESERVE_MEMORY_METHOD.setAccessible(true);
-
-      NIO_BITS_UNRESERVE_MEMORY_METHOD = NIO_BITS_CLASS
-          .getDeclaredMethod("unreserveMemory", long.class, int.class);
-      NIO_BITS_UNRESERVE_MEMORY_METHOD.setAccessible(true);
-
-      final Field countField = NIO_BITS_CLASS.getDeclaredField("COUNT");
-      countField.setAccessible(true);
-      nioBitsCount = (AtomicLong) (countField.get(null));
-
-      final Field reservedMemoryField = 
NIO_BITS_CLASS.getDeclaredField("RESERVED_MEMORY");
-      reservedMemoryField.setAccessible(true);
-      nioBitsReservedMemory = (AtomicLong) (reservedMemoryField.get(null));
-
-      final Field totalCapacityField = 
NIO_BITS_CLASS.getDeclaredField("TOTAL_CAPACITY");
-      totalCapacityField.setAccessible(true);
-      nioBitsTotalCapacity = (AtomicLong) (totalCapacityField.get(null));
-
-    } catch (final Exception e) {
-      throw new RuntimeException("Could not acquire java.nio.Bits class: " + 
e.getClass());
-    }
-  }
-
-  private NioBits() { }
-
-  static long getDirectAllocationsCount() {
-    try {
-      final long count = nioBitsCount.get();
-      return count;
-    } catch (final Exception e) {
-      throw new RuntimeException("Cannot read Bits.count " + e);
-    }
-  }
-
-  static long getReservedMemory() {
-    try {
-      final long resMem = nioBitsReservedMemory.get();
-      return resMem;
-    } catch (final Exception e) {
-      throw new RuntimeException("Cannot read Bits.reservedMemory " + e);
-    }
-  }
-
-  static long getTotalCapacity() {
-    try {
-      final long resMem = nioBitsTotalCapacity.get();
-      return resMem;
-    } catch (final Exception e) {
-      throw new RuntimeException("Cannot read Bits.totalCapacity " + e);
-    }
-  }
-
-  static int pageSize() {
-    return pageSize;
-  }
-
-  static int pageCount(final long bytes) {
-    return (int)((bytes + pageSize()) - 1L) / pageSize();
-  }
-
-  static long getMaxDirectByteBufferMemory() {
-    return maxDBBMemory;
-  }
-
-  static boolean isPageAligned() {
-    return isPageAligned;
-  }
-
-  //RESERVE & UNRESERVE BITS MEMORY TRACKING COUNTERS
-  // Comment from java.nio.Bits.java ~ line 705:
-  // -XX:MaxDirectMemorySize limits the total capacity rather than the
-  // actual memory usage, which will differ when buffers are page aligned.
-  static void reserveMemory(final long allocationSize, final long capacity) {
-    try {
-      reserveUnreserve(allocationSize, capacity, 
NIO_BITS_RESERVE_MEMORY_METHOD);
-    } catch (final Exception e) {
-      throw new RuntimeException("Could not invoke 
java.nio.Bits.reserveMemory(...): "
-          + "allocationSize = " + allocationSize + ", capacity = " + capacity, 
e);
-    }
-  }
-
-  static void unreserveMemory(final long allocationSize, final long capacity) {
-    try {
-      reserveUnreserve(allocationSize, capacity, 
NIO_BITS_UNRESERVE_MEMORY_METHOD);
-    } catch (final Exception e) {
-      throw new RuntimeException("Could not invoke 
java.nio.Bits.unreserveMemory(...): "
-          + "allocationSize = " + allocationSize + ", capacity = " + capacity, 
e);
-    }
-  }
-
-  private static void reserveUnreserve(long allocationSize, long capacity, 
final Method method)
-      throws Exception {
-    Util.zeroCheck(capacity, "capacity");
-    // 1GB is a pretty "safe" limit.
-    final long chunkSizeLimit = 1L << 30;
-    while (capacity > 0) {
-      final long chunk = Math.min(capacity, chunkSizeLimit);
-      if (capacity == chunk) {
-        method.invoke(null, allocationSize, (int) capacity);
-      } else {
-        method.invoke(null, chunk, (int) chunk);
-      }
-      capacity -= chunk;
-      allocationSize -= chunk;
-    }
-  }
-}
diff --git 
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
new file mode 100644
index 0000000..7e9ebbb
--- /dev/null
+++ 
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/NioBitsFields.java
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+/**
+ * Extracts version-dependent field names into standalone class.
+ * The appropriate version will be loaded by the class loader.
+ *
+ * For more information, see:
+ * 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 = "RESERVED_MEMORY";
+    static String TOTAL_CAPACITY_FIELD_NAME = "TOTAL_CAPACITY";
+}

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

Reply via email to