From 173fba35fad2f4f2c5bfc91656536ae4de708f3b Mon Sep 17 00:00:00 2001 From: Nova Date: Sun, 15 Jan 2023 01:13:22 -0500 Subject: [PATCH] feat: even more tracing --- src/core/scenegraph.rs | 20 +++++++++---- src/core/task.rs | 2 ++ src/nodes/input/mod.rs | 65 ++++++++++++++++++++++++---------------- src/nodes/mod.rs | 19 ++++++++---- src/nodes/spatial/mod.rs | 12 ++++---- 5 files changed, 75 insertions(+), 43 deletions(-) diff --git a/src/core/scenegraph.rs b/src/core/scenegraph.rs index f5eec4d..143548a 100644 --- a/src/core/scenegraph.rs +++ b/src/core/scenegraph.rs @@ -5,6 +5,7 @@ use once_cell::sync::OnceCell; use stardust_xr::scenegraph; use stardust_xr::scenegraph::ScenegraphError; use std::sync::{Arc, Weak}; +use tracing::{debug, debug_span, instrument}; use core::hash::BuildHasherDefault; use dashmap::DashMap; @@ -27,10 +28,12 @@ impl Scenegraph { node_arc } pub fn add_node_raw(&self, node: Arc) { + debug!(node = ?&*node, "Add node"); let path = node.get_path().to_string(); self.nodes.insert(path, node); } + #[instrument(level = "debug", skip(self))] pub fn get_node(&self, path: &str) -> Option> { let mut node = self.nodes.get(path)?.clone(); if let Some(alias) = node.alias.get() { @@ -40,6 +43,7 @@ impl Scenegraph { } pub fn remove_node(&self, path: &str) -> Option> { + debug!(path, "Remove node"); let (_, node) = self.nodes.remove(path)?; Some(node) } @@ -47,9 +51,11 @@ impl Scenegraph { impl scenegraph::Scenegraph for Scenegraph { fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> { - self.get_node(path) - .ok_or(ScenegraphError::NodeNotFound)? - .send_local_signal(self.get_client(), method, data) + debug_span!("Handle signal", path, method).in_scope(|| { + self.get_node(path) + .ok_or(ScenegraphError::NodeNotFound)? + .send_local_signal(self.get_client(), method, data) + }) } fn execute_method( &self, @@ -57,8 +63,10 @@ impl scenegraph::Scenegraph for Scenegraph { method: &str, data: &[u8], ) -> Result, ScenegraphError> { - self.get_node(path) - .ok_or(ScenegraphError::NodeNotFound)? - .execute_local_method(self.get_client(), method, data) + debug_span!("Handle method", path, method).in_scope(|| { + self.get_node(path) + .ok_or(ScenegraphError::NodeNotFound)? + .execute_local_method(self.get_client(), method, data) + }) } } diff --git a/src/core/task.rs b/src/core/task.rs index 43bffd8..6392efc 100644 --- a/src/core/task.rs +++ b/src/core/task.rs @@ -1,8 +1,10 @@ use color_eyre::eyre::Result; use std::future::Future; use tokio::task::JoinHandle; +use tracing::instrument; #[allow(unused_variables)] +#[instrument(level = "debug", skip_all)] pub fn new< F: FnOnce() -> S, S: AsRef, diff --git a/src/nodes/input/mod.rs b/src/nodes/input/mod.rs index 49fa856..3faa0c4 100644 --- a/src/nodes/input/mod.rs +++ b/src/nodes/input/mod.rs @@ -25,6 +25,7 @@ use stardust_xr::values::Transform; use std::ops::Deref; use std::sync::atomic::Ordering; use std::sync::{Arc, Weak}; +use tracing::{debug_span, instrument}; static INPUT_METHOD_REGISTRY: Registry = Registry::new(); static INPUT_HANDLER_REGISTRY: Registry = Registry::new(); @@ -145,6 +146,7 @@ impl DistanceLink { fn send_input(&self, frame: u64, datamap: Datamap) { self.handler.send_input(frame, self, datamap); } + #[instrument(level = "debug", skip(self))] fn serialize(&self, datamap: Datamap) -> Vec { let input = self.method.specialization.lock().serialize( self, @@ -185,6 +187,7 @@ impl InputHandler { Ok(()) } + #[instrument(level = "debug", skip(self, distance_link))] fn send_input(&self, frame: u64, distance_link: &DistanceLink, datamap: Datamap) { let data = distance_link.serialize(datamap); let node = self.node.upgrade().unwrap(); @@ -250,39 +253,49 @@ pub fn create_input_handler_flex( #[tracing::instrument(level = "debug")] pub fn process_input() { // Iterate over all valid input methods - for method in INPUT_METHOD_REGISTRY - .get_valid_contents() - .into_iter() - .filter(|method| *method.enabled.lock()) - .filter(|method| method.datamap.lock().is_some()) - { - // Get all valid input handlers and convert them to DistanceLink objects - let mut distance_links: Vec = INPUT_HANDLER_REGISTRY + let methods = debug_span!("Get valid methods").in_scope(|| { + INPUT_METHOD_REGISTRY .get_valid_contents() .into_iter() - .filter(|handler| handler.field.upgrade().is_some()) - .filter_map(|handler| DistanceLink::from(method.clone(), handler)) - .collect(); + .filter(|method| *method.enabled.lock()) + .filter(|method| method.datamap.lock().is_some()) + }); + for method in methods { + debug_span!("Process input method").in_scope(|| { + // Get all valid input handlers and convert them to DistanceLink objects + let mut distance_links: Vec = debug_span!("Generate distance links") + .in_scope(|| { + INPUT_HANDLER_REGISTRY + .get_valid_contents() + .into_iter() + .filter(|handler| handler.field.upgrade().is_some()) + .filter_map(|handler| DistanceLink::from(method.clone(), handler)) + .collect() + }); - // Sort the distance links by their distance in ascending order - distance_links - .sort_unstable_by(|a, b| a.distance.abs().partial_cmp(&b.distance.abs()).unwrap()); + // Sort the distance links by their distance in ascending order + debug_span!("Sort distance links").in_scope(|| { + distance_links.sort_unstable_by(|a, b| { + a.distance.abs().partial_cmp(&b.distance.abs()).unwrap() + }); + }); - // Get the current frame - let frame = FRAME.load(Ordering::Relaxed); + // Get the current frame + let frame = FRAME.load(Ordering::Relaxed); - // Iterate over the distance links and send input to them - for distance_link in distance_links { - distance_link.send_input(frame, method.datamap.lock().clone().unwrap()); + // Iterate over the distance links and send input to them + for distance_link in distance_links { + distance_link.send_input(frame, method.datamap.lock().clone().unwrap()); - // If the current distance link is in the list of captured input handlers, - // break out of the loop to avoid sending input to the remaining distance links - if method.captures.contains(&distance_link.handler) { - break; + // If the current distance link is in the list of captured input handlers, + // break out of the loop to avoid sending input to the remaining distance links + if method.captures.contains(&distance_link.handler) { + break; + } } - } - // Clear the list of captured input handlers for this method - method.captures.clear(); + // Clear the list of captured input handlers for this method + method.captures.clear(); + }); } } diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index b4e51e5..1c26ba4 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -15,11 +15,12 @@ use once_cell::sync::OnceCell; use parking_lot::Mutex; use stardust_xr::messenger::MessageSenderHandle; use stardust_xr::scenegraph::ScenegraphError; +use std::fmt::Debug; use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Weak}; use std::vec::Vec; -use tracing::debug_span; +use tracing::{debug_span, instrument}; use core::hash::BuildHasherDefault; use dashmap::DashMap; @@ -189,10 +190,8 @@ impl Node { .local_signals .get(method) .ok_or(ScenegraphError::SignalNotFound)?; - debug_span!("Handle signal").in_scope(|| { - signal(self, calling_client, data).map_err(|error| ScenegraphError::SignalError { - error: error.to_string(), - }) + signal(self, calling_client, data).map_err(|error| ScenegraphError::SignalError { + error: error.to_string(), }) } } @@ -224,6 +223,7 @@ impl Node { }) } } + #[instrument(level = "debug", skip_all)] pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> { self.aliases .get_valid_contents() @@ -242,6 +242,7 @@ impl Node { .transpose()?; Ok(()) } + #[instrument(level = "debug", skip_all)] pub fn execute_remote_method( &self, method: &str, @@ -257,3 +258,11 @@ impl Node { Ok(async { future.await.map_err(|e| eyre!(e)) }) } } +impl Debug for Node { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Node") + .field("uid", &self.uid) + .field("path", &self.path) + .finish() + } +} diff --git a/src/nodes/spatial/mod.rs b/src/nodes/spatial/mod.rs index 4a073e8..28b1690 100644 --- a/src/nodes/spatial/mod.rs +++ b/src/nodes/spatial/mod.rs @@ -73,14 +73,14 @@ impl Spatial { Ok(spatial_arc) } - #[instrument] + #[instrument(level = "debug", skip_all)] pub fn space_to_space_matrix(from: Option<&Spatial>, to: Option<&Spatial>) -> Mat4 { let space_to_world_matrix = from.map_or(Mat4::IDENTITY, |from| from.global_transform()); let world_to_space_matrix = to.map_or(Mat4::IDENTITY, |to| to.global_transform().inverse()); world_to_space_matrix * space_to_world_matrix } - #[instrument] + #[instrument(level = "debug", skip_all)] pub fn local_transform(&self) -> Mat4 { *self.transform.lock() } @@ -94,7 +94,7 @@ impl Spatial { pub fn set_local_transform(&self, transform: Mat4) { *self.transform.lock() = transform; } - #[instrument] + #[instrument(level = "debug", skip(self, reference_space))] pub fn set_local_transform_components( &self, reference_space: Option<&Spatial>, @@ -132,7 +132,7 @@ impl Spatial { ); } - #[instrument] + #[instrument(level = "debug", skip_all)] pub fn is_ancestor_of(&self, spatial: Arc) -> bool { let mut current_ancestor = spatial; loop { @@ -149,7 +149,7 @@ impl Spatial { } } - #[instrument] + #[instrument(level = "debug", skip_all)] pub fn set_spatial_parent(&self, parent: Option<&Arc>) -> Result<()> { let is_ancestor = parent .map(|parent| self.is_ancestor_of(parent.clone())) @@ -163,7 +163,7 @@ impl Spatial { Ok(()) } - #[instrument] + #[instrument(level = "debug", skip_all)] pub fn set_spatial_parent_in_place(&self, parent: Option<&Arc>) -> Result<()> { let is_ancestor = parent .map(|parent| self.is_ancestor_of(parent.clone()))