ESP32 Task Starvation / Starve / Preemption / Preempt

ESP32 Developer – Priority
Arduino ESP32 FreeRTOS 2: How to use Task Parameter – Task Priorities – Task Handle – Idle Task Hook

There are 3 types of scheduling: Co-operative Scheduling and Prioritized Pre-emptive Scheduling with time slicing and without time slicing.

  • Prioritized Pre-emptive Scheduling with time slicing:
    task with higher priority will pre-empt the tasks that have lower priority. And tasks that have the same priority will run in turn every time tick interrupt occurs (RTOS has a timer interrupt to measure the time, every time the interrupt occurs RTOS will check that it is time to unblock or wake a task).
  • Prioritized Pre-emptive Scheduling without time slicing:
    task with higher priority will pre-empt the tasks that have lower priority. And tasks that have the same priority will not run in turn every time tick interrupt occurs (the task that is running, will continue running until it is pre-empted by high priority task).
  • Co-operative Scheduling:
    context switch occur when running task change from Running state to Blocked state or call taskYIELD(). The next running task is the task that has highest priority and is in Running state.

Watchdogs, Interrupt Watchdog Timer (IWDT), Task Watchdog Timer (TWDT)
Clarifications on freeRTOS
ESP-IDF, multicore & freeRTOS confusion

> Does freeRTOS uses a preemptive scheduler on ESP-idf?
Yes

> 1. Given
> core 0 runs a priority 10 task,
> core 1 runs a priority 12 task
> and a priority 11 task is awoken.
– If the priority 11 task is pinned to core 0 or has no core affinity, the priority 11 task will preempt on core 0.
– If the priority 11 task is pinned to core 1, core 1 will not preempt.

> 3. A high priority task that never calls vTaskDelay will eat
> all the resources and never let lower priorities run, right?
If the high priority task is pinned to a core, then it will starve the lower priority tasks on that core from cpu time. However if the high priority task as no core affinity, it can be possible that the task will bounce between the two cores, giving the lower priority task on each core a chance to run. However, writing a task function that never blocks is very poor application design. We guard against cpu starvation using the task watchdog timer.

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5828
entry 0x400806ac

Serial done!
RS485 done!

99 GT2
E (11904) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (11904) task_wdt:  - IDLE0 (CPU 0)
E (11904) task_wdt: Tasks currently running:
E (11904) task_wdt: CPU 0: taskTempCurrent
E (11904) task_wdt: CPU 1: IDLE1
E (11904) task_wdt: Aborting.
abort() was called at PC 0x400e63cb on core 0

Backtrace:
0x4008ba20:0x3ffbe170 0x4008bc51:0x3ffbe190 0x400e63cb:0x3ffbe1b0
0x40084775:0x3ffbe1d0 0x4000bfed:0x3ffb45d0 0x4008952d:0x3ffb45e0
0x400885af:0x3ffb4600 0x400e457f:0x3ffb4620 0x400e33a1:0x3ffb4640
0x400d17bd:0x3ffb4660 0x400d18a8:0x3ffb4680 0x400d1a12:0x3ffb46c0
0x40088661:0x3ffb46f0

Rebooting...

Leave a Reply

Your email address will not be published. Required fields are marked *