Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TaskStat::setOutput to dynamically switch output stream #2923

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading