mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Merge pull request #154 from Zor-X-L/fallocate
try to preallocate space after RandomAccessFile#setLength()
This commit is contained in:
@@ -26,6 +26,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;
|
||||
@@ -40,10 +41,7 @@ import net.openhft.chronicle.wire.WireIn;
|
||||
import net.openhft.chronicle.wire.WireOut;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.*;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
@@ -957,6 +955,16 @@ public abstract class VanillaChronicleHash<K,
|
||||
// globalMutableStateLock), or on map creation, when race condition should be excluded
|
||||
// by self-bootstrapping header spec
|
||||
raf.setLength(minFileSize);
|
||||
|
||||
// RandomAccessFile#setLength() only calls ftruncate,
|
||||
// which will not preallocate space on XFS filesystem of Linux.
|
||||
// And writing that file will create a sparse file with a large number of extents.
|
||||
// This kind of fragmented file may hang the program and cause dmesg reports
|
||||
// "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 (OS.isLinux()) {
|
||||
PosixFallocate.fallocate(raf.getFD(), 0, minFileSize);
|
||||
}
|
||||
}
|
||||
long address = OS.map(fileChannel, READ_WRITE, mappingOffsetInFile, mapSize);
|
||||
resources.addMemoryResource(address, 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user