From 3900790f03c39aee15c91dbe752a0df7d2540c53 Mon Sep 17 00:00:00 2001 From: Revertron Date: Sat, 10 Apr 2021 14:03:52 +0200 Subject: [PATCH] Returned low thread priorty as it seems after thorough tests that there is an impact afterall. But this functionality is now controlled by option 'mining.lower'. --- Cargo.toml | 4 ++++ alfis.toml | 6 ++++-- src/blockchain/chain.rs | 2 +- src/commons/mod.rs | 22 ++++++++++++++++++++++ src/keys.rs | 10 +++++++--- src/miner.rs | 6 +++++- src/settings.rs | 4 +++- 7 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7d3e764..6935e8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,10 @@ open = { version = "1.6.0", optional = true } [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.7", features = ["impl-default", "wincon", "shellscalingapi"]} +thread-priority = "0.2.1" + +[target.'cfg(target_os = "linux")'.dependencies] +thread-priority = "0.2.1" [build-dependencies] minreq = { version = "2.3.1", features = ["punycode", "https-rustls"] } diff --git a/alfis.toml b/alfis.toml index b001ee3..5009036 100644 --- a/alfis.toml +++ b/alfis.toml @@ -1,5 +1,5 @@ # The hash of first block in a chain to know with which nodes to work -origin = "00000102C2F9BFD2803284D93327F089D60FC72A06F19AF2384567F2646B8348" +origin = "0AE588D62D710422A7972EA1E8A659CC8E93DB59489ACE32C499CD279B000000" # A path to your key file to load autamatically key_file = "default.key" @@ -31,4 +31,6 @@ forwarders = ["94.140.14.14:53", "94.140.15.15:53"] #Mining options [mining] # How many CPU threads to spawn for mining, zero = number of CPU cores -threads = 0 \ No newline at end of file +threads = 0 +# Set lower priority for mining threads +lower = true \ No newline at end of file diff --git a/src/blockchain/chain.rs b/src/blockchain/chain.rs index f3e0ab6..b2ec367 100644 --- a/src/blockchain/chain.rs +++ b/src/blockchain/chain.rs @@ -65,7 +65,7 @@ impl Chain { /// Reads options from DB or initializes and writes them to DB if not found fn init_db(&mut self) { let options = self.get_options(); - if !self.origin.is_zero() && !self.origin.is_zero() && self.origin.to_string() != options.origin { + if !self.origin.is_zero() && !options.origin.is_empty() && self.origin.to_string() != options.origin { self.clear_db(); } if options.version < DB_VERSION { diff --git a/src/commons/mod.rs b/src/commons/mod.rs index 4190587..7e0851c 100644 --- a/src/commons/mod.rs +++ b/src/commons/mod.rs @@ -5,6 +5,9 @@ pub mod constants; pub use constants::*; use std::net::IpAddr; +#[cfg(not(target_os = "macos"))] +use thread_priority::*; + /// Convert bytes array to HEX format pub fn to_hex(buf: &[u8]) -> String { let mut result = String::new(); @@ -91,6 +94,25 @@ pub fn is_yggdrasil(addr: &IpAddr) -> bool { false } +#[cfg(target_os = "windows")] +#[allow(unused_variables)] +pub fn setup_miner_thread(cpu: u32) { + let _ = set_current_thread_priority(ThreadPriority::Min); + //let _ = set_current_thread_ideal_processor(IdealProcessor::from(cpu)); +} + +#[cfg(target_os = "linux")] +#[allow(unused_variables)] +pub fn setup_miner_thread(cpu: u32) { + let _ = set_current_thread_priority(ThreadPriority::Min); +} + +#[cfg(target_os = "macos")] +#[allow(unused_variables)] +pub fn setup_miner_thread(cpu: u32) { + // MacOS is not supported by thread_priority crate +} + #[cfg(test)] mod test { use crate::{check_domain, is_yggdrasil}; diff --git a/src/keys.rs b/src/keys.rs index 1ff2157..87bdede 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -16,7 +16,7 @@ use ed25519_dalek::Keypair; use log::{debug, error, info, trace, warn}; use crate::blockchain::hash_utils::*; -use crate::Context; +use crate::{Context, setup_miner_thread}; use crate::event::Event; use crate::commons::KEYSTORE_DIFFICULTY; use crate::bytes::Bytes; @@ -172,18 +172,22 @@ pub fn check_public_key_strength(key: &Bytes, strength: u32) -> bool { pub fn create_key(context: Arc>) { let mining = Arc::new(AtomicBool::new(true)); let miners_count = Arc::new(AtomicUsize::new(0)); - { context.lock().unwrap().bus.post(Event::KeyGeneratorStarted); } + context.lock().unwrap().bus.post(Event::KeyGeneratorStarted); + let lower = context.lock().unwrap().settings.mining.lower; let threads = context.lock().unwrap().settings.mining.threads; let threads = match threads { 0 => num_cpus::get(), _ => threads }; - for _cpu in 0..threads { + for cpu in 0..threads { let context = Arc::clone(&context); let mining = mining.clone(); let miners_count = miners_count.clone(); thread::spawn(move || { miners_count.fetch_add(1, atomic::Ordering::SeqCst); + if lower { + setup_miner_thread(cpu as u32); + } match generate_key(KEYSTORE_DIFFICULTY, mining.clone()) { None => { debug!("Keystore mining finished"); diff --git a/src/miner.rs b/src/miner.rs index 115ca2e..dff18fc 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -8,7 +8,7 @@ use chrono::Utc; use log::{debug, error, info, trace, warn}; use num_cpus; -use crate::{Block, Bytes, Context, Keystore}; +use crate::{Block, Bytes, Context, Keystore, setup_miner_thread}; use crate::commons::{CHAIN_VERSION, LOCKER_DIFFICULTY, KEYSTORE_DIFFICULTY}; use crate::blockchain::types::BlockQuality; use crate::blockchain::hash_utils::*; @@ -141,6 +141,7 @@ impl Miner { context.lock().unwrap().bus.post(Event::MinerStarted); let thread_spawn_interval = Duration::from_millis(10); let live_threads = Arc::new(AtomicU32::new(0u32)); + let lower = context.lock().unwrap().settings.mining.lower; let cpus = num_cpus::get(); let threads = context.lock().unwrap().settings.mining.threads; let threads = match threads { @@ -155,6 +156,9 @@ impl Miner { let live_threads = Arc::clone(&live_threads); thread::spawn(move || { live_threads.fetch_add(1, Ordering::SeqCst); + if lower { + setup_miner_thread(cpu as u32); + } let full = job.block.transaction.is_some(); match find_hash(Arc::clone(&context), job.block, Arc::clone(&mining), cpu) { None => { diff --git a/src/settings.rs b/src/settings.rs index c521e7e..563a73d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -84,7 +84,9 @@ impl Default for Dns { #[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct Mining { #[serde(default)] - pub threads: usize + pub threads: usize, + #[serde(default)] + pub lower: bool } #[derive(Clone, Debug, Serialize, Deserialize)]