Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nviennot
GitHub Repository: nviennot/core-to-core-latency
Path: blob/main/src/main.rs
228 views
1
mod bench;
2
mod utils;
3
4
use bench::Count;
5
use std::sync::Arc;
6
use clap::Parser;
7
use quanta::Clock;
8
use crate::bench::run_bench;
9
10
const DEFAULT_NUM_SAMPLES: Count = 300;
11
const DEFAULT_NUM_ITERATIONS_PER_SAMPLE: Count = 1000;
12
13
#[derive(Clone)]
14
#[derive(clap::Parser)]
15
pub struct CliArgs {
16
/// The number of iterations per sample
17
#[clap(default_value_t = DEFAULT_NUM_ITERATIONS_PER_SAMPLE, value_parser)]
18
num_iterations: Count,
19
20
/// The number of samples
21
#[clap(default_value_t = DEFAULT_NUM_SAMPLES, value_parser)]
22
num_samples: Count,
23
24
/// Outputs the mean latencies in CSV format on stdout
25
#[clap(long, value_parser)]
26
csv: bool,
27
28
/// Select which benchmark to run, in a comma delimited list, e.g., '1,3' {n}
29
/// 1: CAS latency on a single shared cache line. {n}
30
/// 2: Single-writer single-reader latency on two shared cache lines. {n}
31
/// 3: One writer and one reader on many cache line, using the clock. {n}
32
#[clap(short, long, default_value="1", require_delimiter=true, value_delimiter=',', value_parser)]
33
bench: Vec<usize>,
34
35
/// Specify the cores by id that should be used, comma delimited. By default all cores are used.
36
#[clap(short, long, require_delimiter=true, value_delimiter=',', value_parser)]
37
cores: Vec<usize>,
38
}
39
40
fn main() {
41
let args = CliArgs::parse();
42
43
let cores = core_affinity::get_core_ids().expect("get_core_ids() failed");
44
45
let cores = if !args.cores.is_empty() {
46
args.cores.iter().copied()
47
.map(|cid| *cores.iter().find(|c| c.id == cid)
48
.unwrap_or_else(||panic!("Core {} not found. Available: {:?}", cid, &cores)))
49
.collect()
50
} else {
51
cores
52
};
53
54
utils::show_cpuid_info();
55
eprintln!("Num cores: {}", cores.len());
56
eprintln!("Num iterations per samples: {}", args.num_iterations);
57
eprintln!("Num samples: {}", args.num_samples);
58
#[cfg(target_os = "macos")]
59
eprintln!("{}", ansi_term::Color::Red.bold().paint("WARN macOS may ignore thread-CPU affinity (we can't select a CPU to run on). Results may be inaccurate"));
60
61
let clock = Arc::new(Clock::new());
62
63
for b in &args.bench {
64
match b {
65
1 => {
66
eprintln!();
67
eprintln!("1) CAS latency on a single shared cache line");
68
eprintln!();
69
run_bench(&cores, &clock, &args, bench::cas::Bench::new());
70
}
71
2 => {
72
eprintln!();
73
eprintln!("2) Single-writer single-reader latency on two shared cache lines");
74
eprintln!();
75
run_bench(&cores, &clock, &args, bench::read_write::Bench::new());
76
}
77
3 => {
78
utils::assert_rdtsc_usable(&clock);
79
eprintln!();
80
eprintln!("3) Message passing. One writer and one reader on many cache line");
81
eprintln!();
82
run_bench(&cores, &clock, &args, bench::msg_passing::Bench::new(args.num_iterations));
83
}
84
_ => panic!("--bench should be 1, 2 or 3"),
85
}
86
}
87
}
88
89