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
193
194
195
196
197
// Copyright 2022 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.

use async_trait::async_trait;
use errors::ModelError;
use fuchsia_sync::Mutex;
use hooks::{Event, EventPayload, EventType, HasEventType, Hook, HooksRegistration};
use moniker::Moniker;
use std::sync::{Arc, Weak};
use {
    fuchsia_inspect as inspect, fuchsia_inspect_contrib as inspect_contrib, fuchsia_zircon as zx,
};

const MAX_NUMBER_OF_LIFECYCLE_EVENTS: usize = 150;
const MONIKER: &str = "moniker";
const TYPE: &str = "type";
const STARTED: &str = "started";
const STOPPED: &str = "stopped";
const TIME: &str = "time";
const EARLY: &str = "early";
const LATE: &str = "late";

/// Tracks start and stop timestamps of components.
pub struct ComponentLifecycleTimeStats {
    // Keeps the inspect node alive.
    _node: inspect::Node,
    inner: Mutex<Inner>,
}

/// `early` maintains the first `MAX_NUMBER_OF_LIFECYCLE_EVENTS` start/stop events of
/// components. After more than `MAX_NUMBER_OF_LIFECYCLE_EVENTS` events have occurred,
/// `early` will stay unchanged, and `late` will maintain the the last
/// `MAX_NUMBER_OF_LIFECYCLE_EVENTS` start/stop events of components. When more events are
/// added, the earliest ones in `late` will be discarded. This enables our feedback
/// snapshots to contain a recent history of started and stopped components.
struct Inner {
    early: inspect_contrib::nodes::BoundedListNode,
    late: inspect_contrib::nodes::BoundedListNode,
}

impl Inner {
    fn new(early: inspect::Node, late: inspect::Node) -> Self {
        let early =
            inspect_contrib::nodes::BoundedListNode::new(early, MAX_NUMBER_OF_LIFECYCLE_EVENTS);
        let late =
            inspect_contrib::nodes::BoundedListNode::new(late, MAX_NUMBER_OF_LIFECYCLE_EVENTS);
        Self { early, late }
    }

    fn add_entry(&mut self, moniker: &Moniker, kind: &str, time: zx::MonotonicTime) {
        let node =
            if self.early.len() < self.early.capacity() { &mut self.early } else { &mut self.late };
        node.add_entry(|node| {
            node.record_string(MONIKER, moniker.to_string());
            node.record_string(TYPE, kind);
            node.record_int(TIME, time.into_nanos());
        });
    }
}

impl ComponentLifecycleTimeStats {
    /// Creates a new startup time tracker. Data will be written to the given inspect node.
    pub fn new(node: inspect::Node) -> Self {
        let early = node.create_child(EARLY);
        let late = node.create_child(LATE);
        Self { _node: node, inner: Mutex::new(Inner::new(early, late)) }
    }

    /// Provides the hook events that are needed to work.
    pub fn hooks(self: &Arc<Self>) -> Vec<HooksRegistration> {
        vec![HooksRegistration::new(
            "ComponentLifecycleTimeStats",
            vec![EventType::Started, EventType::Stopped],
            Arc::downgrade(self) as Weak<dyn Hook>,
        )]
    }

    fn on_component_started(self: &Arc<Self>, moniker: &Moniker, start_time: zx::MonotonicTime) {
        self.inner.lock().add_entry(moniker, STARTED, start_time);
    }

    fn on_component_stopped(self: &Arc<Self>, moniker: &Moniker, stop_time: zx::MonotonicTime) {
        self.inner.lock().add_entry(moniker, STOPPED, stop_time);
    }
}

#[async_trait]
impl Hook for ComponentLifecycleTimeStats {
    async fn on(self: Arc<Self>, event: &Event) -> Result<(), ModelError> {
        let target_moniker = event
            .target_moniker
            .unwrap_instance_moniker_or(ModelError::UnexpectedComponentManagerMoniker)?;
        match event.event_type() {
            EventType::Started => {
                if let EventPayload::Started { runtime, .. } = &event.payload {
                    self.on_component_started(target_moniker, runtime.start_time);
                }
            }
            EventType::Stopped => {
                if let EventPayload::Stopped { stop_time, .. } = &event.payload {
                    self.on_component_stopped(target_moniker, *stop_time);
                }
            }
            _ => {}
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use diagnostics_assertions::assert_data_tree;
    use fuchsia_inspect::DiagnosticsHierarchyGetter;
    use itertools::Itertools;
    use moniker::ChildName;

    #[fuchsia::test]
    async fn early_doesnt_track_more_than_limit() {
        let inspector = inspect::Inspector::default();
        let stats =
            Arc::new(ComponentLifecycleTimeStats::new(inspector.root().create_child("lifecycle")));

        for i in 0..2 * MAX_NUMBER_OF_LIFECYCLE_EVENTS {
            stats.on_component_started(
                &Moniker::new(vec![ChildName::parse(format!("{}", i)).unwrap()]),
                zx::MonotonicTime::from_nanos(i as i64),
            );
        }

        let hierarchy = inspector.get_diagnostics_hierarchy();
        let node = &hierarchy.children[0];
        let early = node.children.iter().find_or_first(|c| c.name == "early").unwrap();
        assert_eq!(early.children.len(), MAX_NUMBER_OF_LIFECYCLE_EVENTS);
        assert_eq!(
            early.children.iter().map(|c| c.name.parse::<i32>().unwrap()).sorted().last().unwrap(),
            149
        );
    }

    #[fuchsia::test]
    async fn early_overflow_to_late() {
        let inspector = inspect::Inspector::default();
        let stats =
            Arc::new(ComponentLifecycleTimeStats::new(inspector.root().create_child("lifecycle")));

        for i in 0..MAX_NUMBER_OF_LIFECYCLE_EVENTS + 1 {
            stats.on_component_started(
                &Moniker::new(vec![ChildName::parse(format!("{}", i)).unwrap()]),
                zx::MonotonicTime::from_nanos(i as i64),
            );
        }

        let hierarchy = inspector.get_diagnostics_hierarchy();
        let node = &hierarchy.children[0];
        let early = node.children.iter().find_or_first(|c| c.name == "early").unwrap();
        let late = node.children.iter().find_or_first(|c| c.name == "late").unwrap();
        assert_eq!(early.children.len(), MAX_NUMBER_OF_LIFECYCLE_EVENTS);
        assert_eq!(
            early.children.iter().map(|c| c.name.parse::<i32>().unwrap()).sorted().last().unwrap(),
            149
        );
        assert_eq!(late.children.len(), 1);
        assert_data_tree!(late, late: {
            "0": contains {
                moniker: "150",
                "type": "started",
            }
        });
    }

    #[fuchsia::test]
    async fn late_doesnt_track_more_than_limit() {
        let inspector = inspect::Inspector::default();
        let stats =
            Arc::new(ComponentLifecycleTimeStats::new(inspector.root().create_child("lifecycle")));

        for i in 0..4 * MAX_NUMBER_OF_LIFECYCLE_EVENTS {
            stats.on_component_started(
                &Moniker::new(vec![ChildName::parse(format!("{}", i)).unwrap()]),
                zx::MonotonicTime::from_nanos(i as i64),
            );
        }

        let hierarchy = inspector.get_diagnostics_hierarchy();
        let node = &hierarchy.children[0];
        let early = node.children.iter().find_or_first(|c| c.name == "early").unwrap();
        let late = node.children.iter().find_or_first(|c| c.name == "late").unwrap();
        assert_eq!(early.children.len(), MAX_NUMBER_OF_LIFECYCLE_EVENTS);
        assert_eq!(late.children.len(), MAX_NUMBER_OF_LIFECYCLE_EVENTS);
        assert_eq!(
            late.children.iter().map(|c| c.name.parse::<i32>().unwrap()).sorted().last().unwrap(),
            449
        );
    }
}