Skip to content

Commit

Permalink
metrics: add basic ods support to Counter
Browse files Browse the repository at this point in the history
Summary:
Now (when compiled by buck in fbsource) the Counter type also contains an ODS counter that gets updated as the Counter is updated.

Rather than using the dynamic counter in stats::define_stats macro, I directly instantiate a BoxSingletonCounter and store it in the Counter. This avoids computing the String metric name every time when incrementing.

Reviewed By: quark-zju

Differential Revision: D66848953

fbshipit-source-id: 71858669fb3c4e55339ed52bd2a5023aa7927e6b
  • Loading branch information
muirdm authored and facebook-github-bot committed Dec 6, 2024
1 parent cdcd438 commit cba4249
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
7 changes: 7 additions & 0 deletions eden/scm/lib/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ license = "MIT"
name = "metrics"

[dependencies]
fbinit = { version = "0.2.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main", optional = true }
futures = { version = "0.3.30", features = ["async-await", "compat"] }
once_cell = "1.12"
parking_lot = { version = "0.12.1", features = ["send_guard"] }
stats = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main", optional = true }
stats_traits = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main", optional = true }

[features]
default = []
ods = ["fbinit", "stats", "stats_traits"]
14 changes: 14 additions & 0 deletions eden/scm/lib/metrics/src/dummy_ods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

pub(crate) type Counter = ();

pub(crate) fn new_counter(_name: &'static str) -> Counter {
()
}

pub(crate) fn increment(_counter: &Counter, _value: i64) {}
21 changes: 14 additions & 7 deletions eden/scm/lib/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ use once_cell::sync::Lazy;
use once_cell::sync::OnceCell;
use parking_lot::RwLock;

#[cfg_attr(not(feature = "ods"), path = "dummy_ods.rs")]
mod ods;

pub struct Counter {
name: &'static str,
counter: OnceCell<AtomicUsize>,
counter: OnceCell<(AtomicUsize, ods::Counter)>,
gauge: bool,
}

Expand All @@ -43,15 +46,19 @@ impl Counter {
}

pub fn add(&'static self, val: usize) {
self.counter().fetch_add(val, Ordering::Relaxed);
let (counter, ods) = self.counter();
counter.fetch_add(val, Ordering::Relaxed);
ods::increment(ods, val as i64);
}

pub fn sub(&'static self, val: usize) {
self.counter().fetch_sub(val, Ordering::Relaxed);
let (counter, ods) = self.counter();
counter.fetch_sub(val, Ordering::Relaxed);
ods::increment(ods, -(val as i64));
}

pub fn value(&'static self) -> usize {
self.counter().load(Ordering::Relaxed)
self.counter().0.load(Ordering::Relaxed)
}

/// Increment counter by v and decrement it back by v when returned guard is dropped
Expand All @@ -64,10 +71,10 @@ impl Counter {
self.gauge
}

fn counter(&'static self) -> &AtomicUsize {
fn counter(&'static self) -> &(AtomicUsize, ods::Counter) {
self.counter.get_or_init(|| {
Registry::global().register_counter(self);
AtomicUsize::new(0)
(AtomicUsize::new(0), ods::new_counter(self.name))
})
}
}
Expand Down Expand Up @@ -115,7 +122,7 @@ impl Registry {

pub fn reset(&self) {
for counter in self.counters.read().values() {
counter.counter().store(0, Ordering::Relaxed);
counter.counter().0.store(0, Ordering::Relaxed);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions eden/scm/lib/metrics/src/ods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

pub(crate) type Counter = stats_traits::stat_types::BoxSingletonCounter;

pub(crate) fn new_counter(name: &'static str) -> Counter {
stats::create_singleton_counter(name.to_string())
}

pub(crate) fn increment(counter: &Counter, value: i64) {
if !fbinit::was_performed() {
return;
}

counter.increment_value(fbinit::expect_init(), value);
}

0 comments on commit cba4249

Please sign in to comment.