Skip to content

Commit

Permalink
[CPU usage] Increase loop count
Browse files Browse the repository at this point in the history
Attempting to find a plugin ID based on a task number was done a lot and also rather brute force.
Now a cache is added to lessen the load.
  • Loading branch information
TD-er committed Jun 26, 2018
1 parent 4c1b3a8 commit 2fde3d2
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/Commands/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ bool Command_Malloc(struct EventStruct *event, const char* Line)
bool Command_SysLoad(struct EventStruct *event, const char* Line)
{
bool success = true;
Serial.print(100 - (100 * loopCounterLast / loopCounterMax));
Serial.print(getCPUload());
Serial.print(F("% (LC="));
Serial.print(int(loopCounterLast / 30));
Serial.print(getCPUload());
Serial.println(F(")"));
return success;
}
Expand Down
12 changes: 11 additions & 1 deletion src/ESPEasy-Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,8 @@ boolean WebLoggedIn = false;
int WebLoggedInTimer = 300;

boolean (*Plugin_ptr[PLUGIN_MAX])(byte, struct EventStruct*, String&);
byte Plugin_id[PLUGIN_MAX];
std::vector<byte> Plugin_id;
std::vector<int> Task_id_to_Plugin_id;

boolean (*CPlugin_ptr[CPLUGIN_MAX])(byte, struct EventStruct*, String&);
byte CPlugin_id[CPLUGIN_MAX];
Expand Down Expand Up @@ -1293,6 +1294,15 @@ unsigned long elapsed50ps = 0;
unsigned long loopCounter = 0;
unsigned long loopCounterLast = 0;
unsigned long loopCounterMax = 1;
unsigned long lastLoopStart = 0;
unsigned long shortestLoop = 10000000;
unsigned long longestLoop = 0;
unsigned long loopCounter_full = 1;
float loop_usec_duration_total = 0.0;
unsigned long countFindPluginId = 0;

unsigned long systemTimerCalls = 1;
float systemTimerDurationTotal = 0.0;

unsigned long dailyResetCounter = 0;

Expand Down
82 changes: 74 additions & 8 deletions src/ESPEasy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ void setup()
WiFi.setAutoReconnect(false);
setWifiMode(WIFI_OFF);

Plugin_id.resize(PLUGIN_MAX);
Task_id_to_Plugin_id.resize(TASKS_MAX);

checkRAM(F("setup"));
#if defined(ESP32)
for(byte x = 0; x < 16; x++)
Expand Down Expand Up @@ -364,6 +367,66 @@ bool getControllerProtocolDisplayName(byte ProtocolIndex, byte parameterIdx, Str
return CPlugin_ptr[ProtocolIndex](CPLUGIN_GET_PROTOCOL_DISPLAY_NAME, &tmpEvent, protoDisplayName);
}

void updateLoopStats() {
++loopCounter;
++loopCounter_full;
if (lastLoopStart == 0) {
lastLoopStart = micros();
return;
}
const long usecSince = usecPassedSince(lastLoopStart);
loop_usec_duration_total += usecSince;
lastLoopStart = micros();
if (usecSince <= 0 || usecSince > 10000000)
return; // No loop should take > 1 sec.
if (shortestLoop > usecSince) {
shortestLoop = usecSince;
loopCounterMax = 30 * 1000000 / usecSince;
}
if (longestLoop < usecSince)
longestLoop = usecSince;
}

void updateLoopStats_30sec() {
loopCounterLast = loopCounter;
loopCounter = 0;
if (loopCounterLast > loopCounterMax)
loopCounterMax = loopCounterLast;

String log = F("LoopStats: shortestLoop: ");
log += shortestLoop;
log += F(" longestLoop: ");
log += longestLoop;
log += F(" avgLoopDuration: ");
log += loop_usec_duration_total / loopCounter_full;
log += F(" systemTimerDuration: ");
log += systemTimerDurationTotal / systemTimerCalls;
log += F(" systemTimerCalls: ");
log += systemTimerCalls;
log += F(" loopCounterMax: ");
log += loopCounterMax;
log += F(" loopCounterLast: ");
log += loopCounterLast;
log += F(" countFindPluginId: ");
log += countFindPluginId;
addLog(LOG_LEVEL_INFO, log);
countFindPluginId = 0;
loop_usec_duration_total = 0;
loopCounter_full = 1;
systemTimerDurationTotal = 0;
systemTimerCalls = 1;
}

int getCPUload() {
return 100 - (100 * loopCounterLast / loopCounterMax);
}

int getLoopCountPerSec() {
return loopCounterLast / 30;
}



/*********************************************************************************************\
* MAIN LOOP
\*********************************************************************************************/
Expand All @@ -372,7 +435,7 @@ void loop()
if(MainLoopCall_ptr)
MainLoopCall_ptr();

loopCounter++;
updateLoopStats();

if (wifiSetupConnect)
{
Expand Down Expand Up @@ -629,6 +692,7 @@ void runEach30Seconds()
{
extern void checkRAMtoLog();
checkRAMtoLog();
updateLoopStats_30sec();
wdcounter++;
timerwd = millis() + 30000;
String log;
Expand All @@ -650,10 +714,6 @@ void runEach30Seconds()
#if FEATURE_ADC_VCC
vcc = ESP.getVcc() / 1000.0;
#endif
loopCounterLast = loopCounter;
loopCounter = 0;
if (loopCounterLast > loopCounterMax)
loopCounterMax = loopCounterLast;

#ifdef FEATURE_REPORTING
ReportStatus();
Expand Down Expand Up @@ -831,6 +891,7 @@ void setSystemCMDTimer(unsigned long timer, String& action)
\*********************************************************************************************/
void checkSystemTimers()
{
unsigned long start = micros();
for (byte x = 0; x < SYSTEM_TIMER_MAX; x++)
if (systemTimers[x].timer != 0)
{
Expand All @@ -844,9 +905,10 @@ void checkSystemTimers()
TempEvent.Par4 = systemTimers[x].Par4;
TempEvent.Par5 = systemTimers[x].Par5;
systemTimers[x].timer = 0;
for (byte y = 0; y < PLUGIN_MAX; y++)
if (Plugin_id[y] == systemTimers[x].plugin)
Plugin_ptr[y](PLUGIN_TIMER_IN, &TempEvent, dummyString);
const int y = getPluginId(systemTimers[x].TaskIndex);
if (y >= 0) {
Plugin_ptr[y](PLUGIN_TIMER_IN, &TempEvent, dummyString);
}
}
}

Expand All @@ -861,6 +923,10 @@ void checkSystemTimers()
systemCMDTimers[x].timer = 0;
systemCMDTimers[x].action = "";
}

++systemTimerCalls;
systemTimerDurationTotal += usecPassedSince(start);;

}


Expand Down
2 changes: 1 addition & 1 deletion src/StringConverter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ void parseSystemVariables(String& s, boolean useURLencode)
#endif

if (s.indexOf(F("%sys")) != -1) {
SMART_REPL(F("%sysload%"), String(100 - (100 * loopCounterLast / loopCounterMax)))
SMART_REPL(F("%sysload%"), String(getCPUload()))
SMART_REPL(F("%sysheap%"), String(ESP.getFreeHeap()));
SMART_REPL(F("%systm_hm%"), getTimeString(':', false))
SMART_REPL(F("%systm_hm_am%"), getTimeString_ampm(':', false))
Expand Down
12 changes: 6 additions & 6 deletions src/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,9 @@ void handle_root() {
TXBuffer += F("<TR><TD>Load:<TD>");
if (wdcounter > 0)
{
TXBuffer += String(100 - (100 * loopCounterLast / loopCounterMax));
TXBuffer += String(getCPUload());
TXBuffer += F("% (LC=");
TXBuffer += String(int(loopCounterLast / 30));
TXBuffer += String(getLoopCountPerSec());
TXBuffer += F(")");
}

Expand Down Expand Up @@ -3644,8 +3644,8 @@ void handle_json()

if (wdcounter > 0)
{
stream_next_json_object_value(F("Load"), String( 100 - (100 * loopCounterLast / loopCounterMax) ));
stream_next_json_object_value(F("Load LC"), String( int(loopCounterLast / 30) ));
stream_next_json_object_value(F("Load"), String(getCPUload()));
stream_next_json_object_value(F("Load LC"), String(getLoopCountPerSec()));
}

stream_last_json_object_value(F("Free RAM"), String(ESP.getFreeHeap()));
Expand Down Expand Up @@ -5007,9 +5007,9 @@ void handle_sysinfo() {
TXBuffer += F("<TR><TD>Load<TD>");
if (wdcounter > 0)
{
TXBuffer += 100 - (100 * loopCounterLast / loopCounterMax);
TXBuffer += getCPUload();
TXBuffer += F("% (LC=");
TXBuffer += int(loopCounterLast / 30);
TXBuffer += getLoopCountPerSec();
TXBuffer += F(")");
}

Expand Down
2 changes: 1 addition & 1 deletion src/_P026_Sysinfo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ boolean Plugin_026(byte function, struct EventStruct *event, String& string)
}
case 4:
{
value = (100 - (100 * loopCounterLast / loopCounterMax));
value = getCPUload();
break;
}
case 5:
Expand Down

0 comments on commit 2fde3d2

Please sign in to comment.