Running FreeRTOS only on first core ...?

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

Running FreeRTOS only on first core ...?

Postby kolban » Sun Dec 25, 2016 8:35 pm

In the "make menuconfig" settings for:

Component config > FreeRTOS

There is an entry called "Run FreeRTOS only on first core".

Does anyone know what that "means"?

What are the pros and cons on running on both cores?
What is "meant" by the "first core"? Does that mean "first available" or are the cores identified (0 and 1 or PRO and APP)?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Running FreeRTOS only on first core ...?

Postby WiFive » Sun Dec 25, 2016 11:25 pm

Core = CPU
First core = cpu0 = "pro cpu"

Pros: 2 CPUs to do the work
Cons: some memory overhead, some additional logic for interrupts and flash ops, power consumption?, freertos SMP relatively untested?

Some experts may chime in with more...

Another question is what are the performance considerations for task pinning vs no affinity?

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

Re: Running FreeRTOS only on first core ...?

Postby ESP_Sprite » Mon Dec 26, 2016 1:51 am

Disabling FreeRTOS on the 2nd core should in theory allow you to run something entirely different on that core, which can be useful if e.g. you want to have cycle-accurate timings for something. I haven't seen it being used in practice yet, though. However, something that is useful is that if you also disable 'Reserve memory for 2nd code' in the esp32 component, you will instantaneously gain 32K of memory because the flash cache for the 2nd core is not initialized.

As far as I'm aware, there's no intrinsic performance gain or loss in not pinning a task. There may be performance gain because the task can get picked up earlier by a cpu it wouldn't otherwise run on, though.

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

Re: Running FreeRTOS only on first core ...?

Postby kolban » Mon Dec 26, 2016 3:04 am

In the ESP8266 we had to be careful to relinquish control back to the WiFi subsystem otherwise it could get "confused" and a watch dog timer would restart the system. I had assumed that the dual core technology was to circumvent those restrictions. In a default environment, what would happen if I coded:

Code: Select all

while(1) {
  // do nothing?
}
In my naivety, I had assumed that PRO CPU was being used to host the WiFi subsystem while APP CPU is where my application (eg. the one above) would run ... and we wouldn't be in any danger of starving WiFi. However, if just using one CPU is default ... do we have any guidance with relation to WiFi starving? Is that even a concept on the ESP32?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

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

Re: Running FreeRTOS only on first core ...?

Postby ESP_igrr » Mon Dec 26, 2016 3:17 am

WiFi stack uses a bunch of tasks, each one with different priority.
When you write a busy loop like this one, and run it inside a task in a single core environment, you effectively prevent all lower priority tasks from running. If the priority of your task happens to be pretty high, this may cause issues for WiFi stack. Otherwise, RTOS will preempt your task and run higher priority wifi tasks when necessary.

In dual core mode, if your pin your task to the APP CPU, there won't be any problems for wifi stack, because all WiFi stack tasks are supposed to run only on PRO CPU. In this case, your task can hog all the CPU time it needs.


Edit: by pinning a task to a CPU i mean passing specific CPU number as the last argument to xTaskCreatePinnedToCore function.
Main task (where app_main runs) is pinned to PRO CPU. So if you want to run application code on APP CPU, you can start another task in app_main with xCoreID argument of xTaskCreatePinnedToCore set to 1 (APP CPU).

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Running FreeRTOS only on first core ...?

Postby WiFive » Mon Dec 26, 2016 4:03 am

This is not current because tasks now default to no affinity?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Running FreeRTOS only on first core ...?

Postby WiFive » Mon Dec 26, 2016 4:11 am

ESP_Sprite wrote:As far as I'm aware, there's no intrinsic performance gain or loss in not pinning a task. There may be performance gain because the task can get picked up earlier by a cpu it wouldn't otherwise run on, though.
If a task switches cores does it require cache refresh because caches are independent?

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

Re: Running FreeRTOS only on first core ...?

Postby ESP_igrr » Mon Dec 26, 2016 4:45 am

WiFive wrote:If a task switches cores does it require cache refresh because caches are independent?
Yes, in general you get more cache misses if you run something on both CPUs. Also two caches are competing for one SPI interface, so there will be extra delay due to arbitration for some cache refill requests. Measuring the impact of these effects is something we plan to do, just need to line up all the necessary tools first.

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

Re: Running FreeRTOS only on first core ...?

Postby kolban » Mon Dec 26, 2016 5:32 am

Mr ESP_igrr,

Oooh you said something your last post that i'd like to clarify ...
Otherwise, RTOS will preempt your task and run higher priority wifi tasks when necessary.
Are we saying that if I restrict my work to a single core ... and if I go into an un-ending loop in my app logic, then my task will still be "preempted" ... i.e. there is some form of time slicing going on? My code doesn't have to relinquish control (explicitly or implicitly)? It will literally be "context switched" away from what it is currently doing to allow the processor to go work on a different task?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

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

Re: Running FreeRTOS only on first core ...?

Postby ESP_igrr » Mon Dec 26, 2016 5:52 am

Yes, FreeRTOS has a preemptive scheduler, you can read more about this in FreeRTOS docs here: http://www.freertos.org/implementation/a00005.html

Who is online

Users browsing this forum: No registered users and 118 guests