|
1 |
| -#[cfg(target_os = "linux")] |
2 |
| -mod ffi; |
3 |
| - |
4 |
| -#[cfg(target_os = "linux")] |
5 |
| -mod linux_impl { |
6 |
| - use nix::sys::time::TimeValLike; |
7 |
| - |
8 |
| - use super::ffi; |
9 |
| - use std::ffi::CString; |
10 |
| - use std::sync::OnceLock; |
11 |
| - |
12 |
| - pub struct InstrumentHooks(*mut ffi::InstrumentHooks); |
13 |
| - |
14 |
| - unsafe impl Send for InstrumentHooks {} |
15 |
| - unsafe impl Sync for InstrumentHooks {} |
16 |
| - |
17 |
| - impl InstrumentHooks { |
18 |
| - #[inline(always)] |
19 |
| - pub fn new() -> Option<Self> { |
20 |
| - let ptr = unsafe { ffi::instrument_hooks_init() }; |
21 |
| - if ptr.is_null() { |
22 |
| - None |
23 |
| - } else { |
24 |
| - Some(InstrumentHooks(ptr)) |
25 |
| - } |
26 |
| - } |
27 |
| - |
28 |
| - /// Returns a singleton instance of `InstrumentHooks`. |
29 |
| - #[inline(always)] |
30 |
| - pub fn instance() -> &'static Self { |
31 |
| - static INSTANCE: OnceLock<InstrumentHooks> = OnceLock::new(); |
32 |
| - INSTANCE.get_or_init(|| { |
33 |
| - let instance = |
34 |
| - InstrumentHooks::new().expect("Failed to initialize InstrumentHooks"); |
35 |
| - instance |
36 |
| - .set_integration("codspeed-rust", env!("CARGO_PKG_VERSION")) |
37 |
| - .expect("Failed to set integration"); |
38 |
| - instance |
39 |
| - }) |
40 |
| - } |
| 1 | +use nix::sys::time::TimeValLike; |
| 2 | +use std::ffi::CString; |
| 3 | +use std::sync::OnceLock; |
41 | 4 |
|
42 |
| - #[inline(always)] |
43 |
| - pub fn is_instrumented(&self) -> bool { |
44 |
| - unsafe { ffi::instrument_hooks_is_instrumented(self.0) } |
45 |
| - } |
46 |
| - |
47 |
| - #[inline(always)] |
48 |
| - pub fn start_benchmark(&self) -> Result<(), i8> { |
49 |
| - let result = unsafe { ffi::instrument_hooks_start_benchmark(self.0) }; |
50 |
| - if result == 0 { |
51 |
| - Ok(()) |
52 |
| - } else { |
53 |
| - Err(result) |
54 |
| - } |
55 |
| - } |
56 |
| - |
57 |
| - #[inline(always)] |
58 |
| - pub fn stop_benchmark(&self) -> Result<(), i8> { |
59 |
| - let result = unsafe { ffi::instrument_hooks_stop_benchmark(self.0) }; |
60 |
| - if result == 0 { |
61 |
| - Ok(()) |
62 |
| - } else { |
63 |
| - Err(result) |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 |
| - #[inline(always)] |
68 |
| - pub fn set_executed_benchmark(&self, uri: &str) -> Result<(), i8> { |
69 |
| - let pid = std::process::id() as i32; |
70 |
| - let c_uri = CString::new(uri).map_err(|_| -1i8)?; |
71 |
| - let result = unsafe { |
72 |
| - ffi::instrument_hooks_set_executed_benchmark(self.0, pid, c_uri.as_ptr()) |
73 |
| - }; |
74 |
| - if result == 0 { |
75 |
| - Ok(()) |
76 |
| - } else { |
77 |
| - Err(result) |
78 |
| - } |
79 |
| - } |
| 5 | +mod ffi; |
80 | 6 |
|
81 |
| - #[inline(always)] |
82 |
| - pub fn set_integration(&self, name: &str, version: &str) -> Result<(), i8> { |
83 |
| - let c_name = CString::new(name).map_err(|_| -1i8)?; |
84 |
| - let c_version = CString::new(version).map_err(|_| -1i8)?; |
85 |
| - let result = unsafe { |
86 |
| - ffi::instrument_hooks_set_integration(self.0, c_name.as_ptr(), c_version.as_ptr()) |
87 |
| - }; |
88 |
| - if result == 0 { |
89 |
| - Ok(()) |
90 |
| - } else { |
91 |
| - Err(result) |
92 |
| - } |
93 |
| - } |
| 7 | +pub struct InstrumentHooks(*mut ffi::InstrumentHooks); |
94 | 8 |
|
95 |
| - #[inline(always)] |
96 |
| - pub fn add_benchmark_timestamps(&self, start: u64, end: u64) { |
97 |
| - let pid = std::process::id(); |
98 |
| - |
99 |
| - unsafe { |
100 |
| - ffi::instrument_hooks_add_marker( |
101 |
| - self.0, |
102 |
| - pid, |
103 |
| - ffi::MARKER_TYPE_BENCHMARK_START as u8, |
104 |
| - start, |
105 |
| - ) |
106 |
| - }; |
107 |
| - unsafe { |
108 |
| - ffi::instrument_hooks_add_marker( |
109 |
| - self.0, |
110 |
| - pid, |
111 |
| - ffi::MARKER_TYPE_BENCHMARK_END as u8, |
112 |
| - end, |
113 |
| - ) |
114 |
| - }; |
115 |
| - } |
| 9 | +unsafe impl Send for InstrumentHooks {} |
| 10 | +unsafe impl Sync for InstrumentHooks {} |
116 | 11 |
|
117 |
| - #[inline(always)] |
118 |
| - pub fn current_timestamp() -> u64 { |
119 |
| - nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC) |
120 |
| - .expect("Failed to get current time") |
121 |
| - .num_nanoseconds() as u64 |
| 12 | +impl InstrumentHooks { |
| 13 | + #[inline(always)] |
| 14 | + pub fn new() -> Option<Self> { |
| 15 | + let ptr = unsafe { ffi::instrument_hooks_init() }; |
| 16 | + if ptr.is_null() { |
| 17 | + None |
| 18 | + } else { |
| 19 | + Some(InstrumentHooks(ptr)) |
122 | 20 | }
|
123 | 21 | }
|
124 | 22 |
|
125 |
| - impl Drop for InstrumentHooks { |
126 |
| - fn drop(&mut self) { |
127 |
| - if !self.0.is_null() { |
128 |
| - unsafe { ffi::instrument_hooks_deinit(self.0) }; |
129 |
| - } |
130 |
| - } |
| 23 | + /// Returns a singleton instance of `InstrumentHooks`. |
| 24 | + #[inline(always)] |
| 25 | + pub fn instance() -> &'static Self { |
| 26 | + static INSTANCE: OnceLock<InstrumentHooks> = OnceLock::new(); |
| 27 | + INSTANCE.get_or_init(|| { |
| 28 | + let instance = InstrumentHooks::new().expect("Failed to initialize InstrumentHooks"); |
| 29 | + instance |
| 30 | + .set_integration("codspeed-rust", env!("CARGO_PKG_VERSION")) |
| 31 | + .expect("Failed to set integration"); |
| 32 | + instance |
| 33 | + }) |
131 | 34 | }
|
132 |
| -} |
133 | 35 |
|
134 |
| -#[cfg(not(target_os = "linux"))] |
135 |
| -mod other_impl { |
136 |
| - pub struct InstrumentHooks; |
137 |
| - |
138 |
| - impl InstrumentHooks { |
139 |
| - pub fn instance() -> &'static Self { |
140 |
| - static INSTANCE: InstrumentHooks = InstrumentHooks; |
141 |
| - &INSTANCE |
142 |
| - } |
143 |
| - |
144 |
| - pub fn is_instrumented(&self) -> bool { |
145 |
| - false |
146 |
| - } |
| 36 | + #[inline(always)] |
| 37 | + pub fn is_instrumented(&self) -> bool { |
| 38 | + unsafe { ffi::instrument_hooks_is_instrumented(self.0) } |
| 39 | + } |
147 | 40 |
|
148 |
| - pub fn start_benchmark(&self) -> Result<(), i8> { |
| 41 | + #[inline(always)] |
| 42 | + pub fn start_benchmark(&self) -> Result<(), u8> { |
| 43 | + let result = unsafe { ffi::instrument_hooks_start_benchmark(self.0) }; |
| 44 | + if result == 0 { |
149 | 45 | Ok(())
|
| 46 | + } else { |
| 47 | + Err(result) |
150 | 48 | }
|
| 49 | + } |
151 | 50 |
|
152 |
| - pub fn stop_benchmark(&self) -> Result<(), i8> { |
| 51 | + #[inline(always)] |
| 52 | + pub fn stop_benchmark(&self) -> Result<(), u8> { |
| 53 | + let result = unsafe { ffi::instrument_hooks_stop_benchmark(self.0) }; |
| 54 | + if result == 0 { |
153 | 55 | Ok(())
|
| 56 | + } else { |
| 57 | + Err(result) |
154 | 58 | }
|
| 59 | + } |
155 | 60 |
|
156 |
| - pub fn set_executed_benchmark(&self, _uri: &str) -> Result<(), i8> { |
| 61 | + #[inline(always)] |
| 62 | + pub fn set_executed_benchmark(&self, uri: &str) -> Result<(), u8> { |
| 63 | + let pid = std::process::id() as i32; |
| 64 | + let c_uri = CString::new(uri).map_err(|_| 1u8)?; |
| 65 | + let result = |
| 66 | + unsafe { ffi::instrument_hooks_set_executed_benchmark(self.0, pid, c_uri.as_ptr()) }; |
| 67 | + if result == 0 { |
157 | 68 | Ok(())
|
| 69 | + } else { |
| 70 | + Err(result) |
158 | 71 | }
|
| 72 | + } |
159 | 73 |
|
160 |
| - pub fn set_integration(&self, _name: &str, _version: &str) -> Result<(), i8> { |
| 74 | + #[inline(always)] |
| 75 | + pub fn set_integration(&self, name: &str, version: &str) -> Result<(), u8> { |
| 76 | + let c_name = CString::new(name).map_err(|_| 1u8)?; |
| 77 | + let c_version = CString::new(version).map_err(|_| 1u8)?; |
| 78 | + let result = unsafe { |
| 79 | + ffi::instrument_hooks_set_integration(self.0, c_name.as_ptr(), c_version.as_ptr()) |
| 80 | + }; |
| 81 | + if result == 0 { |
161 | 82 | Ok(())
|
| 83 | + } else { |
| 84 | + Err(result) |
162 | 85 | }
|
| 86 | + } |
163 | 87 |
|
164 |
| - pub fn add_benchmark_timestamps(&self, _start: u64, _end: u64) {} |
| 88 | + #[inline(always)] |
| 89 | + pub fn add_benchmark_timestamps(&self, start: u64, end: u64) { |
| 90 | + let pid = std::process::id(); |
| 91 | + |
| 92 | + unsafe { |
| 93 | + ffi::instrument_hooks_add_marker( |
| 94 | + self.0, |
| 95 | + pid, |
| 96 | + ffi::MARKER_TYPE_BENCHMARK_START as u8, |
| 97 | + start, |
| 98 | + ) |
| 99 | + }; |
| 100 | + unsafe { |
| 101 | + ffi::instrument_hooks_add_marker(self.0, pid, ffi::MARKER_TYPE_BENCHMARK_END as u8, end) |
| 102 | + }; |
| 103 | + } |
165 | 104 |
|
166 |
| - pub fn current_timestamp() -> u64 { |
167 |
| - 0 |
168 |
| - } |
| 105 | + #[inline(always)] |
| 106 | + pub fn current_timestamp() -> u64 { |
| 107 | + nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC) |
| 108 | + .expect("Failed to get current time") |
| 109 | + .num_nanoseconds() as u64 |
169 | 110 | }
|
170 | 111 | }
|
171 | 112 |
|
172 |
| -#[cfg(target_os = "linux")] |
173 |
| -pub use linux_impl::InstrumentHooks; |
174 |
| - |
175 |
| -#[cfg(not(target_os = "linux"))] |
176 |
| -pub use other_impl::InstrumentHooks; |
| 113 | +impl Drop for InstrumentHooks { |
| 114 | + fn drop(&mut self) { |
| 115 | + if !self.0.is_null() { |
| 116 | + unsafe { ffi::instrument_hooks_deinit(self.0) }; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
177 | 120 |
|
178 | 121 | #[cfg(test)]
|
179 | 122 | mod tests {
|
|
0 commit comments