-
Notifications
You must be signed in to change notification settings - Fork 484
/
cpu_monitor.rs
68 lines (58 loc) · 1.72 KB
/
cpu_monitor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use structopt::StructOpt;
use std::io;
#[derive(StructOpt, Debug)]
pub enum ArgScenario {
/// after all tasks have finished, go to sleep
TaskEnded,
/// a root task stalls for a very long time
TaskStallRoot,
/// a task in a scope stalls for a very long time
TaskStallScope,
}
#[derive(StructOpt, Debug)]
#[structopt(about="A test for monitoring how much CPU usage Rayon consumes under various scenarios.")]
pub struct Args {
#[structopt(subcommand)]
arg_scenario: ArgScenario,
/// Control how hard the dummy task works
#[structopt(short="d", long="depth", default_value="27")]
flag_depth: usize,
}
fn main() {
use ArgScenario::*;
let args: Args = Args::from_args();
match args.arg_scenario {
TaskEnded => tasks_ended(&args),
TaskStallRoot => task_stall_root(&args),
TaskStallScope => task_stall_scope(&args),
}
}
fn wait_for_user() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
}
fn task(args: &Args) {
fn join_recursively(n: usize) {
if n == 0 {
return;
}
rayon::join(|| join_recursively(n - 1), || join_recursively(n - 1));
}
println!("Starting heavy work at depth {}...wait.", args.flag_depth);
join_recursively(args.flag_depth);
println!("Heavy work done; check top. You should see CPU usage drop to zero soon.");
println!("Press <enter> to quit...");
}
fn tasks_ended(args: &Args) {
task(args);
wait_for_user();
}
fn task_stall_root(args: &Args) {
rayon::join(|| task(args), wait_for_user);
}
fn task_stall_scope(args: &Args) {
rayon::scope(|scope| {
scope.spawn(move |_| task(args));
scope.spawn(move |_| wait_for_user());
});
}