Skip to content

Commit

Permalink
fix(core/telemetry) do not produce allocator metrics if not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Apr 9, 2024
1 parent 69f9c78 commit 00d246b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions elfo-core/src/supervisor/measure_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ impl<F: Future> Future for MeasurePoll<F> {
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);
Expand All @@ -72,3 +68,17 @@ impl<F: Future> Future for MeasurePoll<F> {
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);
}
});
}
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions examples/examples/usage/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::alloc::System> =
elfo_telemeter::AllocatorStats::new(std::alloc::System);

#[tokio::main]
async fn main() {
elfo::init::start(topology()).await;
Expand Down

0 comments on commit 00d246b

Please sign in to comment.