11use std:: {
22 ffi:: { CString , OsStr , c_long, c_uint} ,
33 io, mem,
4- num:: NonZeroU32 ,
54 os:: fd:: { BorrowedFd , FromRawFd as _} ,
65} ;
76
@@ -124,13 +123,7 @@ pub(crate) fn perf_event_open(
124123 }
125124 }
126125
127- let ( pid, cpu) = match scope {
128- PerfEventScope :: CallingProcess { cpu } => ( 0 , cpu. map_or ( -1 , |cpu| cpu as i32 ) ) ,
129- PerfEventScope :: OneProcess { pid, cpu } => {
130- ( pid. get ( ) as i32 , cpu. map_or ( -1 , |cpu| cpu as i32 ) )
131- }
132- PerfEventScope :: AllProcessesOneCpu { cpu } => ( -1 , cpu as i32 ) ,
133- } ;
126+ let ( pid, cpu) = perf_event_scope_pid_cpu ( scope) ;
134127
135128 perf_event_sys ( attr, pid, cpu, flags)
136129}
@@ -140,7 +133,7 @@ pub(crate) fn perf_event_open_probe(
140133 ret_bit : Option < u32 > ,
141134 name : & OsStr ,
142135 offset : u64 ,
143- pid : Option < u32 > ,
136+ scope : PerfEventScope ,
144137) -> io:: Result < crate :: MockableFd > {
145138 use std:: os:: unix:: ffi:: OsStrExt as _;
146139
@@ -157,23 +150,15 @@ pub(crate) fn perf_event_open_probe(
157150 attr. __bindgen_anon_3 . config1 = c_name. as_ptr ( ) as u64 ;
158151 attr. __bindgen_anon_4 . config2 = offset;
159152
160- let ( pid, cpu) = match pid {
161- Some ( pid) => ( pid as i32 , -1 ) ,
162- None => ( -1 , 0 ) ,
163- } ;
153+ let ( pid, cpu) = perf_event_scope_pid_cpu ( scope) ;
164154
165155 perf_event_sys ( attr, pid, cpu, PERF_FLAG_FD_CLOEXEC )
166156}
167157
168158pub ( crate ) fn perf_event_open_trace_point (
169159 event_id : u64 ,
170- pid : Option < u32 > ,
160+ scope : PerfEventScope ,
171161) -> io:: Result < crate :: MockableFd > {
172- let scope = match pid. map ( NonZeroU32 :: new) {
173- None => PerfEventScope :: AllProcessesOneCpu { cpu : 0 } ,
174- Some ( None ) => PerfEventScope :: CallingProcess { cpu : None } ,
175- Some ( Some ( pid) ) => PerfEventScope :: OneProcess { pid, cpu : None } ,
176- } ;
177162 perf_event_open (
178163 PerfEventConfig :: TracePoint { event_id } ,
179164 scope,
@@ -184,6 +169,16 @@ pub(crate) fn perf_event_open_trace_point(
184169 )
185170}
186171
172+ fn perf_event_scope_pid_cpu ( scope : PerfEventScope ) -> ( pid_t , i32 ) {
173+ match scope {
174+ PerfEventScope :: CallingProcess { cpu } => ( 0 , cpu. map_or ( -1 , |cpu| cpu as i32 ) ) ,
175+ PerfEventScope :: OneProcess { pid, cpu } => {
176+ ( pid. get ( ) as i32 , cpu. map_or ( -1 , |cpu| cpu as i32 ) )
177+ }
178+ PerfEventScope :: AllProcessesOneCpu { cpu } => ( -1 , cpu as i32 ) ,
179+ }
180+ }
181+
187182pub ( crate ) fn perf_event_ioctl (
188183 fd : BorrowedFd < ' _ > ,
189184 request : PerfEventIoctlRequest < ' _ > ,
@@ -260,21 +255,42 @@ impl TryFrom<u32> for perf_event_type {
260255
261256#[ cfg( test) ]
262257mod tests {
263- use std:: os:: fd:: AsRawFd as _;
258+ use std:: { num :: NonZeroU32 , os:: fd:: AsRawFd as _} ;
264259
265260 use libc:: pid_t;
266261 use test_case:: test_case;
267262
268263 use super :: { PERF_FLAG_FD_CLOEXEC , perf_event_open_trace_point} ;
269- use crate :: sys:: { Syscall , override_syscall} ;
264+ use crate :: {
265+ programs:: perf_event:: PerfEventScope ,
266+ sys:: { Syscall , override_syscall} ,
267+ } ;
270268
271269 const EVENT_ID : u64 = 123 ;
272270
273- #[ test_case( None , -1 , 0 ; "all_processes" ) ]
274- #[ test_case( Some ( 0 ) , 0 , -1 ; "calling_process" ) ]
275- #[ test_case( Some ( 42 ) , 42 , -1 ; "one_process" ) ]
276- fn perf_event_open_trace_point_maps_pid_scope (
277- pid : Option < u32 > ,
271+ #[ test_case(
272+ PerfEventScope :: AllProcessesOneCpu { cpu: 0 } ,
273+ -1 ,
274+ 0 ;
275+ "all_processes"
276+ ) ]
277+ #[ test_case(
278+ PerfEventScope :: CallingProcess { cpu: None } ,
279+ 0 ,
280+ -1 ;
281+ "calling_process"
282+ ) ]
283+ #[ test_case(
284+ PerfEventScope :: OneProcess {
285+ pid: NonZeroU32 :: new( 42 ) . unwrap( ) ,
286+ cpu: None ,
287+ } ,
288+ 42 ,
289+ -1 ;
290+ "one_process"
291+ ) ]
292+ fn perf_event_open_trace_point_maps_scope (
293+ scope : PerfEventScope ,
278294 expected_pid : pid_t ,
279295 expected_cpu : i32 ,
280296 ) {
@@ -297,7 +313,7 @@ mod tests {
297313 call => panic ! ( "unexpected syscall: {call:?}" ) ,
298314 } ) ;
299315
300- let fd = perf_event_open_trace_point ( EVENT_ID , pid ) . unwrap ( ) ;
316+ let fd = perf_event_open_trace_point ( EVENT_ID , scope ) . unwrap ( ) ;
301317 assert_eq ! ( fd. as_raw_fd( ) , crate :: MockableFd :: mock_signed_fd( ) ) ;
302318 }
303319}
0 commit comments