diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:19:53 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:20:13 +0100 |
commit | 7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch) | |
tree | bdb47765991cb973b2cd5481a088fac636bd326c /crates/profile/src/google_cpu_profiler.rs | |
parent | ca464650eeaca6195891199a93f4f76cf3e7e697 (diff) | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/profile/src/google_cpu_profiler.rs')
-rw-r--r-- | crates/profile/src/google_cpu_profiler.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/crates/profile/src/google_cpu_profiler.rs b/crates/profile/src/google_cpu_profiler.rs new file mode 100644 index 000000000..db865c65b --- /dev/null +++ b/crates/profile/src/google_cpu_profiler.rs | |||
@@ -0,0 +1,39 @@ | |||
1 | //! https://github.com/gperftools/gperftools | ||
2 | |||
3 | use std::{ | ||
4 | ffi::CString, | ||
5 | os::raw::c_char, | ||
6 | path::Path, | ||
7 | sync::atomic::{AtomicUsize, Ordering}, | ||
8 | }; | ||
9 | |||
10 | #[link(name = "profiler")] | ||
11 | #[allow(non_snake_case)] | ||
12 | extern "C" { | ||
13 | fn ProfilerStart(fname: *const c_char) -> i32; | ||
14 | fn ProfilerStop(); | ||
15 | } | ||
16 | |||
17 | static PROFILER_STATE: AtomicUsize = AtomicUsize::new(OFF); | ||
18 | const OFF: usize = 0; | ||
19 | const ON: usize = 1; | ||
20 | const PENDING: usize = 2; | ||
21 | |||
22 | pub fn start(path: &Path) { | ||
23 | if PROFILER_STATE.compare_and_swap(OFF, PENDING, Ordering::SeqCst) != OFF { | ||
24 | panic!("profiler already started"); | ||
25 | } | ||
26 | let path = CString::new(path.display().to_string()).unwrap(); | ||
27 | if unsafe { ProfilerStart(path.as_ptr()) } == 0 { | ||
28 | panic!("profiler failed to start") | ||
29 | } | ||
30 | assert!(PROFILER_STATE.compare_and_swap(PENDING, ON, Ordering::SeqCst) == PENDING); | ||
31 | } | ||
32 | |||
33 | pub fn stop() { | ||
34 | if PROFILER_STATE.compare_and_swap(ON, PENDING, Ordering::SeqCst) != ON { | ||
35 | panic!("profiler is not started") | ||
36 | } | ||
37 | unsafe { ProfilerStop() }; | ||
38 | assert!(PROFILER_STATE.compare_and_swap(PENDING, OFF, Ordering::SeqCst) == PENDING); | ||
39 | } | ||