pub struct Meter { /* private fields */ }
Expand description
The main structure that makes mesurements and reports values
Create it with new()
then add threads that you want to track in a thread
breakdown information with meter.track_thread()
and
meter.untrack_thread()
.
Then add meter.scan()
with a timer to scan the process info. It’s
recommended to call it on the interval of one second.
Method report()
may be used to get structure with stats. report_json()
can return a rustc_serialize::Json
and report_json_str()
returns that
serialized.
Note that the structure returned with report()
can be changed when we
bump major version of the library. And while report_json()
and
report_json_str()
will never break the type system, their format will
always reflect that of report()
call.
We don’t track all the threads separately because thread ids are useless without names, and we can fine-tune performance in the case we have known number of threads. Obviously, process-wide info accounts all the threads.
Implementations§
Source§impl Meter
impl Meter
Sourcepub fn new(scan_interval: Duration) -> Result<Meter, Error>
pub fn new(scan_interval: Duration) -> Result<Meter, Error>
Create a new meter with scan_interval
Note: meter will not scan by itself, you are expected to call scan()
with interval.
You don’t have to guarantee the interval exactly, but it influences the accuracy of your measurements.
When creating a Meter
object we are trying to discover the number
of processes on the system. If that fails, we return error.
Sourcepub fn track_thread(&mut self, tid: Pid, name: &str)
pub fn track_thread(&mut self, tid: Pid, name: &str)
Start tracking specified thread
Note you must add main thread here manually
Sourcepub fn untrack_thread(&mut self, tid: Pid)
pub fn untrack_thread(&mut self, tid: Pid)
Stop tracking specified thread (for example if it’s dead)
Sourcepub fn track_current_thread(&mut self, name: &str) -> Pid
pub fn track_current_thread(&mut self, name: &str) -> Pid
Add current thread using track_thread
, returns thread id
Sourcepub fn untrack_current_thread(&mut self)
pub fn untrack_current_thread(&mut self)
Remove current thread using untrack_thread
Sourcepub fn get_scan_interval(&self) -> Duration
pub fn get_scan_interval(&self) -> Duration
Returns interval value configured in constructor
Source§impl Meter
impl Meter
Sourcepub fn report(&self) -> Option<Report>
pub fn report(&self) -> Option<Report>
Get report of the last scan interval
We need at least two scans to measure CPU usage, so this method returns None if less than two scans were done ever in the past.
Sourcepub fn thread_report(&self) -> Option<ThreadReportIter<'_>>
pub fn thread_report(&self) -> Option<ThreadReportIter<'_>>
Returns iterator over reports for threads
Note: each thread must be registered with Meter::track_thread
or
Meter::track_current_thread
to be tracked here.
We need at least two scans to measure CPU usage, so this method returns None if less than two scans were done ever in the past.