From 761dac1f0d10337c7a003925e3761db1ff34e5b3 Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Sat, 16 Nov 2019 17:02:37 +0100 Subject: [PATCH] [Cleanup] Make timing stats optional to reduce size --- platformio.ini | 1 - src/Controller.ino | 71 ++++++++++++++ src/ESPEasy-Globals.h | 30 ------ src/ESPEasy.ino | 70 +------------- src/ESPEasyStatistics.ino | 7 ++ src/WebServer.ino | 2 + src/WebServer_JSON.ino | 2 + src/define_plugin_sets.h | 21 +++++ src/src/DataStructs/TimingStats.cpp | 3 + src/src/DataStructs/TimingStats.h | 139 ++++++++++++++++------------ 10 files changed, 187 insertions(+), 159 deletions(-) diff --git a/platformio.ini b/platformio.ini index 7f32104ca7..136e25cbe1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -197,7 +197,6 @@ build_flags = ${debug_flags.build_flags} ${mqtt_flags.build_flags} build_unflags = -DDEBUG_ESP_PORT lib_deps = https://github.com/TD-er/ESPEasySerial.git lib_ignore = ESP32_ping, ESP32WebServer, IRremoteESP8266, HeatpumpIR, SD(esp8266), SDFS -;lib_ignore = ESP32_ping, ESP32WebServer, IRremoteESP8266, HeatpumpIR lib_ldf_mode = chain lib_archive = false upload_speed = 115200 diff --git a/src/Controller.ino b/src/Controller.ino index 6872659895..62bdeb3256 100644 --- a/src/Controller.ino +++ b/src/Controller.ino @@ -419,3 +419,74 @@ void MQTTStatus(const String& status) } } #endif //USES_MQTT + + + +/*********************************************************************************************\ + * send all sensordata +\*********************************************************************************************/ +// void SensorSendAll() +// { +// for (taskIndex_t x = 0; x < TASKS_MAX; x++) +// { +// SensorSendTask(x); +// } +// } + + +/*********************************************************************************************\ + * send specific sensor task data +\*********************************************************************************************/ +void SensorSendTask(taskIndex_t TaskIndex) +{ + if (!validTaskIndex(TaskIndex)) return; + checkRAM(F("SensorSendTask")); + if (Settings.TaskDeviceEnabled[TaskIndex]) + { + byte varIndex = TaskIndex * VARS_PER_TASK; + + bool success = false; + const deviceIndex_t DeviceIndex = getDeviceIndex_from_TaskIndex(TaskIndex); + if (!validDeviceIndex(DeviceIndex)) return; + + LoadTaskSettings(TaskIndex); + + struct EventStruct TempEvent; + TempEvent.TaskIndex = TaskIndex; + TempEvent.BaseVarIndex = varIndex; + // TempEvent.idx = Settings.TaskDeviceID[TaskIndex]; todo check + TempEvent.sensorType = Device[DeviceIndex].VType; + + float preValue[VARS_PER_TASK]; // store values before change, in case we need it in the formula + for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++) + preValue[varNr] = UserVar[varIndex + varNr]; + + if(Settings.TaskDeviceDataFeed[TaskIndex] == 0) // only read local connected sensorsfeeds + { + String dummy; + success = PluginCall(PLUGIN_READ, &TempEvent, dummy); + } + else + success = true; + + if (success) + { + START_TIMER; + for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++) + { + if (ExtraTaskSettings.TaskDeviceFormula[varNr][0] != 0) + { + String formula = ExtraTaskSettings.TaskDeviceFormula[varNr]; + formula.replace(F("%pvalue%"), String(preValue[varNr])); + formula.replace(F("%value%"), String(UserVar[varIndex + varNr])); + float result = 0; + byte error = Calculate(formula.c_str(), &result); + if (error == 0) + UserVar[varIndex + varNr] = result; + } + } + STOP_TIMER(COMPUTE_FORMULA_STATS); + sendData(&TempEvent); + } + } +} \ No newline at end of file diff --git a/src/ESPEasy-Globals.h b/src/ESPEasy-Globals.h index 2016330dac..e216a9463d 100644 --- a/src/ESPEasy-Globals.h +++ b/src/ESPEasy-Globals.h @@ -356,36 +356,6 @@ extern boolean UseRTOSMultitasking; // void (*MainLoopCall_ptr)(void); //FIXME TD-er: No idea what this does. -/* -String getLogLine(const TimingStats& stats) { - unsigned long minVal, maxVal; - unsigned int c = stats.getMinMax(minVal, maxVal); - String log; - log.reserve(64); - log += F("Count: "); - log += c; - log += F(" Avg/min/max "); - log += stats.getAvg(); - log += '/'; - log += minVal; - log += '/'; - log += maxVal; - log += F(" usec"); - return log; -} -*/ - - - - - - - - - - - - #include "src/DataStructs/DeviceModel.h" diff --git a/src/ESPEasy.ino b/src/ESPEasy.ino index 20870e26e7..7b6c2fd695 100644 --- a/src/ESPEasy.ino +++ b/src/ESPEasy.ino @@ -476,7 +476,9 @@ void updateLoopStats() { return; } const long usecSince = usecPassedSince(lastLoopStart); + #ifdef USES_TIMING_STATS miscStats[LOOP_STATS].add(usecSince); + #endif loop_usec_duration_total += usecSince; lastLoopStart = micros(); @@ -881,74 +883,6 @@ void runEach30Seconds() } -/*********************************************************************************************\ - * send all sensordata -\*********************************************************************************************/ -// void SensorSendAll() -// { -// for (taskIndex_t x = 0; x < TASKS_MAX; x++) -// { -// SensorSendTask(x); -// } -// } - - -/*********************************************************************************************\ - * send specific sensor task data -\*********************************************************************************************/ -void SensorSendTask(taskIndex_t TaskIndex) -{ - if (!validTaskIndex(TaskIndex)) return; - checkRAM(F("SensorSendTask")); - if (Settings.TaskDeviceEnabled[TaskIndex]) - { - byte varIndex = TaskIndex * VARS_PER_TASK; - - bool success = false; - const deviceIndex_t DeviceIndex = getDeviceIndex_from_TaskIndex(TaskIndex); - if (!validDeviceIndex(DeviceIndex)) return; - - LoadTaskSettings(TaskIndex); - - struct EventStruct TempEvent; - TempEvent.TaskIndex = TaskIndex; - TempEvent.BaseVarIndex = varIndex; - // TempEvent.idx = Settings.TaskDeviceID[TaskIndex]; todo check - TempEvent.sensorType = Device[DeviceIndex].VType; - - float preValue[VARS_PER_TASK]; // store values before change, in case we need it in the formula - for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++) - preValue[varNr] = UserVar[varIndex + varNr]; - - if(Settings.TaskDeviceDataFeed[TaskIndex] == 0) // only read local connected sensorsfeeds - { - String dummy; - success = PluginCall(PLUGIN_READ, &TempEvent, dummy); - } - else - success = true; - - if (success) - { - START_TIMER; - for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++) - { - if (ExtraTaskSettings.TaskDeviceFormula[varNr][0] != 0) - { - String formula = ExtraTaskSettings.TaskDeviceFormula[varNr]; - formula.replace(F("%pvalue%"), String(preValue[varNr])); - formula.replace(F("%value%"), String(UserVar[varIndex + varNr])); - float result = 0; - byte error = Calculate(formula.c_str(), &result); - if (error == 0) - UserVar[varIndex + varNr] = result; - } - } - STOP_TIMER(COMPUTE_FORMULA_STATS); - sendData(&TempEvent); - } - } -} /*********************************************************************************************\ diff --git a/src/ESPEasyStatistics.ino b/src/ESPEasyStatistics.ino index cf722535f4..73bf6cd8af 100644 --- a/src/ESPEasyStatistics.ino +++ b/src/ESPEasyStatistics.ino @@ -1,3 +1,7 @@ +#include "define_plugin_sets.h" + +#ifdef USES_TIMING_STATS + /* void logStatistics(byte loglevel, bool clearStats) { if (loglevelActiveFor(loglevel)) { @@ -91,3 +95,6 @@ void jsonStatistics(bool clearStats) { stream_json_end_object_element(true); // end "plugin" object stream_json_end_array_element(true); // end "plugin" array } + + +#endif \ No newline at end of file diff --git a/src/WebServer.ino b/src/WebServer.ino index 13d579f0e4..c850ddee4f 100644 --- a/src/WebServer.ino +++ b/src/WebServer.ino @@ -317,10 +317,12 @@ void sendHeadandTail(const String& tmplName, boolean Tail = false, boolean reboo // This function is called twice per serving a web page. // So it must keep track of the timer longer than the scope of this function. // Therefore use a local static variable. + #ifdef USES_TIMING_STATS static unsigned statisticsTimerStart = 0; if (!Tail) { statisticsTimerStart = micros(); } + #endif String pageTemplate = ""; String fileName = tmplName; diff --git a/src/WebServer_JSON.ino b/src/WebServer_JSON.ino index ed0f5df026..aa4551576b 100644 --- a/src/WebServer_JSON.ino +++ b/src/WebServer_JSON.ino @@ -287,7 +287,9 @@ void stream_json_end_object_element(bool isLast) { void handle_timingstats_json() { TXBuffer.startJsonStream(); TXBuffer += '{'; + #ifdef USES_TIMING_STATS jsonStatistics(false); + #endif TXBuffer += '}'; TXBuffer.endStream(); } diff --git a/src/define_plugin_sets.h b/src/define_plugin_sets.h index 35e2e9066c..c0304920c1 100644 --- a/src/define_plugin_sets.h +++ b/src/define_plugin_sets.h @@ -110,8 +110,13 @@ To create/register a plugin, you have to : #ifndef USES_SSDP #define USES_SSDP #endif + #ifndef USES_TIMING_STATS + #define USES_TIMING_STATS + #endif #endif + + #ifdef MEMORY_ANALYSIS #ifdef MQTT_ONLY #define USES_C002 // Domoticz MQTT @@ -237,6 +242,9 @@ To create/register a plugin, you have to : #undef USE_SETTINGS_ARCHIVE #endif // USE_SETTINGS_ARCHIVE + #ifdef USES_TIMING_STATS + #undef USES_TIMING_STATS + #endif #ifndef USES_P001 #define USES_P001 // switch @@ -981,4 +989,17 @@ To create/register a plugin, you have to : #endif #endif + + +// Timing stats page needs timing stats +#if defined(WEBSERVER_TIMINGSTATS) && !defined(USES_TIMING_STATS) + #define USES_TIMING_STATS +#endif + +// If timing stats page is not included, there is no need in collecting the stats +#if !defined(WEBSERVER_TIMINGSTATS) && defined(USES_TIMING_STATS) + #undef USES_TIMING_STATS +#endif + + #endif // DEFINE_PLUGIN_SETS_H \ No newline at end of file diff --git a/src/src/DataStructs/TimingStats.cpp b/src/src/DataStructs/TimingStats.cpp index ffcd426f0b..5263a17695 100644 --- a/src/src/DataStructs/TimingStats.cpp +++ b/src/src/DataStructs/TimingStats.cpp @@ -3,6 +3,7 @@ #include "../../ESPEasy_plugindefs.h" #include "../../_CPlugin_Helper.h" +#ifdef USES_TIMING_STATS std::map pluginStats; @@ -225,3 +226,5 @@ String getMiscStatsName(int stat) { } return getUnknownString(); } + +#endif \ No newline at end of file diff --git a/src/src/DataStructs/TimingStats.h b/src/src/DataStructs/TimingStats.h index c1b218d8ce..b19171b513 100644 --- a/src/src/DataStructs/TimingStats.h +++ b/src/src/DataStructs/TimingStats.h @@ -1,8 +1,12 @@ #ifndef DATASTRUCTS_TIMINGSTATS_H #define DATASTRUCTS_TIMINGSTATS_H -#include -#include +#include "../../define_plugin_sets.h" + +#ifdef USES_TIMING_STATS + +# include +# include /*********************************************************************************************\ @@ -10,60 +14,60 @@ \*********************************************************************************************/ -#define LOADFILE_STATS 0 -#define SAVEFILE_STATS 1 -#define LOOP_STATS 2 -#define PLUGIN_CALL_50PS 3 -#define PLUGIN_CALL_10PS 4 -#define PLUGIN_CALL_10PSU 5 -#define PLUGIN_CALL_1PS 6 -#define SENSOR_SEND_TASK 7 -#define SEND_DATA_STATS 8 -#define COMPUTE_FORMULA_STATS 9 -#define PROC_SYS_TIMER 10 -#define SET_NEW_TIMER 11 -#define TIME_DIFF_COMPUTE 12 -#define MQTT_DELAY_QUEUE 13 -#define C001_DELAY_QUEUE 14 -#define C002_DELAY_QUEUE 15 -#define C003_DELAY_QUEUE 16 -#define C004_DELAY_QUEUE 17 -#define C005_DELAY_QUEUE 18 -#define C006_DELAY_QUEUE 19 -#define C007_DELAY_QUEUE 20 -#define C008_DELAY_QUEUE 21 -#define C009_DELAY_QUEUE 22 -#define C010_DELAY_QUEUE 23 -#define C011_DELAY_QUEUE 24 -#define C012_DELAY_QUEUE 25 -#define C013_DELAY_QUEUE 26 -#define C014_DELAY_QUEUE 27 -#define C015_DELAY_QUEUE 28 -#define C016_DELAY_QUEUE 29 -#define C017_DELAY_QUEUE 30 -#define C018_DELAY_QUEUE 31 -#define C019_DELAY_QUEUE 32 -#define C020_DELAY_QUEUE 33 -#define TRY_CONNECT_HOST_TCP 34 -#define TRY_CONNECT_HOST_UDP 35 -#define HOST_BY_NAME_STATS 36 -#define CONNECT_CLIENT_STATS 37 -#define LOAD_CUSTOM_TASK_STATS 38 -#define WIFI_ISCONNECTED_STATS 39 -#define WIFI_NOTCONNECTED_STATS 40 -#define LOAD_TASK_SETTINGS 41 -#define TRY_OPEN_FILE 42 -#define SPIFFS_GC_SUCCESS 43 -#define SPIFFS_GC_FAIL 44 -#define PARSE_SYSVAR 45 -#define PARSE_SYSVAR_NOCHANGE 46 -#define PARSE_TEMPLATE 47 -#define RULES_PROCESSING 48 -#define GRAT_ARP_STATS 49 -#define BACKGROUND_TASKS 50 -#define HANDLE_SCHEDULER_IDLE 51 -#define HANDLE_SCHEDULER_TASK 52 -#define HANDLE_SERVING_WEBPAGE 53 +# define LOADFILE_STATS 0 +# define SAVEFILE_STATS 1 +# define LOOP_STATS 2 +# define PLUGIN_CALL_50PS 3 +# define PLUGIN_CALL_10PS 4 +# define PLUGIN_CALL_10PSU 5 +# define PLUGIN_CALL_1PS 6 +# define SENSOR_SEND_TASK 7 +# define SEND_DATA_STATS 8 +# define COMPUTE_FORMULA_STATS 9 +# define PROC_SYS_TIMER 10 +# define SET_NEW_TIMER 11 +# define TIME_DIFF_COMPUTE 12 +# define MQTT_DELAY_QUEUE 13 +# define C001_DELAY_QUEUE 14 +# define C002_DELAY_QUEUE 15 +# define C003_DELAY_QUEUE 16 +# define C004_DELAY_QUEUE 17 +# define C005_DELAY_QUEUE 18 +# define C006_DELAY_QUEUE 19 +# define C007_DELAY_QUEUE 20 +# define C008_DELAY_QUEUE 21 +# define C009_DELAY_QUEUE 22 +# define C010_DELAY_QUEUE 23 +# define C011_DELAY_QUEUE 24 +# define C012_DELAY_QUEUE 25 +# define C013_DELAY_QUEUE 26 +# define C014_DELAY_QUEUE 27 +# define C015_DELAY_QUEUE 28 +# define C016_DELAY_QUEUE 29 +# define C017_DELAY_QUEUE 30 +# define C018_DELAY_QUEUE 31 +# define C019_DELAY_QUEUE 32 +# define C020_DELAY_QUEUE 33 +# define TRY_CONNECT_HOST_TCP 34 +# define TRY_CONNECT_HOST_UDP 35 +# define HOST_BY_NAME_STATS 36 +# define CONNECT_CLIENT_STATS 37 +# define LOAD_CUSTOM_TASK_STATS 38 +# define WIFI_ISCONNECTED_STATS 39 +# define WIFI_NOTCONNECTED_STATS 40 +# define LOAD_TASK_SETTINGS 41 +# define TRY_OPEN_FILE 42 +# define SPIFFS_GC_SUCCESS 43 +# define SPIFFS_GC_FAIL 44 +# define PARSE_SYSVAR 45 +# define PARSE_SYSVAR_NOCHANGE 46 +# define PARSE_TEMPLATE 47 +# define RULES_PROCESSING 48 +# define GRAT_ARP_STATS 49 +# define BACKGROUND_TASKS 50 +# define HANDLE_SCHEDULER_IDLE 51 +# define HANDLE_SCHEDULER_TASK 52 +# define HANDLE_SERVING_WEBPAGE 53 class TimingStats { public: @@ -99,13 +103,28 @@ extern std::map controllerStats; extern std::map miscStats; extern unsigned long timingstats_last_reset; -#define START_TIMER const unsigned statisticsTimerStart(micros()); -#define STOP_TIMER_TASK(T, F) \ +# define START_TIMER const unsigned statisticsTimerStart(micros()); +# define STOP_TIMER_TASK(T, F) \ if (mustLogFunction(F)) pluginStats[(T) * 256 + (F)].add(usecPassedSince(statisticsTimerStart)); -#define STOP_TIMER_CONTROLLER(T, F) \ +# define STOP_TIMER_CONTROLLER(T, F) \ if (mustLogCFunction(F)) controllerStats[(T) * 256 + (F)].add(usecPassedSince(statisticsTimerStart)); // #define STOP_TIMER_LOADFILE miscStats[LOADFILE_STATS].add(usecPassedSince(statisticsTimerStart)); -#define STOP_TIMER(L) miscStats[L].add(usecPassedSince(statisticsTimerStart)); +# define STOP_TIMER(L) miscStats[L].add(usecPassedSince(statisticsTimerStart)); + +#else // ifdef USES_TIMING_STATS + +# define START_TIMER +# define STOP_TIMER_TASK(T, F) ; +# define STOP_TIMER_CONTROLLER(T, F) ; +# define STOP_TIMER(L) ; + + +// FIXME TD-er: This class is used as a parameter in functions defined in .ino files. +// The Arduino build process tries to forward declare all functions it can find, regardless of defines. +// Meaning we must make sure the forward declaration of the TimingStats class is made, since it is used as an argument in some function. +class TimingStats; + +#endif // ifdef USES_TIMING_STATS #endif // DATASTRUCTS_TIMINGSTATS_H