Skip to content

Commit 6e6e934

Browse files
committed
Include current task runtime in ulTaskGetRunTimeCounter
Update ulTaskGetRunTimeCounter to include elapsed time since the last context switch when called for the currently running task. Previously, this time was not included in the calculation. Fixes FreeRTOS#1202. Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent ad4e723 commit 6e6e934

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

tasks.c

+39-6
Original file line numberDiff line numberDiff line change
@@ -8411,15 +8411,36 @@ TickType_t uxTaskResetEventItemValue( void )
84118411
configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
84128412
{
84138413
TCB_t * pxTCB;
8414+
configRUN_TIME_COUNTER_TYPE ulCurrentRunTimeCounter = 0, ulTimeSinceLastSwitchedIn = 0, ulTotalRunTime = 0;
84148415

84158416
traceENTER_ulTaskGetRunTimeCounter( xTask );
84168417

84178418
pxTCB = prvGetTCBFromHandle( xTask );
84188419
configASSERT( pxTCB != NULL );
84198420

8420-
traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8421+
taskENTER_CRITICAL();
8422+
{
8423+
if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
8424+
{
8425+
#ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
8426+
portALT_GET_RUN_TIME_COUNTER_VALUE( ulCurrentRunTimeCounter );
8427+
#else
8428+
ulCurrentRunTimeCounter = portGET_RUN_TIME_COUNTER_VALUE();
8429+
#endif
84218430

8422-
return pxTCB->ulRunTimeCounter;
8431+
#if( configNUMBER_OF_CORES == 1 )
8432+
ulTimeSinceLastSwitchedIn = ulCurrentRunTimeCounter - ulTaskSwitchedInTime[ 0 ];
8433+
#else
8434+
ulTimeSinceLastSwitchedIn = ulCurrentRunTimeCounter - ulTaskSwitchedInTime[ pxTCB->xTaskRunState ];
8435+
#endif
8436+
}
8437+
ulTotalRunTime = pxTCB->ulRunTimeCounter + ulTimeSinceLastSwitchedIn;
8438+
}
8439+
taskEXIT_CRITICAL();
8440+
8441+
traceRETURN_ulTaskGetRunTimeCounter( ulTotalRunTime );
8442+
8443+
return ulTotalRunTime;
84238444
}
84248445

84258446
#endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
@@ -8430,11 +8451,17 @@ TickType_t uxTaskResetEventItemValue( void )
84308451
configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
84318452
{
84328453
TCB_t * pxTCB;
8433-
configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8454+
configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn, ulTaskRunTimeCounter;
84348455

84358456
traceENTER_ulTaskGetRunTimePercent( xTask );
84368457

8437-
ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8458+
ulTaskRunTimeCounter = ulTaskGetRunTimeCounter( xTask );
8459+
8460+
#ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
8461+
portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime );
8462+
#else
8463+
ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8464+
#endif
84388465

84398466
/* For percentage calculations. */
84408467
ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
@@ -8445,7 +8472,7 @@ TickType_t uxTaskResetEventItemValue( void )
84458472
pxTCB = prvGetTCBFromHandle( xTask );
84468473
configASSERT( pxTCB != NULL );
84478474

8448-
ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8475+
ulReturn = ulTaskRunTimeCounter/ ulTotalTime;
84498476
}
84508477
else
84518478
{
@@ -8492,7 +8519,13 @@ TickType_t uxTaskResetEventItemValue( void )
84928519

84938520
traceENTER_ulTaskGetIdleRunTimePercent();
84948521

8495-
ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8522+
#ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
8523+
portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime );
8524+
#else
8525+
ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8526+
#endif
8527+
8528+
ulTotalTime = ulTotalTime * configNUMBER_OF_CORES;
84968529

84978530
/* For percentage calculations. */
84988531
ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;

0 commit comments

Comments
 (0)