Skip to content

Commit

Permalink
add optional --cpu-priority unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Sep 23, 2023
1 parent 4a520f6 commit f99905f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 79 deletions.
109 changes: 39 additions & 70 deletions Cargo.lock

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

14 changes: 13 additions & 1 deletion src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
time::Duration,
};

use clap::{builder::PathBufValueParser, ArgAction, Parser};
use clap::{builder::PathBufValueParser, ArgAction, Parser, ValueEnum};
use configparser::ini::Ini;
use url::Url;

Expand Down Expand Up @@ -53,6 +53,11 @@ pub struct Opt {
#[arg(long, alias = "threads", global = true)]
pub cores: Option<Cores>,

/// Override CPU scheduling priorty of fishnet and engine processes.
/// Very low by default.
#[arg(long, global = true)]
pub cpu_priority: Option<CpuPriority>,

/// Maximum backoff time. The client will use randomized expontential
/// backoff when repeatedly receiving no job. Defaults to 30s.
#[arg(long, global = true)]
Expand Down Expand Up @@ -125,6 +130,13 @@ pub struct Verbose {
pub level: u8,
}

#[derive(Debug, Default, Copy, Clone, ValueEnum)]
pub enum CpuPriority {
Unchanged,
#[default]
Min,
}

#[derive(Debug, Clone)]
pub struct Key(pub String);

Expand Down
17 changes: 11 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use tokio::{

use crate::{
assets::{Assets, ByEngineFlavor, Cpu, EngineFlavor},
configure::{Command, Cores, Opt},
configure::{Command, Cores, CpuPriority, Opt},
ipc::{Position, PositionFailed, Pull},
logger::{Logger, ProgressAt},
stockfish::StockfishInit,
Expand Down Expand Up @@ -170,16 +170,21 @@ async fn run(opt: Opt, logger: &Logger) {
rx
};

// Set scheduling priority.
match opt.cpu_priority.unwrap_or_default() {
CpuPriority::Unchanged => (),
CpuPriority::Min => {
if let Err(err) = set_current_thread_priority(ThreadPriority::Min) {
logger.warn(&format!("Failed to decrease CPU priority: {err:?}"));
}
}
}

let mut restart = None;
let mut up_to_date = Instant::now();
let mut summarized = Instant::now();
let mut shutdown_soon = false;

// Decrease CPU priority
if set_current_thread_priority(ThreadPriority::Min).is_err() {
logger.warn("Failed to set CPU priority");
};

loop {
// Check for updates from time to time.
let now = Instant::now();
Expand Down
2 changes: 0 additions & 2 deletions src/systemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub fn systemd_system(opt: Opt) {
"User={}",
env::var("USER").unwrap_or_else(|_| "XXX".to_owned())
);
println!("Nice=5");
println!("CapabilityBoundingSet=");
println!("PrivateTmp=true");
println!("PrivateDevices=true");
Expand Down Expand Up @@ -64,7 +63,6 @@ pub fn systemd_user(opt: Opt) {
println!("ExecStart={} run", exec_start(Invocation::Absolute, &opt));
println!("KillMode=mixed");
println!("WorkingDirectory=/tmp");
println!("Nice=5");
println!("PrivateTmp=true");
println!("DevicePolicy=closed");
if opt.auto_update
Expand Down

0 comments on commit f99905f

Please sign in to comment.