From 8331449ab93857a64656060b2649e682f179c0e3 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 4 Mar 2019 12:03:18 -0500 Subject: [PATCH] update profiles on path build --- llarp/path/pathbuilder.cpp | 4 +++- llarp/profiling.cpp | 38 ++++++++++++++++++++++++++++++++++++++ llarp/profiling.hpp | 12 ++++++++++++ llarp/router/router.cpp | 2 ++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index bb42c68fb..562b04ef7 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -210,7 +210,7 @@ namespace llarp return router->NumberOfConnectedRouters() && router->GetRandomConnectedRouter(cur); - size_t tries = 5; + size_t tries = 10; do { --tries; @@ -326,6 +326,7 @@ namespace llarp Builder::HandlePathBuilt(Path* p) { buildIntervalLimit = MIN_PATH_BUILD_INTERVAL; + router->routerProfiling().MarkPathSuccess(p); PathSet::HandlePathBuilt(p); } @@ -336,6 +337,7 @@ namespace llarp static constexpr llarp_time_t MaxBuildInterval = 30 * 1000; buildIntervalLimit = std::max(1000 + buildIntervalLimit, MaxBuildInterval); + router->routerProfiling().MarkPathFail(p); PathSet::HandlePathBuildTimeout(p); } diff --git a/llarp/profiling.cpp b/llarp/profiling.cpp index baee91d75..c81ec26c4 100644 --- a/llarp/profiling.cpp +++ b/llarp/profiling.cpp @@ -18,6 +18,8 @@ namespace llarp return false; if(!BEncodeWriteDictInt("t", connectTimeoutCount, buf)) return false; + if(!BEncodeWriteDictInt("u", lastUpdated, buf)) + return false; if(!BEncodeWriteDictInt("v", version, buf)) return false; @@ -32,6 +34,8 @@ namespace llarp return false; if(!BEncodeMaybeReadDictInt("t", connectTimeoutCount, read, k, buf)) return false; + if(!BEncodeMaybeReadDictInt("u", lastUpdated, read, k, buf)) + return false; if(!BEncodeMaybeReadDictInt("v", version, read, k, buf)) return false; if(!BEncodeMaybeReadDictInt("s", pathFailCount, read, k, buf)) @@ -41,6 +45,28 @@ namespace llarp return read; } + void + RouterProfile::Clear() + { + connectGoodCount = 0; + connectTimeoutCount = 0; + pathSuccessCount = 0; + pathFailCount = 0; + lastUpdated = llarp::time_now_ms(); + } + + void + RouterProfile::Tick() + { + // 10 minutes + static constexpr llarp_time_t updateInterval = DEFAULT_PATH_LIFETIME; + auto now = llarp::time_now_ms(); + if(lastUpdated < now && now - lastUpdated > updateInterval) + { + Clear(); + } + } + bool RouterProfile::IsGood(uint64_t chances) const { @@ -59,11 +85,20 @@ namespace llarp return !itr->second.IsGood(chances); } + void + Profiling::Tick() + { + lock_t lock(m_ProfilesMutex); + std::for_each(m_Profiles.begin(), m_Profiles.end(), + [](auto& item) { item.second.Tick(); }); + } + void Profiling::MarkTimeout(const RouterID& r) { lock_t lock(m_ProfilesMutex); m_Profiles[r].connectTimeoutCount += 1; + m_Profiles[r].lastUpdated = llarp::time_now_ms(); } void @@ -71,6 +106,7 @@ namespace llarp { lock_t lock(m_ProfilesMutex); m_Profiles[r].connectGoodCount += 1; + m_Profiles[r].lastUpdated = llarp::time_now_ms(); } void @@ -81,6 +117,7 @@ namespace llarp { // TODO: also mark bad? m_Profiles[hop.rc.pubkey].pathFailCount += 1; + m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms(); } } @@ -91,6 +128,7 @@ namespace llarp for(const auto& hop : p->hops) { m_Profiles[hop.rc.pubkey].pathSuccessCount += 1; + m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms(); } } diff --git a/llarp/profiling.hpp b/llarp/profiling.hpp index d926c32f0..2022774a1 100644 --- a/llarp/profiling.hpp +++ b/llarp/profiling.hpp @@ -17,6 +17,7 @@ namespace llarp uint64_t connectGoodCount = 0; uint64_t pathSuccessCount = 0; uint64_t pathFailCount = 0; + llarp_time_t lastUpdated = 0; RouterProfile() : IBEncodeMessage(){}; @@ -30,6 +31,14 @@ namespace llarp bool IsGood(uint64_t chances) const; + + /// clear stats + void + Clear(); + + // rotate stats if timeout reached + void + Tick(); }; struct Profiling final : public IBEncodeMessage @@ -51,6 +60,9 @@ namespace llarp void MarkTimeout(const RouterID& r); + void + Tick(); + bool BEncode(llarp_buffer_t* buf) const override; diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index f3a52711b..57f881be5 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -1021,6 +1021,8 @@ namespace llarp // LogDebug("tick router"); auto now = Now(); + routerProfiling().Tick(); + if(_rc.ExpiresSoon(now, randint() % 10000)) { LogInfo("regenerating RC");