Skip to content

Commit d98d420

Browse files
committed
extract cpu_time, add to noop demo
1 parent 6752b00 commit d98d420

File tree

7 files changed

+71
-35
lines changed

7 files changed

+71
-35
lines changed

rayon-demo/src/cpu_time/mod.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use time::{self, Duration};
2+
3+
#[cfg(windows)]
4+
mod win;
5+
#[cfg(windows)]
6+
pub use self::win::get_cpu_time;
7+
8+
#[cfg(unix)]
9+
mod unix;
10+
#[cfg(unix)]
11+
pub use self::unix::get_cpu_time;
12+
13+
#[cfg(not(any(unix, windows)))]
14+
pub fn get_cpu_time() -> Option<u64> {
15+
None
16+
}
17+
18+
pub fn get_cpu_duration(start: Option<u64>, stop: Option<u64>) -> Option<Duration> {
19+
start.and_then(|start| stop.and_then(|stop| Some(Duration::nanoseconds((stop - start) as i64))))
20+
}
21+
22+
#[derive(Copy, Clone)]
23+
pub struct CpuMeasure {
24+
/// number of ns
25+
pub time_duration: u64,
26+
27+
/// percentage (0-100) of that as cpu time
28+
pub cpu_usage_percent: Option<f64>,
29+
}
30+
31+
pub fn measure_cpu(op: impl FnOnce()) -> CpuMeasure {
32+
let time_start = time::precise_time_ns();
33+
let cpu_start = get_cpu_time();
34+
35+
op();
36+
37+
let cpu_stop = get_cpu_time();
38+
let time_duration = time::precise_time_ns() - time_start;
39+
40+
CpuMeasure {
41+
time_duration,
42+
cpu_usage_percent: get_cpu_duration(cpu_start, cpu_stop)
43+
.and_then(|cpu| cpu.num_nanoseconds())
44+
.and_then(|cpu| Some(100.0 * cpu as f64 / time_duration as f64)),
45+
}
46+
}
47+
48+
pub fn print_time(m: CpuMeasure) {
49+
println!(" wallclock: {} ns", m.time_duration);
50+
if let Some(cpu_usage) = m.cpu_usage_percent {
51+
println!(" cpu usage: {:3.1}%", cpu_usage);
52+
} else {
53+
println!(" cpu usage: N/A");
54+
}
55+
}
File renamed without changes.

rayon-demo/src/life/cpu_time/mod.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

rayon-demo/src/life/mod.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Options:
1414
-h, --help Show this message.
1515
";
1616

17+
use cpu_time::{self, CpuMeasure};
1718
use rand::distributions::Standard;
1819
use rand::{thread_rng, Rng};
1920
use std::iter::repeat;
@@ -28,7 +29,6 @@ use rayon::prelude::*;
2829

2930
#[cfg(test)]
3031
mod bench;
31-
mod cpu_time;
3232

3333
#[derive(Deserialize)]
3434
pub struct Args {
@@ -249,19 +249,14 @@ fn measure_cpu(f: fn(Board, usize, u64) -> (), args: &Args) -> CpuResult {
249249
let (n, gens, rate) = (args.flag_size, args.flag_gens, args.flag_fps);
250250
let interval = 1_000_000_000 / rate as u64;
251251
let brd = Board::new(n, n).random();
252-
let start = time::precise_time_ns();
253-
let cpu_start = cpu_time::get_cpu_time();
254-
255-
f(brd, gens, interval);
256252

257-
let cpu_stop = cpu_time::get_cpu_time();
258-
let duration = time::precise_time_ns() - start;
253+
let CpuMeasure { time_duration, cpu_usage_percent } = cpu_time::measure_cpu(|| {
254+
f(brd, gens, interval)
255+
});
259256

260257
CpuResult {
261-
actual_fps: (1_000_000_000.0 * gens as f64) / duration as f64,
262-
cpu_usage_percent: cpu_time::get_cpu_duration(cpu_start, cpu_stop)
263-
.and_then(|cpu| cpu.num_nanoseconds())
264-
.and_then(|cpu| Some(100.0 * cpu as f64 / duration as f64)),
258+
actual_fps: (1_000_000_000.0 * gens as f64) / time_duration as f64,
259+
cpu_usage_percent,
265260
}
266261
}
267262

rayon-demo/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io;
55
use std::io::prelude::*;
66
use std::process::exit;
77

8+
mod cpu_time;
89
mod life;
910
mod matmul;
1011
mod mergesort;

rayon-demo/src/noop/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Options:
88
--iters N Total time to execution (in millis). [default: 100]
99
";
1010

11+
use cpu_time;
1112
use docopt::Docopt;
1213

1314
#[derive(Deserialize)]
@@ -21,8 +22,12 @@ pub fn main(args: &[String]) {
2122
.and_then(|d| d.argv(args).deserialize())
2223
.unwrap_or_else(|e| e.exit());
2324

24-
for _ in 1..args.flag_iters {
25-
std::thread::sleep(std::time::Duration::from_millis(args.flag_sleep));
26-
rayon::spawn(move || { } );
27-
}
25+
let m = cpu_time::measure_cpu(|| {
26+
for _ in 1..args.flag_iters {
27+
std::thread::sleep(std::time::Duration::from_millis(args.flag_sleep));
28+
rayon::spawn(move || { } );
29+
}
30+
});
31+
println!("noop --iters={} --sleep={}", args.flag_iters, args.flag_sleep);
32+
cpu_time::print_time(m);
2833
}

0 commit comments

Comments
 (0)