Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
mmap
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed May 2, 2022
1 parent 2252bbb commit b16844e
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions runtime/src/snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use {
flate2::read::GzDecoder,
lazy_static::lazy_static,
log::*,
memmap2::Mmap,
rayon::prelude::*,
regex::Regex,
solana_measure::measure::Measure,
Expand All @@ -34,6 +35,7 @@ use {
io::{BufReader, BufWriter, Error as IoError, ErrorKind, Read, Seek, Write},
path::{Path, PathBuf},
process::ExitStatus,
slice,
str::FromStr,
sync::Arc,
},
Expand Down Expand Up @@ -1472,33 +1474,53 @@ fn untar_snapshot_in<P: AsRef<Path>>(
archive_format: ArchiveFormat,
parallel_divisions: usize,
) -> Result<UnpackedAppendVecMap> {
let open_file = || File::open(&snapshot_tar).unwrap();
let file = File::open(&snapshot_tar).unwrap();

// Map the file into immutable memory for better I/O performance
let mmap = unsafe { Mmap::map(&file) };
let mmap = mmap.unwrap_or_else(|e| {
error!(
"Failed to map the snapshot file: {} {}.\n
Please increase the virtual memory on the system.",
snapshot_tar.as_ref().display(),
e,
);
std::process::exit(1);
});

// The following code is safe because the lifetime of mmap last till the end of the function
// while the usage of mmap, BufReader's lifetime only last within fn unpack_snapshot_local.
let len = &mmap[..].len();
let ptr = &mmap[0] as *const u8;
let slice = unsafe { slice::from_raw_parts(ptr, *len) };

let account_paths_map = match archive_format {
ArchiveFormat::TarBzip2 => unpack_snapshot_local(
|| BzDecoder::new(BufReader::new(open_file())),
|| BzDecoder::new(BufReader::new(slice)),
unpack_dir,
account_paths,
parallel_divisions,
)?,
ArchiveFormat::TarGzip => unpack_snapshot_local(
|| GzDecoder::new(BufReader::new(open_file())),
|| GzDecoder::new(BufReader::new(slice)),
unpack_dir,
account_paths,
parallel_divisions,
)?,
ArchiveFormat::TarZstd => unpack_snapshot_local(
|| zstd::stream::read::Decoder::new(BufReader::new(open_file())).unwrap(),
|| zstd::stream::read::Decoder::new(BufReader::new(slice)).unwrap(),
unpack_dir,
account_paths,
parallel_divisions,
)?,
ArchiveFormat::Tar => unpack_snapshot_local(
|| BufReader::new(open_file()),
|| BufReader::new(slice),
unpack_dir,
account_paths,
parallel_divisions,
)?,
};

Ok(account_paths_map)
}

Expand Down

0 comments on commit b16844e

Please sign in to comment.