|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +use super::event::Event; |
| 17 | +use super::event_type::EventType; |
| 18 | +use super::storage::Storage; |
| 19 | +use crate::cxxffi::uptimeMillis; |
| 20 | +use once_cell::sync::Lazy; |
| 21 | +use std::fmt; |
| 22 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 23 | + |
| 24 | +// Lazily initialized static instance of DebugStore. |
| 25 | +static INSTANCE: Lazy<DebugStore> = Lazy::new(DebugStore::new); |
| 26 | + |
| 27 | +/// The `DebugStore` struct is responsible for managing debug events and data. |
| 28 | +pub struct DebugStore { |
| 29 | + /// Atomic counter for generating unique event IDs. |
| 30 | + id_generator: AtomicU64, |
| 31 | + /// Non-blocking storage for debug events. |
| 32 | + event_store: Storage<Event, { DebugStore::DEFAULT_EVENT_LIMIT }>, |
| 33 | +} |
| 34 | + |
| 35 | +impl DebugStore { |
| 36 | + /// The default limit for the number of events that can be stored. |
| 37 | + /// |
| 38 | + /// This limit is used to initialize the storage for debug events. |
| 39 | + const DEFAULT_EVENT_LIMIT: usize = 16; |
| 40 | + /// A designated identifier used for events that cannot be closed. |
| 41 | + /// |
| 42 | + /// This ID is used for point/instantaneous events, or events do not have |
| 43 | + /// a distinct end. |
| 44 | + const NON_CLOSABLE_ID: u64 = 0; |
| 45 | + /// The version number for the encoding of debug store data. |
| 46 | + /// |
| 47 | + /// This constant is used as a part of the debug store's data format, |
| 48 | + /// allowing for version tracking and compatibility checks. |
| 49 | + const ENCODE_VERSION: u32 = 1; |
| 50 | + |
| 51 | + /// Creates a new instance of `DebugStore` with specified event limit and maximum delay. |
| 52 | + fn new() -> Self { |
| 53 | + Self { id_generator: AtomicU64::new(1), event_store: Storage::new() } |
| 54 | + } |
| 55 | + |
| 56 | + /// Returns a shared instance of `DebugStore`. |
| 57 | + /// |
| 58 | + /// This method provides a singleton pattern access to `DebugStore`. |
| 59 | + pub fn get_instance() -> &'static DebugStore { |
| 60 | + &INSTANCE |
| 61 | + } |
| 62 | + |
| 63 | + /// Begins a new debug event with the given name and data. |
| 64 | + /// |
| 65 | + /// This method logs the start of a debug event, assigning it a unique ID and marking its start time. |
| 66 | + /// - `name`: The name of the debug event. |
| 67 | + /// - `data`: Associated data as key-value pairs. |
| 68 | + /// - Returns: A unique ID for the debug event. |
| 69 | + pub fn begin(&self, name: String, data: Vec<(String, String)>) -> u64 { |
| 70 | + let id = self.generate_id(); |
| 71 | + self.event_store.insert(Event::new( |
| 72 | + id, |
| 73 | + Some(name), |
| 74 | + uptimeMillis(), |
| 75 | + EventType::DurationStart, |
| 76 | + data, |
| 77 | + )); |
| 78 | + id |
| 79 | + } |
| 80 | + |
| 81 | + /// Records a debug event without a specific duration, with the given name and data. |
| 82 | + /// |
| 83 | + /// This method logs an instantaneous debug event, useful for events that don't have a duration but are significant. |
| 84 | + /// - `name`: The name of the debug event. |
| 85 | + /// - `data`: Associated data as key-value pairs. |
| 86 | + pub fn record(&self, name: String, data: Vec<(String, String)>) { |
| 87 | + self.event_store.insert(Event::new( |
| 88 | + Self::NON_CLOSABLE_ID, |
| 89 | + Some(name), |
| 90 | + uptimeMillis(), |
| 91 | + EventType::Point, |
| 92 | + data, |
| 93 | + )); |
| 94 | + } |
| 95 | + |
| 96 | + /// Ends a debug event that was previously started with the given ID. |
| 97 | + /// |
| 98 | + /// This method marks the end of a debug event, completing its lifecycle. |
| 99 | + /// - `id`: The unique ID of the debug event to end. |
| 100 | + /// - `data`: Additional data to log at the end of the event. |
| 101 | + pub fn end(&self, id: u64, data: Vec<(String, String)>) { |
| 102 | + if id != Self::NON_CLOSABLE_ID { |
| 103 | + self.event_store.insert(Event::new( |
| 104 | + id, |
| 105 | + None, |
| 106 | + uptimeMillis(), |
| 107 | + EventType::DurationEnd, |
| 108 | + data, |
| 109 | + )); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fn generate_id(&self) -> u64 { |
| 114 | + let mut id = self.id_generator.fetch_add(1, Ordering::Relaxed); |
| 115 | + while id == Self::NON_CLOSABLE_ID { |
| 116 | + id = self.id_generator.fetch_add(1, Ordering::Relaxed); |
| 117 | + } |
| 118 | + id |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl fmt::Display for DebugStore { |
| 123 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 124 | + let uptime_now = uptimeMillis(); |
| 125 | + write!(f, "{},{},{}::", Self::ENCODE_VERSION, self.event_store.len(), uptime_now)?; |
| 126 | + |
| 127 | + write!( |
| 128 | + f, |
| 129 | + "{}", |
| 130 | + self.event_store.fold(String::new(), |mut acc, event| { |
| 131 | + if !acc.is_empty() { |
| 132 | + acc.push_str("||"); |
| 133 | + } |
| 134 | + acc.push_str(&event.to_string()); |
| 135 | + acc |
| 136 | + }) |
| 137 | + ) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#[cfg(test)] |
| 142 | +mod tests { |
| 143 | + use super::*; |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn test_begin_event() { |
| 147 | + let debug_store = DebugStore::new(); |
| 148 | + let _event_id = debug_store.begin("test_event".to_string(), vec![]); |
| 149 | + let output = debug_store.to_string(); |
| 150 | + assert!( |
| 151 | + output.contains("test_event"), |
| 152 | + "The output should contain the event name 'test_event'" |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_unique_event_ids() { |
| 158 | + let debug_store = DebugStore::new(); |
| 159 | + let event_id1 = debug_store.begin("event1".to_string(), vec![]); |
| 160 | + let event_id2 = debug_store.begin("event2".to_string(), vec![]); |
| 161 | + assert_ne!(event_id1, event_id2, "Event IDs should be unique"); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn test_end_event() { |
| 166 | + let debug_store = DebugStore::new(); |
| 167 | + let event_id = debug_store.begin("test_event".to_string(), vec![]); |
| 168 | + debug_store.end(event_id, vec![]); |
| 169 | + let output = debug_store.to_string(); |
| 170 | + |
| 171 | + let id_pattern = format!("ID:{},", event_id); |
| 172 | + assert!( |
| 173 | + output.contains("test_event"), |
| 174 | + "The output should contain the event name 'test_event'" |
| 175 | + ); |
| 176 | + assert_eq!( |
| 177 | + output.matches(&id_pattern).count(), |
| 178 | + 2, |
| 179 | + "The output should contain two events (start and end) associated with the given ID" |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn test_event_data_handling() { |
| 185 | + let debug_store = DebugStore::new(); |
| 186 | + debug_store |
| 187 | + .record("data_event".to_string(), vec![("key".to_string(), "value".to_string())]); |
| 188 | + let output = debug_store.to_string(); |
| 189 | + assert!( |
| 190 | + output.contains("data_event"), |
| 191 | + "The output should contain the event name 'data_event'" |
| 192 | + ); |
| 193 | + assert!( |
| 194 | + output.contains("key=value"), |
| 195 | + "The output should contain the event data 'key=value'" |
| 196 | + ); |
| 197 | + } |
| 198 | +} |
0 commit comments