Expand description
Get cpu usage for current process and specified thread.
A method named cpu on ThreadStat and ProcessStat
can retrieve cpu usage of thread and process respectively.
The returning value is unnormalized, that is for multi-processor machine, the cpu usage will beyond 100%, for example returning 2.8 means 280% cpu usage. If normalized value is what you expected, divide the returning by processor_numbers.
§Example
let mut stat = ThreadStat::cur().unwrap();
let _ = (0..1_000_000).into_iter().sum::<u64>();
let usage = stat.cpu().unwrap();
println!("current thread cpu usage is {:.2}%", usage * 100f64);§Bottom Layer Interface
| platform | thread | process |
|---|---|---|
| windows | GetThreadTimes | GetProcessTimes |
| linux & android | /proc/{pid}/task/{tid}/stat | clockgettime |
| macos & ios | thread_info | getrusage |
Structs§
- Process
Stat - A struct to monitor process cpu usage
- Thread
Id - Thread
Stat - A struct to monitor thread cpu usage
Functions§
- cpu_
time - processor_
numbers - logical processor number