Skip to content

Commit

Permalink
add cmd arg n_thread
Browse files Browse the repository at this point in the history
  • Loading branch information
seamile committed Jan 6, 2023
1 parent 6005655 commit 11459e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ $ fcnt3 [OPTIONS] [DIRECTORIES]...
* `-a` count all regular and hidden files
* `-s` count the total size of files
* `-R` non-recursive mode (files in sub-directories will be ignored)
* `-t <N_THREAD>` the number of threads for traversal (invalid in `non_recursive` mode)
* `-h, --help` Print help information
* `-V, --version` Print version information

Expand Down
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod output;
mod walker;

use clap::Parser;
use std::path::PathBuf;
use std::process::exit;

use clap::Parser;
use num_cpus::get as cpu_count;

use walker::Counter;

fn main() {
Expand All @@ -21,14 +23,22 @@ fn main() {
};
}
} else {
counters = walker::parallel_walk(directories, args.all_files, args.count_size);
let n_thread: usize;
if args.n_thread == None {
let n_cpu = cpu_count();
n_thread = if n_cpu > 4 { n_cpu } else { 4 };
} else {
n_thread = args.n_thread.unwrap();
}

counters = walker::parallel_walk(directories, args.all_files, args.count_size, n_thread);
}
Counter::output(&counters, args.count_size);
}

#[derive(Parser)]
#[command(name = "fcnt")]
#[command(version = "0.1.0")]
#[command(version = "0.2.0")]
#[command(about = "Count the total number of files in given directories.")]
struct CmdLineArgs {
/// the directories (default: ./)
Expand All @@ -45,6 +55,10 @@ struct CmdLineArgs {
/// non-recursive mode (files in sub-directories will be ignored).
#[arg(short = 'R')]
non_recursive: bool,

/// the number of threads for traversal (invalid in `non_recursive` mode).
#[arg(short = 't')]
n_thread: Option<usize>,
}

impl CmdLineArgs {
Expand Down
13 changes: 8 additions & 5 deletions src/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::thread;
use std::time::Duration;

use flume::unbounded as m_channel;
use num_cpus::get as cpu_count;

#[cfg(target_os = "linux")]
use std::os::linux::fs::MetadataExt;
Expand Down Expand Up @@ -198,8 +197,12 @@ pub fn walk(dirpath: &PathBuf, with_hidden: bool, count_sz: bool) -> Result<DirD

type Locker = Arc<Mutex<HashMap<usize, bool>>>;

pub fn parallel_walk(dirlist: Vec<PathBuf>, with_hidden: bool, count_sz: bool) -> Vec<Counter> {
let n_threads = cpu_count();
pub fn parallel_walk(
dirlist: Vec<PathBuf>,
with_hidden: bool,
count_sz: bool,
n_thread: usize,
) -> Vec<Counter> {
let (path_tx, path_rx) = m_channel::<PathBuf>();
let (cnt_tx, cnt_rx) = s_channel::<Counter>();
let mut counters = Vec::from_iter(dirlist.iter().map(|p| Counter::new(p)));
Expand All @@ -210,8 +213,8 @@ pub fn parallel_walk(dirlist: Vec<PathBuf>, with_hidden: bool, count_sz: bool) -
path_tx.send(path.clone()).expect("path send err");
}

// create walk threads which amount is n_threads
for t_idx in 0..n_threads {
// create walk threads which amount is n_thread
for t_idx in 0..n_thread {
// clone channels
let _path_tx = path_tx.clone();
let _path_rx = path_rx.clone();
Expand Down

0 comments on commit 11459e7

Please sign in to comment.