Skip to main content

glean_core/metrics/
memory_distribution.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::mem;
6use std::sync::Arc;
7
8use crate::common_metric_data::{CommonMetricDataInternal, MetricLabel};
9use crate::error_recording::{record_error, test_get_num_recorded_errors, ErrorType};
10use crate::histogram::{Functional, Histogram};
11use crate::metrics::memory_unit::MemoryUnit;
12use crate::metrics::{DistributionData, Metric, MetricType};
13use crate::Glean;
14use crate::{CommonMetricData, TestGetValue};
15
16// The base of the logarithm used to determine bucketing
17const LOG_BASE: f64 = 2.0;
18
19// The buckets per each order of magnitude of the logarithm.
20const BUCKETS_PER_MAGNITUDE: f64 = 16.0;
21
22// Set a maximum recordable value of 1 terabyte so the buckets aren't
23// completely unbounded.
24const MAX_BYTES: u64 = 1 << 40;
25
26/// A memory distribution metric.
27///
28/// Memory distributions are used to accumulate and store memory sizes.
29#[derive(Clone, Debug)]
30pub struct MemoryDistributionMetric {
31    meta: Arc<CommonMetricDataInternal>,
32    memory_unit: MemoryUnit,
33}
34
35/// Create a snapshot of the histogram.
36///
37/// The snapshot can be serialized into the payload format.
38pub(crate) fn snapshot(hist: &Histogram<Functional>) -> DistributionData {
39    DistributionData {
40        // **Caution**: This cannot use `Histogram::snapshot_values` and needs to use the more
41        // specialized snapshot function.
42        values: hist
43            .snapshot()
44            .iter()
45            .map(|(&k, &v)| (k as i64, v as i64))
46            .collect(),
47        sum: hist.sum() as i64,
48        count: hist.count() as i64,
49    }
50}
51
52impl MetricType for MemoryDistributionMetric {
53    fn meta(&self) -> &CommonMetricDataInternal {
54        &self.meta
55    }
56
57    fn with_name(&self, name: String) -> Self {
58        let mut meta = (*self.meta).clone();
59        meta.inner.name = name;
60        Self {
61            meta: Arc::new(meta),
62            memory_unit: self.memory_unit,
63        }
64    }
65
66    fn with_label(&self, label: MetricLabel) -> Self {
67        let mut meta = (*self.meta).clone();
68        meta.inner.label = Some(label);
69        Self {
70            meta: Arc::new(meta),
71            memory_unit: self.memory_unit,
72        }
73    }
74}
75
76// IMPORTANT:
77//
78// When changing this implementation, make sure all the operations are
79// also declared in the related trait in `../traits/`.
80impl MemoryDistributionMetric {
81    /// Creates a new memory distribution metric.
82    pub fn new(meta: CommonMetricData, memory_unit: MemoryUnit) -> Self {
83        Self {
84            meta: Arc::new(meta.into()),
85            memory_unit,
86        }
87    }
88
89    /// Accumulates the provided sample in the metric.
90    ///
91    /// # Arguments
92    ///
93    /// * `sample` - The sample to be recorded by the metric. The sample is assumed to be in the
94    ///   configured memory unit of the metric.
95    ///
96    /// ## Notes
97    ///
98    /// Values bigger than 1 Terabyte (2<sup>40</sup> bytes) are truncated
99    /// and an [`ErrorType::InvalidValue`] error is recorded.
100    pub fn accumulate(&self, sample: i64) {
101        let metric = self.clone();
102        crate::launch_with_glean(move |glean| metric.accumulate_sync(glean, sample))
103    }
104
105    /// Accumulates the provided sample in the metric synchronously.
106    ///
107    /// See [`accumulate`](Self::accumulate) for details.
108    #[doc(hidden)]
109    pub fn accumulate_sync(&self, glean: &Glean, sample: i64) {
110        if !self.should_record(glean) {
111            return;
112        }
113
114        if sample < 0 {
115            record_error(
116                glean,
117                &self.meta,
118                ErrorType::InvalidValue,
119                "Accumulated a negative sample",
120                None,
121            );
122            return;
123        }
124
125        let mut sample = self.memory_unit.as_bytes(sample as u64);
126
127        if sample > MAX_BYTES {
128            let msg = "Sample is bigger than 1 terabyte";
129            record_error(glean, &self.meta, ErrorType::InvalidValue, msg, None);
130            sample = MAX_BYTES;
131        }
132
133        // Let's be defensive here:
134        // The uploader tries to store some memory distribution metrics,
135        // but in tests that storage might be gone already.
136        // Let's just ignore those.
137        // We do the same for counters and timing distributions.
138        // This should never happen in real app usage.
139        if let Some(storage) = glean.storage_opt() {
140            storage.record_with(glean, &self.meta, |old_value| match old_value {
141                Some(Metric::MemoryDistribution(mut hist)) => {
142                    hist.accumulate(sample);
143                    Metric::MemoryDistribution(hist)
144                }
145                _ => {
146                    let mut hist = Histogram::functional(LOG_BASE, BUCKETS_PER_MAGNITUDE);
147                    hist.accumulate(sample);
148                    Metric::MemoryDistribution(hist)
149                }
150            });
151        } else {
152            log::warn!(
153                "Couldn't get storage. Can't record memory distribution '{}'.",
154                self.meta.base_identifier()
155            );
156        }
157    }
158
159    /// Accumulates the provided signed samples in the metric.
160    ///
161    /// This is required so that the platform-specific code can provide us with
162    /// 64 bit signed integers if no `u64` comparable type is available. This
163    /// will take care of filtering and reporting errors for any provided negative
164    /// sample.
165    ///
166    /// Please note that this assumes that the provided samples are already in
167    /// the "unit" declared by the instance of the metric type (e.g. if the the
168    /// instance this method was called on is using [`MemoryUnit::Kilobyte`], then
169    /// `samples` are assumed to be in that unit).
170    ///
171    /// # Arguments
172    ///
173    /// * `samples` - The vector holding the samples to be recorded by the metric.
174    ///
175    /// ## Notes
176    ///
177    /// Discards any negative value in `samples` and report an [`ErrorType::InvalidValue`]
178    /// for each of them.
179    ///
180    /// Values bigger than 1 Terabyte (2<sup>40</sup> bytes) are truncated
181    /// and an [`ErrorType::InvalidValue`] error is recorded.
182    pub fn accumulate_samples(&self, samples: Vec<i64>) {
183        let metric = self.clone();
184        crate::launch_with_glean(move |glean| metric.accumulate_samples_sync(glean, samples))
185    }
186
187    /// Accumulates the provided signed samples in the metric synchronously.
188    ///
189    /// See [`accumulate_samples`](Self::accumulate_samples) for details.
190    #[doc(hidden)]
191    pub fn accumulate_samples_sync(&self, glean: &Glean, samples: Vec<i64>) {
192        if !self.should_record(glean) {
193            return;
194        }
195
196        let mut num_negative_samples = 0;
197        let mut num_too_log_samples = 0;
198
199        glean.storage().record_with(glean, &self.meta, |old_value| {
200            let mut hist = match old_value {
201                Some(Metric::MemoryDistribution(hist)) => hist,
202                _ => Histogram::functional(LOG_BASE, BUCKETS_PER_MAGNITUDE),
203            };
204
205            for &sample in samples.iter() {
206                if sample < 0 {
207                    num_negative_samples += 1;
208                } else {
209                    let sample = sample as u64;
210                    let mut sample = self.memory_unit.as_bytes(sample);
211                    if sample > MAX_BYTES {
212                        num_too_log_samples += 1;
213                        sample = MAX_BYTES;
214                    }
215
216                    hist.accumulate(sample);
217                }
218            }
219            Metric::MemoryDistribution(hist)
220        });
221
222        if num_negative_samples > 0 {
223            let msg = format!("Accumulated {} negative samples", num_negative_samples);
224            record_error(
225                glean,
226                &self.meta,
227                ErrorType::InvalidValue,
228                msg,
229                num_negative_samples,
230            );
231        }
232
233        if num_too_log_samples > 0 {
234            let msg = format!(
235                "Accumulated {} samples larger than 1TB",
236                num_too_log_samples
237            );
238            record_error(
239                glean,
240                &self.meta,
241                ErrorType::InvalidValue,
242                msg,
243                num_too_log_samples,
244            );
245        }
246    }
247
248    /// Gets the currently stored value synchronously.
249    #[doc(hidden)]
250    pub fn get_value<'a, S: Into<Option<&'a str>>>(
251        &self,
252        glean: &Glean,
253        ping_name: S,
254    ) -> Option<DistributionData> {
255        let queried_ping_name = ping_name
256            .into()
257            .unwrap_or_else(|| &self.meta().inner.send_in_pings[0]);
258
259        match glean.storage().get_metric(
260            #[cfg(not(feature = "sqlite"))]
261            glean,
262            self.meta(),
263            queried_ping_name,
264        ) {
265            Some(Metric::MemoryDistribution(hist)) => Some(snapshot(&hist)),
266            _ => None,
267        }
268    }
269
270    /// **Exported for test purposes.**
271    ///
272    /// Gets the number of recorded errors for the given metric and error type.
273    ///
274    /// # Arguments
275    ///
276    /// * `error` - The type of error
277    ///
278    /// # Returns
279    ///
280    /// The number of errors reported.
281    pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
282        crate::block_on_dispatcher();
283
284        crate::core::with_glean(|glean| {
285            test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0)
286        })
287    }
288
289    /// **Experimental:** Start a new histogram buffer associated with this memory distribution metric.
290    ///
291    /// A histogram buffer accumulates in-memory.
292    /// Data is recorded into the metric on drop.
293    pub fn start_buffer(&self) -> LocalMemoryDistribution<'_> {
294        LocalMemoryDistribution::new(self)
295    }
296
297    fn commit_histogram(&self, histogram: Histogram<Functional>, errors: usize) {
298        let metric = self.clone();
299        crate::launch_with_glean(move |glean| {
300            if errors > 0 {
301                let msg = format!("Accumulated {} samples larger than 1TB", errors);
302                record_error(
303                    glean,
304                    &metric.meta,
305                    ErrorType::InvalidValue,
306                    msg,
307                    Some(errors as i32),
308                );
309            }
310
311            glean
312                .storage()
313                .record_with(glean, &metric.meta, move |old_value| {
314                    let mut hist = match old_value {
315                        Some(Metric::MemoryDistribution(hist)) => hist,
316                        _ => Histogram::functional(LOG_BASE, BUCKETS_PER_MAGNITUDE),
317                    };
318
319                    hist.merge(&histogram);
320                    Metric::MemoryDistribution(hist)
321                });
322        });
323    }
324}
325
326impl TestGetValue for MemoryDistributionMetric {
327    type Output = DistributionData;
328
329    /// **Test-only API (exported for FFI purposes).**
330    ///
331    /// Gets the currently stored value.
332    ///
333    /// This doesn't clear the stored value.
334    ///
335    /// # Arguments
336    ///
337    /// * `ping_name` - the optional name of the ping to retrieve the metric
338    ///                 for. Defaults to the first value in `send_in_pings`.
339    ///
340    /// # Returns
341    ///
342    /// The stored value or `None` if nothing stored.
343    fn test_get_value(&self, ping_name: Option<String>) -> Option<DistributionData> {
344        crate::block_on_dispatcher();
345        crate::core::with_glean(|glean| self.get_value(glean, ping_name.as_deref()))
346    }
347}
348
349/// **Experimental:** A histogram buffer associated with a specific instance of a [`MemoryDistributionMetric`].
350///
351/// Accumulation happens in-memory.
352/// Data is merged into the metric on [`Drop::drop`].
353#[derive(Debug)]
354pub struct LocalMemoryDistribution<'a> {
355    histogram: Histogram<Functional>,
356    metric: &'a MemoryDistributionMetric,
357    errors: usize,
358}
359
360impl<'a> LocalMemoryDistribution<'a> {
361    /// Create a new histogram buffer referencing the memory distribution it will record into.
362    fn new(metric: &'a MemoryDistributionMetric) -> Self {
363        let histogram = Histogram::functional(LOG_BASE, BUCKETS_PER_MAGNITUDE);
364        Self {
365            histogram,
366            metric,
367            errors: 0,
368        }
369    }
370
371    /// Accumulates one sample into the histogram.
372    ///
373    /// The provided sample must be in the "unit" declared by the instance of the metric type
374    /// (e.g. if the instance this method was called on is using [`crate::MemoryUnit::Kilobyte`], then
375    /// `sample` is assumed to be in kilobytes).
376    ///
377    /// Accumulation happens in-memory only.
378    pub fn accumulate(&mut self, sample: u64) {
379        let mut sample = self.metric.memory_unit.as_bytes(sample);
380        if sample > MAX_BYTES {
381            self.errors += 1;
382            sample = MAX_BYTES;
383        }
384        self.histogram.accumulate(sample)
385    }
386
387    /// Abandon this histogram buffer and don't commit accumulated data.
388    pub fn abandon(mut self) {
389        // Replace any recordings with an empty histogram.
390        self.histogram.clear();
391    }
392}
393
394impl Drop for LocalMemoryDistribution<'_> {
395    fn drop(&mut self) {
396        if self.histogram.is_empty() {
397            return;
398        }
399
400        // We want to move that value.
401        // A `0/0` histogram doesn't allocate.
402        let buffer = mem::replace(&mut self.histogram, Histogram::functional(0.0, 0.0));
403        self.metric.commit_histogram(buffer, self.errors);
404    }
405}