Merge pull request #2074 from meshtastic/nice-threads

a lot of thread housekeeping
This commit is contained in:
Thomas Göttgens 2022-12-30 20:58:09 +01:00 committed by GitHub
commit 9a04aaa811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 150 additions and 97 deletions

View File

@ -288,7 +288,17 @@ void Power::readPowerStatus()
newStatus.notifyObservers(&powerStatus2); newStatus.notifyObservers(&powerStatus2);
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
if (lastheap != ESP.getFreeHeap()) { if (lastheap != ESP.getFreeHeap()) {
LOG_DEBUG("Heap status: %d/%d bytes free (%d), running %d threads\n", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getFreeHeap() - lastheap , concurrency::mainController.size(false)); LOG_DEBUG("Threads running:");
int running = 0;
for(int i = 0; i < MAX_THREADS; i++){
auto thread = concurrency::mainController.get(i);
if((thread != nullptr) && (thread->enabled)) {
LOG_DEBUG(" %s", thread->ThreadName.c_str());
running++;
}
}
LOG_DEBUG("\n");
LOG_DEBUG("Heap status: %d/%d bytes free (%d), running %d/%d threads\n", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getFreeHeap() - lastheap, running, concurrency::mainController.size(false));
lastheap = ESP.getFreeHeap(); lastheap = ESP.getFreeHeap();
} }
#endif #endif

View File

@ -74,8 +74,18 @@ bool OSThread::shouldRun(unsigned long time)
void OSThread::run() void OSThread::run()
{ {
#ifdef DEBUG_HEAP
auto heap = ESP.getFreeHeap();
#endif
currentThread = this; currentThread = this;
auto newDelay = runOnce(); auto newDelay = runOnce();
#ifdef DEBUG_HEAP
auto newHeap = ESP.getFreeHeap();
if (newHeap < heap)
LOG_DEBUG("------ Thread %s leaked heap %d -> %d (%d) ------\n", ThreadName.c_str(), heap, newHeap, newHeap - heap);
if (heap < newHeap)
LOG_DEBUG("++++++ Thread %s freed heap %d -> %d (%d) ++++++\n", ThreadName.c_str(), heap, newHeap, newHeap - heap);
#endif
runned(); runned();
@ -85,6 +95,14 @@ void OSThread::run()
currentThread = NULL; currentThread = NULL;
} }
int32_t OSThread::disable()
{
enabled = false;
setInterval(INT32_MAX);
return INT32_MAX;
}
/** /**
* This flag is set **only** when setup() starts, to provide a way for us to check for sloppy static constructor calls. * This flag is set **only** when setup() starts, to provide a way for us to check for sloppy static constructor calls.
* Call assertIsSetup() to force a crash if someone tries to create an instance too early. * Call assertIsSetup() to force a crash if someone tries to create an instance too early.

View File

@ -53,6 +53,8 @@ class OSThread : public Thread
static void setup(); static void setup();
int32_t disable();
/** /**
* Wait a specified number msecs starting from the current time (rather than the last time we were run) * Wait a specified number msecs starting from the current time (rather than the last time we were run)
*/ */

View File

@ -54,7 +54,7 @@ int32_t RotaryEncoderInterruptBase::runOnce()
this->action = ROTARY_ACTION_NONE; this->action = ROTARY_ACTION_NONE;
return 30000; // TODO: technically this can be MAX_INT return INT32_MAX;
} }
void RotaryEncoderInterruptBase::intPressHandler() void RotaryEncoderInterruptBase::intPressHandler()

View File

@ -7,7 +7,7 @@ enum RotaryEncoderInterruptBaseStateType { ROTARY_EVENT_OCCURRED, ROTARY_EVENT_C
enum RotaryEncoderInterruptBaseActionType { ROTARY_ACTION_NONE, ROTARY_ACTION_PRESSED, ROTARY_ACTION_CW, ROTARY_ACTION_CCW }; enum RotaryEncoderInterruptBaseActionType { ROTARY_ACTION_NONE, ROTARY_ACTION_PRESSED, ROTARY_ACTION_CW, ROTARY_ACTION_CCW };
class RotaryEncoderInterruptBase : public Observable<const InputEvent *>, private concurrency::OSThread class RotaryEncoderInterruptBase : public Observable<const InputEvent *>, public concurrency::OSThread
{ {
public: public:
explicit RotaryEncoderInterruptBase(const char *name); explicit RotaryEncoderInterruptBase(const char *name);

View File

@ -9,6 +9,7 @@ void RotaryEncoderInterruptImpl1::init()
{ {
if (!moduleConfig.canned_message.rotary1_enabled) { if (!moduleConfig.canned_message.rotary1_enabled) {
// Input device is disabled. // Input device is disabled.
disable();
return; return;
} }

View File

@ -12,7 +12,7 @@ void CardKbI2cImpl::init()
{ {
if (cardkb_found != CARDKB_ADDR) if (cardkb_found != CARDKB_ADDR)
{ {
// Input device is not detected. disable();
return; return;
} }

View File

@ -5,7 +5,7 @@
class KbI2cBase : class KbI2cBase :
public Observable<const InputEvent *>, public Observable<const InputEvent *>,
private concurrency::OSThread public concurrency::OSThread
{ {
public: public:
explicit KbI2cBase(const char *name); explicit KbI2cBase(const char *name);

View File

@ -221,7 +221,10 @@ ErrorCode Router::send(MeshPacket *p)
if (p->which_payload_variant == MeshPacket_decoded_tag) { if (p->which_payload_variant == MeshPacket_decoded_tag) {
ChannelIndex chIndex = p->channel; // keep as a local because we are about to change it ChannelIndex chIndex = p->channel; // keep as a local because we are about to change it
bool shouldActuallyEncrypt = true;
#if HAS_WIFI || HAS_ETHERNET #if HAS_WIFI || HAS_ETHERNET
if(moduleConfig.mqtt.enabled) {
// check if we should send decrypted packets to mqtt // check if we should send decrypted packets to mqtt
// truth table: // truth table:
@ -234,7 +237,6 @@ ErrorCode Router::send(MeshPacket *p)
* => so we only decrypt mqtt if they have a custom mqtt server AND mqtt_encryption_enabled is FALSE * => so we only decrypt mqtt if they have a custom mqtt server AND mqtt_encryption_enabled is FALSE
*/ */
bool shouldActuallyEncrypt = true;
if (*moduleConfig.mqtt.address && !moduleConfig.mqtt.encryption_enabled) { if (*moduleConfig.mqtt.address && !moduleConfig.mqtt.encryption_enabled) {
shouldActuallyEncrypt = false; shouldActuallyEncrypt = false;
} }
@ -244,6 +246,7 @@ ErrorCode Router::send(MeshPacket *p)
// the packet is currently in a decrypted state. send it now if they want decrypted packets // the packet is currently in a decrypted state. send it now if they want decrypted packets
if (mqtt && !shouldActuallyEncrypt) if (mqtt && !shouldActuallyEncrypt)
mqtt->onSend(*p, chIndex); mqtt->onSend(*p, chIndex);
}
#endif #endif
auto encodeResult = perhapsEncode(p); auto encodeResult = perhapsEncode(p);
@ -253,10 +256,12 @@ ErrorCode Router::send(MeshPacket *p)
} }
#if HAS_WIFI || HAS_ETHERNET #if HAS_WIFI || HAS_ETHERNET
if (moduleConfig.mqtt.enabled) {
// the packet is now encrypted. // the packet is now encrypted.
// check if we should send encrypted packets to mqtt // check if we should send encrypted packets to mqtt
if (mqtt && shouldActuallyEncrypt) if (mqtt && shouldActuallyEncrypt)
mqtt->onSend(*p, chIndex); mqtt->onSend(*p, chIndex);
}
#endif #endif
} }

View File

@ -165,11 +165,18 @@ void createSSLCert()
WebServerThread *webServerThread; WebServerThread *webServerThread;
WebServerThread::WebServerThread() : concurrency::OSThread("WebServerThread") {} WebServerThread::WebServerThread() : concurrency::OSThread("WebServerThread") {
if (!config.network.wifi_enabled) {
disable();
}
}
int32_t WebServerThread::runOnce() int32_t WebServerThread::runOnce()
{ {
// LOG_DEBUG("WebServerThread::runOnce()\n"); if (!config.network.wifi_enabled) {
disable();
}
handleWebResponse(); handleWebResponse();
if (requestRestart && (millis() / 1000) > requestRestart) { if (requestRestart && (millis() / 1000) > requestRestart) {

View File

@ -51,10 +51,14 @@ CannedMessageModule::CannedMessageModule()
if ((this->splitConfiguredMessages() <= 0) && (cardkb_found != CARDKB_ADDR)) { if ((this->splitConfiguredMessages() <= 0) && (cardkb_found != CARDKB_ADDR)) {
LOG_INFO("CannedMessageModule: No messages are configured. Module is disabled\n"); LOG_INFO("CannedMessageModule: No messages are configured. Module is disabled\n");
this->runState = CANNED_MESSAGE_RUN_STATE_DISABLED; this->runState = CANNED_MESSAGE_RUN_STATE_DISABLED;
disable();
} else { } else {
LOG_INFO("CannedMessageModule is enabled\n"); LOG_INFO("CannedMessageModule is enabled\n");
this->inputObserver.observe(inputBroker); this->inputObserver.observe(inputBroker);
} }
} else {
this->runState = CANNED_MESSAGE_RUN_STATE_DISABLED;
disable();
} }
} }

View File

@ -208,7 +208,7 @@ ExternalNotificationModule::ExternalNotificationModule()
} }
} else { } else {
LOG_INFO("External Notification Module Disabled\n"); LOG_INFO("External Notification Module Disabled\n");
enabled = false; disable();
} }
} }

View File

@ -48,12 +48,13 @@ static uint64_t digitalReads(uint64_t mask)
RemoteHardwareModule::RemoteHardwareModule() RemoteHardwareModule::RemoteHardwareModule()
: ProtobufModule("remotehardware", PortNum_REMOTE_HARDWARE_APP, &HardwareMessage_msg), concurrency::OSThread( : ProtobufModule("remotehardware", PortNum_REMOTE_HARDWARE_APP, &HardwareMessage_msg), concurrency::OSThread(
"remotehardware") "RemoteHardwareModule")
{ {
} }
bool RemoteHardwareModule::handleReceivedProtobuf(const MeshPacket &req, HardwareMessage *pptr) bool RemoteHardwareModule::handleReceivedProtobuf(const MeshPacket &req, HardwareMessage *pptr)
{ {
if (moduleConfig.remote_hardware.enabled) {
auto p = *pptr; auto p = *pptr;
LOG_INFO("Received RemoteHardware typ=%d\n", p.type); LOG_INFO("Received RemoteHardware typ=%d\n", p.type);
@ -107,13 +108,14 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const MeshPacket &req, Hardwar
LOG_ERROR("Hardware operation %d not yet implemented! FIXME\n", p.type); LOG_ERROR("Hardware operation %d not yet implemented! FIXME\n", p.type);
break; break;
} }
}
return false; return false;
} }
int32_t RemoteHardwareModule::runOnce() int32_t RemoteHardwareModule::runOnce()
{ {
if (watchGpios) { if (moduleConfig.remote_hardware.enabled && watchGpios) {
uint32_t now = millis(); uint32_t now = millis();
if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) { if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) {
@ -133,7 +135,7 @@ int32_t RemoteHardwareModule::runOnce()
} }
} else { } else {
// No longer watching anything - stop using CPU // No longer watching anything - stop using CPU
enabled = false; return disable();
} }
return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg) return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg)

View File

@ -220,9 +220,7 @@ int32_t SerialModule::runOnce()
return (10); return (10);
} else { } else {
LOG_INFO("Serial Module Disabled\n"); return disable();
return INT32_MAX;
} }
} }
@ -303,9 +301,6 @@ ProcessMessage SerialModuleRadio::handleReceived(const MeshPacket &mp)
} }
} }
} }
} else {
LOG_INFO("Serial Module Disabled\n");
} }
return ProcessMessage::CONTINUE; // Let others look at this message also if they want return ProcessMessage::CONTINUE; // Let others look at this message also if they want
} }

View File

@ -67,7 +67,7 @@ int32_t EnvironmentTelemetryModule::runOnce()
if (!(moduleConfig.telemetry.environment_measurement_enabled || if (!(moduleConfig.telemetry.environment_measurement_enabled ||
moduleConfig.telemetry.environment_screen_enabled)) { moduleConfig.telemetry.environment_screen_enabled)) {
// If this module is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it // If this module is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it
return result; return disable();
} }
if (firstTime) { if (firstTime) {

View File

@ -139,7 +139,7 @@ AudioModule::AudioModule() : SinglePortModule("AudioModule", PortNum_AUDIO_APP),
LOG_INFO(" using %d frames of %d bytes for a total payload length of %d bytes\n", encode_frame_num, encode_codec_size, encode_frame_size); LOG_INFO(" using %d frames of %d bytes for a total payload length of %d bytes\n", encode_frame_num, encode_codec_size, encode_frame_size);
xTaskCreate(&run_codec2, "codec2_task", 30000, NULL, 5, &codec2HandlerTask); xTaskCreate(&run_codec2, "codec2_task", 30000, NULL, 5, &codec2HandlerTask);
} else { } else {
LOG_INFO("Codec2 disabled (AudioModule %d, Region %s, permitted %d)\n", moduleConfig.audio.codec2_enabled, myRegion->name, myRegion->audioPermitted); disable();
} }
} }
@ -258,8 +258,7 @@ int32_t AudioModule::runOnce()
} }
return 100; return 100;
} else { } else {
LOG_INFO("Audio Module Disabled\n"); return disable();
return INT32_MAX;
} }
} }

View File

@ -82,7 +82,7 @@ int32_t RangeTestModule::runOnce()
return (senderHeartbeat); return (senderHeartbeat);
} else { } else {
return (INT32_MAX); return disable();
// This thread does not need to run as a receiver // This thread does not need to run as a receiver
} }
@ -93,7 +93,7 @@ int32_t RangeTestModule::runOnce()
} }
#endif #endif
return (INT32_MAX); return disable();
} }
MeshPacket *RangeTestModuleRadio::allocReply() MeshPacket *RangeTestModuleRadio::allocReply()

View File

@ -52,7 +52,7 @@ int32_t StoreForwardModule::runOnce()
return (this->packetTimeMax); return (this->packetTimeMax);
} }
#endif #endif
return (INT32_MAX); return disable();
} }
/* /*
@ -458,6 +458,8 @@ StoreForwardModule::StoreForwardModule()
is_client = true; is_client = true;
LOG_INFO("*** Initializing Store & Forward Module in Client mode\n"); LOG_INFO("*** Initializing Store & Forward Module in Client mode\n");
} }
} else {
disable();
} }
#endif #endif
} }

View File

@ -128,12 +128,17 @@ void mqttInit()
MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient), mqttQueue(MAX_MQTT_QUEUE) MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient), mqttQueue(MAX_MQTT_QUEUE)
{ {
if(moduleConfig.mqtt.enabled) {
assert(!mqtt); assert(!mqtt);
mqtt = this; mqtt = this;
pubSub.setCallback(mqttCallback); pubSub.setCallback(mqttCallback);
// preflightSleepObserver.observe(&preflightSleep); // preflightSleepObserver.observe(&preflightSleep);
} else {
disable();
}
} }
bool MQTT::connected() bool MQTT::connected()
@ -239,6 +244,9 @@ bool MQTT::wantsLink() const
int32_t MQTT::runOnce() int32_t MQTT::runOnce()
{ {
if(!moduleConfig.mqtt.enabled) {
return disable();
}
bool wantConnection = wantsLink(); bool wantConnection = wantsLink();
// If connected poll rapidly, otherwise only occasionally check for a wifi connection change and ability to contact server // If connected poll rapidly, otherwise only occasionally check for a wifi connection change and ability to contact server