Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit edf7ada

Browse files
committedOct 24, 2024·
feature(rtos): Add Tasks status print function
1 parent 6dfd958 commit edf7ada

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
 

‎cores/esp32/Arduino.h

Lines changed: 1 addition & 0 deletions
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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

‎cores/esp32/freertos_stats.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include "Print.h"
3+
4+
void printRunningTasks(Print & printer);

0 commit comments

Comments
 (0)
Please sign in to comment.