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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! Usage:
//!
//! ``` no_run
//! fn main() {
//!     puffin::set_scopes_on(true); // you may want to control this with a flag
//!
//!     // game loop
//!     loop {
//!         puffin::GlobalProfiler::lock().new_frame();
//!
//!         {
//!             puffin::profile_scope!("slow_code");
//!             slow_code();
//!         }
//!
//!     }
//! }
//!
//! # fn slow_code(){}
//! ```

#![forbid(unsafe_code)]
#![deny(missing_docs)]

mod data;
mod frame_data;
mod global_profiler;
mod merge;
mod profile_view;
mod scope_details;
mod thread_profiler;
mod utils;

use std::num::NonZeroU32;
use std::sync::atomic::{AtomicBool, Ordering};

/// TODO: Improve encapsulation.
pub use data::{Error, Reader, Result, Scope, ScopeRecord, Stream, StreamInfo, StreamInfoRef};
pub use frame_data::{FrameData, FrameMeta, UnpackedFrameData};
pub use global_profiler::{FrameSink, GlobalProfiler};
pub use merge::{merge_scopes_for_thread, MergeScope};
pub use profile_view::{select_slowest, FrameView, GlobalFrameView};
pub use scope_details::{ScopeCollection, ScopeDetails, ScopeType};
pub use thread_profiler::{ThreadInfo, ThreadProfiler};
pub use utils::{clean_function_name, short_file_name, shorten_rust_function_name, type_name_of};

static MACROS_ON: AtomicBool = AtomicBool::new(false);

/// Turn on/off the profiler macros ([`profile_function`], [`profile_scope`] etc).
/// When off, these calls take only 1-2 ns to call (100x faster).
/// This is [`false`] by default.
pub fn set_scopes_on(on: bool) {
    MACROS_ON.store(on, Ordering::Relaxed);
}

/// Are the profiler scope macros turned on?
/// This is [`false`] by default.
pub fn are_scopes_on() -> bool {
    MACROS_ON.load(Ordering::Relaxed)
}

/// All times are expressed as integer nanoseconds since some event.
pub type NanoSecond = i64;

/// An incremental monolithic counter to identify frames.
pub type FrameIndex = u64;

type NsSource = fn() -> NanoSecond;

/// Incremental monolithic counter to identify scopes.
static SCOPE_ID_TRACKER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(1);

fn fetch_add_scope_id() -> ScopeId {
    let new_id = SCOPE_ID_TRACKER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    ScopeId(
        NonZeroU32::new(new_id)
            .expect("safe because integer is retrieved from fetch-add atomic operation"),
    )
}

/// Identifies a specific [`FrameSink`] when added to [`GlobalProfiler`].
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct FrameSinkId(u64);

/// Returns a high-precision, monotonically increasing nanosecond count since unix epoch.
#[inline]
#[cfg(any(not(target_arch = "wasm32"), feature = "web"))]
pub fn now_ns() -> NanoSecond {
    #[cfg(target_arch = "wasm32")]
    fn nanos_since_epoch() -> NanoSecond {
        (js_sys::Date::new_0().get_time() * 1e6) as _
    }

    #[cfg(not(target_arch = "wasm32"))]
    fn nanos_since_epoch() -> NanoSecond {
        if let Ok(duration_since_epoch) = std::time::UNIX_EPOCH.elapsed() {
            duration_since_epoch.as_nanos() as NanoSecond
        } else {
            0 // system time is set before 1970. this should be quite rare.
        }
    }

    // This can maybe be optimized

    #[cfg(not(target_arch = "wasm32"))]
    use std::time::Instant;
    #[cfg(target_arch = "wasm32")]
    use web_time::Instant;

    static START_TIME: once_cell::sync::Lazy<(NanoSecond, Instant)> =
        once_cell::sync::Lazy::new(|| (nanos_since_epoch(), Instant::now()));
    START_TIME.0 + START_TIME.1.elapsed().as_nanos() as NanoSecond
}

/// Should not be used.
#[inline]
#[cfg(all(target_arch = "wasm32", not(feature = "web")))]
pub fn now_ns() -> NanoSecond {
    // This should be unused.
    panic!("Wasm without the `web` feature requires passing a custom source of time via `ThreadProfiler::initialize`");
}

// We currently store an Option<ProfilerScope> on the stack (None when profiling is off).
// This currently takes up 16 bytes of stack space. TODO: get this down to 4 bytes.
/// Created by the `puffin::profile*!(...)` macros.
pub struct ProfilerScope {
    start_stream_offset: usize,

    /// Prevent the scope from being sent between threads.
    /// The scope must start/stop on the same thread.
    /// In particular, we do NOT want this to migrate threads in some async code.
    /// Workaround until `impl !Send for ProfilerScope {}` is stable.
    _dont_send_me: std::marker::PhantomData<*const ()>,
}

impl ProfilerScope {
    /// The scope id identifies which scopes' time is being reported.
    /// `data` can be changing, i.e. a name of a mesh or a texture.
    #[inline]
    pub fn new(scope_id: ScopeId, data: impl AsRef<str>) -> Self {
        Self {
            start_stream_offset: ThreadProfiler::call(|tp| tp.begin_scope(scope_id, data.as_ref())),
            _dont_send_me: Default::default(),
        }
    }
}

impl Drop for ProfilerScope {
    #[inline]
    fn drop(&mut self) {
        ThreadProfiler::call(|tp| tp.end_scope(self.start_stream_offset));
    }
}

/// A unique id for each scope and [`ScopeDetails`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct ScopeId(pub NonZeroU32);

impl ScopeId {
    #[cfg(test)]
    pub(crate) fn new(id: u32) -> Self {
        ScopeId(NonZeroU32::new(id).expect("Scope id was not non-zero u32"))
    }
}

/// Returns the name of the calling function without a long module path prefix.
#[macro_export]
macro_rules! current_function_name {
    () => {{
        fn f() {}
        $crate::type_name_of(f)
    }};
}

#[allow(clippy::doc_markdown)] // clippy wants to put "MacBook" in ticks 🙄
/// Automatically name the profiling scope based on function name.
///
/// Names should be descriptive, ASCII and without spaces.
///
/// Example:
/// ```
/// # struct Image {};
/// fn load_image(path: &str) -> Image {
///     puffin::profile_function!();
///     /* … */
///     # let image = Image {};
///     image
/// }
/// ```
///
/// An optional argument can be a string describing e.g. an argument, to help diagnose what was slow.
///
/// ```
/// # struct Image {};
/// fn load_image(path: &str) -> Image {
///     puffin::profile_function!(path);
///     /* … */
///     # let image = Image {};
///     image
/// }
/// ```
///
/// Overhead: around 54 ns on Macbook Pro with Apple M1 Max.
#[macro_export]
macro_rules! profile_function {
    () => {
        $crate::profile_function!("");
    };
    ($data:expr) => {
        let _profiler_scope = if $crate::are_scopes_on() {
            static SCOPE_ID: std::sync::OnceLock<$crate::ScopeId> = std::sync::OnceLock::new();
            let scope_id = SCOPE_ID.get_or_init(|| {
                $crate::ThreadProfiler::call(|tp| {
                    let id = tp.register_function_scope(
                        $crate::clean_function_name($crate::current_function_name!()),
                        $crate::short_file_name(file!()),
                        line!(),
                    );
                    id
                })
            });

            Some($crate::ProfilerScope::new(*scope_id, $data))
        } else {
            None
        };
    };
}

#[allow(clippy::doc_markdown)] // clippy wants to put "MacBook" in ticks 🙄
/// Profile the current scope with the given name (unique in the parent scope).
///
/// Names should be descriptive, ASCII and without spaces.
///
/// Example: `profile_scope!("load_mesh");`.
///
/// An optional second argument can be a string (e.g. a mesh name) to help diagnose what was slow.
/// Example: `profile_scope!("load_mesh", mesh_name);`
///
/// Overhead: around 54 ns on Macbook Pro with Apple M1 Max.
#[macro_export]
macro_rules! profile_scope {
    ($name:expr) => {
        $crate::profile_scope!($name, "");
    };
    ($name:expr, $data:expr) => {
        let _profiler_scope = if $crate::are_scopes_on() {
            static SCOPE_ID: std::sync::OnceLock<$crate::ScopeId> = std::sync::OnceLock::new();
            let scope_id = SCOPE_ID.get_or_init(|| {
                $crate::ThreadProfiler::call(|tp| {
                    let id = tp.register_named_scope(
                        $name,
                        $crate::clean_function_name($crate::current_function_name!()),
                        $crate::short_file_name(file!()),
                        line!(),
                    );
                    id
                })
            });
            Some($crate::ProfilerScope::new(*scope_id, $data))
        } else {
            None
        };
    };
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use crate::{
        clean_function_name, set_scopes_on, short_file_name, utils::USELESS_SCOPE_NAME_SUFFIX,
        GlobalFrameView, GlobalProfiler, ScopeId,
    };

    #[test]
    fn test_short_file_name() {
        for (before, after) in [
            ("", ""),
            ("foo.rs", "foo.rs"),
            ("foo/bar.rs", "foo/bar.rs"),
            ("foo/bar/baz.rs", "bar/baz.rs"),
            ("crates/cratename/src/main.rs", "cratename/src/main.rs"),
            ("crates/cratename/src/module/lib.rs", "cratename/…/module/lib.rs"),
            ("workspace/cratename/examples/hello_world.rs", "examples/hello_world.rs"),
            ("/rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/ops/function.rs", "core/…/function.rs"),
            ("/Users/emilk/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/runtime.rs", "tokio-1.24.1/…/runtime.rs"),
            ]
            {
            assert_eq!(short_file_name(before), after);
        }
    }

    #[test]
    fn test_clean_function_name() {
        assert_eq!(clean_function_name(""), "");
        assert_eq!(
            clean_function_name(&format!("foo{}", USELESS_SCOPE_NAME_SUFFIX)),
            "foo"
        );
        assert_eq!(
            clean_function_name(&format!("foo::bar{}", USELESS_SCOPE_NAME_SUFFIX)),
            "foo::bar"
        );
        assert_eq!(
            clean_function_name(&format!("foo::bar::baz{}", USELESS_SCOPE_NAME_SUFFIX)),
            "bar::baz"
        );
        assert_eq!(
            clean_function_name(&format!(
                "some::GenericThing<_, _>::function_name{}",
                USELESS_SCOPE_NAME_SUFFIX
            )),
            "GenericThing<_, _>::function_name"
        );
        assert_eq!(
            clean_function_name(&format!(
                "<some::ConcreteType as some::bloody::Trait>::function_name{}",
                USELESS_SCOPE_NAME_SUFFIX
            )),
            "<ConcreteType as Trait>::function_name"
        );
    }

    #[test]
    fn profile_macros_test() {
        set_scopes_on(true);

        let frame_view = GlobalFrameView::default();

        GlobalProfiler::lock().add_sink(Box::new(|data| {
            if data.frame_index() == 0 {
                assert_eq!(data.frame_index(), 0);
                assert_eq!(data.meta().num_scopes, 2);
                assert_eq!(data.meta().num_bytes, 62);
            } else if data.frame_index() == 1 {
                assert_eq!(data.frame_index(), 1);
                assert_eq!(data.meta().num_scopes, 2);
                assert_eq!(data.meta().num_bytes, 62);
            } else {
                panic!("Only two frames in this test");
            }
        }));

        let line_nr_fn = line!() + 3;
        let line_nr_scope = line!() + 4;
        fn a() {
            profile_function!();
            {
                profile_scope!("my-scope");
            }
        }

        a();

        // First frame
        GlobalProfiler::lock().new_frame();

        let lock = frame_view.lock();
        let scope_details = lock
            .scope_collection()
            .fetch_by_id(&ScopeId::new(1))
            .unwrap();
        assert_eq!(scope_details.file_path, "puffin/src/lib.rs");
        assert_eq!(scope_details.function_name, "profile_macros_test::a");
        assert_eq!(scope_details.line_nr, line_nr_fn);

        let scope_details = lock
            .scope_collection()
            .fetch_by_id(&ScopeId::new(2))
            .unwrap();

        assert_eq!(scope_details.file_path, "puffin/src/lib.rs");
        assert_eq!(scope_details.function_name, "profile_macros_test::a");
        assert_eq!(scope_details.scope_name, Some(Cow::Borrowed("my-scope")));
        assert_eq!(scope_details.line_nr, line_nr_scope);

        let scope_details = lock.scope_collection().fetch_by_name("my-scope").unwrap();
        assert_eq!(scope_details, &ScopeId::new(2));

        drop(lock);

        // Second frame
        a();

        GlobalProfiler::lock().new_frame();
    }
}