Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve multiple accessing pxCurrentTCB in a function #1065

Open
wants to merge 33 commits into
base: main
Choose a base branch
from

Conversation

chinglee-iot
Copy link
Member

@chinglee-iot chinglee-iot commented May 20, 2024

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:

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.

Copy link

sonarcloud bot commented May 21, 2024

Quality Gate Failed Quality Gate failed

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

See analysis details on SonarCloud

@chinglee-iot chinglee-iot marked this pull request as ready for review July 9, 2024 01:46
@chinglee-iot chinglee-iot requested a review from a team as a code owner July 9, 2024 01:46
Copy link

sonarcloud bot commented Sep 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants