Skip to content

Commit

Permalink
Add solver config to control default the solver and displayed output (#…
Browse files Browse the repository at this point in the history
…1060)

Adds --solver-to-show argument, and environment variables for controlling the solves. Adds config file fields for solver to run and solver to show settings, and adds constants for the solver names.

Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
  • Loading branch information
dcookspi authored Aug 6, 2024
1 parent 106be3a commit e25d2bd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
39 changes: 31 additions & 8 deletions crates/spk-cli/common/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,10 +1008,19 @@ pub enum SolverToRun {
Cli,
/// Run and show output from the "impossible requests" checking solver
Checks,
/// Run both solvers, showing the output from the basic solver
/// Run both solvers, showing the output from the basic solver,
/// unless overridden with --solver-to-show
All,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum SolverToShow {
/// Show output from the basic solver
Cli,
/// Show output from the "impossible requests" checking solver
Checks,
}

impl From<SolverToRun> for MultiSolverKind {
fn from(item: SolverToRun) -> MultiSolverKind {
match item {
Expand All @@ -1022,6 +1031,15 @@ impl From<SolverToRun> for MultiSolverKind {
}
}

impl From<SolverToShow> for MultiSolverKind {
fn from(item: SolverToShow) -> MultiSolverKind {
match item {
SolverToShow::Cli => MultiSolverKind::Unchanged,
SolverToShow::Checks => MultiSolverKind::AllImpossibleChecks,
}
}
}

#[derive(Args, Clone)]
pub struct DecisionFormatterSettings {
/// If true, display solver time and stats after each solve
Expand Down Expand Up @@ -1073,20 +1091,24 @@ pub struct DecisionFormatterSettings {
#[clap(long)]
pub status_bar: bool,

/// Control what solver(s) are used and what output is shown.
/// Control what solver(s) are used.
///
/// There are currently two modes for the solver, one that is faster when
/// there are few problems encountered looking for packages (cli) and one
/// that is faster when it is difficult to find a set of packages that
/// satisfy a request (checks).
///
/// By default, both solvers are run in parallel and the result is taken
/// from the first one that finishes. However, the output from the (cli)
/// solver is displayed even if the result ultimately comes from the
/// (checks) solver. To see the output from the (checks) solver, use
/// `--solver-to-run checks` to see that output.
#[clap(long, value_enum, default_value_t = SolverToRun::All)]
/// By default, both solvers are run in parallel and the result is
/// taken from the first one that finishes, and the output from
/// the (cli) solver is displayed. even if the result ultimately
/// comes from the (checks) solver. To run only one solver, use
/// `--solver-to-run <cli|checks>`.
#[clap(long, env = "SPK_SOLVER__SOLVER_TO_RUN", value_enum, default_value_t = SolverToRun::All)]
pub solver_to_run: SolverToRun,
/// Control what solver's output is shown when multiple solvers
/// (all) are being run.
#[clap(long, env = "SPK_SOLVER__SOLVER_TO_SHOW", value_enum, default_value_t = SolverToShow::Cli)]
pub solver_to_show: SolverToShow,

/// Display a report on of the search space size for the resolved solution.
#[clap(long)]
Expand Down Expand Up @@ -1144,6 +1166,7 @@ impl DecisionFormatterSettings {
.with_max_frequent_errors(self.max_frequent_errors)
.with_status_bar(self.status_bar)
.with_solver_to_run(self.solver_to_run.into())
.with_solver_to_show(self.solver_to_show.into())
.with_search_space_size(self.show_search_size)
.with_stop_on_block(self.stop_on_block)
.with_step_on_block(self.step_on_block)
Expand Down
6 changes: 6 additions & 0 deletions crates/spk-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ pub struct Solver {
/// Comma-separated list of option names to promote to the front of the
/// resolve order.
pub request_priority_order: String,

/// Name of the solver, or all, to run when performing a solve
pub solver_to_run: String,

/// Name of the solver whose output to show when multiple solvers are being run.
pub solver_to_show: String,
}

#[derive(Clone, Default, Debug, Deserialize, Serialize)]
Expand Down
49 changes: 44 additions & 5 deletions crates/spk-solve/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ use crate::{
const STOP_ON_BLOCK_FLAG: &str = "--stop-on-block";
const BY_USER: &str = "by user";

const CLI_SOLVER: &str = "cli";
const IMPOSSIBLE_CHECKS_SOLVER: &str = "check";
const ALL_SOLVERS: &str = "all";

static USER_CANCELLED: Lazy<Arc<AtomicBool>> = Lazy::new(|| {
// Initialise the USER_CANCELLED value
let b = Arc::new(AtomicBool::new(false));
Expand Down Expand Up @@ -486,6 +490,7 @@ pub struct DecisionFormatterBuilder {
max_frequent_errors: usize,
status_bar: bool,
solver_to_run: MultiSolverKind,
solver_to_show: MultiSolverKind,
show_search_space_size: bool,
compare_solvers: bool,
stop_on_block: bool,
Expand All @@ -507,6 +512,7 @@ impl Default for DecisionFormatterBuilder {
max_frequent_errors: 0,
status_bar: false,
solver_to_run: MultiSolverKind::Unchanged,
solver_to_show: MultiSolverKind::Unchanged,
show_search_space_size: false,
compare_solvers: false,
stop_on_block: false,
Expand All @@ -531,6 +537,8 @@ impl DecisionFormatterBuilder {
timeout: cfg.solve_timeout,
long_solves_threshold: cfg.long_solve_threshold,
max_frequent_errors: cfg.max_frequent_errors,
solver_to_run: MultiSolverKind::from_config_run_value(&cfg.solver_to_run),
solver_to_show: MultiSolverKind::from_config_output_value(&cfg.solver_to_show),
..Default::default()
}
}
Expand Down Expand Up @@ -590,6 +598,11 @@ impl DecisionFormatterBuilder {
self
}

pub fn with_solver_to_show(&mut self, kind: MultiSolverKind) -> &mut Self {
self.solver_to_show = kind;
self
}

pub fn with_search_space_size(&mut self, enable: bool) -> &mut Self {
self.show_search_space_size = enable;
self
Expand Down Expand Up @@ -653,6 +666,7 @@ impl DecisionFormatterBuilder {
max_frequent_errors: self.max_frequent_errors,
status_bar: self.status_bar,
solver_to_run: self.solver_to_run.clone(),
solver_to_show: self.solver_to_show.clone(),
show_search_space_size: self.show_search_space_size,
compare_solvers: self.compare_solvers,
stop_on_block: self.stop_on_block,
Expand Down Expand Up @@ -715,6 +729,7 @@ pub(crate) struct DecisionFormatterSettings {
pub(crate) max_frequent_errors: usize,
pub(crate) status_bar: bool,
pub(crate) solver_to_run: MultiSolverKind,
pub(crate) solver_to_show: MultiSolverKind,
pub(crate) show_search_space_size: bool,
pub(crate) compare_solvers: bool,
pub(crate) stop_on_block: bool,
Expand Down Expand Up @@ -746,9 +761,32 @@ impl MultiSolverKind {
/// Return the command line option value for this MultiSolveKind
fn cli_name(&self) -> &'static str {
match self {
MultiSolverKind::Unchanged => "cli",
MultiSolverKind::AllImpossibleChecks => "checks",
MultiSolverKind::All => "all",
MultiSolverKind::Unchanged => CLI_SOLVER,
MultiSolverKind::AllImpossibleChecks => IMPOSSIBLE_CHECKS_SOLVER,
MultiSolverKind::All => ALL_SOLVERS,
}
}

/// Return the MultiSolverKind setting for a solver to run from a
/// config value. This will fallback to MultiSolverKind:Cli if the
/// given value is invalid.
fn from_config_run_value(value: &str) -> MultiSolverKind {
match value.to_lowercase().as_ref() {
CLI_SOLVER => MultiSolverKind::Unchanged,
IMPOSSIBLE_CHECKS_SOLVER => MultiSolverKind::AllImpossibleChecks,
ALL_SOLVERS => MultiSolverKind::All,
_ => MultiSolverKind::Unchanged,
}
}

/// Return the MultiSolverKind setting for a solver to output from a
/// config value. This will fallback to MultiSolverKind:Cli if the
/// given value is invalid.
fn from_config_output_value(value: &str) -> MultiSolverKind {
match value.to_lowercase().as_ref() {
CLI_SOLVER => MultiSolverKind::Unchanged,
IMPOSSIBLE_CHECKS_SOLVER => MultiSolverKind::AllImpossibleChecks,
_ => MultiSolverKind::Unchanged,
}
}
}
Expand Down Expand Up @@ -807,6 +845,7 @@ impl DecisionFormatter {
max_frequent_errors: 5,
status_bar: false,
solver_to_run: MultiSolverKind::Unchanged,
solver_to_show: MultiSolverKind::Unchanged,
show_search_space_size: false,
compare_solvers: false,
stop_on_block: false,
Expand Down Expand Up @@ -930,7 +969,7 @@ impl DecisionFormatter {
for solver_settings in solvers {
let mut task_formatter = self.clone();
if self.settings.solver_to_run.is_multi()
&& solver_settings.solver_kind != MultiSolverKind::Unchanged
&& self.settings.solver_to_show != solver_settings.solver_kind
{
// Hide the output from all the solvers except the
// unchanged one. The output from the unchanged solver
Expand Down Expand Up @@ -1086,7 +1125,7 @@ impl DecisionFormatter {
};
let name = solver_kind.cli_name();

tracing::info!("The {solver_kind} solver found {solver_outcome}, but its output was disabled. To see its output, rerun the spk command with '--solver-to-run {name}'" );
tracing::info!("The {solver_kind} solver found {solver_outcome}, but its output was disabled. To see its output, rerun the spk command with '--solver-to-show {name}' or `--solver-to-run {name}`" );
}

if self.settings.compare_solvers {
Expand Down

0 comments on commit e25d2bd

Please sign in to comment.