Skip to content

Commit

Permalink
Advise random file access on Windows too
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Oct 4, 2023
1 parent 7b120fe commit c43e85d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/subspace-farmer-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ thiserror = "1.0.38"
tokio = { version = "1.28.2", features = ["macros", "parking_lot", "rt-multi-thread", "signal", "sync"] }
tracing = "0.1.37"

[target.'cfg(windows)'.dependencies]
winapi = "0.3.9"

[dev-dependencies]
criterion = "0.5.1"
futures = "0.3.28"
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer-components/benches/auditing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use subspace_core_primitives::{
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_sector;
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt};
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy, PlottedSector};
use subspace_farmer_components::sector::{
sector_size, SectorContentsMap, SectorMetadata, SectorMetadataChecksummed,
Expand Down Expand Up @@ -168,6 +168,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.write(true)
.create(true)
.truncate(true)
.advise_random_access()
.open(&plot_file_path)
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer-components/benches/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use subspace_core_primitives::{
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_sector;
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt};
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy, PlottedSector};
use subspace_farmer_components::sector::{
sector_size, SectorContentsMap, SectorMetadata, SectorMetadataChecksummed,
Expand Down Expand Up @@ -223,6 +223,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.write(true)
.create(true)
.truncate(true)
.advise_random_access()
.open(&plot_file_path)
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer-components/benches/reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use subspace_core_primitives::{
HistorySize, PieceOffset, PublicKey, Record, RecordedHistorySegment, SectorId,
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt};
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy, PlottedSector};
use subspace_farmer_components::reading::read_piece;
use subspace_farmer_components::sector::{
Expand Down Expand Up @@ -168,6 +168,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.write(true)
.create(true)
.truncate(true)
.advise_random_access()
.open(&plot_file_path)
.unwrap();

Expand Down
52 changes: 49 additions & 3 deletions crates/subspace-farmer-components/src/file_ext.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
//! File extension trait

use std::fs::File;
use std::fs::{File, OpenOptions};
use std::io::Result;

pub trait OpenOptionsExt {
/// Advise OS/file system that file will use random access and read-ahead behavior is
/// undesirable, only has impact on Windows, for other operating systems see [`FileExt`]
fn advise_random_access(&mut self) -> &mut Self;

/// Advise OS/file system that file will use sequential access and read-ahead behavior is
/// desirable, only has impact on Windows, for other operating systems see [`FileExt`]
fn advise_sequential_access(&mut self) -> &mut Self;
}

impl OpenOptionsExt for OpenOptions {
#[cfg(target_os = "linux")]
fn advise_random_access(&mut self) -> &mut Self {
// Not supported
self
}

#[cfg(target_os = "macos")]
fn advise_random_access(&mut self) -> &mut Self {
// Not supported
self
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn advise_random_access(&mut self) -> &mut Self {
self.custom_flags(winapi::um::winbase::FILE_FLAG_RANDOM_ACCESS)
}

#[cfg(target_os = "linux")]
fn advise_sequential_access(&mut self) -> &mut Self {
// Not supported
self
}

#[cfg(target_os = "macos")]
fn advise_sequential_access(&mut self) -> &mut Self {
// Not supported
self
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn advise_sequential_access(&mut self) -> &mut Self {
self.custom_flags(winapi::um::winbase::FILE_FLAG_SEQUENTIAL_SCAN)
}
}

/// Extension convenience trait that allows pre-allocating files, suggesting random access pattern
/// and doing cross-platform exact reads/writes
pub trait FileExt {
/// Make sure file has specified number of bytes allocated for it
fn preallocate(&self, len: u64) -> Result<()>;

/// Advise OS/file system that file will use random access and read-ahead behavior is
/// undesirable
/// undesirable, on Windows this can only be set when file is opened, see [`OpenOptionsExt`]
fn advise_random_access(&self) -> Result<()>;

/// Advise OS/file system that file will use sequential access and read-ahead behavior is
/// desirable
/// desirable, on Windows this can only be set when file is opened, see [`OpenOptionsExt`]
fn advise_sequential_access(&self) -> Result<()>;

/// Read exact number of bytes at a specific offset
Expand Down
4 changes: 3 additions & 1 deletion crates/subspace-farmer/src/single_disk_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use subspace_core_primitives::{
SegmentIndex,
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt};
use subspace_farmer_components::plotting::{PieceGetter, PlottedSector};
use subspace_farmer_components::sector::{sector_size, SectorMetadata, SectorMetadataChecksummed};
use subspace_farmer_components::FarmerProtocolInfo;
Expand Down Expand Up @@ -748,6 +748,7 @@ impl SingleDiskFarm {
.read(true)
.write(true)
.create(true)
.advise_random_access()
.open(directory.join(Self::METADATA_FILE))?;

metadata_file.advise_random_access()?;
Expand Down Expand Up @@ -823,6 +824,7 @@ impl SingleDiskFarm {
.read(true)
.write(true)
.create(true)
.advise_random_access()
.open(directory.join(Self::PLOT_FILE))?,
);

Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer/src/single_disk_farm/piece_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use std::{fs, io, mem};
use subspace_core_primitives::crypto::blake3_hash_list;
use subspace_core_primitives::{Blake3Hash, Piece, PieceIndex};
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt};
use thiserror::Error;
use tracing::{debug, info, warn};

Expand Down Expand Up @@ -63,6 +63,7 @@ impl DiskPieceCache {
.read(true)
.write(true)
.create(true)
.advise_random_access()
.open(directory.join(Self::FILE_NAME))?;

file.advise_random_access()?;
Expand Down

0 comments on commit c43e85d

Please sign in to comment.