Skip to content

cpumeter

Reports system-wide CPU usage, overall or per logical CPU. This includes other processes; it does not measure Csound's CPU usage alone.

Supported on GNU/Linux and macOS.

Syntax

ktot [, kcpu1, kcpu2, ...] = cpumeter(ifreq)
ktot [, kcpu1, kcpu2, ...] cpumeter ifreq

Initialization

ifreq is the refresh interval in seconds. A value of 0.1 is a useful starting point. Updates occur at control block boundaries, so the interval is rounded up to a whole number of control blocks. Very short intervals can give coarse readings such as zero or one hundred percent.

In Csound 7, ifreq must be finite and nonnegative. Zero requests an update every control block. Initialization and reinitialization set all outputs to zero and take a new CPU counter snapshot. The first interval reading is available at the next update.

Performance

ktot reports the percentage of non-idle CPU time between counter snapshots. Its range is 0 to 100 across all logical CPUs, even when no per-CPU outputs are requested. It is not the sum of the per-CPU percentages.

The optional kcpu1, kcpu2, and subsequent outputs report the same percentage for individual logical CPUs, starting with CPU 0. Up to 32 outputs are supported: the overall reading and the first 31 logical CPUs. Outputs for CPUs that are not present report zero.

Outputs retain their values between updates. In Csound 7, a reading is zero when no CPU counter ticks have elapsed. On macOS, Csound 7 measures usage over the refresh interval; earlier versions used counters accumulated since system startup.

Examples

Here is an example of the cpumeter opcode. It uses the file cpumeter.csd.

Example of the cpumeter opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac     ;;;realtime audio out
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o cpumeter.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 1
0dbfs  = 1


instr 1 ;cpu metering; stop when too large
k0   cpumeter   0.1
     printk2 k0
     if k0>70 then
       event "i", 3, 0.1, 1
     endif
endin

instr 2
     event_i    "i", 2, 1, 1000
     asig oscil 0.2, 440, 1
     out asig
endin

instr 3
     exitnow
endin
</CsInstruments>
<CsScore>
f 1 0 32768 10 1        ; sine wave

i 1 0 1000
i 2 0 1000
e
</CsScore>
</CsoundSynthesizer>

See also

Real-time Performance Control

Credits

Author: John ffitch
May 2011

New in Csound version 5.14.