-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memory-mapped file improvements (#103)
* Use large memory-mapped file segments to avoid running out of space on smaller machines * Add `--nodemap-madvise` argument to opt into madvise(random) for memory-mapped file access
- Loading branch information
Showing
12 changed files
with
293 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
planetiler-core/src/main/java/com/onthegomap/planetiler/util/Madvise.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2017 Upserve, Inc. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
package com.onthegomap.planetiler.util; | ||
|
||
import com.kenai.jffi.MemoryIO; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import jnr.ffi.LibraryLoader; | ||
import jnr.ffi.types.size_t; | ||
|
||
/** | ||
* Wrapper for native madvise function to be used via the public API | ||
* {@link MmapUtil#madvise(ByteBuffer, MmapUtil.Madvice)}. | ||
* <p> | ||
* Ported from <a href= | ||
* "https://github.com/upserve/uppend/blob/70967c6f24d7f1a3bbc18799f485d981da93f53b/src/main/java/com/upserve/uppend/blobs/NativeIO.java">upserve/uppend/NativeIO</a>. | ||
* | ||
* @see <a href="https://man7.org/linux/man-pages/man2/madvise.2.html">madvise(2) — Linux manual page</a> | ||
*/ | ||
class Madvise { | ||
|
||
private static final NativeC nativeC = LibraryLoader.create(NativeC.class).load("c"); | ||
static int pageSize; | ||
|
||
static { | ||
try { | ||
pageSize = nativeC.getpagesize(); // 4096 on most Linux | ||
} catch (UnsatisfiedLinkError e) { | ||
pageSize = -1; | ||
} | ||
} | ||
|
||
private static long alignedAddress(long address) { | ||
return address & (-pageSize); | ||
} | ||
|
||
private static long alignedSize(long address, int capacity) { | ||
long end = address + capacity; | ||
end = (end + pageSize - 1) & (-pageSize); | ||
return end - alignedAddress(address); | ||
} | ||
|
||
/** | ||
* Give a hint to the system how a mapped memory segment will be used so the OS can optimize performance. | ||
* | ||
* @param buffer The mapped memory segment. | ||
* @param value The advice to use. | ||
* @throws IOException If an error occurs or madvise not available on this system | ||
* @see <a href="https://man7.org/linux/man-pages/man2/madvise.2.html">madvise(2) — Linux manual page</a> | ||
*/ | ||
static void madvise(ByteBuffer buffer, int value) throws IOException { | ||
if (pageSize <= 0) { | ||
throw new IOException("madvise failed, pagesize not available"); | ||
} | ||
final long address = MemoryIO.getInstance().getDirectBufferAddress(buffer); | ||
final int capacity = buffer.capacity(); | ||
|
||
long alignedAddress = alignedAddress(address); | ||
long alignedSize = alignedSize(alignedAddress, capacity); | ||
try { | ||
int val = nativeC.madvise(alignedAddress, alignedSize, value); | ||
if (val != 0) { | ||
throw new IOException(String.format("System call madvise failed with code: %d", val)); | ||
} | ||
} catch (UnsatisfiedLinkError error) { | ||
throw new IOException("madvise failed", error); | ||
} | ||
} | ||
|
||
/** JNR-FFI will automatically compile these to wrappers around native functions with the same signatures. */ | ||
public interface NativeC { | ||
|
||
int madvise(@size_t long address, @size_t long size, int advice); | ||
|
||
int getpagesize(); | ||
} | ||
} |
Oops, something went wrong.