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 1 commit
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
27 changes: 26 additions & 1 deletion src/app/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
fs_tree_builder::FsTreeBuilder,
get_size::GetSize,
json_data::{BinaryVersion, JsonData, SchemaVersion, UnitAndTree},
mount_point::find_mountpoint,
os_string_display::OsStringDisplay,
reporter::ParallelReporter,
runtime_error::RuntimeError,
Expand All @@ -12,7 +13,8 @@ use crate::{
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
};
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::{DiskKind, Disks};

/// The sub program of the main application.
pub struct Sub<Size, SizeGetter, Report>
Expand Down Expand Up @@ -69,6 +71,29 @@ 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 files
.iter()
.filter_map(|file| fs::canonicalize(file).ok())
.any(|path| {
let mount_points: Vec<_> = disks.iter().map(|disk| disk.mount_point()).collect();
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
if let Some(mount_point) = find_mountpoint(&path, &mount_points) {
disks.iter().any(|disk| {
matches!(disk.kind(), DiskKind::HDD) && disk.mount_point() == mount_point
})
} else {
false
}
})
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
{
rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build_global()
.unwrap();
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
}

let mut iter = files
.into_iter()
.map(|root| -> DataTree<OsStringDisplay, Size> {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub mod data_tree;
pub mod fs_tree_builder;
pub mod get_size;
pub mod json_data;
pub mod mount_point;
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
pub mod os_string_display;
pub mod reporter;
pub mod size;
Expand Down
11 changes: 11 additions & 0 deletions src/mount_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::path::{Path, PathBuf};

pub fn find_mountpoint(path: &Path, mount_points: &[&Path]) -> Option<PathBuf> {
let path = path.to_string_lossy();

mount_points
.iter()
.filter(|mnt| path.starts_with(&*mnt.to_string_lossy()))
.max_by_key(|mnt| mnt.to_string_lossy().len())
.map(|mnt| mnt.to_path_buf())
}
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 26 additions & 0 deletions tests/find_mountpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use parallel_disk_usage::mount_point::find_mountpoint;
use std::path::Path;

#[test]
fn test_mountpoint() {
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/"),
] {
assert_eq!(
find_mountpoint(Path::new(path), mount_points).unwrap(),
Path::new(mount_point)
);
}
}
Loading