Skip to main content

CpuUsage

Struct CpuUsage 

Source
pub struct CpuUsage { /* private fields */ }
Expand description

A reading of the CPU monitoring counters.

percentage is the figure to look at; the rest describes the acquisition cycle it came from and how far along the next one is.

Implementations§

Source§

impl CpuUsage

Source

pub const fn percentage(&self) -> f32

Percentage of the measured period spent busy, averaged over the last completed acquisition cycle.

Zero until the first cycle completes, which takes measurements_per_cycle tics — for the audio thread, that many blocks.

Examples found in repository?
examples/cpu.rs (line 237)
221    fn render_post(&mut self, states: &mut [Bank], context: &mut BlockContext) {
222        #[allow(
223            clippy::cast_precision_loss,
224            reason = "a block's frame count is far below f32's exact integer range"
225        )]
226        let frames = context.audio_frames() as f32;
227        for (phase, increment) in self.phases.iter_mut().zip(&self.phase_increments) {
228            *phase = (*phase + frames * increment) % TAU;
229        }
230
231        self.blocks += 1;
232        if self.blocks % self.blocks_per_report != 0 {
233            return;
234        }
235        // Read here, on the main audio thread, and handed to the task
236        // as plain numbers.
237        let thread = context.cpu_usage().map_or(0.0, |usage| usage.percentage());
238        let section = states
239            .first()
240            .map_or(0.0, |bank| bank.timer.usage().percentage());
241        self.published
242            .thread
243            .store(thread.to_bits(), Ordering::Relaxed);
244        self.published
245            .section
246            .store(section.to_bits(), Ordering::Relaxed);
247        if let Some(task) = &self.task {
248            task.schedule(context);
249        }
250    }
More examples
Hide additional examples
examples/parallel.rs (line 320)
308    fn cleanup(&mut self, states: &mut [Voice], context: &CleanupContext) {
309        let mut rendered = 0;
310        for state in states.iter() {
311            rt_println!(
312                "parallel: thread={} tid={} cpu={} range={}..{} calls={} frames={} section={:.1}%",
313                state.thread,
314                state.thread_id,
315                state.cpu,
316                state.first_frame,
317                state.last_frame,
318                state.calls,
319                state.frames,
320                state.timer.usage().percentage()
321            );
322            rendered += state.frames;
323        }
324        let frames = context.audio_frames() as u64;
325        let expected = self.started * frames;
326        rt_println!(
327            "parallel: blocks={} frames={frames} rendered={rendered} expected={expected}",
328            self.started
329        );
330        rt_println!(
331            "parallel: uncovered={} abandoned={} unfinished={}",
332            self.uncovered,
333            self.abandoned,
334            self.started - self.finished
335        );
336        let busy = context.cpu_usage().map_or(0.0, |usage| usage.percentage());
337        rt_println!("parallel: audio-thread={busy:.1}%");
338    }
Source

pub const fn busy(&self) -> Duration

Busy time accumulated so far in the current, incomplete cycle.

Source

pub const fn total(&self) -> Duration

Total time accumulated so far in the current, incomplete cycle — busy and idle together.

Source

pub const fn measurements_per_cycle(&self) -> u32

How many tics make up an acquisition cycle (Bela’s count).

Source

pub const fn measurements_taken(&self) -> u32

How many tics have happened in the current cycle (Bela’s currentCount).

Trait Implementations§

Source§

impl Clone for CpuUsage

Source§

fn clone(&self) -> CpuUsage

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for CpuUsage

Source§

impl Debug for CpuUsage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for CpuUsage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats as 12.3% busy, averaged over 1000 measurements.

Source§

impl PartialEq for CpuUsage

Source§

fn eq(&self, other: &CpuUsage) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CpuUsage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.