FreeRTOS multicore behavior

kurtzweber
Posts: 64
Joined: Tue Jan 10, 2017 1:09 pm

FreeRTOS multicore behavior

Postby kurtzweber » Tue Jan 10, 2017 1:15 pm

Hi!

general question about FreeRTOS working on a multicore chip like ESP32: how tasks are scheduled on the two cores if no "pin" option was chosen when created? WIll FreeRTOS schedule the tasks based on the first available core? Or a task is always executed on a single core (the one chosen at the first execution?)

is there a way to "know" (and maybe printout) which core is executing the task?

thanks!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: FreeRTOS multicore behavior

Postby kolban » Tue Jan 10, 2017 10:52 pm

@kurtzweber,
Your question is great and I too keenly await any feedback on it. I had a grunge through the source and it "appears" that we can determine which core we are running on by using the macro:

Code: Select all

int id = xPortGetCoreID();
This will return either 0 or 1 depending on the currently executing core. I'm not sure if this is an exposed/documented API or not, so your mileage may vary.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: FreeRTOS multicore behavior

Postby ESP_Sprite » Wed Jan 11, 2017 1:23 am

Your asumption indeed is correct; not pinning the task essentially allows the scheduler of any of the two CPUs to pick it up, which in a way means that whichever CPU has computing time left will run it. Kolbans way of figuring out the CPU that the task running on is correct. Take care, however; because the CPU the task is running on can change at any moment, it's possible that after returning from this function the core already changed, making the value you got invalid. (To get around it, you can wrap the entire thing in a critical section; that disables context switches for the duration of that section.)

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: FreeRTOS multicore behavior

Postby ESP_igrr » Wed Jan 11, 2017 3:49 am

One exception to the above rule are non-pinned tasks which use FPU. In the current implementation FreeRTOS will pin them to the CPU they are running on the first time the code tries to use FPU.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: FreeRTOS multicore behavior

Postby kolban » Wed Jan 11, 2017 3:58 am

The comment on the FPU brings up a follow on question ... as I understand it ... FPU is a "Floating Point Unit" which is (I believe) and internal component of a CPU. If we are writing arbitrary C applications, how do we know or control whether the FPU is being engaged?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: FreeRTOS multicore behavior

Postby ESP_igrr » Wed Jan 11, 2017 4:09 am

Unless you are running something like an interpreter which allows arbitrary code to be executed, most of the time you can tell whether use are using floats and doubles in the application. If you are operating on float and double variables, FPU will probably be used. If you don't have anything related to floats and doubles in your task code, FPU will not be used, because all the "system" components (RTOS, TCP/IP stack, etc) don't use floating point operations. One obvious exception to this are C library formatting functions, when floating point formats are used in the format string.

kurtzweber
Posts: 64
Joined: Tue Jan 10, 2017 1:09 pm

Re: FreeRTOS multicore behavior

Postby kurtzweber » Mon Jan 16, 2017 9:14 am

guys, thanks a lot for your explanation!

Coopdis
Posts: 14
Joined: Wed Jun 20, 2018 7:59 pm

Re: FreeRTOS multicore behavior

Postby Coopdis » Mon Aug 06, 2018 1:46 pm

This thread has been very helpful.

I wonder if there has been any progress made to remove the CPU pinned on FPU usage?

My application uses the cJSON library included in the IDF. It stores and parses every numerical value as a double... there for FPU usage, right?

Seems like any process that touches a JSON is going to get pinned to a CPU... or at least any JSON which contains a number value.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: FreeRTOS multicore behavior

Postby ESP_Angus » Mon Aug 06, 2018 11:41 pm

Hi coopDis,

Yes, any task which does floating point calculations will end up pinned to a core. Working with numeric values in cJSON will probably cause this to happen.

This is still planned to be addressed in a future update, but we don't have an ETA for it yet.

BTW, since this thread was started the ESP-IDF docs have been updated with descriptions of the SMP features:
https://docs.espressif.com/projects/esp ... s-smp.html

Who is online

Users browsing this forum: No registered users and 191 guests