CPU_Usage is a very lightweight library that can be easily incorporated into your application for testing performance. The reading is given as a percentage of CPU time which can be output however you like (serial, tft.. etc).

Dependents:   nucleo_encoder_stepper mbed-os-rest-api-V1-1

. If you are looking to measure the overall CPU Usage, call the working() function at the start of your code, and just call update() any time that you want to read the CPU usage value. Easy as that!

If you want to measure the CPU usage of a particular algorithm, place the working() function call before the code, and place the stopped() function call at the end. This limits the measuring purely to the usage of that code. calling update() will then then give you the percentage result.

When creating a CPU_Usage object you pass in 2x arguments. the first is a timer object, and the second is a ballast number (in seconds) precise to a single precision float value. This number can be though of as a smoothing number, play with this number to suit your application.

Hot Tips!:

- Use the library's delay() function instead of the built in mbed wait() function, this will exclude wait time from the usage value.

- If putting the device to sleep, place the stopped() function beforehand, and call the working() function after waking. This will exclude sleep time from CPU usage. ( will work on this for future so it will be handled automatically).

The API is so simple It almost needs no introduction. But documentation will follow soon. In the mean time here are two demos to get you started:

General Use Sample Code (captures all CPU usage)

#include "mbed.h"
#include "CPU_Usage.h"

Serial pc(USBTX, USBRX);

Timer t;		                // create your timer object
CPU_Usage cpu(t, 1);	// create you CPU_Usage object

int main() {

   cpu.working();	// tell the CPU_Usage object that work has started
   uint8_t value = 0;

   while(1) {
                                        // HOT TIP!!!
       cpu.delay(0.25);	// use this in place of wait() to exclude waiting time from
			                // your usage value.

       value = cpu.update();	// retrieves the usage value

       pc.printf("CPU %i", value);	// send the value over serial.. if you want.
       }

}

Specific Code Section Sample Code (captures CPU usage only for the code between working() and stopped() )

#include "mbed.h"
#include "CPU_Usage.h"

Serial pc(USBTX, USBRX);

Timer t;
CPU_Usage cpu(t, 1);

// the code that you specifically want to measure
void function() {
    cpu.working();	// Tell the object work has started
    //
    // your awesome code here...
    //
    cpu.stopped();	// tell the object work has finished
}



int main() {

   uint8_t value = 0;

   while(1) {

       //
       // Do all your cool stuff,
       // calling function() when ever you want
       //

       value = cpu.update();           // retrieves the usage value

       pc.printf("CPU %i", value);   // send the value over serial.. if you want.

       }
}

CPU_Usage.cpp

Committer:
dextorslabs
Date:
2016-01-08
Revision:
2:807947fd7dde
Parent:
1:1a339f7c3de3

File content as of revision 2:807947fd7dde:

/******************************************************
    CPU_Usage Library V1.0   Ian Weston  15.06.2014
    CPU_Usage Library V1.01  Ian Weston  08.01.2016
    
A very lightweight tool for calculating the CPU usage
of the mbed micro. Based in the time domain, this will
calculate the percentage of CPU time used.

Please see the project page for more details.

Enjoy!

******************************************************/
#include "mbed.h"
#include "CPU_Usage.h"


// Constructor - time_ballast can be considered as a smoothing
// number, representing time to reset all data. 1 seconds is
// a good place to start.
CPU_Usage::CPU_Usage(Timer &t, float time_ballast) {
    _t = &t;
    _active = 0;
    _inactive = 0;
    _time_ballast = time_ballast;
    }
    
// Use this function when isolating precise areas of code for measurement
// place it at the start of the code of interest    
void CPU_Usage::working(void) {
    _t->stop();
    _inactive += _t->read();
    _t->reset();
    _t->start();
    }
    
// Use this function when isolating precise areas of code for measurement
// place this at the end of the code thats of interest
void CPU_Usage::stopped(void) {
    _t->stop();
    _active += _t->read();
    _t->reset();
    _t->start();
    }
    
    
// call this to return the usage value. returns the value as a percentage
uint8_t CPU_Usage::update(void) {
    float total = _active + _inactive;
    float percentile = 100 / total;
    uint8_t usage = _active * percentile;
    
    if (total > _time_ballast) {
        _active = 0;
        _inactive = 0;
        }
    
    return usage;
    }    
    

// use this function instead of wait() to exclude CPU wait processing from the value.
void CPU_Usage::delay(float duration) {
    this->stopped();
    wait(duration);
    this->working();
    }

// Destructor... empty... as you can see...
CPU_Usage::~CPU_Usage(void) {
    
    }