From c3b784e6e995e6e791e09e9391f27a60c11b8b2b Mon Sep 17 00:00:00 2001 From: Aloxaf Date: Fri, 16 Aug 2024 15:47:41 +0800 Subject: [PATCH 1/2] fix: improve core binding --- src/benchmark.rs | 11 ++++------- src/mine.rs | 6 +----- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/benchmark.rs b/src/benchmark.rs index bb6972bf..4a1e0e03 100644 --- a/src/benchmark.rs +++ b/src/benchmark.rs @@ -22,6 +22,7 @@ impl Miner { let core_ids = core_affinity::get_core_ids().unwrap(); let handles: Vec<_> = core_ids .into_iter() + .take(args.cores as usize) .map(|i| { std::thread::spawn({ move || { @@ -31,15 +32,11 @@ impl Miner { .saturating_mul(i.id as u64); let mut nonce = first_nonce; let mut memory = equix::SolverMemory::new(); - loop { - // Return if core should not be used - if (i.id as u64).ge(&args.cores) { - return 0; - } - // Pin to core - let _ = core_affinity::set_for_current(i); + // Pin to core + let _ = core_affinity::set_for_current(i); + loop { // Create hash let _hx = drillx::hash_with_memory( &mut memory, diff --git a/src/mine.rs b/src/mine.rs index 90e597a1..cb2f0a25 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -101,6 +101,7 @@ impl Miner { let core_ids = core_affinity::get_core_ids().unwrap(); let handles: Vec<_> = core_ids .into_iter() + .take(cores as usize) .map(|i| { let global_best_difficulty = Arc::clone(&global_best_difficulty); std::thread::spawn({ @@ -108,11 +109,6 @@ impl Miner { let progress_bar = progress_bar.clone(); let mut memory = equix::SolverMemory::new(); move || { - // Return if core should not be used - if (i.id as u64).ge(&cores) { - return (0, 0, Hash::default()); - } - // Pin to core let _ = core_affinity::set_for_current(i); From 0c7c8920f635e789a0e90a824d0fa2bbca4de0ff Mon Sep 17 00:00:00 2001 From: Aloxaf Date: Fri, 16 Aug 2024 15:54:36 +0800 Subject: [PATCH 2/2] fix: first nonce --- Cargo.lock | 197 ++++++++++++++++++++++++++++++++++++++--------- Cargo.toml | 1 + src/benchmark.rs | 3 +- src/mine.rs | 3 +- 4 files changed, 165 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2aeba29c..cd37ac81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,7 +42,7 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cipher", "cpufeatures", "opaque-debug", @@ -80,7 +80,7 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "getrandom 0.2.15", "once_cell", "version_check", @@ -458,7 +458,7 @@ checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ "addr2line", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide", "object", @@ -546,7 +546,7 @@ dependencies = [ "arrayref", "arrayvec", "cc", - "cfg-if", + "cfg-if 1.0.0", "constant_time_eq", "digest 0.10.7", ] @@ -828,6 +828,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -1001,7 +1007,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen", ] @@ -1037,16 +1043,32 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" +[[package]] +name = "core-foundation" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171" +dependencies = [ + "core-foundation-sys 0.7.0", + "libc", +] + [[package]] name = "core-foundation" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" dependencies = [ - "core-foundation-sys", + "core-foundation-sys 0.8.6", "libc", ] +[[package]] +name = "core-foundation-sys" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -1079,7 +1101,7 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1241,7 +1263,7 @@ version = "5.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "hashbrown 0.14.5", "lock_api", "once_cell", @@ -1341,7 +1363,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "dirs-sys-next", ] @@ -1489,7 +1511,7 @@ version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1734,7 +1756,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "libc", "wasi 0.9.0+wasi-snapshot-preview1", @@ -1747,7 +1769,7 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", @@ -1873,6 +1895,53 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "heim-common" +version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f5a8c66797cb950fdbf6ea1d750f5ffe687e4cb13713fe1f3214d693ffd72a4" +dependencies = [ + "cfg-if 0.1.10", + "core-foundation 0.7.0", + "futures-core", + "futures-util", + "lazy_static", + "libc", + "mach", + "nix 0.17.0", + "pin-utils", + "uom", + "winapi", +] + +[[package]] +name = "heim-cpu" +version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc191b97ede884083df6d4c0ac47246b86782a0d253d001d7b4610f6ec9cb21f" +dependencies = [ + "cfg-if 0.1.10", + "heim-common", + "heim-runtime", + "lazy_static", + "libc", + "mach", + "winapi", +] + +[[package]] +name = "heim-runtime" +version = "0.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1504955d04ec7cf8df8bbe541a8636f619450af6f618a6a6111dfecc79743ee5" +dependencies = [ + "cfg-if 0.1.10", + "futures-channel", + "heim-common", + "lazy_static", + "threadpool", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -2105,7 +2174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" dependencies = [ "android_system_properties", - "core-foundation-sys", + "core-foundation-sys 0.8.6", "iana-time-zone-haiku", "js-sys", "wasm-bindgen", @@ -2192,7 +2261,7 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -2374,6 +2443,15 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "mach" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.7.4" @@ -2481,6 +2559,19 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nix" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +dependencies = [ + "bitflags 1.3.2", + "cc", + "cfg-if 0.1.10", + "libc", + "void", +] + [[package]] name = "nix" version = "0.26.4" @@ -2488,7 +2579,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" dependencies = [ "bitflags 1.3.2", - "cfg-if", + "cfg-if 1.0.0", "libc", "memoffset 0.7.1", "pin-utils", @@ -2514,7 +2605,7 @@ dependencies = [ "num-complex", "num-integer", "num-iter", - "num-rational", + "num-rational 0.2.4", "num-traits", ] @@ -2609,6 +2700,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -2713,7 +2815,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" dependencies = [ "bitflags 2.6.0", - "cfg-if", + "cfg-if 1.0.0", "foreign-types", "libc", "once_cell", @@ -2784,6 +2886,7 @@ dependencies = [ "core_affinity", "drillx", "futures", + "heim-cpu", "indicatif", "num_cpus", "ore-api", @@ -2839,7 +2942,7 @@ version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall", "smallvec", @@ -2955,7 +3058,7 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "opaque-debug", "universal-hash", @@ -3385,7 +3488,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" dependencies = [ "cc", - "cfg-if", + "cfg-if 1.0.0", "getrandom 0.2.15", "libc", "spin 0.9.8", @@ -3580,8 +3683,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" dependencies = [ "bitflags 2.6.0", - "core-foundation", - "core-foundation-sys", + "core-foundation 0.9.4", + "core-foundation-sys 0.8.6", "libc", "security-framework-sys", ] @@ -3592,7 +3695,7 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" dependencies = [ - "core-foundation-sys", + "core-foundation-sys 0.8.6", "libc", ] @@ -3696,7 +3799,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug", @@ -3708,7 +3811,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.7", ] @@ -3720,7 +3823,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug", @@ -3732,7 +3835,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.7", ] @@ -4037,7 +4140,7 @@ dependencies = [ "clap 3.2.25", "crossbeam-channel", "log", - "nix", + "nix 0.26.4", "rand 0.8.5", "serde", "serde_derive", @@ -4065,7 +4168,7 @@ dependencies = [ "lazy_static", "libc", "log", - "nix", + "nix 0.26.4", "rand 0.8.5", "rayon", "rustc_version", @@ -4392,7 +4495,7 @@ dependencies = [ "itertools", "libc", "log", - "nix", + "nix 0.26.4", "pem", "percentage", "pkcs8", @@ -4915,7 +5018,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ "bitflags 1.3.2", - "core-foundation", + "core-foundation 0.9.4", "system-configuration-sys", ] @@ -4925,7 +5028,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" dependencies = [ - "core-foundation-sys", + "core-foundation-sys 0.8.6", "libc", ] @@ -4935,7 +5038,7 @@ version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "rustix", "windows-sys 0.52.0", @@ -4985,6 +5088,15 @@ dependencies = [ "syn 2.0.70", ] +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + [[package]] name = "time" version = "0.3.36" @@ -5369,6 +5481,17 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "uom" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e76503e636584f1e10b9b3b9498538279561adcef5412927ba00c2b32c4ce5ed" +dependencies = [ + "num-rational 0.3.2", + "num-traits", + "typenum", +] + [[package]] name = "uriparse" version = "0.6.4" @@ -5464,7 +5587,7 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -5489,7 +5612,7 @@ version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -5743,7 +5866,7 @@ version = "0.50.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "windows-sys 0.48.0", ] @@ -5753,7 +5876,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "windows-sys 0.48.0", ] diff --git a/Cargo.toml b/Cargo.toml index d908dc09..6a98bd57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ tokio = "1.35.1" url = "2.5" tokio-tungstenite = "0.16" serde = { version = "1.0", features = ["derive"] } +heim-cpu = "0.0.11" # [patch.crates-io] # drillx = { path = "../drillx/drillx" } diff --git a/src/benchmark.rs b/src/benchmark.rs index 4a1e0e03..bc64b4f8 100644 --- a/src/benchmark.rs +++ b/src/benchmark.rs @@ -20,6 +20,7 @@ impl Miner { TEST_DURATION )); let core_ids = core_affinity::get_core_ids().unwrap(); + let core_num = heim_cpu::logical_count().await.unwrap(); let handles: Vec<_> = core_ids .into_iter() .take(args.cores as usize) @@ -28,7 +29,7 @@ impl Miner { move || { let timer = Instant::now(); let first_nonce = u64::MAX - .saturating_div(args.cores) + .saturating_div(core_num) .saturating_mul(i.id as u64); let mut nonce = first_nonce; let mut memory = equix::SolverMemory::new(); diff --git a/src/mine.rs b/src/mine.rs index cb2f0a25..41f5b9c3 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -99,6 +99,7 @@ impl Miner { let global_best_difficulty = Arc::new(RwLock::new(0u32)); progress_bar.set_message("Mining..."); let core_ids = core_affinity::get_core_ids().unwrap(); + let core_num = heim_cpu::logical_count().await.unwrap(); let handles: Vec<_> = core_ids .into_iter() .take(cores as usize) @@ -114,7 +115,7 @@ impl Miner { // Start hashing let timer = Instant::now(); - let mut nonce = u64::MAX.saturating_div(cores).saturating_mul(i.id as u64); + let mut nonce = u64::MAX.saturating_div(core_num).saturating_mul(i.id as u64); let mut best_nonce = nonce; let mut best_difficulty = 0; let mut best_hash = Hash::default();