Skip to content

Instantly share code, notes, and snippets.

@SteveLauC
Last active December 12, 2023 06:43
Show Gist options
  • Save SteveLauC/80d06120554845679feee54e71003a22 to your computer and use it in GitHub Desktop.
Save SteveLauC/80d06120554845679feee54e71003a22 to your computer and use it in GitHub Desktop.
A program demostrating the impact of cache line bouncing
//! This is basically the Rust version of the C code from this post
//!
//! https://arighi.blogspot.com/2008/12/cacheline-bouncing.html
use nix::sched::{sched_setaffinity, CpuSet};
use nix::unistd::Pid;
use std::cell::UnsafeCell;
use std::thread::spawn;
use std::time::SystemTime;
const LOOPS_MAX: usize = 2000000000;
struct Foo {
for_thread1: UnsafeCell<i64>,
#[cfg(feature = "distinct_cache_line")]
_padding: [u8; 56],
for_thread2: UnsafeCell<i64>,
}
impl Foo {
const fn new() -> Self {
Self {
for_thread1: UnsafeCell::new(0),
#[cfg(feature = "distinct_cache_line")]
_padding: [0; 56],
for_thread2: UnsafeCell::new(0),
}
}
}
unsafe impl Sync for Foo {}
static FOO: Foo = Foo::new();
fn main() {
let now = SystemTime::now();
let mut cpu0 = CpuSet::new();
cpu0.set(0).unwrap();
let mut cpu1 = CpuSet::new();
cpu1.set(1).unwrap();
let handle = spawn(move || {
sched_setaffinity(Pid::from_raw(0), &cpu0).unwrap();
for _ in 0..LOOPS_MAX {
let ptr = FOO.for_thread2.get();
unsafe {
*ptr += 1;
}
}
});
sched_setaffinity(Pid::from_raw(0), &cpu1).unwrap();
for _ in 0..LOOPS_MAX {
let ptr = FOO.for_thread1.get();
unsafe {
*ptr += 1;
}
}
handle.join().unwrap();
let dur = now.elapsed().unwrap();
println!("{:?}", dur);
}
$ neofetch
_,met$$$$$gg. steve@debian
,g$$$$$$$$$$$$$$$P. ------------
,g$$P" """Y$$.". OS: Debian GNU/Linux 12 (bookworm) x86_64
,$$P' `$$$. Kernel: 6.1.0-15-amd64
',$$P ,ggs. `$$b: Uptime: 1 day, 5 hours, 34 mins
`d$$' ,$P"' . $$$ Packages: 1807 (dpkg), 22 (flatpak)
$$P d$' , $$P Shell: zsh 5.9
$$: $$. - ,d$$' Resolution: 3440x1440
$$; Y$b._ _,d$P' DE: GNOME 43.9
Y$$. `.`"Y$$$$P"' WM: Mutter
`$$b "-.__ WM Theme: Adwaita
`Y$$ Theme: adw-gtk3 [GTK2/3]
`Y$$. Icons: Adwaita [GTK2/3]
`$$b. Terminal: tmux
`Y$$b. CPU: AMD Ryzen 9 7950X (32) @ 4.500GHz
`"Y$b._ GPU: AMD ATI 0e:00.0 Raphael
`""" GPU: AMD ATI Radeon RX 5600 OEM/5600 XT / 5700/5700 XT
Memory: 15.45GiB / 30.49GiB (50%)
Disk (/): 15G / 28G (57%)
# Don't build it in release mode, or the compiler would not modify the field
$ cargo b -q
$ ./target/debug/rust
18.374641728s
$ cargo b --features distinct_cache_line -q
$ ./target/debug/rust
9.507881497s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment