|
| 1 | +use core_affinity::CoreId; |
| 2 | +use log::{debug, error, info, trace}; |
| 3 | +use rand::Rng; |
| 4 | +use std::sync::{atomic::{AtomicBool, AtomicUsize, Ordering}, Barrier}; |
| 5 | +use crate::{traits::{ConcurrentQueue, Handle}, benchmarks::{calc_fairness, BenchConfig}}; |
| 6 | +use std::fs::OpenOptions; |
| 7 | +use std::io::Write; |
| 8 | +use std::sync::{mpsc, Arc}; |
| 9 | + |
| 10 | +/// # Explanation: |
| 11 | +#[allow(dead_code)] |
| 12 | +pub fn benchmark_enq_deq_pairs<C, T> (cqueue: C, bench_conf: &BenchConfig) -> Result<(), std::io::Error> |
| 13 | +where |
| 14 | +C: ConcurrentQueue<T>, |
| 15 | +T: Default, |
| 16 | + for<'a> &'a C: Send |
| 17 | +{ |
| 18 | + let args = match &bench_conf.args.benchmark { |
| 19 | + crate::arguments::Benchmarks::EnqDeqPairs(a) => a, |
| 20 | + _ => panic!(), |
| 21 | + }; |
| 22 | + { |
| 23 | + debug!("Prefilling queue with {} items.", bench_conf.args.prefill_amount); |
| 24 | + let mut tmp_handle = cqueue.register(); |
| 25 | + for _ in 0..bench_conf.args.prefill_amount { |
| 26 | + let _ = tmp_handle.push(Default::default()); |
| 27 | + } |
| 28 | + } |
| 29 | + let thread_count = args.thread_count; |
| 30 | + let time_limit: u64 = bench_conf.args.time_limit; |
| 31 | + let barrier = Barrier::new(thread_count + 1); |
| 32 | + let pops = AtomicUsize::new(0); |
| 33 | + let pushes = AtomicUsize::new(0); |
| 34 | + let done = AtomicBool::new(false); |
| 35 | + let (tx, rx) = mpsc::channel(); |
| 36 | + info!("Starting pingpong benchmark with {} threads", thread_count); |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + // Get cores for fairness of threads |
| 41 | + let available_cores: Vec<CoreId> = |
| 42 | + core_affinity::get_core_ids().unwrap_or(vec![CoreId { id: 0 }]); |
| 43 | + let mut core_iter = available_cores.into_iter().cycle(); |
| 44 | + |
| 45 | + // Shared atomic bool for when a thread fails |
| 46 | + let thread_failed = Arc::new(AtomicBool::new(false)); |
| 47 | + |
| 48 | + |
| 49 | + let _ = std::thread::scope(|s| -> Result<(), std::io::Error>{ |
| 50 | + let queue = &cqueue; |
| 51 | + let thread_failed = &thread_failed; // Every thread clones the thread_failed bool |
| 52 | + let pushes = &pushes; |
| 53 | + let pops = &pops; |
| 54 | + let done = &done; |
| 55 | + let barrier = &barrier; |
| 56 | + let &thread_count = &thread_count; |
| 57 | + let is_one_socket = &bench_conf.args.one_socket; |
| 58 | + let tx = &tx; |
| 59 | + for _i in 0..thread_count{ |
| 60 | + let mut core : CoreId = core_iter.next().unwrap(); |
| 61 | + // if is_one_socket is true, make all thread ids even |
| 62 | + // (this was used for our testing enviroment to get one socket) |
| 63 | + if *is_one_socket { |
| 64 | + core = core_iter.next().unwrap(); |
| 65 | + } |
| 66 | + // println!("{:?}", core); |
| 67 | + s.spawn(move || { |
| 68 | + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 69 | + core_affinity::set_for_current(core); |
| 70 | + let mut handle = queue.register(); |
| 71 | + let mut l_pushes = 0; |
| 72 | + let mut l_pops = 0; |
| 73 | + let _thread_failed = thread_failed.clone(); |
| 74 | + barrier.wait(); |
| 75 | + while !done.load(Ordering::Relaxed) { |
| 76 | + let _ = handle.push(T::default()); |
| 77 | + l_pushes += 1; |
| 78 | + let _ = handle.pop(); |
| 79 | + l_pops += 1; |
| 80 | + for _ in 0..bench_conf.args.delay { |
| 81 | + let _some_num = rand::rng().random::<f64>(); |
| 82 | + } |
| 83 | + } |
| 84 | + pushes.fetch_add(l_pushes, Ordering::Relaxed); |
| 85 | + pops.fetch_add(l_pops, Ordering::Relaxed); |
| 86 | + tx.send(l_pops + l_pushes).unwrap(); |
| 87 | + trace!("{}: Pushed: {}, Popped: {}", _i, l_pushes, l_pops); |
| 88 | + })); |
| 89 | + // A thread panicked, aborting the benchmark... |
| 90 | + if let Err(e) = result { |
| 91 | + error!("Thread {} panicked: {:?}. Aborting benchmark, padding results to zero", _i, e); |
| 92 | + thread_failed.store(true, Ordering::Relaxed); |
| 93 | + done.store(true, Ordering::Relaxed); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + } |
| 98 | + barrier.wait(); |
| 99 | + std::thread::sleep(std::time::Duration::from_secs(time_limit)); |
| 100 | + done.store(true, Ordering::Relaxed); |
| 101 | + Ok(()) |
| 102 | + }); |
| 103 | + drop(tx); |
| 104 | + let pops = pops.into_inner(); |
| 105 | + let pushes = pushes.into_inner(); |
| 106 | + // Fairness |
| 107 | + let ops_per_thread = { |
| 108 | + let mut vals = vec![]; |
| 109 | + for received in rx { |
| 110 | + vals.push(received); |
| 111 | + } |
| 112 | + vals |
| 113 | + }; |
| 114 | + let fairness = calc_fairness(ops_per_thread); |
| 115 | + |
| 116 | + // If a thread crashed, pad the results with zero-values |
| 117 | + let formatted = if thread_failed.load(Ordering::Relaxed) { |
| 118 | + format!("0,0,0,-1,-1,{},{},{},{},0,{},{}", |
| 119 | + thread_count, |
| 120 | + cqueue.get_id(), |
| 121 | + bench_conf.args.benchmark, |
| 122 | + bench_conf.benchmark_id, |
| 123 | + -1, |
| 124 | + bench_conf.args.queue_size |
| 125 | + ) |
| 126 | + } |
| 127 | + else { |
| 128 | + format!("{},{},{},{},{},{},{},{},{},{},{},{}", |
| 129 | + (pushes + pops) as f64 / time_limit as f64, |
| 130 | + pushes, |
| 131 | + pops, |
| 132 | + -1, |
| 133 | + -1, |
| 134 | + thread_count, |
| 135 | + cqueue.get_id(), |
| 136 | + bench_conf.args.benchmark, |
| 137 | + bench_conf.benchmark_id, |
| 138 | + fairness, |
| 139 | + -1, |
| 140 | + bench_conf.args.queue_size) |
| 141 | + }; |
| 142 | + // Write to file or stdout depending on flag |
| 143 | + if !bench_conf.args.write_to_stdout { |
| 144 | + let mut file = OpenOptions::new() |
| 145 | + .append(true) |
| 146 | + .create(true) |
| 147 | + .open(&bench_conf.output_filename)?; |
| 148 | + writeln!(file, "{}", formatted)?; |
| 149 | + } else { |
| 150 | + println!("{}", formatted); |
| 151 | + } |
| 152 | + Ok(()) |
| 153 | +} |
| 154 | + |
| 155 | +mod tests { |
| 156 | + #[cfg(feature = "basic_queue")] |
| 157 | + #[test] |
| 158 | + fn test_enq_deq_pairs() { |
| 159 | + use crate::benchmarks::enq_deq_pairs::benchmark_enq_deq_pairs; |
| 160 | + use crate::benchmarks::*; |
| 161 | + use crate::arguments::*; |
| 162 | + use crate::queues::basic_queue::*; |
| 163 | + |
| 164 | + let args = Args { |
| 165 | + benchmark: Benchmarks::EnqDeqPairs(EnqDeqPairsArgs { thread_count: 10 }), |
| 166 | + ..Default::default() |
| 167 | + }; |
| 168 | + let bench_conf = BenchConfig { |
| 169 | + args, |
| 170 | + date_time: "".to_string(), |
| 171 | + benchmark_id: "test2".to_string(), |
| 172 | + output_filename: "".to_string() |
| 173 | + }; |
| 174 | + let q: BasicQueue<usize> = BasicQueue::new(0); |
| 175 | + assert!(benchmark_enq_deq_pairs(q, &bench_conf).is_ok()); |
| 176 | + } |
| 177 | +} |
0 commit comments