Skip to content

Commit

Permalink
chore: avoid deprecated items, fix warnings (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored Nov 26, 2024
1 parent 47fbe3b commit 28ab355
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "tokio-metrics"
version = "0.3.1"
edition = "2021"
rust-version = "1.56.0"
rust-version = "1.70.0"
authors = ["Tokio Contributors <[email protected]>"]
license = "MIT"
readme = "README.md"
Expand All @@ -14,6 +14,9 @@ Runtime and task level metrics for Tokio applications.
categories = ["asynchronous", "network-programming"]
keywords = ["async", "futures", "metrics", "debugging"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] }

[features]
default = ["rt"]
rt = ["tokio"]
Expand All @@ -22,7 +25,7 @@ rt = ["tokio"]
tokio-stream = "0.1.11"
futures-util = "0.3.19"
pin-project-lite = "0.2.7"
tokio = { version = "1.31.0", features = ["rt", "time", "net"], optional = true }
tokio = { version = "1.41.0", features = ["rt", "time", "net"], optional = true }

[dev-dependencies]
axum = "0.6"
Expand All @@ -31,7 +34,7 @@ futures = "0.3.21"
num_cpus = "1.13.1"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
tokio = { version = "1.26.0", features = ["full", "rt", "time", "macros", "test-util"] }
tokio = { version = "1.41.0", features = ["full", "rt", "time", "macros", "test-util"] }

[[example]]
name = "runtime"
Expand Down
74 changes: 36 additions & 38 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,37 +244,35 @@ pub struct RuntimeMetrics {
/// times.
///
/// This metric must be explicitly enabled when creating the runtime with
/// [`enable_metrics_poll_count_histogram`][tokio::runtime::Builder::enable_metrics_poll_count_histogram].
/// [`enable_metrics_poll_time_histogram`][tokio::runtime::Builder::enable_metrics_poll_time_histogram].
/// Bucket sizes are fixed and configured at the runtime level. See
/// configuration options on
/// [`runtime::Builder`][tokio::runtime::Builder::enable_metrics_poll_count_histogram].
/// [`runtime::Builder`][tokio::runtime::Builder::enable_metrics_poll_time_histogram].
///
/// ##### Examples
/// ```
/// use tokio::runtime::HistogramScale;
/// use tokio::runtime::HistogramConfiguration;
/// use std::time::Duration;
///
/// fn main() {
/// let rt = tokio::runtime::Builder::new_multi_thread()
/// .enable_metrics_poll_count_histogram()
/// .metrics_poll_count_histogram_scale(HistogramScale::Linear)
/// .metrics_poll_count_histogram_resolution(Duration::from_micros(50))
/// .metrics_poll_count_histogram_buckets(12)
/// .build()
/// .unwrap();
/// let config = HistogramConfiguration::linear(Duration::from_micros(50), 12);
///
/// rt.block_on(async {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
/// let rt = tokio::runtime::Builder::new_multi_thread()
/// .enable_metrics_poll_time_histogram()
/// .metrics_poll_time_histogram_configuration(config)
/// .build()
/// .unwrap();
///
/// let interval = next_interval();
/// println!("poll count histogram {:?}", interval.poll_count_histogram);
/// });
/// }
/// rt.block_on(async {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval();
/// println!("poll count histogram {:?}", interval.poll_time_histogram);
/// });
/// ```
pub poll_count_histogram: Vec<u64>,
pub poll_time_histogram: Vec<u64>,

/// The number of times worker threads unparked but performed no work before parking again.
///
Expand Down Expand Up @@ -527,7 +525,7 @@ pub struct RuntimeMetrics {
///
/// The remote schedule count increases by one each time a task is woken from **outside** of
/// the runtime. This usually means that a task is spawned or notified from a non-runtime
/// thread and must be queued using the Runtime's injection queue, which tends to be slower.
/// thread and must be queued using the Runtime's global queue, which tends to be slower.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::remote_schedule_count`].
Expand Down Expand Up @@ -683,7 +681,7 @@ pub struct RuntimeMetrics {
///
/// The worker steal count increases by one each time the worker attempts to schedule a task
/// locally, but its local queue is full. When this happens, half of the
/// local queue is moved to the injection queue.
/// local queue is moved to the global queue.
///
/// This metric only applies to the **multi-threaded** scheduler.
///
Expand Down Expand Up @@ -957,15 +955,15 @@ pub struct RuntimeMetrics {
/// - [`RuntimeMetrics::max_busy_duration`]
pub min_busy_duration: Duration,

/// The number of tasks currently scheduled in the runtime's injection queue.
/// The number of tasks currently scheduled in the runtime's global queue.
///
/// Tasks that are spawned or notified from a non-runtime thread are scheduled using the
/// runtime's injection queue. This metric returns the **current** number of tasks pending in
/// the injection queue. As such, the returned value may increase or decrease as new tasks are
/// runtime's global queue. This metric returns the **current** number of tasks pending in
/// the global queue. As such, the returned value may increase or decrease as new tasks are
/// scheduled and processed.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::injection_queue_depth`].
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::global_queue_depth`].
///
/// ##### Example
/// ```
Expand Down Expand Up @@ -1003,7 +1001,7 @@ pub struct RuntimeMetrics {
/// assert_eq!(interval.num_remote_schedules, 2);
/// # }
/// ```
pub injection_queue_depth: usize,
pub global_queue_depth: usize,

/// The total number of tasks currently scheduled in workers' local queues.
///
Expand Down Expand Up @@ -1143,7 +1141,7 @@ struct Worker {
total_overflow_count: u64,
total_polls_count: u64,
total_busy_duration: Duration,
poll_count_histogram: Vec<u64>,
poll_time_histogram: Vec<u64>,
}

/// Iterator returned by [`RuntimeMonitor::intervals`].
Expand Down Expand Up @@ -1172,7 +1170,7 @@ impl RuntimeIntervals {
let mut metrics = RuntimeMetrics {
workers_count: self.runtime.num_workers(),
elapsed: now - self.started_at,
injection_queue_depth: self.runtime.injection_queue_depth(),
global_queue_depth: self.runtime.global_queue_depth(),
num_remote_schedules: num_remote_schedules - self.num_remote_schedules,
min_park_count: u64::MAX,
min_noop_count: u64::MAX,
Expand All @@ -1183,7 +1181,7 @@ impl RuntimeIntervals {
min_busy_duration: Duration::from_secs(1000000000),
min_local_queue_depth: usize::MAX,
mean_poll_duration_worker_min: Duration::MAX,
poll_count_histogram: vec![0; self.runtime.poll_count_histogram_num_buckets()],
poll_time_histogram: vec![0; self.runtime.poll_time_histogram_num_buckets()],
budget_forced_yield_count: budget_forced_yields - self.budget_forced_yield_count,
io_driver_ready_count: io_driver_ready_events - self.io_driver_ready_count,
..Default::default()
Expand Down Expand Up @@ -1292,8 +1290,8 @@ impl RuntimeMonitor {

impl Worker {
fn new(worker: usize, rt: &runtime::RuntimeMetrics) -> Worker {
let poll_count_histogram = if rt.poll_count_histogram_enabled() {
vec![0; rt.poll_count_histogram_num_buckets()]
let poll_time_histogram = if rt.poll_time_histogram_enabled() {
vec![0; rt.poll_time_histogram_num_buckets()]
} else {
vec![]
};
Expand All @@ -1308,7 +1306,7 @@ impl Worker {
total_overflow_count: rt.worker_overflow_count(worker),
total_polls_count: rt.worker_poll_count(worker),
total_busy_duration: rt.worker_total_busy_duration(worker),
poll_count_histogram,
poll_time_histogram,
}
}

Expand Down Expand Up @@ -1411,10 +1409,10 @@ impl Worker {

// Update the histogram counts if there were polls since last count
if worker_polls_count > 0 {
for (bucket, cell) in metrics.poll_count_histogram.iter_mut().enumerate() {
let new = rt.poll_count_histogram_bucket_count(self.worker, bucket);
let delta = new - self.poll_count_histogram[bucket];
self.poll_count_histogram[bucket] = new;
for (bucket, cell) in metrics.poll_time_histogram.iter_mut().enumerate() {
let new = rt.poll_time_histogram_bucket_count(self.worker, bucket);
let delta = new - self.poll_time_histogram[bucket];
self.poll_time_histogram[bucket] = new;

*cell += delta;
}
Expand Down
12 changes: 6 additions & 6 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ use std::time::{Duration, Instant};
/// The culprit is likely some combination of:
/// - **Your tasks are accidentally blocking.** Common culprits include:
/// 1. Using the Rust standard library's [filesystem](https://doc.rust-lang.org/std/fs/) or
/// [networking](https://doc.rust-lang.org/std/net/) APIs.
/// [networking](https://doc.rust-lang.org/std/net/) APIs.
/// These APIs are synchronous; use tokio's [filesystem](https://docs.rs/tokio/latest/tokio/fs/)
/// and [networking](https://docs.rs/tokio/latest/tokio/net/) APIs, instead.
/// 3. Calling [`block_on`](https://docs.rs/tokio/latest/tokio/runtime/struct.Handle.html#method.block_on).
/// 4. Invoking `println!` or other synchronous logging routines.
/// Invocations of `println!` involve acquiring an exclusive lock on stdout, followed by a
/// synchronous write to stdout.
/// Invocations of `println!` involve acquiring an exclusive lock on stdout, followed by a
/// synchronous write to stdout.
/// 2. **Your tasks are computationally expensive.** Common culprits include:
/// 1. TLS/cryptographic routines
/// 2. doing a lot of processing on bytes
Expand All @@ -253,7 +253,7 @@ use std::time::{Duration, Instant};
/// You observed that [your tasks are spending more time waiting to be polled](#are-my-tasks-spending-more-time-waiting-to-be-polled)
/// suggesting some combination of:
/// - Your application is inflating the time elapsed between instrumentation and first poll.
/// - Your tasks are being scheduled into tokio's injection queue.
/// - Your tasks are being scheduled into tokio's global queue.
/// - Other tasks are spending too long without yielding, thus backing up tokio's queues.
///
/// Start by asking: [*Is time-to-first-poll unusually high?*](#is-time-to-first-poll-unusually-high)
Expand Down Expand Up @@ -307,9 +307,9 @@ use std::time::{Duration, Instant};
/// ```
///
/// Otherwise, [`mean_first_poll_delay`][TaskMetrics::mean_first_poll_delay] might be unusually high
/// because [*your application is spawning key tasks into tokio's injection queue...*](#is-my-application-spawning-more-tasks-into-tokio’s-injection-queue)
/// because [*your application is spawning key tasks into tokio's global queue...*](#is-my-application-spawning-more-tasks-into-tokio’s-global-queue)
///
/// ##### Is my application spawning more tasks into tokio's injection queue?
/// ##### Is my application spawning more tasks into tokio's global queue?
/// Tasks awoken from threads *not* managed by the tokio runtime are scheduled with a slower,
/// global "injection" queue.
///
Expand Down

0 comments on commit 28ab355

Please sign in to comment.