figments: mainloop: add debugging tool that records last task prior to reset

This commit is contained in:
Torrie Fischer 2023-02-19 18:41:25 +01:00
parent 1426b1524b
commit de5d3e836a
2 changed files with 40 additions and 20 deletions

View File

@ -13,12 +13,13 @@ MainLoop::dispatch(const InputEvent& evt)
m_eventBuf.insert(evt); m_eventBuf.insert(evt);
} }
//#ifdef BOARD_ESP32
__NOINIT_ATTR const char* s_lastTaskName;
//#endif
void void
MainLoop::loop() MainLoop::dispatchSync(const InputEvent& evt)
{ {
s_instance = this;
InputEvent evt;
while (m_eventBuf.take(evt)) {
if (evt.intent == InputEvent::StartThing || evt.intent == InputEvent::StopThing) { if (evt.intent == InputEvent::StartThing || evt.intent == InputEvent::StopThing) {
const bool jobState = (evt.intent == InputEvent::StartThing); const bool jobState = (evt.intent == InputEvent::StartThing);
for(auto figmentJob: scheduler.tasks) { for(auto figmentJob: scheduler.tasks) {
@ -36,9 +37,25 @@ MainLoop::loop()
for(Task* task : scheduler) { for(Task* task : scheduler) {
Log.verbose("** Eventing %s", task->name); Log.verbose("** Eventing %s", task->name);
s_lastTaskName = task->name;
task->handleEvent(evt); task->handleEvent(evt);
} }
} }
const char*
MainLoop::lastTaskName()
{
return s_lastTaskName;
}
void
MainLoop::loop()
{
s_instance = this;
InputEvent evt;
while (m_eventBuf.take(evt)) {
dispatchSync(evt);
}
unsigned int slowest = 0; unsigned int slowest = 0;
unsigned int frameSpeed = 0; unsigned int frameSpeed = 0;
unsigned int frameStart = millis(); unsigned int frameStart = millis();
@ -52,6 +69,7 @@ MainLoop::loop()
unsigned int start = millis(); unsigned int start = millis();
#endif #endif
Log.verbose("Running %s", task->name); Log.verbose("Running %s", task->name);
s_lastTaskName = task->name;
task->loop(); task->loop();
#if defined(BOARD_ESP32) or defined(BOARD_ESP8266) #if defined(BOARD_ESP32) or defined(BOARD_ESP8266)
unsigned int runtime = (ESP.getCycleCount() - start) / 160000; unsigned int runtime = (ESP.getCycleCount() - start) / 160000;
@ -66,7 +84,7 @@ MainLoop::loop()
} }
} }
frameSpeed = millis() - frameStart; frameSpeed = millis() - frameStart;
if (frameSpeed >= 23) { if (frameSpeed >= 23) { // TODO: Configure max frame time at build
const char* slowestName = (slowestTask->name ? slowestTask->name : "(Unnamed)"); const char* slowestName = (slowestTask->name ? slowestTask->name : "(Unnamed)");
Log.warning("Slow frame: %dms, %d tasks, longest task %s was %dms", frameSpeed, taskCount, slowestTask->name, slowest); Log.warning("Slow frame: %dms, %d tasks, longest task %s was %dms", frameSpeed, taskCount, slowestTask->name, slowest);
} }

View File

@ -60,7 +60,9 @@ struct MainLoop {
void start(); void start();
void loop(); void loop();
void dispatch(const InputEvent& event); void dispatch(const InputEvent& event);
void dispatchSync(const InputEvent& event);
static MainLoop* instance() { return s_instance; } static MainLoop* instance() { return s_instance; }
static const char* lastTaskName();
private: private:
Ringbuf<InputEvent, 32> m_eventBuf; Ringbuf<InputEvent, 32> m_eventBuf;