Skip to content

Commit

Permalink
Use frequency rather than period and make it configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
javierhonduco committed Apr 21, 2024
1 parent a9ea414 commit c0d0956
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
7 changes: 3 additions & 4 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ unsafe fn perf_event_open(
}

/// # Safety
pub unsafe fn setup_perf_event(cpu: i32, sample_period: u64) -> Result<c_int> {
pub unsafe fn setup_perf_event(cpu: i32, sample_freq: u64) -> Result<c_int> {
let mut attrs = perf_event_open_sys::bindings::perf_event_attr {
size: std::mem::size_of::<sys::bindings::perf_event_attr>() as u32,
type_: sys::bindings::PERF_TYPE_SOFTWARE,
config: sys::bindings::PERF_COUNT_SW_CPU_CLOCK as u64,
..Default::default()
};
attrs.__bindgen_anon_1.sample_period = sample_period;
attrs.__bindgen_anon_1.sample_freq = sample_freq;
attrs.set_disabled(1);
attrs.set_freq(1);

let fd = perf_event_open(
&mut attrs,
Expand Down Expand Up @@ -85,8 +86,6 @@ pub unsafe fn setup_syscall_event(syscall: &str) -> Result<c_int> {
debug!("syscall with id {} found in {}", id, &path);

attrs.config = id.parse::<u64>()?;
// attrs.__bindgen_anon_1.sample_period = sample_period;
// attr.wakeup_events = 1;
attrs.set_disabled(1);

let fd = perf_event_open(
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct RecordSubcommand {
pid: i32,
#[clap(short, long)]
duration: Option<u64>,
#[clap(short, long)]
sample_freq: Option<u64>,
#[clap(subcommand)]
record_type: RecordType,
#[clap(long)]
Expand Down Expand Up @@ -135,9 +137,12 @@ fn main() -> Result<()> {
};

let event = match record.record_type {
RecordType::Cpu => RbperfEvent::Cpu {
sample_period: 99999,
},
RecordType::Cpu => {
let sample_freq = record.sample_freq.unwrap_or(977);
println!("Profiling at {sample_freq} hz");

RbperfEvent::Cpu { sample_freq }
}
RecordType::Syscall(ref syscall_subcommand) => {
RbperfEvent::Syscall(syscall_subcommand.names.clone())
}
Expand Down
17 changes: 6 additions & 11 deletions src/rbperf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ use num_cpus;

#[derive(Clone)]
pub enum RbperfEvent {
Cpu { sample_period: u64 },
Cpu { sample_freq: u64 },
Syscall(Vec<String>),
}

impl From<RbperfEvent> for rbperf_event_type {
fn from(event: RbperfEvent) -> rbperf_event_type {
match event {
RbperfEvent::Cpu { sample_period: _ } => {
rbperf_event_type::RBPERF_EVENT_ON_CPU_SAMPLING
}
RbperfEvent::Cpu { sample_freq: _ } => rbperf_event_type::RBPERF_EVENT_ON_CPU_SAMPLING,
RbperfEvent::Syscall(_) => rbperf_event_type::RBPERF_EVENT_SYSCALL,
}
}
Expand Down Expand Up @@ -173,7 +171,7 @@ impl<'a> Rbperf<'a> {
}

match options.event {
RbperfEvent::Cpu { sample_period: _ } => {
RbperfEvent::Cpu { sample_freq: _ } => {
for prog in open_skel.obj.progs_iter_mut() {
prog.set_prog_type(ProgramType::PerfEvent);
}
Expand Down Expand Up @@ -297,10 +295,9 @@ impl<'a> Rbperf<'a> {
let mut fds = Vec::new();

match self.event {
RbperfEvent::Cpu { sample_period } => {
RbperfEvent::Cpu { sample_freq } => {
for i in 0..num_cpus::get() {
let perf_fd =
unsafe { setup_perf_event(i.try_into().unwrap(), sample_period) }?;
let perf_fd = unsafe { setup_perf_event(i.try_into().unwrap(), sample_freq) }?;
fds.push(perf_fd);
}
}
Expand Down Expand Up @@ -608,9 +605,7 @@ mod tests {
thread::sleep(Duration::from_millis(250));

let options = RbperfOptions {
event: RbperfEvent::Cpu {
sample_period: 99999,
},
event: RbperfEvent::Cpu { sample_freq: 977 },
verbose_bpf_logging: true,
use_ringbuf: false,
verbose_libbpf_logging: false,
Expand Down

0 comments on commit c0d0956

Please sign in to comment.