跳到主要内容

RuntimeMetrics

搜索

结构体 RuntimeMetrics 

源代码
pub struct RuntimeMetrics { /* 私有字段 */ }
展开描述

运行时指标的句柄。

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

实现§

源代码§

impl RuntimeMetrics

源代码

pub fn num_workers(&self) -> usize

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

工作线程数通过在 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);
源代码

pub fn num_alive_tasks(&self) -> usize

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

当任务生成时此计数器增加,当任务退出时此计数器减少。

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

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

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

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

pub fn global_queue_depth(&self) -> usize

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

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

§示例
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);
源代码

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

返回给定工作线程处于忙碌状态的时间量。

工作线程忙碌持续时间在运行时创建时从零开始,并在工作线程花费时间处理工作时增加。使用此值可以指示给定工作线程的负载。如果花费大量时间处于忙碌状态,则工作线程处于负载状态,并且将更少地检查入站事件。

计时器单调递增。它永远不会减少或重置为零。

§Arguments

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

§恐慌

worker 表示无效的工作线程时(即大于或等于 num_workers()),该方法会发生 panic。

§示例
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);
源代码

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

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

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

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

§Arguments

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

§恐慌

worker 表示无效的工作线程时(即大于或等于 num_workers()),该方法会发生 panic。

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

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

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

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

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

The worker

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

奇数计数表示工作线程当前已 park。偶数计数表示工作线程当前处于活动状态。

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

§Arguments

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

§恐慌

worker 表示无效的工作线程时(即大于或等于 num_workers()),该方法会发生 panic。

§示例
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 实现§

源代码§

impl Clone for RuntimeMetrics

源代码§

fn clone(&self) -> RuntimeMetrics

返回值的副本。 更多信息
1.0.0 · 源代码§

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

Performs copy-assignment from source. 更多信息
源代码§

impl Debug for RuntimeMetrics

源代码§

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

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

自动 trait 实现§

blanket 实现§

源代码§

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

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

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

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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

Mutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

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

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

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

源代码§

type Owned = T

获得所有权后的类型。
源代码§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
源代码§

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

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

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

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

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

执行转换。
源代码§

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

源代码§

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

转换出错时返回的类型。
源代码§

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

执行转换。