Struct Meter

Source
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

Source

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.

Source

pub fn track_thread(&mut self, tid: Pid, name: &str)

Start tracking specified thread

Note you must add main thread here manually

Source

pub fn untrack_thread(&mut self, tid: Pid)

Stop tracking specified thread (for example if it’s dead)

Source

pub fn track_current_thread(&mut self, name: &str) -> Pid

Add current thread using track_thread, returns thread id

Source

pub fn untrack_current_thread(&mut self)

Remove current thread using untrack_thread

Source

pub fn get_scan_interval(&self) -> Duration

Returns interval value configured in constructor

Source§

impl Meter

Source

pub fn scan(&mut self) -> Result<(), Error>

Scan system for metrics

This method must be called regularly at intervals specified in constructor.

Source§

impl Meter

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Meter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Meter

§

impl RefUnwindSafe for Meter

§

impl Send for Meter

§

impl Sync for Meter

§

impl Unpin for Meter

§

impl UnwindSafe for Meter

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.