|
| 1 | +#include "freertos_stats.h" |
| 2 | +#include "sdkconfig.h" |
| 3 | +//#undef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 4 | +//#undef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 5 | + |
| 6 | +#if CONFIG_FREERTOS_USE_TRACE_FACILITY |
| 7 | +#include "freertos/FreeRTOS.h" |
| 8 | +#include "freertos/task.h" |
| 9 | +#include "freertos/portable.h" |
| 10 | +#endif /* CONFIG_FREERTOS_USE_TRACE_FACILITY */ |
| 11 | + |
| 12 | +void printRunningTasks(Print & printer) { |
| 13 | +#if CONFIG_FREERTOS_USE_TRACE_FACILITY |
| 14 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 15 | +#define FREERTOS_TASK_NUMBER_MAX_NUM 256 |
| 16 | + static UBaseType_t ulRunTimeCounters[FREERTOS_TASK_NUMBER_MAX_NUM]; |
| 17 | + static configRUN_TIME_COUNTER_TYPE ulLastRunTime = 0; |
| 18 | +#endif |
| 19 | + TaskStatus_t *pxTaskStatusArray = NULL; |
| 20 | + volatile UBaseType_t uxArraySize = 0, x = 0; |
| 21 | + configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0, uiCurrentRunTime = 0, uiTaskRunTime = 0; |
| 22 | + const char * taskStates[] = { |
| 23 | + "Running", |
| 24 | + "Ready", |
| 25 | + "Blocked", |
| 26 | + "Suspended", |
| 27 | + "Deleted", |
| 28 | + "Invalid" |
| 29 | + }; |
| 30 | + |
| 31 | + // Take a snapshot of the number of tasks in case it changes while this function is executing. |
| 32 | + uxArraySize = uxTaskGetNumberOfTasks(); |
| 33 | + //printer.printf("Running tasks: %u\n", uxArraySize); |
| 34 | + |
| 35 | + // Allocate a TaskStatus_t structure for each task. |
| 36 | + pxTaskStatusArray = (TaskStatus_t*)pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) ); |
| 37 | + |
| 38 | + if( pxTaskStatusArray != NULL ) { |
| 39 | + // Generate raw status information about each task. |
| 40 | + uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalRunTime ); |
| 41 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 42 | + uiCurrentRunTime = ulTotalRunTime - ulLastRunTime; |
| 43 | + ulLastRunTime = ulTotalRunTime; |
| 44 | +#endif |
| 45 | + printer.printf("Tasks: %u" |
| 46 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 47 | + ", Runtime: %us, Period: %uus" |
| 48 | +#endif |
| 49 | + "\n", uxArraySize |
| 50 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 51 | + , ulTotalRunTime / 1000000, uiCurrentRunTime |
| 52 | +#endif |
| 53 | + ); |
| 54 | + printer.printf("Num\t Name" |
| 55 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 56 | + "\t Load" |
| 57 | +#endif |
| 58 | + "\tPrio\t Free" |
| 59 | +#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 60 | + "\tCore" |
| 61 | +#endif |
| 62 | + "\tState\r\n"); |
| 63 | + for( x = 0; x < uxArraySize; x++ ) { |
| 64 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 65 | + if (pxTaskStatusArray[ x ].xTaskNumber < FREERTOS_TASK_NUMBER_MAX_NUM) { |
| 66 | + uiTaskRunTime = (pxTaskStatusArray[ x ].ulRunTimeCounter - ulRunTimeCounters[pxTaskStatusArray[ x ].xTaskNumber]); |
| 67 | + ulRunTimeCounters[pxTaskStatusArray[ x ].xTaskNumber] = pxTaskStatusArray[ x ].ulRunTimeCounter; |
| 68 | + uiTaskRunTime = (uiTaskRunTime * 100) / uiCurrentRunTime; // in percentage |
| 69 | + } else { |
| 70 | + uiTaskRunTime = 0; |
| 71 | + } |
| 72 | +#endif |
| 73 | + printer.printf("%3u\t%16s" |
| 74 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 75 | + "\t%8lu%%" |
| 76 | +#endif |
| 77 | + "\t%4u\t%5lu" |
| 78 | +#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 79 | + "\t%4c" |
| 80 | +#endif |
| 81 | + "\t%s\r\n", |
| 82 | + pxTaskStatusArray[ x ].xTaskNumber, |
| 83 | + pxTaskStatusArray[ x ].pcTaskName, |
| 84 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 85 | + uiTaskRunTime, |
| 86 | +#endif |
| 87 | + pxTaskStatusArray[ x ].uxCurrentPriority, |
| 88 | + pxTaskStatusArray[ x ].usStackHighWaterMark, |
| 89 | +#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 90 | + (pxTaskStatusArray[ x ].xCoreID == tskNO_AFFINITY)?'*':('0'+pxTaskStatusArray[ x ].xCoreID), |
| 91 | +#endif |
| 92 | + taskStates[pxTaskStatusArray[ x ].eCurrentState] |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + // The array is no longer needed, free the memory it consumes. |
| 97 | + vPortFree( pxTaskStatusArray ); |
| 98 | + printer.println(); |
| 99 | + } |
| 100 | +#endif /* CONFIG_FREERTOS_USE_TRACE_FACILITY */ |
| 101 | +} |
0 commit comments