aboutsummaryrefslogtreecommitdiff
path: root/crates/profile/src/google_cpu_profiler.rs
blob: cae6caeaa669c034f69725d0e5847da6a773262a (plain)
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
//! https://github.com/gperftools/gperftools

use std::{
    ffi::CString,
    os::raw::c_char,
    path::Path,
    sync::atomic::{AtomicUsize, Ordering},
};

#[link(name = "profiler")]
#[allow(non_snake_case)]
extern "C" {
    fn ProfilerStart(fname: *const c_char) -> i32;
    fn ProfilerStop();
}

const OFF: usize = 0;
const ON: usize = 1;
const PENDING: usize = 2;

fn transition(current: usize, new: usize) -> bool {
    static STATE: AtomicUsize = AtomicUsize::new(OFF);

    STATE.compare_exchange(current, new, Ordering::SeqCst, Ordering::SeqCst).is_ok()
}

pub(crate) fn start(path: &Path) {
    if !transition(OFF, PENDING) {
        panic!("profiler already started");
    }
    let path = CString::new(path.display().to_string()).unwrap();
    if unsafe { ProfilerStart(path.as_ptr()) } == 0 {
        panic!("profiler failed to start")
    }
    assert!(transition(PENDING, ON));
}

pub(crate) fn stop() {
    if !transition(ON, PENDING) {
        panic!("profiler is not started")
    }
    unsafe { ProfilerStop() };
    assert!(transition(PENDING, OFF));
}