FreeRTOS-Kernel icon indicating copy to clipboard operation
FreeRTOS-Kernel copied to clipboard

Improve multiple accessing pxCurrentTCB in a function

Open chinglee-iot opened this issue 1 year ago • 2 comments

Description

Address Richard's feedback to update the usage of pxCurrentTCB in a function. Since pxCurrentTCB is a volatile variable, accessing it multiple times in a function can be slow if this pointer is not changed.

In this PR:

  • Use a const pointer variable to access pxCurrentTCB in a function to prevent multiple accessing a volatile variable.
  • The prvGetCurrentTCB() and prvGetCurrentTCBUnsafe() functions are provided. prvGetCurrentTCBUnsafe() can improve performance in SMP environments as it does not disable/enable interrupts. However, it should only be called in contexts where race conditions cannot occur.

Test Steps

The performance gain is evaluated by comparing main branch with this PR with a hardware cycle counter.

We use the following example to illustrate the performance gain on ulTaskGenericNotifyTake.

static void PerfTaskNotifyHigh( void * pvParameter )
{
    ( void ) pvParameter;

    ulTaskNotifyTake( pdTRUE, portMAX_DELAY );

    perfTestEndCycles = HardwareCounter_Read();
    HardwareCounter_Stop();

    vTaskDelete( NULL );
}

uint32_t PerfTest_xTaskNotifyGive_UnblockHighTask( void )
{
    TaskHandle_t notifyTaskHandle;

    xTaskCreate( PerfTaskNotifyHigh,
                 "PerfTaskNotifyHigh",
                 PERFTEST_TASK_STACK_SIZE,
                 &pTaskPerfTest,
                 PERFTEST_TASK_PRIORITY_HIGH,
                 &notifyTaskHandle );

    HardwareCounter_Start();
    perfTestStartCycles = HardwareCounter_Read();

    xTaskNotifyGive( notifyTaskHandle );

    /* The high priority task delete itself. Wait idle task to free the resource. */
    vTaskDelay( 1 );

    return( perfTestEndCycles - perfTestStartCycles );
}

In the example above, bottom half of the ulTaskGenericNotifyTake are evaluated with compile option ( -O3 ).

Cycle counts difference main branch This PR Performance gain
PerfTest_xTaskNotifyGive_UnblockHighTask 485 465 20

Checklist:

  • [x] I have tested my changes. No regression in existing tests.
  • [x] I have modified and/or added unit-tests to cover the code changes in this Pull Request. - https://github.com/FreeRTOS/FreeRTOS/pull/1235

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

chinglee-iot avatar May 20 '24 08:05 chinglee-iot

Quality Gate Failed Quality Gate failed

Failed conditions
3.3% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

sonarqubecloud[bot] avatar May 21 '24 15:05 sonarqubecloud[bot]

Hi @chinglee-iot, are there still plans to merge these optimizations?

felixvanoost avatar Jun 17 '25 02:06 felixvanoost