Struct ThreadStat

Source
pub struct ThreadStat { /* private fields */ }
Expand description

A struct to monitor thread cpu usage

Implementations§

Source§

impl ThreadStat

Source

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}
Source

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.

Source

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}
Source

pub fn cpu_time(&mut self) -> Result<Duration>

return the cpu_time in user mode and system mode from last invoke, or when this struct created if it is the first invoke.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.