This commit is contained in:
shenjack 2024-04-27 21:20:45 +08:00
parent b2520dba60
commit 38f09583ae
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 28 additions and 14 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -49,6 +49,10 @@ pub fn cacl(config: CacluateConfig, id: u64, outfile: &PathBuf) {
let mut start_time = std::time::Instant::now();
let mut k: u64 = 0;
let mut get_count: u32 = 0;
// 设置线程亲和性
if let Some(core_affinity) = config.core_affinity {
crate::set_thread2core(core_affinity)
}
// 提前准备好 team_namer
let team_namer = TeamNamer::new(&config.team).unwrap();

View File

@ -63,6 +63,24 @@ impl Command {
}
}
pub fn set_thread2core(core: usize) {
#[cfg(windows)]
unsafe {
use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadAffinityMask};
let thread_id = GetCurrentThread();
let core_mask = core;
match SetThreadAffinityMask(thread_id, core_mask) {
0 => warn!("设置线程亲和性失败 {}", std::io::Error::last_os_error()),
x => info!("设置线程亲和性成功 {}", x),
}
}
#[cfg(linux)]
{
warn!("Linux 下不支持设置线程亲和性 (未实现) {}", core)
}
}
fn main() {
tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init();
let mut cli_arg = Command::parse();
@ -85,17 +103,7 @@ fn main() {
if cli_arg.bench {
cli_arg.thread_count = 1;
#[cfg(windows)]
unsafe {
use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadAffinityMask};
let thread_id = GetCurrentThread();
let core_mask = 0x02;
match SetThreadAffinityMask(thread_id, core_mask) {
0 => warn!("设置线程亲和性失败 {}", std::io::Error::last_os_error()),
x => info!("设置线程亲和性成功 {}", x),
}
}
set_thread2core(cli_arg.bench_core);
}
info!("开始: {} 结尾: {}", cli_arg.start, cli_arg.end);
@ -114,7 +122,9 @@ fn main() {
let mut n = 0;
for i in 0..cli_arg.thread_count {
n += 1;
let config = cli_arg.as_cacl_config();
let mut config = cli_arg.as_cacl_config();
// 核心亲和性: n, n+1
config.core_affinity = Some((0001 << i) + (0001 << (i + 1)));
let out_path = out_path.clone();
let thread_name = format!("thread_{}", i);
threads.push(std::thread::spawn(move || {