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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#[macro_use]
extern crate serde_derive;
extern crate threadinfo;

#[cfg(target_os = "linux")]
mod lib_linux;
#[cfg(target_os = "linux")]
pub use lib_linux::*;

#[cfg(target_os = "macos")]
mod lib_mac;
#[cfg(target_os = "macos")]
pub use lib_mac::*;

pub mod output;
pub mod speedscope;

mod module_cache;
pub mod types;

use std::collections::HashMap;

use threadinfo::Thread as ThreadId;
use types::{Frame, Unwinder};

pub struct Profiler {
    sampler: Sampler,
    // TODO: If we want to support programs that load and unload shared libraries, we will want to
    // capture the state of all modules at the time of profile capture. Then we'd have to have a
    // module cache here, and propagate it to the profile.
}

impl Profiler {
    pub fn new() -> Profiler {
        Profiler {
            // TODO: Once we have a start/stop based interface, move this construction to there,
            // since this overrides the signal handler for the process and is only needed later. We
            // probably want some session kind of concept.
            sampler: Sampler::new(),
        }
    }

    pub fn session(&self) -> Session {
        Session {
            profiler: &self,
            threads: HashMap::new(),
        }
    }
}

pub struct Session<'a> {
    profiler: &'a Profiler,
    threads: HashMap<ThreadId, Vec<Vec<Frame>>>,
}

impl<'a> Session<'a> {
    /// Samples one thread once.
    /// Panics if the thread is the sampling thread.
    pub fn sample_thread(&mut self, thread: ThreadId) {
        let sample = self.sample_once(thread);
        self.threads
            .entry(thread)
            .or_insert_with(|| Vec::new())
            .push(sample);
    }

    fn sample_once(&self, thread: ThreadId) -> Vec<Frame> {
        // TODO: Want to make the sample sizes configurable.
        let unwinder = LibunwindUnwinder::new(150);
        // TODO: Need to think if this interface is the best.
        self.profiler
            .sampler
            .suspend_and_resume_thread(thread, move |context| {
                // TODO: For perf we probably actually want to allow re-use of the sample storage,
                // instead of allocating new frames above every time.
                // i.e. once a sample has been captured and turned into some other representation, we
                // could re-use the vector.
                unwinder.unwind(context).expect("sample succeeded")
            })
    }

    pub fn finish(self) -> Profile {
        Profile {
            threads: self.threads,
        }
    }
}

/// In-memory profile. This is just an opaque container for now.
/// Use the Outputter to obtain a serializable form with build IDs resolved.
pub struct Profile {
    threads: HashMap<ThreadId, Vec<Vec<Frame>>>,
}

// TODO: Can we also have an iterator interface where each iteration causes a sampling? That way it
// would be lazy.

#[cfg(test)]
mod tests {
    use super::*;
    use std::{sync::mpsc::channel, thread::spawn};

    #[cfg(target_os = "linux")]
    #[test]
    fn test_suspend_resume() {
        let sampler = Sampler::new();
        let (tx, rx) = channel();
        // Just to get the thread to wait until the test is done.
        let (tx2, rx2) = channel();
        let handle = spawn(move || {
            tx.send(threadinfo::current_thread().unwrap()).unwrap();
            rx2.recv().unwrap();
        });

        let to = rx.recv().unwrap();
        sampler.suspend_and_resume_thread(to, |context| {
            // TODO: This is where we would want to use libunwind in a real program.
            assert!(context.uc_stack.ss_size > 0);

            // we can tell the thread to shutdown once it is resumed.
            tx2.send(()).unwrap();
        });

        handle.join().unwrap();
    }
}