fix recursion issue, better macros

This commit is contained in:
2025-12-11 08:03:49 +01:00
parent 06b8cac896
commit 11d9ebe2b6
4 changed files with 220 additions and 77 deletions

View File

@@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicUsize, Ordering};
use rand::Rng;
use teleprof::instrument;
use teleprof::{instrument, instrument_calls};
const WIDTH: usize = 800;
const HEIGHT: usize = 600;
@@ -39,12 +39,17 @@ fn main() {
// Name the main thread
teleprof::set_thread_name("Main");
println!("Bouncing Ball Demo");
println!("Bouncing Ball Demo - Enhanced Instrumentation");
println!("The ball window should appear alongside the profiler");
println!("Press Space in profiler window to pause");
println!("Mouse wheel to zoom, drag to pan in profiler");
println!("Press Escape in either window to quit");
println!();
println!("Features demonstrated:");
println!("- Flame graph rendering (all depth-N spans on row N)");
println!("- instrument_calls macro for automatic call instrumentation");
println!("- Runtime deduplication preventing double-wrapping");
println!();
let mut window = Window::new(
"Bouncing Ball",
@@ -74,7 +79,10 @@ fn main() {
}
}
#[instrument]
// Using instrument_calls - automatically instruments all function calls in the body
// This will show update_physics, render, print_status, thread::sleep, Arc::clone, etc.
// Pause handling is now automatic in SpanGuard!
#[instrument_calls]
fn main_frame(
ball: &Arc<Mutex<Ball>>,
framebuffer: &mut [u32],
@@ -82,16 +90,8 @@ fn main_frame(
frame_count: &mut u32,
frame_time: Duration,
) {
// Check if paused
if teleprof::PAUSE.try_lock().is_err() {
println!("Paused!");
while teleprof::PAUSE.try_lock().is_err() {
thread::sleep(Duration::from_millis(100));
}
println!("Resumed!");
}
// Update physics
// Update physics - this is also instrumented with #[instrument]
// Runtime dedup will prevent double-wrapping!
let hit_wall = update_physics(ball, frame_time.as_secs_f32());
// If we hit a wall, spawn a thread to pick a new color
@@ -104,7 +104,7 @@ fn main_frame(
});
}
// Render
// Render - also instrumented, so dedup will handle it
render(ball, framebuffer);
// Update window
@@ -162,7 +162,8 @@ fn pick_new_color(ball: Arc<Mutex<Ball>>) {
println!(" → New color selected: RGB({}, {}, {})", r, g, b);
}
#[instrument]
// Using instrument_calls here too to see the breakdown of rendering
#[instrument_calls]
fn render(ball: &Arc<Mutex<Ball>>, framebuffer: &mut [u32]) {
clear_background(framebuffer);
draw_ball(ball, framebuffer);