Skip to content

Commit c8b8b59

Browse files
committed
Change Builder to use &mut self and implement Clone
There are two main changes in this commit: - derive Clone and Debug for Builder - make the methods on Builder take &mut self and return &mut Self Combined, these make Builder a bit easier to use and closer in uses to something like std::process::Command.
1 parent bb6640e commit c8b8b59

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

perf-event/src/builder.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ use crate::{check_errno_syscall, sys, Counter, SampleFlag};
5858
/// [`enable`]: Counter::enable
5959
/// [`Group`]: crate::Group
6060
/// [`Group::add`]: crate::Group::add
61+
#[derive(Clone, Debug)]
6162
pub struct Builder<'a> {
6263
attrs: perf_event_attr,
6364
who: EventPid<'a>,
6465
cpu: Option<usize>,
6566
}
6667

67-
#[derive(Debug)]
68+
#[derive(Clone, Debug)]
6869
enum EventPid<'a> {
6970
/// Monitor the calling process.
7071
ThisProcess,
@@ -119,19 +120,19 @@ impl<'a> Builder<'a> {
119120
}
120121

121122
/// Include kernel code.
122-
pub fn include_kernel(mut self) -> Self {
123+
pub fn include_kernel(&mut self) -> &mut Self {
123124
self.attrs.set_exclude_kernel(0);
124125
self
125126
}
126127

127128
/// Include hypervisor code.
128-
pub fn include_hv(mut self) -> Self {
129+
pub fn include_hv(&mut self) -> &mut Self {
129130
self.attrs.set_exclude_hv(0);
130131
self
131132
}
132133

133134
/// Observe the calling process. (This is the default.)
134-
pub fn observe_self(mut self) -> Self {
135+
pub fn observe_self(&mut self) -> &mut Self {
135136
self.who = EventPid::ThisProcess;
136137
self
137138
}
@@ -140,7 +141,7 @@ impl<'a> Builder<'a> {
140141
/// [`CAP_SYS_PTRACE`][man-capabilities] capabilities.
141142
///
142143
/// [man-capabilities]: http://man7.org/linux/man-pages/man7/capabilities.7.html
143-
pub fn observe_pid(mut self, pid: pid_t) -> Self {
144+
pub fn observe_pid(&mut self, pid: pid_t) -> &mut Self {
144145
self.who = EventPid::Other(pid);
145146
self
146147
}
@@ -160,7 +161,7 @@ impl<'a> Builder<'a> {
160161
/// [`build`]: Builder::build
161162
/// [`one_cpu`]: Builder::one_cpu
162163
/// [cap]: http://man7.org/linux/man-pages/man7/capabilities.7.html
163-
pub fn any_pid(mut self) -> Self {
164+
pub fn any_pid(&mut self) -> &mut Self {
164165
self.who = EventPid::Any;
165166
self
166167
}
@@ -170,13 +171,13 @@ impl<'a> Builder<'a> {
170171
/// in the cgroupfs filesystem.
171172
///
172173
/// [man-cgroups]: http://man7.org/linux/man-pages/man7/cgroups.7.html
173-
pub fn observe_cgroup(mut self, cgroup: &'a File) -> Self {
174+
pub fn observe_cgroup(&mut self, cgroup: &'a File) -> &mut Self {
174175
self.who = EventPid::CGroup(cgroup);
175176
self
176177
}
177178

178179
/// Observe only code running on the given CPU core.
179-
pub fn one_cpu(mut self, cpu: usize) -> Self {
180+
pub fn one_cpu(&mut self, cpu: usize) -> &mut Self {
180181
self.cpu = Some(cpu);
181182
self
182183
}
@@ -193,7 +194,7 @@ impl<'a> Builder<'a> {
193194
/// [`observe_self`]: Builder::observe_self
194195
/// [`observe_pid`]: Builder::observe_pid
195196
/// [`observe_cgroup`]: Builder::observe_cgroup
196-
pub fn any_cpu(mut self) -> Self {
197+
pub fn any_cpu(&mut self) -> &mut Self {
197198
self.cpu = None;
198199
self
199200
}
@@ -209,7 +210,7 @@ impl<'a> Builder<'a> {
209210
/// This flag cannot be set if the counter belongs to a `Group`. Doing so
210211
/// will result in an error when the counter is built. This is a kernel
211212
/// limitation.
212-
pub fn inherit(mut self, inherit: bool) -> Self {
213+
pub fn inherit(&mut self, inherit: bool) -> &mut Self {
213214
let flag = if inherit { 1 } else { 0 };
214215
self.attrs.set_inherit(flag);
215216
self
@@ -290,7 +291,7 @@ impl<'a> Builder<'a> {
290291
///
291292
/// [`SampleFlag`]: crate::SampleFlag
292293
/// [manpage]: http://man7.org/linux/man-pages/man2/perf_event_open.2.html
293-
pub fn sample(mut self, sample: SampleFlag) -> Self {
294+
pub fn sample(&mut self, sample: SampleFlag) -> &mut Self {
294295
self.attrs.sample_type |= sample.bits();
295296
self
296297
}
@@ -299,7 +300,7 @@ impl<'a> Builder<'a> {
299300
///
300301
/// MMAP records are emitted when the process/thread that is being
301302
/// observed creates a new executable memory mapping.
302-
pub fn mmap(mut self, mmap: bool) -> Self {
303+
pub fn mmap(&mut self, mmap: bool) -> &mut Self {
303304
self.attrs.set_mmap(mmap.into());
304305
self
305306
}
@@ -311,7 +312,7 @@ impl<'a> Builder<'a> {
311312
/// configured.
312313
///
313314
/// [`wakeup_events`]: Self::wakeup_events
314-
pub fn wakeup_watermark(mut self, watermark: usize) -> Self {
315+
pub fn wakeup_watermark(&mut self, watermark: usize) -> &mut Self {
315316
self.attrs.set_watermark(1);
316317
self.attrs.__bindgen_anon_2.wakeup_watermark = watermark as _;
317318
self
@@ -327,7 +328,7 @@ impl<'a> Builder<'a> {
327328
///
328329
/// [manpage]: https://man7.org/linux/man-pages/man2/perf_event_open.2.html
329330
/// [`wakeup_watermark`]: Self::wakeup_watermark
330-
pub fn wakeup_events(mut self, events: usize) -> Self {
331+
pub fn wakeup_events(&mut self, events: usize) -> &mut Self {
331332
self.attrs.set_watermark(0);
332333
self.attrs.__bindgen_anon_2.wakeup_events = events as _;
333334
self

perf-event/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ fn test_error_code_is_correct() {
177177
// about test failures when in VMs.
178178
let builder = Builder::new(events::Software::CPU_CLOCK)
179179
// There should _hopefully_ never be a system with this many CPUs.
180-
.one_cpu(i32::MAX as usize);
180+
.one_cpu(i32::MAX as usize)
181+
.clone();
181182

182183
match builder.build() {
183184
Ok(_) => panic!("counter construction was not supposed to succeed"),

0 commit comments

Comments
 (0)