Added standby mode to reduce idling cpu load to near 0%

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-11-15 22:30:50 +01:00
parent 611e08340d
commit 486064cef3
15 changed files with 74 additions and 5 deletions

View File

@ -80,6 +80,7 @@ void Framework::Update()
httpServer->Update(); httpServer->Update();
DownloadManager::Update(); DownloadManager::Update();
LogHistory::Update(); LogHistory::Update();
Idler::Update();
return; return;
} }

View File

@ -4,6 +4,7 @@
#include "HttpServer.h" #include "HttpServer.h"
#include "DownloadManager.h" #include "DownloadManager.h"
#include "ConsoleManager.h" #include "ConsoleManager.h"
#include "Idler.h"
#include "XGControl.h" #include "XGControl.h"
#include "XGConfig.h" #include "XGConfig.h"

View File

@ -78,6 +78,8 @@ void HttpServer::EventHandler(mg_connection* pNc, int ev, void* p)
switch (ev) switch (ev)
{ {
case MG_EV_HTTP_REQUEST: case MG_EV_HTTP_REQUEST:
// Reset standby timer
XGControl::last_query_time = time(0);
http_message* hpm = (http_message*)p; http_message* hpm = (http_message*)p;
std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len); std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len);
@ -224,7 +226,7 @@ bool HttpServer::IsConnectionAllowed(std::string peer_address, std::string& deni
} }
// Whitelist is enabled, but peer is NOT whitelisted // Whitelist is enabled, but peer is NOT whitelisted
denialReason = "Not whitelisted!"; denialReason = std::string("Not whitelisted! Ask your tubio administrator to whitelist '") + peer_address + "' in order to gain access.";
return false; return false;
} }
else // Whitelist is NOT enabled and only_allow_localhost is FALSE! else // Whitelist is NOT enabled and only_allow_localhost is FALSE!

21
Tubio/Idler.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "Idler.h"
#include <iostream>
#define TIME_TO_FALL_ASLEEP MINUTES(5)
#define SLEEP_TIME SECONDS(3)
void Idler::Update()
{
// Has no request been made within 3 minutes?
if (time(0) - XGControl::last_query_time > TIME_TO_FALL_ASLEEP)
{
// Let the processor chill for a second.
// This should reduce the idling-cpu load to near 0%
#ifdef _WIN
std::cout << "Sleeping..." << std::endl;
Sleep(SLEEP_TIME * 1000); // Uses milliseconds
std::cout << "Waking..." << std::endl;
#else
usleep(SLEEP_TIME * 10000000); // Uses microseconds
#endif
}
}

15
Tubio/Idler.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include "XGControl.h"
#ifdef _WIN
#include <Windows.h>
#else
#include <sys/wait.h>
#include <unistd.h>
#endif
class Idler
{
public:
static void Update();
};

View File

@ -124,7 +124,7 @@ bool RestQueryHandler::FetchSessionCache(const JsonBlock& request, JsonBlock& re
#ifdef _WIN #ifdef _WIN
max_age = min(request["max_age"].AsInt, max_age); max_age = min(request["max_age"].AsInt, max_age);
#else #else
max_age = std::min(request["max_age"].AsInt, max_age); max_age = std::min<long int>(request["max_age"].AsInt, max_age);
#endif #endif
} }
} }

11
Tubio/TimeUnits.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
/* These are APPROXIMATIONS */
/* UNITS ARE SECONDS */
#define SECONDS(x) (x)
#define MINUTES(x) (SECONDS(60) * x)
#define HOURS(x) (MINUTES(60) * x)
#define DAYS(x) (HOURS(24) * x)
#define WEEKS(x) (DAYS(7) * x)
#define MONTHS(x) (WEEKS(4) * x)
#define YEARS(x) (MONTHS(12) * x)

View File

@ -153,6 +153,7 @@
<ClCompile Include="external_dependencies\leonetienne\JasonPP\JasonPP.cpp" /> <ClCompile Include="external_dependencies\leonetienne\JasonPP\JasonPP.cpp" />
<ClCompile Include="FileSystem.cpp" /> <ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Framework.cpp" /> <ClCompile Include="Framework.cpp" />
<ClCompile Include="Idler.cpp" />
<ClCompile Include="Logger.cpp" /> <ClCompile Include="Logger.cpp" />
<ClCompile Include="LogHistory.cpp" /> <ClCompile Include="LogHistory.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
@ -170,12 +171,14 @@
<ClInclude Include="external_dependencies\leonetienne\JasonPP\JasonPP.hpp" /> <ClInclude Include="external_dependencies\leonetienne\JasonPP\JasonPP.hpp" />
<ClInclude Include="FileSystem.h" /> <ClInclude Include="FileSystem.h" />
<ClInclude Include="Framework.h" /> <ClInclude Include="Framework.h" />
<ClInclude Include="Idler.h" />
<ClInclude Include="Logger.h" /> <ClInclude Include="Logger.h" />
<ClInclude Include="LogHistory.h" /> <ClInclude Include="LogHistory.h" />
<ClInclude Include="LogTypes.h" /> <ClInclude Include="LogTypes.h" />
<ClInclude Include="RestQueryHandler.h" /> <ClInclude Include="RestQueryHandler.h" />
<ClInclude Include="RestResponseTemplates.h" /> <ClInclude Include="RestResponseTemplates.h" />
<ClInclude Include="HttpServer.h" /> <ClInclude Include="HttpServer.h" />
<ClInclude Include="TimeUnits.h" />
<ClInclude Include="Updater.h" /> <ClInclude Include="Updater.h" />
<ClInclude Include="XGConfig.h" /> <ClInclude Include="XGConfig.h" />
<ClInclude Include="XGControl.h" /> <ClInclude Include="XGControl.h" />

View File

@ -63,6 +63,9 @@
<ClCompile Include="Updater.cpp"> <ClCompile Include="Updater.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Idler.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Logger.h"> <ClInclude Include="Logger.h">
@ -110,5 +113,11 @@
<ClInclude Include="Updater.h"> <ClInclude Include="Updater.h">
<Filter>Headerdateien</Filter> <Filter>Headerdateien</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Idler.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="TimeUnits.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -14,6 +14,6 @@ std::string Updater::UpdateYoutubeDL()
return "error code: 0x" + (JasonPP::Internal::Helpers::Base10_2_X(res, "0123456789abcdef")); return "error code: 0x" + (JasonPP::Internal::Helpers::Base10_2_X(res, "0123456789abcdef"));
} }
#else #else
#endif
return "not implemented"; return "not implemented";
#endif
} }

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#ifdef _WIN
#include <string> #include <string>
#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers
#ifdef _WIN
#include <urlmon.h> #include <urlmon.h>
#include <Windows.h> #include <Windows.h>
#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers
#endif #endif
class Updater class Updater

View File

@ -2,3 +2,4 @@
bool XGControl::keepServerRunning = false; bool XGControl::keepServerRunning = false;
time_t XGControl::boot_time = time(0); time_t XGControl::boot_time = time(0);
time_t XGControl::last_query_time = time(0) - DAYS(1); // A day before to prevent it from "waking up" at boot

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <ctime> #include <ctime>
#include "TimeUnits.h"
/// <summary> /// <summary>
/// Class to house control variables /// Class to house control variables
@ -10,5 +11,6 @@ class XGControl
public: public:
static bool keepServerRunning; static bool keepServerRunning;
static time_t boot_time; static time_t boot_time;
static time_t last_query_time;
}; };

View File

@ -18,6 +18,8 @@ g++ \
../Tubio/RestResponseTemplates.cpp \ ../Tubio/RestResponseTemplates.cpp \
../Tubio/XGConfig.cpp \ ../Tubio/XGConfig.cpp \
../Tubio/XGControl.cpp \ ../Tubio/XGControl.cpp \
../Tubio/Updater.cpp \
../Tubio/Idler.cpp \
\ \
\ \
../Tubio/external_dependencies/casenta/mongoose/mongoose.c \ ../Tubio/external_dependencies/casenta/mongoose/mongoose.c \

Binary file not shown.