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.

       }
}
Committer:
dextorslabs
Date:
Sun Jun 15 20:32:26 2014 +0000
Revision:
0:d1e470a5f56e
Child:
1:1a339f7c3de3
CPU_Usage Library v1.0 - First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dextorslabs 0:d1e470a5f56e 1 /******************************************************
dextorslabs 0:d1e470a5f56e 2 CPU_Usage Library V1.0 Ian Weston 15.06.2014
dextorslabs 0:d1e470a5f56e 3
dextorslabs 0:d1e470a5f56e 4 A very lightweight tool for calculating the CPU usage
dextorslabs 0:d1e470a5f56e 5 of the mbed micro. Based in the time domain, this will
dextorslabs 0:d1e470a5f56e 6 calculate the percentage of CPU time used.
dextorslabs 0:d1e470a5f56e 7
dextorslabs 0:d1e470a5f56e 8 Please see the project page for more details.
dextorslabs 0:d1e470a5f56e 9
dextorslabs 0:d1e470a5f56e 10 Enjoy!
dextorslabs 0:d1e470a5f56e 11
dextorslabs 0:d1e470a5f56e 12 ******************************************************/
dextorslabs 0:d1e470a5f56e 13 #include "mbed.h"
dextorslabs 0:d1e470a5f56e 14 #include "CPU_Usage.h"
dextorslabs 0:d1e470a5f56e 15
dextorslabs 0:d1e470a5f56e 16
dextorslabs 0:d1e470a5f56e 17 // Constructor - time_ballast can be considered as a smoothing
dextorslabs 0:d1e470a5f56e 18 // number, representing time to reset all data. 1 seconds is
dextorslabs 0:d1e470a5f56e 19 // a good place to start.
dextorslabs 0:d1e470a5f56e 20 CPU_Usage::CPU_Usage(Timer &t, float time_ballast) {
dextorslabs 0:d1e470a5f56e 21 _t = &t;
dextorslabs 0:d1e470a5f56e 22 _active = 0;
dextorslabs 0:d1e470a5f56e 23 _inactive = 0;
dextorslabs 0:d1e470a5f56e 24 _time_ballast = time_ballast;
dextorslabs 0:d1e470a5f56e 25 }
dextorslabs 0:d1e470a5f56e 26
dextorslabs 0:d1e470a5f56e 27 // Use this function when isolating precise areas of code for measurement
dextorslabs 0:d1e470a5f56e 28 // place it at the start of the code of interest
dextorslabs 0:d1e470a5f56e 29 void CPU_Usage::working(void) {
dextorslabs 0:d1e470a5f56e 30 _t->stop();
dextorslabs 0:d1e470a5f56e 31 _inactive += _t->read();
dextorslabs 0:d1e470a5f56e 32 _t->reset();
dextorslabs 0:d1e470a5f56e 33 _t->start();
dextorslabs 0:d1e470a5f56e 34 }
dextorslabs 0:d1e470a5f56e 35
dextorslabs 0:d1e470a5f56e 36 // Use this function when isolating precise areas of code for measurement
dextorslabs 0:d1e470a5f56e 37 // place this at the end of the code thats of interest
dextorslabs 0:d1e470a5f56e 38 void CPU_Usage::stopped(void) {
dextorslabs 0:d1e470a5f56e 39 _t->stop();
dextorslabs 0:d1e470a5f56e 40 _active += _t->read();
dextorslabs 0:d1e470a5f56e 41 _t->reset();
dextorslabs 0:d1e470a5f56e 42 _t->start();
dextorslabs 0:d1e470a5f56e 43 }
dextorslabs 0:d1e470a5f56e 44
dextorslabs 0:d1e470a5f56e 45
dextorslabs 0:d1e470a5f56e 46 // call this to return the usage value. returns the value as a percentage
dextorslabs 0:d1e470a5f56e 47 uint8_t CPU_Usage::update(void) {
dextorslabs 0:d1e470a5f56e 48 float total = _active + _inactive;
dextorslabs 0:d1e470a5f56e 49 float percentile = 100 / total;
dextorslabs 0:d1e470a5f56e 50 uint8_t usage = _active * percentile;
dextorslabs 0:d1e470a5f56e 51
dextorslabs 0:d1e470a5f56e 52 if (total > _time_ballast) {
dextorslabs 0:d1e470a5f56e 53 _active = 0;
dextorslabs 0:d1e470a5f56e 54 _inactive = 0;
dextorslabs 0:d1e470a5f56e 55 }
dextorslabs 0:d1e470a5f56e 56
dextorslabs 0:d1e470a5f56e 57 return usage;
dextorslabs 0:d1e470a5f56e 58 }
dextorslabs 0:d1e470a5f56e 59
dextorslabs 0:d1e470a5f56e 60
dextorslabs 0:d1e470a5f56e 61 // use this function instead of wait() to exclude CPU wait processing from the value.
dextorslabs 0:d1e470a5f56e 62 void CPU_Usage::delay(float duration) {
dextorslabs 0:d1e470a5f56e 63 this->stopped();
dextorslabs 0:d1e470a5f56e 64 wait(0.25);
dextorslabs 0:d1e470a5f56e 65 this->working();
dextorslabs 0:d1e470a5f56e 66 }
dextorslabs 0:d1e470a5f56e 67
dextorslabs 0:d1e470a5f56e 68 // Destructor... empty... as you can see...
dextorslabs 0:d1e470a5f56e 69 CPU_Usage::~CPU_Usage(void) {
dextorslabs 0:d1e470a5f56e 70
dextorslabs 0:d1e470a5f56e 71 }
dextorslabs 0:d1e470a5f56e 72
dextorslabs 0:d1e470a5f56e 73
dextorslabs 0:d1e470a5f56e 74
dextorslabs 0:d1e470a5f56e 75
dextorslabs 0:d1e470a5f56e 76