Skip to content

Commit 38a4c29

Browse files
me-no-devpre-commit-ci-lite[bot]
andauthoredOct 25, 2024··
feature(rtos): Add Tasks status print function (#10515)
* feature(rtos): Add Tasks status print function * fix(cmake): Add the new cpp file to CMakeLists * fix(stats): Adjust size of Load column * fix(format): Fix print of runtime formatting * fix(stats): Add license, usage note and C++ guards * fix(stats): Fix formatting and variable names * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 9ac705e commit 38a4c29

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed
 

‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ set(CORE_SRCS
4949
cores/esp32/esp32-hal-uart.c
5050
cores/esp32/esp32-hal-rmt.c
5151
cores/esp32/Esp.cpp
52+
cores/esp32/freertos_stats.cpp
5253
cores/esp32/FunctionalInterrupt.cpp
5354
cores/esp32/HardwareSerial.cpp
5455
cores/esp32/HEXBuilder.cpp

‎cores/esp32/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
199199
#include "Udp.h"
200200
#include "HardwareSerial.h"
201201
#include "Esp.h"
202+
#include "freertos_stats.h"
202203

203204
// Use float-compatible stl abs() and round(), we don't use Arduino macros to avoid issues with the C++ libraries
204205
using std::abs;

‎cores/esp32/freertos_stats.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

‎cores/esp32/freertos_stats.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
#pragma once
16+
17+
#ifdef __cplusplus
18+
19+
#include "Print.h"
20+
21+
/*
22+
* Executing this function will cause interrupts and
23+
* the scheduler to be blocked for some time.
24+
* Please use only for debugging purposes.
25+
*/
26+
void printRunningTasks(Print &printer);
27+
28+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.