firestorm_core/
lib.rs

1//! The idea of firestorm-core  was to try to make a small API subset so that multiple
2//! versions of firestorm with the potential for backward incompatable API changes
3//! between them could share common data so that profiling all dependencies would
4//! still work. This probably fell short, but it's a step toward that.
5// TODO: The main problem about the above is that multiple dependencies would not be enabled.
6// So, even the enable/disable re-export paradigm has to come from this crate.
7
8use std::cell::UnsafeCell;
9
10// The TimeSample type exists to make time opaque for backward compatability.
11// The means by which time is sampled has a huge impact on the performance of this
12// crate. So, it is desirable to be able to change the method without requiring
13// a version bump.
14//
15// We also make the method configurable
16
17#[cfg(feature = "cpu_time")]
18mod cpu_time;
19#[cfg(feature = "cpu_time")]
20pub use cpu_time::*;
21
22#[cfg(feature = "system_time")]
23mod system_time;
24#[cfg(feature = "system_time")]
25pub use system_time::*;
26
27#[cfg(not(any(feature = "system_time", feature = "cpu_time")))]
28mod disabled;
29#[cfg(not(any(feature = "system_time", feature = "cpu_time")))]
30pub use disabled::*;
31
32type Str = &'static &'static str;
33
34// TODO: Add Pause, Resume to help with things like the
35// amortized cost of expanding the length of the events
36// array getting reported as a part of another operation.
37#[non_exhaustive]
38pub enum Start {
39    Method { typ: &'static str, signature: Str },
40    Func { signature: Str },
41    Section { name: Str },
42}
43
44/// A lazy string format.
45#[non_exhaustive]
46pub enum EventData {
47    Start(Start),
48    End,
49}
50
51/// A tiny record of a method call which when played back can construct
52/// a profiling state. Several representations were considered, the most
53/// elaborate of which would write variable length data to the event stream.
54/// Ideally, data would be an &'static EventData to avoid writing more data
55/// than is necessary but limitations in the Rust compiler prevent this.
56pub struct Event {
57    pub time: TimeSample,
58    pub data: EventData,
59}
60
61// Having a large capacity here buys some time before having to implement Pause/Resume capabilities to hide
62// the time spent in expanding the array. It may be a good idea to have the core API do a warmup and clear.
63thread_local!(pub static EVENTS: UnsafeCell<Vec<Event>> = UnsafeCell::new(Vec::with_capacity(8192)));