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