Skip to content
Permalink
Browse files
Add support for Java 9 and Java 10
Extract the platform specific features to the java9 maven module:
- reference cleaner
- nio bits fields
- virtual machine memory fields

This makes use of the new MR-jar to coexist with the java8 equivalents.
  • Loading branch information
davecromberge committed Jun 17, 2021
1 parent 352dddd commit 3664b8d376da9a080d2fc331ea0599aaa456559f
Show file tree
Hide file tree
Showing 17 changed files with 565 additions and 35 deletions.
@@ -0,0 +1,64 @@
/*
* 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.test;

import org.apache.datasketches.memory.internal.MemoryCleaner;
import org.testng.annotations.Test;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

@SuppressWarnings("javadoc")
public class MemoryCleanerTest {

@Test
public void cleanerDeallocates() {
SimpleDeallocator deallocator = new SimpleDeallocator();
org.apache.datasketches.memory.internal.MemoryCleaner cleaner = new org.apache.datasketches.memory.internal.MemoryCleaner(this, deallocator);
cleaner.clean();
assertTrue(deallocator.getHasRun());
}

@Test
public void noDeallocation() {
SimpleDeallocator deallocator = new SimpleDeallocator();
org.apache.datasketches.memory.internal.MemoryCleaner cleaner = new MemoryCleaner(this, deallocator);
assertFalse(deallocator.getHasRun());
}

private static final class SimpleDeallocator implements Runnable {
static final AtomicBoolean hasRun = new AtomicBoolean();

private SimpleDeallocator() {
hasRun.set(false);
}

@Override
public void run() {
hasRun.compareAndSet(false, true);
}

public Boolean getHasRun() {
return hasRun.get();
}
}
}
@@ -0,0 +1,38 @@
/*
* 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.test.internal;

import org.apache.datasketches.memory.internal.VirtualMachineMemory;
import org.testng.annotations.Test;

@SuppressWarnings("javadoc")
public class VirtualMachineMemoryTest {

@Test
public void maxDirectBufferMemory() {
assert(org.apache.datasketches.memory.internal.VirtualMachineMemory.getMaxDBBMemory() >= 0);
}

@Test
public void inertPageAlignment() {
System.out.println("VM page alignment:" + VirtualMachineMemory.getIsPageAligned());
assert(true);
}
}
@@ -37,8 +37,8 @@ final class AllocateDirect {
private static final Logger LOG = LoggerFactory.getLogger(AllocateDirect.class);

private final Deallocator deallocator;
private final Cleaner cleaner; //JDK9+ moved to jdk.internal.ref.Cleaner;
private final long nativeBaseOffset;
private final MemoryCleaner cleaner;

/**
* Base Constructor for allocate native memory.
@@ -68,7 +68,7 @@ final class AllocateDirect {
nativeBaseOffset = nativeAddress;
}
deallocator = new Deallocator(nativeAddress, allocationSize, capacityBytes);
cleaner = Cleaner.create(this, deallocator);
cleaner = new MemoryCleaner(this, deallocator);
}

boolean doClose() {
@@ -97,7 +97,7 @@ class AllocateDirectMap implements Map {
}

private final Deallocator deallocator;
private final Cleaner cleaner;//JDK9+ moved to jdk.internal.ref.Cleaner;
private final MemoryCleaner cleaner;

final long capacityBytes;
final RandomAccessFile raf;
@@ -120,7 +120,7 @@ class AllocateDirectMap implements Map {
raf = mapper(file, fileOffsetBytes, capacityBytes, resourceReadOnly);
nativeBaseOffset = map(raf.getChannel(), resourceReadOnly, fileOffsetBytes, capacityBytes);
deallocator = new Deallocator(nativeBaseOffset, capacityBytes, raf);
cleaner = Cleaner.create(this, deallocator);
cleaner = new MemoryCleaner(this, deallocator);
}

//Map Interface
@@ -0,0 +1,53 @@
/*
* 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 sun.misc.Cleaner;

/**
* Extracts a version-dependent reference to the `sun.misc.Cleaner` into
* a standalone class. The package name for Cleaner 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 class MemoryCleaner {
private final Cleaner cleaner;

/**
* Creates a new `sun.misc.Cleaner`.
* @param referent the object to be cleaned
* @param deallocator - the cleanup code to be run when the cleaner is invoked.
* return MemoryCleaner
*/
public MemoryCleaner(Object referent, Runnable deallocator) {
cleaner = Cleaner.create(referent, deallocator);
}

/**
* Runs this cleaner, if it has not been run before.
*/
public void clean() {
cleaner.clean();
}
}

@@ -33,10 +33,6 @@
*/
@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;
@@ -51,18 +47,8 @@ final class NioBits {

static {
try {
VM_CLASS = Class.forName("sun.misc.VM"); //JDK9+ moved to 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
isPageAligned = VirtualMachineMemory.getIsPageAligned();
maxDBBMemory = VirtualMachineMemory.getMaxDBBMemory();

NIO_BITS_CLASS = Class.forName("java.nio.Bits");

@@ -74,22 +60,20 @@ final class NioBits {
.getDeclaredMethod("unreserveMemory", long.class, int.class); //JD16 requires (long, long)
NIO_BITS_UNRESERVE_MEMORY_METHOD.setAccessible(true);

//JDK 8-10: "count", "reservedMemory", "totalCapacity"
//JDK 11-16: "COUNT", "RESERVE_MEMORY", "TOTAL_CAPACITY"
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));

} catch (final ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException | SecurityException | NoSuchFieldException e) {
| IllegalArgumentException | SecurityException | NoSuchFieldException e) {
throw new RuntimeException("Could not acquire java.nio.Bits class: " + e.getClass());
}
}
@@ -0,0 +1,35 @@
/*
* 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;

/**
* 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 COUNT_FIELD_NAME = "count";
static String RESERVED_MEMORY_FIELD_NAME = "reservedMemory";
static String TOTAL_CAPACITY_FIELD_NAME = "totalCapacity";
}
@@ -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.InvocationTargetException;
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 ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException | SecurityException e) {
throw new RuntimeException("Could not acquire sun.misc.VM class: " + 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;
}
}
@@ -17,11 +17,8 @@
* under the License.
*/

package org.apache.datasketches.memory;

public class PlaceHolder {
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
module org.apache.datasketches.memory.tests {
requires java.base;
requires org.testng;
requires org.apache.datasketches.memory;
}

0 comments on commit 3664b8d

Please sign in to comment.