From 22b1066b0a880083d447ca9101aa600018a58c37 Mon Sep 17 00:00:00 2001 From: weko Date: Wed, 1 Feb 2023 14:06:28 +0300 Subject: [PATCH 1/2] Add parameter for show TCSR with old algorithm and it's realization --- daemon/HTTPServer.cpp | 5 +++++ libi2pd/Config.cpp | 1 + libi2pd/Tunnel.cpp | 6 +++++- libi2pd/Tunnel.h | 9 ++++++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/daemon/HTTPServer.cpp b/daemon/HTTPServer.cpp index d9daeb5e..5095bcd7 100644 --- a/daemon/HTTPServer.cpp +++ b/daemon/HTTPServer.cpp @@ -289,6 +289,11 @@ namespace http { if (family.length () > 0) s << ""<< tr("Family") << ": " << family << "
\r\n"; s << "" << tr("Tunnel creation success rate") << ": " << i2p::tunnel::tunnels.GetTunnelCreationSuccessRate () << "%
\r\n"; + bool isOldTCSR; + i2p::config::GetOption("http.old_tcsr", isOldTCSR); + if (isOldTCSR) { + s << "" << tr("Tunnel creation success rate (old algorithm)") << ": " << i2p::tunnel::tunnels.OldGetTunnelCreationSuccessRate() << "%
\r\n"; + } s << "" << tr("Received") << ": "; ShowTraffic (s, i2p::transport::transports.GetTotalReceivedBytes ()); s << " (" << tr(/* tr: Kibibyte/s */ "%.2f KiB/s", (double) i2p::transport::transports.GetInBandwidth15s () / 1024) << ")
\r\n"; diff --git a/libi2pd/Config.cpp b/libi2pd/Config.cpp index 07ab2f20..9181e28e 100644 --- a/libi2pd/Config.cpp +++ b/libi2pd/Config.cpp @@ -95,6 +95,7 @@ namespace config { ("http.hostname", value()->default_value("localhost"), "Expected hostname for WebUI") ("http.webroot", value()->default_value("/"), "WebUI root path (default: / )") ("http.lang", value()->default_value("english"), "WebUI language (default: english )") + ("http.old_tcsr", value()->default_value(false), "Show TCSR with old algorithm (default: false)") ; options_description httpproxy("HTTP Proxy options"); diff --git a/libi2pd/Tunnel.cpp b/libi2pd/Tunnel.cpp index 4d205544..d19649d6 100644 --- a/libi2pd/Tunnel.cpp +++ b/libi2pd/Tunnel.cpp @@ -332,7 +332,8 @@ namespace tunnel Tunnels tunnels; Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr), - m_TunnelCreationSuccessRate (TCSR_START_VALUE), m_TunnelCreationAttemptsNum(0) { + m_TunnelCreationSuccessRate (TCSR_START_VALUE), m_TunnelCreationAttemptsNum(0), + m_OldNumSuccesiveTunnelCreations (0), m_OldNumFailedTunnelCreations (0) { } Tunnels::~Tunnels () @@ -634,6 +635,7 @@ namespace tunnel // delete it = pendingTunnels.erase (it); FailedTunnelCreation(); + m_OldNumFailedTunnelCreations++; } else ++it; @@ -642,6 +644,7 @@ namespace tunnel LogPrint (eLogDebug, "Tunnel: Pending build request ", it->first, " failed, deleted"); it = pendingTunnels.erase (it); FailedTunnelCreation(); + m_OldNumFailedTunnelCreations++; break; case eTunnelStateBuildReplyReceived: // intermediate state, will be either established of build failed @@ -651,6 +654,7 @@ namespace tunnel // success it = pendingTunnels.erase (it); SuccesiveTunnelCreation(); + m_OldNumSuccesiveTunnelCreations++; } } } diff --git a/libi2pd/Tunnel.h b/libi2pd/Tunnel.h index 7fa1eb54..a997502a 100644 --- a/libi2pd/Tunnel.h +++ b/libi2pd/Tunnel.h @@ -269,7 +269,9 @@ namespace tunnel i2p::util::Queue > m_Queue; i2p::util::MemoryPoolMt > m_I2NPTunnelEndpointMessagesMemoryPool; i2p::util::MemoryPoolMt > m_I2NPTunnelMessagesMemoryPool; - + // some old stats + int m_OldNumSuccesiveTunnelCreations, m_OldNumFailedTunnelCreations; + // Calculating of tunnel creation success rate // A modified version of the EWMA algorithm, where alpha is increased at the beginning to accelerate similarity void SuccesiveTunnelCreation() { @@ -297,6 +299,11 @@ namespace tunnel int GetQueueSize () { return m_Queue.GetSize (); }; int GetTunnelCreationSuccessRate () const { return std::round(m_TunnelCreationSuccessRate * 100); } // in percents + int OldGetTunnelCreationSuccessRate () const // in percents + { + int totalNum = m_OldNumSuccesiveTunnelCreations + m_OldNumFailedTunnelCreations; + return totalNum ? m_OldNumSuccesiveTunnelCreations*100/totalNum : 0; + } }; extern Tunnels tunnels; From 7df2ed611470f105669a5daf668fc5ce08f9a116 Mon Sep 17 00:00:00 2001 From: weko Date: Wed, 1 Feb 2023 19:14:56 +0300 Subject: [PATCH 2/2] rename and refactor --- daemon/HTTPServer.cpp | 8 ++++---- libi2pd/Config.cpp | 2 +- libi2pd/Tunnel.cpp | 5 +---- libi2pd/Tunnel.h | 16 ++++++++++------ 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/daemon/HTTPServer.cpp b/daemon/HTTPServer.cpp index 5095bcd7..8706f786 100644 --- a/daemon/HTTPServer.cpp +++ b/daemon/HTTPServer.cpp @@ -289,10 +289,10 @@ namespace http { if (family.length () > 0) s << ""<< tr("Family") << ": " << family << "
\r\n"; s << "" << tr("Tunnel creation success rate") << ": " << i2p::tunnel::tunnels.GetTunnelCreationSuccessRate () << "%
\r\n"; - bool isOldTCSR; - i2p::config::GetOption("http.old_tcsr", isOldTCSR); - if (isOldTCSR) { - s << "" << tr("Tunnel creation success rate (old algorithm)") << ": " << i2p::tunnel::tunnels.OldGetTunnelCreationSuccessRate() << "%
\r\n"; + bool isTotalTCSR; + i2p::config::GetOption("http.showTotalTCSR", isTotalTCSR); + if (isTotalTCSR) { + s << "" << tr("Total tunnel creation success rate") << ": " << i2p::tunnel::tunnels.GetTotalTunnelCreationSuccessRate() << "%
\r\n"; } s << "" << tr("Received") << ": "; ShowTraffic (s, i2p::transport::transports.GetTotalReceivedBytes ()); diff --git a/libi2pd/Config.cpp b/libi2pd/Config.cpp index 9181e28e..971b1273 100644 --- a/libi2pd/Config.cpp +++ b/libi2pd/Config.cpp @@ -95,7 +95,7 @@ namespace config { ("http.hostname", value()->default_value("localhost"), "Expected hostname for WebUI") ("http.webroot", value()->default_value("/"), "WebUI root path (default: / )") ("http.lang", value()->default_value("english"), "WebUI language (default: english )") - ("http.old_tcsr", value()->default_value(false), "Show TCSR with old algorithm (default: false)") + ("http.showTotalTCSR", value()->default_value(false), "Show additional value with total TCSR since router's start (default: false)") ; options_description httpproxy("HTTP Proxy options"); diff --git a/libi2pd/Tunnel.cpp b/libi2pd/Tunnel.cpp index d19649d6..a0e36978 100644 --- a/libi2pd/Tunnel.cpp +++ b/libi2pd/Tunnel.cpp @@ -333,7 +333,7 @@ namespace tunnel Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr), m_TunnelCreationSuccessRate (TCSR_START_VALUE), m_TunnelCreationAttemptsNum(0), - m_OldNumSuccesiveTunnelCreations (0), m_OldNumFailedTunnelCreations (0) { + m_TotalNumSuccesiveTunnelCreations (0), m_TotalNumFailedTunnelCreations (0) { // for normal avarage } Tunnels::~Tunnels () @@ -635,7 +635,6 @@ namespace tunnel // delete it = pendingTunnels.erase (it); FailedTunnelCreation(); - m_OldNumFailedTunnelCreations++; } else ++it; @@ -644,7 +643,6 @@ namespace tunnel LogPrint (eLogDebug, "Tunnel: Pending build request ", it->first, " failed, deleted"); it = pendingTunnels.erase (it); FailedTunnelCreation(); - m_OldNumFailedTunnelCreations++; break; case eTunnelStateBuildReplyReceived: // intermediate state, will be either established of build failed @@ -654,7 +652,6 @@ namespace tunnel // success it = pendingTunnels.erase (it); SuccesiveTunnelCreation(); - m_OldNumSuccesiveTunnelCreations++; } } } diff --git a/libi2pd/Tunnel.h b/libi2pd/Tunnel.h index a997502a..03693206 100644 --- a/libi2pd/Tunnel.h +++ b/libi2pd/Tunnel.h @@ -269,17 +269,21 @@ namespace tunnel i2p::util::Queue > m_Queue; i2p::util::MemoryPoolMt > m_I2NPTunnelEndpointMessagesMemoryPool; i2p::util::MemoryPoolMt > m_I2NPTunnelMessagesMemoryPool; - // some old stats - int m_OldNumSuccesiveTunnelCreations, m_OldNumFailedTunnelCreations; + // count of tunnels for total TCSR algorithm + int m_TotalNumSuccesiveTunnelCreations, m_TotalNumFailedTunnelCreations; // Calculating of tunnel creation success rate - // A modified version of the EWMA algorithm, where alpha is increased at the beginning to accelerate similarity void SuccesiveTunnelCreation() { + // total TCSR + m_TotalNumSuccesiveTunnelCreations++; + // A modified version of the EWMA algorithm, where alpha is increased at the beginning to accelerate similarity double alpha = TCSR_SMOOTHING_CONSTANT + (1 - TCSR_SMOOTHING_CONSTANT)/++m_TunnelCreationAttemptsNum; m_TunnelCreationSuccessRate = alpha * 1 + (1 - alpha) * m_TunnelCreationSuccessRate; }; void FailedTunnelCreation() { + m_TotalNumFailedTunnelCreations++; + double alpha = TCSR_SMOOTHING_CONSTANT + (1 - TCSR_SMOOTHING_CONSTANT)/++m_TunnelCreationAttemptsNum; m_TunnelCreationSuccessRate = alpha * 0 + (1 - alpha) * m_TunnelCreationSuccessRate; }; @@ -299,10 +303,10 @@ namespace tunnel int GetQueueSize () { return m_Queue.GetSize (); }; int GetTunnelCreationSuccessRate () const { return std::round(m_TunnelCreationSuccessRate * 100); } // in percents - int OldGetTunnelCreationSuccessRate () const // in percents + int GetTotalTunnelCreationSuccessRate () const // in percents { - int totalNum = m_OldNumSuccesiveTunnelCreations + m_OldNumFailedTunnelCreations; - return totalNum ? m_OldNumSuccesiveTunnelCreations*100/totalNum : 0; + int totalNum = m_TotalNumSuccesiveTunnelCreations + m_TotalNumFailedTunnelCreations; + return totalNum ? m_TotalNumSuccesiveTunnelCreations*100/totalNum : 0; } };