Sha256: 5227aaf4207a35cbfb9aa457e32a99518260579c296e34184019ae389c8900c3

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

use std::time::Instant;

use rb_sys::*;

use crate::backtrace::{Backtrace, BacktraceState};

const MAX_STACK_DEPTH: usize = 500;
const MAX_C_STACK_DEPTH: usize = 1000;

#[derive(Debug, PartialEq)]
pub struct Sample {
    pub ruby_thread: VALUE,
    pub timestamp: Instant,
    pub line_count: i32,
    pub frames: [VALUE; MAX_STACK_DEPTH],
    pub linenos: [i32; MAX_STACK_DEPTH],
    pub c_backtrace_pcs: [usize; MAX_C_STACK_DEPTH + 1],
}

impl Sample {
    // Nearly async-signal-safe
    // (rb_profile_thread_frames isn't defined as a-s-s)
    pub fn capture(ruby_thread: VALUE, backtrace_state: &BacktraceState) -> Self {
        let mut c_backtrace_pcs = [0; MAX_C_STACK_DEPTH + 1];

        Backtrace::backtrace_simple(
            backtrace_state,
            0,
            |pc: usize| -> i32 {
                if c_backtrace_pcs[0] >= MAX_C_STACK_DEPTH {
                    return 1;
                }
                c_backtrace_pcs[0] += 1;
                c_backtrace_pcs[c_backtrace_pcs[0]] = pc;
                0
            },
            Some(Backtrace::backtrace_error_callback),
        );

        let mut sample = Sample {
            ruby_thread,
            timestamp: Instant::now(),
            line_count: 0,
            frames: [0; MAX_STACK_DEPTH],
            linenos: [0; MAX_STACK_DEPTH],
            c_backtrace_pcs,
        };
        unsafe {
            sample.line_count = rb_profile_thread_frames(
                ruby_thread,
                0,
                2000,
                sample.frames.as_mut_ptr(),
                sample.linenos.as_mut_ptr(),
            );
        };
        sample
    }

    pub unsafe fn dmark(&self) {
        rb_gc_mark(self.ruby_thread);
        for frame in self.frames.iter() {
            rb_gc_mark(*frame);
        }
    }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pf2-0.6.0 ext/pf2/src/sample.rs
pf2-0.5.2 ext/pf2/src/sample.rs
pf2-0.5.1 ext/pf2/src/sample.rs
pf2-0.5.0 ext/pf2/src/sample.rs
pf2-0.4.0 ext/pf2/src/sample.rs
pf2-0.3.0 ext/pf2/src/sample.rs