namerena-rs/miner/src/main.rs

133 lines
4.2 KiB
Rust
Raw Normal View History

2024-03-01 21:57:29 +08:00
#![feature(portable_simd)]
2024-03-03 17:12:29 +08:00
#![feature(slice_swap_unchecked)]
2024-03-01 21:57:29 +08:00
2024-03-03 17:12:29 +08:00
mod cacluate;
2024-03-03 13:44:04 +08:00
mod evaluate;
2024-03-03 17:12:29 +08:00
mod generate;
2024-03-03 14:51:01 +08:00
mod name;
2024-02-28 19:04:57 +08:00
2024-03-03 17:12:29 +08:00
use std::path::PathBuf;
2024-02-29 01:05:41 +08:00
use clap::Parser;
2024-03-02 02:06:54 +08:00
use tracing::{info, warn};
2024-02-28 23:33:59 +08:00
2024-03-03 17:12:29 +08:00
use crate::cacluate::CacluateConfig;
2024-02-28 23:33:59 +08:00
#[derive(Parser, Debug, Clone)]
2024-02-29 01:05:41 +08:00
pub struct Command {
/// 开始的 id
2024-02-29 01:05:41 +08:00
#[arg(long, default_value_t = 0)]
pub start: u64,
/// 结束的 id
2024-02-29 01:05:41 +08:00
#[arg(long, default_value_t = u64::MAX)]
pub end: u64,
/// 线程数
2024-02-29 01:05:41 +08:00
#[arg(long, short = 't', default_value_t = 10)]
pub thread_count: u32,
/// 八围预期值
2024-03-31 00:36:36 +08:00
#[arg(long = "prop-expected", short = 'p', default_value_t = 640)]
pub prop_expect: u32,
2024-03-31 00:36:36 +08:00
/// qp 预期值
#[arg(long = "qp-expected", short = 'q', default_value_t = 0)]
pub qp_expect: u32,
/// 队伍名称
2024-02-29 01:05:41 +08:00
#[arg(long)]
pub team: String,
/// 预期状态输出时间间隔 (秒)
#[arg(long, short = 'r', default_value_t = 10)]
pub report_interval: u64,
/// Windows 下会强制单线程, 且设置线程亲和性为核心 0
#[arg(long = "bench", default_value_t = false)]
pub bench: bool,
2024-05-25 12:16:03 +08:00
/// 单线程模式 / benchmark 模式下的核心亲和性核心号 (从 0 开始)
#[arg(long = "core-pick", default_value_t = 0)]
pub pick_core: usize,
2024-02-29 01:05:41 +08:00
}
2024-03-03 17:12:29 +08:00
impl Command {
pub fn as_cacl_config(&self) -> CacluateConfig {
CacluateConfig {
start: self.start,
end: self.end,
thread_count: self.thread_count,
prop_expect: self.prop_expect,
2024-03-31 00:36:36 +08:00
qp_expect: self.qp_expect,
2024-03-03 17:12:29 +08:00
team: self.team.clone(),
report_interval: self.report_interval,
2024-05-25 12:16:03 +08:00
core_affinity: if self.bench { Some(1 << self.pick_core) } else { None },
2024-02-29 00:25:19 +08:00
}
}
2024-06-23 20:05:57 +08:00
pub fn is_single_thread(&self) -> bool {
self.thread_count == 1
}
2024-02-29 00:25:19 +08:00
}
2024-02-28 23:33:59 +08:00
2024-04-27 21:20:45 +08:00
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),
}
}
2024-04-28 20:39:31 +08:00
#[cfg(unix)]
2024-04-27 21:20:45 +08:00
{
warn!("Linux 下不支持设置线程亲和性 (未实现) {}", core)
}
}
2024-04-28 06:18:26 +08:00
pub fn set_process_cores(cores: usize) {
#[cfg(windows)]
unsafe {
2024-04-30 06:09:08 +08:00
use windows_sys::Win32::System::Threading::{GetCurrentProcess, SetProcessAffinityMask};
2024-04-28 06:18:26 +08:00
let process = GetCurrentProcess();
let core_mask = cores;
match SetProcessAffinityMask(process, core_mask) {
0 => warn!("设置进程亲和性失败 {}", std::io::Error::last_os_error()),
x => info!("设置进程亲和性成功 {}", x),
}
}
2024-04-28 20:39:31 +08:00
#[cfg(unix)]
2024-04-28 06:18:26 +08:00
{
warn!("Linux 下不支持设置进程亲和性 (未实现) {}", cores)
}
}
2024-02-28 19:04:57 +08:00
fn main() {
2024-03-03 20:05:33 +08:00
tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init();
2024-02-29 01:05:41 +08:00
let mut cli_arg = Command::parse();
2024-02-28 23:33:59 +08:00
// 将数据量处理成可被 thread_count 整除
2024-02-29 01:05:41 +08:00
let left = cli_arg.start % cli_arg.thread_count as u64;
cli_arg.end = cli_arg.end.wrapping_add(left);
2024-02-29 01:05:41 +08:00
2024-05-25 12:28:10 +08:00
let mut threads = vec![];
let now = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S").to_string();
2024-04-27 10:37:18 +08:00
// namerena-<team>-<time>.csv
2024-03-02 00:08:56 +08:00
// <time>: %Y-%m-%d-%H-%M-%S
2024-04-27 10:37:18 +08:00
let output_filename = format!("namerena-{}-{}.csv", cli_arg.team, now);
let out_path = PathBuf::from(format!("./namerena/{}", output_filename));
info!("输出文件: {:?}", out_path);
// 先创建文件夹
2024-04-28 20:39:31 +08:00
if let Err(e) = std::fs::create_dir_all(out_path.parent().unwrap()) {
warn!("创建文件夹失败: {}", e);
2024-06-23 20:05:57 +08:00
return;
}
2024-03-02 02:06:54 +08:00
info!("开始: {} 结尾: {}", cli_arg.start, cli_arg.end);
info!("线程数: {}", cli_arg.thread_count);
2024-03-02 01:43:18 +08:00
info!("八围预期: {}", cli_arg.prop_expect);
2024-03-02 02:06:54 +08:00
info!("队伍名: {}", cli_arg.team);
info!("输出文件名: {:?}", out_path);
info!("预期状态输出时间间隔: {} 秒", cli_arg.report_interval);
info!("是否启动 benchmark 模式: {}", cli_arg.bench);
2024-06-23 20:05:57 +08:00
cacluate::start_main(cli_arg, out_path);
2024-02-28 23:33:59 +08:00
2024-02-28 19:04:57 +08:00
}