mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-19 16:25:38 +00:00
Bug 1635255 - Skeleton IPC impl for Counter r=janerik
Differential Revision: https://phabricator.services.mozilla.com/D79746
This commit is contained in:
parent
61ff9175f3
commit
a622fa625c
@ -17,7 +17,9 @@ use {
|
||||
/// Contains all the information necessary to update the metrics on the main
|
||||
/// process.
|
||||
#[derive(Debug)]
|
||||
pub struct IPCPayload {}
|
||||
pub struct IPCPayload {
|
||||
pub counters: HashMap<MetricId, i32>,
|
||||
}
|
||||
|
||||
/// Uniquely identifies a single metric within its metric type.
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||
@ -36,7 +38,11 @@ impl MetricId {
|
||||
}
|
||||
|
||||
/// Global singleton: pending IPC payload.
|
||||
static PAYLOAD: Lazy<Mutex<IPCPayload>> = Lazy::new(|| Mutex::new(IPCPayload {}));
|
||||
static PAYLOAD: Lazy<Mutex<IPCPayload>> = Lazy::new(|| {
|
||||
Mutex::new(IPCPayload {
|
||||
counters: HashMap::new(),
|
||||
})
|
||||
});
|
||||
|
||||
pub fn with_ipc_payload<F, R>(f: F) -> R
|
||||
where
|
||||
|
@ -4,17 +4,30 @@
|
||||
|
||||
use super::CommonMetricData;
|
||||
|
||||
use crate::ipc::{need_ipc, with_ipc_payload, MetricId};
|
||||
|
||||
/// A counter metric.
|
||||
///
|
||||
/// Used to count things.
|
||||
/// The value can only be incremented, not decremented.
|
||||
#[derive(Debug)]
|
||||
pub enum CounterMetric {
|
||||
Parent(CounterMetricImpl),
|
||||
Child(CounterMetricIpc),
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CounterMetric(pub(crate) glean_core::metrics::CounterMetric);
|
||||
pub struct CounterMetricImpl(pub(crate) glean_core::metrics::CounterMetric);
|
||||
#[derive(Debug)]
|
||||
pub struct CounterMetricIpc(MetricId);
|
||||
|
||||
impl CounterMetric {
|
||||
/// Create a new counter metric.
|
||||
pub fn new(meta: CommonMetricData) -> Self {
|
||||
Self(glean_core::metrics::CounterMetric::new(meta))
|
||||
if need_ipc() {
|
||||
CounterMetric::Child(CounterMetricIpc(MetricId::new(meta)))
|
||||
} else {
|
||||
CounterMetric::Parent(CounterMetricImpl::new(meta))
|
||||
}
|
||||
}
|
||||
|
||||
/// Increase the counter by `amount`.
|
||||
@ -27,7 +40,18 @@ impl CounterMetric {
|
||||
///
|
||||
/// Logs an error if the `amount` is 0 or negative.
|
||||
pub fn add(&self, amount: i32) {
|
||||
crate::with_glean(move |glean| self.0.add(glean, amount))
|
||||
match self {
|
||||
CounterMetric::Parent(p) => p.add(amount),
|
||||
CounterMetric::Child(c) => {
|
||||
with_ipc_payload(move |payload| {
|
||||
if let Some(v) = payload.counters.get_mut(&c.0) {
|
||||
*v += amount;
|
||||
} else {
|
||||
payload.counters.insert(c.0.clone(), amount);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// **Test-only API.**
|
||||
@ -42,6 +66,26 @@ impl CounterMetric {
|
||||
/// ## Return value
|
||||
///
|
||||
/// Returns the stored value or `None` if nothing stored.
|
||||
pub fn test_get_value(&self, storage_name: &str) -> Option<i32> {
|
||||
match self {
|
||||
CounterMetric::Parent(p) => p.test_get_value(storage_name),
|
||||
CounterMetric::Child(_c) => panic!(
|
||||
"Cannot get test value for {:?} in non-parent process!",
|
||||
self
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CounterMetricImpl {
|
||||
pub fn new(meta: CommonMetricData) -> Self {
|
||||
Self(glean_core::metrics::CounterMetric::new(meta))
|
||||
}
|
||||
|
||||
pub fn add(&self, amount: i32) {
|
||||
crate::with_glean(move |glean| self.0.add(glean, amount))
|
||||
}
|
||||
|
||||
pub fn test_get_value(&self, storage_name: &str) -> Option<i32> {
|
||||
crate::with_glean(move |glean| self.0.test_get_value(glean, storage_name))
|
||||
}
|
||||
|
@ -71,7 +71,8 @@ mod private {
|
||||
type Inner = glean_core::metrics::CounterMetric;
|
||||
|
||||
fn from_inner(metric: Self::Inner) -> Self {
|
||||
CounterMetric(metric)
|
||||
assert!(!need_ipc());
|
||||
CounterMetric::Parent(crate::metrics::counter::CounterMetricImpl(metric))
|
||||
}
|
||||
|
||||
fn new_inner(meta: CommonMetricData) -> Self::Inner {
|
||||
|
Loading…
Reference in New Issue
Block a user