code refactoring

This commit is contained in:
Liu Xiao
2018-07-02 09:00:34 +08:00
parent 89a35a108e
commit e78677120b
2 changed files with 48 additions and 38 deletions
@@ -17,7 +17,6 @@
package net.openhft.chronicle.hash.impl;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import net.openhft.chronicle.algo.locks.*;
import net.openhft.chronicle.bytes.BytesStore;
@@ -29,6 +28,7 @@ import net.openhft.chronicle.hash.*;
import net.openhft.chronicle.hash.impl.util.BuildVersion;
import net.openhft.chronicle.hash.impl.util.Cleaner;
import net.openhft.chronicle.hash.impl.util.CleanerUtils;
import net.openhft.chronicle.hash.impl.util.jna.PosixFallocate;
import net.openhft.chronicle.hash.impl.util.jna.PosixMsync;
import net.openhft.chronicle.hash.impl.util.jna.WindowsMsync;
import net.openhft.chronicle.hash.serialization.DataAccess;
@@ -42,12 +42,9 @@ import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.WireIn;
import net.openhft.chronicle.wire.WireOut;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
@@ -70,9 +67,6 @@ public abstract class VanillaChronicleHash<K,
ECQ extends ExternalHashQueryContext<K>>
implements ChronicleHash<K, C, SC, ECQ>, Marshallable {
private static final Logger LOG =
LoggerFactory.getLogger(VanillaChronicleHash.class.getName());
public static final long TIER_COUNTERS_AREA_SIZE = 64;
public static final long RESERVED_GLOBAL_MUTABLE_STATE_BYTES = 1024;
@@ -949,36 +943,6 @@ public abstract class VanillaChronicleHash<K,
firstBulkToMapOffsetWithinMapping);
}
private static class PosixFallocateProxy {
static {
Native.register(Platform.C_LIBRARY_NAME);
}
private static native int posix_fallocate(int fd, long offset, long length);
private static int getNativeFileDescriptor(FileDescriptor descriptor) {
try {
final Field field = descriptor.getClass().getDeclaredField("fd");
field.setAccessible(true);
return (int) field.get(descriptor);
} catch (final Exception e) {
LOG.warn("unsupported FileDescriptor implementation: e={}", e.getLocalizedMessage());
return -1;
}
}
static void posixFallocate(FileDescriptor descriptor, long offset, long length) {
int fd = getNativeFileDescriptor(descriptor);
if (fd != -1) {
int ret = posix_fallocate(getNativeFileDescriptor(descriptor), offset, length);
if (ret != 0) {
LOG.warn("posix_fallocate() returned {}", ret);
}
}
}
}
/**
* @see net.openhft.chronicle.bytes.MappedFile#acquireByteStore(long, MappedBytesStoreFactory)
*/
@@ -1001,7 +965,7 @@ public abstract class VanillaChronicleHash<K,
// "XFS: ... possible memory allocation deadlock size ... in kmem_alloc (mode:0x250)".
// We can fix this by trying calling posix_fallocate to preallocate the space.
if (Platform.isLinux()) {
PosixFallocateProxy.posixFallocate(raf.getFD(), 0, minFileSize);
PosixFallocate.fallocate(raf.getFD(), 0, minFileSize);
}
}
long address = OS.map(fileChannel, READ_WRITE, mappingOffsetInFile, mapSize);
@@ -0,0 +1,46 @@
package net.openhft.chronicle.hash.impl.util.jna;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileDescriptor;
import java.lang.reflect.Field;
public final class PosixFallocate {
private static final Logger LOG =
LoggerFactory.getLogger(PosixFallocate.class.getName());
static {
Native.register(Platform.C_LIBRARY_NAME);
}
private PosixFallocate() {
}
public static void fallocate(FileDescriptor descriptor, long offset, long length) {
int fd = getNativeFileDescriptor(descriptor);
if (fd != -1) {
int ret = posix_fallocate(getNativeFileDescriptor(descriptor), offset, length);
if (ret != 0) {
LOG.warn("posix_fallocate() returned {}", ret);
}
}
}
private static native int posix_fallocate(int fd, long offset, long length);
private static int getNativeFileDescriptor(FileDescriptor descriptor) {
try {
final Field field = descriptor.getClass().getDeclaredField("fd");
field.setAccessible(true);
return (int) field.get(descriptor);
} catch (final Exception e) {
LOG.warn("unsupported FileDescriptor implementation: e={}", e.getLocalizedMessage());
return -1;
}
}
}