Skip to content

Commit

Permalink
Add TaskStat::setOutput (#2923)
Browse files Browse the repository at this point in the history
This PR supports switching the output of TaskStat at runtime. This allows monitoring of freeRTOS task usage for esp32 devices. Useful for switching output between standard serial and USB serial.
  • Loading branch information
mikee47 authored Dec 13, 2024
1 parent c1d5b9d commit e21f8c7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
23 changes: 13 additions & 10 deletions Sming/Arch/Esp32/Services/Profiling/TaskStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct TaskStat::Info {
#endif
};

TaskStat::TaskStat(Print& out) : out(out)
TaskStat::TaskStat(Print& out) : out(&out)
{
#if CONFIG_FREERTOS_USE_TRACE_FACILITY && CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
taskInfo = std::make_unique<Info[]>(2);
Expand All @@ -38,13 +38,16 @@ TaskStat::~TaskStat() = default;

bool TaskStat::update()
{
if(!out) {
return false;
}
#if CONFIG_FREERTOS_USE_TRACE_FACILITY && CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS

// Get current task states
auto& info = taskInfo[endIndex];
info.count = uxTaskGetSystemState(info.status, maxTasks, &info.runTime);
if(info.count == 0) {
out.println(_F("[TaskStat] uxTaskGetSystemState returned 0"));
out->println(_F("[TaskStat] uxTaskGetSystemState returned 0"));
return false;
}

Expand Down Expand Up @@ -79,16 +82,16 @@ bool TaskStat::update()
// Calculate totalElapsedTime in units of run time stats clock period.
uint32_t totalElapsedTime = endInfo.runTime - startInfo.runTime;
if(totalElapsedTime == 0) {
out.println(_F("[TaskStat] Run time was 0: Have you set CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS?"));
out->println(_F("[TaskStat] Run time was 0: Have you set CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS?"));
return false;
}

std::bitset<maxTasks> startMatched, endMatched;

PSTR_ARRAY(hdrfmt, "# | Core | Prio | Handle | Run Time | % Time | Name");
PSTR_ARRAY(datfmt, "%-3u | %c | %4u | %08x | %8u | %3u%% | %s\r\n");
out.println();
out.println(hdrfmt);
out->println();
out->println(hdrfmt);
// Match each task in startInfo.status to those in the endInfo.status
for(unsigned i = 0; i < startInfo.count; i++) {
int k = -1;
Expand All @@ -108,27 +111,27 @@ bool TaskStat::update()
#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
coreId = status.xCoreID;
#endif
out.printf(datfmt, status.xTaskNumber, (coreId == CONFIG_FREERTOS_NO_AFFINITY) ? '-' : ('0' + coreId),
status.uxCurrentPriority, status.xHandle, taskElapsedTime, percentageTime, status.pcTaskName);
out->printf(datfmt, status.xTaskNumber, (coreId == CONFIG_FREERTOS_NO_AFFINITY) ? '-' : ('0' + coreId),
status.uxCurrentPriority, status.xHandle, taskElapsedTime, percentageTime, status.pcTaskName);
}
}

// Print unmatched tasks
for(unsigned i = 0; i < startInfo.count; i++) {
if(!startMatched[i]) {
out.printf("Deleted: %s\r\n", startInfo.status[i].pcTaskName);
out->printf("Deleted: %s\r\n", startInfo.status[i].pcTaskName);
}
}
for(unsigned i = 0; i < endInfo.count; i++) {
if(!endMatched[i]) {
out.printf("Created: %s\r\n", endInfo.status[i].pcTaskName);
out->printf("Created: %s\r\n", endInfo.status[i].pcTaskName);
}
}

return true;

#else
out.println("[TaskStat] Tracing disabled");
out->println("[TaskStat] Tracing disabled");
return false;
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions Sming/Arch/Esp8266/Services/Profiling/TaskStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Profiling
struct TaskStat::Info {
};

TaskStat::TaskStat(Print& out) : out(out)
TaskStat::TaskStat(Print& out) : out(&out)
{
}

TaskStat::~TaskStat() = default;

bool TaskStat::update()
{
out.println("[TaskStat] Not Implemented");
out->println("[TaskStat] Not Implemented");
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions Sming/Arch/Host/Services/Profiling/TaskStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Profiling
struct TaskStat::Info {
};

TaskStat::TaskStat(Print& out) : out(out)
TaskStat::TaskStat(Print& out) : out(&out)
{
}

TaskStat::~TaskStat() = default;

bool TaskStat::update()
{
out.println("[TaskStat] Not Implemented");
out->println("[TaskStat] Not Implemented");
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions Sming/Arch/Rp2040/Services/Profiling/TaskStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Profiling
struct TaskStat::Info {
};

TaskStat::TaskStat(Print& out) : out(out)
TaskStat::TaskStat(Print& out) : out(&out)
{
}

TaskStat::~TaskStat() = default;

bool TaskStat::update()
{
out.println("[TaskStat] Not Implemented");
out->println("[TaskStat] Not Implemented");
return false;
}

Expand Down
10 changes: 9 additions & 1 deletion Sming/Services/Profiling/TaskStat.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ class TaskStat
*/
bool update();

/**
* @brief Change the output stream
*/
void setOutput(Print& out)
{
this->out = &out;
}

private:
Print& out;
Print* out;
static constexpr size_t maxTasks{32};
struct Info;
std::unique_ptr<Info[]> taskInfo;
Expand Down

0 comments on commit e21f8c7

Please sign in to comment.