From 00d246bc01cd8e18be35a22d2e328dcbf4580b72 Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Tue, 9 Apr 2024 16:59:36 +0200 Subject: [PATCH] fix(core/telemetry) do not produce allocator metrics if not enabled --- CHANGELOG.md | 1 + elfo-core/src/supervisor/measure_poll.rs | 20 +++++++++++++++----- examples/Cargo.toml | 4 ++++ examples/examples/usage/main.rs | 6 ++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b0fff55..fba31be1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - telemetry: now `elfo_message_handling_time_seconds` doesn't include the time of task switching if an actor is preempted due to elfo's budget system. +- telemetry: don't produce allocator metrics if not enabled. - telemeter: validate quantiles (0.0..=1.0) in the config. ## [0.2.0-alpha.14] - 2024-02-27 diff --git a/elfo-core/src/supervisor/measure_poll.rs b/elfo-core/src/supervisor/measure_poll.rs index 1a4e7c56..9f9b3ad2 100644 --- a/elfo-core/src/supervisor/measure_poll.rs +++ b/elfo-core/src/supervisor/measure_poll.rs @@ -54,11 +54,7 @@ impl Future for MeasurePoll { let res = this.inner.poll(cx); let elapsed = Instant::now().secs_f64_since(start_time); recorder.record_histogram(&BUSY_TIME_SECONDS, elapsed); - crate::scope::with(|scope| { - recorder.increment_counter(&ALLOCATED_BYTES, scope.take_allocated_bytes() as u64); - recorder - .increment_counter(&DEALLOCATED_BYTES, scope.take_deallocated_bytes() as u64); - }); + publish_alloc_metrics(recorder); res } else { crate::coop::reset(None); @@ -72,3 +68,17 @@ impl Future for MeasurePoll { result } } + +fn publish_alloc_metrics(recorder: &dyn metrics::Recorder) { + crate::scope::with(|scope| { + let allocated = scope.take_allocated_bytes(); + let deallocated = scope.take_deallocated_bytes(); + + if allocated > 0 { + recorder.increment_counter(&ALLOCATED_BYTES, allocated as u64); + } + if deallocated > 0 { + recorder.increment_counter(&DEALLOCATED_BYTES, deallocated as u64); + } + }); +} diff --git a/examples/Cargo.toml b/examples/Cargo.toml index ea0a6d76..bc1e05f8 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -9,8 +9,12 @@ license.workspace = true edition.workspace = true readme.workspace = true +[features] +unstable = ["elfo/unstable", "elfo-telemeter/unstable"] + [dev-dependencies] elfo = { path = "../elfo", features = ["full", "network", "test-util"] } +elfo-telemeter = { path = "../elfo-telemeter" } # for `AllocatorStats` anyhow = "1.0.40" futures = "0.3.12" diff --git a/examples/examples/usage/main.rs b/examples/examples/usage/main.rs index 4d4869bc..e7c1cfd8 100644 --- a/examples/examples/usage/main.rs +++ b/examples/examples/usage/main.rs @@ -311,6 +311,12 @@ fn topology() -> elfo::Topology { // setup //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Optionally, the allocation statistics can be also enabled. +#[cfg(feature = "unstable")] // TODO: stabilize it +#[global_allocator] +static ALLOCATOR: elfo_telemeter::AllocatorStats = + elfo_telemeter::AllocatorStats::new(std::alloc::System); + #[tokio::main] async fn main() { elfo::init::start(topology()).await;