|
| 1 | +#![allow(missing_copy_implementations, missing_debug_implementations)] |
| 2 | + |
| 3 | +use measureme::{EventId, Profiler, TimingGuard}; |
| 4 | +use once_cell::sync::OnceCell; |
| 5 | +use std::fmt::{self, Debug}; |
| 6 | +use std::{ |
| 7 | + path::Path, |
| 8 | + thread::{current, ThreadId}, |
| 9 | +}; |
| 10 | + |
| 11 | +/// MmapSerializatioSink is faster on macOS and Linux |
| 12 | +/// but FileSerializationSink is faster on Windows |
| 13 | +#[cfg(not(windows))] |
| 14 | +type SerializationSink = measureme::MmapSerializationSink; |
| 15 | +#[cfg(windows)] |
| 16 | +type SerializationSink = measureme::FileSerializationSink; |
| 17 | + |
| 18 | +pub struct BoaProfiler { |
| 19 | + profiler: Profiler<SerializationSink>, |
| 20 | +} |
| 21 | + |
| 22 | +pub static mut INSTANCE: OnceCell<BoaProfiler> = OnceCell::new(); |
| 23 | + |
| 24 | +impl BoaProfiler { |
| 25 | + pub fn start_event(&self, label: &str, category: &str) -> TimingGuard<'_, SerializationSink> { |
| 26 | + let kind = self.profiler.alloc_string(category); |
| 27 | + let id = EventId::from_label(self.profiler.alloc_string(label)); |
| 28 | + let thread_id = Self::thread_id_to_u32(current().id()); |
| 29 | + self.profiler |
| 30 | + .start_recording_interval_event(kind, id, thread_id) |
| 31 | + } |
| 32 | + |
| 33 | + pub fn default() -> BoaProfiler { |
| 34 | + let profiler = Profiler::new(Path::new("./my_trace")).unwrap(); |
| 35 | + BoaProfiler { profiler } |
| 36 | + } |
| 37 | + |
| 38 | + // init creates a global instance of BoaProfiler which can be used across the whole application |
| 39 | + pub fn init() { |
| 40 | + let profiler = Self::default(); |
| 41 | + unsafe { |
| 42 | + INSTANCE |
| 43 | + .set(profiler) |
| 44 | + .expect("Failed to set BoaProfiler globally"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub fn global() -> &'static BoaProfiler { |
| 49 | + unsafe { INSTANCE.get().expect("Profiler is not initialized") } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn drop(&self) { |
| 53 | + // In order to drop the INSTANCE we need to get ownership of it, which isn't possible on a static unless you make it a mutable static |
| 54 | + // mutating statics is unsafe, so we need to wrap it as so. |
| 55 | + // This is actually safe though because init and drop are only called at the beginning and end of the application |
| 56 | + unsafe { |
| 57 | + INSTANCE |
| 58 | + .take() |
| 59 | + .expect("Could not take back profiler instance"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Sadly we need to use the unsafe method until this is resolved: |
| 64 | + // https://github.com/rust-lang/rust/issues/67939 |
| 65 | + // Once `as_64()` is in stable we can do this: |
| 66 | + // https://github.com/rust-lang/rust/pull/68531/commits/ea42b1c5b85f649728e3a3b334489bac6dce890a |
| 67 | + // Until then our options are: use rust-nightly or use unsafe {} |
| 68 | + fn thread_id_to_u32(tid: ThreadId) -> u32 { |
| 69 | + unsafe { std::mem::transmute::<ThreadId, u64>(tid) as u32 } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl Debug for BoaProfiler { |
| 74 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 75 | + Debug::fmt("no debug implemented", f) |
| 76 | + } |
| 77 | +} |
0 commit comments