|
| 1 | +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "freertos_stats.h" |
| 16 | +#include "sdkconfig.h" |
| 17 | + |
| 18 | +#if CONFIG_FREERTOS_USE_TRACE_FACILITY |
| 19 | +#include "freertos/FreeRTOS.h" |
| 20 | +#include "freertos/task.h" |
| 21 | +#include "freertos/portable.h" |
| 22 | +#endif /* CONFIG_FREERTOS_USE_TRACE_FACILITY */ |
| 23 | + |
| 24 | +void printRunningTasks(Print &printer) { |
| 25 | +#if CONFIG_FREERTOS_USE_TRACE_FACILITY |
| 26 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 27 | +#define FREERTOS_TASK_NUMBER_MAX_NUM 256 // RunTime stats for how many Tasks to be stored |
| 28 | + static configRUN_TIME_COUNTER_TYPE ulRunTimeCounters[FREERTOS_TASK_NUMBER_MAX_NUM]; |
| 29 | + static configRUN_TIME_COUNTER_TYPE ulLastRunTime = 0; |
| 30 | + configRUN_TIME_COUNTER_TYPE ulCurrentRunTime = 0, ulTaskRunTime = 0; |
| 31 | +#endif |
| 32 | + configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0; |
| 33 | + TaskStatus_t *pxTaskStatusArray = NULL; |
| 34 | + volatile UBaseType_t uxArraySize = 0, x = 0; |
| 35 | + const char *taskStates[] = {"Running", "Ready", "Blocked", "Suspended", "Deleted", "Invalid"}; |
| 36 | + |
| 37 | + // Take a snapshot of the number of tasks in case it changes while this function is executing. |
| 38 | + uxArraySize = uxTaskGetNumberOfTasks(); |
| 39 | + |
| 40 | + // Allocate a TaskStatus_t structure for each task. |
| 41 | + pxTaskStatusArray = (TaskStatus_t *)pvPortMalloc(uxArraySize * sizeof(TaskStatus_t)); |
| 42 | + |
| 43 | + if (pxTaskStatusArray != NULL) { |
| 44 | + // Generate raw status information about each task. |
| 45 | + uxArraySize = uxTaskGetSystemState(pxTaskStatusArray, uxArraySize, &ulTotalRunTime); |
| 46 | + |
| 47 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 48 | + ulCurrentRunTime = ulTotalRunTime - ulLastRunTime; |
| 49 | + ulLastRunTime = ulTotalRunTime; |
| 50 | +#endif |
| 51 | + printer.printf( |
| 52 | + "Tasks: %u" |
| 53 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 54 | + ", Runtime: %lus, Period: %luus" |
| 55 | +#endif |
| 56 | + "\n", |
| 57 | + uxArraySize |
| 58 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 59 | + , |
| 60 | + ulTotalRunTime / 1000000, ulCurrentRunTime |
| 61 | +#endif |
| 62 | + ); |
| 63 | + printer.printf("Num\t Name" |
| 64 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 65 | + "\tLoad" |
| 66 | +#endif |
| 67 | + "\tPrio\t Free" |
| 68 | +#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 69 | + "\tCore" |
| 70 | +#endif |
| 71 | + "\tState\r\n"); |
| 72 | + for (x = 0; x < uxArraySize; x++) { |
| 73 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 74 | + if (pxTaskStatusArray[x].xTaskNumber < FREERTOS_TASK_NUMBER_MAX_NUM) { |
| 75 | + ulTaskRunTime = (pxTaskStatusArray[x].ulRunTimeCounter - ulRunTimeCounters[pxTaskStatusArray[x].xTaskNumber]); |
| 76 | + ulRunTimeCounters[pxTaskStatusArray[x].xTaskNumber] = pxTaskStatusArray[x].ulRunTimeCounter; |
| 77 | + ulTaskRunTime = (ulTaskRunTime * 100) / ulCurrentRunTime; // in percentage |
| 78 | + } else { |
| 79 | + ulTaskRunTime = 0; |
| 80 | + } |
| 81 | +#endif |
| 82 | + printer.printf( |
| 83 | + "%3u\t%16s" |
| 84 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 85 | + "\t%3lu%%" |
| 86 | +#endif |
| 87 | + "\t%4u\t%5lu" |
| 88 | +#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 89 | + "\t%4c" |
| 90 | +#endif |
| 91 | + "\t%s\r\n", |
| 92 | + pxTaskStatusArray[x].xTaskNumber, pxTaskStatusArray[x].pcTaskName, |
| 93 | +#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS |
| 94 | + ulTaskRunTime, |
| 95 | +#endif |
| 96 | + pxTaskStatusArray[x].uxCurrentPriority, pxTaskStatusArray[x].usStackHighWaterMark, |
| 97 | +#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID |
| 98 | + (pxTaskStatusArray[x].xCoreID == tskNO_AFFINITY) ? '*' : ('0' + pxTaskStatusArray[x].xCoreID), |
| 99 | +#endif |
| 100 | + taskStates[pxTaskStatusArray[x].eCurrentState] |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + // The array is no longer needed, free the memory it consumes. |
| 105 | + vPortFree(pxTaskStatusArray); |
| 106 | + printer.println(); |
| 107 | + } |
| 108 | +#else |
| 109 | + printer.println("FreeRTOS trace facility is not enabled."); |
| 110 | +#endif /* CONFIG_FREERTOS_USE_TRACE_FACILITY */ |
| 111 | +} |
0 commit comments