How to get "CPU load" and "Memory Usage"

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: How to get "CPU load" and "Memory Usage"

Postby snahmad75 » Thu Nov 01, 2018 10:04 am

I am not getting good CPU thread/task utlization in percentage.

static char __stats_buffer[1024];
vTaskGetRunTimeStats(__stats_buffer);
printf("%s\n", __stats_buffer);
vTaskList(__stats_buffer);
printf( "%s\n", __stats_buffer);


Any thing wrong here.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: How to get "CPU load" and "Memory Usage"

Postby fly135 » Thu Nov 01, 2018 5:03 pm

snahmad75 wrote:Any thing wrong here.
Your interpretation of what you see, perhaps?

I use that routine. It seems to provide useful info as long as the uptime is not too long.

John A

username
Posts: 534
Joined: Thu May 03, 2018 1:18 pm

Re: How to get "CPU load" and "Memory Usage"

Postby username » Thu Nov 01, 2018 7:36 pm

Just FYI....
NOTE 1: This function will disable interrupts for its duration. It is not intended for normal application runtime use but as a debug aid.
http://esp32.info/docs/esp_idf/html/d3/ ... Stats.html

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: How to get "CPU load" and "Memory Usage"

Postby snahmad75 » Thu Nov 01, 2018 8:35 pm

ok, I see . Thanks for information. yes it was slowly down my application processing. good to know.
I am planning to use visual debugger with visual studio. I hope that do not disable interrupt during get CPU usage.

Is get remaining internal/external ram esp32 function also do same disable interrupt?

axellin
Posts: 200
Joined: Mon Sep 17, 2018 9:09 am

Re: How to get "CPU load" and "Memory Usage"

Postby axellin » Fri Nov 02, 2018 2:02 pm

vTaskGetRunTimeStats output looks wrong (after running for 6 hours):
e.g.
IDLE0 3975459304 241%
IDLE1 819548835 49%
tiT 95644729 5%
Tmr Svc 49 <1%
....

How can IDLE0 reach 241%?

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: How to get "CPU load" and "Memory Usage"

Postby fly135 » Fri Nov 02, 2018 2:33 pm

axellin wrote:vTaskGetRunTimeStats output looks wrong (after running for 6 hours):
e.g.
IDLE0 3975459304 241%
IDLE1 819548835 49%
tiT 95644729 5%
Tmr Svc 49 <1%
....

How can IDLE0 reach 241%?
It seems to provide useful info as long as the uptime is not too long.
I suggest you call either "esp_timer_get_time" or "xTaskGetTickCount()" to get the total system uptime and compare against the runtime numbers for each task. You will get a different result. Not sure why the call to vTaskGetRunTimeStats gets it wrong after running for a while. The source is in the IDF but I never spent the time to investigate.

Also... eventually getting the system uptime will give incorrect results as well. Bottom line... it appears that counters roll over and the times no longer do the trick. I imagine that it could be fixable with a little work. Or you could simply do your profiling closer to the boot time.

John A

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: How to get "CPU load" and "Memory Usage"

Postby snahmad75 » Wed Dec 05, 2018 7:58 pm

Hi,

I tried vTaskList. It does not shows cpu usage in percentage.


printf( "Task Name\tStatus\tPrio\tHWM\tTask\tAffinity\n");
char stats_buffer[1024];
vTaskList(stats_buffer);
printf("%s\n", stats_buffer);

Thanks,
Naeem

DrMickeyLauer
Posts: 168
Joined: Sun May 22, 2022 2:42 pm

Re: How to get "CPU load" and "Memory Usage"

Postby DrMickeyLauer » Sun Apr 30, 2023 1:53 pm

A cumulated view since reboot is just not cutting it. This is a big oversight. Please support me @ https://github.com/espressif/esp-idf/issues/11302

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: How to get "CPU load" and "Memory Usage"

Postby Craige Hales » Sun Apr 30, 2023 3:09 pm

I've been using this to get a smoothed idle count for each CPU. The reported number is typically around 2,000 - 20,000 or higher when everything is good. Under 1000 means there is not much idle time left.

Code: Select all

// Register a callback to be called from the specified core’s idle hook. 
// The callback should return true if it should be called by the idle hook 
// once per interrupt (or FreeRTOS tick), and return false if it should be 
// called repeatedly as fast as possible by the idle hook. 

#define idleUsageTask_stack 1024 // reports.c
#define idleUsageTask_core OTHERCORE
#define idleUsageTask_wait_ms 100
#define idleUsageTask_priority 3 /*configMAX_PRIORITIES is 5*/


static int s_idle0;
static int s_idle0report;
static bool idle0() { s_idle0 += 1; return false;}
static int s_idle1;
static int s_idle1report;
static bool idle1() { s_idle1 += 1; return false;}
int report_get_idle0() { return s_idle0report; }
int report_get_idle1() { return s_idle1report; }
static void idleUsageTask(void *arg)
{
    while(1)
    {
        s_idle0report = s_idle0;
        s_idle0 /= 2;
        s_idle1report = s_idle1;
        s_idle1 /= 2;
        vTaskDelay(idleUsageTask_wait_ms/portTICK_PERIOD_MS);
    }
} 
void report_init_cpu_usage()
{
    esp_register_freertos_idle_hook_for_cpu(idle0, 0/*cpuid*/);
    esp_register_freertos_idle_hook_for_cpu(idle1, 1/*cpuid*/);
    xTaskCreatePinnedToCore(idleUsageTask, "idleUsageTask", idleUsageTask_stack, NULL/*arg*/, idleUsageTask_priority, NULL/*handle*/, idleUsageTask_core);
}
Call the two report_get_idleN() functions from somewhere...
This is for ESP-IDF; probably something similar could work on the Arduino IDE.
Craige

Who is online

Users browsing this forum: Baidu [Spider] and 124 guests