-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathactivity_monitor.rs
69 lines (55 loc) · 1.81 KB
/
activity_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
69
use std::time::Duration;
use std::time::Instant;
use perf_monitor::cpu::processor_numbers;
use perf_monitor::cpu::ProcessStat;
use perf_monitor::cpu::ThreadStat;
use perf_monitor::fd::fd_count_cur;
use perf_monitor::io::get_process_io_stats;
use perf_monitor::mem::get_process_memory_info;
fn main() {
build_some_threads();
// cpu
let core_num = processor_numbers().unwrap();
let mut stat_p = ProcessStat::cur().unwrap();
let mut stat_t = ThreadStat::cur().unwrap();
let mut last_loop = Instant::now();
loop {
if last_loop.elapsed() > Duration::from_secs(1) {
last_loop = Instant::now();
} else {
std::thread::sleep(Duration::from_micros(100));
continue;
}
println!("----------");
// cpu
let _ = (0..1_000).into_iter().sum::<i128>();
let usage_p = stat_p.cpu().unwrap() * 100f64;
let usage_t = stat_t.cpu().unwrap() * 100f64;
println!(
"[CPU] core Number: {}, process usage: {:.2}%, current thread usage: {:.2}%",
core_num, usage_p, usage_t
);
// mem
let mem_info = get_process_memory_info().unwrap();
println!(
"[Memory] memory used: {} bytes, virtural memory used: {} bytes ",
mem_info.resident_set_size, mem_info.virtual_memory_size
);
// fd
let fd_num = fd_count_cur().unwrap();
println!("[FD] fd number: {}", fd_num);
// io
let io_stat = get_process_io_stats().unwrap();
println!(
"[IO] io-in: {} bytes, io-out: {} bytes",
io_stat.read_bytes, io_stat.write_bytes
);
}
}
fn build_some_threads() {
for _ in 0..5 {
std::thread::spawn(|| loop {
let _ = (0..9_000).into_iter().sum::<i128>();
});
}
}