Server: a bit nicer supportlib

This commit is contained in:
2024-10-17 12:44:11 +02:00
parent 7c1cbe3eb6
commit d15ed3ecdd
8 changed files with 94 additions and 13 deletions

View File

@@ -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;

View File

@@ -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();
}
}

View File

@@ -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();
}

View File

@@ -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:?
}
}

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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);
}
});

View File

@@ -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) {