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(self.meta(), queried_ping_name) {
260 Some(Metric::MemoryDistribution(hist)) => Some(snapshot(&hist)),
261 _ => None,
262 }
263 }
264
265 /// **Exported for test purposes.**
266 ///
267 /// Gets the number of recorded errors for the given metric and error type.
268 ///
269 /// # Arguments
270 ///
271 /// * `error` - The type of error
272 ///
273 /// # Returns
274 ///
275 /// The number of errors reported.
276 pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
277 crate::block_on_dispatcher();
278
279 crate::core::with_glean(|glean| {
280 test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0)
281 })
282 }
283
284 /// **Experimental:** Start a new histogram buffer associated with this memory distribution metric.
285 ///
286 /// A histogram buffer accumulates in-memory.
287 /// Data is recorded into the metric on drop.
288 pub fn start_buffer(&self) -> LocalMemoryDistribution<'_> {
289 LocalMemoryDistribution::new(self)
290 }
291
292 fn commit_histogram(&self, histogram: Histogram<Functional>, errors: usize) {
293 let metric = self.clone();
294 crate::launch_with_glean(move |glean| {
295 if errors > 0 {
296 let msg = format!("Accumulated {} samples larger than 1TB", errors);
297 record_error(
298 glean,
299 &metric.meta,
300 ErrorType::InvalidValue,
301 msg,
302 Some(errors as i32),
303 );
304 }
305
306 glean
307 .storage()
308 .record_with(glean, &metric.meta, move |old_value| {
309 let mut hist = match old_value {
310 Some(Metric::MemoryDistribution(hist)) => hist,
311 _ => Histogram::functional(LOG_BASE, BUCKETS_PER_MAGNITUDE),
312 };
313
314 hist.merge(&histogram);
315 Metric::MemoryDistribution(hist)
316 });
317 });
318 }
319}
320
321impl TestGetValue for MemoryDistributionMetric {
322 type Output = DistributionData;
323
324 /// **Test-only API (exported for FFI purposes).**
325 ///
326 /// Gets the currently stored value.
327 ///
328 /// This doesn't clear the stored value.
329 ///
330 /// # Arguments
331 ///
332 /// * `ping_name` - the optional name of the ping to retrieve the metric
333 /// for. Defaults to the first value in `send_in_pings`.
334 ///
335 /// # Returns
336 ///
337 /// The stored value or `None` if nothing stored.
338 fn test_get_value(&self, ping_name: Option<String>) -> Option<DistributionData> {
339 crate::block_on_dispatcher();
340 crate::core::with_glean(|glean| self.get_value(glean, ping_name.as_deref()))
341 }
342}
343
344/// **Experimental:** A histogram buffer associated with a specific instance of a [`MemoryDistributionMetric`].
345///
346/// Accumulation happens in-memory.
347/// Data is merged into the metric on [`Drop::drop`].
348#[derive(Debug)]
349pub struct LocalMemoryDistribution<'a> {
350 histogram: Histogram<Functional>,
351 metric: &'a MemoryDistributionMetric,
352 errors: usize,
353}
354
355impl<'a> LocalMemoryDistribution<'a> {
356 /// Create a new histogram buffer referencing the memory distribution it will record into.
357 fn new(metric: &'a MemoryDistributionMetric) -> Self {
358 let histogram = Histogram::functional(LOG_BASE, BUCKETS_PER_MAGNITUDE);
359 Self {
360 histogram,
361 metric,
362 errors: 0,
363 }
364 }
365
366 /// Accumulates one sample into the histogram.
367 ///
368 /// The provided sample must be in the "unit" declared by the instance of the metric type
369 /// (e.g. if the instance this method was called on is using [`crate::MemoryUnit::Kilobyte`], then
370 /// `sample` is assumed to be in kilobytes).
371 ///
372 /// Accumulation happens in-memory only.
373 pub fn accumulate(&mut self, sample: u64) {
374 let mut sample = self.metric.memory_unit.as_bytes(sample);
375 if sample > MAX_BYTES {
376 self.errors += 1;
377 sample = MAX_BYTES;
378 }
379 self.histogram.accumulate(sample)
380 }
381
382 /// Abandon this histogram buffer and don't commit accumulated data.
383 pub fn abandon(mut self) {
384 // Replace any recordings with an empty histogram.
385 self.histogram.clear();
386 }
387}
388
389impl Drop for LocalMemoryDistribution<'_> {
390 fn drop(&mut self) {
391 if self.histogram.is_empty() {
392 return;
393 }
394
395 // We want to move that value.
396 // A `0/0` histogram doesn't allocate.
397 let buffer = mem::replace(&mut self.histogram, Histogram::functional(0.0, 0.0));
398 self.metric.commit_histogram(buffer, self.errors);
399 }
400}