Skip to content

Commit

Permalink
remove Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Jan 30, 2024
1 parent 6f3f095 commit cfdf5be
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 307 deletions.
113 changes: 0 additions & 113 deletions src/perf_event/builder.rs

This file was deleted.

45 changes: 45 additions & 0 deletions src/perf_event/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::{io, result};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("PID is invalid: {0}")]
InvalidPid(u32),
#[error("Measures any process on any cpu is invalid")]
InvalidProcessCpu,
#[error("Failed to perform perf_event_open: {0}")]
SyscallFailed(io::Error),
}

pub type Result<T> = result::Result<T, Error>;

pub enum Process {
Any, // -1
Calling, // 0
Specified(u32),
}

impl Process {
pub(crate) const fn as_i32(&self) -> Result<i32> {
match self {
Self::Any => Ok(-1),
Self::Calling => Ok(0),
Self::Specified(0) => Err(Error::InvalidPid(0)),
Self::Specified(n) => Ok(*n as _),
}
}
}

pub enum Cpu {
Any, // -1
Specified(u32),
}

impl Cpu {
pub(crate) const fn as_i32(&self) -> i32 {
match self {
Self::Any => -1,
Self::Specified(n) => *n as _,
}
}
}
29 changes: 0 additions & 29 deletions src/perf_event/counting/builder.rs

This file was deleted.

15 changes: 10 additions & 5 deletions src/perf_event/counting/group/inner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::counting::{inner_stat, Config, Counter, CounterGroupStat};
use crate::counting::{inner_stat, Counter, CounterGroupStat};
use crate::perf_event::RawAttr;
use crate::syscall::bindings::*;
use crate::syscall::ioctl_wrapped;
use crate::syscall::{ioctl_wrapped, perf_event_open_wrapped};
use libc::pid_t;
use std::fs::File;
use std::io;
use std::io::ErrorKind;
use std::os::fd::AsRawFd;
use std::os::fd::{AsRawFd, FromRawFd};

pub struct Inner {
pub(crate) members: Vec<Counter>, // members[0] is the group leader, if it exists.
Expand All @@ -23,9 +25,12 @@ impl Inner {
self.members.get_mut(0)
}

pub fn add_member(&mut self, pid: pid_t, cpu: i32, cfg: &Config) -> io::Result<u64> {
pub fn add_member(&mut self, pid: pid_t, cpu: i32, raw_attr: &RawAttr) -> io::Result<u64> {
let group_fd = self.leader().map(|it| it.file.as_raw_fd()).unwrap_or(-1);
let member = unsafe { Counter::new(cfg, pid, cpu, group_fd, 0) }?;
let fd = unsafe { perf_event_open_wrapped(raw_attr, pid, cpu, group_fd, 0) }?;
let member = Counter {
file: unsafe { File::from_raw_fd(fd) },
};

let event_id = member.event_id()?;
self.members.push(member);
Expand Down
20 changes: 13 additions & 7 deletions src/perf_event/counting/group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub use stat::*;
use std::io;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};

use crate::config;
use crate::config::{Cpu, Process};
use crate::counting::group::inner::Inner;
pub use fixed::*;
#[allow(unused_imports)]
Expand All @@ -27,12 +29,14 @@ pub struct CounterGroup {
}

impl CounterGroup {
pub(crate) unsafe fn new(pid: pid_t, cpu: i32) -> Self {
Self {
pid,
cpu,
inner: Arc::new(RwLock::new(Inner::new())),
}
pub fn new(process: &Process, cpu: &Cpu) -> config::Result<Self> {
let (pid, cpu) = match (process.as_i32()?, cpu.as_i32()) {
(-1, -1) => return Err(config::Error::InvalidProcessCpu),
(pid, cpu) => (pid, cpu),
};
let inner = Arc::new(RwLock::new(Inner::new()));

Ok(Self { pid, cpu, inner })
}

#[allow(dead_code)]
Expand All @@ -45,7 +49,9 @@ impl CounterGroup {
}

pub fn add_member(&mut self, cfg: &Config) -> io::Result<CounterGuard> {
let event_id = self.inner_mut().add_member(self.cpu, self.pid, cfg)?;
let event_id = self
.inner_mut()
.add_member(self.cpu, self.pid, cfg.as_raw())?;
CounterGuard::new(event_id, self.inner.clone()).wrap_ok()
}

Expand Down
2 changes: 0 additions & 2 deletions src/perf_event/counting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod builder;
mod config;
mod group;
mod single;

#[allow(unused_imports)]
pub use builder::*;
pub use config::*;
pub use group::*;
pub use single::*;
Expand Down
30 changes: 13 additions & 17 deletions src/perf_event/counting/single/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ mod stat;
#[cfg(test)]
mod tests;

use crate::config;
use crate::config::{Cpu, Error, Process};
use crate::counting::single::stat::counter_stat;
use crate::counting::Config;
use crate::infra::WrapResult;
use crate::syscall::bindings::*;
use crate::syscall::{ioctl_wrapped, perf_event_open};
use crate::syscall::{ioctl_wrapped, perf_event_open_wrapped};
pub use stat::CounterStat;
use std::fs::File;
use std::io;
Expand All @@ -17,21 +18,16 @@ pub struct Counter {
}

impl Counter {
pub(crate) unsafe fn new(
cfg: &Config,
pid: i32,
cpu: i32,
group_fd: i32,
flags: u64,
) -> io::Result<Self> {
let i32 = unsafe { perf_event_open(cfg.as_raw(), pid, cpu, group_fd, flags) };
match i32 {
-1 => Err(io::Error::last_os_error()),
fd => Self {
file: File::from_raw_fd(fd),
}
.wrap_ok(),
}
pub fn new(process: &Process, cpu: &Cpu, cfg: &Config) -> config::Result<Self> {
let (pid, cpu) = match (process.as_i32()?, cpu.as_i32()) {
(-1, -1) => return Err(Error::InvalidProcessCpu),
(pid, cpu) => (pid, cpu),
};
let fd = unsafe { perf_event_open_wrapped(cfg.as_raw(), pid, cpu, -1, 0) }
.map_err(Error::SyscallFailed)?;
let file = unsafe { File::from_raw_fd(fd) };

Ok(Self { file })
}

pub fn enable(&self) -> io::Result<()> {
Expand Down
3 changes: 1 addition & 2 deletions src/perf_event/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
mod builder;
pub mod counting;
pub mod event;
pub mod sampling;
pub mod tracing;
pub mod config;

use crate::syscall::bindings::perf_event_attr;
pub use builder::*;
pub use event::*;

type RawAttr = perf_event_attr;
39 changes: 0 additions & 39 deletions src/perf_event/sampling/builder.rs

This file was deleted.

Loading

0 comments on commit cfdf5be

Please sign in to comment.