Page 2 of 2

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

Posted: Thu Nov 01, 2018 10:04 am
by snahmad75
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.

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

Posted: Thu Nov 01, 2018 5:03 pm
by fly135
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

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

Posted: Thu Nov 01, 2018 7:36 pm
by username
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

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

Posted: Thu Nov 01, 2018 8:35 pm
by snahmad75
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?

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

Posted: Fri Nov 02, 2018 2:02 pm
by axellin
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%?

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

Posted: Fri Nov 02, 2018 2:33 pm
by fly135
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

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

Posted: Wed Dec 05, 2018 7:58 pm
by snahmad75
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

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

Posted: Sun Apr 30, 2023 1:53 pm
by DrMickeyLauer
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

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

Posted: Sun Apr 30, 2023 3:09 pm
by Craige Hales
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.