Struct perf_monitor::cpu::ThreadStat
source · 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)
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
);
}
}
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)
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
);
}
}
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