added max cpu mode

master
Avril 4 years ago
parent 5db64236f7
commit 7368adc761
Signed by: flanchan
GPG Key ID: 284488987C31F630

3
Cargo.lock generated

@ -188,11 +188,12 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "leanify-many" name = "leanify-many"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"futures", "futures",
"lazy_static", "lazy_static",
"num_cpus",
"recolored", "recolored",
"rustc_version", "rustc_version",
"termprogress", "termprogress",

@ -1,6 +1,6 @@
[package] [package]
name = "leanify-many" name = "leanify-many"
version = "0.1.0" version = "0.2.0"
description = "spawn leanify subprocesses" description = "spawn leanify subprocesses"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018" edition = "2018"
@ -22,6 +22,7 @@ futures = "0.3"
termprogress = {version="0.3", optional=true} termprogress = {version="0.3", optional=true}
cfg-if = "0.1.10" cfg-if = "0.1.10"
recolored = { version = "1.9.3", optional = true } recolored = { version = "1.9.3", optional = true }
num_cpus = "1.13.0"
[build-dependencies] [build-dependencies]
rustc_version = "0.2" rustc_version = "0.2"

@ -8,6 +8,7 @@ use std::{
PathBuf, PathBuf,
Path, Path,
}, },
marker::PhantomData,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -24,9 +25,13 @@ fn extra_args() -> &'static str
lazy_static! { lazy_static! {
static ref EXTRA: &'static str = { static ref EXTRA: &'static str = {
let work = || -> Result<String, std::fmt::Error> { let work = || -> Result<String, std::fmt::Error> {
#[allow(unused_imports)]
use std::fmt::Write as _; use std::fmt::Write as _;
#[allow(unused_mut)]
let mut output = String::new(); let mut output = String::new();
#[cfg(feature="progress")] writeln!(output, " --no-progress Do not display progress bar")?; #[cfg(feature="progress")] writeln!(output, " --no-progress Do not display progress bar")?;
#[cfg(feature="colour")] writeln!(output, " --no-colour Do not display terminal colours")?;
#[cfg(feature="colour")] writeln!(output, " --colour Always display terminal colour, even if env flags tell us not to")?;
Ok(output) Ok(output)
}; };
match work() { match work() {
@ -41,54 +46,55 @@ fn extra_args() -> &'static str
&EXTRA[..] &EXTRA[..]
} }
#[allow(unused_variables)]
mod feature mod feature
{ {
use cfg_if::cfg_if; use cfg_if::cfg_if;
#[macro_export] macro_rules! check { #[macro_export] macro_rules! check {
(on $name:literal) => { (on $name:literal, $desc:expr) => {
if cfg!(feature = $name) { if cfg!(feature = $name) {
feature::on($name, true); feature::on($name, true, $desc);
} else { } else {
feature::off($name, false); feature::off($name, false, $desc);
} }
}; };
(off $name:literal) => { (off $name:literal, $desc:expr) => {
if cfg!(feature = $name) { if cfg!(feature = $name) {
feature::on($name, false); feature::on($name, false, $desc);
} else { } else {
feature::off($name, true); feature::off($name, true, $desc);
} }
} }
} }
pub fn on(name: impl AsRef<str>, default: bool) pub fn on(name: impl AsRef<str>, default: bool, desc: impl AsRef<str>)
{ {
cfg_if! { cfg_if! {
if #[cfg(feature="colour")] { if #[cfg(feature="colour")] {
use recolored::Colorize; use recolored::Colorize;
if default { if default {
println!(" {}", format!("+{}", name.as_ref()).red()); println!(" {} {}", format!("+{}", name.as_ref()).red(), desc.as_ref().bright_black());
} else { } else {
println!(" {}", format!("+{}", name.as_ref()).bright_red()); println!(" {} {}", format!("+{}", name.as_ref()).bright_red(), desc.as_ref());
} }
} else { } else {
println!(" +{}", name.as_ref()) println!(" +{} {}", name.as_ref(), desc.as_ref())
} }
} }
} }
pub fn off(name: impl AsRef<str>, default: bool) pub fn off(name: impl AsRef<str>, default: bool, desc: impl AsRef<str>)
{ {
cfg_if! { cfg_if! {
if #[cfg(feature="colour")] { if #[cfg(feature="colour")] {
use recolored::Colorize; use recolored::Colorize;
if default { if default {
println!(" {}", format!("-{}", name.as_ref()).blue()); println!(" {} {}", format!("-{}", name.as_ref()).blue(), desc.as_ref().bright_black());
} else { } else {
println!(" {}", format!("-{}", name.as_ref()).bright_blue()); println!(" {} {}", format!("-{}", name.as_ref()).bright_blue(), desc.as_ref());
} }
} else { } else {
println!(" -{}", name.as_ref()) println!(" -{} {}", name.as_ref(), desc.as_ref())
} }
} }
} }
@ -96,10 +102,10 @@ mod feature
fn comp_flags() fn comp_flags()
{ {
check!(on "splash"); check!(on "splash", "Show splash-screen");
check!(on "colour"); check!(on "colour", "Enable coloured output");
check!(on "progress"); check!(on "progress", "Enable progress bar");
check!(off "threads"); check!(off "threads", "Enable threaded scheduler (usually not needed).");
} }
pub fn usage() -> ! pub fn usage() -> !
@ -111,15 +117,15 @@ pub fn usage() -> !
Usage: {prog} --help Usage: {prog} --help
OPTIONS: OPTIONS:
- Stop parsing args here.
--max-children <number> Max subprocesses allowed to live at once. Infinite by default. --max-children <number> Max subprocesses allowed to live at once. Infinite by default.
-m Limit subprocesses to number of CPU cores.
--recursive <number> Recurse up to `<number>` times. Must be at least `1`. Default is off. --recursive <number> Recurse up to `<number>` times. Must be at least `1`. Default is off.
-r Resurse infinitely. -r Resurse infinitely.
{extra} {extra}
- Stop parsing args here.
ENVIRONMENT VARS: ENVIRONMENT VARS:
LEANIFY=<process name> Path to leanify executable, defaults to looking in `PATH' if set. LEANIFY=<process name> Path to leanify executable, defaults to looking in `PATH' if set.
", prog = program_name(), extra = &extra_args()[..extra_args().len()-1]); ", prog = program_name(), extra = extra_args());
println!("Compiled with:"); println!("Compiled with:");
comp_flags(); comp_flags();
@ -132,6 +138,8 @@ pub enum Error {
#[cfg(not(nightly))] BadNumber(()), #[cfg(not(nightly))] BadNumber(()),
NoExist(PathBuf), NoExist(PathBuf),
Walking(dir::Error), Walking(dir::Error),
Other(&'static str),
NoFiles, NoFiles,
} }
impl std::error::Error for Error{} impl std::error::Error for Error{}
@ -158,6 +166,7 @@ impl std::fmt::Display for Error
Self::NoExist(path) => write!(f, "path {:?} does not exist", path), Self::NoExist(path) => write!(f, "path {:?} does not exist", path),
Self::NoFiles => write!(f, "need at least one argument"), Self::NoFiles => write!(f, "need at least one argument"),
Self::Walking(dir) => write!(f, "error walking directory structure(s): {}", dir), Self::Walking(dir) => write!(f, "error walking directory structure(s): {}", dir),
Self::Other(msg) => write!(f, "{}", msg),
} }
} }
} }
@ -178,6 +187,13 @@ pub struct Flags
{ {
/// Display the progress bar /// Display the progress bar
#[cfg(feature="progress")] pub progress: bool, #[cfg(feature="progress")] pub progress: bool,
/// Force use of colour
#[cfg(feature="colour")] pub coloured: Option<bool>,
/// Limit max children to this number
pub hard_limit: Option<NonZeroUsize>,
/// To shut the linter up when we have no elements here
_data: PhantomData<()>,
} }
impl Default for Flags impl Default for Flags
@ -187,6 +203,10 @@ impl Default for Flags
{ {
Self { Self {
#[cfg(feature="progress")] progress: true, #[cfg(feature="progress")] progress: true,
#[cfg(feature="colour")] coloured: None,
hard_limit: None,
_data: PhantomData,
} }
} }
} }
@ -254,6 +274,13 @@ where I: IntoIterator<Item=T>,
continue; continue;
} }
}, },
"-m" => {
cfg.flags.hard_limit = NonZeroUsize::new(num_cpus::get());
if cfg.flags.hard_limit.is_none() {
return Err(Error::Other("-m: Could not determine number of CPUs, try setting max children manually with `--max-children`"));
}
continue;
},
"--recursive" => { "--recursive" => {
if let Some(nzi) = args.next() { if let Some(nzi) = args.next() {
cfg.recursive = Some(nzi.parse()?); cfg.recursive = Some(nzi.parse()?);
@ -265,6 +292,16 @@ where I: IntoIterator<Item=T>,
cfg.flags.progress = false; cfg.flags.progress = false;
continue; continue;
}, },
#[cfg(feature="colour")]
"--no-colour" => {
cfg.flags.coloured = Some(false);
continue;
},
#[cfg(feature="colour")]
"--colour" => {
cfg.flags.coloured = Some(true);
continue;
},
"-r" => { "-r" => {
cfg.recursive = None; cfg.recursive = None;
}, },

@ -35,7 +35,27 @@ async fn work() -> Result<(), Box<dyn std::error::Error>>
let args = arg::parse_args().await.with_prefix("failed to parse args")?; let args = arg::parse_args().await.with_prefix("failed to parse args")?;
let leanify = leanify::find_binary().with_prefix("Couldn't find leanify binary")?; let leanify = leanify::find_binary().with_prefix("Couldn't find leanify binary")?;
work::work(&args.flags, leanify, args.files, args.max_children).await #[cfg(feature="colour")] {
match args.flags.coloured {
Some(x) => recolored::control::set_override(x),
_ => {},
}
}
work::work(&args.flags, leanify, args.files, match args.flags.hard_limit {
Some(hard_limit) => {
// We have hard limit
match args.max_children {
// We also have max children, and it's higher than hard limit,
Some(max_children) if max_children > hard_limit => Some(hard_limit),
// We have no max children
None => Some(hard_limit),
//Max children is lower than hard limit
_ => args.max_children,
}
},
_ => args.max_children,
}).await
} }

@ -1,10 +1,8 @@
use super::*; use super::*;
use std::{ use std::{
iter::FromIterator as _, iter::FromIterator as _,
sync::Arc,
borrow::{ borrow::{
Cow, Cow,
Borrow as _,
}, },
marker::{ marker::{
Send, Send,
@ -37,7 +35,6 @@ use futures::{
}, },
}; };
use termprogress::{ use termprogress::{
Display as _,
WithTitle, WithTitle,
ProgressBar, ProgressBar,
}; };

@ -8,17 +8,16 @@ use std::{
}, },
path::{Path,PathBuf,}, path::{Path,PathBuf,},
ffi::OsStr, ffi::OsStr,
borrow::BorrowMut,
iter,
}; };
#[allow(unused_imports)]
use std::iter;
use tokio::{ use tokio::{
prelude::*,
sync::{ sync::{
Semaphore, Semaphore,
mpsc, mpsc,
}, },
io,
}; };
#[allow(unused_imports)]
use futures::future::{ use futures::future::{
self, self,
join_all, join_all,
@ -83,7 +82,7 @@ async fn do_work(process: impl AsRef<Path>, file: impl AsRef<OsStr>, mut prog: P
} }
} }
pub async fn work<I,T,U>(flags: &arg::Flags, process: U, files: I, children: Option<NonZeroUsize>) -> Result<(), Box<dyn std::error::Error>> pub async fn work<I,T,U>(#[allow(unused_variables)] flags: &arg::Flags, process: U, files: I, children: Option<NonZeroUsize>) -> Result<(), Box<dyn std::error::Error>>
where I: IntoIterator<Item=T>, where I: IntoIterator<Item=T>,
<I as IntoIterator>::IntoIter: ExactSizeIterator, <I as IntoIterator>::IntoIter: ExactSizeIterator,
T: AsRef<OsStr> + Send + Sync + 'static + Clone, T: AsRef<OsStr> + Send + Sync + 'static + Clone,

Loading…
Cancel
Save