| 112 | /// Returns [`SamplerError`] if reading thread counters fails. |
| 113 | #[expect(clippy::cast_possible_truncation, clippy::indexing_slicing)] |
| 114 | pub fn sample(&self) -> Result<[u64; N], SamplerError> { |
| 115 | if !self.running { |
| 116 | return Err(SamplerError::SamplerNotRunning); |
| 117 | } |
| 118 | |
| 119 | let kpc_vt = self.sampler.kperf.vtable(); |
| 120 | let mut counters = [0; KPC_MAX_COUNTERS]; |
| 121 | |
| 122 | try_kpc( |
| 123 | // SAFETY: buffer is KPC_MAX_COUNTERS elements, matching the count |
| 124 | // parameter. tid=0 reads the calling thread's counters. |
| 125 | unsafe { |
| 126 | (kpc_vt.kpc_get_thread_counters)(0, KPC_MAX_COUNTERS as u32, counters.as_mut_ptr()) |
| 127 | }, |
| 128 | SamplerError::UnableToReadCounters, |
| 129 | )?; |
| 130 | |
| 131 | let output = array::from_fn(|index| { |
| 132 | let counter_index = self.counter_map[index]; |
| 133 | counters[counter_index] |
| 134 | }); |
| 135 | |
| 136 | Ok(output) |
| 137 | } |
| 138 | |
| 139 | /// Disables counting. |
| 140 | /// |