跳到主要内容

RuntimeMetrics

搜索

结构体 RuntimeMetrics 

Source
pub struct RuntimeMetrics { /* private fields */ }
展开描述

运行时 metrics 的句柄。

此句柄内部使用引用计数,可以自由克隆。可以使用 Runtime::metrics 方法获取 RuntimeMetrics 句柄。

实现§

Source§

impl RuntimeMetrics

Source

pub fn num_workers(&self) -> usize

返回运行时使用的工作线程数。

worker 数量通过在 runtime::Builder 上配置 worker_threads 来设置。使用 current_thread 运行时时,返回值始终为 1

§示例
use tokio::runtime::Handle;

let metrics = Handle::current().metrics();

let n = metrics.num_workers();
println!("Runtime is using {} workers", n);
Source

pub fn num_alive_tasks(&self) -> usize

返回运行时中当前存活任务的数量。

派生任务时此计数器会增加,任务退出时则会减少。

注意:在使用多线程运行时时,此数字可能不具有强一致性,即可能没有任务在运行,但 metric 却报告有任务。

§示例
use tokio::runtime::Handle;

let metrics = Handle::current().metrics();

let n = metrics.num_alive_tasks();
println!("Runtime has {} alive tasks", n);
Source

pub fn global_queue_depth(&self) -> usize

返回运行时全局队列中当前已调度的任务数。

从非运行时线程派生或通知的任务使用运行时的全局队列进行调度。此 metric 返回全局队列中待处理任务的当前数量。因此,随着新任务被调度和处理,返回值可能增加或减少。

§示例
use tokio::runtime::Handle;

let metrics = Handle::current().metrics();

let n = metrics.global_queue_depth();
println!("{} tasks currently pending in the runtime's global queue", n);
Source

pub fn worker_total_busy_duration(&self, worker: usize) -> Duration

返回给定工作线程已忙的时间量。

worker 忙时计时从运行时创建时的零开始,每当 worker 花费时间处理工作时就会增加。使用此值可以指示给定 worker 的负载。如果大部分时间都处于忙状态,则说明 worker 负载较重,将不那么频繁地检查入站事件。

此计时器单调递增,永不递减或重置为零。

§Arguments

worker 是被查询 worker 的索引。给定值必须介于 0 和 num_workers() 之间。该索引唯一标识一个 worker,并在运行时实例的整个生命周期内持续标识该 worker。

§Panics

worker 表示无效 worker(即大于或等于 num_workers().

§示例
use tokio::runtime::Handle;

let metrics = Handle::current().metrics();

let n = metrics.worker_total_busy_duration(0);
println!("worker 0 was busy for a total of {:?}", n);
Source

pub fn worker_park_count(&self, worker: usize) -> u64

返回给定工作线程 park 的总次数。

worker park 计数从运行时创建时的零开始,每当 worker park 线程等待新的入站事件处理时增加一。这通常意味着 worker 已处理完所有待处理工作,当前处于空闲状态。

此计数器单调递增,永不递减或重置为零。

§Arguments

worker 是被查询 worker 的索引。给定值必须介于 0 和 num_workers() 之间。该索引唯一标识一个 worker,并在运行时实例的整个生命周期内持续标识该 worker。

§Panics

worker 表示无效 worker(即大于或等于 num_workers().

§示例
use tokio::runtime::Handle;

let metrics = Handle::current().metrics();

let n = metrics.worker_park_count(0);
println!("worker 0 parked {} times", n);
Source

pub fn worker_park_unpark_count(&self, worker: usize) -> u64

返回给定工作线程 park 和 unpark 的总次数。

worker park/unpark 计数从运行时创建时的零开始,每当 worker park 线程等待新的入站事件处理时增加一。这通常意味着 worker 已处理完所有待处理工作,当前处于空闲状态。当新工作变得可用时,worker 会被 unpark,park/unpark 计数再次增加一。

奇数表示 worker 当前处于 parked 状态。 偶数表示 worker 当前处于 active 状态。

此计数器单调递增,永不递减或重置为零。

§Arguments

worker 是被查询 worker 的索引。给定值必须介于 0 和 num_workers() 之间。该索引唯一标识一个 worker,并在运行时实例的整个生命周期内持续标识该 worker。

§Panics

worker 表示无效 worker(即大于或等于 num_workers().

§示例
use tokio::runtime::Handle;

let metrics = Handle::current().metrics();
let n = metrics.worker_park_unpark_count(0);

println!("worker 0 parked and unparked {} times", n);

if n % 2 == 0 {
    println!("worker 0 is active");
} else {
    println!("worker 0 is parked");
}

Trait 实现§

Source§

impl Clone for RuntimeMetrics

Source§

fn clone(&self) -> RuntimeMetrics

返回值的副本。 更多信息
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 更多信息
Source§

impl Debug for RuntimeMetrics

Source§

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

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

获得所有权后的类型。
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 更多信息
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。