支持进程亲和性设置

This commit is contained in:
shenjack-5600u 2024-04-28 06:18:26 +08:00
parent 8ab812eabd
commit 2f416cb153
Signed by: shenjack
GPG Key ID: FDF9864E11C7E79F
5 changed files with 32 additions and 3 deletions

2
Cargo.lock generated
View File

@ -1831,7 +1831,7 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]] [[package]]
name = "tswn" name = "tswn"
version = "0.1.8" version = "0.1.9"
dependencies = [ dependencies = [
"base16384", "base16384",
"chrono", "chrono",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "tswn" name = "tswn"
description = "tool shenjack work shop namerena" description = "tool shenjack work shop namerena"
version = "0.1.8" version = "0.1.9"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1 +1,2 @@
runs runs
namerena

7
miner/docs/updates.md Normal file
View File

@ -0,0 +1,7 @@
# tswn 的 更新
## 0.1.x
### 0.1.6~9
更新了一大堆核心亲和性相关的东西

View File

@ -81,6 +81,23 @@ pub fn set_thread2core(core: usize) {
} }
} }
pub fn set_process_cores(cores: usize) {
#[cfg(windows)]
unsafe {
use windows_sys::Win32::System::Threading::{SetProcessAffinityMask, GetCurrentProcess};
let process = GetCurrentProcess();
let core_mask = cores;
match SetProcessAffinityMask(process, core_mask) {
0 => warn!("设置进程亲和性失败 {}", std::io::Error::last_os_error()),
x => info!("设置进程亲和性成功 {}", x),
}
}
#[cfg(linux)]
{
warn!("Linux 下不支持设置进程亲和性 (未实现) {}", cores)
}
}
fn main() { fn main() {
tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init(); tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init();
let mut cli_arg = Command::parse(); let mut cli_arg = Command::parse();
@ -113,14 +130,17 @@ fn main() {
info!("开始 benchmark"); info!("开始 benchmark");
let mut config = cli_arg.as_cacl_config(); let mut config = cli_arg.as_cacl_config();
config.core_affinity = Some(0001 << cli_arg.bench_core); config.core_affinity = Some(0001 << cli_arg.bench_core);
set_process_cores(cli_arg.bench_core);
cacluate::cacl(config, 1, &out_path); cacluate::cacl(config, 1, &out_path);
} else { } else {
let mut n = 0; let mut n = 0;
let mut cores = 0;
for i in 0..cli_arg.thread_count { for i in 0..cli_arg.thread_count {
n += 1; n += 1;
let mut config = cli_arg.as_cacl_config(); let mut config = cli_arg.as_cacl_config();
// 核心亲和性: n, n+1 // 核心亲和性: n, n+1
config.core_affinity = Some((0001 << i) + (0001 << (i + 1))); config.core_affinity = Some((0001 << i) + (0001 << (i + 1)));
cores |= (0001 << i) + (0001 << (i + 1));
let out_path = out_path.clone(); let out_path = out_path.clone();
let thread_name = format!("thread_{}", n); let thread_name = format!("thread_{}", n);
threads.push(std::thread::spawn(move || { threads.push(std::thread::spawn(move || {
@ -129,6 +149,7 @@ fn main() {
info!("线程 {} 结束计算", thread_name); info!("线程 {} 结束计算", thread_name);
})); }));
} }
set_process_cores(cores);
} }
info!("开始计算"); info!("开始计算");