# CPU usage in standalone plugin

**URL:** <https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895>\
**Category:** Audio Plugins\
**Created:** [March 9, 2021, 8:31pm UTC](https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895 "2021-03-09T20:31:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [March 9, 2021, 8:31pm UTC](https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895/1 "2021-03-09T20:31:42Z")

</div>

I’d like to get the CPU usage of my plugin and I assume that I can only do that when I’m running the standalone version of my plugin. And to get the CPU usage, I need to get the AudioDeviceManager from my processor.

Any ideas how to do that?

---

<div class="post-metadata">

**Author:** ![connorreviere](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/connorreviere/32/4890_2.png) [@connorreviere](https://forum.juce.com/u/connorreviere)\
**Post date:** [March 9, 2021, 9:01pm UTC](https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895/2 "2021-03-09T21:01:27Z")

</div>

The only way I know of is to use the [`StandalonePluginHolder`](https://docs.juce.com/master/classStandalonePluginHolder.html) class. You’ll have to include the header file since it’s not included with the `audio_plugin_client` module by default:

```
#include <juce_audio_plugin_client/Standalone/juce_StandaloneFilterWindow.h>

if (auto* holder = juce::StandalonePluginHolder::getInstance())
{
    auto cpuUsage = holder->deviceManager.getCpuUsage();
    // ...
}
```

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [March 9, 2021, 10:17pm UTC](https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895/3 "2021-03-09T22:17:37Z")

</div>

The above solution totally works.

But - you can also init one of those on the stack so you if you have multiple processors and want to measure just one:

```auto
//in your class somewhere:
juce::AudioProcessLoadMeasurer measurer;

void prepareToPlay(double sr, int blockSize) override
{
    measurer.reset(sr, blockSize);
}

void processBlock(AudioBuffer<float>&, MidiBuffer&) override
{
    juce::AudioProcessLoadMeasurer::ScopedTimer s(measurer);
    //processing you want to measure
}

```

---

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [March 10, 2021, 5:24pm UTC](https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895/4 "2021-03-10T17:24:06Z")

</div>

Wonderful. I didn’t even know that class existed. Works perfectly.

I fleshed out your example to be a little more complete. Thanks again.

```
//in your processor class somewhere:
juce::AudioProcessLoadMeasurer measurer;
float cpuLoad; 

void prepareToPlay(double sr, int blockSize) override
{
    measurer.reset(sr, blockSize);
}

void processBlock(AudioBuffer<float>&, MidiBuffer&) override
{
    // I added the inner brackets to be clear exactly what was being measured
    {
        juce::AudioProcessLoadMeasurer::ScopedTimer s(measurer);

        //processing you want to measure
    }
    cpuLoad = measurer.getLoadAsPercentage();
}

// probably called on a timer in your editor
float getCPULoad()
{
    return cpuLoad;
}
```

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [March 10, 2021, 5:35pm UTC](https://forum.juce.com/t/cpu-usage-in-standalone-plugin/44895/5 "2021-03-10T17:35:50Z")

</div>

Great! You probably want to make cpuLoad an `std::atomic<float>`.
