This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[FAQ] How to profile CPU Usage with MCU+ SDK?

Part Number: PROCESSOR-SDK-AM64X

Tool/software:

How to measure CPU load in an application with FreeRTOS/MCU+ SDK?

  • An important metric to get an insight about the system performance is the CPU load.

    CPU load or utilisation refers to the total active time of the core spent in executing application code (including all the tasks and ISR execution time) divided by the total observation time.

    MCU+SDK provides a set of APIs for Task and related computations.

    To get CPU load prints at regular intervals:

    APIs used:

    • ClockP_getTimeUsec - Used to calculate the CPU load print interval
    • TaskP_loadResetAll - Resets the task load statistics after each print. Until a reset is encountered, the load statistics keep getting accumulated.
    • TaskP_loadGetTotalCpuLoad - Returns the total CPU load including all the tasks and ISR execution time in units of percentage with 2 decimal point precision.

    Steps to add CPU load prints in the code:

    • The below function prints the total CPU load every 5 seconds and resets the load statistics after each print.

      static void App_printCpuLoad()
      {
          static uint32_t startTime_ms = 0;
          const uint32_t currTime_ms   = ClockP_getTimeUsec()/1000;
          const uint32_t printInterval_ms = 5000;
      
          if (startTime_ms == 0)
          {
              startTime_ms = currTime_ms;
          }
          else if ( (currTime_ms - startTime_ms) > printInterval_ms )
          {
              const uint32_t cpuLoad = TaskP_loadGetTotalCpuLoad();
              DebugP_log("%6d.%3ds : CPU load = %3d.%02d %%\r\n",
                          currTime_ms/1000, currTime_ms%1000,
                          cpuLoad/100, cpuLoad%100 );
              startTime_ms = currTime_ms;
      
              TaskP_loadResetAll();
          }
          return;
      }

    • In the application code, import the Task.h and ClockP.h header files and add the above function.
    • Add an infinite loop and call the new added function inside it. The CPU load prints will appear on the Console/Terminal.

      while (true)
      {
          // Print CPU load
          ClockP_usleep(1000);
          App_printCpuLoad();
      }

    Similarly, to get the CPU load at any desired point in the code instead of continuous prints, modify the App_printCpuLoad function as below:

    static void App_printCpuLoad()
    {
        const uint32_t cpuLoad = TaskP_loadGetTotalCpuLoad();
        DebugP_log("%6d.%3ds : CPU load = %3d.%02d %%\r\n",
                    currTime_ms/1000, currTime_ms%1000,
                    cpuLoad/100, cpuLoad%100 );
    
        TaskP_loadResetAll();
    }