From 5d5dbc7080865296964b21a886b345bfbfb08c7d Mon Sep 17 00:00:00 2001 From: Thaumy Date: Fri, 2 Feb 2024 11:16:17 +0800 Subject: [PATCH] introduce `reset` fn for sampling mode and tracing mode --- src/perf_event/sampling/group/fixed.rs | 4 ++++ src/perf_event/sampling/group/inner.rs | 13 +++++++++++++ src/perf_event/sampling/single/mod.rs | 4 ++++ src/perf_event/tracing/tracer/mod.rs | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/src/perf_event/sampling/group/fixed.rs b/src/perf_event/sampling/group/fixed.rs index 58d9857..4403dd4 100644 --- a/src/perf_event/sampling/group/fixed.rs +++ b/src/perf_event/sampling/group/fixed.rs @@ -30,6 +30,10 @@ impl FixedSamplerGroup { self.inner().disable() } + pub fn reset(&self) -> io::Result<()> { + self.inner().reset() + } + pub fn next_record(&mut self, guard: &SamplerGuard) -> Option { self.inner_mut().next_record(guard.event_id()) } diff --git a/src/perf_event/sampling/group/inner.rs b/src/perf_event/sampling/group/inner.rs index 67aec7f..81d93ec 100644 --- a/src/perf_event/sampling/group/inner.rs +++ b/src/perf_event/sampling/group/inner.rs @@ -101,6 +101,19 @@ impl Inner { ) } + pub fn reset(&self) -> io::Result<()> { + self.leader().map_or_else( + || Err(io::Error::new(ErrorKind::Other, "Group has no members")), + |leader| { + ioctl_wrapped( + &leader.file, + PERF_EVENT_IOCTL_RESET, + Some(PERF_IOC_FLAG_GROUP), + ) + }, + ) + } + pub fn next_record(&mut self, event_id: u64) -> Option { self.members .get_mut(&event_id) diff --git a/src/perf_event/sampling/single/mod.rs b/src/perf_event/sampling/single/mod.rs index 02ad752..ac9670a 100644 --- a/src/perf_event/sampling/single/mod.rs +++ b/src/perf_event/sampling/single/mod.rs @@ -95,6 +95,10 @@ impl Sampler { ioctl_wrapped::<()>(&self.file, PERF_EVENT_IOCTL_DISABLE, None) } + pub fn reset(&self) -> io::Result<()> { + ioctl_wrapped::<()>(&self.file, PERF_EVENT_IOCTL_RESET, None) + } + #[cfg(feature = "linux-4.7")] pub fn pause(&self) -> io::Result<()> { ioctl_wrapped(&self.file, PERF_EVENT_IOCTL_PAUSE_OUTPUT, Some(1i32)) diff --git a/src/perf_event/tracing/tracer/mod.rs b/src/perf_event/tracing/tracer/mod.rs index 88e3c8b..e817b72 100644 --- a/src/perf_event/tracing/tracer/mod.rs +++ b/src/perf_event/tracing/tracer/mod.rs @@ -78,6 +78,10 @@ impl Tracer { self.sampler.disable() } + pub fn reset(&self) -> io::Result<()> { + self.sampler.reset() + } + #[cfg(feature = "linux-4.7")] pub fn pause(&self) -> io::Result<()> { self.sampler.pause()