Debian Codegraph @

debian/rust-vendor/codspeed/src/instrument_hooks/mod.rs

raw
1 #[cfg(use_instrument_hooks)]
2 mod ffi;
3
4 #[cfg(use_instrument_hooks)]
5 mod linux_impl {
6
7 use super::ffi;
8 use std::ffi::CString;
9 use std::sync::OnceLock;
10
11 #[derive(PartialEq)]
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.register_toolchain_environment();
39 instance
40 })
41 }
42
43 /// Registers Rust toolchain information (captured at build time) via the
44 /// environment API.
45 ///
46 /// The env var names here must be kept in sync with `build.rs`.
47 fn register_toolchain_environment(&self) {
48 const SECTION: &str = "rust";
49
50 if let Some(v) = option_env!("CODSPEED_RUSTC_VERSION") {
51 let _ = self.set_environment(SECTION, "rustc", v);
52 }
53 if let Some(v) = option_env!("CODSPEED_RUSTC_RELEASE") {
54 let _ = self.set_environment(SECTION, "release", v);
55 }
56 if let Some(v) = option_env!("CODSPEED_RUSTC_LLVM_VERSION") {
57 let _ = self.set_environment(SECTION, "LLVM version", v);
58 }
59 if let Some(v) = option_env!("CODSPEED_CARGO_VERSION") {
60 let _ = self.set_environment(SECTION, "cargo", v);
61 }
62 if let Some(v) = option_env!("CODSPEED_BUILD_PROFILE") {
63 let _ = self.set_environment(SECTION, "profile", v);
64 }
65 if let Some(v) = option_env!("CODSPEED_BUILD_OPT_LEVEL") {
66 let _ = self.set_environment(SECTION, "opt-level", v);
67 }
68 if let Some(v) = option_env!("CODSPEED_BUILD_TARGET_CPU") {
69 let _ = self.set_environment(SECTION, "target-cpu", v);
70 }
71
72 if let Err(e) = self.write_environment() {
73 eprintln!("Warning: failed to write environment info: {e}");
74 }
75 }
76
77 #[inline(always)]
78 pub fn is_instrumented(&self) -> bool {
79 unsafe { ffi::instrument_hooks_is_instrumented(self.0) }
80 }
81
82 #[inline(always)]
83 pub fn start_benchmark(&self) -> Result<(), u8> {
84 let result = unsafe { ffi::instrument_hooks_start_benchmark(self.0) };
85 if result == 0 {
86 Ok(())
87 } else {
88 Err(result)
89 }
90 }
91
92 #[inline(always)]
93 pub fn stop_benchmark(&self) -> Result<(), u8> {
94 let result = unsafe { ffi::instrument_hooks_stop_benchmark(self.0) };
95 if result == 0 {
96 Ok(())
97 } else {
98 Err(result)
99 }
100 }
101
102 #[inline(always)]
103 pub fn set_executed_benchmark(&self, uri: &str) -> Result<(), u8> {
104 let pid = std::process::id() as i32;
105 let c_uri = CString::new(uri).map_err(|_| 1u8)?;
106 let result = unsafe {
107 ffi::instrument_hooks_set_executed_benchmark(self.0, pid, c_uri.as_ptr())
108 };
109 if result == 0 {
110 Ok(())
111 } else {
112 Err(result)
113 }
114 }
115
116 #[inline(always)]
117 pub fn set_integration(&self, name: &str, version: &str) -> Result<(), u8> {
118 let c_name = CString::new(name).map_err(|_| 1u8)?;
119 let c_version = CString::new(version).map_err(|_| 1u8)?;
120 let result = unsafe {
121 ffi::instrument_hooks_set_integration(self.0, c_name.as_ptr(), c_version.as_ptr())
122 };
123 if result == 0 {
124 Ok(())
125 } else {
126 Err(result)
127 }
128 }
129
130 #[inline(always)]
131 pub fn add_benchmark_timestamps(&self, start: u64, end: u64) {
132 let pid = std::process::id() as i32;
133
134 unsafe {
135 ffi::instrument_hooks_add_marker(
136 self.0,
137 pid,
138 ffi::MARKER_TYPE_BENCHMARK_START as u8,
139 start,
140 )
141 };
142 unsafe {
143 ffi::instrument_hooks_add_marker(
144 self.0,
145 pid,
146 ffi::MARKER_TYPE_BENCHMARK_END as u8,
147 end,
148 )
149 };
150 }
151
152 #[inline(always)]
153 pub fn current_timestamp() -> u64 {
154 #[cfg(target_os = "linux")]
155 {
156 use nix::sys::time::TimeValLike;
157
158 nix::time::clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC)
159 .expect("Failed to get current time")
160 .num_nanoseconds() as u64
161 }
162
163 #[cfg(not(target_os = "linux"))]
164 unsafe {
165 ffi::instrument_hooks_current_timestamp()
166 }
167 }
168
169 pub fn set_environment(
170 &self,
171 section_name: &str,
172 key: &str,
173 value: &str,
174 ) -> Result<(), u8> {
175 let c_section = CString::new(section_name).map_err(|_| 1u8)?;
176 let c_key = CString::new(key).map_err(|_| 1u8)?;
177 let c_value = CString::new(value).map_err(|_| 1u8)?;
178 let result = unsafe {
179 ffi::instrument_hooks_set_environment(
180 self.0,
181 c_section.as_ptr(),
182 c_key.as_ptr(),
183 c_value.as_ptr(),
184 )
185 };
186 if result == 0 {
187 Ok(())
188 } else {
189 Err(result)
190 }
191 }
192
193 pub fn write_environment(&self) -> Result<(), u8> {
194 let pid = std::process::id() as i32;
195 let result = unsafe { ffi::instrument_hooks_write_environment(self.0, pid) };
196 if result == 0 {
197 Ok(())
198 } else {
199 Err(result)
200 }
201 }
202
203 pub fn disable_callgrind_markers() {
204 unsafe {
205 ffi::instrument_hooks_set_feature(
206 ffi::instrument_hooks_feature_t_FEATURE_DISABLE_CALLGRIND_MARKERS.into(),
207 true,
208 )
209 };
210 }
211 }
212
213 impl Drop for InstrumentHooks {
214 fn drop(&mut self) {
215 if !self.0.is_null() {
216 unsafe { ffi::instrument_hooks_deinit(self.0) };
217 }
218 }
219 }
220 }
221
222 #[cfg(not(use_instrument_hooks))]
223 mod other_impl {
224 #[derive(PartialEq)]
225 pub struct InstrumentHooks;
226
227 impl InstrumentHooks {
228 pub fn instance() -> &'static Self {
229 static INSTANCE: InstrumentHooks = InstrumentHooks;
230 &INSTANCE
231 }
232
233 pub fn is_instrumented(&self) -> bool {
234 false
235 }
236
237 pub fn start_benchmark(&self) -> Result<(), u8> {
238 Ok(())
239 }
240
241 pub fn stop_benchmark(&self) -> Result<(), u8> {
242 Ok(())
243 }
244
245 pub fn set_executed_benchmark(&self, _uri: &str) -> Result<(), u8> {
246 Ok(())
247 }
248
249 pub fn set_integration(&self, _name: &str, _version: &str) -> Result<(), u8> {
250 Ok(())
251 }
252
253 pub fn add_benchmark_timestamps(&self, _start: u64, _end: u64) {}
254
255 pub fn current_timestamp() -> u64 {
256 0
257 }
258
259 pub fn set_environment(
260 &self,
261 _section_name: &str,
262 _key: &str,
263 _value: &str,
264 ) -> Result<(), u8> {
265 Ok(())
266 }
267
268 pub fn write_environment(&self) -> Result<(), u8> {
269 Ok(())
270 }
271
272 pub fn disable_callgrind_markers() {}
273 }
274 }
275
276 #[cfg(use_instrument_hooks)]
277 pub use linux_impl::InstrumentHooks;
278
279 #[cfg(not(use_instrument_hooks))]
280 pub use other_impl::InstrumentHooks;
281
282 #[cfg(test)]
283 mod tests {
284 use super::InstrumentHooks;
285
286 #[test]
287 fn test_instrument_hooks() {
288 let hooks = InstrumentHooks::instance();
289 assert!(!hooks.is_instrumented() || hooks.start_benchmark().is_ok());
290 assert!(hooks.set_executed_benchmark("test_uri").is_ok());
291 assert!(hooks.set_integration("test_integration", "1.0.0").is_ok());
292 let start = InstrumentHooks::current_timestamp();
293 let end = start + 1_000_000; // Simulate 1ms later
294 hooks.add_benchmark_timestamps(start, end);
295 assert!(!hooks.is_instrumented() || hooks.stop_benchmark().is_ok());
296 }
297 }