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

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

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

Postby Gfast2 » Mon Nov 06, 2017 2:34 pm

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

User avatar
urbanze
Posts: 295
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

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

Postby urbanze » Mon Nov 06, 2017 3:57 pm

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.

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

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

Postby Gfast2 » Mon Nov 06, 2017 7:22 pm

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

User avatar
urbanze
Posts: 295
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

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

Postby urbanze » Wed Nov 08, 2017 11:22 am

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).

f.h-f.s.
Posts: 214
Joined: Thu Dec 08, 2016 2:53 pm

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

Postby f.h-f.s. » Wed Nov 08, 2017 11:49 am

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.

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

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

Postby Gfast2 » Thu Nov 09, 2017 8:10 am

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

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

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

Postby Gfast2 » Thu Nov 09, 2017 8:12 am

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

User avatar
urbanze
Posts: 295
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

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

Postby urbanze » Thu Nov 09, 2017 11:00 am

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

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

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

Postby Gfast2 » Thu Nov 09, 2017 7:10 pm

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

mikemoy
Posts: 599
Joined: Fri Jan 12, 2018 9:10 pm

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

Postby mikemoy » Sat Feb 17, 2018 5:51 am

What you want is built into the RTOS, but sadly ESP has not added it in.
void vTaskGetRunTimeStats( char *pcWriteBuffer );

Who is online

Users browsing this forum: No registered users and 142 guests