pub struct RuntimeMetrics { /* 私有字段 */ }展开描述
运行时指标的句柄。
此句柄内部使用引用计数,可以自由克隆。可使用 Runtime::metrics 方法获取 RuntimeMetrics 句柄。
实现§
源代码§impl RuntimeMetrics
impl RuntimeMetrics
源代码pub fn num_workers(&self) -> usize
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
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
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
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
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
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
impl Clone for RuntimeMetrics
源代码§fn clone(&self) -> RuntimeMetrics
fn clone(&self) -> RuntimeMetrics
1.0.0 · 源代码§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. 更多信息