CPU load monitoring

A project log for VSTiBox

VSTiBox is a synthesizer for hosting VST software synths.

Jan BertJan Bert 10/17/2018 at 19:380 Comments

There is a CPU load monitor in the bottom right of the main screen of the VST host. 

It doesn’t show the overall CPU load like Windows Task Manager, but the actual system load for updating VST's and processing the audio buffers. The idea is to measure the duration of all the processing in the ASIO buffer update handler. To do this, a stopwatch is used which is restarted at the beginning of the handler. At the end of the handler function call, the elapsed time inside the handler is compared with the period of one ASIO buffer update, all measured in ticks.  

private Stopwatch mCpuLoadStopWatch = new Stopwatch();

stopWatchTicksForOneAsioBuffer = (long)(Stopwatch.Frequency / (mAsio.SampleRate / mAsioBuffSize));

private void asio_BufferUpdateHandler(object sender, EventArgs e)

    ... VST processing and buffer updates

    int cpuLoad = 0;
    long elapsedTicksDuringHandler = mCpuLoadStopWatch.ElapsedTicks;
    cpuLoad = (int)(elapsedTicksDuringHandler * 100 / stopWatchTicksForOneAsioBuffer);
    if (cpuLoad > mMaxCpuLoad)
        mMaxCpuLoad = cpuLoad;

 The maximum CPU load is read and reset by the UI thread on a regular interval. The idle CPU load is currently 3%. This is a bit high. There are a number of things that are not so efficient here:

1. The programmer :) I’m sure there are still many optimizations possible. I have not used any profiling tools yet. I’ve tried to use unsafe pointer based buffer handling as much as possible, but I have not deeply reviewed what I put together yet.

2. The jump from unmanaged to managed C# code.

3. Garbage collection.

When running a single lightweight VST, the CPU load is about 8%. I'm thinking of rewriting the entire VST plugin and audio buffer update handling in C++. This would also allow me to implement multicore processing with a dedicated audio thread on each CPU core.