先写这么多

This commit is contained in:
shenjack 2024-06-23 21:19:51 +08:00
parent a2e5e8b1ae
commit a958e594a0
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 91 additions and 32 deletions

59
Cargo.lock generated
View File

@ -400,6 +400,62 @@ dependencies = [
"libc",
]
[[package]]
name = "crossbeam"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "d3d12"
version = "0.19.0"
@ -1837,9 +1893,10 @@ dependencies = [
"chrono",
"clap",
"colored",
"crossbeam",
"tracing",
"tracing-subscriber",
"windows-sys 0.48.0",
"windows-sys 0.52.0",
]
[[package]]

View File

@ -14,6 +14,7 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["time"] }
chrono = "0.4"
colored = "2.1.0"
crossbeam = { version = "0.8.4", features = ["crossbeam-channel"] }
# windows 下额外指定一个 windows 依赖 (微软赛高!)
# 用于在 bench 状态下指定核心(就是闲得)

View File

@ -23,12 +23,10 @@ pub fn gen_name(id: u64) -> String {
}
pub struct CacluateConfig {
/// 开始的 id
pub start: u64,
/// 结束的 id
pub end: u64,
/// 计算 range
pub range: std::ops::Range<u64>,
/// 线程数
pub thread_count: u32,
pub thread_id: u32,
/// 八围预期值
pub prop_expect: u32,
/// qp 预期值
@ -44,32 +42,36 @@ pub struct CacluateConfig {
}
pub fn start_main(cli_arg: Command, out_path: PathBuf) {
let mut n = 0;
let mut cores = 0;
if cli_arg.is_single_thread() {
// 单线程运行的时候也是让他放在主线程跑
let config = cli_arg.as_cacl_config(&out_path);
crate::set_process_cores(config.core_affinity.unwrap());
cacl(config, 0);
} else {
let mut threads = vec![];
for i in 0..cli_arg.thread_count {
n += 1;
let mut config = cli_arg.as_cacl_config(&out_path);
// 核心亲和性: n
config.core_affinity = Some(1 << i);
cores |= 1 << i;
let thread_name = format!("thread_{}", n);
threads.push(std::thread::spawn(move || {
info!("线程 {} 开始计算", thread_name);
cacl(config, n);
info!("线程 {} 结束计算", thread_name);
}));
}
crate::set_process_cores(cores);
for t in threads {
t.join().unwrap();
}
schdule_threads(cli_arg, out_path);
}
}
pub fn schdule_threads(cli_arg: Command, out_path: PathBuf) {
let mut n = 0;
let mut cores = 0;
let mut threads = vec![];
for i in 0..cli_arg.thread_count {
n += 1;
let mut config = cli_arg.as_cacl_config(&out_path);
// 核心亲和性: n
config.core_affinity = Some(1 << i);
cores |= 1 << i;
let thread_name = format!("thread_{}", n);
threads.push(std::thread::spawn(move || {
info!("线程 {} 开始计算", thread_name);
cacl(config, n);
info!("线程 {} 结束计算", thread_name);
}));
}
crate::set_process_cores(cores);
for t in threads {
t.join().unwrap();
}
}
@ -91,14 +93,14 @@ pub fn cacl(config: CacluateConfig, id: u64) {
let mut main_namer = Namer::new_from_team_namer_unchecked(&team_namer, "dummy");
for i in (config.start + id..config.end).step_by(config.thread_count as usize) {
for i in (config.start + id..config.end).step_by(config.thread_id as usize) {
k += 1;
if k >= report_interval {
let now = std::time::Instant::now();
let d_t: std::time::Duration = now.duration_since(start_time);
let new_run_speed = k as f64 / d_t.as_secs_f64();
// 预估剩余时间
let wait_time = (config.end - i) / config.thread_count as u64 / new_run_speed as u64;
let wait_time = (config.range.end - i) / new_run_speed as u64;
let wait_time = chrono::Duration::seconds(wait_time as i64);
// 转换成 时:分:秒
// 根据实际运行速率来调整 report_interval

View File

@ -37,7 +37,7 @@ pub struct Command {
#[arg(long, short = 'r', default_value_t = 10)]
pub report_interval: u64,
/// 一个 batch 多大 单线程下无效
#[arg(long, short = 'b', default_value_t = 100000)]
#[arg(long, short = 'b', default_value_t = 10_0000)]
pub batch_size: u64,
/// 单线程模式模式下的核心亲和性核心号 (从 0 开始)
#[arg(long = "core-pick")]
@ -47,9 +47,8 @@ pub struct Command {
impl Command {
pub fn as_cacl_config(&self, path: &PathBuf) -> CacluateConfig {
CacluateConfig {
start: self.start,
end: self.end,
thread_count: self.thread_count,
range: self.start..self.end,
thread_id: 0,
prop_expect: self.prop_expect,
qp_expect: self.qp_expect,
team: self.team.clone(),