@@ -21,12 +21,9 @@ pub(crate) fn read_thread_name(tid: u32) -> Option<String> {
2121/// Configuration for CPU profiling integration.
2222#[ derive( Debug , Clone ) ]
2323pub struct CpuProfilingConfig {
24- /// Sampling frequency in Hz. Default: 99 (low overhead).
25- pub frequency_hz : u64 ,
26- /// Which perf event source to use.
27- pub event_source : EventSource ,
28- /// Whether to include kernel stack frames.
29- pub include_kernel : bool ,
24+ frequency_hz : u64 ,
25+ event_source : EventSource ,
26+ include_kernel : bool ,
3027}
3128
3229impl Default for CpuProfilingConfig {
@@ -39,6 +36,26 @@ impl Default for CpuProfilingConfig {
3936 }
4037}
4138
39+ impl CpuProfilingConfig {
40+ /// Sampling frequency in Hz. Default: 99 (low overhead).
41+ pub fn frequency_hz ( mut self , hz : u64 ) -> Self {
42+ self . frequency_hz = hz;
43+ self
44+ }
45+
46+ /// Which perf event source to use.
47+ pub fn event_source ( mut self , source : EventSource ) -> Self {
48+ self . event_source = source;
49+ self
50+ }
51+
52+ /// Whether to include kernel stack frames.
53+ pub fn include_kernel ( mut self , yes : bool ) -> Self {
54+ self . include_kernel = yes;
55+ self
56+ }
57+ }
58+
4259/// Configuration for per-worker sched event capture (context switches).
4360///
4461/// Uses `perf_event_open` with `SwContextSwitches` in per-thread mode,
@@ -82,11 +99,12 @@ pub(crate) struct CpuProfiler {
8299
83100impl CpuProfiler {
84101 pub ( crate ) fn start ( config : CpuProfilingConfig ) -> io:: Result < Self > {
85- let sampler = PerfSampler :: start ( SamplerConfig {
86- event_source : config. event_source ,
87- sampling : SamplingMode :: FrequencyHz ( config. frequency_hz ) ,
88- include_kernel : config. include_kernel ,
89- } ) ?;
102+ let sampler = PerfSampler :: start (
103+ SamplerConfig :: default ( )
104+ . event_source ( config. event_source )
105+ . sampling ( SamplingMode :: FrequencyHz ( config. frequency_hz ) )
106+ . include_kernel ( config. include_kernel ) ,
107+ ) ?;
90108 Ok ( Self {
91109 sampler,
92110 pid : std:: process:: id ( ) ,
@@ -130,11 +148,12 @@ pub(crate) struct SchedProfiler {
130148
131149impl SchedProfiler {
132150 pub ( crate ) fn new ( config : SchedEventConfig ) -> io:: Result < Self > {
133- let sampler = PerfSampler :: new_per_thread ( SamplerConfig {
134- event_source : EventSource :: SwContextSwitches ,
135- sampling : SamplingMode :: Period ( config. sampling_interval . unwrap_or ( 1 ) ) ,
136- include_kernel : config. include_kernel ,
137- } ) ?;
151+ let sampler = PerfSampler :: new_per_thread (
152+ SamplerConfig :: default ( )
153+ . event_source ( EventSource :: SwContextSwitches )
154+ . sampling ( SamplingMode :: Period ( config. sampling_interval . unwrap_or ( 1 ) ) )
155+ . include_kernel ( config. include_kernel ) ,
156+ ) ?;
138157 Ok ( Self { sampler } )
139158 }
140159
0 commit comments