From d15ed3ecddb6d097422418d35e4da29323f4c15f Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Thu, 17 Oct 2024 12:44:11 +0200 Subject: [PATCH] Server: a bit nicer supportlib --- .../FileObjectPersistentStore.java | 4 +-- .../usatiuk/dhfs/supportlib/DhfsSupport.java | 35 +++++++++++++++++++ .../dhfs/supportlib/DhfsSupportImpl.java | 11 ++++++ .../supportlib/DhfsSupportImplFallback.java | 21 +++++++++++ .../supportlib/DhfsSupportImplNative.java | 20 +++++++++++ .../dhfs/supportlib/DhfsSupportNative.java | 4 +-- .../supportlib/UninitializedByteBuffer.java | 8 ++--- .../src/DhfsSupportNative.cpp | 4 --- 8 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupport.java create mode 100644 dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImpl.java create mode 100644 dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplFallback.java create mode 100644 dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplNative.java diff --git a/dhfs-parent/server/src/main/java/com/usatiuk/dhfs/objects/repository/persistence/FileObjectPersistentStore.java b/dhfs-parent/server/src/main/java/com/usatiuk/dhfs/objects/repository/persistence/FileObjectPersistentStore.java index 81b7bae5..3dd12370 100644 --- a/dhfs-parent/server/src/main/java/com/usatiuk/dhfs/objects/repository/persistence/FileObjectPersistentStore.java +++ b/dhfs-parent/server/src/main/java/com/usatiuk/dhfs/objects/repository/persistence/FileObjectPersistentStore.java @@ -6,7 +6,7 @@ import com.google.protobuf.UnsafeByteOperations; import com.usatiuk.dhfs.SerializationHelper; import com.usatiuk.dhfs.objects.persistence.JObjectDataP; import com.usatiuk.dhfs.objects.persistence.ObjectMetadataP; -import com.usatiuk.dhfs.supportlib.DhfsSupportNative; +import com.usatiuk.dhfs.supportlib.DhfsSupport; import com.usatiuk.dhfs.supportlib.UninitializedByteBuffer; import com.usatiuk.utils.ByteUtils; import com.usatiuk.utils.StatusRuntimeExceptionNoStacktrace; @@ -51,7 +51,7 @@ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; @ApplicationScoped public class FileObjectPersistentStore implements ObjectPersistentStore { - private final int META_BLOCK_SIZE = DhfsSupportNative.PAGE_SIZE; + private final int META_BLOCK_SIZE = DhfsSupport.PAGE_SIZE; private final Path _root; private final Path _txManifest; private ExecutorService _flushExecutor; diff --git a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupport.java b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupport.java new file mode 100644 index 00000000..082f2916 --- /dev/null +++ b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupport.java @@ -0,0 +1,35 @@ +package com.usatiuk.dhfs.supportlib; + +import java.nio.ByteBuffer; +import java.util.logging.Logger; + +public class DhfsSupport { + public static final int PAGE_SIZE; + private static final Logger LOGGER = Logger.getLogger(DhfsSupport.class.getName()); + private static final DhfsSupportImpl IMPLEMENTATION; + + static { + DhfsSupportImpl tmp; + try { + System.load(DhfsNativeLibFinder.getLibPath().toAbsolutePath().toString()); + tmp = new DhfsSupportImplNative(); + } catch (Throwable e) { + LOGGER.warning("Failed to load native libraries, using fallback: \n" + e); + tmp = new DhfsSupportImplFallback(); + } + IMPLEMENTATION = tmp; + PAGE_SIZE = getPageSizeInternal(); + } + + static long allocateUninitializedByteBuffer(ByteBuffer[] bb, int size) { + return IMPLEMENTATION.allocateUninitializedByteBuffer(bb, size); + } + + static void releaseByteBuffer(long token) { + IMPLEMENTATION.releaseByteBuffer(token); + } + + private static int getPageSizeInternal() { + return IMPLEMENTATION.getPageSizeInternal(); + } +} diff --git a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImpl.java b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImpl.java new file mode 100644 index 00000000..b261f56d --- /dev/null +++ b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImpl.java @@ -0,0 +1,11 @@ +package com.usatiuk.dhfs.supportlib; + +import java.nio.ByteBuffer; + +interface DhfsSupportImpl { + long allocateUninitializedByteBuffer(ByteBuffer[] bb, int size); + + void releaseByteBuffer(long token); + + int getPageSizeInternal(); +} diff --git a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplFallback.java b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplFallback.java new file mode 100644 index 00000000..e7e6d64c --- /dev/null +++ b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplFallback.java @@ -0,0 +1,21 @@ +package com.usatiuk.dhfs.supportlib; + +import java.nio.ByteBuffer; + +class DhfsSupportImplFallback implements DhfsSupportImpl { + @Override + public long allocateUninitializedByteBuffer(ByteBuffer[] bb, int size) { + bb[0] = ByteBuffer.allocateDirect(size); + return -1; + } + + @Override + public void releaseByteBuffer(long token) { + // GC + } + + @Override + public int getPageSizeInternal() { + return 4096; // FIXME:? + } +} diff --git a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplNative.java b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplNative.java new file mode 100644 index 00000000..d7ed580c --- /dev/null +++ b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportImplNative.java @@ -0,0 +1,20 @@ +package com.usatiuk.dhfs.supportlib; + +import java.nio.ByteBuffer; + +class DhfsSupportImplNative implements DhfsSupportImpl { + @Override + public long allocateUninitializedByteBuffer(ByteBuffer[] bb, int size) { + return DhfsSupportNative.allocateUninitializedByteBuffer(bb, size); + } + + @Override + public void releaseByteBuffer(long token) { + DhfsSupportNative.releaseByteBuffer(token); + } + + @Override + public int getPageSizeInternal() { + return DhfsSupportNative.PAGE_SIZE; + } +} diff --git a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportNative.java b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportNative.java index 3179ff20..392fa793 100644 --- a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportNative.java +++ b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/DhfsSupportNative.java @@ -4,7 +4,7 @@ package com.usatiuk.dhfs.supportlib; import java.nio.ByteBuffer; -public class DhfsSupportNative { +class DhfsSupportNative { static public final int PAGE_SIZE; static { @@ -12,8 +12,6 @@ public class DhfsSupportNative { PAGE_SIZE = getPageSizeInternal(); } - public static native void hello(); - static native long allocateUninitializedByteBuffer(ByteBuffer[] bb, int size); static native void releaseByteBuffer(long token); diff --git a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/UninitializedByteBuffer.java b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/UninitializedByteBuffer.java index 4479527c..c5f16629 100644 --- a/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/UninitializedByteBuffer.java +++ b/dhfs-parent/supportlib/src/main/java/com/usatiuk/dhfs/supportlib/UninitializedByteBuffer.java @@ -9,17 +9,17 @@ public class UninitializedByteBuffer { private static final Logger LOGGER = Logger.getLogger(UninitializedByteBuffer.class.getName()); public static ByteBuffer allocateUninitialized(int size) { - if (size < DhfsSupportNative.PAGE_SIZE) + if (size < DhfsSupport.PAGE_SIZE) return ByteBuffer.allocateDirect(size); var bb = new ByteBuffer[1]; - long token = DhfsSupportNative.allocateUninitializedByteBuffer(bb, size); + long token = DhfsSupport.allocateUninitializedByteBuffer(bb, size); var ret = bb[0]; CLEANER.register(ret, () -> { try { - DhfsSupportNative.releaseByteBuffer(token); + DhfsSupport.releaseByteBuffer(token); } catch (Throwable e) { - LOGGER.severe("Error releasing buffer: " + e.toString()); + LOGGER.severe("Error releasing buffer: " + e); System.exit(-1); } }); diff --git a/libdhfs_support/DhfsSupportNative/src/DhfsSupportNative.cpp b/libdhfs_support/DhfsSupportNative/src/DhfsSupportNative.cpp index b3d34e81..2786ce5d 100644 --- a/libdhfs_support/DhfsSupportNative/src/DhfsSupportNative.cpp +++ b/libdhfs_support/DhfsSupportNative/src/DhfsSupportNative.cpp @@ -9,10 +9,6 @@ #include "MemoryHelpers.h" extern "C" { -JNIEXPORT void JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_hello(JNIEnv* env, jclass klass) { - printf("Hello, World!\n"); -} - JNIEXPORT jlong JNICALL Java_com_usatiuk_dhfs_supportlib_DhfsSupportNative_allocateUninitializedByteBuffer (JNIEnv* env, jclass klass, jobjectArray bb, jint size) { if (size < 0) {