code teensy 4.0 print cpu usaged

If you use the audio library, it provides CPU usage info.

https://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html

If you wish to build this for other code, you can measure elapsed time by reading the ARM DWT cycle counter at the beginning and end of doing some work and then compute how many cycles were taken.

An alternate approach to directly measuring is to find the place in your program where you expect it to run when no other work is happening. You can add code there to perform some work like incrementing a number and then periodically check how much it has increased. Or you can output a signal on a pin using digitalToggle(pin) and watch it with an oscilloscope or logic analyzer to estimate how much of the time it's rapidly toggling versus "stuck" high or low while other parts of your program run.
 
Teensyduino is an add-on to the Arduino software that allows you to program a Teensy microcontroller using the Arduino IDE. It is not possible for me to determine the CPU usage of a Teensyduino program without more information about the specific program you are running.

In general, the CPU usage of an Arduino program will depend on the instructions being executed and the speed at which the processor is running. If you are trying to optimize the CPU usage of a Teensyduino program, there are several things you can try:

Minimize the amount of work being done in the main loop of your program. Try to move as much processing as possible to functions that are called less frequently.

Use the Arduino millis() function to pace the execution of your program. This can help prevent your program from using too much CPU time.

Use the Arduino yield() function to allow other tasks to be run during long-running loops.

Use low-power sleep modes to reduce the CPU usage of your program when it is not actively doing work.

Use a faster processor if available. Teensy boards are available in a range of processor speeds, so you may be able to get better performance by using a faster processor.

I hope this information is helpful! Let me know if you have any other questions.
 
If you use the audio library, it provides CPU usage info.

https://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html

If you wish to build this for other code, you can measure elapsed time by reading the ARM DWT cycle counter at the beginning and end of doing some work and then compute how many cycles were taken.

An alternate approach to directly measuring is to find the place in your program where you expect it to run when no other work is happening. You can add code there to perform some work like incrementing a number and then periodically check how much it has increased. Or you can output a signal on a pin using digitalToggle(pin) and watch it with an oscilloscope or logic analyzer to estimate how much of the time it's rapidly toggling versus "stuck" high or low while other parts of your program run.

------------------------------------
It is not for audio, I have a code that reads the serial port and sends it through UART, just like UART to Serial Port...

I want to know the % of the CPU or RAM according to the amount of data that arrives to me.
 
@PaulStoffregen Percent real time is not possible as Memory is divided into RAM1/RAM2 and they are used separately.
@tvsFg you can try doing cycles count, I haven't tried it but it should work.
 
I'm using the following code but it only shows the global CPU usage, I'll try the cycles. Thanks!

Code:
// RAM FREE
#define LOG 0
#define LIN 1
#define SCALE LIN

static IntervalTimer cpu;

uint32_t Idle_Counter;
uint8_t CPU_Utilization_Info_Read_To_Compute;
uint32_t Prev_Idle_Counter;
uint32_t Idle_Counts;
uint32_t Calculate_Idle_Counts (void);

extern unsigned long _heap_start;
extern unsigned long _heap_end;
extern char *__brkval;

extern "C" char* sbrk(int incr);

int freeRam() {
  // char top;
  // return &top - reinterpret_cast<char*>(sbrk(0));
  return (char *)&_heap_end - __brkval;
}

void setup() {
  delay(1000);
  // initialize the serial communication at max speed:
  Serial.begin(250000);
  Serial.println("started");
  cpu.begin(Update_Task_Ready_Flags, 1000);
}

uint32_t Read_Idle_Counts(void) {
  return Idle_Counts;
}

uint32_t Calculate_CPU_Utilization (uint32_t temp_counts) {
  return 100 - ((100 * temp_counts) / 13600000);
}

uint32_t Calculate_Idle_Counts (void) {
  Idle_Counts = Idle_Counter - Prev_Idle_Counter;
  Prev_Idle_Counter = Idle_Counter;
  return Idle_Counts;
}

bool One_MS_Task_Ready;
bool Ten_MS_Task_Ready;
bool One_Hundred_MS_Task_Ready;
bool One_S_Task_Ready;

inline void One_MS_Task(void) {
  
  for (int i = 0; i < random(1000, 6000); i++) {
    Serial.print(".");
  } Serial.println();
  delay(100);
}

inline void Ten_MS_Task(void) {
  // uncomment line below to put in some cpu load :)
  // delay(8);
}

inline void One_Hundred_MS_Task(void) {}

inline void One_S_Task(void) {
  // Serial.print(F("One_S_Task: "));
  uint32_t idleCounts = Calculate_Idle_Counts();
  #if SCALE == (LOG)
    uint8_t percent1 = (Calculate_CPU_Utilization(idleCounts)*2)/2;
    // Serial.print(F(" percent1: ")); Serial.print(percent1);
    uint8_t percent2 = (Calculate_CPU_Utilization(idleCounts)*2)/2;
    // Serial.print(F(" percent2: ")); Serial.print(percent2);
    uint8_t percent = (percent1*percent2)/100;
    // Serial.print(F(" percent: ")); Serial.print(percent);
  #elif SCALE == (LIN)
    uint8_t percent = Calculate_CPU_Utilization(idleCounts);
    // Serial.print(F(" percent: ")); Serial.print(percent);
  #endif
  // output percent to serial monitor
  Serial.print(F("CPU usage: "));
  Serial.print(100 - percent);
  Serial.println(F("%"));
  Serial.print(F("Free RAM: "));
  Serial.println(freeRam());
}

inline void Run_Tasks(void) {
  if(One_MS_Task_Ready) {
    One_MS_Task_Ready = 0;
    One_MS_Task();
  }
  if(Ten_MS_Task_Ready) {
    Ten_MS_Task_Ready = 0;
    Ten_MS_Task();
  }
  if(One_Hundred_MS_Task_Ready) {
    One_Hundred_MS_Task_Ready = 0;
    One_Hundred_MS_Task();
  }
  if(One_S_Task_Ready) {
    One_S_Task_Ready = 0;
    One_S_Task();
  }
}

void loop() {
  Idle_Counter++; 
  Run_Tasks();
  delay(1000);
}

/* WARNING this function called from ISR */
void Update_Task_Ready_Flags(void) {
  static uint32_t counter;
  One_MS_Task_Ready = 1;
  counter++;
  if((counter%10) == 0) Ten_MS_Task_Ready = 1;
  if((counter%100) == 0) One_Hundred_MS_Task_Ready = 1;
  if(counter == 1000) {
    One_S_Task_Ready = 1;
    counter = 0;
  }
}
 
I don't think it is an AI since chatGPT does not reach you this code that Ñeñe_Kinich shared with me and he was right (Y).... You CANNOT know the % of CPU usage but you can calculate the available one and depending of the size of the code perform the calculation.

Thanks Ñeñe_Kinich!

Code:
#include <ArduinoJson.h>

DynamicJsonDocument doc(370000); // MAX: RAM2(Free)
JsonArray           myArray                 = doc.to<JsonArray>(); // volatile unsigned 

int ELEMENT_COUNT_MAX = 0; // 10/100/1000/5000/10000
int timeStart = 0;
int cycles    = 0;

extern unsigned long _heap_end;
extern char *__brkval;
extern "C" char* sbrk(int incr);

IntervalTimer bufferCanSendObj;
int           bufferCanSendSpeed = 1000;

int long      counterSendingCan  = 0;

String last_CANID_from_serial = "7E9";

void setup() {
  bufferCanSendObj.begin(sendMsgsBufferCan, bufferCanSendSpeed);  // run every 1 millisecond
  createMsgsTest();
}

void loop() {
  if      (timeStart == 0 && myArray.size()>0) timeStart = micros(), Serial.print(" | Start:");
  
  if (myArray.size() == 0 && timeStart != 0) {
    int _timeEnd = micros();
    int timeEnd = _timeEnd - timeStart;
    Serial.print("End"); // Serial.print(_timeEnd);
    // Serial.print(" | cycles: "); Serial.print(cycles);
    Serial.print(" | ");  Serial.print(timeEnd); Serial.print(" micros"); 
    // Serial.print(" | memoryUsage Obj: "); Serial.print(myArray.memoryUsage());  // 10 on AVR
    Serial.print(" | memoryUsage Doc: "); Serial.print(doc.memoryUsage());     // 16 on AVR
    Serial.print(" | memoryUsage Free: "); Serial.println(freeRam());
    delay(10);
    createMsgsTest();
  }
}

void sendMsgsBufferCan() {
  if (timeStart > 0 && myArray.size()>0) {
    // Serial.println("proccesing index 0");
    myArray.remove(0);
  }
}

void readMsgs() {
  cycles++;
  clearMsgs();
  /* 
   *  NO USAR == ERROR 
   *  for (int i = 0; i < myArray.size(); i++) {
   *    // String msg = myArray[i];
   *    myArray.erase(myArray.begin()+i); // myArray.begin()
   *  }
  */
}

void clearMsgs() {
  if (myArray.size() > 0) {
    // Metro serialMetro = Metro(250);  // Instantiate an instance
    // if (serialMetro.check() == 1) 
    myArray.remove(0), clearMsgs();
  }
  // Serial.print(".");
}

void createMsgsTest() {
  // 10/100/1000/5000/10000
  if (ELEMENT_COUNT_MAX == 0 || ELEMENT_COUNT_MAX == 10000) ELEMENT_COUNT_MAX = 10;
  else if (ELEMENT_COUNT_MAX == 10) ELEMENT_COUNT_MAX = 100;
  else if (ELEMENT_COUNT_MAX == 100) ELEMENT_COUNT_MAX = 1000;
  else if (ELEMENT_COUNT_MAX == 1000) ELEMENT_COUNT_MAX = 5000;
  else if (ELEMENT_COUNT_MAX == 5000) ELEMENT_COUNT_MAX = 10000;
  else ELEMENT_COUNT_MAX = 0;
  doc.clear();
  myArray                 = doc.to<JsonArray>();
  Serial.print("Loading... "); 
  timeStart = 0;
  cycles    = 0;
  for (int i = 0; i < ELEMENT_COUNT_MAX; i++) {
    char Byte0[15]; sprintf(Byte0, "%06X", int(i+1));
    myArray.add("7E180123456789" + String(Byte0));
  }
  Serial.print("Total Msgs: "); Serial.print(myArray.size());
  delay(3000);
}

float freeRam() {
  return (char *)&_heap_end - __brkval;
}
 
Back
Top