Skip to content

Commit 2d0d712

Browse files
Mohamad MahmoudGerrit Code Review
Mohamad Mahmoud
authored and
Gerrit Code Review
committed
Merge "Add libdebugstore for Future Use by ART and Frameworks" into main
2 parents 65a5341 + 55626ed commit 2d0d712

File tree

8 files changed

+603
-0
lines changed

8 files changed

+603
-0
lines changed

libs/debugstore/OWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+

libs/debugstore/rust/Android.bp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (C) 2024 The Android Open Source Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package {
16+
default_team: "trendy_team_android_telemetry_infra",
17+
default_applicable_licenses: ["frameworks_native_license"],
18+
}
19+
20+
rust_defaults {
21+
name: "libdebugstore_defaults",
22+
srcs: ["src/lib.rs"],
23+
rustlibs: [
24+
"libcrossbeam_queue",
25+
"libparking_lot",
26+
"libonce_cell",
27+
"libcxx",
28+
],
29+
shared_libs: ["libutils"],
30+
edition: "2021",
31+
}
32+
33+
rust_ffi_static {
34+
name: "libdebugstore_rust_ffi",
35+
crate_name: "debugstore",
36+
defaults: ["libdebugstore_defaults"],
37+
}
38+
39+
cc_library {
40+
name: "libdebugstore_cxx",
41+
generated_headers: ["libdebugstore_cxx_bridge_header"],
42+
generated_sources: ["libdebugstore_cxx_bridge_code"],
43+
export_generated_headers: ["libdebugstore_cxx_bridge_header"],
44+
shared_libs: ["libutils"],
45+
whole_static_libs: ["libdebugstore_rust_ffi"],
46+
}
47+
48+
rust_test {
49+
name: "libdebugstore_tests",
50+
defaults: ["libdebugstore_defaults"],
51+
test_options: {
52+
unit_test: true,
53+
},
54+
shared_libs: ["libdebugstore_cxx"],
55+
}
56+
57+
genrule {
58+
name: "libdebugstore_cxx_bridge_header",
59+
tools: ["cxxbridge"],
60+
cmd: "$(location cxxbridge) $(in) --header >> $(out)",
61+
srcs: ["src/lib.rs"],
62+
out: ["debugstore/debugstore_cxx_bridge.rs.h"],
63+
}
64+
65+
genrule {
66+
name: "libdebugstore_cxx_bridge_code",
67+
tools: ["cxxbridge"],
68+
cmd: "$(location cxxbridge) $(in) >> $(out)",
69+
srcs: ["src/lib.rs"],
70+
out: ["debugstore/debugstore_cxx_bridge.rs.cpp"],
71+
}

libs/debugstore/rust/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "debugstore"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib", "rlib"]
8+
9+
[dependencies]

libs/debugstore/rust/src/core.rs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}

libs/debugstore/rust/src/event.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
17+
use super::event_type::EventType;
18+
use std::fmt;
19+
20+
/// Represents a single debug event within the Debug Store system.
21+
///
22+
/// It contains all the necessary information for a debug event.
23+
#[derive(Clone)]
24+
pub struct Event {
25+
/// The unique identifier for this event.
26+
pub id: u64,
27+
/// The optional name of the event.
28+
pub name: Option<String>,
29+
/// The system uptime when the event occurred.
30+
pub timestamp: i64,
31+
/// The type of the event.
32+
pub event_type: EventType,
33+
/// Additional data associated with the event, stored in the given order as key-value pairs.
34+
data: Vec<(String, String)>,
35+
}
36+
37+
impl Event {
38+
/// Constructs a new `Event`.
39+
///
40+
/// - `id`: The unique identifier for the event.
41+
/// - `name`: An optional name for the event.
42+
/// - `timestamp`: The system uptime when the event occurred.
43+
/// - `event_type`: The type of the event.
44+
/// - `data`: Additional data for the event, represented as ordered key-value pairs.
45+
pub fn new(
46+
id: u64,
47+
name: Option<String>,
48+
timestamp: i64,
49+
event_type: EventType,
50+
data: Vec<(String, String)>,
51+
) -> Self {
52+
Self { id, name, timestamp, event_type, data }
53+
}
54+
}
55+
56+
impl fmt::Display for Event {
57+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58+
write!(f, "ID:{},C:{},T:{}", self.id, self.event_type, self.timestamp)?;
59+
60+
if let Some(ref name) = self.name {
61+
write!(f, ",N:{}", name)?;
62+
}
63+
64+
if !self.data.is_empty() {
65+
let data_str =
66+
self.data.iter().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<_>>().join(";");
67+
write!(f, ",D:{}", data_str)?;
68+
}
69+
70+
Ok(())
71+
}
72+
}

0 commit comments

Comments
 (0)