Skip to content
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

feat: limit thread number to 1 when HDD is detected #269

Merged
merged 27 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ca5b7a7
feat: limit thread number to 1 when HDD is detected
Integral-Tech Nov 24, 2024
0ceed30
Fix change requests
Integral-Tech Nov 25, 2024
3452f39
chore(git): merge from master
KSXGitHub Nov 25, 2024
0c76273
refactor: create a module
KSXGitHub Nov 25, 2024
44423b0
refactor: rename a test
KSXGitHub Nov 25, 2024
0aaee0a
refactor: unnecessary visibility scope
KSXGitHub Nov 25, 2024
30883d7
refactor: make `detect_hdd_in_files` testable
KSXGitHub Nov 25, 2024
053527e
feat: improve warning messages
KSXGitHub Nov 25, 2024
a53ee1d
refactor: rename a function
KSXGitHub Nov 25, 2024
4446031
refactor: rename an argument
KSXGitHub Nov 25, 2024
3e51cb2
refactor: extract a function
KSXGitHub Nov 25, 2024
42fee43
Add tests for path_is_in_hdd() & any_path_is_in_hdd()
Integral-Tech Nov 25, 2024
8b90da7
Apply suggestions
Integral-Tech Nov 25, 2024
c2d05f3
Update src/app/sub/hdd.rs
Integral-Tech Nov 25, 2024
f9d57b6
Remove unused import
Integral-Tech Nov 25, 2024
bf08ad6
refactor: use mocked trait
KSXGitHub Dec 1, 2024
94802d8
style: remove unnecessary trailing comma
KSXGitHub Dec 1, 2024
788a8f3
refactor: simplify the code
KSXGitHub Dec 1, 2024
8955f37
refactor: use constructor to reduce lines of code
KSXGitHub Dec 1, 2024
2beb762
test: add more cases
KSXGitHub Dec 1, 2024
33837e8
refactor: shorten the code
KSXGitHub Dec 1, 2024
ed39eab
refactor: descriptive paths
KSXGitHub Dec 1, 2024
e3a049d
refactor: rearrange
KSXGitHub Dec 1, 2024
b357e07
refactor: shorten the code
KSXGitHub Dec 1, 2024
187a6a5
style: add trailing comma
KSXGitHub Dec 1, 2024
8f55b0f
refactor: early return
KSXGitHub Dec 1, 2024
80d0ba1
docs: add
KSXGitHub Dec 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ clap_complete = { version = "^4.5.36", optional = true }
clap-utilities = { version = "^0.2.0", optional = true }
serde = { version = "^1.0.214", optional = true }
serde_json = { version = "^1.0.132", optional = true }
sysinfo = "0.32.0"

[dev-dependencies]
build-fs-tree = "^0.7.1"
Expand Down
24 changes: 23 additions & 1 deletion src/app/sub.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod hdd;
mod mount_point;

use crate::{
args::Fraction,
data_tree::{DataTree, DataTreeReflection},
Expand All @@ -11,8 +14,10 @@ use crate::{
status_board::GLOBAL_STATUS_BOARD,
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
};
use hdd::any_path_is_in_hdd;
use serde::Serialize;
use std::{io::stdout, iter::once, num::NonZeroUsize, path::PathBuf};
use std::{fs, io::stdout, iter::once, num::NonZeroUsize, path::PathBuf};
use sysinfo::Disks;

/// The sub program of the main application.
pub struct Sub<Size, SizeGetter, Report>
Expand Down Expand Up @@ -69,6 +74,23 @@ where
no_sort,
} = self;

// If one of the files is on HDD, set thread number to 1
let disks = Disks::new_with_refreshed_list();

if any_path_is_in_hdd(
&files,
&disks,
|disk| disk.kind(),
|disk| disk.mount_point(),
|path| fs::canonicalize(path),
) {
eprintln!("warning: HDD detected, the thread limit will be set to 1");
rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build_global()
.unwrap_or_else(|_| eprintln!("warning: Failed to set thread limit to 1"));
}

let mut iter = files
.into_iter()
.map(|root| -> DataTree<OsStringDisplay, Size> {
Expand Down
151 changes: 151 additions & 0 deletions src/app/sub/hdd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use super::mount_point::find_mount_point;
use std::{
io,
path::{Path, PathBuf},
};
use sysinfo::DiskKind;

pub fn any_path_is_in_hdd<Disk>(
paths: &[PathBuf],
disks: &[Disk],
get_disk_kind: impl Fn(&Disk) -> DiskKind + Copy,
get_mount_point: impl Fn(&Disk) -> &Path + Copy,
canonicalize: impl Fn(&Path) -> io::Result<PathBuf>,
) -> bool {
paths
.iter()
.filter_map(|file| canonicalize(file).ok())
.any(|path| path_is_in_hdd(&path, disks, get_disk_kind, get_mount_point))
}

fn path_is_in_hdd<Disk>(
path: &Path,
disks: &[Disk],
get_disk_kind: impl Fn(&Disk) -> DiskKind,
get_mount_point: impl Fn(&Disk) -> &Path + Copy,
) -> bool {
if let Some(mount_point) = find_mount_point(path, disks.iter().map(get_mount_point)) {
disks.iter().any(|disk| {
get_disk_kind(disk) == DiskKind::HDD && get_mount_point(disk) == mount_point
})
} else {
false
}
}

#[cfg(test)]
mod tests {
use super::{any_path_is_in_hdd, path_is_in_hdd};
use pretty_assertions::assert_eq;
use std::path::{Path, PathBuf};
use sysinfo::DiskKind;

struct Disk {
kind: DiskKind,
mount_point: &'static Path,
}

#[test]
fn test_path_in_hdd() {
let disks = &[
Disk {
kind: DiskKind::SSD,
mount_point: Path::new("/"),
},
Disk {
kind: DiskKind::HDD,
mount_point: Path::new("/home"),
},
Disk {
kind: DiskKind::HDD,
mount_point: Path::new("/mnt/data"),
},
Disk {
kind: DiskKind::SSD,
mount_point: Path::new("/mnt/repo"),
},
Disk {
kind: DiskKind::HDD,
mount_point: Path::new("/mnt/data/repo"),
},
];

for (path, in_hdd) in [
("/etc/fstab", false),
("/mnt/", false),
("/mnt/data/repo/test", true),
("/mnt/data/test/test", true),
("/mnt/repo/test/test", false),
] {
println!("CASE: {path} → {in_hdd:?}");
assert_eq!(
path_is_in_hdd(
Path::new(path),
disks,
|disk| disk.kind,
|disk| &disk.mount_point
),
in_hdd
);
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[test]
fn test_any_path_in_hdd() {
let disks = &[
Disk {
kind: DiskKind::SSD,
mount_point: Path::new("/"),
},
Disk {
kind: DiskKind::HDD,
mount_point: Path::new("/home"),
},
Disk {
kind: DiskKind::HDD,
mount_point: Path::new("/mnt/data"),
},
Disk {
kind: DiskKind::SSD,
mount_point: Path::new("/mnt/repo"),
},
Disk {
kind: DiskKind::HDD,
mount_point: Path::new("/mnt/data/repo"),
},
];

for (paths, in_hdd) in [
(
[
PathBuf::from("/etc/fstab"),
PathBuf::from("/home/user/file"),
],
true,
),
(
[
PathBuf::from("/mnt/data/file"),
PathBuf::from("/mnt/data/repo/test"),
],
true,
),
(
[PathBuf::from("/usr/share"), PathBuf::from("/mnt/repo/test")],
false,
),
] {
println!("CASE: {paths:?} → {in_hdd:?}");
assert_eq!(
any_path_is_in_hdd(
&paths,
disks,
|disk| disk.kind,
|disk| &disk.mount_point,
|path| Ok(path.to_path_buf()),
),
in_hdd
);
}
}
}
43 changes: 43 additions & 0 deletions src/app/sub/mount_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::{ffi::OsStr, path::Path};

pub fn find_mount_point<'a>(
path: &Path,
mount_points: impl IntoIterator<Item = &'a Path>,
) -> Option<&'a Path> {
mount_points
.into_iter()
.filter(|mnt| path.starts_with(mnt))
.max_by_key(|mnt| AsRef::<OsStr>::as_ref(mnt).len()) // Mount points can be nested in each other
}

#[cfg(test)]
mod tests {
use super::find_mount_point;
use pretty_assertions::assert_eq;
use std::path::Path;

#[test]
fn test_mount_point() {
let mount_points = [
Path::new("/"),
Path::new("/home"),
Path::new("/mnt/data"),
Path::new("/mnt/data/repo"),
Path::new("/mnt/repo"),
];

for (path, mount_point) in &[
("/etc/fstab", "/"),
("/home/user", "/home"),
("/mnt/data/repo/test", "/mnt/data/repo"),
("/mnt/data/test/test", "/mnt/data/"),
("/mnt/repo/test/test", "/mnt/repo/"),
] {
println!("CASE: {path} → {mount_point}");
assert_eq!(
find_mount_point(Path::new(path), mount_points).unwrap(),
Path::new(mount_point)
);
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading