Updated Jun 2025
uxTaskGetSystemState()
task.h
1UBaseType_t uxTaskGetSystemState(2 TaskStatus_t * const pxTaskStatusArray,3 const UBaseType_t uxArraySize,4 unsigned long * const pulTotalRunTime );
configUSE_TRACE_FACILITY
uxTaskGetSystemState()
uxTaskGetSystemState()
TaskStatus_t
See vTaskGetInfo() for a version that populates a
TaskStatus_t
NOTE: This function is intended for debugging use only as its use results in the scheduler remaining suspended for an extended period.
Parameters:
-
pxTaskStatusArray
A pointer to an array of TaskStatus_t structures. The array must contain at least one
structure for each task that is under the control of the RTOS. The number of tasks under the control of the RTOS can be determined using the uxTaskGetNumberOfTasks() API function.TaskStatus_t -
uxArraySize
The size of the array pointed to by the
parameter. The size is specified as the number of indexes in the array (the number of TaskStatus_t structures contained in the array), not by the number of bytes in the array.pxTaskStatusArray -
pulTotalRunTime
If
is set to 1 in FreeRTOSConfig.h thenconfigGENERATE_RUN_TIME_STATSis set by*pulTotalRunTimeto the total run time (as defined by the run time stats clock) since the target booted.uxTaskGetSystemState()can be set to NULL to omit the total run time value.pulTotalRunTime
Returns:
- The number of TaskStatus_t structures that were populated
by . This should equal the number returned by theuxTaskGetSystemState()API function, but will be zero if the value passed in theuxTaskGetNumberOfTasks()parameter was too small.uxArraySize
Example usage:
1/* This example demonstrates how a human readable table of run time stats2 information is generated from raw data provided by uxTaskGetSystemState().3 The human readable table is written to pcWriteBuffer. (see the vTaskList()4 API function which actually does just this). */5void vTaskGetRunTimeStats( char *pcWriteBuffer )6{7 TaskStatus_t *pxTaskStatusArray;8 volatile UBaseType_t uxArraySize, x;9 unsigned long ulTotalRunTime, ulStatsAsPercentage;1011 /* Make sure the write buffer does not contain a string. */12 *pcWriteBuffer = 0x00;1314 /* Take a snapshot of the number of tasks in case it changes while this15 function is executing. */16 uxArraySize = uxTaskGetNumberOfTasks();1718 /* Allocate a TaskStatus_t structure for each task. An array could be19 allocated statically at compile time. */20 pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );2122 if( pxTaskStatusArray != NULL )23 {24 /* Generate raw status information about each task. */25 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray,26 uxArraySize,27 &ulTotalRunTime );2829 /* For percentage calculations. */30 ulTotalRunTime /= 100UL;3132 /* Avoid divide by zero errors. */33 if( ulTotalRunTime > 0 )34 {35 /* For each populated position in the pxTaskStatusArray array,36 format the raw data as human readable ASCII data. */37 for( x = 0; x < uxArraySize; x++ )38 {39 /* What percentage of the total run time has the task used?40 This will always be rounded down to the nearest integer.41 ulTotalRunTimeDiv100 has already been divided by 100. */42 ulStatsAsPercentage =43 pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalRunTime;4445 if( ulStatsAsPercentage > 0UL )46 {47 sprintf( pcWriteBuffer, "%stt%lutt%lu%%rn",48 pxTaskStatusArray[ x ].pcTaskName,49 pxTaskStatusArray[ x ].ulRunTimeCounter,50 ulStatsAsPercentage );51 }52 else53 {54 /* If the percentage is zero here then the task has55 consumed less than 1% of the total run time. */56 sprintf( pcWriteBuffer, "%stt%lutt<1%%rn",57 pxTaskStatusArray[ x ].pcTaskName,58 pxTaskStatusArray[ x ].ulRunTimeCounter );59 }6061 pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );62 }63 }6465 /* The array is no longer needed, free the memory it consumes. */66 vPortFree( pxTaskStatusArray );67 }68}
The TaskStatus_t definition
1typedef struct xTASK_STATUS2{3 /* The handle of the task to which the rest of the information in the4 structure relates. */5 TaskHandle_t xHandle;67 /* A pointer to the task's name. This value will be invalid if the task was8 deleted since the structure was populated! */9 const char *pcTaskName;1011 /* A number unique to the task. */12 UBaseType_t xTaskNumber;1314 /* The state in which the task existed when the structure was populated. */15 eTaskState eCurrentState;1617 /* The priority at which the task was running (may be inherited) when the18 structure was populated. */19 UBaseType_t uxCurrentPriority;2021 /* The priority to which the task will return if the task's current priority22 has been inherited to avoid unbounded priority inversion when obtaining a23 mutex. Only valid if configUSE_MUTEXES is defined as 1 in24 FreeRTOSConfig.h. */25 UBaseType_t uxBasePriority;2627 /* The total run time allocated to the task so far, as defined by the run time stats clock.28 Only valid when configGENERATE_RUN_TIME_STATS is defined as 1 in FreeRTOSConfig.h. */29 unsigned long ulRunTimeCounter;3031 /* Points to the lowest address of the task's stack area. */32 StackType_t *pxStackBase;3334 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )35 /* Points to the top address of the task's stack area. */36 StackType_t * pxTopOfStack;3738 /* Points to the bottom address of the task's stack area. */39 StackType_t * pxEndOfStack;40 #endif4142 /* The minimum amount of stack space that has remained for the task since43 the task was created. The closer this value is to zero the closer the task44 has come to overflowing its stack. */45 configSTACK_DEPTH_TYPE usStackHighWaterMark;46} TaskStatus_t;