[7192145] | 1 | use std::sync::Arc; |
---|
[751e2eb] | 2 | use std::sync::atomic::Ordering; |
---|
| 3 | use std::time::Instant; |
---|
[7192145] | 4 | |
---|
| 5 | use tokio::runtime::Builder; |
---|
| 6 | use tokio::sync; |
---|
| 7 | |
---|
| 8 | use clap::{Arg, App}; |
---|
[751e2eb] | 9 | use num_format::{Locale, ToFormattedString}; |
---|
[7192145] | 10 | |
---|
[751e2eb] | 11 | #[path = "../bench.rs"] |
---|
| 12 | mod bench; |
---|
[7192145] | 13 | |
---|
[751e2eb] | 14 | // ================================================== |
---|
[7192145] | 15 | struct Partner { |
---|
| 16 | sem: sync::Semaphore, |
---|
| 17 | next: usize, |
---|
| 18 | } |
---|
| 19 | |
---|
[751e2eb] | 20 | async fn partner_main(idx: usize, others: Arc<Vec<Arc<Partner>>>, exp: Arc<bench::BenchData> ) -> u64 { |
---|
[7192145] | 21 | let this = &others[idx]; |
---|
| 22 | let mut count:u64 = 0; |
---|
| 23 | loop { |
---|
| 24 | this.sem.acquire().await.forget(); |
---|
| 25 | others[this.next].sem.add_permits(1); |
---|
| 26 | count += 1; |
---|
| 27 | |
---|
[751e2eb] | 28 | if exp.clock_mode && exp.stop.load(Ordering::Relaxed) { break; } |
---|
| 29 | if !exp.clock_mode && count >= exp.stop_count { break; } |
---|
[7192145] | 30 | } |
---|
| 31 | |
---|
[751e2eb] | 32 | exp.threads_left.fetch_sub(1, Ordering::SeqCst); |
---|
| 33 | count |
---|
[7192145] | 34 | } |
---|
| 35 | |
---|
[751e2eb] | 36 | // ================================================== |
---|
[7192145] | 37 | fn main() { |
---|
| 38 | let options = App::new("Cycle Tokio") |
---|
[751e2eb] | 39 | .args(&bench::args()) |
---|
[7192145] | 40 | .arg(Arg::with_name("ringsize") .short("r").long("ringsize") .takes_value(true).default_value("1").help("Number of threads in a cycle")) |
---|
| 41 | .get_matches(); |
---|
| 42 | |
---|
| 43 | let ring_size = options.value_of("ringsize").unwrap().parse::<usize>().unwrap(); |
---|
| 44 | let nthreads = options.value_of("nthreads").unwrap().parse::<usize>().unwrap(); |
---|
| 45 | let nprocs = options.value_of("nprocs").unwrap().parse::<usize>().unwrap(); |
---|
| 46 | |
---|
[751e2eb] | 47 | let tthreads = nthreads * ring_size; |
---|
[821c534] | 48 | let exp = Arc::new(bench::BenchData::new(options, tthreads, None)); |
---|
[7192145] | 49 | |
---|
| 50 | let s = (1000000 as u64).to_formatted_string(&Locale::en); |
---|
| 51 | assert_eq!(&s, "1,000,000"); |
---|
| 52 | |
---|
[751e2eb] | 53 | let thddata : Arc<Vec<Arc<Partner>>> = Arc::new( |
---|
| 54 | (0..tthreads).map(|i| { |
---|
| 55 | let pi = (i + nthreads) % tthreads; |
---|
| 56 | Arc::new(Partner{ |
---|
| 57 | sem: sync::Semaphore::new(0), |
---|
| 58 | next: pi, |
---|
| 59 | }) |
---|
| 60 | }).collect() |
---|
| 61 | ); |
---|
[7192145] | 62 | |
---|
| 63 | let mut global_counter :u64 = 0; |
---|
| 64 | let mut duration : std::time::Duration = std::time::Duration::from_secs(0); |
---|
| 65 | let runtime = Builder::new_multi_thread() |
---|
| 66 | .worker_threads(nprocs) |
---|
| 67 | .enable_all() |
---|
| 68 | .build() |
---|
| 69 | .unwrap(); |
---|
| 70 | |
---|
| 71 | runtime.block_on(async { |
---|
[751e2eb] | 72 | let threads: Vec<_> = (0..tthreads).map(|i| { |
---|
| 73 | tokio::spawn(partner_main(i, thddata.clone(), exp.clone())) |
---|
| 74 | }).collect(); |
---|
| 75 | println!("Starting"); |
---|
[7192145] | 76 | |
---|
[751e2eb] | 77 | let start = Instant::now(); |
---|
[7192145] | 78 | |
---|
[751e2eb] | 79 | for i in 0..nthreads { |
---|
| 80 | thddata[i].sem.add_permits(1); |
---|
| 81 | } |
---|
[7192145] | 82 | |
---|
[751e2eb] | 83 | duration = exp.wait(&start).await; |
---|
[7192145] | 84 | |
---|
[751e2eb] | 85 | println!("\nDone"); |
---|
[7192145] | 86 | |
---|
[751e2eb] | 87 | for i in 0..tthreads { |
---|
| 88 | thddata[i].sem.add_permits(1); |
---|
| 89 | } |
---|
[7192145] | 90 | |
---|
[751e2eb] | 91 | for t in threads { |
---|
| 92 | global_counter += t.await.unwrap(); |
---|
[7192145] | 93 | } |
---|
| 94 | }); |
---|
| 95 | |
---|
| 96 | println!("Duration (ms) : {}", (duration.as_millis()).to_formatted_string(&Locale::en)); |
---|
| 97 | println!("Number of processors : {}", (nprocs).to_formatted_string(&Locale::en)); |
---|
| 98 | println!("Number of threads : {}", (tthreads).to_formatted_string(&Locale::en)); |
---|
| 99 | println!("Cycle size (# thrds) : {}", (ring_size).to_formatted_string(&Locale::en)); |
---|
| 100 | println!("Total Operations(ops): {:>15}", (global_counter).to_formatted_string(&Locale::en)); |
---|
| 101 | println!("Ops per second : {:>15}", (((global_counter as f64) / duration.as_secs() as f64) as u64).to_formatted_string(&Locale::en)); |
---|
| 102 | println!("ns per ops : {:>15}", ((duration.as_nanos() as f64 / global_counter as f64) as u64).to_formatted_string(&Locale::en)); |
---|
| 103 | println!("Ops per threads : {:>15}", (global_counter / tthreads as u64).to_formatted_string(&Locale::en)); |
---|
| 104 | println!("Ops per procs : {:>15}", (global_counter / nprocs as u64).to_formatted_string(&Locale::en)); |
---|
| 105 | println!("Ops/sec/procs : {:>15}", ((((global_counter as f64) / nprocs as f64) / duration.as_secs() as f64) as u64).to_formatted_string(&Locale::en)); |
---|
| 106 | println!("ns per ops/procs : {:>15}", ((duration.as_nanos() as f64 / (global_counter as f64 / nprocs as f64)) as u64).to_formatted_string(&Locale::en)); |
---|
| 107 | } |
---|