Struct perf_monitor::cpu::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)
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
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
        );
    }
}
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)
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
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
        );
    }
}
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>,

§

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>,

§

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.