🪄 Formats TaskManager

This commit is contained in:
Rune Harlyk
2024-07-09 20:00:17 +02:00
committed by Rune Harlyk
parent 6a0ff5cd80
commit 0ba9ad75b0
+45 -79
View File
@@ -6,12 +6,12 @@
#include <vector> #include <vector>
#define IDLE_STACK_SIZE 2048 #define IDLE_STACK_SIZE 2048
#define DEFAULT_STACK_SIZE 2048+512 #define DEFAULT_STACK_SIZE 2048 + 512
#define DELETE_TASK(handle) if (handle != nullptr) vTaskDelete(handle) #define DELETE_TASK(handle) \
if (handle != nullptr) vTaskDelete(handle)
struct task_t struct task_t {
{
String name; String name;
TaskHandle_t handle; TaskHandle_t handle;
uint32_t stackSize; uint32_t stackSize;
@@ -21,8 +21,7 @@ struct task_t
bool active; // blocked ('B'), ready ('R'), deleted ('D') or suspended ('S'). bool active; // blocked ('B'), ready ('R'), deleted ('D') or suspended ('S').
}; };
class IdleTask class IdleTask {
{
private: private:
float _idleRatio = 0; float _idleRatio = 0;
unsigned long _lastMeasurement; unsigned long _lastMeasurement;
@@ -33,22 +32,17 @@ class IdleTask
unsigned long counter = 0; unsigned long counter = 0;
public: public:
void ProcessIdleTime() void ProcessIdleTime() {
{
_lastMeasurement = millis(); _lastMeasurement = millis();
counter = 0; counter = 0;
for(;;) for (;;) {
{
int delta = millis() - _lastMeasurement; int delta = millis() - _lastMeasurement;
if (delta >= kMillisPerCalc) if (delta >= kMillisPerCalc) {
{
_idleRatio = static_cast<float>(counter) / delta; _idleRatio = static_cast<float>(counter) / delta;
_lastMeasurement = millis(); _lastMeasurement = millis();
counter = 0; counter = 0;
} } else {
else
{
esp_task_wdt_reset(); esp_task_wdt_reset();
delayMicroseconds(kMillisPerLoop * 1000); delayMicroseconds(kMillisPerLoop * 1000);
counter += kMillisPerLoop; counter += kMillisPerLoop;
@@ -56,26 +50,18 @@ class IdleTask
} }
} }
IdleTask() : _lastMeasurement(millis()) IdleTask() : _lastMeasurement(millis()) {}
{
}
float GetCPUUsage() const float GetCPUUsage() const {
{ if (millis() - _lastMeasurement > kMillisPerCalc) return 100.0f;
if (millis() - _lastMeasurement > kMillisPerCalc)
return 100.0f;
return 100.0f - 100 * _idleRatio; return 100.0f - 100 * _idleRatio;
} }
static void IdleTaskEntry(void *that) static void IdleTaskEntry(void *that) { static_cast<IdleTask *>(that)->ProcessIdleTime(); }
{
static_cast<IdleTask *>(that)->ProcessIdleTime();
}
}; };
class TaskManager class TaskManager {
{
private: private:
std::map<const char *, task_t> _tasks; std::map<const char *, task_t> _tasks;
IdleTask _taskIdle0; IdleTask _taskIdle0;
@@ -85,95 +71,75 @@ class TaskManager
TaskHandle_t _hIdle1; TaskHandle_t _hIdle1;
public: public:
TaskManager() TaskManager() {}
{
}
void begin() void begin() {
{ createTask(IdleTask::IdleTaskEntry, "Idle Core 0", IDLE_STACK_SIZE, &_taskIdle0, tskIDLE_PRIORITY - 1, &_hIdle0,
createTask(IdleTask::IdleTaskEntry, "Idle Core 0", IDLE_STACK_SIZE, &_taskIdle0, tskIDLE_PRIORITY-1, &_hIdle0, 0); 0);
createTask(IdleTask::IdleTaskEntry, "Idle Core 1", IDLE_STACK_SIZE, &_taskIdle1, tskIDLE_PRIORITY-1, &_hIdle1, 1); createTask(IdleTask::IdleTaskEntry, "Idle Core 1", IDLE_STACK_SIZE, &_taskIdle1, tskIDLE_PRIORITY - 1, &_hIdle1,
1);
esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(0)); esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(0));
esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(1)); esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(1));
esp_task_wdt_add(_hIdle0); esp_task_wdt_add(_hIdle0);
esp_task_wdt_add(_hIdle1); esp_task_wdt_add(_hIdle1);
} }
std::vector<task_t> getTasks() std::vector<task_t> getTasks() {
{
std::vector<task_t> tasks; std::vector<task_t> tasks;
for (auto const &task : _tasks) for (auto const &task : _tasks) tasks.push_back(task.second);
tasks.push_back(task.second);
return tasks; return tasks;
} }
int getTaskCount() const int getTaskCount() const { return _tasks.size(); }
{
return _tasks.size();
}
int getKernelTaskCount() const int getKernelTaskCount() const { return uxTaskGetNumberOfTasks(); }
{
return uxTaskGetNumberOfTasks();
}
void update() void update() {
{ for (auto const &task : _tasks) {
for (auto const &task : _tasks)
{
_tasks[task.first].priority = uxTaskPriorityGet(task.second.handle); _tasks[task.first].priority = uxTaskPriorityGet(task.second.handle);
_tasks[task.first].coreId = xTaskGetAffinity(task.second.handle); _tasks[task.first].coreId = xTaskGetAffinity(task.second.handle);
} }
} }
float getCpuUsage(int iCore = -1) const float getCpuUsage(int iCore = -1) const {
{ if (iCore == 0)
if (iCore == 0) return _taskIdle0.GetCPUUsage(); return _taskIdle0.GetCPUUsage();
else if (iCore == 1) return _taskIdle1.GetCPUUsage(); else if (iCore == 1)
return _taskIdle1.GetCPUUsage();
return (_taskIdle0.GetCPUUsage() + _taskIdle1.GetCPUUsage()) / 2; return (_taskIdle0.GetCPUUsage() + _taskIdle1.GetCPUUsage()) / 2;
} }
BaseType_t createTask(void (*taskFunction)(void *), const char * name, uint32_t stackSize = 2048, BaseType_t createTask(void (*taskFunction)(void *), const char *name, uint32_t stackSize = 2048,
void *params = nullptr, UBaseType_t priority = tskIDLE_PRIORITY + 1, TaskHandle_t* handle = nullptr, void *params = nullptr, UBaseType_t priority = tskIDLE_PRIORITY + 1,
BaseType_t coreId = -1) TaskHandle_t *handle = nullptr, BaseType_t coreId = -1) {
{ BaseType_t res =
BaseType_t res = coreId == -1 coreId == -1 ? xTaskCreate(taskFunction, name, stackSize, params, priority + 1, handle)
? xTaskCreate(taskFunction, name, stackSize, params, priority + 1, handle)
: xTaskCreatePinnedToCore(taskFunction, name, stackSize, params, priority + 1, handle, coreId); : xTaskCreatePinnedToCore(taskFunction, name, stackSize, params, priority + 1, handle, coreId);
task_t task = {name, handle, stackSize, priority + 1, coreId, coreId != -1, true}; task_t task = {name, handle, stackSize, priority + 1, coreId, coreId != -1, true};
if (res == pdPASS) if (res == pdPASS) _tasks[name] = task;
_tasks[name] = task;
return res; return res;
} }
void suspendTask(const char * name) void suspendTask(const char *name) {
{ if (_tasks.find(name) != _tasks.end()) {
if (_tasks.find(name) != _tasks.end())
{
vTaskSuspend(_tasks[name].handle); vTaskSuspend(_tasks[name].handle);
_tasks[name].active = false; _tasks[name].active = false;
} }
} }
void resumeTask(const char * name) void resumeTask(const char *name) {
{ if (_tasks.find(name) != _tasks.end()) {
if (_tasks.find(name) != _tasks.end())
{
vTaskResume(_tasks[name].handle); vTaskResume(_tasks[name].handle);
_tasks[name].active = true; _tasks[name].active = true;
} }
} }
void notifyTask(const char * name, uint32_t notificationValue, eNotifyAction action = eSetValueWithOverwrite) void notifyTask(const char *name, uint32_t notificationValue, eNotifyAction action = eSetValueWithOverwrite) {
{ if (_tasks.find(name) != _tasks.end()) xTaskNotify(_tasks[name].handle, notificationValue, action);
if (_tasks.find(name) != _tasks.end())
xTaskNotify(_tasks[name].handle, notificationValue, action);
} }
void deleteTask(const char * name) void deleteTask(const char *name) {
{ if (_tasks.find(name) != _tasks.end()) {
if (_tasks.find(name) != _tasks.end())
{
vTaskDelete(_tasks[name].handle); vTaskDelete(_tasks[name].handle);
_tasks.erase(name); _tasks.erase(name);
} }