fuchsia_inspect/writer/types/
double_linear_histogram.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::writer::{
6    ArithmeticArrayProperty, ArrayProperty, DoubleArrayProperty, HistogramProperty, InspectType,
7    Node,
8};
9use diagnostics_hierarchy::{ArrayFormat, LinearHistogramParams};
10use inspect_format::constants;
11use log::error;
12use std::borrow::Cow;
13
14#[derive(Debug, Default)]
15/// A linear histogram property for double values.
16pub struct DoubleLinearHistogramProperty {
17    array: DoubleArrayProperty,
18    floor: f64,
19    slots: usize,
20    step_size: f64,
21}
22
23impl InspectType for DoubleLinearHistogramProperty {}
24
25impl DoubleLinearHistogramProperty {
26    pub(crate) fn new(
27        name: Cow<'_, str>,
28        params: LinearHistogramParams<f64>,
29        parent: &Node,
30    ) -> Self {
31        let slots = params.buckets + constants::LINEAR_HISTOGRAM_EXTRA_SLOTS;
32        let array = parent.create_double_array_internal(name, slots, ArrayFormat::LinearHistogram);
33        array.set(0, params.floor);
34        array.set(1, params.step_size);
35        Self { floor: params.floor, step_size: params.step_size, slots, array }
36    }
37
38    fn get_index(&self, value: f64) -> usize {
39        let mut current_floor = self.floor;
40        // Start in the underflow index.
41        let mut index = constants::LINEAR_HISTOGRAM_EXTRA_SLOTS - 2;
42        while value >= current_floor && index < self.slots - 1 {
43            current_floor += self.step_size;
44            index += 1;
45        }
46        index
47    }
48}
49
50impl HistogramProperty for DoubleLinearHistogramProperty {
51    type Type = f64;
52
53    fn insert(&self, value: f64) {
54        self.insert_multiple(value, 1);
55    }
56
57    fn insert_multiple(&self, value: f64, count: usize) {
58        self.array.add(self.get_index(value), count as f64);
59    }
60
61    fn clear(&self) {
62        if let Some(ref inner_ref) = self.array.inner.inner_ref() {
63            // Ensure we don't delete the array slots that contain histogram metadata.
64            inner_ref
65                .state
66                .try_lock()
67                .and_then(|mut state| {
68                    // -2 = the overflow and underflow slots which still need to be cleared.
69                    state.clear_array(
70                        inner_ref.block_index,
71                        constants::LINEAR_HISTOGRAM_EXTRA_SLOTS - 2,
72                    )
73                })
74                .unwrap_or_else(|err| {
75                    error!(err:?; "Failed to clear property");
76                });
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84    use crate::writer::testing_utils::GetBlockExt;
85    use crate::writer::Inspector;
86    use inspect_format::{Array, Double};
87
88    #[fuchsia::test]
89    fn double_linear_histogram() {
90        let inspector = Inspector::default();
91        let root = inspector.root();
92        let node = root.create_child("node");
93        {
94            let double_histogram = node.create_double_linear_histogram(
95                "double-histogram",
96                LinearHistogramParams { floor: 10.0, step_size: 5.0, buckets: 5 },
97            );
98            double_histogram.insert_multiple(0.0, 2); // underflow
99            double_histogram.insert(25.3);
100            double_histogram.insert(500.0); // overflow
101            double_histogram.array.get_block::<_, Array<Double>>(|block| {
102                for (i, value) in [10.0, 5.0, 2.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0].iter().enumerate()
103                {
104                    assert_eq!(block.get(i).unwrap(), *value);
105                }
106            });
107
108            node.get_block::<_, inspect_format::Node>(|node_block| {
109                assert_eq!(node_block.child_count(), 1);
110            });
111        }
112        node.get_block::<_, inspect_format::Node>(|node_block| {
113            assert_eq!(node_block.child_count(), 0);
114        });
115    }
116}