Page 1 of 2

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

Posted: Mon Nov 06, 2017 2:34 pm
by Gfast2
Hi ESP-IDF,

Is there some API that I can grab to get the CPU usage and Memory Usage, say, like man in the "system monitor" can got?

I'm just planning to build a web-interface for my project. In which a very "nice to have" feature is this one. For me at least. :twisted: ;)

Cheers

Gfast2

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

Posted: Mon Nov 06, 2017 3:57 pm
by urbanze
To create a "CPU LOAD" display, you can take advantage of RTOS. Adding a priority 0 task (which indicates when CPU is in IDLE, delay / etc), we can start playing. However it is just a mediocre and basic solution.

There are functions that return the value of free HEAP, such as ESP.getfreeheap () (something similar to this in the Arduino core). If the total is ~ 520kB, we can take the percentage.

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

Posted: Mon Nov 06, 2017 7:22 pm
by Gfast2
urbanze wrote:To create a "CPU LOAD" display, you can take advantage of RTOS. Adding a priority 0 task (which indicates when CPU is in IDLE, delay / etc), we can start playing. However it is just a mediocre and basic solution.

There are functions that return the value of free HEAP, such as ESP.getfreeheap () (something similar to this in the Arduino core). If the total is ~ 520kB, we can take the percentage.
Hi urbanze,

Thanks for your brilient "Idle Task" solusion for CPU load indication! I'll try it out. But if you have a example of its implementation, please do let me know, I believe it will save me a lot time to solve this problem.

About memory:
Because I'm really new in this nearly bare bone level, I did some google about this topic: stack vs heap, but still didn't get the core idea about what you said. Is that means all I got from "free Heap" is all the free memories from the 512KB?

Cheers

Su Gao

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

Posted: Wed Nov 08, 2017 11:22 am
by urbanze
Hi Gfast.

Idle task ideia:

Code: Select all

long ctr = 0;

void setup()
{
	xTaskCreatePinnedToCore(id, "id", 4096, NULL, 0, NULL, 0);
}

void loop()
{

}

void id(void*z)
{
	while (1)
	{
		ctr++;
		delay(10);
	}
}
if the cpu is stopped, in 1 second the CTR will be 100. If not, it will be any smaller value. After 1sec, you need to restart ctr.


RAM function: ESP.getFreeHeap();

Here, with blank project (just Serial), has 300k of free heap. (total ~520kB).

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

Posted: Wed Nov 08, 2017 11:49 am
by f.h-f.s.
You can also loop though all running tasks and get the TaskStatus_t for each task. Which contains usStackHighWaterMark.
if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is defined you can also get vTaskGetRunTimeStats or ulRunTimeCounter from TaskStatus_t, although you might have to do some things to get it working.

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

Posted: Thu Nov 09, 2017 8:10 am
by Gfast2
f.h-f.s. wrote:You can also loop though all running tasks and get the TaskStatus_t for each task. Which contains usStackHighWaterMark.
if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is defined you can also get vTaskGetRunTimeStats or ulRunTimeCounter from TaskStatus_t, although you might have to do some things to get it working.
Hi, f.h-f.s.

Thanks for your answers. I'll checkout them when I got the chance later. 8-)

cheers

gfast2

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

Posted: Thu Nov 09, 2017 8:12 am
by Gfast2
urbanze wrote:Hi Gfast.

Idle task ideia:

Code: Select all

long ctr = 0;

void setup()
{
	xTaskCreatePinnedToCore(id, "id", 4096, NULL, 0, NULL, 0);
}

void loop()
{

}

void id(void*z)
{
	while (1)
	{
		ctr++;
		delay(10);
	}
}


if the cpu is stopped, in 1 second the CTR will be 100. If not, it will be any smaller value. After 1sec, you need to restart ctr.


RAM function: ESP.getFreeHeap();

Here, with blank project (just Serial), has 300k of free heap. (total ~520kB).
Hi urbanze,

I'm using ESP-IDF right now. If I got chance to check out your Arduino Flavor, I will post my results here.

cheers

gfast2

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

Posted: Thu Nov 09, 2017 11:00 am
by urbanze
Gfast2 wrote:
urbanze wrote:Hi Gfast.

Idle task ideia:

Code: Select all

long ctr = 0;

void setup()
{
	xTaskCreatePinnedToCore(id, "id", 4096, NULL, 0, NULL, 0);
}

void loop()
{

}

void id(void*z)
{
	while (1)
	{
		ctr++;
		delay(10);
	}
}


if the cpu is stopped, in 1 second the CTR will be 100. If not, it will be any smaller value. After 1sec, you need to restart ctr.


RAM function: ESP.getFreeHeap();

Here, with blank project (just Serial), has 300k of free heap. (total ~520kB).
Hi urbanze,

I'm using ESP-IDF right now. If I got chance to check out your Arduino Flavor, I will post my results here.

cheers

gfast2
I thought you used Arduino. You can use this code exactly equals. However, you just don't need to create setup/loop.

You also need to create another higher-priority task on cpu 0 (for example priority 10), so that every X seconds, it shows the value of the counter on the screen.

You can test that it works, but in an extremely simple way. If you really need this and can not solve it, we can create something together :D

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

Posted: Thu Nov 09, 2017 7:10 pm
by Gfast2
urbanze wrote:
Gfast2 wrote:
urbanze wrote:Hi Gfast.

Idle task ideia:

Code: Select all

long ctr = 0;

void setup()
{
	xTaskCreatePinnedToCore(id, "id", 4096, NULL, 0, NULL, 0);
}

void loop()
{

}

void id(void*z)
{
	while (1)
	{
		ctr++;
		delay(10);
	}
}



if the cpu is stopped, in 1 second the CTR will be 100. If not, it will be any smaller value. After 1sec, you need to restart ctr.


RAM function: ESP.getFreeHeap();

Here, with blank project (just Serial), has 300k of free heap. (total ~520kB).
Hi urbanze,

I'm using ESP-IDF right now. If I got chance to check out your Arduino Flavor, I will post my results here.

cheers

gfast2
I thought you used Arduino. You can use this code exactly equals. However, you just don't need to create setup/loop.

You also need to create another higher-priority task on cpu 0 (for example priority 10), so that every X seconds, it shows the value of the counter on the screen.

You can test that it works, but in an extremely simple way. If you really need this and can not solve it, we can create something together :D
Hi urbanze,

Thanks again for your examples again. It is such a pleasure to have so many support here! I will check this feature very soon.

Cheers

Su

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

Posted: Sat Feb 17, 2018 5:51 am
by mikemoy
What you want is built into the RTOS, but sadly ESP has not added it in.
void vTaskGetRunTimeStats( char *pcWriteBuffer );