fuchsia_inspect_contrib/nodes/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Utilities and wrappers providing higher level functionality for Inspect Nodes and properties.

use std::{fmt, marker};

mod list;
mod lru_cache;

pub use list::BoundedListNode;
pub use lru_cache::LruCacheNode;
pub use zx::{BootTimeline, MonotonicTimeline};

use fuchsia_inspect::{InspectType, IntProperty, Node, Property, StringReference};

/// Implemented by timelines for which we can get the current time.
pub trait GetCurrentTime: zx::Timeline + Sized {
    fn get_current_time() -> zx::Instant<Self>;
}

impl GetCurrentTime for zx::MonotonicTimeline {
    fn get_current_time() -> zx::MonotonicInstant {
        zx::MonotonicInstant::get()
    }
}

impl GetCurrentTime for zx::BootTimeline {
    fn get_current_time() -> zx::BootInstant {
        zx::BootInstant::get()
    }
}

/// Returned by functions which take the current time and write it to a property.
pub struct CreateTimeResult<T> {
    /// The time written to the property.
    pub timestamp: zx::Instant<T>,
    /// A property to which the timestamp was written.
    pub property: TimeProperty<T>,
}

/// Extension trait that allows to manage timestamp properties.
pub trait NodeTimeExt<T: zx::Timeline> {
    /// Creates a new property holding the current timestamp on the given timeline. Returns the
    /// current timestamp that was used for the returned property too.
    fn create_time(&self, name: impl Into<StringReference>) -> CreateTimeResult<T>;

    /// Creates a new property holding the given timestamp.
    fn create_time_at(
        &self,
        name: impl Into<StringReference>,
        timestamp: zx::Instant<T>,
    ) -> TimeProperty<T>;

    /// Records a new property holding the current timestamp and returns the instant that was
    /// recorded.
    fn record_time(&self, name: impl Into<StringReference>) -> zx::Instant<T>;
}

impl<T> NodeTimeExt<T> for Node
where
    T: zx::Timeline + GetCurrentTime,
{
    fn create_time(&self, name: impl Into<StringReference>) -> CreateTimeResult<T> {
        let timestamp = T::get_current_time();
        CreateTimeResult { timestamp, property: self.create_time_at(name, timestamp) }
    }

    fn create_time_at(
        &self,
        name: impl Into<StringReference>,
        timestamp: zx::Instant<T>,
    ) -> TimeProperty<T> {
        TimeProperty {
            inner: self.create_int(name, timestamp.into_nanos()),
            _phantom: marker::PhantomData,
        }
    }

    fn record_time(&self, name: impl Into<StringReference>) -> zx::Instant<T> {
        let instant = T::get_current_time();
        self.record_int(name, instant.into_nanos());
        instant
    }
}

/// Wrapper around an int property that stores a monotonic timestamp.
#[derive(Debug)]
pub struct TimeProperty<T> {
    pub(crate) inner: IntProperty,
    _phantom: marker::PhantomData<T>,
}

impl<T> TimeProperty<T>
where
    T: zx::Timeline + GetCurrentTime,
{
    /// Updates the underlying property with the current monotonic timestamp.
    pub fn update(&self) {
        self.set_at(T::get_current_time());
    }

    /// Updates the underlying property with the given timestamp.
    pub fn set_at(&self, timestamp: zx::Instant<T>) {
        Property::set(&self.inner, timestamp.into_nanos());
    }
}

/// An Inspect Time Property on the boot timeline.
pub type BootTimeProperty = TimeProperty<zx::BootTimeline>;

/// An Inspect Time Property on the monotonictimeline.
pub type MonotonicTimeProperty = TimeProperty<zx::MonotonicTimeline>;

impl<T: fmt::Debug + Send + Sync> InspectType for TimeProperty<T> {}

#[cfg(test)]
mod tests {
    use super::*;
    use diagnostics_assertions::{assert_data_tree, AnyProperty, PropertyAssertion};
    use fuchsia_inspect::{DiagnosticsHierarchyGetter, Inspector};
    use test_util::assert_lt;

    #[fuchsia::test]
    fn test_time_metadata_format() {
        let inspector = Inspector::default();

        let time_property = inspector
            .root()
            .create_time_at("time", zx::MonotonicInstant::from_nanos(123_456_700_000));
        let t1 = validate_inspector_get_time(&inspector, 123_456_700_000i64);

        time_property.set_at(zx::MonotonicInstant::from_nanos(333_005_000_000));
        let t2 = validate_inspector_get_time(&inspector, 333_005_000_000i64);

        time_property.set_at(zx::MonotonicInstant::from_nanos(333_444_000_000));
        let t3 = validate_inspector_get_time(&inspector, 333_444_000_000i64);

        assert_lt!(t1, t2);
        assert_lt!(t2, t3);
    }

    #[fuchsia::test]
    fn test_create_time_and_update() {
        let inspector = Inspector::default();
        let CreateTimeResult { timestamp: recorded_t1, property: time_property }: CreateTimeResult<
            zx::MonotonicTimeline,
        > = inspector.root().create_time("time");
        let t1 = validate_inspector_get_time(&inspector, AnyProperty);
        assert_eq!(recorded_t1.into_nanos(), t1);

        time_property.update();
        let t2 = validate_inspector_get_time(&inspector, AnyProperty);

        time_property.update();
        let t3 = validate_inspector_get_time(&inspector, AnyProperty);

        assert_lt!(t1, t2);
        assert_lt!(t2, t3);
    }

    #[fuchsia::test]
    fn test_record_time() {
        let before_time = zx::MonotonicInstant::get().into_nanos();
        let inspector = Inspector::default();
        NodeTimeExt::<zx::MonotonicTimeline>::record_time(inspector.root(), "time");
        let after_time = validate_inspector_get_time(&inspector, AnyProperty);
        assert_lt!(before_time, after_time);
    }

    #[fuchsia::test]
    fn test_create_time_no_executor() {
        let inspector = Inspector::default();
        let _: CreateTimeResult<zx::MonotonicTimeline> = inspector.root().create_time("time");
    }

    #[fuchsia::test]
    fn test_record_time_no_executor() {
        let inspector = Inspector::default();
        NodeTimeExt::<zx::MonotonicTimeline>::record_time(inspector.root(), "time");
    }

    fn validate_inspector_get_time<T>(inspector: &Inspector, expected: T) -> i64
    where
        T: PropertyAssertion<String> + 'static,
    {
        let hierarchy = inspector.get_diagnostics_hierarchy();
        assert_data_tree!(hierarchy, root: { time: expected });
        hierarchy.get_property("time").and_then(|t| t.int()).unwrap()
    }
}