pub struct ThreadStat { /* private fields */ }
Expand description
A struct to monitor thread cpu usage
Implementations§
Source§impl ThreadStat
impl ThreadStat
Sourcepub fn cur() -> Result<Self>
pub fn cur() -> Result<Self>
return a monitor of current thread.
Examples found in repository?
examples/activity_monitor.rs (line 17)
11fn main() {
12 build_some_threads();
13
14 // cpu
15 let core_num = processor_numbers().unwrap();
16 let mut stat_p = ProcessStat::cur().unwrap();
17 let mut stat_t = ThreadStat::cur().unwrap();
18
19 let mut last_loop = Instant::now();
20 loop {
21 if last_loop.elapsed() > Duration::from_secs(1) {
22 last_loop = Instant::now();
23 } else {
24 std::thread::sleep(Duration::from_micros(100));
25 continue;
26 }
27 println!("----------");
28
29 // cpu
30 let _ = (0..1_000).into_iter().sum::<i128>();
31
32 let usage_p = stat_p.cpu().unwrap() * 100f64;
33 let usage_t = stat_t.cpu().unwrap() * 100f64;
34
35 println!(
36 "[CPU] core Number: {}, process usage: {:.2}%, current thread usage: {:.2}%",
37 core_num, usage_p, usage_t
38 );
39
40 // mem
41 let mem_info = get_process_memory_info().unwrap();
42
43 println!(
44 "[Memory] memory used: {} bytes, virtural memory used: {} bytes ",
45 mem_info.resident_set_size, mem_info.virtual_memory_size
46 );
47
48 // fd
49 let fd_num = fd_count_cur().unwrap();
50
51 println!("[FD] fd number: {}", fd_num);
52
53 // io
54 let io_stat = get_process_io_stats().unwrap();
55
56 println!(
57 "[IO] io-in: {} bytes, io-out: {} bytes",
58 io_stat.read_bytes, io_stat.write_bytes
59 );
60 }
61}
Sourcepub fn build(thread_id: ThreadId) -> Result<Self>
pub fn build(thread_id: ThreadId) -> Result<Self>
return a monitor of specified thread.
tid
is NOT std::thread::ThreadId
.
ThreadId::current
can be used to retrieve a valid tid.
Sourcepub fn cpu(&mut self) -> Result<f64>
pub fn cpu(&mut self) -> Result<f64>
return the cpu usage from last invoke, or when this struct created if it is the first invoke.
Examples found in repository?
examples/activity_monitor.rs (line 33)
11fn main() {
12 build_some_threads();
13
14 // cpu
15 let core_num = processor_numbers().unwrap();
16 let mut stat_p = ProcessStat::cur().unwrap();
17 let mut stat_t = ThreadStat::cur().unwrap();
18
19 let mut last_loop = Instant::now();
20 loop {
21 if last_loop.elapsed() > Duration::from_secs(1) {
22 last_loop = Instant::now();
23 } else {
24 std::thread::sleep(Duration::from_micros(100));
25 continue;
26 }
27 println!("----------");
28
29 // cpu
30 let _ = (0..1_000).into_iter().sum::<i128>();
31
32 let usage_p = stat_p.cpu().unwrap() * 100f64;
33 let usage_t = stat_t.cpu().unwrap() * 100f64;
34
35 println!(
36 "[CPU] core Number: {}, process usage: {:.2}%, current thread usage: {:.2}%",
37 core_num, usage_p, usage_t
38 );
39
40 // mem
41 let mem_info = get_process_memory_info().unwrap();
42
43 println!(
44 "[Memory] memory used: {} bytes, virtural memory used: {} bytes ",
45 mem_info.resident_set_size, mem_info.virtual_memory_size
46 );
47
48 // fd
49 let fd_num = fd_count_cur().unwrap();
50
51 println!("[FD] fd number: {}", fd_num);
52
53 // io
54 let io_stat = get_process_io_stats().unwrap();
55
56 println!(
57 "[IO] io-in: {} bytes, io-out: {} bytes",
58 io_stat.read_bytes, io_stat.write_bytes
59 );
60 }
61}
Auto Trait Implementations§
impl Freeze for ThreadStat
impl RefUnwindSafe for ThreadStat
impl Send for ThreadStat
impl Sync for ThreadStat
impl Unpin for ThreadStat
impl UnwindSafe for ThreadStat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more