How can I calculate processor usage?

I am not sure what the correct term would be but, using my PC as an example, I can go to task manager and look at processor usage to determine how busy my processor is. Looking at processor usage, I can then estimate the ability of running additional programs assuming, that I have enough ram etc.

With an Atmega, how would I calculate the free time/processor usage of the chip? I do know that the calculation will take up processor time also but, that is ok for my level of ability.

Posting an example code for a starting point, how would I add code to show through Serial.print how much free time my microcontroller has?

/*
  Mega multple serial test
 
 Receives from the main serial port, sends to the others. 
 Receives from serial port 1, sends to the main serial (Serial 0).
 
 This example works only on the Arduino Mega
 
 The circuit: 
 * Any serial device attached to Serial port 1
 * Serial monitor open on Serial port 0:
 
 created 30 Dec. 2008
 by Tom Igoe
 
 This example code is in the public domain.
 
 */


void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(50000);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte); 
  }
}

A microcontroller is somewhat different to a PC.

A PC has many many tasks running concurrently, organized and managed by a scheduler.

A Microcontroller typically only has one task and no management.

A PC switches between tasks, timing the amount of time spent in each task, the amount of time spend handling interrupts, the amount of time spent doing system (kernel) tasks, and the amount of time when not doing anything.

A microcontroller is either running or it isn't.

If the microcontroller is running, then it is at 100% usage. If it's not, then it's asleep and waiting to be woken by some interrupt (or it's turned off).

Hi,

In arduino, processor usage is always 100% because if it isnt doing anything else its looping in a simple wrapper around the loop function.

If you want to measure the impact of a section of code, i have done this in the past by creating a busy loop and counting how many times the loop completes in a second with and without my code under test. I used a hardware timer to set/clear a flag to measure the second and cause the count to be sent to serial at the end of each second.

Duane B
rcarduino.blogspot.com

One thing you can do is turn on an LED at the top of loop() and turn it off when you do any useful work. The brightness of the LED will give you a sense of how much idle time you have.

Thanks for the quick answers, all of them very good!

I think I am going to try this suggestion first and then put the LED in place for my application when it is service.

If you want to measure the impact of a section of code, i have done this in the past by creating a busy loop and counting how many times the loop completes in a second with and without my code under test. I used a hardware timer to set/clear a flag to measure the second and cause the count to be sent to serial at the end of each second.

cyclegadget:
Posting an example code for a starting point, how would I add code to show through Serial.print how much free time my microcontroller has?

You could print micros() or millis() before / after operations you are interested in.

However, in the code you posted, the processor would spend nearly all of its time doing the printing.

ABout all you can really do is profile your code. What this would entail is either adding lines at various points that add 1 to a counter. You could then print out these values to see where your program is spending all its time.

For instance - in loop you could have several functions that are called based on some conditions.
you would have a global variable defined for each function.
in each function you would have a line that increments each time the function is run.
at some point you could print out those values and see what function is doing all the work.

Clearly, processor utilization is useful knowledge on a PC, but on an Arduino, why do you care? Your code is either fast enough or it's not. If not, it's time for some tuning, likely using the profiling suggestions above. Don't worry about it until you're forced to.

You could print micros() or millis() before / after operations you are interested in.

However, in the code you posted, the processor would spend nearly all of its time doing the printing.

I am using a 1284p "Bobuino" with both serial0 and serial1.

I have a code that is a little hard to explain but, basically it is running serial1 at 50000 baud with about 45 milliseconds down time for every 65 bytes sent and about 500 micros pause between each byte sent. I was sending a serial0 copy to the my computer for debugging and I found that I could speed the sketch up a lot by reducing the copies sent to the computer which means I must have been blocking with Serial0. Later, I plan to eliminate serial0 prints and have a LCD display.

I am doing everything without using delay but, I am going to need to add code for saving to a SD card and that is where my worries are going to begin. I can spread out the serial1 communication to allow time for other operations as long as they do not block for more than about 100milliseconds.

This is the part of my code that I am running over and over. At the moment, I am happy that it is working as it is so, I prefer not to share all of it but, this may show enough of what I am doing for those interested.

  /////////////////////////////////////////REQUEST MODE//////////////////////////
  if (mode == 2 && (currentmicros - previousmicros) >= interval)  //delay between transmits 400 micros
  {
    if (currentMillis - previousMillis >= callpause)
    {
    previousmicros = currentmicros; 
    //send request package 7 bytes long
    static int i = 0; 
  
    Serial1.write(request[i]);
    // byte inByte = Serial1.read();
    // Serial.println(inByte,HEX);  //send copy to computer
    i++;
    if (i == 7)
    {
      byte inByte = 0x00;
      while (inByte != 0xAE)
      {
        inByte = Serial1.read();
      }
      i = 0;
      // Serial.println("return 3");
      mode = 3;
      }
    }
  }
  ///////////////////////////////////////DATA RETURN READ////////////////////////
  if (mode == 3)
  {
    if (Serial1.available()) 
    {
      static int counter = 0;
      byte inByte = Serial1.read();
      buf[counter] = inByte;
      // Serial.println(inByte, HEX);
      counter++; 
      /////////////////////////////////READ RPM AND THROTTLE/////////////////////
      if (counter == messagelength)     // adjust to index size expected
      {
        for (int i = 0; i < messagelength; i++)
        {
          Serial.print(" ");
          Serial.print(buf[i],HEX);
        }
        Serial.println("");
        // RPM = (((buf[rpm1] * 256) + buf[rpm2]) / 2.56); 
        Battery = (buf[BATT] / 12.7);
        Throttle = ((buf[TPS] - 55)*125 / (256-55)); 
        if (Throttle < 0) 
        {
          Throttle = 0;
        }
        else if (Throttle > 100) 
        {
          Throttle = 100;
        }

               datacounter = datacounter ++;  //count how many return strings have been received
        counter = 0;        // reset index
        mode = 4;         // move to mode 4
      }
    }
  }

  ////////////////////////////////////////LOG THE DATA//////////////////////
  if (mode == 4) 
  {
    //save data
    mode = 2;
   previousMillis = currentMillis;  // to add a delay between requests
  }
}

Thank you kf2qd and wildbill for your additions to the thread!

Clearly, processor utilization is useful knowledge on a PC, but on an Arduino, why do you care? Your code is either fast enough or it's not. If not, it's time for some tuning, likely using the profiling suggestions above. Don't worry about it until you're forced to.

I understand what you are saying. At the moment the sketch works but, I am still adding necessary functions so, it may be easier to just add the code and see if it all still works.

I do like the ideas given for bench marking the free time of the micro-controller.

kf2qd:
ABout all you can really do is profile your code. What this would entail is either adding lines at various points that add 1 to a counter. You could then print out these values to see where your program is spending all its time.

For instance - in loop you could have several functions that are called based on some conditions.
you would have a global variable defined for each function.
in each function you would have a line that increments each time the function is run.
at some point you could print out those values and see what function is doing all the work.

I think I can implement that fairly easily so that I don't spend too much time looking for problems that might no be there.

Thanks to all!
Mark