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
#![allow(missing_copy_implementations, missing_debug_implementations)]
#[cfg(feature = "profiler")]
use measureme::{EventId, Profiler, TimingGuard};
#[cfg(feature = "profiler")]
use once_cell::sync::OnceCell;
use std::fmt::{self, Debug};
#[cfg(feature = "profiler")]
use std::{
path::Path,
thread::{current, ThreadId},
};
#[cfg(not(windows))]
#[cfg(feature = "profiler")]
type SerializationSink = measureme::MmapSerializationSink;
#[cfg(windows)]
#[cfg(feature = "profiler")]
type SerializationSink = measureme::FileSerializationSink;
#[cfg(feature = "profiler")]
pub struct BoaProfiler {
profiler: Profiler<SerializationSink>,
}
#[cfg(feature = "profiler")]
static mut INSTANCE: OnceCell<BoaProfiler> = OnceCell::new();
#[cfg(feature = "profiler")]
impl BoaProfiler {
pub fn start_event(&self, label: &str, category: &str) -> TimingGuard<'_, SerializationSink> {
let kind = self.profiler.alloc_string(category);
let id = EventId::from_label(self.profiler.alloc_string(label));
let thread_id = Self::thread_id_to_u32(current().id());
self.profiler
.start_recording_interval_event(kind, id, thread_id)
}
pub fn default() -> BoaProfiler {
let profiler = Profiler::new(Path::new("./my_trace")).unwrap();
BoaProfiler { profiler }
}
pub fn global() -> &'static BoaProfiler {
unsafe { INSTANCE.get_or_init(Self::default) }
}
pub fn drop(&self) {
unsafe {
INSTANCE
.take()
.expect("Could not take back profiler instance");
}
}
fn thread_id_to_u32(tid: ThreadId) -> u32 {
unsafe { std::mem::transmute::<ThreadId, u64>(tid) as u32 }
}
}
impl Debug for BoaProfiler {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt("no debug implemented", f)
}
}
#[cfg(not(feature = "profiler"))]
pub struct BoaProfiler;
#[allow(clippy::unused_unit)]
#[cfg(not(feature = "profiler"))]
impl BoaProfiler {
pub fn start_event(&self, _label: &str, _category: &str) -> () {
()
}
pub fn drop(&self) {
()
}
pub fn global() -> BoaProfiler {
BoaProfiler
}
}