-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a second mmap-based serialization sink that is backed directly by a file. #16
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::serialization::{Addr, SerializationSink}; | ||
use std::fs::{File, OpenOptions}; | ||
use std::path::{Path}; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::os::unix::io::AsRawFd; | ||
use std::io; | ||
|
||
/// Implements a `SerializationSink` that uses a file-backed mmap. | ||
pub struct AsyncMmapSerializationSink { | ||
file: File, | ||
current_pos: AtomicUsize, | ||
mapping_start: *mut u8, | ||
mapping_len: usize, | ||
} | ||
|
||
impl SerializationSink for AsyncMmapSerializationSink { | ||
fn from_path(path: &Path) -> Self { | ||
|
||
// Lazily allocate 1 GB | ||
let file_size = 1 << 30; | ||
|
||
let file = OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.create(true) | ||
.truncate(true) | ||
.open(path) | ||
.unwrap(); | ||
|
||
if let Err(e) = file.set_len(file_size as u64) { | ||
panic!("Error setting file length: {:?}", e); | ||
} | ||
|
||
// | ||
let ptr: *mut libc::c_void = unsafe { | ||
match libc::mmap(0 as *mut _, file_size, libc::PROT_WRITE, libc::MAP_SHARED, file.as_raw_fd(), 0) { | ||
libc::MAP_FAILED => { | ||
panic!("Error creating mmap: {:?}", io::Error::last_os_error()) | ||
} | ||
other => other, | ||
} | ||
}; | ||
|
||
// Hint to the OS that it can write old pages to disk once they are | ||
// fully written. | ||
unsafe { | ||
if libc::madvise(ptr, file_size as _, libc::MADV_SEQUENTIAL) != 0 { | ||
eprintln!("Error during `madvise`: {:?}", io::Error::last_os_error()); | ||
} | ||
} | ||
|
||
AsyncMmapSerializationSink { | ||
file, | ||
current_pos: AtomicUsize::new(0), | ||
mapping_start: ptr as *mut u8, | ||
mapping_len: file_size as usize, | ||
} | ||
} | ||
|
||
#[inline] | ||
fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr | ||
where | ||
W: FnOnce(&mut [u8]), | ||
{ | ||
// Reserve the range of bytes we'll copy to | ||
let pos = self.current_pos.fetch_add(num_bytes, Ordering::SeqCst); | ||
|
||
// Bounds checks | ||
assert!(pos.checked_add(num_bytes).unwrap() <= self.mapping_len); | ||
|
||
let bytes: &mut [u8] = unsafe { | ||
let start: *mut u8 = self.mapping_start.offset(pos as isize); | ||
std::slice::from_raw_parts_mut(start, num_bytes) | ||
}; | ||
|
||
write(bytes); | ||
|
||
Addr(pos as u32) | ||
} | ||
} | ||
|
||
impl Drop for AsyncMmapSerializationSink { | ||
fn drop(&mut self) { | ||
let actual_size = *self.current_pos.get_mut(); | ||
|
||
unsafe { | ||
// First use `mremap` to shrink the memory map. Otherwise `munmap` | ||
// would write everything to the backing file, including the | ||
// memory we never touched. | ||
let new_addr = libc::mremap(self.mapping_start as *mut _, | ||
self.mapping_len as _, | ||
actual_size as _, | ||
0); | ||
|
||
if new_addr == libc::MAP_FAILED { | ||
eprintln!("mremap failed: {:?}", io::Error::last_os_error()) | ||
} | ||
|
||
if libc::munmap(new_addr, actual_size as _) != 0 { | ||
eprintln!("munmap failed: {:?}", io::Error::last_os_error()) | ||
} | ||
} | ||
|
||
if let Err(e) = self.file.set_len(actual_size as u64) { | ||
eprintln!("Error setting file length: {:?}", e); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mremap
isn't supported on macOS :(