Page 1 of 1

On which core my code is running?

Posted: Sat Jun 13, 2020 8:35 am
by MoustachedBird
Hello everyone, i'm new using ESP-IDF (FreeRTOS) and I want to know if it is possible to identify on which core my code is running?

If I understand correctly FreeRTOS uses the two cores automatically, it knows which one is free and which is not, so it runs a segment of a task (or a complete it depends how long the task is) on the available core.

I see that some users identify the core selecting it previously (something like the following code), so now I'm not sure if FreeRTOS selects the core automatically or if I have to select it manually.

Code: Select all

/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"

#define core_0 0
#define core_1 1

void hello_task_core_1(void *pvParameter)
{
	while(1){
	printf("Hello world from core %d!\n", xPortGetCoreID() );
    vTaskDelay(872 / portTICK_PERIOD_MS);
    fflush(stdout);
	} 
}

void hello_task_core_0(void *pvParameter)
{
	while(1) {
	printf("Hello world from core %d!\n", xPortGetCoreID() );
    vTaskDelay(1323 / portTICK_PERIOD_MS);
    fflush(stdout);
	}
}

void app_main()
{
    nvs_flash_init();
	xTaskCreatePinnedToCore(&hello_task_core_0, "core1_task", 1024*4, NULL, configMAX_PRIORITIES - 1, NULL, core_0);
	xTaskCreatePinnedToCore(&hello_task_core_1, "core0_task", 1024*4, NULL, configMAX_PRIORITIES - 1, NULL, core_1);
}
I mean, it's possible to do something like this?

Code: Select all

#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_system.h"
#include "nvs_flash.h"

void task1(void *pvParameter){
    while(1) {
        printf("Running task 1\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void task2(void *pvParameter) {
    while(1) {
        printf("Running task 2\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void task3(void *pvParameter) {
    while(1) {
        printf("Running task 3\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void task4(void *pvParameter) {
    while(1) {
        printf("Running task 4\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void app_main() {
     nvs_flash_init();
     xTaskCreate(&task1, "task1", 1024, NULL, 1, NULL);
     xTaskCreate(&task2, "task2", 1024, NULL, 2, NULL);
     xTaskCreate(&task3, "task3", 1024, NULL, 3, NULL);
     xTaskCreate(&task4, "task4", 1024, NULL, 4, NULL);
     
     //vTaskStartScheduler();
}


Re: On which core my code is running?

Posted: Sat Jun 13, 2020 10:24 am
by Sprite
Both are valid and have their use. Pinning to a certain core will give you more control over what tasks get executed when, which can be useful in situations where e.g. you want to have low latency. Letting tasks roam free can give you more overall CPU power, as there's a larger chance the CPUs will be actively working on something.

Re: On which core my code is running?

Posted: Mon Dec 01, 2025 6:06 pm
by Bryght-Richard
This was helpful, and I have a little more to add rather than creating a new post.

Tasks can be pinned at creation, or at other times. One notable event: any usage of FPU registers will pin the thread to the current CPU core(SIMD-unit might also do this, if present)

Because pinning can be adjusted unintentionally, it's sometimes helpful to check. To check the pinning of a thread, it depends on the FreeRTOS version, which depends on ESP-IDF version, but seems to be one of these three options:

Code: Select all

xTaskGetAffinity()//returns affinity-mask, deprecated
xTaskGetCoreID()//returns core-id xor tskNO_AFFINITY 
vTaskCoreAffinityGet()//takes task, return affinity-mask

Re: On which core my code is running?

Posted: Tue Dec 02, 2025 4:14 am
by nopnop2002
You are currently in the elevator lobby of a hotel with two elevators.

When there are two elevators, you normally get on the one that arrives first.

Starting a task by specifying a core is the same as always waiting for only one of the elevators.

You are waiting for the left elevator to arrive.
When the right elevator arrives, you don't get on it.
You keep waiting until the left elevator arrives.

Re: On which core my code is running?

Posted: Tue Dec 02, 2025 8:26 am
by MicroController
- "Sir, you cannot use this elevator, you have to wait for the other one."
- "But why? I did use it yesterday just fine, and it's vacant now."
- "Since then you used the other elevator too, and pushed its FPU button. Now that's your only elevator forever. Have a nice day."