From f5c5b8cbac16754ca1d4a12e2768e855c7038175 Mon Sep 17 00:00:00 2001 From: Chengdong Li Date: Thu, 11 Apr 2024 14:35:22 +0800 Subject: [PATCH] Add a get_fd() help for sampling to support for other uses. --- src/perf_event/sampling/single/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/perf_event/sampling/single/mod.rs b/src/perf_event/sampling/single/mod.rs index 5915e6f..045d7f4 100644 --- a/src/perf_event/sampling/single/mod.rs +++ b/src/perf_event/sampling/single/mod.rs @@ -29,7 +29,7 @@ use crate::syscall::{ioctl_wrapped, perf_event_open_wrapped}; use memmap2::{MmapMut, MmapOptions}; use std::fs::File; use std::io; -use std::os::fd::FromRawFd; +use std::os::fd::{AsRawFd, FromRawFd}; use crate::config::{Cpu, Error, Process}; use crate::sampling::single::stat::sampler_stat; @@ -145,4 +145,10 @@ impl Sampler { pub fn stat(&mut self) -> io::Result { sampler_stat(self) } + + /// Returns the underlying perf event fd for other uses. + /// For example attach a eBPF program to this event. + pub fn get_fd(&self) -> i32 { + self.file.as_raw_fd() + } }